tool interface

Claude Code 的工具系统基于统一的接口设计,确保类型安全和可扩展性。

Tool 接口

核心接口定义

// src/Tool.ts
interface Tool<TInput = any, TOutput = any> {
  // 基本信息
  name: string;
  description: string;
  
  // 输入验证
  inputSchema: ZodSchema<TInput>;
  
  // 执行方法
  execute(
    input: TInput,
    context: ToolContext
  ): Promise<ToolResult<TOutput>>;
  
  // 可选:转换为 Anthropic 工具格式
  toAnthropicTool?(): AnthropicTool;
}

ToolContext

ToolResult

实现示例

简单工具

复杂工具

类型安全

Zod Schema

运行时验证

工具注册

静态注册

动态注册

MCP 工具

工具组合

Composite Tool

下一步

  • 深入 FileEditTool 的实现

  • 了解 BashTool 的权限检查

  • 探索 AgentTool 的协作机制