Skip to content

Latest commit

 

History

History
79 lines (61 loc) · 2.66 KB

File metadata and controls

79 lines (61 loc) · 2.66 KB

MCP Python SDK Examples

Practical examples for building MCP (Model Context Protocol) servers and clients with the Python SDK.

Examples

# Example Concepts
01 Basic Server Tools, resources, and prompts in one server
02 Tools + Context Logging, progress reporting, async tools
03 Resources & Templates Static and dynamic URI-based resources
04 Structured Output Pydantic, TypedDict, dataclass return types
05 Prompts Code review, debug assistant, architecture decision templates
06 Lifespan Startup/shutdown lifecycle with shared DB & cache
07 Stdio Client Server discovery, tool calls, Anthropic API chat loop
08 Streamable HTTP Remote HTTP server + Starlette mounting
09 Images Color swatches, bar charts, gradients via Pillow
10 Complex Inputs Nested Pydantic validation, constraints, unions

Quick Start

# Install the MCP SDK
pip install "mcp[cli]"

# Install example dependencies
pip install -r requirements.txt

# Run any server example
python examples/01_basic_server.py

# Test with the MCP Inspector
mcp dev examples/01_basic_server.py

# Run the client (connects to 01_basic_server automatically)
python examples/07_stdio_client.py

# Interactive chat with Anthropic API + MCP tools
ANTHROPIC_API_KEY=sk-... python examples/07_stdio_client.py --chat

# HTTP server
python examples/08_streamable_http.py

Presentation

A Keynote-compatible slide deck is included: MCP_Python_SDK.pptx

Regenerate it with:

pip install python-pptx
python build_deck.py

Project Structure

├── examples/
│   ├── 01_basic_server.py          # Tools, resources, prompts
│   ├── 02_tools_with_context.py    # Context: logging & progress
│   ├── 03_resources_and_templates.py # Static & template resources
│   ├── 04_structured_output.py     # Typed return values
│   ├── 05_prompts.py               # Reusable prompt templates
│   ├── 06_lifespan.py              # Server lifecycle management
│   ├── 07_stdio_client.py          # Client + Anthropic API
│   ├── 08_streamable_http.py       # HTTP transport + Starlette
│   ├── 09_images.py                # Image generation tools
│   └── 10_complex_inputs.py        # Pydantic input validation
├── build_deck.py                   # Slide deck generator
├── MCP_Python_SDK.pptx
├── requirements.txt
└── .env.example

Requirements

  • Python 3.10+
  • MCP Python SDK 1.2.0+
  • Anthropic API key (for example 07 chat mode)