ClientSessionGroup raises KeyError when connecting a server that exposes no components
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
- Domain
- backend-api-design
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
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from modelcontextprotocol/python-sdk
-
v1 v2
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
modelcontextprotocol/python-sdk#3573 · 2 comments ·
-
v2
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
modelcontextprotocol/python-sdk#3566 ·
-
v1 v2
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
modelcontextprotocol/python-sdk#3546 · 5 comments ·
-
v1 v2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
modelcontextprotocol/python-sdk#3545 · 2 comments ·
-
v1 v2
Difficulty 1/5 Under an hour Newbie friendliness 91/100
modelcontextprotocol/python-sdk#3508 · 2 comments ·
All issues in modelcontextprotocol/python-sdk
Similar issues
-
area: harness bug status: needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Human-Agent-Society/reef#625 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 1/5 Under an hour Newbie friendliness 80/100
learningequality/kolibri#15351 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Name consistency Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
eellak/triplestore#65 · 1 comment ·