Skip to content

[v1.x] Fall back to "unknown" when package version metadata is None - #3507

Closed
arpankernel wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
arpankernel:fix-pkg-version-none-guard-v1x
Closed

arpankernel wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
arpankernel:fix-pkg-version-none-guard-v1x

Conversation

@arpankernel

Copy link
Copy Markdown

Fixes #3487

Problem

Server.create_initialization_options fills server_version from the local pkg_version("mcp") helper when no explicit version is set. That helper guards against importlib.metadata.version() raising, but not against it returning None (which happens on some environments, e.g. an embedded Python whose package metadata is present but unreadable). Because InitializationOptions.server_version is typed str, the None reaches the pydantic model and raises ValidationError, 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_options sets server_version=self.version directly and does not go through pkg_version.

Fix

Guard the return value as well as the exception, consistent with the helper already returning "unknown" on failure:

return version(package) or "unknown"

Test

Adds a regression test that patches importlib.metadata.version to return None and asserts create_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.

`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
@github-actions

Copy link
Copy Markdown
Contributor

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:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Sep 15, 2026
@github-actions github-actions Bot closed this Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant