$ show modelcontextprotocol/python-sdk
This is the official Python SDK for the Model Context Protocol (MCP), a protocol for integrating AI models with external tools, resources, and prompts. The SDK provides both a high-level decorator-based API (MCPServer) and a low-level handler-based API (Server) for building MCP servers, along with a full client implementation (Client) for connecting to servers. It supports multiple transports (stdio, SSE, streamable HTTP), OAuth 2.0 authentication, caching, subscriptions, progress reporting, structured output, and an extension system. The codebase is well-structured with clean separation of concerns, extensive test coverage (unit, integration, documentation-sourced), and reusable patterns for authentication, caching, path security, and URI template parsing. It is actively maintained with recent commits and a comprehensive CI pipeline.
The LLM identified 5 files worth highlighting for reuse:
Provides safe_join and path traversal detection functions (contains_path_traversal, is_absolute_path) that prevent directory traversal attacks when serving files. Reusable in any file-serving application or API that needs to validate user-supplied paths.
Implements RFC 6570 URI template parsing with ambiguity detection and parameter extraction. Can be extracted for any URI matching or routing system that needs to handle variable path segments.
Starlette middleware for Bearer token authentication with configurable token verification. Demonstrates a clean middleware pattern for HTTP services, including WWW-Authenticate challenge generation and scope-based access control.
Client-side response cache with configurable store (InMemoryResponseCacheStore with LRU eviction) and cache modes (refresh, bypass). Reusable caching layer for any API client that needs to cache responses with TTL and scope awareness.
AES-256-GCM encryption and HMAC verification for request state tokens, with key rotation and principal binding. Demonstrates a production-grade security pattern for distributed systems that need to pass encrypted state between services.