Tools
Tools are the primary way MCP clients interact with your server. They represent functions that can be invoked with parameters and return results. This guide covers everything you need to know about creating powerful and reliable tools.Understanding Tools
Tools in MCP are:- Invocable Functions: Clients can call them with parameters
- Typed: Parameters and returns have defined types
- Async: All tool handlers are asynchronous
- Stateless: Each invocation is independent
Basic Tool Structure
Every tool has three main components:Defining Tool Inputs
Input Types
Tools support five parameter types:Optional Parameters and Defaults
Tool Callbacks
Basic Response
The simplest tool response is text:Multiple Content Items
Tools can return multiple content items:Including Resources
Tools can reference resources:Advanced Tool Patterns
Input Validation
Always validate inputs in your tool handlers:Async Operations
Tools naturally support async operations:Progress Reporting
For long-running operations, provide status updates:Tool Composition
Tools can work together:Tool Annotations
Add metadata to tools for better client integration:OpenAI Apps SDK Integration
For ChatGPT and OpenAI compatible clients:Error Handling Best Practices
Graceful Degradation
Input Sanitization
Testing Tools
Unit Testing Example
Manual Testing with Inspector
Use the built-in inspector for interactive testing:- Start your server with the inspector
- Navigate to
http://localhost:3000/inspector - Select your tool from the list
- Enter parameter values
- Execute and view results
Performance Optimization
Caching Results
Streaming Large Results
For large datasets, consider pagination:Best Practices
- Clear Naming: Use descriptive, action-oriented names
- Comprehensive Descriptions: Explain what the tool does and how to use it
- Input Validation: Always validate and sanitize inputs
- Error Handling: Provide helpful error messages
- Idempotency: Make tools idempotent when possible
- Documentation: Document complex tools with examples
- Testing: Write tests for critical tools
- Performance: Consider caching and pagination for expensive operations
Next Steps
- Resources Guide - Managing static and dynamic content
- UI Widgets - Creating interactive UI components
- API Reference - Complete API documentation
- Examples - Real-world tool implementations