How to Connect the Vercel AI SDK to MewCP
The AI SDK's MCP client turns MewCP's tools into AI SDK tools you pass to generateText or streamText. Your agent then reaches every app connected to its toolset through one HTTP connection.
Before you start
-
Node.js 18 or newer and a TypeScript project
-
A toolset and its key. See Choose a toolset
-
Your environment set as in Set your environment:
Code
Install
Code
The examples use an AI Gateway model string. Any model provider the AI SDK supports works the same way.
Connect
Code
Keep stopWhen. Without it, generateText and streamText stop after one step, so the agent finds a tool with search and never runs it.
Serve your users
With a project toolset, add x-mewcp-enduser-id to the transport headers, set to the user the agent is acting for. Create the client per request, so each user's calls carry their own ID:
Code
Use the same ID you pass as externalUserId when the user connects their accounts through the MewCP Auth API. If they have not connected the app a tool needs, the tool result carries a link for them to connect it, and the model can pass it on.
Run this on your server, for example in a route handler. The key must never reach the browser.
Check it works
List the tools before handing them to the model:
Code
Four tools, not the tools of every app, is expected. See What your agent sees.
In production
- Close the client. Close it when the response is done: in
finallyforgenerateText, or in theonEndcallback forstreamText. - Leave
maxRetriesoff. The client can retry failed tool calls, butcall_toolruns actions such as sending an email, and a retry can repeat them. - Approve risky calls. Pass a
toolApprovalpolicy togenerateTextorstreamTextif a person should confirm before the agent acts.
Troubleshooting
The agent searches for a tool and then stops
stopWhen is missing or too low. Set stopWhen: isStepCount(10) so the model can run search, get_schema and call_tool in one answer.
createMCPClient throws an MCPClientError with status 401 or 403
MewCP rejected the key.
- Check
MEWCP_API_KEYis set where the code runs, and the header readsBearerfollowed by the whole key. - Check the key matches the toolset in
MEWCP_MCP_URL: your API Key for the personal toolset, the project's key for a project toolset. - A
403that says the billing period is not active means your MewCP plan needs renewing in the dashboard.
Calls run against the wrong user's accounts
Each client sends the x-mewcp-enduser-id it was created with. Do not share one client across users: create it inside the request, as in Serve your users.
Last checked against the AI SDK's documentation on 13 October 2026.