Model Context Protocol (MCP)
The AI SDK supports connecting to Model Context Protocol (MCP) servers to access their tools, resources, and prompts. MCP enables your AI applications to discover and use capabilities across various services through a standardized interface.What is MCP?
Model Context Protocol (MCP) is an open standard that allows AI applications to:- Discover tools from external servers dynamically
- Access resources like files, databases, and APIs
- Use prompts defined by service providers
- Integrate services without custom integrations
When to Use MCP
MCP is best suited for:- Rapid development: Quickly prototype with external tools
- User-provided tools: Allow users to bring their own MCP servers
- Dynamic tool discovery: Tools that change frequently
- Third-party integrations: Connect to services with MCP support
AI SDK Tools vs MCP Tools
For production applications, prefer AI SDK tools for full control and type safety:Installation
Creating an MCP Client
HTTP Transport (Recommended)
For production deployments, use HTTP transport:With OAuth
SSE Transport
Server-Sent Events provide an alternative HTTP-based transport:Stdio Transport (Local Only)
Use stdio only for local development. It cannot be deployed to production.
Using MCP Tools
Schema Discovery
Automatically load all tools from the MCP server:Schema Definition
Define schemas explicitly for type safety:Typed Tool Outputs
Define output schemas for type-safe results:Without
outputSchema, the tool returns the raw CallToolResult object. With outputSchema, you get validated, typed data.Closing the Client
Short-Lived Usage
Close after the response completes:Long-Lived Usage
Use try/finally for non-streaming:MCP Resources
Resources are application-driven data sources that provide context to the model.Listing Resources
Reading Resources
Resource Templates
Resource templates allow dynamic URI patterns:MCP Prompts
MCP Prompts is an experimental feature and may change.
Listing Prompts
Getting a Prompt
Elicitation Requests
Elicitation allows MCP servers to request additional information during tool execution.Enable Elicitation
Handle Elicitation Requests
Elicitation Actions
string
User provided the requested information (include
content)string
User chose not to provide the information
string
User cancelled the operation entirely
Complete Example
Best Practices
Use HTTP Transport for Production
HTTP and SSE transports work in all deployment environments:Define Schemas for Type Safety
Close Clients Properly
Next Steps
Tool Calling
Learn about AI SDK tools
Prompt Engineering
Tips for effective tool usage