第3集:Tool — Claude 的手和脚

⏱ 预计阅读 5 分钟 更新于 2026/5/7
💡 进群学习加 wx: agentupdate
(申请发送: agentupdate)

Tool 是 Claude 能执行的最小操作单元。内置 Tool + MCP Tool 构成了 Claude 的全部实际操作能力。

内置 Tool 列表

Tool 作用 安全级别
Read 读取文件内容 只读
Write 创建/覆盖文件 写入
Edit 精确字符串替换 写入
Bash 执行 Shell 命令 最高
Glob 文件模式搜索 只读
Grep 内容搜索 只读
Agent 启动子 Agent 中等
AskUserQuestion 向用户提问 只读
WebSearch 网络搜索 只读
LSP 代码智能(跳转/引用) 只读

Tool 调用流程

sequenceDiagram
    participant Claude as Claude AI
    participant Engine as Tool Engine
    participant Guard as 权限检查
    participant Hook as Hook 系统
    participant FS as 文件系统/Shell

    Claude->>Engine: 调用 Edit(file, old, new)
    Engine->>Guard: 检查权限
    alt 需要用户确认
        Guard->>Guard: 弹出确认提示
        Note over Guard: 用户拒绝 → 返回错误
    end
    Guard->>Hook: PreToolUse 事件
    Hook->>Hook: 执行 PreToolUse hooks
    Hook-->>Engine: 允许/阻止
    Engine->>FS: 执行文件修改
    FS-->>Engine: 返回结果
    Engine->>Hook: PostToolUse 事件
    Hook->>Hook: 执行 PostToolUse hooks
    Engine-->>Claude: 返回操作结果

关键点

  • Tool 是 Claude 唯一能"做事"的途径。没有 Tool,Claude 只能输出文字。
  • 每次调用都经过:权限检查 → PreToolUse Hook → 执行 → PostToolUse Hook。
  • Skill 不是 Tool。Skill 是指令模板,最终通过 Tool 执行。

MCP Tool — 扩展 Tool 的能力

MCP Server 通过协议暴露额外的 Tool。对 Claude 来说,MCP Tool 和内置 Tool 使用方式相同。

flowchart LR
    subgraph Claude 可用的 Tool
        B[Read] 
        C[Write]
        D[Edit]
        E[Bash]
        F[...内置]
        G[mcp__search
claude-mem 搜索] H[mcp__get_observations
claude-mem 获取] I[mcp__analyze_image
图片分析] J[mcp__webReader
网页读取] end subgraph 来源 CORE[Claude Code 内置] MCP1[claude-mem MCP] MCP2[4_5v MCP] MCP3[web_reader MCP] end CORE --> B & C & D & E & F MCP1 --> G & H MCP2 --> I MCP3 --> J