# tool api

工具 API 定义了如何创建和使用 Claude Code 的工具。

## Tool 接口

### 基本定义

```typescript
interface Tool<TInput = any, TOutput = any> {
  name: string;
  description: string;
  inputSchema: ZodSchema<TInput>;
  execute(input: TInput, context: ToolContext): Promise<ToolResult<TOutput>>;
}
```

### ToolContext

```typescript
interface ToolContext {
  cwd: string;
  state: DeepImmutable<AppState>;
  checkPermission(action: string): Promise<boolean>;
  tools: Map<string, Tool>;
  signal: AbortSignal;
}
```

### ToolResult

```typescript
type ToolResult<T = any> =
  | { success: true; output: T }
  | { success: false; error: string };
```

## 创建工具

### 简单工具

```typescript
import { Tool, ToolContext, ToolResult } from '@anthropic-ai/claude-code';
import { z } from 'zod';

class GreetTool implements Tool {
  name = 'greet';
  description = 'Greet a user';
  
  inputSchema = z.object({
    name: z.string(),
  });
  
  async execute(
    input: z.infer<typeof this.inputSchema>,
    context: ToolContext
  ): Promise<ToolResult> {
    return {
      success: true,
      output: `Hello, ${input.name}!`,
    };
  }
}
```

## 下一步

* 查看 [用户数据](https://halls-organization-2.gitbook.io/halls-organization/api/broken-reference)
* 了解 [Anthropic API](https://halls-organization-2.gitbook.io/halls-organization/api/broken-reference)
