Skip to content

Claude Code Context 9

YoungCan-Wang edited this page Sep 1, 2026 · 8 revisions

Claude Code 工作机制(九):MCP 集成

本文拆解 MCP(Model Context Protocol)在 Claude Code API trace 里的完整形态:工具注册、tool_use 往返、tool_result 结构,以及与内置工具的对比。 日志来源:在 WyckoffAgent 仓库连接 wyckoff MCP 的实战会话 session-ccaae2f6(27 次,持仓诊断);编写 MCP Server 的开发会话 trace_172109.jsonl(568 次)。

系列目录:


一句话总结

MCP 连接后,tools 数组从 26 → 43(+17),新增工具以 mcp__<server>__<tool> 命名;调用/结果走标准 tool_use/tool_result,与内置工具 无差别。


1. 连接前后的 tools 数组

状态 tools 数量 来源
未连接 MCP 26(内置) session-ffcb08f5
连接 wyckoff MCP 43(26 内置 + 17 MCP) session-ccaae2f6

连接成功后,Turn 2 起每轮请求的 tools 数组包含:

  • 26 个内置工具(Agent, Bash, Read, Edit…)
  • 2 个 MCP 通用工具:ListMcpResourcesTool、ReadMcpResourceTool
  • 15 个 mcp__wyckoff__* 业务工具

claude-tap 中可见 MCP 工具列表


2. MCP 工具定义的 wire 格式

以 mcp__wyckoff__intraday_analysis 为例(Turn 2 tools 数组原文):

{
  "name": "mcp__wyckoff__intraday_analysis",
  "description": "单股盘中多周期分析。纯引擎计算,返回分钟线结构化特征。\n\n    **调用时机**:用户问\"盘中表现如何\"、\"现在能买吗\"、\"今天走势怎样\"时调用。\n    返回 VWAP 位置、5m/15m 趋势方向、动量、量能分布、综合强度分等。\n    **结果处理**:strength_score 是核心(0-100),配合趋势和VWAP位置给出通俗建议。\n    ",
  "input_schema": {
    "type": "object",
    "properties": {
      "code": { "title": "Code", "type": "string" }
    },
    "required": ["code"],
    "title": "intraday_analysisArguments"
  }
}

与内置工具(如 Read)结构完全一致:name + description + input_schema。唯一区别是命名规则 mcp__<server>__<tool>。

input_schema.title 来自 FastMCP 自动生成({function_name}Arguments),非手写。


3. MCP tool_use / tool_result 往返

MCP tool_use 调用 + tool_result 完整往返

Turn 4 Response — mcp__wyckoff__portfolio 调用

3.1 调用(Turn 4,Opus)

{
  "type": "tool_use",
  "id": "toolu_bdrk_01CivZZQWsHx1P42ivgKDx6G",
  "name": "mcp__wyckoff__portfolio",
  "input": { "mode": "diagnose" }
}

3.2 结果(Turn 5 user message)

{
  "type": "tool_result",
  "tool_use_id": "toolu_bdrk_01CivZZQWsHx1P42ivgKDx6G",
  "content": [
    {
      "type": "text",
      "text": "{\n  \"portfolio_id\": \"USER_LIVE:local\",\n  \"free_cash\": 0.0,\n  \"position_count\": 3,\n  \"successful_count\": 3,\n  \"diagnostics\": [{\"code\": \"300825\", \"health\": \"🔴危险\", ...}]\n}"
    }
  ]
}

完整 portfolio 诊断返回 2,173 字符 JSON,含每只持仓的 Wyckoff 结构评估。

3.3 错误返回

MCP server 内部异常时返回 JSON error(非 HTTP 错误):

{ "error": "未配置 TICKFLOW_API_KEY,无法获取分钟线数据" }

模型收到后会向用户解释原因并调整策略——行为与内置工具的 is_error: true 一致。


4. 并行 MCP 调用

Turn 6 同轮输出 3 个 mcp__wyckoff__search_stock_by_name:

[
  { "name": "mcp__wyckoff__search_stock_by_name", "input": { "keyword": "福日电子" } },
  { "name": "mcp__wyckoff__search_stock_by_name", "input": { "keyword": "阿尔特" } },
  { "name": "mcp__wyckoff__search_stock_by_name", "input": { "keyword": "苏州天脉" } }
]

Turn 7 同轮 3 个 update_portfolio(add 操作);Turn 11 同轮 3 个 intraday_analysis。

行为与内置工具并行完全一致:客户端并发调用 MCP server,3 个 tool_result 在下一轮 user message 中按 tool_use_id 配对返回。


5. 完整会话时序

session-ccaae2f6(27 条 API,持仓诊断场景):

