Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for cancelling in-flight Model Context Protocol (MCP) requests on the server when they time out or are abandoned by the client. It implements a custom _Session class to track request lifetimes and send notifications/cancelled messages, alongside comprehensive tests to validate this behavior. The review feedback highlights a critical issue regarding fragile private attribute access (self._request_id) and a potential race condition under concurrency, proposing a robust alternative that intercepts outgoing messages to capture request IDs safely.
d535af1 to
9ae52c1
Compare
huangjeff5
left a comment
There was a problem hiding this comment.
This rebases cleanly onto the new feat/py-mcp-host. Drove it against the fake server: a timed-out call and a cancelled call each reached the server as notifications/cancelled with the right id ({6: 'Request timed out', 7: 'Request cancelled'}), and a concurrent call on the same connection finished. Also ran it through ai.generate: aborting the generate cancels the tool task and the server gets the cancel. Nits inline.
9ae52c1 to
e9ca2fe
Compare
a313e25 to
93b8d9f
Compare
huangjeff5
left a comment
There was a problem hiding this comment.
Re-checked on main: the five nits hold. Drove it against the fake server: timed-out and cancelled calls each reach the server as notifications/cancelled with the right id and reason, a concurrent call finishes, and a cancel during close sends nothing. 3 runs, no flakes. Two nits inline.
Merge note: conflicts with #6930 and #6957 in _connection.py and the README.
93b8d9f to
87deb82
Compare
|
Closing here as Python SDK development has moved to https://github.com/genkit-ai/genkit-python and this PR has already been re-opened there as genkit-ai/genkit-python#120. |
Why
When an MCP tool call timed out, or the code waiting on it was cancelled, we never told the server. The SDK doesn't send
notifications/cancelled, so the server kept working on a call nobody wanted. Withrequest_timeout_millis=Nonethe client side leaked too: the abandoned call kept running until the connection closed.Changes
notifications/cancelledwith the request id, so the server can stop.Verification
New tests use the fake server, which now records every cancel notice it gets:
Part of #6194.