Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

ClientSessionGroup raises KeyError when connecting a server that exposes no components

Open Beginner friendly
#3,578 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Look at the ClientSessionGroup class, specifically the _aggregate_components method. The issue is in the conditional block that deletes a session from _session_exit_stacks when no components are exposed. The fix is to remove that block or handle the case where the session isn't in the dictionary. Check the connect_with_session and connect_to_server methods to understand the flow. Run the provided minimal reproduction to verify the fix.

Written by the indexing model from the issue text.

Description

v1 v2
Initial Checks
Release line

2.x (current stable)

Description
What I ran into

ClientSessionGroup._aggregate_components deletes the session from
_session_exit_stacks whenever a connected server exposes no prompts, resources,
or tools:

if not any((prompts_temp, resources_temp, tools_temp)):
    del self._session_exit_stacks[session]

Via connect_with_session(server_info, session) the caller supplies a session
that was never registered in _session_exit_stacks, so this raises KeyError.
Via connect_to_server it drops the freshly-registered exit stack, so a later
disconnect_from_server can't close that session's transport.

Why it matters

A valid MCP server can legitimately register zero tools/resources/prompts (e.g.
one that only does sampling/roots), and list_* failures are swallowed into
empty dicts too. In both cases connecting an otherwise-fine server either crashes
or leaks a transport until group teardown.

Minimal reproduction
group = ClientSessionGroup()
# server_info + an in-memory session whose list_tools/resources/prompts are empty
await group.connect_with_session(server_info, session)  # -> KeyError
Suggested fix

Remove the empty-server cleanup block so an empty server is aggregated like any
other. Happy to open a PR (branch already prepared).

Example Code

Python & MCP Python SDK
Python 3.13.2
MCP Python SDK: main @ f1b6589 (dev build 0.0.1.dev1063)
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 16h
Merged PRs (30d)
25

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelcontextprotocol/python-sdk

All issues in modelcontextprotocol/python-sdk

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.