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 Inspector —
npx @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
Tool Structure
Full Example: Task Manager
A complete task management service with CRUD operations:- “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:- Objects
- Text
- Images
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
Write clear descriptions
Write clear descriptions
AI uses descriptions to understand when to use tools.Bad:
"Process data"Good: "Search customer orders by date range and status"Design for minimal tool calls
Design for minimal tool calls
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 allOne call, one response. The server handles the complexity, not the AI.
Keep tools focused
Keep tools focused
One tool, one job. Don’t combine unrelated actions.Bad:
"manageUser" (create, update, delete in one)Good: "createUser", "updateUser", "deleteUser"Validate all inputs
Validate all inputs
Use
@SchemaConstraint for all inputs. AI makes fewer mistakes with validated schemas.Return meaningful data
Return meaningful data
Return useful data AI can use in follow-up actions.Bad:
return { success: true }Good: return { created: true, id: "123", name: "John" }Handle errors gracefully
Handle errors gracefully
Throw clear errors. AI can explain issues to users.
The Three MCP Primitives
Understanding when to use each:| Primitive | Control | Purpose | Example |
|---|---|---|---|
| Tools | Model-driven | Actions the AI performs | Send email, create task, query API |
| Resources | Application-driven | Context data for AI | Files, preferences, schedules |
| Prompts | User-driven | Templates users invoke | /analyze, /review, /support |