Skip to content

fix(py/plugins/mcp): say which server failed and why - #6930

Merged
hilariie merged 4 commits into
mainfrom
fix/py-mcp-error-server-name
Nov 10, 2026
Merged

hilariie merged 4 commits into
mainfrom
fix/py-mcp-error-server-name

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

Why

Connection errors named only the server's command, so two servers in a host that run the same command (say, two npx servers) gave identical errors, and timeout and startup errors dropped the reason the SDK or OS gave. A server that sent a malformed tools/list or tools/call result escaped as a raw pydantic ValidationError instead of a GenkitError. Part of #6194.

Changes

  • Connection errors name the client, or the server within a host, alongside its command. The "MCP connection ended" log line carries the name too.
  • Timeout and connection-failure errors now carry their cause, so the message ends with the SDK's or OS's own text, for example [Errno 2] No such file or directory.
  • Any request failure that is not a JSON-RPC error reply, such as a malformed result, is reported as _ProtocolError (INTERNAL) with the original exception as its cause. The package's own errors pass through unchanged and the connection stays open.
  • Module-only constants in _connection.py and _tools.py are now _-prefixed.

Verification

  • New tests: errors from two host servers sharing a command name each server; timeout and startup errors carry their cause; a fake server answering with an empty result gets a _ProtocolError caused by the ValidationError, the connection stays open, and the server env stays out of a rendered traceback.
  • Unit tests cover the pass-through and the mapping in _answered_error.

@hilariie hilariie added this to the py-1.0.0 milestone Oct 28, 2026
@hilariie
hilariie requested a review from cabljac October 28, 2026 12:17

@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 a name parameter to McpConnection to distinguish between different MCP servers/clients, particularly when they share the same command. Error messages and logs have been updated to include this name via a new _target property. Additionally, several module-level constants in _connection.py and _tools.py were made private, and error handling was improved to report malformed replies as protocol errors with proper causes. Test suites were updated to accommodate these changes and verify the new behavior. I have no feedback to provide as there are no review comments.

@hilariie hilariie changed the title fix(py/plugins/mcp): name the client or host server in connection errors fix(py/plugins/mcp): say which server failed and why Oct 28, 2026
@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.

heads up: feat/py-mcp-host got rebased onto main today, so GitHub shows the whole genkit-mcp package as added and this conflicts. The real change is git diff d535af1cc..HEAD; that is what I reviewed. The conflict is in mcp_connection_test.py.

Tried the three cases by hand: a missing binary reads UNAVAILABLE: MCP connection to 'my-broken-client' ('/usr/bin/nonexistent-binary-xyz') failed: [Errno 2] No such file or directory, two servers sharing python3 are told apart, and a malformed tools/list is INTERNAL ... ValidationError with the cause kept. Approving with two rebase notes.

Base automatically changed from feat/py-mcp-host to main October 30, 2026 10:54

@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 three threads hold. Verified the error strings by hand for a client, a hosted server, and the same server name on two hosts. A malformed tools/list is INTERNAL with the cause kept. Error text carries name/host/command only, no args or env.

Merge note: landing this before #6957/#6958 is easiest. Both build McpConnection without name and will need it on rebase.

  • py/packages/genkit-mcp/src/genkit_mcp/_host.py:289: Question, not for this PR: get_active_tools skips an unreachable or timed-out server, but a malformed tools/list raises _ProtocolError and fails the whole host listing. Main did the same before (as a bare ValidationError). Should a malformed reply be skipped the same way?

@hilariie

Copy link
Copy Markdown
Contributor Author

Agreed it should probably match the unreachable/timeout case. I'll pick that up as a follow-up rather than widen this PR: genkit-ai/genkit-python#200

@hilariie
hilariie merged commit d0d2e7a into main Nov 10, 2026
26 checks passed
@hilariie
hilariie deleted the fix/py-mcp-error-server-name branch November 10, 2026 11:51
@huangjeff5

Copy link
Copy Markdown
Contributor

Ported to genkit-python in genkit-ai/genkit-python#72 (git am -p2, authorship preserved, Upstream-PR trailer points back here). Python changes go to genkit-python from now on.

huangjeff5 added a commit to genkit-ai/genkit-python that referenced this pull request Nov 10, 2026
Upstream-Commit: genkit-ai/genkit@d0d2e7a
Upstream-PR: genkit-ai/genkit#6930
Upstream-Commit: genkit-ai/genkit@494584f
Upstream-PR: genkit-ai/genkit#7153

Co-authored-by: Hilary Chukwuma <holaryc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants