File: mcp-tools.md | Updated: 11/15/2025
Menu
v5 (Latest)
AI SDK 5.x
Model Context Protocol (MCP) Tools
Copy markdown
Model Context Protocol (MCP) Tools
=====================================================================================================================
The MCP tools feature is experimental and may change in the future.
The AI SDK supports connecting to Model Context Protocol (MCP) servers to access their tools, resources, and prompts. This enables your AI applications to discover and use capabilities across various services through a standardized interface.
We recommend using HTTP transport (like StreamableHTTPClientTransport) for production deployments. The stdio transport should only be used for connecting to local servers as it cannot be deployed to production environments.
Create an MCP client using one of the following transport options:
transport: { type: 'http', ... }, or use MCP's official TypeScript SDK StreamableHTTPClientTransportstdio: For local development only. Uses standard input/output streams for local MCP serversFor production deployments, we recommend using the HTTP transport. You can configure it directly on the client:
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
const mcpClient = await createMCPClient({ transport: { type: 'http', url: 'https://your-server.com/mcp',
// optional: configure HTTP headers headers: { Authorization: 'Bearer my-api-key' },
// optional: provide an OAuth client provider for automatic authorization authProvider: myOAuthClientProvider, },});
Alternatively, you can use StreamableHTTPClientTransport from MCP's official TypeScript SDK:
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
const url = new URL('https://your-server.com/mcp');const mcpClient = await createMCPClient({ transport: new StreamableHTTPClientTransport(url, { sessionId: 'session_123', }),});
SSE provides an alternative HTTP-based transport option. Configure it with a type and url property. You can also provide an authProvider for OAuth:
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
const mcpClient = await createMCPClient({ transport: { type: 'sse', url: 'https://my-server.com/sse',
// optional: configure HTTP headers headers: { Authorization: 'Bearer my-api-key' },
// optional: provide an OAuth client provider for automatic authorization authProvider: myOAuthClientProvider, },});
The stdio transport should only be used for local servers.
The Stdio transport can be imported from either the MCP SDK or the AI SDK:
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';// Or use the AI SDK's stdio transport:// import { Experimental_StdioMCPTransport as StdioClientTransport } from '@ai-sdk/mcp/mcp-stdio';
const mcpClient = await createMCPClient({ transport: new StdioClientTransport({ command: 'node', args: ['src/stdio/dist/server.js'], }),});
You can also bring your own transport by implementing the MCPTransport interface for specific requirements not covered by the standard transports.
The client returned by the experimental_createMCPClient function is a lightweight client intended for use in tool conversion. It currently does not support all features of the full MCP client, such as: session management, resumable streams, and receiving notifications.
Authorization via OAuth is supported when using the AI SDK MCP HTTP or SSE transports by providing an authProvider.
After initialization, you should close the MCP client based on your usage pattern:
When streaming responses, you can close the client when the LLM response has finished. For example, when using streamText, you should use the onFinish callback:
const mcpClient = await experimental_createMCPClient({ // ...});
const tools = await mcpClient.tools();
const result = await streamText({ model: 'openai/gpt-4.1', tools, prompt: 'What is the weather in Brooklyn, New York?', onFinish: async () => { await mcpClient.close(); },});
When generating responses without streaming, you can use try/finally or cleanup functions in your framework:
let mcpClient: MCPClient | undefined;
try { mcpClient = await experimental_createMCPClient({ // ... });} finally { await mcpClient?.close();}
The client's tools method acts as an adapter between MCP tools and AI SDK tools. It supports two approaches for working with tool schemas:
With schema discovery, all tools offered by the server are automatically listed, and input parameter types are inferred based on the schemas provided by the server:
const tools = await mcpClient.tools();
This approach is simpler to implement and automatically stays in sync with server changes. However, you won't have TypeScript type safety during development, and all tools from the server will be loaded
For better type safety and control, you can define the tools and their input schemas explicitly in your client code:
import { z } from 'zod';
const tools = await mcpClient.tools({ schemas: { 'get-data': { inputSchema: z.object({ query: z.string().describe('The data query'), format: z.enum(['json', 'text']).optional(), }), }, // For tools with zero inputs, you should use an empty object: 'tool-with-no-args': { inputSchema: z.object({}), }, },});
This approach provides full TypeScript type safety and IDE autocompletion, letting you catch parameter mismatches during development. When you define schemas, the client only pulls the explicitly defined tools, keeping your application focused on the tools it needs
According to the MCP specification , resources are application-driven data sources that provide context to the model. Unlike tools (which are model-controlled), your application decides when to fetch and pass resources as context.
The MCP client provides three methods for working with resources:
List all available resources from the MCP server:
const resources = await mcpClient.listResources();
Read the contents of a specific resource by its URI:
const resourceData = await mcpClient.readResource({ uri: 'file:///example/document.txt',});
Resource templates are dynamic URI patterns that allow flexible queries. List all available templates:
const templates = await mcpClient.listResourceTemplates();
According to the MCP specification, prompts are user-controlled templates that servers expose for clients to list and retrieve with optional arguments.
const prompts = await mcpClient.listPrompts();
Retrieve prompt messages, optionally passing arguments defined by the server:
const prompt = await mcpClient.getPrompt({ name: 'code_review', arguments: { code: 'function add(a, b) { return a + b; }' },});
You can see MCP tools in action in the following example:
Learn to use MCP tools in Node.js
On this page
Model Context Protocol (MCP) Tools
Stdio Transport (Local Servers)
Deploy and Scale AI Apps with Vercel.
Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.
Trusted by industry leaders: