Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

1.8 KB
Raw Blame
import { isMcpController } from './metadata';
import { McpRegistry } from './registry';
import { MCP_MODULE_OPTIONS, MCP_TOOL_GATE, type McpModuleOptions, type McpToolGate } from './types';
import { Inject, Injectable, type OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner } from '@nestjs/core';

/**
 * DI-discovered registry: at boot, scans every provider marked with
 * `@McpController()` and records its decorated methods. Attach it to a
 * per-session `McpServer` with `attach(server, ctx)`.
 */
@Injectable()
export class McpRegistryService extends McpRegistry implements OnModuleInit {
  constructor(
    private readonly discovery: DiscoveryService,
    private readonly scanner: MetadataScanner,
    @Inject(MCP_MODULE_OPTIONS) options: McpModuleOptions,
    @Inject(MCP_TOOL_GATE) toolGate: McpToolGate | null,
  ) {
    super({
      accessPolicy: options.accessPolicy,
      validateAccess: options.validateAccess,
      toolGate: toolGate ?? undefined,
      errorMapper: options.errorMapper,
      inputMapper: options.inputMapper,
    });
  }

  onModuleInit(): void {
    // A provider can be wrapped once per module that lists it — dedupe by
    // instance so an entry is never recorded (and later registered) twice.
    const seen = new Set<object>();
    for (const wrapper of this.discovery.getProviders()) {
      const { instance, metatype } = wrapper;
      if (!instance || typeof instance !== 'object' || seen.has(instance)) continue;
      if (!isMcpController(metatype)) continue;
      seen.add(instance);
      this.register(instance, this.scanner.getAllMethodNames(Object.getPrototypeOf(instance)));
    }
    // Fail app boot on duplicate names / policy-less declarative access
    // instead of failing per-session at attach().
    this.validate();
  }
}