Skip to content

Conversation

@maxisbey
Copy link
Contributor

Adds a convenience function for proxying messages between two MCP transports, enabling bidirectional message forwarding with proper error handling.

Motivation and Context

This implements issue #12 - porting the TypeScript proxy pattern to Python. It's a simpler reimplementation of #1711/#1763 that addresses all review feedback from those PRs.

Key differences from #1763:

  • 72 lines vs 144 - simpler design with inline helper
  • No pragma: no cover - all code paths are tested
  • No timing sleeps in tests - uses anyio.Event for synchronization
  • No logging - errors propagate to caller
  • Correct stream ownership - proxy doesn't close streams it doesn't own

How Has This Been Tested?

13 unit tests covering:

  • Bidirectional forwarding
  • Error callback (sync and async)
  • Stream closure handling (ClosedResourceError, BrokenResourceError)
  • Error callback exceptions (ignored, proxy continues)
  • Context exit cleanup

All tests pass with pytest-xdist parallel execution.

Breaking Changes

None - this is a new feature.

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

Closes #12

Related PRs: #1711, #1763

AI Disclaimer

Adds a convenience function for proxying messages between two MCP transports,
enabling bidirectional message forwarding with proper error handling.

Features:
- Bidirectional forwarding between client and server transports
- Optional error callback (sync or async) for exceptions on streams
- Graceful handling of closed/broken streams
- Clean shutdown on context exit

This is a simpler reimplementation of the proxy pattern from #1711/#1763,
addressing all review feedback.
@maxisbey maxisbey closed this Dec 11, 2025
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.

Add conveniences for MCP proxy pattern

2 participants