| 1 | import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' |
| 2 | import { Inject, Injectable, Logger, Scope } from '@nestjs/common' |
| 3 | import { ModuleRef } from '@nestjs/core' |
| 4 | import { HttpRequest } from '../interfaces/http-adapter.interface' |
| 5 | import { MCP_TOOL_MONITOR, McpToolMonitor } from '../interfaces/mcp-tool-monitor.interface' |
| 6 | import { McpPromptsHandler } from './handlers/mcp-prompts.handler' |
| 7 | import { McpResourcesHandler } from './handlers/mcp-resources.handler' |
| 8 | import { McpToolsHandler } from './handlers/mcp-tools.handler' |
| 9 | import { McpRegistryService } from './mcp-registry.service' |
| 10 | |
| 11 | /** |
| 12 | * Request-scoped service for executing MCP tools |
| 13 | */ |
| 14 | @Injectable({ scope: Scope.REQUEST }) |
| 15 | export class McpExecutorService { |
| 16 | private logger = new Logger(McpExecutorService.name) |
| 17 | private toolsHandler: McpToolsHandler |
| 18 | private resourcesHandler: McpResourcesHandler |
| 19 | private promptsHandler: McpPromptsHandler |
| 20 | |
| 21 | constructor( |
| 22 | moduleRef: ModuleRef, |
| 23 | registry: McpRegistryService, |
| 24 | @Inject('MCP_MODULE_ID') mcpModuleId: string, |
| 25 | @Inject(MCP_TOOL_MONITOR) toolMonitor: McpToolMonitor, |
| 26 | ) { |
| 27 | this.toolsHandler = new McpToolsHandler(moduleRef, registry, mcpModuleId, toolMonitor) |
| 28 | this.resourcesHandler = new McpResourcesHandler( |
| 29 | moduleRef, |
| 30 | registry, |
| 31 | mcpModuleId, |
| 32 | ) |
| 33 | this.promptsHandler = new McpPromptsHandler( |
| 34 | moduleRef, |
| 35 | registry, |
| 36 | mcpModuleId, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Register tool-related request handlers with the MCP server |
| 42 | * @param mcpServer - The MCP server instance |
| 43 | * @param httpRequest - The current HTTP request object |
| 44 | */ |
| 45 | registerRequestHandlers(mcpServer: McpServer, httpRequest: HttpRequest) { |
| 46 | this.toolsHandler.registerHandlers(mcpServer, httpRequest) |
| 47 | this.resourcesHandler.registerHandlers(mcpServer, httpRequest) |
| 48 | this.promptsHandler.registerHandlers(mcpServer, httpRequest) |
| 49 | } |
| 50 | } |
| 51 |