MCP Server Setup Guide
This document outlines the process for creating new MCP servers with custom tools.
1. Project Structure Setup
2. Package Configuration
Create pyproject.toml
:
3. Server Implementation Steps
3.1 init.py Template
3.2 server.py Template
- Name: Clear, hyphen-separated
- Description: Concise, action-oriented
- Input Schema: JSON Schema format
- Define all parameters
- Mark required parameters
- Include clear descriptions
4.2 Response Types
Available response types:
types.TextContent
: For text responses
types.ImageContent
: For image data
types.EmbeddedResource
: For other resources
4.3 Error Handling
- Validate all input parameters
- Use clear error messages
- Log important operations
- Handle edge cases gracefully
5. Testing
- Install the package in development mode:
- Run the server:
6. Claude Desktop Integration
Update Claude Desktop configuration (claude_desktop_config.json
):
Best Practices
-
Logging
- Use the
log()
function for debugging
- Log important operations and errors
- Include relevant context in logs
-
Error Handling
- Validate all inputs
- Return clear error messages
- Handle edge cases gracefully
-
Tool Design
- Keep tools focused and single-purpose
- Use clear, descriptive names
- Provide comprehensive parameter descriptions
- Return structured, useful responses
-
Code Organization
- Keep server.py focused on MCP protocol
- Move complex logic to separate modules
- Use type hints consistently
- Document all public functions
-
Testing
- Test tools with various inputs
- Verify error handling
- Check response formats
- Validate schema compliance