Give your Vercel AI SDK agent a phone number
With @ai-sdk/mcp, an AI SDK agent can load the AgentPhone tools in a few lines and text or call from inside generateText or streamText. Code for the client, the tools and the inbound webhook on Next.js.

The AI SDK is the TypeScript toolkit under a large share of production agents, and its MCP client means a hosted tool server is a dependency, not an integration project. Load the AgentPhone tools once, hand them to generateText or streamText, and the model can decide to send a text or place a call as part of answering. This page is the client, the agent loop and the webhook route that brings replies back in.
Install and connect
npm install ai @ai-sdk/mcp @ai-sdk/anthropic
import { createMCPClient } from '@ai-sdk/mcp';const agentphone = await createMCPClient({transport: {type: 'http',url: 'https://mcp.agentphone.ai/mcp',headers: { Authorization: `Bearer ${process.env.AGENTPHONE_API_KEY}` },},});const tools = await agentphone.tools();
The hosted server speaks Streamable HTTP, which is the transport the AI SDK docs recommend. Close the client when the request is done.
Use the tools in an agent
import { generateText, stepCountIs } from 'ai';import { anthropic } from '@ai-sdk/anthropic';const result = await generateText({model: anthropic('claude-sonnet-5-5'),tools,stopWhen: stepCountIs(5),system: 'You are the scheduling assistant for Northside Dental. Confirm appointments by text.',prompt: 'Text +14155550123 to confirm their 2pm tomorrow and ask them to reply C to confirm.',});await agentphone.close();
The model calls send_message with the right number, and the result includes the tool call and the message ID. If you only want a subset of tools, filter the object before passing it; leaving buy_number and make_call out of a public-facing agent is sensible.
Bring replies back in
Set your project webhook to a route in your Next.js app. Every inbound SMS, iMessage, WhatsApp message or call transcript lands there as an agent.message event with recentHistory, so the next generateText call has the thread without a database read.
// app/api/agentphone/route.tsimport { createHmac, timingSafeEqual } from 'node:crypto';export async function POST(req: Request) {const raw = await req.text();const ts = req.headers.get('x-webhook-timestamp') ?? '';const sig = req.headers.get('x-webhook-signature') ?? '';// Signatures are "sha256=<hex>" over "<timestamp>.<raw body>". Reject stale// timestamps so a captured delivery cannot be replayed later.if (Math.abs(Date.now() / 1000 - Number(ts)) > 300) return new Response('stale timestamp', { status: 401 });const expected = `sha256=${createHmac('sha256', process.env.AGENTPHONE_WEBHOOK_SECRET!).update(`${ts}.${raw}`).digest('hex')}`;if (sig.length !== expected.length || !timingSafeEqual(Buffer.from(sig), Buffer.from(expected))) {return new Response('bad signature', { status: 401 });}const event = JSON.parse(raw);if (event.event === 'agent.message') {// event.data.message, event.data.from, event.recentHistory}return new Response('ok');}
Reply with another send_message call, or directly with POST /v1/messages if you want to skip the model for a canned answer.
Things to know
- Keep the MCP client per request on serverless, or cache it in a long-running worker. It is a thin HTTP client, so creating it is cheap.
- The $5 signup credit covers the first number and about 100 inbound or outbound segments for testing; numbers are $3 a month, SMS $0.02 a segment, hosted calls $0.22 a minute.
- Outbound SMS on a US number needs 10DLC registration, which we submit for you. Inbound works immediately.
- v0 users can add the same server from the prompt menu under MCPs with a bearer token, for generation-time tool use.
FAQ
Which package gives the AI SDK MCP support? @ai-sdk/mcp, with createMCPClient. Use the http transport with the hosted URL and a bearer header; tools() returns tool definitions you pass to generateText or streamText.
Can I limit which tools the model sees? Yes. tools() returns a plain object, so pick the keys you want before passing it. Leave out purchases and calls for a public chat agent.
How do replies reach my app? Set a webhook route as your AgentPhone project webhook. Inbound messages on every channel arrive as one agent.message event, signed as sha256=<hmac> over the timestamp and raw body; check the timestamp is recent before trusting it.
Does this work on Vercel serverless? Yes. The MCP client is a short-lived HTTP client, so create it inside the handler and close it before returning.
What does it cost to try? $5 of credit on signup, no card. A number is $3 a month and SMS is $0.02 a segment.


