Overview
import { ProviderRegistry } from '@frontmcp/sdk';
// Access via scope
const providers = scope.providers;
// Get a singleton provider
const logger = providers.get(LoggerToken);
// Get a scoped provider
const service = providers.getScoped(ServiceToken, views);
Methods
get()
Get a DEFAULT-scoped (singleton) provider.
get<T>(token: Token<T>): T
Example:
const logger = registry.get(LoggerToken);
const config = registry.get(ConfigToken);
getScoped()
Get a provider from given views (for session/request scoped providers).
getScoped<T>(token: Token<T>, views: ProviderViews): T
Example:
const views = registry.buildViews(sessionKey, contextProviders);
const session = registry.getScoped(SessionToken, views);
resolve()
Lightweight synchronous resolver for app-scoped DI.
resolve<T>(cls: Class<T>): T
Example:
const service = registry.resolve(MyService);
resolveBootstrapDep()
Resolve a dependency usable during bootstrap phase.
resolveBootstrapDep<T>(token: Token<T>): T
getProviders()
Get all provider entries.
getProviders(): ReadonlyArray<ProviderEntry>
buildViews()
Build provider views for different scopes.
buildViews(
sessionKey: string,
contextProviders?: Map<Token, unknown>
): ProviderViews
Example:
const views = registry.buildViews('session-123', new Map([
[RequestToken, currentRequest],
]));
addRegistry()
Add a registry by type.
addRegistry<T>(type: RegistryType, value: T): void
getRegistries()
Get registries by type.
getRegistries<T>(type: RegistryType): T[]
getHooksRegistry()
Get the hooks registry.
getHooksRegistry(): HookRegistry
getScopeRegistry()
Get the scope registry.
getScopeRegistry(): ScopeRegistry
mergeFromRegistry()
Merge providers from another registry.
mergeFromRegistry(providedBy: Token, exported: ProviderExport[]): void
getProviderInfo()
Get exported provider definitions.
getProviderInfo(token: Token): ProviderDef | undefined
injectProvider()
Inject a provider value directly.
injectProvider<T>(injected: InjectedProvider<T>): void
Example:
registry.injectProvider({
token: ConfigToken,
value: { debug: true },
});
addDynamicProviders()
Add providers dynamically at runtime.
addDynamicProviders(dynamicProviders: DynamicProvider[]): void
Session Management
cleanupSession()
Clean up a specific session’s provider cache.
cleanupSession(sessionKey: string): void
cleanupExpiredSessions()
Clean up expired sessions from cache.
cleanupExpiredSessions(): void
startSessionCleanup()
Start background session cleanup timer.
startSessionCleanup(): void
stopSessionCleanup()
Stop background session cleanup timer.
stopSessionCleanup(): void
getSessionCacheStats()
Get session cache statistics.
getSessionCacheStats(): SessionCacheStats
isSessionCacheEnabled()
Check if session caching is enabled.
isSessionCacheEnabled(): boolean
dispose()
Dispose registry and clean up all resources.
Server/Scope Access
getActiveScope()
Get the active scope.
getActiveServer()
Get the active server.
getActiveServer(): Server
Provider Scopes
FrontMCP supports multiple provider scopes:
| Scope | Description | Lifecycle |
|---|
SINGLETON | Shared across all requests | Server lifetime |
SESSION | Per-session instance | Session lifetime |
REQUEST | Per-request instance | Request lifetime |
CONTEXT | Per-execution context | Context lifetime |
In distributed/serverless mode, SESSION is normalized to CONTEXT to avoid stale instances.
Provider Views
Provider views represent the hierarchy of available providers:
interface ProviderViews {
singleton: Map<Token, unknown>; // Server-wide singletons
session: Map<Token, unknown>; // Session-scoped instances
context: Map<Token, unknown>; // Context-scoped instances
}
Indexes
| Index | Key | Description |
|---|
providedBy | Token | Which registry provided each token |
order | number | Topological order for instantiation |
registries | RegistryType | Map of registries by kind |
Session Caching
The registry implements LRU eviction and TTL-based session caching:
// Session cache stats
const stats = registry.getSessionCacheStats();
console.log(`Active sessions: ${stats.activeCount}`);
console.log(`Cache hits: ${stats.hits}`);
console.log(`Cache misses: ${stats.misses}`);
Provider Definition
Providers are defined with:
interface ProviderDef<T> {
token: Token<T>; // Injection token
useClass?: Class<T>; // Class to instantiate
useFactory?: () => T | Promise<T>; // Factory function
useValue?: T; // Direct value
scope?: ProviderScope; // Lifecycle scope
deps?: Token[]; // Dependencies
}
Example:
@Provider({
token: CacheToken,
useFactory: () => new RedisCache(),
scope: ProviderScope.SINGLETON,
})
class CacheProvider { }