createMCPClient converts MCP tools into AI SDK tools, so they drop straight into generateText or streamText.
Requirements
- A project with
aiand@ai-sdk/mcpinstalled. - A Sendmux key for the surface you are calling. Mailbox work needs an
smx_mbx_key or a scopedsmx_agent_token. - Python available in the same runtime if you use the local stdio connection.
Install
Connect over private HTTP
HTTP is the better fit for most AI SDK deployments, because serverless and edge runtimes usually cannot spawn a process. Start the server where your key can live:Connect over local stdio
Use this when your process can launch the server itself.Run the agent
Fetch the tools, pass them to the model, and close the client when the call finishes.app/triage.ts
onEnd so the connection does not outlive the response:
Spreading several tool sets into one object lets later sets override earlier
tools with the same name. Keep Sendmux in its own client, or namespace the
others, when you connect more than one MCP server.
Give each user their own mailbox
Mailbox-scoped keys are the isolation boundary. Create the client per request with that tenant’s key so the agent working one tenant structurally cannot read another tenant’s mail. Close it when the request ends.Sending stays gated
A durablesmx_agent_ token includes mailbox.read and email.receive, not email.send. After a named human owner accepts the invite and approves sending, exchange the durable token for a one-hour Sending-resource token and start the server with it as SENDMUX_SENDING_API_KEY.
Troubleshooting
tools() returns nothing
tools() returns nothing
Confirm the surfaces the server started with.
sendmux-mcp requires
SENDMUX_MCP_SURFACES; the single-surface entry points do not.The HTTP transport returns 401
The HTTP transport returns 401
Send
Authorization: Bearer <SENDMUX_MCP_HTTP_BEARER_TOKEN> in the
transport headers.The HTTP transport returns 403 origin_forbidden
The HTTP transport returns 403 origin_forbidden
Add the calling origin to
SENDMUX_MCP_ALLOWED_ORIGINS on the server.Connections build up under load
Connections build up under load
Close the client in
finally, or in onEnd when streaming. Every request
that opens a client has to close one.Next steps
Agent frameworks
Compare connections across frameworks.
MCP
Review environment variables and tool discovery.
Mailbox API guides
Work with threads and conversation state.
TypeScript SDK
Call Sendmux directly when you want full endpoint coverage.