MCP Integration
The AG-Kit MCP integration provides seamless bidirectional conversion between AG-Kit’s tool system and the Model Context Protocol (MCP). This allows you to:- Connect to standard MCP servers and use their tools as AG-Kit BaseTool instances
- Expose AG-Kit tools as MCP servers for external clients to consume
- Support multiple transport protocols including stdio, HTTP, SSE, in-memory etc.
Overview
The MCP integration consists of several key components:- MCPClientTool: Wraps external MCP tools to work within AG-Kit
- MCPToolkit: High-level toolkit for managing MCP tools
- AGKitMCPServer: Exposes AG-Kit tools as a standard MCP server
- MCPClientManager: Manages connections to multiple MCP servers
Quick Start
Using External MCP Tools in AG-Kit
Exposing AG-Kit Tools as MCP Server
Transport Protocols
Stdio Transport
The most common transport for MCP servers, using standard input/output.Server Configuration
Client Configuration
HTTP Transport (StreamableHTTP)
For web-based MCP servers and clients.Server Configuration
Client Configuration
SSE Transport
Server-Sent Events transport for real-time communication.Server Configuration
Client Configuration
In-Memory Transport
For testing and same-process communication.Server Configuration
Client Configuration
Core Examples
Example 1: Creating and Running an MCP Server
Expose AG-Kit tools as a standard MCP server:Example 2: Connecting to External MCP Servers
Use external MCP servers as AG-Kit tools:Example 3: Using MCPToolkit for Simplified Management
High-level toolkit approach for managing multiple MCP connections:Example 4: Memory Transport for Testing
Use memory transport for testing MCP integrations:Example 5: Advanced Connection Management
Handle connection options and error scenarios:Example 6: Event Handling and Monitoring
Monitor MCP operations with event listeners:Advanced Configuration
Tool Configuration
Customize how AG-Kit tools are exposed via MCP:Client Tool Configuration
Customize MCP client tools:Connection Management
Advanced connection options:Event Handling
Monitor MCP operations:Schema Conversion
AG-Kit automatically converts between Zod schemas and MCP JSON schemas:Zod to MCP Schema
MCP to Zod Schema
Error Handling
Server Error Handling
Client Error Handling
API Reference
For complete API documentation including all interfaces, types, and detailed method signatures, see the MCP API Reference. AGKitMCPServer - Exposes AG-Kit tools as a standard MCP serverregisterTool(tool: BaseTool, config?: MCPToolConfig): MCPToolMetadata- Register a single toolregisterTools(tools: BaseTool[], config?: MCPToolConfig): MCPToolMetadata[]- Register multiple toolsunregisterTool(name: string): boolean- Remove a tool from the serverrun(transportConfig: MCPTransportConfig): Promise<void>- Start the server with specified transportstop(): Promise<void>- Stop the server and cleanup resourcescallTool(name: string, args: Record<string, any>): Promise<CallToolResult>- Execute a tool directlylistTools()- Get list of all registered toolsisServerRunning(): boolean- Check if server is runninggetStats(): object- Get server statistics
addServer(serverId: string, config: MCPClientConfig, options?: MCPConnectionOptions): Promise<void>- Connect to an MCP serverdisconnectServer(serverId: string): Promise<void>- Disconnect from a specific serverdisconnectAll(): Promise<void>- Disconnect from all serverscreateClientTools(serverId?: string): MCPClientTool[]- Create AG-Kit tool wrapperscreateClientTool(serverId: string, toolName: string, agKitToolName?: string): MCPClientTool- Create specific tool wrappercallTool(serverId: string, toolName: string, args: any): Promise<any>- Call an MCP tool directlyisServerConnected(serverId: string): boolean- Check connection statusgetStats(): object- Get client manager statistics
invoke(input: any, context?: ToolExecutionContext): Promise<ToolResult>- Execute the MCP toolgetMCPMetadata(): MCPToolMetadata- Get original MCP tool metadataisConnected(): boolean- Check if underlying client is connectedupdateConfig(newConfig: Partial<MCPToolConfig>): void- Update tool configuration
addServer(serverId: string, config: MCPClientConfig): Promise<void>- Add an MCP serverremoveServer(serverId: string): Promise<void>- Remove a servergetConnectedServers(): string[]- Get list of connected serversgetServerTools(serverId: string): MCPClientTool[]- Get tools from specific serverrefresh(): Promise<void>- Refresh all connectionscleanup(): Promise<void>- Cleanup all resourcesgetClientManager(): MCPClientManager- Get underlying client manager
Best Practices
- Use appropriate transports: stdio for CLI tools, HTTP for web services, memory for testing
- Handle errors gracefully: Implement proper error handling and retry logic
- Monitor connections: Use event listeners to track connection status
- Clean up resources: Always call cleanup methods when done
- Test thoroughly: Use memory transport for comprehensive testing
- Schema validation: Ensure your schemas are compatible between Zod and MCP JSON Schema
- Connection management: Use connection options for reliable production deployments
Troubleshooting
Common Issues
- Connection timeouts: Increase timeout values in transport configuration
- Schema conversion errors: Ensure Zod schemas use supported types
- Tool not found: Check tool registration and naming
- Transport errors: Verify transport configuration and server availability
- Memory leaks: Always clean up connections and event listeners