Skip to main content
Use this page to connect the AI SDK to Sendmux. createMCPClient converts MCP tools into AI SDK tools, so they drop straight into generateText or streamText.
Replace placeholder keys and tokens before running any snippet. Do not commit smx_root_, smx_mbx_, or private HTTP bearer tokens to version control.

Requirements

  • A project with ai and @ai-sdk/mcp installed.
  • A Sendmux key for the surface you are calling. Mailbox work needs an smx_mbx_ key or a scoped smx_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:
Then connect and send the bearer token on every request:

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
When you stream, close the client in 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 durable smx_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

Confirm the surfaces the server started with. sendmux-mcp requires SENDMUX_MCP_SURFACES; the single-surface entry points do not.
Send Authorization: Bearer <SENDMUX_MCP_HTTP_BEARER_TOKEN> in the transport headers.
Add the calling origin to SENDMUX_MCP_ALLOWED_ORIGINS on the server.
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.