Use this file to discover all available pages before exploring further.
The Python samples use FastMCP, a high-level decorator-based API that makes it straightforward to define tools, resources, and prompts with minimal boilerplate.
When run directly in a terminal you will see JSON-RPC validation errors — this is expected. The server waits for properly formatted MCP client messages on stdin.
#!/usr/bin/env python3"""Sample MCP Calculator Server implementation in Python."""from mcp.server.fastmcp import FastMCP# Create a FastMCP servermcp = FastMCP("Calculator MCP Server")@mcp.tool()def add(a: float, b: float) -> float: """Add two numbers together and return the result.""" return a + b@mcp.tool()def subtract(a: float, b: float) -> float: """Subtract b from a and return the result.""" return a - b@mcp.tool()def multiply(a: float, b: float) -> float: """Multiply two numbers together and return the result.""" return a * b@mcp.tool()def divide(a: float, b: float) -> float: """ Divide a by b and return the result. Raises: ValueError: If b is zero """ if b == 0: raise ValueError("Cannot divide by zero") return a / bif __name__ == "__main__": mcp.run()
FastMCP reads type hints and docstrings to automatically generate the MCP tool schema — no manual JSON schema required.
Located at 04-PracticalImplementation/samples/python, this sample demonstrates all three MCP primitives — tools, resources, and prompts — along with a matching async client.
from mcp.server.fastmcp import FastMCPimport logging, jsonlogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)AVAILABLE_MODELS = ["gpt-4", "llama-3-70b", "claude-3-sonnet"]mcp = FastMCP("Python MCP Demo Server")@mcp.tool()def completion(model: str, prompt: str, temperature: float = 0.7, max_tokens: int = 100) -> str: """Generate completions using AI models.""" if model not in AVAILABLE_MODELS: raise ValueError(f"Model {model} not supported") return f"Simulated response to: {prompt[:30]}..."@mcp.tool()def add(a: int, b: int) -> int: """Add two numbers together.""" logger.info(f"Adding {a} and {b}") return a + b