-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
It would be great to have an API to interact with the MCP's underlying Uvicorn server.
More specifically, It would be great to have a leaky interface, that can act as a bridge with the abstraction of the underlying Uvicorn server, a way to configure it, or maybe just allowing us to pass arguments/kwargs to the Uvicorn server.
Problem
As a start, there is no clean way to close a Uvicorn server programmatically/gracefully, there are some hacks to go around it, here are some related issues:
Uvicorn cannot be shutdown programmatically - Kludex/uvicorn#1103
Uvicorn cannot be shutdown programmatically - Kludex/uvicorn#742
Running multiple Uvicorn servers simultaneously hangs on shutdown - Kludex/uvicorn#1773
Uvicorn eats SIGINTs, does not propagate exceptions - Kludex/uvicorn#1579
And so on....
Idea
You might say that, we already have access to the server and low-level API with
from mcp.server.lowlevel.server import ServerHowever just to handle a graceful shutdown, not being able to handle all the out-of-box features that FastMCP class provides, feels like re-inventing the wheel.
So one of the easiest way(s) to do it is, passing the timeout_graceful_shutdown=0 to the uvicorn.Config which solves the (partially) hanging server problem.
Uvicorn is configurable via environment variables, when they are prefixed with UVICORN_, so in a similar way, when we pass values to:
FastMCP(
"some_server",
lifespan=app_lifespan,
debug=True, # no prefix, for the Settings class
uvicorn_timeout_graceful_shutdown=0, # uvicorn prefix, for UvicornConfig?
)We can buffer this, similar to **settings then we can pass to the initialization of the server, would it be feasible?
I'm sure there will be other use cases for this, also I'm not sure if the example above is the best way to handle that, just to give you a brief idea.