Skip to content

Forward a client's cancellation to vMCP backends - #7342

Merged
rdimitrov merged 1 commit into
mainfrom
fix/vmcp-forward-cancellation
Nov 26, 2026
Merged

rdimitrov merged 1 commit into
mainfrom
fix/vmcp-forward-cancellation

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Summary

When a client cancels a tool call, the vMCP now passes the cancellation on to the backend, so the backend tool stops instead of running to completion.

Before: the client sends notifications/cancelled (Legacy) or closes its request (Modern). The vMCP cancels its own handler and answers context canceled, but the backend's tool never sees its context cancelled. With separate processes this happened every time; in-process it was intermittent.

After: the backend tool's context is cancelled, on Legacy and on Modern.

Cause

For a Legacy backend, the vMCP opens a client per request and closes it when the request returns. When the request's context is cancelled, go-sdk (v1.8.0, and still in v1.9.0-pre.1) retires the call at once and sends notifications/cancelled from a background goroutine, so the caller is not held up. The vMCP's deferred Close() ran immediately; once closing has started and no call is in flight, go-sdk's Notify refuses to write, and the notice was dropped.

A Modern client hits the same path whenever the backend is Legacy: closing the request cancels the vMCP handler, which cancels the Legacy backend call.

Fix

closePerCallClient replaces the deferred Close() on the five Legacy per-request paths (list, call, read, prompt, complete). A request that ended normally closes its client at once, as before. A cancelled one closes it in the background after 6 seconds, which is longer than go-sdk's 5-second budget for sending the notice. The vMCP's reply is not delayed; the cost is a backend connection kept open for up to 6 seconds after a cancellation.

The pinned per-session backend client (pkg/vmcp/session/internal/backend) is long-lived and not closed after each call, so it is unaffected.

A cleaner fix belongs in go-sdk: Close() could send pending cancel notices before closing. If that lands, the delay can go.

Testing

  • New pkg/vmcp/server/cancellation_realbackend_integration_test.go: a real Legacy backend whose tool blocks until cancelled, behind the real vMCP server and backend client. One test cancels through a Legacy client (notifications/cancelled), the other closes a Modern request; each repeats 5 times. Both fail on main in the first round and pass with the fix.
  • go test -race ./pkg/vmcp/...
  • task lint

🤖 Generated with Claude Code

When a call to a Legacy backend was cancelled, the vMCP closed its
per-request backend client before go-sdk had sent notifications/cancelled
from its background goroutine, so the notice was dropped and the backend
tool ran to completion. Keep a cancelled request's client open long enough
for the notice to go out, then close it in the background.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.42%. Comparing base (9300f3f) to head (ba1b93d).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7342      +/-   ##
==========================================
+ Coverage   82.40%   82.42%   +0.02%     
==========================================
  Files         937      937              
  Lines       95821    95844      +23     
==========================================
+ Hits        78962    79002      +40     
+ Misses      16854    16837      -17     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rdimitrov
rdimitrov merged commit ad8336b into main Nov 26, 2026
47 checks passed
@rdimitrov
rdimitrov deleted the fix/vmcp-forward-cancellation branch November 26, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants