Skip to main content

Tools System Architecture

Qwen Code’s tools system provides the AI model with capabilities to interact with the local environment. This guide explains how the tools system works and how to extend it.

Overview

The tools system is built on these core principles:
  • Self-describing: Tools define their parameters using JSON Schema
  • Type-safe: Full TypeScript type safety throughout
  • Extensible: Easy to add new tools
  • Secure: Validation and approval mechanisms
  • Testable: Comprehensive test coverage

Architecture

Core Components

Tool Lifecycle

  1. Registration: Tool registered in ToolRegistry
  2. Discovery: Model receives tool definitions (name, description, schema)
  3. Invocation: Model requests tool use with parameters
  4. Validation: Parameters validated against schema and business rules
  5. Confirmation: User approval for dangerous operations (if needed)
  6. Execution: Tool executes and returns results
  7. Response: Results sent back to model

Base Tool Class

All tools extend BaseDeclarativeTool:

Tool Kinds

Tool Invocation

Each tool execution creates a ToolInvocation instance:

Tool Result

Tools return a ToolResult:

Creating a New Tool

Step 1: Create Tool File

Create a new file in packages/core/src/tools/:

Step 2: Add Tool Names

Update packages/core/src/tools/tool-names.ts:

Step 3: Register Tool

Register in packages/core/src/config/config.ts:

Step 4: Add Tests

Create packages/core/src/tools/my-tool.test.ts:

Advanced Features

Confirmation Dialogs

For tools that need user approval:

Progress Updates

For long-running operations:

File Path Validation

Common pattern for file-based tools:

Built-in Tools

Qwen Code includes these built-in tools:

File System Tools

  • read_file - Read file contents
  • write_file - Write to files
  • edit - Edit files with string replacement
  • glob - Find files by pattern
  • grep_search - Search file contents
  • list_directory - List directory contents
See File System Tools

Execution Tools

  • run_shell_command - Execute shell commands
See Shell Tool

Delegation Tools

  • task - Delegate to subagents
See Task Tool

Memory Tools

  • save_memory - Save long-term memories
See Memory Tool

Integration Tools

  • MCP tools (dynamically loaded)
  • LSP tools (language server integration)
  • Web fetch and search

Tool Registry

The ToolRegistry manages all available tools:

Best Practices

  1. Clear Descriptions: Write clear, detailed descriptions for the model
  2. Validation: Validate all parameters thoroughly
  3. Error Handling: Provide helpful error messages
  4. Testing: Write comprehensive tests
  5. Documentation: Document parameters and behavior
  6. Security: Validate paths, sanitize inputs
  7. Performance: Optimize for common cases
  8. Telemetry: Log usage for analytics (respecting privacy)

Next Steps