梦兽编程
AI_SUITE

MCP Server Integration: Turning Any External Service into an Agent Tool

Detailed MCP (Model Context Protocol) working principles, and how to connect external services (like databases, APIs) to ADK Go Agents through MCP.

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

ConceptDescription
MCP ServerMCP endpoint exposing external service
MCP ClientADK Go client connecting to MCP Server
ResourceData resource provided by MCP Server
ToolCallable tool exposed by MCP Server
PromptPreset 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 weather
  • get_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

MethodProsCons
Hand-written Function ToolFully controllableEach integration needs code
MCPDeclarative config, Server upgrades auto-enable new ToolsRequires Server support MCP

vs REST API Direct Connect

MethodProsCons
REST API directSimple, directTool implementation scattered in code
MCPTool definitions in Server, Agent just callsOne 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 实战干货.

Frequently Asked Questions

What is MCP?

Model Context Protocol, a standard for Agents to call external services uniformly.

When should I use MCP instead of hand-written Function Tools?

When an external service already exposes an MCP Server or you want declarative integration.

How does ADK Go connect to an MCP Server?

Use mcp.NewMCPTool from google.golang.org/adk/tool/mcp and register the result.

What security considerations apply?

Add auth, enforce TLS, and do not expose internal Servers publicly.

Why is an MCP Tool not recognized?

Verify the Server exposes Tool definitions and check Agent startup logs.