| 1 | import { ServerCapabilities } from '@modelcontextprotocol/sdk/types.js' |
| 2 | import { McpOptions } from '../interfaces' |
| 3 | import { McpRegistryService } from '../services/mcp-registry.service' |
| 4 | |
| 5 | /** |
| 6 | * Utility function to build MCP server capabilities dynamically based on |
| 7 | * discovered tools, resources, and prompts |
| 8 | */ |
| 9 | export function buildMcpCapabilities( |
| 10 | mcpModuleId: string, |
| 11 | registry: McpRegistryService, |
| 12 | options: McpOptions, |
| 13 | ): ServerCapabilities { |
| 14 | // Start with user-provided capabilities or empty object |
| 15 | const baseCapabilities = options.capabilities || {} |
| 16 | |
| 17 | const capabilities: ServerCapabilities = { ...baseCapabilities } |
| 18 | |
| 19 | // Add tools capability if tools are discovered |
| 20 | if (registry.getTools(mcpModuleId).length > 0) { |
| 21 | capabilities.tools = capabilities.tools || { |
| 22 | listChanged: true, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | if ( |
| 27 | registry.getResources(mcpModuleId).length > 0 |
| 28 | || registry.getResourceTemplates(mcpModuleId).length > 0 |
| 29 | ) { |
| 30 | capabilities.resources = capabilities.resources || { |
| 31 | listChanged: true, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Add prompts capability if prompts are discovered |
| 36 | if (registry.getPrompts(mcpModuleId).length > 0) { |
| 37 | capabilities.prompts = capabilities.prompts || { |
| 38 | listChanged: true, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return capabilities |
| 43 | } |
| 44 |