Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
MCP Integration for HF Agent
This agent now supports the Model Context Protocol (MCP), allowing it to connect to and use tools from MCP servers.
Overview
The MCP integration allows the agent to:
- Connect to multiple MCP servers simultaneously
- Automatically discover and use tools from connected servers
- Execute tool calls through the MCP protocol
- Seamlessly integrate MCP tools with the agent's existing tool system
Architecture
The integration consists of several components:
- MCPClient (
agent/core/mcp_client.py): Manages connections to MCP servers - ToolExecutor (
agent/core/executor.py): Executes both MCP and local tools - Config (
agent/config.py): Stores MCP server configurations - Session (
agent/core/session.py): Initializes MCP connections and manages lifecycle
Configuration
To use MCP servers with your agent, add them to your configuration file:
{
"model_name": "anthropic/claude-sonnet-4-5-20250929",
"tools": [],
"system_prompt_path": "",
"mcp_servers": [
{
"name": "weather",
"command": "python",
"args": ["path/to/weather_server.py"],
"env": null
},
{
"name": "filesystem",
"command": "node",
"args": ["path/to/filesystem_server.js"],
"env": {
"ALLOWED_PATHS": "/home/user/documents"
}
}
]
}
Configuration Fields
name: Unique identifier for the MCP servercommand: Command to execute the server (python,node, etc.)args: Arguments to pass to the command (path to server script)env: (Optional) Environment variables for the server process
Usage
Basic Usage
import asyncio
from agent.config import Config, load_config
from agent.core.agent_loop import submission_loop
async def main():
# Load config with MCP servers
config = load_config("config.json")
# Create queues
submission_queue = asyncio.Queue()
event_queue = asyncio.Queue()
# Start agent loop (MCP connections initialized automatically)
await submission_loop(submission_queue, event_queue, config)
if __name__ == "__main__":
asyncio.run(main())
Programmatic Configuration
from agent.config import Config, MCPServerConfig
config = Config(
model_name="anthropic/claude-sonnet-4-5-20250929",
tools=[],
system_prompt_path="",
mcp_servers=[
MCPServerConfig(
name="weather",
command="python",
args=["weather_server.py"],
env=None
)
]
)
How It Works
- Initialization: When the agent loop starts, it calls
session.initialize_mcp() - Connection: The session connects to all configured MCP servers
- Tool Discovery: Tools from all servers are discovered and added to the agent's tool list
- Tool Naming: MCP tools are prefixed with their server name (e.g.,
weather__get_forecast) - Execution: When the LLM calls a tool, the ToolExecutor routes it to the appropriate MCP server
- Cleanup: When the agent shuts down, all MCP connections are cleaned up properly
Tool Naming Convention
MCP tools are automatically prefixed with their server name to avoid conflicts:
- Original tool:
get_forecast - MCP tool name:
weather__get_forecast
This ensures that tools from different servers don't conflict, even if they have the same name.
Example: Creating a Simple MCP Server
Here's a minimal example of an MCP server (save as calculator_server.py):
import asyncio
from mcp.server import Server, stdio_server
from mcp.types import Tool, TextContent
app = Server("calculator")
@app.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(
name="add",
description="Add two numbers",
inputSchema={
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"}
},
"required": ["a", "b"]
}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
if name == "add":
result = arguments["a"] + arguments["b"]
return [TextContent(type="text", text=str(result))]
raise ValueError(f"Unknown tool: {name}")
async def main():
async with stdio_server() as (read_stream, write_stream):
await app.run(read_stream, write_stream, app.create_initialization_options())
if __name__ == "__main__":
asyncio.run(main())
Troubleshooting
Server Connection Issues
If you see errors connecting to an MCP server:
- Check that the server script path is correct
- Ensure the command (
python,node) is in your PATH - Verify the server script is executable
- Check server logs for initialization errors
Tool Not Found
If the agent can't find an MCP tool:
- Verify the server is connected (check startup logs)
- Check tool naming (should be
servername__toolname) - Ensure the server properly implements
list_tools()
Performance Considerations
- MCP server initialization happens once at startup
- Tool calls are asynchronous and don't block the agent
- Multiple servers can be used simultaneously
- Consider using local tools for high-frequency operations
Best Practices
- Unique Server Names: Give each MCP server a unique, descriptive name
- Error Handling: MCP connection failures are logged but don't crash the agent
- Resource Cleanup: Always let the agent shut down gracefully to cleanup connections
- Testing: Test MCP servers independently before integrating them
- Security: Be cautious with file system and network access in MCP servers
Future Enhancements
Potential improvements to consider:
- Dynamic server addition/removal during runtime
- Server health monitoring and auto-reconnection
- Tool caching and performance optimization
- Support for MCP resources and prompts
- Rate limiting and timeout configuration