| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6b6f34e commit a9aca20
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,7 @@ | |||
| 31 | 31 | - [Development Mode](#development-mode) | |
| 32 | 32 | - [Claude Desktop Integration](#claude-desktop-integration) | |
| 33 | 33 | - [Direct Execution](#direct-execution) | |
| 34 | + - [Mounting to an Existing ASGI Server](#mounting-to-an-existing-asgi-server) | ||
| 34 | 35 | - [Examples](#examples) | |
| 35 | 36 | - [Echo Server](#echo-server) | |
| 36 | 37 | - [SQLite Explorer](#sqlite-explorer) | |
@@ -346,6 +347,31 @@ python server.py | |||
| 346 | 347 | mcp run server.py | |
| 347 | 348 | ``` | |
| 348 | 349 | ||
| 350 | + ### Mounting to an Existing ASGI Server | ||
| 351 | + | ||
| 352 | + You can mount the SSE server to an existing ASGI server using the `sse_app` method. This allows you to integrate the SSE server with other ASGI applications. | ||
| 353 | + | ||
| 354 | + ```python | ||
| 355 | + from starlette.applications import Starlette | ||
| 356 | + from starlette.routes import Mount, Host | ||
| 357 | + from mcp.server.fastmcp import FastMCP | ||
| 358 | + | ||
| 359 | + | ||
| 360 | + mcp = FastMCP("My App") | ||
| 361 | + | ||
| 362 | + # Mount the SSE server to the existing ASGI server | ||
| 363 | + app = Starlette( | ||
| 364 | + routes=[ | ||
| 365 | + Mount('/', app=mcp.sse_app()), | ||
| 366 | + ] | ||
| 367 | + ) | ||
| 368 | + | ||
| 369 | + # or dynamically mount as host | ||
| 370 | + app.router.routes.append(Host('mcp.acme.corp', app=mcp.sse_app())) | ||
| 371 | + ``` | ||
| 372 | + | ||
| 373 | + For more information on mounting applications in Starlette, see the [Starlette documentation](https://www.starlette.io/routing/#submounting-routes). | ||
| 374 | + | ||
| 349 | 375 | ## Examples | |
| 350 | 376 | ||
| 351 | 377 | ### Echo Server | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,8 @@ | |||
| 19 | 19 | from pydantic import BaseModel, Field | |
| 20 | 20 | from pydantic.networks import AnyUrl | |
| 21 | 21 | from pydantic_settings import BaseSettings, SettingsConfigDict | |
| 22 | + from starlette.applications import Starlette | ||
| 23 | + from starlette.routing import Mount, Route | ||
| 22 | 24 | ||
| 23 | 25 | from mcp.server.fastmcp.exceptions import ResourceError | |
| 24 | 26 | from mcp.server.fastmcp.prompts import Prompt, PromptManager | |
@@ -461,9 +463,19 @@ async def run_stdio_async(self) -> None: | |||
| 461 | 463 | ||
| 462 | 464 | async def run_sse_async(self) -> None: | |
| 463 | 465 | """Run the server using SSE transport.""" | |
| 464 | - from starlette.applications import Starlette | ||
| 465 | - from starlette.routing import Mount, Route | ||
| 466 | + starlette_app = self.sse_app() | ||
| 466 | 467 | ||
| 468 | + config = uvicorn.Config( | ||
| 469 | + starlette_app, | ||
| 470 | + host=self.settings.host, | ||
| 471 | + port=self.settings.port, | ||
| 472 | + log_level=self.settings.log_level.lower(), | ||
| 473 | + ) | ||
| 474 | + server = uvicorn.Server(config) | ||
| 475 | + await server.serve() | ||
| 476 | + | ||
| 477 | + def sse_app(self) -> Starlette: | ||
| 478 | + """Return an instance of the SSE server app.""" | ||
| 467 | 479 | sse = SseServerTransport("/messages/") | |
| 468 | 480 | ||
| 469 | 481 | async def handle_sse(request): | |
@@ -476,23 +488,14 @@ async def handle_sse(request): | |||
| 476 | 488 | self._mcp_server.create_initialization_options(), | |
| 477 | 489 | ) | |
| 478 | 490 | ||
| 479 | - starlette_app = Starlette( | ||
| 491 | + return Starlette( | ||
| 480 | 492 | debug=self.settings.debug, | |
| 481 | 493 | routes=[ | |
| 482 | 494 | Route("/sse", endpoint=handle_sse), | |
| 483 | 495 | Mount("/messages/", app=sse.handle_post_message), | |
| 484 | 496 | ], | |
| 485 | 497 | ) | |
| 486 | 498 | ||
| 487 | - config = uvicorn.Config( | ||
| 488 | - starlette_app, | ||
| 489 | - host=self.settings.host, | ||
| 490 | - port=self.settings.port, | ||
| 491 | - log_level=self.settings.log_level.lower(), | ||
| 492 | - ) | ||
| 493 | - server = uvicorn.Server(config) | ||
| 494 | - await server.serve() | ||
| 495 | - | ||
| 496 | 499 | async def list_prompts(self) -> list[MCPPrompt]: | |
| 497 | 500 | """List all available prompts.""" | |
| 498 | 501 | prompts = self._prompt_manager.list_prompts() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments