Skip to content
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

Allow customization of the Starlette app (middleware, routes, etc) #194

Open
mconflitti-pbc opened this issue Feb 7, 2025 · 2 comments
Open

Comments

@mconflitti-pbc
Copy link

mconflitti-pbc commented Feb 7, 2025

Is your feature request related to a problem? Please describe.
Found that it has been useful in my fork of this repo to split out the creation of the starlette app into its own function so I can create it and customize it in my code before running it.

Describe the solution you'd like

    async def _create_sse_app(self):
        """Run the server using SSE transport."""
        from starlette.applications import Starlette
        from starlette.routing import Mount, Route

        sse = SseServerTransport("/messages/")

        async def handle_sse(request):
            async with sse.connect_sse(
                request.scope, request.receive, request._send
            ) as streams:
                await self._mcp_server.run(
                    streams[0],
                    streams[1],
                    self._mcp_server.create_initialization_options(),
                )

        starlette_app = Starlette(
            debug=self.settings.debug,
            routes=[
                Route("/sse", endpoint=handle_sse),
                Mount("/messages/", app=sse.handle_post_message),
            ],
        )
        
        return starlette_app

    async def run_sse_async(self) -> None:
        """Run the server using SSE transport."""
        starlette_app = self._create_sse_app()
        # ... rest is same

and then use this like:

mcp_server = FastMCP("example", transport="sse")

#...server set up

app = mcp_server._create_sse_app()
app.add_route(...)
app.add_middleware(...)

# run with this or the uvicorn cli
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

Describe alternatives you've considered
none since it is currently contained in the run_sse_async function

@mconflitti-pbc
Copy link
Author

This should also address a bug related to urllib url joining that truncates server urls paths when combininb /messages/ set in the transport object.

sse = SseServerTransport("/messages/")

For example, if joining http://localhost:8000/some/path/to/sse with /messages/, it will output: http://localhost:8000/messages

Removing the leading slash fixes this.

@panz2018
Copy link

I have developed web servers that integrate MCP SSE functionality:

These servers can be extended with custom routes while retaining full MCP SSE capabilities. Thus, it is possible to add any customization (middleware, routes, etc) in the fully developed FastAPI or Starlette servers.

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

No branches or pull requests

2 participants