UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

97 lines (96 loc) 4.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SERVER_CAPABILITIES = exports.SERVER_INFO = exports.injectedTools = void 0; exports.configureServer = configureServer; exports.loadTools = loadTools; exports.createServer = createServer; const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js"); const tools_1 = require("./tools"); // @ts-expect-error: injected by compiler exports.injectedTools = INJECTED_TOOLS; // Parse identity config from injected variable // @ts-expect-error: injected by compiler const rawIdentityConfig = typeof IDENTITY_CONFIG !== "undefined" ? IDENTITY_CONFIG : undefined; const identityConfig = rawIdentityConfig ? JSON.parse(rawIdentityConfig) : undefined; // Server info (first parameter to Server constructor) exports.SERVER_INFO = { name: "MCP Server", version: "0.0.1", }; // Server capabilities (second parameter to Server constructor) exports.SERVER_CAPABILITIES = { capabilities: { tools: {}, }, }; if (typeof global !== 'undefined') { global.__MCPI_SERVER_CREATED__ = global.__MCPI_SERVER_CREATED__ || false; } /** Loads tools and injects them into the server */ async function configureServer(server, toolModules) { await (0, tools_1.addToolsToServer)(server, toolModules, identityConfig); // TODO: implement addResourcesToServer, addPromptsToServer return server; } function loadTools() { const toolModules = new Map(); // Debug: log what tools are being injected const toolPaths = Object.keys(exports.injectedTools); if (identityConfig?.debug) { console.error("[loadTools] Injected tool paths:", toolPaths); } const toolPromises = toolPaths.map((path) => exports.injectedTools[path]().then((toolModule) => { if (identityConfig?.debug) { console.error("[loadTools] Loaded tool from path:", path); } toolModules.set(path, toolModule); })); return [toolPromises, toolModules]; } async function createServer() { if (identityConfig?.debug) { console.error("[createServer] Starting server creation"); } // Guard against double initialization using global scope // This ensures singleton even if webpack creates multiple module instances if (typeof global !== 'undefined' && global.__MCPI_SERVER_CREATED__) { if (identityConfig?.debug) { console.error("[createServer] Server already exists, returning cached instance"); } if (global.__MCPI_SERVER_INSTANCE__) { return global.__MCPI_SERVER_INSTANCE__; } } if (identityConfig?.debug) { console.error("[createServer] Creating new McpServer"); console.error("[createServer] SERVER_INFO constant:", JSON.stringify(exports.SERVER_INFO)); console.error("[createServer] SERVER_CAPABILITIES constant:", JSON.stringify(exports.SERVER_CAPABILITIES)); console.error("[createServer] Calling: new McpServer(SERVER_INFO, SERVER_CAPABILITIES)"); } const server = new index_js_1.Server(exports.SERVER_INFO, exports.SERVER_CAPABILITIES); if (identityConfig?.debug) { console.error("[createServer] Server created successfully"); // Debug: Verify what the server actually received const serverAny = server; console.error("[createServer] Actual server._serverInfo:", JSON.stringify(serverAny._serverInfo || "undefined")); console.error("[createServer] Actual server._capabilities:", JSON.stringify(serverAny._capabilities || "undefined")); console.error("[createServer] Loading tools..."); } const [toolPromises, toolModules] = loadTools(); await Promise.all(toolPromises); if (identityConfig?.debug) { console.error("[createServer] Tools loaded, configuring server"); } const configuredServer = await configureServer(server, toolModules); if (identityConfig?.debug) { console.error("[createServer] Server configured successfully"); } // Store in global scope if (typeof global !== 'undefined') { global.__MCPI_SERVER_CREATED__ = true; global.__MCPI_SERVER_INSTANCE__ = configuredServer; } return configuredServer; }