@frontmcp/di

中文文档
1.12.2发布于 2026/11/15Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.3万

Generic dependency injection container and registry utilities for TypeScript applications

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. Providers
  4. Scopes
  5. Hierarchy and registries
  6. Docs
  7. Related packages
  8. Contributing
  9. License

@frontmcp/di

The dependency injection container behind FrontMCP. This is an internal package: @frontmcp/sdk uses it to build providers and registries, and installs it for you. In a FrontMCP server, declare shared services with @Provider and read them with this.get() from @frontmcp/sdk instead. The container has no FrontMCP-specific code, so you can also use it on its own in any TypeScript project.

npm License

Install

npm install @frontmcp/di @frontmcp/lazy-zod zod reflect-metadata

@frontmcp/lazy-zod (which needs zod 4) and reflect-metadata are peer dependencies. Import reflect-metadata once, before anything else. It needs Node.js 24 or later.

Usage

import 'reflect-metadata';

import { createTokenFactory, DiContainer, ProviderScope } from '@frontmcp/di';

const tokens = createTokenFactory({ prefix: 'Desk' });
const DESK_URL = tokens.type('DeskUrl');

class TicketStore {
  static metadata = { name: 'TicketStore', scope: ProviderScope.GLOBAL };
  readonly tickets = new Map<string, string>();
}

class TicketClient {
  constructor(
    readonly store: TicketStore,
    readonly baseUrl: string,
  ) {}
}

const container = new DiContainer([
  TicketStore,
  { provide: DESK_URL, useValue: 'https://desk.example.com' },
  {
    provide: TicketClient,
    inject: () => [TicketStore, DESK_URL],
    useFactory: (store: TicketStore, baseUrl: string) => new TicketClient(store, baseUrl),
  },
]);

await container.ready;

const client = container.get(TicketClient);

Providers

FormHow it's built
MyClassWith new. An optional static metadata = { name, scope } names it and sets its scope. Constructor parameters are injected when TypeScript records their types, which it does for a decorated class with emitDecoratorMetadata on.
@AsyncWith(() => [DepA, DepB]) on a classBy calling its static with(depA, depB), which may be async.
{ provide, useValue }The value itself.
{ provide, useFactory, inject }By calling useFactory with the providers inject() returns, in order.
{ provide, useClass }Like a class, registered under provide.

provide is a class, a symbol or a string. createTokenFactory({ prefix }) makes symbols with Symbol.for(), so two copies of a package in one process share them: tokens.type('DeskUrl') is Symbol.for('Desk:type:DeskUrl').

Scopes

  • ProviderScope.GLOBAL (the default): one instance, built in dependency order when the container starts. await container.ready, then read it with container.get(token), or container.tryGet(token), which returns undefined instead of throwing.
  • ProviderScope.CONTEXT: one instance per context, like a request or a session. Build a context with const views = await container.buildViews(key), then read it with container.getScoped(token, views).

A GLOBAL provider can't depend on a CONTEXT one, and the container throws on dependency cycles.

Hierarchy and registries

new DiContainer(providers, parent) looks up tokens it doesn't have in parent. The package also exports the registry base classes FrontMCP's own registries extend: RegistryAbstract, SimpleRegistry, and IndexedRegistry, which indexes entries by ID, name and owner, adopts child registries, and emits change events to subscribe() callbacks.

Docs

Contributing

Source: libs/di. Issues and pull requests are welcome; see CONTRIBUTING.md.

License

Apache-2.0. See LICENSE.

版本与发布记录

@frontmcp/di 共发布 111 个版本,首次发布于 2026/01/04;过去 12 个月发布了 111 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.12.2latest2026/11/151 天前
1.12.12026/11/132 天前
1.12.02026/11/114 天前
1.12.0-rc.1rc2026/11/114 天前
1.11.32026/11/106 天前
1.11.22026/11/096 天前

其他发布通道:beta → 1.1.2-beta.1,rc → 1.12.0-rc.1。

关于 @frontmcp/di 的常见问题

@frontmcp/di 是什么?

Generic dependency injection container and registry utilities for TypeScript applications

如何安装 @frontmcp/di?

在项目目录运行 npm install @frontmcp/di,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:reflect-metadata(^0.2.0)、@frontmcp/lazy-zod(1.12.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@frontmcp/di 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frontmcp/di 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@frontmcp/di 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@frontmcp/di 可以商用吗?

@frontmcp/di 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@frontmcp/di 的下载量有多少?

根据 npm 官方下载统计,@frontmcp/di 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,263 次,日均约 442 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frontmcp/di 还在维护吗?

最新版本 1.12.2 发布于 2026/11/15(1 天前);至今共发布 111 个版本,其中过去 12 个月发布了 111 个;首次发布于 2026/01/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。