Skip to content
kubed-io[bot] edited this page Nov 27, 2026 · 21 revisions

App

QuickMCP: the object a server's factory builds, what it passes through to FastMCP, and how it runs. This area covers kubed/mcp/app.py: QuickMCP, mcp.auth and run().

Use cases

  • A server's create_app(settings) builds a QuickMCP, registers its recipes and its tools, and returns it; its main() calls run().
  • A tool author writes @mcp.tool, @mcp.resource and @mcp.prompt exactly as on FastMCP.
  • A server serves HTTP routes beside MCP, a health probe or its OpenAPI document, with @mcp.custom_route, and reshapes what it lists with mcp.add_transform.
  • A server calls run() with nothing: it serves as the config's serving section says, set by TRANSPORT, --port or the file, or by a hook from the server's own config (CMP-6).
  • A server's auth is built from its config after construction and set as mcp.auth, by the server or by one of its components.
  • A server serves its plugin folder, skills, commands and agents, with mcp.add_plugin(PLUGIN_DIR).
  • A server reaches FastMCP for what QuickMCP does not cover through mcp.server.

Neighbours

APP-1. Recipes, lifetimes and Inject[T] are the container's. What a plugin serves and how is the plugins area's. The names re-exported from FastMCP are the facade's, and the FastMCP pin is the package's (PKG-9). The config and its loading are config's (CFG), and the components and the built-in sections components' (CMP). A server's own sections and its logging configuration belong to that server, but for the root logger's level (APP-9).

The object

APP-2. QuickMCP(name, *, loaded=None, sections=None, components=None, hooks=None, config=None, argv=None, environ=None, header_prefix=None, **fastmcp_options) builds one FastMCP server, named name, with fastmcp_options passed through unchanged. header_prefix names the server's own headers (DLV-16). mcp.server is that instance, public, for what QuickMCP does not cover. QuickMCP wraps FastMCP and never subclasses it.

