Skip to content

Tool Development Overview MCP

ChatAI Plugin implements its tool system based on MCP (Model Context Protocol) standard, supporting three tool sources.

Tool Sources

Three Tool Sources

Choose the appropriate development method based on your needs, from simple to complex: Custom JS → Built-in Tools → External MCP

SourceLocationDescriptionHot Reload
Built-in Toolssrc/mcp/tools/Core functionality, 22 categorized modules
Custom JSdata/tools/User scripts, no source code modification needed
External MCPdata/mcp-servers.jsonnpm packages or remote servers

Tool Definition Format

All tools follow the MCP standard unified definition format:

javascript
{
  // Tool name (unique identifier, snake_case format)
  name: 'my_tool',
  
  // Tool description (visible to AI, clear description helps AI call correctly)
  description: 'Tool function description, explain when to use and parameter meanings',
  
  // Parameter definition (JSON Schema format)
  inputSchema: {
    type: 'object',
    properties: {
      param1: {
        type: 'string',
        description: 'Parameter description'
      }
    },
    required: ['param1']
  },
  
  // Handler function (async)
  handler: async (args) => {
    // Implementation logic
    return { result: '...' }
  }
}

Context Access

Built-in tools access runtime context through ToolContext to get Bot, event, permissions info.

ToolContext API

ToolContext is the core context object during tool execution, defined in src/mcp/BuiltinMcpServer.js

javascript
import { getBuiltinToolContext } from '../../mcp/BuiltinMcpServer.js'

handler: async (args) => {
  const ctx = getBuiltinToolContext()
  
  // Get Bot instance (handles multi-Bot environment automatically)
  const bot = ctx.getBot()
  // Support specifying Bot ID
  const specificBot = ctx.getBot(botId)
  
  // Get message event
  const event = ctx.getEvent()
  const userId = event?.user_id
  const groupId = event?.group_id
  
  // Check if master
  const isMaster = ctx.isMaster
  
  // Get adapter info
  const adapter = ctx.getAdapter()
  // Returns: { adapter: 'icqq'|'napcat'|'onebot', isNT: boolean, canAiVoice: boolean }
  
  // Shortcut methods
  ctx.isIcqq()    // Is ICQQ adapter
  ctx.isNapCat()  // Is NapCat adapter
  ctx.isNT()      // Supports NT features
  
  // Get Bot permission in group
  const permission = await ctx.getBotPermission(groupId)
  // Returns: { role: 'owner'|'admin'|'member', isAdmin: boolean, isOwner: boolean, inGroup: boolean }
}

Return Format

Tool return values are automatically serialized and returned to AI:

javascript
return { text: 'Result text' }
javascript
return { 
  success: true,
  data: { key: 'value' }
}
javascript
return {
  text: 'Current time: 14:30',
  datetime: '2024-12-15T06:30:00.000Z',
  timestamp: 1702622400000
}
javascript
// Method 1: Throw error
throw new Error('Operation failed: Permission denied')

// Method 2: Return error object
return { error: true, message: 'Operation failed' }

Return Value Notes

  • Return values should be concise, avoid returning large amounts of irrelevant data
  • text field will be shown directly to AI, should be human-readable format
  • Structured data is suitable for scenarios requiring further processing

Development Workflow

StepDescriptionKey Points
1. Determine TypeChoose built-in/custom/MCPUse custom JS for simple features, built-in for complex
2. Define InterfaceName, description, parametersClear description, use JSON Schema for params
3. Implement LogicWrite handler functionHandle exceptions and check permissions
4. Test & VerifyAPI test or conversation testUse #工具日志 to view call details
5. Deploy & EnableConfigure permissions and enableManage tool enable status via Web panel

Detailed Documentation

📚 Choose the Right Development Method

DocumentUse CaseDifficulty
Built-in ToolsDeep integration, access internal APIs⭐⭐⭐
Custom JS ToolsQuick development, no source modification
MCP ServerExternal services, reuse existing MCP⭐⭐
SecurityUnderstand tool security mechanisms⭐⭐

Released under the MIT License