MCP Server Integration: Turning Any External Service into an Agent Tool
MCP Server Integration: Turning Any External Service into an Agent Tool
Function Tools are good for custom logic, but to connect existing external services to an Agent—like a MySQL database, Redis cache, or gRPC service—MCP (Model Context Protocol) is a more elegant approach.
What is MCP
MCP is a protocol defining how Agents communicate with external services. Its design philosophy: turn any external service into a Tool an Agent can call, without writing hard-coded integration logic in Agent code.
ADK Go Agent ← MCP Client ← MCP Protocol → MCP Server ← External Service
↓
MySQL / Redis / gRPC / HTTP
MCP Core Concepts
| Concept | Description |
|---|---|
| MCP Server | MCP endpoint exposing external service |
| MCP Client | ADK Go client connecting to MCP Server |
| Resource | Data resource provided by MCP Server |
| Tool | Callable tool exposed by MCP Server |
| Prompt | Preset prompts provided by MCP Server |
Using MCP Tools
ADK Go provides mcp package to connect to MCP Servers:
import "google.golang.org/adk/tool/mcp"
mcpTool, err := mcp.NewMCPTool(ctx,
mcp.WithServerURL("http://localhost:8081/mcp"),
)
Then register to Agent:
Tools: []tool.Tool{
mcpTool, // All Tools exposed by MCP Server
}
Complete Example: Connecting a Weather MCP Server
Say there’s an MCP Server exposing two Tools:
get_weather: query weatherget_forecast: query forecast
1. Start MCP Server
MCP Server can be implemented in any language, here using Python as example:
# mcp_server.py
from mcp.server import MCPServer
server = MCPServer("weather-service")
@server.tool("get_weather")
def get_weather(city: str) -> str:
return f"Weather in {city}: sunny, 22°C"
@server.tool("get_forecast")
def get_forecast(city: str, days: int) -> str:
return f"{city} {days}-day forecast: sunny, rainy, cloudy"
if __name__ == "__main__":
server.run()
Start:
python mcp_server.py --port 8081
2. Go Side Integration
mcpTool, err := mcp.NewMCPTool(ctx,
mcp.WithServerURL("http://localhost:8081/mcp"),
)
if err != nil {
log.Fatalf("Failed to connect to MCP server: %v", err)
}
agent, _ := llmagent.New(llmagent.Config{
Name: "weather_assistant",
Model: model,
Instruction: "你是天气助手,用工具查询天气信息。",
Tools: []tool.Tool{
mcpTool, // Register MCP Tool
},
})
Agent will automatically recognize the two Tools exposed by MCP Server, can call them directly.
MCP Advantages
vs Hand-written Tools
| Method | Pros | Cons |
|---|---|---|
| Hand-written Function Tool | Fully controllable | Each integration needs code |
| MCP | Declarative config, Server upgrades auto-enable new Tools | Requires Server support MCP |
vs REST API Direct Connect
| Method | Pros | Cons |
|---|---|---|
| REST API direct | Simple, direct | Tool implementation scattered in code |
| MCP | Tool definitions in Server, Agent just calls | One more protocol layer |
MCP Security Considerations
Auth
MCP Server can add auth:
mcpTool, err := mcp.NewMCPTool(ctx,
mcp.WithServerURL("http://localhost:8081/mcp"),
mcp.WithAuth(mcp.BearerToken("your-token")),
)
Network Isolation
Don’t expose internal MCP Server directly to public net. Use VPC or VPN:
mcp.WithTLS(true), // Force TLS
mcp.WithServerURL("https://internal-mcp.vpc.local/mcp"),
Common Issues
Q: MCP Server unresponsive
A: Check if Server is running: curl http://localhost:8081/mcp/health. Confirm network connectivity.
Q: MCP Tool not recognized by Agent A: Confirm MCP Server correctly exposed Tool definitions. Check Agent startup logs, see if MCP Tool loaded successfully.
Q: Hard to access MCP Server from Mainland China A: If MCP Server deployed on internal network, ensure Agent’s network can access Server. Consider exposing MCP Server to public net with TLS.
Next Steps
MCP done. Next, OpenAPI Tool—dynamically generate Tools from OpenAPI spec, suitable for services with REST API docs but no MCP interface.
← Function Tool Performance | OpenAPI Tool →
Follow “Mengshou Programming” on WeChat for more Go ADK hands-on tutorials, weekly updates on Go / AI programming 实战干货.
