Skip to main content

Tools

Tools are actions AI can perform — the primary way AI interacts with your system. When an AI agent needs to do something (send email, create task, query database), it calls a tool.
Tools are triggered by the AI agent, not the user. The LLM decides when to call a tool based on the user’s request and the tool’s description. Users don’t directly invoke tools — they ask the AI, and the AI decides which tools to use.

Testing Your Tools

Since tools are AI-triggered, you’ll need a way to test them:
  • MCP Inspectornpx @modelcontextprotocol/inspector http://localhost:3001/mcp
  • Claude Desktop / Cursor — Connect your MCP and chat with it
  • LeanMCP Sandbox — Test tools directly in the browser
  • Postman — Send raw MCP requests to your server
Every tool you add increases token usage. Keep descriptions concise and only expose tools the AI actually needs. See Reducing Tokens in MCPs for optimization strategies.

Tool Structure

Full Example: Task Manager

A complete task management service with CRUD operations:
How AI uses this:
  • “Create a task called ‘Review PR #42’ with high priority”
  • “List all tasks that are in progress”
  • “Mark task_1234 as complete”
  • “Delete the task about reviewing PR”

Schema Validation

Use @SchemaConstraint for input validation and better AI understanding:

Return Types

Tools can return different types:

Async Tools

Tools can be async for API calls, database queries, file operations:

Error Handling

Throw errors — they’re caught and returned properly to the AI:

Organizing Services

Group related tools into services. One file per domain:

Best Practices

AI uses descriptions to understand when to use tools.Bad: "Process data"Good: "Search customer orders by date range and status"
Every tool call costs tokens and time. Design tools that accomplish goals in one call, not a chain of 7-8 calls.❌ BAD: Hotel booking with 7+ tool callsEach call: AI generates request → waits for response → processes → generates next request. 7 round trips, thousands of tokens.✅ GOOD: One tool that does it all
One call, one response. The server handles the complexity, not the AI.
One tool, one job. Don’t combine unrelated actions.Bad: "manageUser" (create, update, delete in one)Good: "createUser", "updateUser", "deleteUser"
Use @SchemaConstraint for all inputs. AI makes fewer mistakes with validated schemas.
Return useful data AI can use in follow-up actions.Bad: return { success: true }Good: return { created: true, id: "123", name: "John" }
Throw clear errors. AI can explain issues to users.

The Three MCP Primitives

Understanding when to use each:

Next Steps

Resources

Expose data to AI agents

Prompts

Template prompts for AI