Commit graph python-sdk/src/mcp/shared/session.py
Author SHA1 Message Date
Marcelo Trylesinski
3e0ab1e7ee
Drop AbstractAsyncContextManager for proper type hints (#257) 2025-03-11 13:17:15 +00:00
David Soria Parra
733db0c9cf
fix: enforce context manager usage for RequestResponder 2025-02-04 20:13:26 +00:00
David Soria Parra
08cfbe522a
fix: improve error handling and request cancellation for issue #88 2025-02-04 18:20:17 +00:00
David Soria Parra
827e494df4
feat: add request cancellation and in-flight request tracking
This commit adds support for request cancellation and tracking of
in-flight requests in the MCP protocol implementation. The key
architectural changes are:

1. Request Lifecycle Management:
   - Added _in_flight dictionary to BaseSession to track active requests
   - Requests are tracked from receipt until completion/cancellation
   - Added proper cleanup via on_complete callback

2. Cancellation Support:
   - Added CancelledNotification handling in _receive_loop
   - Implemented cancel() method in RequestResponder
   - Uses anyio.CancelScope for robust cancellation
   - Sends error response on cancellation

3. Request Context:
   - Added request_ctx ContextVar for request context
   - Ensures proper cleanup after request handling
   - Maintains request state throughout lifecycle

4. Error Handling:
   - Improved error propagation for cancelled requests
   - Added proper cleanup of cancelled requests
   - Maintains consistency of in-flight tracking

This change enables clients to cancel long-running requests and
servers to properly clean up resources when requests are cancelled.

Github-Issue:#88
2025-02-03 20:50:05 +00:00
David Soria Parra
37f6a3229c
fix: use pydantic Field with alias for _meta fields
Pydantic treats fields starting with underscore as private/hidden. To fix this,
we need to use Field with alias='_meta' to properly handle these fields while
keeping the external API unchanged. This fixes #103 where meta fields were not
being properly assigned in request contexts.
2024-12-21 23:16:24 +00:00
David Soria Parra
ed87ae9f06
rename mcp_python to mcp 2024-11-11 12:40:32 +00:00
Renamed from mcp_python/shared/session.py (Previous history)