This document provides detailed information about the MCP (Model Context Protocol) integration in this application.
The MCP integration allows you to connect to external MCP servers and use their tools alongside your existing application tools. MCP tools are automatically discovered, converted to Vercel AI SDK format, and made available to the LLM.
-
MCPClient (
lib/mcp/client.ts)- Implements the MCP client protocol
- Handles JSON-RPC communication
- Supports HTTP and stdio transports (HTTP fully implemented)
- Manages session lifecycle and capability negotiation
-
MCP Adapter (
lib/mcp/adapter.ts)- Converts MCP tools to Vercel AI SDK tools
- Maps JSON Schema to Zod schemas
- Handles tool execution and result formatting
-
MCP Server Manager (
lib/mcp/manager.ts)- Manages multiple MCP server connections
- Handles initialization and cleanup
- Provides unified tool access
-
API Routes (
app/api/mcp/route.ts)- REST API for managing MCP servers
- Supports adding, removing, and listing servers
- Tool discovery endpoint
-
UI Component (
components/MCPServerManager.tsx)- React component for managing MCP servers
- Add/remove servers via UI
- Display server status and tool counts
The client follows the MCP initialization flow:
- Send
initializerequest with client capabilities - Receive server capabilities and info
- Send
notifications/initializednotification
Tools are discovered via:
tools/listmethod (supports pagination)- Returns tool definitions with JSON Schema input/output schemas
Tools are called via:
tools/callmethod with tool name and arguments- Returns structured content (text, images, etc.)
The adapter converts JSON Schema properties to Zod types:
string→z.string()number→z.number()integer→z.number().int()boolean→z.boolean()array→z.array(itemType)object→z.object(shape)
Required fields are marked with .optional() for optional properties.
MCP tools are prefixed with mcp_ to avoid conflicts:
- Original:
get_weather - Prefixed:
mcp_get_weather
MCP tools are integrated into the tool system but have special handling:
- Not available in sandbox: MCP tools require external connections and cannot be used within
code_execution - Direct calls only: MCP tools must be called directly by the LLM
- System prompt: The LLM is informed about MCP tools and their limitations
Edit lib/mcp/mcp-config.ts:
export const mcpServers: MCPServerConfig[] = [
{
name: "GitHub MCP",
type: "http",
url: "https://api.githubcopilot.com/mcp/"
}
];
export const enableMCP: boolean = true;The server will be automatically loaded when the application starts.
- Open the application
- Find the "MCP Servers" panel
- Click "Add Server"
- Fill in:
- Name: "GitHub MCP"
- Type: HTTP
- URL:
https://api.githubcopilot.com/mcp/
- Click "Add Server"
Note: UI-added servers are session-specific and won't persist across restarts.
curl -X POST http://localhost:3000/api/mcp \
-H "Content-Type: application/json" \
-d '{
"action": "initialize",
"servers": [{
"name": "GitHub MCP",
"type": "http",
"url": "https://api.githubcopilot.com/mcp/"
}]
}'Once an MCP server is connected, its tools are automatically available. The LLM can call them like any other tool:
User: "Search for repositories about TypeScript on GitHub"
Assistant: [Calls mcp_github_search_repositories with query="TypeScript"]
- Connection failures: Logged and server marked as error
- Tool call failures: Errors are returned to the LLM
- Schema conversion failures: Fallback to
z.any() - Timeout handling: 30-second timeout for API requests
- Server URLs: Only connect to trusted MCP servers
- Tool execution: MCP tools execute with the permissions of the server
- Session management: Session IDs are managed securely
- Input validation: All tool inputs are validated against schemas
- Stdio transport: Not yet fully implemented (HTTP recommended)
- Sandbox exclusion: MCP tools cannot be used in code_execution
- Schema conversion: Some advanced JSON Schema features may not convert perfectly
- Concurrent requests: Each server connection is independent
- Full stdio transport implementation
- Tool result caching
- Server health monitoring
- Tool usage analytics
- WebSocket transport support
- Resource and prompt support (beyond tools)
Enable debug logging by checking console output:
[MCP]prefix indicates MCP-related logs- Tool discovery logs show loaded tools
- Tool execution logs show calls and results
- Error logs show connection and execution failures