T1   Sonnet  msgs= 1  tools= 0  stop=end_turn    ← 标题预检
T2   Opus    msgs= 1  tools=43  stop=tool_use     ← ListMcpResourcesTool
T3   Opus    msgs= 3  tools=43  stop=end_turn     ← 介绍可用工具
T4   Opus    msgs= 5  tools=43  stop=tool_use     ← portfolio(diagnose)
T5   Opus    msgs= 7  tools=43  stop=end_turn     ← 持仓为空,提示添加
T6   Opus    msgs= 9  tools=43  stop=tool_use     ← search_stock ×3
T7   Opus    msgs=11  tools=43  stop=tool_use     ← update_portfolio ×3
T8   Opus    msgs=13  tools=43  stop=tool_use     ← portfolio(diagnose)
T9   Opus    msgs=15  tools=43  stop=end_turn     ← 诊断报告输出
T10  Opus    msgs=17  tools=43  stop=end_turn     ← 用户追问
T11  Opus    msgs=17  tools=43  stop=tool_use     ← intraday_analysis ×3
T12  Opus    msgs=19  tools=43  stop=end_turn     ← 报错(缺 API key)
...
T20  Opus    msgs=33  tools=43  stop=tool_use     ← market_overview + market_regime
T22  Opus    msgs=37  tools=43  stop=tool_use     ← update_portfolio(set_cash)

关键观察:

  • tools=43 贯穿全程:一旦连接,MCP 工具每轮都在
  • 模型调度:仅 T1 用 Sonnet(标题),其余全 Opus
  • stop_reason 机制不变:tool_use → 客户端执行 → tool_result 回注 → 下一轮

6. 通用 MCP 工具:ListMcpResourcesTool

Turn 2 模型首次响应即调用:

{ "name": "ListMcpResourcesTool", "input": {} }

返回:"No resources found. MCP servers may still provide tools even if they have no resources."

另有 ReadMcpResourceTool(参数:server + uri),用于读取 MCP server 暴露的静态资源(如配置文件、数据集 URI)。本次 trace 未触发。


7. MCP 工具清单(session-ccaae2f6)

工具名 参数 调用次数
ListMcpResourcesTool server? 1
mcp__wyckoff__portfolio mode 2
mcp__wyckoff__search_stock_by_name keyword 3
mcp__wyckoff__update_portfolio action, code, name, ... 4
mcp__wyckoff__intraday_analysis code 6
mcp__wyckoff__get_market_overview — 1
mcp__wyckoff__market_regime — 1
mcp__wyckoff__analyze_stock code, mode, cost, days 0
mcp__wyckoff__generate_ai_report stock_codes 0
mcp__wyckoff__wyckoff_diagnose code 0
mcp__wyckoff__run_backtest start, end, ... 0
mcp__wyckoff__screen_stocks board 0
...

19 次 MCP tool_use,覆盖 7 个不同工具。


8. 与 Skills / 内置工具的对比

内置工具 Skill MCP 工具
来源 Claude Code 写死 .claude/skills/ YAML 外部 MCP server 进程
注册 始终存在 Skill tool 内触发 连接后动态注入 tools 数组
命名 Read, Bash, ... /skill-name mcp__<server>__<tool>
tools 数组 26 个 不体现(走 Skill 转发) 额外 +N 个
input_schema 手写 无(自然语言 prompt) FastMCP 自动生成
执行者 CC 客户端 CC 客户端 + 模型 CC 客户端 → MCP server
tool_result 格式 text/image blocks text text(JSON string)

9. 开发侧证据(trace_172109)

568 次 API 的 WyckoffAgent 开发会话中,工程师编写 mcp_server.py(大量 Read/Edit),但 tools=26 始终不变——因为该会话本身未连接 MCP,只是在写 server 代码。

与 session-ccaae2f6 的区别:

trace_172109(开发) session-ccaae2f6(使用)
目的 编写 MCP server 调用 MCP 工具
tools 数量 26 43
mcp__ 调用 0 19
相关代码 Read/Edit mcp_server.py 模型调用 mcp__wyckoff__*

10. 可观测事实 vs 推断

结论 可信度
连接 MCP 后 tools 从 26 增至 43 ✅ session-ccaae2f6
MCP 工具命名 mcp__<server>__<tool> ✅
tool_use / tool_result 与内置工具 wire 格式一致 ✅
并行 MCP 调用(同轮多个 tool_use)正常工作 ✅ T6/T7/T11
MCP 错误以 JSON {"error": "..."} 返回 ✅ T12
ListMcpResourcesTool 为首次调用 ✅ T2
input_schema.title 来自 FastMCP 自动生成 ✅ *Arguments 后缀
MCP 连接失败时 tools 仍为 26(静默降级) ✅ session-ffcb08f5
ReadMcpResourceTool 的实际返回格式 ❓ 未触发

上一篇 / 下一篇 / 系列索引

Clone this wiki locally