ccgo 实现了 MCP(Model Context Protocol)客户端,支持连接外部 MCP 服务器并将其 tool/resource/prompt 集成到 Claude 会话中。
MCPManager
internal/mcp/manager.go 管理所有 MCP 服务器连接:
type ServerConnection struct {
Name string
Type string // "connected" | "failed" | "disabled"
Client Client
Tools []MCPTool
Resources []MCPResource
Prompts []MCPPrompt
Config settings.MCPServerConfig
Error string
Capabilities map[string]interface{}
}连接类型
| 类型 | 说明 | 配置字段 |
|---|---|---|
stdio | 本地进程,通过 stdin/stdout 通信 | command, args, env |
http | HTTP/SSE 远程服务器 | url, headers |
sse | 同 http | 同上 |
重试策略
初始连接失败时指数退避重试,最多 5 次:
- 初始延迟:1s
- 最大延迟:30s
- 公式:
min(1s × 2^(attempt-1), 30s)
并发连接
LoadFromSettings() 对所有配置的服务器并发建连(sync.WaitGroup),连接失败为非 fatal(记录 log,不中断启动)。
MCP Tool 命名
MCP tool 命名格式:mcp__serverName__toolName
调用时 CallTool() 解析 server name 并路由到对应连接:
info := MCPInfoFromString("mcp__myserver__mytool")
// info.ServerName = "myserver"
// info.ToolName = "mytool"MCP Prompt 作为 Skill
MCP 服务器的 Prompt 接口通过 GetMCPSkills() 转换为 skill,注册到 skill registry(命名:mcp__serverName__promptName)。
Resource 访问
两个 built-in tool 支持 MCP resource:
ListMCPResources:列出所有 connected 服务器的 resourceReadMCPResource:读取指定 resource 内容
相关页面
- skills — MCP prompt 作为 skill
- tools — MCP tool 注册
- architecture — MCP 在依赖注入中的位置