-
Notifications
You must be signed in to change notification settings - Fork 217
Add library for serving an sse server proxying a stdio server #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6cf925e
Add library for serving an sse server proxying a stdio server
allenporter 7ea5edd
Change context manager for running server in the background thread
allenporter efb673a
Fix lint errors in new test fixture
allenporter a9835c5
Update starlette response routing
allenporter 1210203
Fix ruff format errors
allenporter b2dc331
Fix ruff format errors
allenporter 5cfa7d0
Fix typos in SseServerSettigs
allenporter 5657f5d
Rename host to bind host and update to localhost
allenporter 78d8d92
Update for new import location
allenporter cae49f5
Update imports based on ruff rules
allenporter 255823c
Update src/mcp_proxy/sse_server.py
allenporter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Create a local SSE server that proxies requests to a stdio MCP server.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Literal | ||
|
|
||
| import uvicorn | ||
| from mcp.client.session import ClientSession | ||
| from mcp.client.stdio import StdioServerParameters, stdio_client | ||
| from mcp.server import Server | ||
| from mcp.server.sse import SseServerTransport | ||
| from starlette.applications import Starlette | ||
| from starlette.requests import Request | ||
| from starlette.routing import Mount, Route | ||
|
|
||
| from .proxy_server import create_proxy_server | ||
|
|
||
|
|
||
| @dataclass | ||
| class SseServerSettings: | ||
| """Settings for the server.""" | ||
|
|
||
| bind_host: str = "127.0.0.1" | ||
| port: int = 8000 | ||
| log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO" | ||
|
|
||
|
|
||
| def create_starlette_app(mcp_server: Server, debug: bool | None = None) -> Starlette: | ||
| """Create a Starlette application that can server the provied mcp server with SSE.""" | ||
| sse = SseServerTransport("/messages/") | ||
|
|
||
| async def handle_sse(request: Request) -> None: | ||
| async with sse.connect_sse( | ||
| request.scope, | ||
| request.receive, | ||
| request._send, # noqa: SLF001 | ||
| ) as (read_stream, write_stream): | ||
| await mcp_server.run( | ||
| read_stream, | ||
| write_stream, | ||
| mcp_server.create_initialization_options(), | ||
| ) | ||
|
|
||
| return Starlette( | ||
| debug=debug, | ||
| routes=[ | ||
| Route("/sse", endpoint=handle_sse), | ||
| Mount("/messages/", app=sse.handle_post_message), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| async def run_sse_server( | ||
| stdio_params: StdioServerParameters, | ||
| sse_settings: SseServerSettings, | ||
| ) -> None: | ||
| """Run the stdio client and expose an SSE server. | ||
|
|
||
| Args: | ||
| ---- | ||
| stdio_params: The parameters for the stdio client that spawns a stdio server. | ||
| sse_settings: The settings for the SSE server that accepts incoming requests. | ||
|
|
||
| """ | ||
| async with stdio_client(stdio_params) as streams, ClientSession(*streams) as session: | ||
| mcp_server = await create_proxy_server(session) | ||
|
|
||
| # Bind SSE request handling to MCP server | ||
| starlette_app = await create_starlette_app(mcp_server, sse_settings.log_level == "DEBUG") | ||
|
|
||
| # Configure HTTP server | ||
| config = uvicorn.Config( | ||
| starlette_app, | ||
| host=sse_settings.bind_host, | ||
| port=sse_settings.port, | ||
| log_level=sse_settings.log_level.lower(), | ||
| ) | ||
| http_server = uvicorn.Server(config) | ||
| await http_server.serve() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Tests for the sse server.""" | ||
|
|
||
| import asyncio | ||
| import contextlib | ||
|
|
||
| import uvicorn | ||
| from mcp import types | ||
| from mcp.client.session import ClientSession | ||
| from mcp.client.sse import sse_client | ||
| from mcp.server import Server | ||
|
|
||
| from mcp_proxy.sse_server import create_starlette_app | ||
|
|
||
|
|
||
| class BackgroundServer(uvicorn.Server): | ||
| """A test server that runs in a background thread.""" | ||
|
|
||
| def install_signal_handlers(self) -> None: | ||
| """Do not install signal handlers.""" | ||
|
|
||
| @contextlib.asynccontextmanager | ||
| async def run_in_background(self) -> None: | ||
| """Run the server in a background thread.""" | ||
| task = asyncio.create_task(self.serve()) | ||
| try: | ||
| while not self.started: # noqa: ASYNC110 | ||
| await asyncio.sleep(1e-3) | ||
| yield | ||
| finally: | ||
| task.cancel() | ||
| self.shutdown() | ||
|
|
||
| @property | ||
| def url(self) -> str: | ||
| """Return the url of the started server.""" | ||
| hostport = next( | ||
| iter([socket.getsockname() for server in self.servers for socket in server.sockets]), | ||
| ) | ||
| return f"http://{hostport[0]}:{hostport[1]}" | ||
|
|
||
|
|
||
| async def test_create_starlette_app() -> None: | ||
| """Test basic glue code for the SSE transport and a fake MCP server.""" | ||
| server = Server("prompt-server") | ||
|
|
||
| @server.list_prompts() | ||
| async def list_prompts() -> list[types.Prompt]: | ||
| return [types.Prompt(name="prompt1")] | ||
|
|
||
| app = create_starlette_app(server) | ||
|
|
||
| config = uvicorn.Config(app, port=0, log_level="info") | ||
| server = BackgroundServer(config) | ||
| async with server.run_in_background(): | ||
| mcp_url = f"{server.url}/sse" | ||
| async with sse_client(url=mcp_url) as streams, ClientSession(*streams) as session: | ||
| await session.initialize() | ||
| response = await session.list_prompts() | ||
| assert len(response.prompts) == 1 | ||
| assert response.prompts[0].name == "prompt1" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.