From the V2 checklist in #1253:
Create dependency injection in MCPServer.
MCPServer currently has no built-in mechanism for injecting dependencies (database connections, auth services, configuration objects, etc.) into tool, resource, and prompt handlers. The existing Context parameter injection is specific to the MCP request context and does not cover arbitrary user-defined dependencies.
A DI system — similar to FastAPI's Depends() — would allow:
- Declaring dependencies as handler parameters that the framework resolves automatically
- Sharing expensive resources (DB pools, HTTP clients) across handlers without global state
- Making handlers more testable by swapping dependencies in tests
- Composing dependencies (dependencies that depend on other dependencies)
Related but distinct issues
Open questions
- Should this follow FastAPI's
Depends() pattern exactly, or adapt it for MCP's handler signatures?
- How does this interact with the
Context parameter and lifespan state?
- Should dependencies be scoped per-request, per-session, or per-server?
Ref: Kludex's V2 checklist in #1253
From the V2 checklist in #1253:
MCPServer currently has no built-in mechanism for injecting dependencies (database connections, auth services, configuration objects, etc.) into tool, resource, and prompt handlers. The existing
Contextparameter injection is specific to the MCP request context and does not cover arbitrary user-defined dependencies.A DI system — similar to FastAPI's
Depends()— would allow:Related but distinct issues
Contexttype specifically)Open questions
Depends()pattern exactly, or adapt it for MCP's handler signatures?Contextparameter and lifespan state?Ref: Kludex's V2 checklist in #1253