Calculator Operations Server
This example demonstrates how to build a simple MCP server in Python using the FastMCP framework. It’s perfect for understanding the basics of MCP tool implementation.Overview
The Calculator server shows:- FastMCP Framework: Quick server setup with minimal boilerplate
- Tool Registration: Simple decorator-based tool registration
- Input Validation: Parameter validation and error handling
- Basic Operations: Addition, subtraction, multiplication, and division
Complete Implementation
server.py
Understanding the Code
FastMCP Initialization
Tool Decorator
@mcp.tool() decorator automatically:
- Registers the function as an MCP tool
- Extracts parameter types from type hints
- Generates JSON schema for the tool
- Handles serialization/deserialization
Error Handling
Installation & Setup
Dependencies
Create apyproject.toml or requirements.txt:
pyproject.toml
requirements.txt:
requirements.txt
Installation
Running the Server
Standalone Mode
With MCP Inspector
Use the MCP Inspector to test your server:Using the Calculator
From Python Client
client.py
Expected Output
Testing Operations
Addition
Subtraction
Multiplication
Division
Error Case: Division by Zero
Configuration for Claude Desktop
claude_desktop_config.json
Extending the Calculator
Adding More Operations
Scientific Calculator
Key Advantages of FastMCP
- Minimal Boilerplate: No need to manually define schemas
- Type Hints: Automatic parameter validation from Python types
- Easy Debugging: Clear error messages and stack traces
- Rapid Development: Get a server running in minutes
Common Patterns
Input Validation
Default Parameters
Troubleshooting
Server Not Starting
- Ensure
mcppackage is installed:pip install mcp - Check Python version:
python --version(should be 3.10+) - Verify the script has no syntax errors
Tool Not Found
- Ensure the
@mcp.tool()decorator is present - Check that the function is defined before
mcp.run() - Verify the server is running in stdio mode
Type Errors
- Use proper type hints:
float,int,str, etc. - FastMCP uses type hints for validation
Next Steps
- Learn about State Management with the Todo server
- Explore API Integration for production patterns
- See how to use this with Ollama