Title: feat: add MCP proxy pattern convenience function (#12) by dgenio · Pull Request #1711 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: feat: add MCP proxy pattern convenience function (#12) by dgenio · Pull Request #1711 · modelcontextprotocol/python-sdk
X Title: feat: add MCP proxy pattern convenience function (#12) by dgenio · Pull Request #1711 · modelcontextprotocol/python-sdk
Description: Description Implements the mcp_proxy() convenience function that enables bidirectional message forwarding between two MCP transports, porting the TypeScript proxy pattern to the Python SDK. Changes Added mcp_proxy() context manager in src/mcp/shared/proxy.py Implements bidirectional message forwarding using anyio task groups Supports optional error handling with sync/async callbacks Automatically closes both transports when one closes Comprehensive test coverage in tests/shared/test_proxy.py Features Bidirectional forwarding: Messages flow in both directions simultaneously Error handling: Optional onerror callback (supports both sync and async) Automatic cleanup: When one transport closes, the other is also closed Exception handling: Properly handles SessionMessage and Exception objects from read streams Resource management: Uses context manager pattern for proper cleanup Usage Example from mcp.shared.proxy import mcp_proxy async with mcp_proxy( transport_to_client=(client_read, client_write), transport_to_server=(server_read, server_write), onerror=lambda e: logger.error(f"Proxy error: {e}"), ): # Proxy is active, forwarding messages bidirectionally await some_operation() Both transports are closed when exiting the context## Testing Unit tests cover bidirectional forwarding, error handling, stream cleanup, and edge cases All tests pass with proper async/await patterns Follows existing test patterns in the repository Related Closes #12 Type of Change New feature (non-breaking change which adds functionality) Bug fix (non-breaking change which fixes an issue) Breaking change (fix or feature that would cause existing functionality to not work as expected) Documentation update Checklist Code follows the repository's style guidelines Tests added/updated for new functionality All tests pass locally Linting and type checking pass Documentation updated (if applicable)
Open Graph Description: Description Implements the mcp_proxy() convenience function that enables bidirectional message forwarding between two MCP transports, porting the TypeScript proxy pattern to the Python SDK. Changes...
X Description: Description Implements the mcp_proxy() convenience function that enables bidirectional message forwarding between two MCP transports, porting the TypeScript proxy pattern to the Python SDK. Changes...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/1711
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6df9f8c2-b456-679f-a216-7be9c9cc26db |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9BE2:3BA98A:28F38CD:3B438FA:6A60CE08 |
| html-safe-nonce | b77db0eb6f674285c0948883de71062daabe79b0c9cbcffa8e4a22289c8a6c8b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkUyOjNCQTk4QToyOEYzOENEOjNCNDM4RkE6NkE2MENFMDgiLCJ2aXNpdG9yX2lkIjoiNjczNTY2Mzg1NzY2MTk1NTU5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d4faa65cbfff2baf34d20048a828be07b80eeacea540371392b5c2baedf7b5f2 |
| hovercard-subject-tag | pull_request:3060218210 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/1711/files |
| twitter:image | https://avatars.githubusercontent.com/u/12731907?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/12731907?s=400&v=4 |
| og:image:alt | Description Implements the mcp_proxy() convenience function that enables bidirectional message forwarding between two MCP transports, porting the TypeScript proxy pattern to the Python SDK. Changes... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | a9556c67fd2e63f5fecafc192915a3b089ef8170a9384ff6b59d7b4a2f6b73f8 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f464e59c3a800187a50f3c89c36ee16df9992c1b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width