[v1.x] Fall back to "unknown" when package version metadata is None - #3507
arpankernel wants to merge 1 commit into
Conversation
`Server.create_initialization_options` fills `server_version` from
`pkg_version("mcp")` when no explicit version is set. That helper caught
exceptions from `importlib.metadata.version` but not the case where it returns
None instead of a string, which happens on some environments (an embedded
Python whose package metadata is present but unreadable). Because
`InitializationOptions.server_version` is typed `str`, the None propagated into
the model and raised a ValidationError, killing the server before the stdio
handshake and surfacing to the client as a bare "Connection closed".
Fall back to "unknown" for a None (or empty) version, matching the helper's
existing behaviour on failure. Add a regression test.
Github-Issue: modelcontextprotocol#3487
Reported-by: j-brumback
|
This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3487. If a maintainer assigns you to #3487, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take. You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way. CONTRIBUTING.md has the full reasoning, but in short:
Maintainers: reopen, remove |
Fixes #3487
Problem
Server.create_initialization_optionsfillsserver_versionfrom the localpkg_version("mcp")helper when no explicit version is set. That helper guards againstimportlib.metadata.version()raising, but not against it returningNone(which happens on some environments, e.g. an embedded Python whose package metadata is present but unreadable). BecauseInitializationOptions.server_versionis typedstr, theNonereaches the pydantic model and raisesValidationError, so the server dies before the stdio handshake and the client only sees a bare "Connection closed" with nothing actionable.This is v1.x-only: on
main(v2)create_initialization_optionssetsserver_version=self.versiondirectly and does not go throughpkg_version.Fix
Guard the return value as well as the exception, consistent with the helper already returning
"unknown"on failure:Test
Adds a regression test that patches
importlib.metadata.versionto returnNoneand assertscreate_initialization_options().server_version == "unknown". It fails before the change (ValidationError) and passes after. Full suite and 100% coverage pass locally.The reporter (@j-brumback) confirmed the root cause and asked for this fix in #3487.