Skip to content

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

Closed

Description

@mconflitti-pbc

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

Activity

  1. mconflitti-pbc commented on Feb 10, 2025

    @mconflitti-pbc
    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.

  2. panz2018 commented on Mar 10, 2025

    @panz2018

    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.

  3. andrewburtnett commented on Mar 19, 2025

    @andrewburtnett

    I would also like the ability to add middleware and routes. We have specific requirements for healthchecks and request headers that would need to be handled by the MCP Server.

  4. epinault commented on Mar 26, 2025

    @epinault

    @panz2018 do you know if it is possible to extend the timeout for a request? I am getting timeout on slower request and I am unable to figure out an doc in starlette or uvicorn to extend it

  5. panz2018 commented on Mar 27, 2025

    @panz2018

    @panz2018 do you know if it is possible to extend the timeout for a request? I am getting timeout on slower request and I am unable to figure out an doc in starlette or uvicorn to extend it

    Sorry, I have never faced this issue before.

  6. mconflitti-pbc commented on Mar 27, 2025

    @mconflitti-pbc
    Author

    @epinault check out the parameters for the sse_client transport here: https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/sse.py#L25

    The default timeout is set to 5, but can be changed by passing in what you need.

  7. SinOverCos commented on Apr 5, 2025

    @SinOverCos

    Is there a recommended solution for a health check endpoint that works today?

  8. felipeavilis commented on Apr 22, 2025

    @felipeavilis

    Is there a recommended solution for a health check endpoint that works today?

    The @panz2018 solution worked for my project.

    • Unicorn for multithread;
    • FastAPI for HTTP Requets (health check, etc) ;
    • FastMCP for SSE and MPC requets;

    #194 (comment)

  9. added
    enhancementRequest for a new feature that's not currently supported
    ready for workEnough information for someone to start working on
    P1Significant bug affecting many users, highly requested feature
    on Oct 7, 2025
  10. added
    needs decisionIssue is actionable, needs maintainer decision on whether to implement
    and removed
    ready for workEnough information for someone to start working on
    on Apr 17, 2026
  11. maxisbey commented on Jul 29, 2026

    @maxisbey
    Contributor

    Thanks for the report. This has been available for a while now, including in the current release (see #312). Closing as part of a general backlog cleanup following the v2 release. If it still reproduces on v2, feel free to reopen.

    AI Disclaimer

  12. Ecocitizenz commented on Jul 29, 2026

    @Ecocitizenz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Significant bug affecting many users, highly requested featureenhancementRequest for a new feature that's not currently supportedneeds decisionIssue is actionable, needs maintainer decision on whether to implement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions