Skip to content

fix(py/plugins/mcp): send notifications/cancelled when a call is cancelled or times out - #6958

Closed
hilariie wants to merge 3 commits into
mainfrom
fix/py-mcp-cancel-notification
Closed

hilariie wants to merge 3 commits into
mainfrom
fix/py-mcp-cancel-notification

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

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. With request_timeout_millis=None the client side leaked too: the abandoned call kept running until the connection closed.

Changes

  • A cancelled caller now cancels its call on the client side too. The connection stays open for other calls.
  • On a timeout or a cancel, the client sends notifications/cancelled with the request id, so the server can stop.
  • No notice is sent while the connection is closing or already broken, or for the initial handshake.

Verification

New tests use the fake server, which now records every cancel notice it gets:

  • a cancelled call sends one notice for its own id, and other calls keep running
  • a timed-out call sends one notice
  • closing the connection sends none

Part of #6194.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hilariie
hilariie requested a review from a team as a code owner October 29, 2026 18:45

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread py/packages/genkit-mcp/README.md Resolved
Base automatically changed from feat/py-mcp-host to main October 30, 2026 10:54
@hilariie hilariie changed the title fix(py/plugins/mcp): send notifications/cancelled when a call is canc… fix(py/plugins/mcp): send notifications/cancelled when a call is cancelled or times out Oct 30, 2026
@hilariie
hilariie force-pushed the fix/py-mcp-cancel-notification branch from a313e25 to 93b8d9f Compare October 30, 2026 14:25
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Oct 30, 2026

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation fix python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants