What is MCP?
The Model Context Protocol is an open standard for connecting AI models to external tools and data sources. It enables:- Custom Tools: Create application-specific functionality
- External APIs: Connect to third-party services
- Data Sources: Access databases, file systems, and more
- Process Isolation: Run tools in separate processes or in-process
MCP Server Types
The SDK supports two types of MCP servers:SDK-Embedded Servers (Recommended)
Run in the same process as your application. No separate server process needed. Benefits:- Simple setup - no process management
- Direct access to your application’s state
- Type-safe with Zod schema validation
- Lower latency - no IPC overhead
- Application-specific tools
- Tools that need access to app state
- Simple utility functions
External Servers
Run as separate processes, connected via stdio, SSE, HTTP, or WebSocket. Benefits:- Process isolation and security
- Language agnostic - write servers in any language
- Share servers across multiple SDK instances
- Standard MCP protocol compliance
- Complex server implementations
- Shared services across applications
- Third-party MCP servers
- Security-sensitive operations
Quick Start
Create your first SDK-embedded MCP server in 3 steps:Step 1: Install Dependencies
Step 2: Create a Tool
example.ts
Step 3: Run It
Architecture
SDK-Embedded Servers
External Servers
Communication Flow
When the AI calls an MCP tool:- CLI requests tool execution: AI decides to use a tool and sends request to CLI
- CLI routes to SDK: CLI identifies SDK-embedded server and sends control request
- SDK invokes handler: SDK validates input and calls your tool handler
- Handler returns result: Your function returns MCP-formatted result
- SDK sends response: Result is sent back to CLI as control response
- CLI forwards to AI: Tool result is included in the AI’s context
Tool Result Format
All tool handlers must return aCallToolResult object:
Text Response
Image Response
Multiple Content Blocks
Error Response
Configuration Options
When connecting MCP servers to a query:Timeout Configuration
Configure timeouts for MCP operations:Best Practices
1. Use SDK-Embedded Servers for Simple Tools
2. Use External Servers for Complex Operations
3. Validate Input with Zod
4. Handle Errors Gracefully
5. Provide Clear Descriptions
Next Steps
tool() Function
Learn how to create tools
createSdkMcpServer()
Create MCP server instances
MCP Examples
Complete working examples
External MCP Servers
Connect to external servers
