Skip to content

fix(mcp): back off before respawning a failed server - #100

Merged
hilariie merged 1 commit into
mainfrom
fix/mcp-respawn-backoff
Dec 2, 2026
Merged

hilariie merged 1 commit into
mainfrom
fix/mcp-respawn-backoff

Conversation

@hilariie

@hilariie hilariie commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Ports genkit-ai/genkit#6957.

Why

A server that can't start was started again on every tool listing. In a host it was worse: all servers share one listing, so the broken server got a new process every time the healthy ones were listed, and every time a tool call on any of them failed.

Changes

  • A connection that fails before completing a request, including one that never finishes its handshake, waits before the server is started again on that event loop: 1s, doubling with each failure in a row, up to 30s. During the wait, listings and tool calls fail at once with UNAVAILABLE and an error saying how long is left. A timed-out request does not count as completed.
  • A server that worked and then died is still replaced on next use, with no wait.
  • restart() / host.reconnect() start the server straight away and reset the wait.

Differences from upstream

  • Conflict in _client.py: kept this repo's McpConnection(self._name, ..., host=self._host) call.
  • The waiting error passes cause= instead of only setting __cause__, so its message and Dev UI entry say why the server failed, like the package's other errors.
  • test_a_server_that_timed_out_and_then_died_waits_too uses an 8s STALL_TIMEOUT_MILLIS instead of 3s, matching the host tests, since the timeout also bounds the handshake and 3s flaked under heavy load.
  • The restart test also checks that a restart resets the wait to its first delay.
  • restart() and host.reconnect() docstrings and the README say they skip the wait.

Part of genkit-ai/genkit#6194.

Fixes #192.

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

Port of genkit-ai/genkit#6957. A connection that fails before completing a
request, including one that never finishes its handshake, now waits before
the server is started again: 1s, doubling with each failure in a row, up to
30s. Meanwhile listings and tool calls fail at once with UNAVAILABLE. A
server that answered and then died is still replaced on next use, and
restart()/host.reconnect() skip the wait.

On top of upstream: the waiting error passes its cause so the message and
Dev UI say why the server failed, the timed-out-then-died test uses an 8s
timeout like the host stall tests, and the host docs say reconnect() skips
the wait.

Upstream-Commit: genkit-ai/genkit@b01ef02
Upstream-PR: genkit-ai/genkit#6957
@hilariie
hilariie force-pushed the fix/mcp-respawn-backoff branch from a28cdbd to 2be7286 Compare December 2, 2026 13:39
@hilariie
hilariie merged commit 66ec829 into main Dec 2, 2026
22 checks passed
@hilariie
hilariie deleted the fix/mcp-respawn-backoff branch December 2, 2026 14:18
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: a server that fails to start is respawned on every listing, with no backoff

2 participants