File: create-mcp-client.md | Updated: 11/15/2025
Menu
v5 (Latest)
AI SDK 5.x
Model Context Protocol (MCP) Tools
Experimental_StdioMCPTransport
Copy markdown
experimental_createMCPClient()
=================================================================================================================================
Creates a lightweight Model Context Protocol (MCP) client that connects to an MCP server. The client provides:
It currently does not support accepting notifications from an MCP server, and custom configuration of the client.
This feature is experimental and may change or be removed in the future.
import { experimental_createMCPClient } from "@ai-sdk/mcp"
MCPClientConfig
Configuration for the MCP client.
MCPClientConfig
TransportConfig = MCPTransport | McpSSEServerConfig
Configuration for the message transport layer.
MCPTransport
() => Promise<void>
A method that starts the transport
(message: JSONRPCMessage) => Promise<void>
A method that sends a message through the transport
() => Promise<void>
A method that closes the transport
() => void
A method that is called when the transport is closed
(error: Error) => void
A method that is called when the transport encounters an error
(message: JSONRPCMessage) => void
A method that is called when the transport receives a message
MCPTransportConfig
'sse' | 'http
Use Server-Sent Events for communication
string
URL of the MCP server
Record<string, string>
Additional HTTP headers to be sent with requests.
OAuthClientProvider
Optional OAuth provider for authorization to access protected remote MCP servers.
string
Client name. Defaults to "ai-sdk-mcp-client"
(error: unknown) => void
Handler for uncaught errors
Returns a Promise that resolves to an MCPClient with the following methods:
async (options?: { schemas?: TOOL_SCHEMAS }) => Promise<McpToolSet<TOOL_SCHEMAS>>
Gets the tools available from the MCP server.
options
TOOL_SCHEMAS
Schema definitions for compile-time type checking. When not provided, schemas are inferred from the server.
async (options?: { params?: PaginatedRequest['params']; options?: RequestOptions; }) => Promise<ListResourcesResult>
Lists all available resources from the MCP server.
options
PaginatedRequest['params']
Optional pagination parameters including cursor.
RequestOptions
Optional request options including signal and timeout.
async (args: { uri: string; options?: RequestOptions; }) => Promise<ReadResourceResult>
Reads the contents of a specific resource by URI.
args
string
The URI of the resource to read.
RequestOptions
Optional request options including signal and timeout.
async (options?: { options?: RequestOptions; }) => Promise<ListResourceTemplatesResult>
Lists all available resource templates from the MCP server.
options
RequestOptions
Optional request options including signal and timeout.
async (options?: { params?: PaginatedRequest['params']; options?: RequestOptions; }) => Promise<ListPromptsResult>
Lists available prompts from the MCP server.
options
PaginatedRequest['params']
Optional pagination parameters including cursor.
RequestOptions
Optional request options including signal and timeout.
async (args: { name: string; arguments?: Record<string, unknown>; options?: RequestOptions; }) => Promise<GetPromptResult>
Retrieves a prompt by name, optionally passing arguments.
args
string
Prompt name to retrieve.
Record<string, unknown>
Optional arguments to fill into the prompt.
RequestOptions
Optional request options including signal and timeout.
async () => void
Closes the connection to the MCP server and cleans up resources.
import { experimental_createMCPClient, generateText } from '@ai-sdk/mcp';import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';import { openai } from '@ai-sdk/openai';
let client;
try { client = await experimental_createMCPClient({ transport: new Experimental_StdioMCPTransport({ command: 'node server.js', }), });
const tools = await client.tools();
const response = await generateText({ model: openai('gpt-4o-mini'), tools, messages: [{ role: 'user', content: 'Query the data' }], });
console.log(response);} catch (error) { console.error('Error:', error);} finally { // ensure the client is closed even if an error occurs if (client) { await client.close(); }}
The client throws MCPClientError for:
For tool execution, errors are propagated as CallToolError errors.
For unknown errors, the client exposes an onUncaughtError callback that can be used to manually log or handle errors that are not covered by known error types.
On this page
experimental_createMCPClient()
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: