mjdi

Search documentation

Search the mjdi v0.10.0 documentation

Mojo 1.2.0v0.10.0compile-time first

Dependency injection for Mojo that fails at compile time, not in production.

mjdi validates your whole dependency graph while the compiler runs — missing providers, cycles, duplicates and scope mismatches become compile errors with actionable diagnostics. At runtime, a Container resolves cached singletons, fresh transients and scoped instances.

documentation pages:
example applications:
release:

Everything checked before main() runs

mjdi keeps registration and validation entirely at compile time, and does exactly one runtime job: constructing and caching your objects.

Type-declared dependencies
Services declare what they need with Inject[T] members. The dependency graph is written down in your types, not inferred from magic strings.
Compile-time validation
Missing providers, circular dependencies, duplicate registrations and scope violations are rejected by the compiler before your program ever runs.
Four provider kinds
Class providers via Injectable, value providers for constants, factory providers for functions, and alias providers for indirection.
Singleton, transient, scoped
Every binding has an explicit lifetime: cached singletons, fresh transients, and scoped instances with create_scope()/close_scope().
Precise diagnostics
Every compile error names the dependency, the consumer, the missing provider and the full dependency path that led there.
Ownership-aware
Built for Mojo's value semantics: immutable registry references, explicit release, and no hidden shared mutable state.
Versioned, copyable docs
Every page lives at a versioned URL (/docs/0.10.0/…) and is copyable as raw markdown — one click for humans, plain HTTP for agents. Archived releases stay frozen.

Wire an application in three lines

Declare dependencies with Inject[T] members, build an immutable Registry by chaining register[T]() calls, and hand it to a Container. The graph is validated when the container alias is defined — miswiring never reaches runtime.

Follow the full tutorial

Install Mojo 1.2.0, drop the mjdi package next to your source, and wire your first application:

from mjdi import Container, Inject, Injectable, Registry

struct MessageRenderer(Injectable):
    def render(self, name: String) -> String:
        return String("Hello, ") + name + String("!")

struct GreetingService(Injectable):
    var renderer: Inject[MessageRenderer]

    def greet(self, name: String) -> String:
        return self.renderer.value().render(name)

def main() raises:
    comptime app_registry = (
        Registry()
        .register[MessageRenderer]()
        .register[GreetingService]()
    )
    var app = Container[type_of(app_registry)]()
    var service = app.resolve[GreetingService]()
    print(service.greet("mojo"))

If a provider is missing — say you delete .register[MessageRenderer]() — the program no longer compiles: the diagnostic names the missing provider, the consumer GreetingService and how to fix it.

Model Context Protocol

An MCP endpoint for agents: /api/mcp

The website exposes the mjdi documentation and example sources over the Model Context Protocol (Streamable HTTP, JSON-RPC 2.0). Wire it into your editor or agent and the docs travel with the tools.

MCP playground
connecting...
Post JSON-RPC 2.0 messages to /api/mcp and read the raw response — exactly what an MCP client does.
Send a request to see the response.

Point any MCP-capable client at the endpoint:

{
  "mcpServers": {
    "mjdi-docs": {
      "type": "http",
      "url": "https://your-host/api/mcp"
    }
  }
}

The server is stateless and read-only. It exposes tools (list_doc_versions, list_docs, get_doc, search_docs, get_quickstart, get_api_reference, get_changelog, list_examples, get_example — each documentation tool accepts an optional version argument) and resources (mjdi://docs/<version>/<slug>), so coding agents can pull accurate mjdi documentation — including compile-fail and crash-test behavior — straight from the repository markdown. Prefer plain HTTP? Every page is raw markdown at /api/docs/0.10.0/<page>.md. See the MCP server docs and the website docs.