feat(session-group): add read_resource and get_prompt routing

Closed
BA
1mo ago
+273-1

Fixes #3579

Add ClientSessionGroup.read_resource() and ClientSessionGroup.get_prompt() so the
group can route resource reads and prompt fetches to the owning server, the same way
call_tool() already does.

Motivation and Context

ClientSessionGroup aggregates tools, resources, and prompts from every connected
server into one view, and call_tool(name, ...) resolves a name to the session that
owns it and forwards the call. But that routing existed only for tools. A caller who
wanted to read an aggregated resource or fetch an aggregated prompt had to track the
owning ClientSession themselves and bypass the group entirely — an asymmetry in an
otherwise "one object, one view" abstraction.

This adds the two missing routed calls:

  • read_resource(name, ...)
  • get_prompt(name, arguments, ...)

Both resolve the aggregate key (honoring component_name_hook) to the owning session
via new _resource_to_session / _prompt_to_session reverse indexes, then forward the
resource's real wire URI / the prompt's real wire name. They carry the same
allow_input_required overloads as ClientSession, so a server that needs input
mid-call behaves identically through the group. The reverse indexes are cleaned up in
disconnect_from_server, matching how _tool_to_session is already handled.

How Has This Been Tested?

  • Unit tests (mock sessions) asserting each method resolves the aggregate key to the
    owning session and forwards the correct wire identifier and arguments, plus KeyError
    for unknown names.
  • In-memory end-to-end tests using Client(server) against real MCPServer instances:
    one reads a resource (library://hours), one fetches a prompt, both routed through the
    group.
  • A disconnect test confirming the resource/prompt reverse indexes are cleared.
  • Full local gate per AGENTS.md: pytest, ruff format, ruff check, pyright,
    100% coverage on src/mcp/client/session_group.py, strict-no-cover, and markdownlint
    on the docs page. All green.

Breaking Changes

None. This only adds two methods to an existing public class and internal reverse
indexes. No existing signature or 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

read_resource/get_prompt deliberately mirror the existing call_tool design: same
reverse-index routing, same "aggregate key in, real wire identifier out" behavior, and
the same allow_input_required overload pair. Docs updated in
docs/client/session-groups.md with a new "Reading resources and prompts" section and a
recap line.