Recipes and troubleshooting
Patterns for running a data agent on Vuon in production, and fixes for common problems. Start with the quickstart if your bot isn't connected yet.
Why one client per conversation
The quickstart opens a client for each conversation and closes it afterwards, with the conversation's headers fixed. Vuon's MCP servers keep no session: each request stands alone, so a client costs one handshake and leaves nothing behind. The pattern works the same in a long-running server, a container, or a serverless function, and one person's headers can't leak into another's conversation.
A long-lived client
If your bot must keep one client for its whole life, add the session id and end user to each request with a hook, never as fixed headers. The Vercel AI SDK and the MCP TypeScript SDK support this through a custom fetch; most harnesses, including the Pi and OpenCode CLIs, don't. Carry the current conversation into the hook with AsyncLocalStorage:
Long-lived client (Vercel AI SDK)
import { AsyncLocalStorage } from 'node:async_hooks';
import { createMCPClient } from '@ai-sdk/mcp';
import { generateText, isStepCount } from 'ai';
const conversation = new AsyncLocalStorage<{
endUser: string;
sessionId: string;
}>();
const withConversation = (input: RequestInfo | URL, init?: RequestInit) => {
const headers = new Headers(init?.headers);
const current = conversation.getStore();
if (current) {
headers.set('X-Vuon-End-User', current.endUser);
headers.set('X-Vuon-Session-Id', current.sessionId);
}
return fetch(input, { ...init, headers });
};
const vuon = await createMCPClient({
transport: {
type: 'http',
url: `${process.env.VUON_URL}/api/mcp`,
headers: { Authorization: `Bearer ${process.env.VUON_API_KEY}` },
fetch: withConversation,
},
});
const tools = await vuon.tools();
export function answer(slackUserId: string, threadTs: string, question: string) {
const current = { endUser: `slack:${slackUserId}`, sessionId: threadTs };
return conversation.run(current, () =>
generateText({ model, tools, stopWhen: isStepCount(10), prompt: question }),
);
}
Create a second client the same way for /api/mcp/bigquery. Requests made outside any conversation.run, such as the handshake and the tool list at startup, carry no end user. That is why the quickstart's pattern is safer for the BigQuery server: nothing can run without one.
With the MCP TypeScript SDK, pass the same fetch to StreamableHTTPClientTransport, or use the SDK's createMiddleware.
Stream an answer
With the AI SDK's streamText, the function returns before the stream finishes, so close the client from the stream's callbacks instead of a finally. onEnd runs when the response is complete, onError when it fails, and onAbort when it is cancelled. A second close() is harmless.
Stream an answer
import { isStepCount, streamText } from 'ai';
import { connectVuon } from './vuon.ts'; // from the quickstart
export async function answerStreaming(
slackUserId: string,
threadTs: string,
question: string,
) {
const vuon = await connectVuon(`slack:${slackUserId}`, threadTs);
return streamText({
model, // your model
tools: vuon.tools,
stopWhen: isStepCount(10),
prompt: question,
onEnd: () => vuon.close(),
onError: () => vuon.close(),
onAbort: () => vuon.close(),
});
}
In a serverless function, make sure the client closes before the function is frozen. If the platform returns the response before the stream ends, keep the function alive with its waitUntil or equivalent.
Older SDK versions
- AI SDK. The quickstart is for
ai7 with@ai-sdk/mcp2.x. Onai6 with@ai-sdk/mcp1.x, usestepCountIsinstead ofisStepCount, andonFinishinstead ofonEnd. Thetransportoptions are the same. - MCP TypeScript SDK. In the original
@modelcontextprotocol/sdk1.x (checked against 1.32.2), import from@modelcontextprotocol/sdk/client/index.jsand@modelcontextprotocol/sdk/client/streamableHttp.js. The options are the same. - MCP Python SDK. On
mcp1.x, importhttpxinstead ofhttpx2.streamshas three items there; use the first two, as the quickstart does.
Run a CLI process per conversation
Pi and OpenCode can read Vuon's servers from a config file instead of code. Their headers are fixed when the process starts, so start one process per conversation, with the conversation's values in its environment. Fixed headers can't carry a verified end user for longer than five minutes.
Pi
Pi reads mcp.json from its agent directory (~/.pi/agent/, or PI_CODING_AGENT_DIR), and expands ${NAME} from the environment. Checked against Pi 1.3.0.
~/.pi/agent/mcp.json
{
"mcpServers": {
"vuon": {
"url": "https://<workspace>.vuon.ai/api/mcp",
"headers": {
"Authorization": "Bearer ${VUON_API_KEY}",
"X-Vuon-Session-Id": "${VUON_SESSION_ID}",
"X-Vuon-End-User": "${VUON_END_USER}"
}
},
"vuon-bigquery": {
"url": "https://<workspace>.vuon.ai/api/mcp/bigquery",
"headers": {
"Authorization": "Bearer ${VUON_API_KEY}",
"X-Vuon-Session-Id": "${VUON_SESSION_ID}",
"X-Vuon-End-User": "${VUON_END_USER}"
}
}
}
}
VUON_SESSION_ID="1794266886.801979" VUON_END_USER="slack:U0123ABC" \
pi --print "How many members do we have?"
Run pi mcp list with the variables set to check that both servers connect. Keep the file in the agent directory: Pi reads a project's .pi/mcp.json only in a trusted project, which an unattended bot has no one to approve. Pi calls MCP tools from codemode scripts by default; see Control tool exposure. The quickstart's Pi SDK example sets exposure: 'direct' so the model calls the tools itself.
OpenCode
OpenCode reads remote MCP servers from opencode.json, and expands {env:NAME} from the environment. Set oauth to false so it doesn't try to sign in. Checked against OpenCode 1.19.
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"vuon": {
"type": "remote",
"url": "https://<workspace>.vuon.ai/api/mcp",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:VUON_API_KEY}",
"X-Vuon-Session-Id": "{env:VUON_SESSION_ID}",
"X-Vuon-End-User": "{env:VUON_END_USER}"
}
},
"vuon-bigquery": {
"type": "remote",
"url": "https://<workspace>.vuon.ai/api/mcp/bigquery",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:VUON_API_KEY}",
"X-Vuon-Session-Id": "{env:VUON_SESSION_ID}",
"X-Vuon-End-User": "{env:VUON_END_USER}"
}
}
}
}
VUON_SESSION_ID="1794266886.801979" VUON_END_USER="slack:U0123ABC" \
opencode run "How many members do we have?"
Run opencode mcp list to check that both servers connect. OpenCode replaces an unset variable with an empty string, and Vuon ignores an empty header, so a missing variable fails quietly: check Query checks for a dash where the end user should be.
Other warehouses
Every example on Connect your data agent works on Snowflake, Databricks, and Redshift. Create the client on that warehouse's database, and change the governed query server's path from /api/mcp/bigquery to /api/mcp/snowflake, /api/mcp/databricks, or /api/mcp/redshift. Clients, client keys, tool switches, sessions, and end-user recording work the same way. See the Snowflake, Databricks, and Redshift guides.
On a warehouse without a governed query server, such as PostgreSQL or MySQL, a bot can use the Vuon server with a personal key from Settings → API Keys (Member role or above). That key acts as the person who created it and stops working when they leave. It also reaches more than a client key: every Vuon server tool, including create_metric, with no tool switches and no end-user recording. See API keys.
Troubleshooting
A request is refused with 401 or 403
A 401 means the key is missing, wrong, revoked, or expired, or the member it acts as was deactivated. A 403 that says a server is not enabled means Vuon hasn't enabled that server for your workspace. See also the Vuon server and BigQuery server troubleshooting.
A tool is missing
It is switched off on the client's page, or a client key can't reach it. See Tools and Scopes.
Project "…" is not available on this server
The call's projectId isn't your connection's project. Use the project the error names.
The end user is missing, wrong, or unverified
See Identify who asked.
A transform can't find an earlier query
The two servers saw different session ids, or none. Send the same X-Vuon-Session-Id to both servers for the whole conversation. See transform_results under Tools.
FAQ
Can the bot sign in with OAuth instead of a key?
OAuth signs in a person, which suits a person's own agent, such as Claude Code. A bot that runs unattended uses a client key.