mcp.auth is the server's auth provider, mcp.server.auth, and None serves without auth. It may be set after construction, by the server or a component's register, until serving starts. Serving starts when run() is called, the HTTP app is built (mcp.server.http_app(), which FastMCP's own HTTP run calls too) or the lifespan is entered, and stays started: FastMCP reads auth only when it builds the HTTP app. Setting it after that is a RuntimeError, auth is fixed once serving starts: set mcp.auth before run() or http_app(). An auth option still sets it at construction.

APP-3. sections registers the server's config sections (CFG-2, CFG-3), and config, when given, is the config's values in code (CFG-17). components are the server's components (CMP-2), and hooks supply built-ins' sections (CMP-6). mcp.config is the loaded config, read-only, from construction on (CFG-11). argv is the server's flags and environ its env vars (CFG-8): with argv=None no flag is read, and with environ=None env vars come from os.environ. Passing either with config is a TypeError. mcp.config_sources is the record of where each item came from (CFG-12). loaded is a config load_config() loaded, and the server is built from it (CFG-18): passing it with sections, components, hooks, config, argv or environ is a TypeError.

APP-4. mcp.prompt, mcp.add_middleware, mcp.add_provider, mcp.add_extension, mcp.add_transform and mcp.custom_route are FastMCP's own methods of mcp.server, so each takes what FastMCP takes and a decorator returns the function it was given. mcp.resource has FastMCP's signature and passes what it is given through to mcp.server.resource, except that it refuses prompts:// and agents:// once the server has a plugin (PLUG-72). mcp.tool has FastMCP's signature and passes what it is given through to mcp.server.tool, except that it refuses list_resources and read_resource once the server has a plugin (DLV-7).

APP-5. The lifespan QuickMCP gives FastMCP does this on every entry:

  1. marks serving as started (APP-2);
  2. logs the config's boot line (CFG-11);
  3. starts each component in order, the built-ins first (CMP-3);
  4. warms FastMCP's session-state store, so a session's first request doesn't load it;
  5. runs a lifespan option as the server's own lifespan;
  6. stops the started components in reverse;
  7. runs the container's shutdown (DI-10) last.

The container's shutdown runs also when a start or the lifespan fails. A start that fails stops the components already started, the server's own lifespan doesn't run, and the boot fails with its error.

The warm-up touches FastMCP's private _state_store, which has no public way to warm it. If FastMCP has none, or the warm-up fails, one warning is logged on kubed.mcp.config, naming only the error's type when there is one, and serving goes on.

APP-6. QuickMCP puts one middleware of its own on the server, ahead of any passed as an option or added later. Only the call metrics (TEL-16) come before it, when otel is on, so they record what the client received. It opens the per-call slot transients are kept in (DI-8), so every middleware and handler inside it sees the same call.

APP-13. mcp.websocket_route(path, endpoint, *, name=None) serves a Starlette WebSocket endpoint at path on the HTTP app, beside the custom routes. path is used as given, as custom_route's is, so a prefix is the caller's to put in it. It is the one place QuickMCP touches FastMCP's private route list, which has no public way to add a WebSocket route. Nothing is served on stdio.

APP-14. await mcp.get_tool(name) is FastMCP's own, passed through: the tool as registered, with its full schema and none of what a caller is shown, or None. It is never built from list_tools, which is shaped per caller. An injected parameter is not in its schema (DI-14).

APP-12. mcp.add_plugin(address, *, namespace=None) serves a plugin and returns it (PLUG-2, PLUG-3), mcp.add_loader(loader) registers a loader for its schemes (PLUG-9), and mcp.plugins is the tuple of plugins served, in the order added (PLUG-4). They are QuickMCP's own, not FastMCP's. mcp.binary_pointer(fn) says where a binary's link is listed (DLV-6). mcp.delivery_default(fn) sets a client's defaults from its clientInfo (DLV-14). mcp.instructions(fn) writes the instructions per request (DLV-11).

Running

APP-7. run() reads the serving section (CMP-4): transport, host, port, log_level and graceful_timeout, as the config loaded them at construction.

APP-8. mcp.run(**http_options) takes no serving settings of its own. A transport, host, port, log_level or show_banner in http_options is a TypeError before anything runs, and serving hasn't started. It names the key and where it is set: run() takes no 'port': set serving.port in the config (PORT, --port, a serving: file key) or through hooks={'serving': ...}, or for show_banner, run() takes no 'show_banner': no banner is ever printed. A uvicorn_config holding timeout_graceful_shutdown is the same TypeError: run() takes no uvicorn_config['timeout_graceful_shutdown']: set serving.graceful_timeout in the config (GRACEFUL_TIMEOUT, --graceful-timeout, a serving: file key) or through hooks={'serving': ...}. Serving starts when it is called, so mcp.auth is fixed from then on (APP-2).

APP-9. Before serving, run() sets the root logger's level from log_level. It calls logging.basicConfig() first when the root logger has no handler, and adds none when it has one. Nothing is set on logging at construction. run() then removes every handler on the fastmcp logger and makes it propagate, so FastMCP's records reach whatever handlers the server configured.

APP-10. On stdio, run() serves with transport="stdio" and show_banner=False, and nothing else. On any other transport it serves with that transport, host, port and show_banner=False, plus http_options. Its uvicorn_config is the caller's, with timeout_graceful_shutdown set to graceful_timeout. Its middleware is the listen-ender (APP-15) first, then the caller's. The rest of http_options, such as path, is unchanged. No banner is ever printed, so FastMCP never asks PyPI for a newer release at start.

APP-11. run() never passes a log level to FastMCP: given one, FastMCP's run puts its own handlers back on the fastmcp logger, undoing APP-9. It also stops that logger propagating, for the life of the process.

APP-15. On HTTP, open subscriptions/listen streams end cleanly when the server begins to stop, on a stop signal: each gets a complete response with an empty final body, and nothing is logged above DEBUG for it. Every other request keeps graceful_timeout seconds to finish. A request still running past graceful_timeout is cancelled, and uvicorn logs it. A stop by signal ends listen streams at once; a programmatic stop waits up to graceful_timeout for them. The listen-ender hooks uvicorn's signal handler, so a stop made by setting should_exit directly leaves open listen streams to uvicorn, which waits out the full graceful_timeout while one is open. A server that stops itself in code can set graceful_timeout lower.

Clone this wiki locally