Skip to content

Custom Tools

Translation in Progress

This page is being translated. Content below is a placeholder.

Creating a Custom Tool

Follow the ServerTool interface:

typescript
const myTool: ServerTool = {
  name: 'my_tool',

  schema: {
    name: 'my_tool',
    description: 'What this tool does',
    parameters: {
      type: 'object',
      properties: {
        // Define parameters
      }
    }
  },

  async execute(params) {
    // Implementation
    return { result: 'something' }
  }
}

Example: Git Status Tool

typescript
const gitStatusTool: ServerTool = {
  name: 'git_status',

  schema: {
    name: 'git_status',
    description: 'Get the current git status',
    parameters: {
      type: 'object',
      properties: {}
    }
  },

  async execute() {
    const { stdout } = await execAsync('git status --porcelain')
    const files = stdout.split('\\n').filter(Boolean)

    return {
      modified: files.filter(f => f.startsWith(' M')),
      added: files.filter(f => f.startsWith('A ')),
      untracked: files.filter(f => f.startsWith('??'))
    }
  }
}

Best Practices

  1. Clear descriptions: Help LLM know when to use
  2. Structured output: Return objects, not raw strings
  3. Error handling: Always catch and return errors
  4. Limit output size: Truncate large results

Summary

  • Implement ServerTool interface
  • Provide clear schema description
  • Handle errors gracefully
  • Return structured data

Next

Understand the Agent loop: Loop Structure →

Learn AI Agent development through real source code