import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { logger } from '../utils/internal/logger.js';
import { requestContextService } from '../utils/internal/requestContext.js';
/**
 * Creates and configures a new instance of the `McpServer`.
 * Registries are provided directly — no DI container resolution.
 *
 * @returns A promise resolving with the configured `McpServer` instance.
 * @throws {McpError} If any resource or tool registration fails.
 */
export async function createMcpServerInstance(deps) {
    const context = requestContextService.createRequestContext({
        operation: 'createMcpServerInstance',
    });
    logger.debug('Initializing MCP server instance', context);
    const server = new McpServer({
        name: deps.config.mcpServerName,
        version: deps.config.mcpServerVersion,
        ...(deps.title && { title: deps.title }),
        ...(deps.websiteUrl && { websiteUrl: deps.websiteUrl }),
        ...(deps.description && { description: deps.description }),
        ...(deps.icons && { icons: deps.icons }),
    }, {
        capabilities: {
            logging: {},
            resources: { listChanged: true },
            tools: { listChanged: true },
            prompts: { listChanged: true },
            // SEP-1686 (spec revision 2025-11-25, GA) — only advertised when the
            // server registers at least one task tool. Clients pinned to 2025-06-18
            // that strict-parse capabilities fail on the unknown key.
            ...(deps.advertiseTasks && {
                tasks: {
                    list: {},
                    cancel: {},
                    requests: {
                        tools: { call: {} },
                    },
                },
            }),
            ...(deps.extensions && { extensions: deps.extensions }),
        },
        ...(deps.instructions && { instructions: deps.instructions }),
        ...(deps.taskStore && { taskStore: deps.taskStore }),
        ...(deps.taskMessageQueue && { taskMessageQueue: deps.taskMessageQueue }),
    });
    try {
        logger.debug('Registering all MCP capabilities via registries...', context);
        await Promise.all([
            deps.toolRegistry.registerAll(server),
            deps.resourceRegistry.registerAll(server),
            deps.promptRegistry.registerAll(server),
        ]);
        logger.debug('All MCP capabilities registered successfully', context);
    }
    catch (err) {
        logger.error('Failed to register MCP capabilities', err instanceof Error ? err : new Error(String(err)), context);
        throw err;
    }
    return server;
}
//# sourceMappingURL=server.js.map