Skip to content

fix(session-group): don't crash on servers with no components - #3571

Closed
baselarw wants to merge 1 commit into
modelcontextprotocol:mainfrom
baselarw:fix/session-group-empty-server-keyerror
Closed

baselarw wants to merge 1 commit into
modelcontextprotocol:mainfrom
baselarw:fix/session-group-empty-server-keyerror

Conversation

@baselarw

@baselarw baselarw commented Sep 23, 2026 •

Copy link
Copy Markdown

Fixes #3578

ClientSessionGroup._aggregate_components deleted the session from _session_exit_stacks whenever a server exposed no prompts, resources, or tools. Via connect_with_session (caller-supplied session, never registered) this raised KeyError; via connect_to_server it dropped the freshly-registered exit stack so a later disconnect_from_server could not close the transport.

Remove the erroneous cleanup so empty servers are aggregated like any other, and add regression tests for both entry points.

AI assistance: this change was drafted with AI help. I've reviewed it, tested it locally, and can explain the fix and reasoning in my own words.

Motivation and Context

A valid MCP server can register zero tools/resources/prompts (e.g. one that only does sampling/roots), and swallowed list_* failures leave the temp dicts empty too. In those cases the empty-server cleanup block ran and either crashed the connect (KeyError via connect_with_session) or dropped the session's exit stack so its transport leaked until group teardown (connect_to_server). The block was marked # pragma: no cover, so this path had no test coverage.

How Has This Been Tested?

  • Added a regression test for connect_with_session with an empty server: it no longer raises, the session is tracked, and its components stay empty.
  • Added a regression test for connect_to_server with an empty server: the exit stack is retained and a later disconnect_from_server closes it.
  • Full local gate per AGENTS.md: pytest, ruff format, ruff check, pyright, 100% coverage on src/mcp/client/session_group.py, and strict-no-cover. All green.

Breaking Changes

None. Removes an internal cleanup block that only ran on the empty-server edge case; no public signature or documented behavior changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The removed block also let me drop two now-covered # pragma: no cover markers on the stack-close lines in disconnect_from_server, since the new test exercises them.

ClientSessionGroup._aggregate_components deleted the session from
_session_exit_stacks whenever a server exposed no prompts, resources, or
tools. Via connect_with_session (caller-supplied session, never registered)
this raised KeyError; via connect_to_server it dropped the freshly-registered
exit stack so a later disconnect_from_server could not close the transport.

Remove the erroneous cleanup so empty servers are aggregated like any other,
and add regression tests for both entry points.
@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 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

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 #3578.

If a maintainer assigns you to #3578, 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 closed this Sep 23, 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.

ClientSessionGroup raises KeyError when connecting a server that exposes no components

1 participant