You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow customization of the Starlette app (middleware, routes, etc) #194
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
asyncdef_create_sse_app(self):
"""Run the server using SSE transport."""fromstarlette.applicationsimportStarlettefromstarlette.routingimportMount, Routesse=SseServerTransport("/messages/")
asyncdefhandle_sse(request):
asyncwithsse.connect_sse(
request.scope, request.receive, request._send
) asstreams:
awaitself._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),
],
)
returnstarlette_appasyncdefrun_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 upapp=mcp_server._create_sse_app()
app.add_route(...)
app.add_middleware(...)
# run with this or the uvicorn cliif__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
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.
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.
Reacted by Tan Wang, earonesty and Eka Cahya Pratama
@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
@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
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.
Activity
mconflitti-pbc commentedon Feb 10, 2025
This should also address a bug related to urllib url joining that truncates server urls paths when combininb
/messages/set in the transport object.For example, if joining
http://localhost:8000/some/path/to/ssewith/messages/, it will output:http://localhost:8000/messagesRemoving the leading slash fixes this.
panz2018 commentedon Mar 10, 2025
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.
andrewburtnett commentedon Mar 19, 2025
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.
epinault commentedon Mar 26, 2025
@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
panz2018 commentedon Mar 27, 2025
Sorry, I have never faced this issue before.
mconflitti-pbc commentedon Mar 27, 2025
@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.
SinOverCos commentedon Apr 5, 2025
Is there a recommended solution for a health check endpoint that works today?
felipeavilis commentedon Apr 22, 2025
The @panz2018 solution worked for my project.
#194 (comment)
maxisbey commentedon Jul 29, 2026
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
Ecocitizenz commentedon Jul 29, 2026