Model Context Protocol (MCP) 1 cheatsheet
Model Context Protocol (MCP) 1: Python server
8 common Model Context Protocol (MCP) 1 tasks.
Create a Python server with a tool
Define a function the model can call. The docstring and type hints become the tool's description and schema.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Demo")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
if __name__ == "__main__":
mcp.run()
Expose a resource
Share read-only data that clients can load into context, here with a URI template.
@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
"""Greet someone by name."""
return f"Hello, {name}!"
Expose a prompt template
Offer a reusable prompt that clients can show to users.
@mcp.prompt()
def review_code(code: str) -> str:
return f"Please review this code:\n\n{code}"
Serve over HTTP
Run the server as a remote endpoint with the streamable HTTP transport instead of stdio.
mcp.run(transport="streamable-http")
Log without breaking stdio
A stdio server must not write to stdout, which carries protocol messages. Log to stderr.
import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
Install into Claude Desktop
Register the server in Claude Desktop's configuration.
mcp install server.py