The official Python SDK for Model Context Protocol servers and clients
by modelcontextprotocolPython
Last 12 weeks · 178 commits
5 of 6 standards met
Summary keep / balance validation strict split the missing-follow-up case into a clearer add a direct regression test for an assistant followed by user text instead of Related to #2960. This PR does not relax the balance rule; it only makes the diagnostic distinguish a missing from an ID mismatch when a is followed by a non-result message. Tests
Release line 2.x (), observed at b2025ab8 and on #3331. Bug description When an MCPServer tool's return type is self-referential, pydantic emits the output schema as with no root . That's fine on 2026-07-28 sessions, but the 2025-11-25 shape requires at the root, so serializing the result for a legacy-negotiated client fails validation and the client gets an error for the entire listing, not just that tool. On this affects recursive return types. #3331 hands returns to pydantic natively, so recursive s join them (previously the hand-built mirror model happened to inline the root). Steps to reproduce Server log on the legacy session: Expected behaviour succeeds on both protocol versions; the recursive tool's schema has an object root (e.g. inline the root , or wrap it as ). Separately, it seems worth deciding whether one tool's unrepresentable schema should fail the whole listing on a legacy session or just drop/degrade that tool. AI Disclaimer
Description Summary MCP tools and resources built with the Python SDK can, by default, access the full file system, network, and environment of the host process. This is powerful but risky, especially when: servers are installed from third parties, or LLM agents can call tools based on prompt-injected instructions. The SDK should clearly document this and provide hooks/patterns for limiting capabilities. Proposal 1. Document security considerations Add a “Security considerations” section explicitly calling out: file system access, network access, environment variables and credentials. 2. Provide capability hooks Offer configuration points or helper utilities to: restrict file access to specific directories (allowlist), restrict outbound network calls to certain hosts/domains, prevent access to environment variables by default. 3. Encourage process-level sandboxing Provide guidance on running MCP servers in: containers with minimal privileges, separate processes with limited OS capabilities. Why this matters Defense in depth: Even if application code is careful, configuration mistakes or third-party code can introduce risk. Prompt-injection mitigation: Restricting what tools can do reduces the impact of malicious prompts. Operational guidance:** Many users are new to MCP and benefit from clear security best practices. Acceptance criteria [ ] Documentation clearly describes the security implications of running MCP servers and tools. [ ] There are example patterns for restricting file and network access. [ ] Where feasible, the SDK exposes hooks or configuration points for capability control. References _No response_
Fixes # Motivation and Context How Has This Been Tested? Breaking Changes Types of changes [ ] Bug fix (non-breaking change which fixes an issue) [ ] New feature (non-breaking change which adds functionality) [ ] Breaking change (fix or feature that would cause existing functionality to change) [ ] Documentation update Checklist [ ] I am assigned to the linked issue (or it is labeled , or I'm a maintainer) [ ] I have disclosed any AI assistance and can explain the change in my own words [ ] I have read the MCP Documentation [ ] My code follows the repository's style guidelines [ ] New and existing tests pass locally [ ] I have added appropriate error handling [ ] I have added or updated documentation as needed Additional context
Summary This PR addresses issue #348 by adding support for returning tool errors with non-text content (images, audio, structured data) without raising exceptions. Changes Core Changes ( and version-specific models) Added classmethod for creating error results with non-text content Added to auto-convert SDK Image/Audio helpers to their wire content types (, ) Server Integration () Updated to handle Image/Audio helpers when passed directly in Tests () Added tests for image, audio, and structured content error results Usage Example Backwards Compatibility Existing code using with continues to work The auto-conversion only applies when helper objects are detected (no breaking changes) Testing All 5,747 existing tests pass + 3 new tests added for the new functionality.
Summary This PR addresses issue #1401 by adding a dedicated callback to and classes. This allows users to properly handle transport-level exceptions (timeouts, connection errors, etc.) instead of having them silently swallowed by the default . Changes Core Changes () Added new protocol for the callback type Added parameter to Updated to use the new handler with fallback to for backwards compatibility Modified to log transport exceptions at ERROR level (so users are aware of issues even without a custom handler) Client Integration () Added field to dataclass Updated to pass the handler to Tests () Added : verifies transport exceptions go to the dedicated handler while server notifications go to Added : verifies backwards compatibility when no dedicated handler is provided Usage Example Backwards Compatibility Existing code using continues to work unchanged When no is provided, transport exceptions fall back to The default now logs transport exceptions at ERROR level Testing All existing tests pass (5744 tests). Two new tests added for the new functionality.
Repository: modelcontextprotocol/python-sdk. Description: The official Python SDK for Model Context Protocol servers and clients Stars: 24097, Forks: 3831. Primary language: Python. Languages: Python (99.8%), Shell (0.2%), HTML (0%). License: MIT. Homepage: https://py.sdk.modelcontextprotocol.io/ Topics: mcp, mcp-client, mcp-server, python. Latest release: v2.0.0 (3w ago). Open PRs: 100, open issues: 298. Last activity: 2d ago. Community health: 87%. Top contributors: dsp-ant, maxisbey, Kludex, ihrpr, jspahrsummers, felixweinberger, nick-merrill, jerome3o-anthropic, calclavia, dependabot[bot] and others.