MCP Agent
MCP Agent
This example demonstrates how to build an autonomous agent that uses tools from an MCP (Model Context Protocol) server. MCP is a standard protocol for connecting AI models to external tools and data sources.
Understanding MCP Integration
graph LR
subgraph Synalinks
A[Agent] --> B[MultiServerMCPClient]
end
subgraph MCP Servers
B --> C[Math Server]
B --> D[Search Server]
B --> E[...]
end
C --> F[add, subtract, ...]
D --> G[search, fetch, ...]
MCP enables seamless integration between language models and external tools
through a standardized protocol. Synalinks provides the MultiServerMCPClient
class to connect to one or more MCP servers and load their tools as native
Synalinks Tool modules.
Key benefits of using MCP:
- Standardized Protocol: Use tools from any MCP-compatible server
- Multiple Servers: Connect to multiple MCP servers simultaneously
- Namespace Support: Avoid tool name collisions with namespacing
- Transport Flexibility: Support for stdio, HTTP, SSE, and WebSocket
Setting Up the MCP Server
First, you need an MCP server. This example uses a simple math server
(mcp_math_server.py) that provides basic arithmetic operations.
To run the server standalone (for testing):
Creating an MCP Agent
Connect to MCP servers using MultiServerMCPClient:
client = synalinks.MultiServerMCPClient(
{
"math": {
"command": "python",
"args": ["examples/mcp_math_server.py"],
"transport": "stdio",
},
}
)
# Load all tools from connected servers
tools = await client.get_tools()
Then use the tools with a FunctionCallingAgent:
inputs = synalinks.Input(data_model=Query)
outputs = await synalinks.FunctionCallingAgent(
data_model=NumericalFinalAnswer,
tools=tools,
language_model=language_model,
autonomous=True,
)(inputs)
Key Takeaways
- MCP Protocol: Synalinks supports the Model Context Protocol for standardized tool integration.
- MultiServerMCPClient: Connect to multiple MCP servers and load tools with automatic namespacing.
- Transport Options: Support for stdio (subprocess), HTTP, SSE, and WebSocket transports.
- Seamless Integration: MCP tools work identically to native Synalinks tools with full observability support.
