Skip to content

Conversation

@felixweinberger
Copy link
Contributor

Refactors unit tests to use the ergonomic Client class instead of the verbose InMemoryTransport + ClientSession pattern.

Motivation and Context

Follow-up from #1870. The new Client class provides a simpler API for testing MCP servers:

# Before
transport = InMemoryTransport(server)
async with transport.connect() as (read_stream, write_stream):
    async with ClientSession(read_stream, write_stream) as session:
        await session.initialize()
        result = await session.list_tools()

# After
async with Client(server) as client:
    result = await client.list_tools()

How Has This Been Tested?

All refactored tests pass. The stream_spy fixture continues to work with Client since it patches the underlying memory streams used by InMemoryTransport.

Breaking Changes

None - internal test refactoring only.

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 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

Files changed (5):

  • tests/client/transports/test_memory.py - 3 tests
  • tests/shared/test_session.py - 2 tests
  • tests/server/test_cancel_handling.py - 1 test
  • tests/shared/test_progress_notifications.py - 1 test
  • tests/client/test_list_methods_cursor.py - 6 tests

Net change: -64 lines (163 added, 227 removed)

Refactored tests to use the ergonomic Client class instead of the
verbose InMemoryTransport + ClientSession pattern:

- tests/client/transports/test_memory.py: 3 tests
- tests/shared/test_session.py: 2 tests
- tests/server/test_cancel_handling.py: 1 test
- tests/shared/test_progress_notifications.py: 1 test
- tests/client/test_list_methods_cursor.py: 6 tests (including stream_spy test)

The stream_spy fixture continues to work with Client since it patches
the underlying memory streams used by InMemoryTransport.

Github-Issue: #1891
@felixweinberger felixweinberger marked this pull request as ready for review January 16, 2026 20:52
@Kludex Kludex merged commit dcc9b4f into main Jan 17, 2026
48 of 49 checks passed
@Kludex Kludex deleted the fweinberger/refactor-unit-tests-client branch January 17, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants