|
| 1 | +# Tool Development |
| 2 | + |
| 3 | +Learn how to develop custom tools to extend Agent capabilities. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Tools are the building blocks that enable Agents to interact with external systems. @agent-tars/core uses the MCP (Model Context Protocol) framework for tool integration, providing a standardized way to create and register tools. |
| 8 | + |
| 9 | +## Tool Architecture |
| 10 | + |
| 11 | +Tools in @agent-tars/core are based on MCP Tool interface from `@tarko/mcp-agent`: |
| 12 | + |
| 13 | +```typescript |
| 14 | +import { Tool } from '@tarko/mcp-agent'; |
| 15 | + |
| 16 | +// Tools are created using the Tool class constructor |
| 17 | +const tool = new Tool({ |
| 18 | + id: 'tool_name', |
| 19 | + description: 'Tool description', |
| 20 | + parameters: JSONSchema7, |
| 21 | + function: async (args) => { /* implementation */ } |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +## Creating Your First Tool |
| 26 | + |
| 27 | +### Basic Tool Structure |
| 28 | + |
| 29 | +```typescript |
| 30 | +import { Tool, JSONSchema7 } from '@tarko/mcp-agent'; |
| 31 | + |
| 32 | +export function createCalculatorTool(): Tool { |
| 33 | + return new Tool({ |
| 34 | + id: 'calculator', |
| 35 | + description: 'Perform basic mathematical calculations', |
| 36 | + parameters: { |
| 37 | + type: 'object', |
| 38 | + properties: { |
| 39 | + operation: { |
| 40 | + type: 'string', |
| 41 | + enum: ['add', 'subtract', 'multiply', 'divide'], |
| 42 | + description: 'Mathematical operation to perform' |
| 43 | + }, |
| 44 | + a: { |
| 45 | + type: 'number', |
| 46 | + description: 'First number' |
| 47 | + }, |
| 48 | + b: { |
| 49 | + type: 'number', |
| 50 | + description: 'Second number' |
| 51 | + } |
| 52 | + }, |
| 53 | + required: ['operation', 'a', 'b'] |
| 54 | + } as JSONSchema7, |
| 55 | + function: async (params: { |
| 56 | + operation: string; |
| 57 | + a: number; |
| 58 | + b: number; |
| 59 | + }) => { |
| 60 | + const { operation, a, b } = params; |
| 61 | + |
| 62 | + let result: number; |
| 63 | + |
| 64 | + switch (operation) { |
| 65 | + case 'add': |
| 66 | + result = a + b; |
| 67 | + break; |
| 68 | + case 'subtract': |
| 69 | + result = a - b; |
| 70 | + break; |
| 71 | + case 'multiply': |
| 72 | + result = a * b; |
| 73 | + break; |
| 74 | + case 'divide': |
| 75 | + if (b === 0) { |
| 76 | + throw new Error('Division by zero is not allowed'); |
| 77 | + } |
| 78 | + result = a / b; |
| 79 | + break; |
| 80 | + default: |
| 81 | + throw new Error(`Unknown operation: ${operation}`); |
| 82 | + } |
| 83 | + |
| 84 | + return [{ |
| 85 | + type: 'text', |
| 86 | + text: `${a} ${operation} ${b} = ${result}` |
| 87 | + }]; |
| 88 | + } |
| 89 | + }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Registering Tools |
| 94 | + |
| 95 | +Tools are registered through MCP servers or directly in AgentTARS: |
| 96 | + |
| 97 | +```typescript |
| 98 | +import { AgentTARS } from '@agent-tars/core'; |
| 99 | +import { createCalculatorTool } from './calculator-tool'; |
| 100 | + |
| 101 | +// Register tools through custom MCP server |
| 102 | +const agent = new AgentTARS({ |
| 103 | + mcpServers: { |
| 104 | + calculator: { |
| 105 | + // Custom MCP server with calculator tool |
| 106 | + } |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +// Or register during initialization |
| 111 | +agent.registerTool(createCalculatorTool()); |
| 112 | +``` |
| 113 | + |
| 114 | +## Built-in Tool Categories |
| 115 | + |
| 116 | +@agent-tars/core includes built-in tools across 4 MCP server categories: |
| 117 | + |
| 118 | +- **browser**: Web automation via `@agent-infra/mcp-server-browser` |
| 119 | +- **filesystem**: File operations via `@agent-infra/mcp-server-filesystem` |
| 120 | +- **commands**: System commands via `@agent-infra/mcp-server-commands` |
| 121 | +- **search**: Web search via `@agent-infra/search` |
| 122 | + |
| 123 | +These are managed through `BrowserToolsManager`, `FilesystemToolsManager`, and `SearchToolProvider` classes. |
| 124 | + |
| 125 | +## Tool Implementation Patterns |
| 126 | + |
| 127 | +### Async Operations |
| 128 | + |
| 129 | +MCP tools naturally support async operations: |
| 130 | + |
| 131 | +```typescript |
| 132 | +export function createApiTool(): Tool { |
| 133 | + return new Tool({ |
| 134 | + id: 'api_request', |
| 135 | + description: 'Make HTTP API requests', |
| 136 | + parameters: { |
| 137 | + type: 'object', |
| 138 | + properties: { |
| 139 | + url: { type: 'string', description: 'Request URL' }, |
| 140 | + method: { type: 'string', enum: ['GET', 'POST', 'PUT', 'DELETE'] } |
| 141 | + }, |
| 142 | + required: ['url', 'method'] |
| 143 | + } as JSONSchema7, |
| 144 | + function: async (params: { url: string; method: string }) => { |
| 145 | + try { |
| 146 | + const response = await fetch(params.url, { |
| 147 | + method: params.method |
| 148 | + }); |
| 149 | + |
| 150 | + const data = await response.text(); |
| 151 | + |
| 152 | + return [{ |
| 153 | + type: 'text', |
| 154 | + text: `Status: ${response.status}\n${data}` |
| 155 | + }]; |
| 156 | + } catch (error) { |
| 157 | + throw new Error(`API request failed: ${error.message}`); |
| 158 | + } |
| 159 | + } |
| 160 | + }); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## Best Practices |
| 165 | + |
| 166 | +### Error Handling |
| 167 | + |
| 168 | +Use proper error handling with meaningful messages: |
| 169 | + |
| 170 | +```typescript |
| 171 | +function: async (params: any) => { |
| 172 | + try { |
| 173 | + // Tool logic here |
| 174 | + return [{ type: 'text', text: 'Success result' }]; |
| 175 | + } catch (error) { |
| 176 | + throw new Error(`Tool execution failed: ${error.message}`); |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +### Parameter Validation |
| 182 | + |
| 183 | +Define comprehensive JSON Schema for parameters: |
| 184 | + |
| 185 | +```typescript |
| 186 | +parameters: { |
| 187 | + type: 'object', |
| 188 | + properties: { |
| 189 | + email: { |
| 190 | + type: 'string', |
| 191 | + format: 'email', |
| 192 | + description: 'Valid email address' |
| 193 | + }, |
| 194 | + age: { |
| 195 | + type: 'number', |
| 196 | + minimum: 0, |
| 197 | + maximum: 150, |
| 198 | + description: 'Age in years' |
| 199 | + } |
| 200 | + }, |
| 201 | + required: ['email'], |
| 202 | + additionalProperties: false |
| 203 | +} as JSONSchema7 |
| 204 | +``` |
| 205 | + |
| 206 | +## Testing Tools |
| 207 | + |
| 208 | +Test your tools using standard testing frameworks: |
| 209 | + |
| 210 | +```typescript |
| 211 | +import { createCalculatorTool } from './calculator-tool'; |
| 212 | + |
| 213 | +describe('Calculator Tool', () => { |
| 214 | + it('should perform calculations', async () => { |
| 215 | + const tool = createCalculatorTool(); |
| 216 | + |
| 217 | + const result = await tool.function({ |
| 218 | + operation: 'add', |
| 219 | + a: 5, |
| 220 | + b: 3 |
| 221 | + }); |
| 222 | + |
| 223 | + expect(result[0].text).toContain('8'); |
| 224 | + }); |
| 225 | +}); |
| 226 | +``` |
| 227 | + |
| 228 | +## Next Steps |
| 229 | + |
| 230 | +- Learn about [Tool Management](/guide/tool/management) for filtering and organizing tools |
| 231 | +- Explore [Tool Call Engine](/guide/tool/tool-call-engine) for advanced execution patterns |
| 232 | + |
| 233 | +{/* Placeholder: Add screenshots of tool development workflow */} |
| 234 | +{/* Placeholder: Add video tutorial for creating custom tools */} |
0 commit comments