Skip to content

2. 工具系统

2.1 文件操作工具

typescript
const fileTools = [
  {
    name: "read_file",
    description: `Read file from local filesystem. 
    
Usage:
- Specify full path or relative to workspace
- Can read any accessible file
- Line numbers in output start at 1

Parameters:
- target_file (required): Path to the file
- offset (optional): Starting line number
- limit (optional): Max lines to read`,
    
    input_schema: {
      type: "object",
      properties: {
        target_file: { type: "string" },
        offset: { type: "integer" },
        limit: { type: "integer" }
      },
      required: ["target_file"]
    }
  },
  
  {
    name: "write_file",
    description: `Write content to a file. Overwrites if exists.

IMPORTANT:
- Read the file first if it exists
- ALWAYS prefer editing existing files
- Never create documentation unless requested`,
    
    input_schema: {
      type: "object",
      properties: {
        file_path: { type: "string" },
        content: { type: "string" }
      },
      required: ["file_path", "content"]
    }
  },
  
  {
    name: "edit_file",
    description: `Edit a file by replacing old_string with new_string.

CRITICAL:
- old_string must match EXACTLY (including whitespace)
- Include enough context for unique matching
- The edit will FAIL if old_string is not unique
- Use replace_all for multiple occurrences`,
    
    input_schema: {
      type: "object",
      properties: {
        file_path: { type: "string" },
        old_string: { type: "string" },
        new_string: { type: "string" },
        replace_all: { type: "boolean" }
      },
      required: ["file_path", "old_string", "new_string"]
    }
  }
];

2.2 搜索工具

typescript
const searchTools = [
  {
    name: "codebase_search",
    description: `Semantic search that finds code by meaning.

When to Use:
- Explore unfamiliar codebases
- Find code by concept, not exact text
- Ask "how/where/what" questions

When NOT to Use:
- Exact string matches (use grep)
- Simple symbol lookups (use grep)

Examples:
- Good: "Where is user authentication handled?"
- Bad: "AuthService" (use grep for exact match)`,
    
    input_schema: {
      type: "object",
      properties: {
        query: { 
          type: "string",
          description: "Complete question about what you want to find"
        },
        target_directories: {
          type: "array",
          items: { type: "string" },
          description: "Limit search to specific directory"
        }
      },
      required: ["query", "target_directories"]
    }
  },
  
  {
    name: "grep",
    description: `Regex search in files using ripgrep.

Features:
- Full regex syntax
- Respects .gitignore
- Multiple output modes: content, files_with_matches, count

Tips:
- Escape special characters for literal match
- Use 'type' to filter by file type
- Results are capped for performance`,
    
    input_schema: {
      type: "object",
      properties: {
        pattern: { type: "string" },
        path: { type: "string" },
        type: { type: "string" },
        output_mode: { 
          type: "string",
          enum: ["content", "files_with_matches", "count"]
        }
      },
      required: ["pattern"]
    }
  }
];

2.3 终端工具

typescript
{
  name: "run_terminal_cmd",
  description: `Execute a terminal command.

Sandboxing:
- By default, runs in a sandbox with limited permissions
- Write access limited to workspace
- Network access blocked
- Git writes blocked

Permissions:
- 'network': For package installs, API calls
- 'git_write': For commits, branch changes
- 'all': Disable sandbox entirely

Guidelines:
- For long-running processes, use is_background: true
- Check existing terminals before starting servers
- Pass non-interactive flags (e.g., --yes)`,

  input_schema: {
    type: "object",
    properties: {
      command: { type: "string" },
      is_background: { type: "boolean" },
      required_permissions: {
        type: "array",
        items: { 
          type: "string",
          enum: ["network", "git_write", "all"]
        }
      }
    },
    required: ["command", "is_background"]
  }
}

2.4 记忆工具

typescript
{
  name: "update_memory",
  description: `Create, update, or delete persistent memory.

Usage:
- Store user preferences
- Remember project-specific decisions
- Track important context

Actions:
- 'create': Add new memory
- 'update': Modify existing memory (requires existing_knowledge_id)
- 'delete': Remove memory (requires existing_knowledge_id)

Rules:
- Only create memory when user explicitly asks
- Use delete (not update) for contradictions
- Keep memories concise (one paragraph max)`,

  input_schema: {
    type: "object",
    properties: {
      action: { 
        type: "string",
        enum: ["create", "update", "delete"]
      },
      title: { type: "string" },
      knowledge_to_store: { type: "string" },
      existing_knowledge_id: { type: "string" }
    }
  }
}

前端面试知识库