UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

119 lines (118 loc) 4.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateTypeDefinitionPlugin = exports.InjectRuntimePlugin = exports.runtimeFiles = void 0; const constants_1 = require("../../utils/constants"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const compiler_context_1 = require("../compiler-context"); // Fallback runtime files if RUNTIME_FILES is not defined const getDefaultRuntimeFiles = () => { const path = require("path"); const fs = require("fs"); // Try multiple possible locations for the mcp-i runtime dist directory const possiblePaths = [ // When running from compiled dist path.resolve(__dirname, "../../../dist/runtime"), path.resolve(__dirname, "../../dist/runtime"), path.resolve(__dirname, "../dist/runtime"), path.resolve(__dirname, "../runtime"), // When running from source path.resolve(__dirname, "../../../packages/mcp-i/dist/runtime"), path.resolve(__dirname, "../../packages/mcp-i/dist/runtime"), path.resolve(__dirname, "../packages/mcp-i/dist/runtime"), ]; let mcpDistPath = ""; for (const possiblePath of possiblePaths) { if (fs.existsSync(possiblePath) && fs.existsSync(path.join(possiblePath, "http.js"))) { mcpDistPath = possiblePath; break; } } const runtimeFiles = {}; if (mcpDistPath) { // Add known runtime files const knownFiles = [ "http.js", "stdio.js", "adapter-express.js", "adapter-nextjs.js", "headers.js", ]; for (const file of knownFiles) { const filePath = path.join(mcpDistPath, file); if (fs.existsSync(filePath)) { runtimeFiles[file] = fs.readFileSync(filePath, "utf8"); } } } return runtimeFiles; }; // IMPORTANT: Always use getDefaultRuntimeFiles() to ensure we get the latest runtime files // DefinePlugin embedding was causing stale file issues exports.runtimeFiles = getDefaultRuntimeFiles(); class InjectRuntimePlugin { apply(compiler) { let hasRun = false; compiler.hooks.beforeCompile.tap("InjectRuntimePlugin", (_compilationParams) => { if (hasRun) return; hasRun = true; // Only copy headers.js - transport files are now compiled from source // to avoid double-wrapping issues with webpack for (const [fileName, fileContent] of Object.entries(exports.runtimeFiles)) { if (fileName === "headers.js") { const targetPath = path_1.default.join(constants_1.runtimeFolderPath, fileName); fs_extra_1.default.writeFileSync(targetPath, fileContent); } } }); } } exports.InjectRuntimePlugin = InjectRuntimePlugin; const nextJsTypeDefinition = ` export const xmcpHandler: (req: Request) => Promise<Response>; export const withAuth: (handler: (req: Request) => Promise<Response>, authConfig: AuthConfig) => (req: Request) => Promise<Response>; export type VerifyToken = (req: Request, bearerToken?: string) => Promise<AuthInfo | undefined>; export type Options = { required?: boolean; requiredScopes?: string[]; resourceMetadataPath?: string; }; export type AuthConfig = { verifyToken: VerifyToken; options?: Options; }; export type AuthInfo = { token: string; clientId: string; scopes: string[]; expiresAt?: number; resource?: URL; extra?: Record<string, unknown>; }; `; const expressTypeDefinition = ` export const xmcpHandler: (req: Request, res: Response) => Promise<void>; `; class CreateTypeDefinitionPlugin { apply(compiler) { let hasRun = false; compiler.hooks.afterEmit.tap("CreateTypeDefinitionPlugin", (_compilationParams) => { if (hasRun) return; hasRun = true; const xmcpConfig = (0, compiler_context_1.getXmcpConfig)(); // For Express adapters, create type definitions // Next.js doesn't need .d.ts files as it causes webpack conflicts if (xmcpConfig.experimental?.adapter === "express") { const typeDefinitionContent = expressTypeDefinition; fs_extra_1.default.writeFileSync(path_1.default.join(constants_1.adapterOutputPath, "index.d.ts"), typeDefinitionContent); } }); } } exports.CreateTypeDefinitionPlugin = CreateTypeDefinitionPlugin;