Skip to content

container

kubed-io[bot] edited this page Nov 26, 2026 · 3 revisions

Container

The inversion-of-control container beside each QuickMCP: recipes keyed by type, three lifetimes, teardown, and Inject[T], which hands what it builds to a tool. This area covers kubed/mcp/container.py and kubed/mcp/inject.py, and QuickMCP's singleton, session, transient, override and end_session.

Use cases

  • A server builds a long-lived object once, as a singleton, and its tools take it as Inject[Actions] instead of a closure over it.
  • A server builds an object before it serves, so a refused credential stops the process at boot, and hands that object to the container as a singleton instance.
  • A server reads a value that changes under it, a catalogue that a refresh swaps, fresh on every call, as a transient over a singleton.
  • A server keeps per-workspace state in memory, as a session keyed by the workspace's name, ended when the workspace's browser ends or when it goes unused.
  • A test overrides a type with a fake and calls the tool through a real Client.
  • A test calls a tool function directly with explicit arguments, and no container is involved.

Neighbours

DI-1. QuickMCP itself, its lifespan and its call middleware are the app's (APP-5, APP-6). FastMCP's own dependency injection does the per-call work under Inject[T]: hiding a parameter from the schema, refusing a client that sends it, and caching a resolver for the call.

Recipes

DI-2. A key is a type. mcp.singleton(T, factory=None), mcp.session(T, factory=None, *, by, ttl) and mcp.transient(T, factory=None) register a recipe for T: a factory and a lifetime. With no factory, T itself is the factory. Registering a key again replaces its recipe, or its instance (DI-17).

DI-3. Building a recipe resolves first each factory parameter whose annotation is a registered key, or an Annotated whose first argument is one, and passes it by name. String annotations are evaluated. Any other parameter must have a default, or the build is refused with a TypeError naming the factory and the parameter. Constructors run here, never in server code.

DI-4. A factory may be sync or async; an async one is awaited. When what a factory returns is an async context manager, the container enters it, injects what it entered, and exits it when its lifetime ends (DI-10).

Lifetimes

DI-5. A singleton is built once per QuickMCP, on first use. Calls that ask for it at once build it once.

DI-6. A session is built once per value of its by key, which is itself a registered key, resolved for the call. It lives in process memory only.

DI-7. await mcp.end_session(value) ends every session built for value, under any key, in reverse creation order. A value's sessions also end when none of them has been resolved for ttl seconds, the shortest ttl among them; that is checked whenever a session is resolved. The next call that asks builds them again.

DI-8. A transient is built once per call and shared by every parameter and every factory in that call that asks for it, nested ones included. The per-call slot is a ContextVar opened by QuickMCP's middleware (APP-6), never FastMCP's private request state. Outside that slot a transient is built each time it is asked for.

DI-9. A longer lifetime never depends on a shorter one: a singleton never on a session or a transient, and a session never on a transient other than its own by key, which is fixed for the session's life. Such a recipe is refused when it is built, with a TypeError naming both keys and both lifetimes. A recipe that depends on itself, however indirectly, is refused naming the cycle.

DI-10. Teardown runs in reverse creation order: a transient's when its call ends, a session's when it ends (DI-7), and, when the server shuts down at the end of its lifespan, every open session's and then every singleton's. A transient built outside a call slot (DI-8) is torn down with the singletons. Singletons are built again on first use after a restart.

Instances, overrides and config

DI-11. mcp.override(T, value) makes T resolve to value from then on, whatever its recipe and whether or not it has one. An override is never built, entered or torn down, and the order rule (DI-9) does not apply to it.

DI-12. The loaded config's sections, and the config itself, resolve as their classes (config, CFG-16). Like an override, none is built, entered or torn down.

DI-17. mcp.singleton(T, *, instance=obj) registers obj, built already, as T's singleton. instance is keyword-only and the alternative to a factory: passing both is refused with a TypeError. It resolves like any singleton, injected with Inject[T] and passed to any factory that asks for T, and an override (DI-11) still replaces it. The container never builds it, enters it or tears it down, at shutdown or ever: whoever built it owns its lifetime. A restart (DI-10) keeps it, the same object.

Injecting

DI-13. Inject[T] is Annotated[T, Depends(lookup(T))]. A type checker reads it as T, through the package's py.typed marker. It works in tools, resource templates and prompts.

DI-14. An injected parameter is not in the tool's input schema, nor a prompt's arguments, nor a template's parameters. A client that sends its name is refused as an unexpected argument. A direct call of the function with explicit arguments uses them and touches no container.

DI-15. lookup(T) returns one async resolver per key, the same object every time, so FastMCP's per-call cache shares it between parameters. The resolver resolves through the QuickMCP serving the call: its call slot (DI-8), or else the request's Context. Outside any call it raises RuntimeError, and for a key with no recipe it raises LookupError naming the key. A middleware reaches the container the same way, with await lookup(T)().

DI-16. Nothing is injected by its type alone. A parameter is injected only when it is marked, with Inject[T] or FastMCP's Depends; an unmarked parameter of a registered type is an ordinary argument.

Clone this wiki locally