This commit is contained in:
Fredrik Jensen 2025-12-06 17:21:01 +01:00
parent 1055bb98ee
commit 4f275c8f0d
4 changed files with 55 additions and 59 deletions

39
package-lock.json generated
View File

@ -686,6 +686,44 @@
"@types/node": "*"
}
},
"node_modules/@vercel/analytics": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.6.1.tgz",
"integrity": "sha512-oH9He/bEM+6oKlv3chWuOOcp8Y6fo6/PSro8hEkgCW3pu9/OiCXiUpRUogDh3Fs3LH2sosDrx8CxeOLBEE+afg==",
"license": "MPL-2.0",
"peerDependencies": {
"@remix-run/react": "^2",
"@sveltejs/kit": "^1 || ^2",
"next": ">= 13",
"react": "^18 || ^19 || ^19.0.0-rc",
"svelte": ">= 4",
"vue": "^3",
"vue-router": "^4"
},
"peerDependenciesMeta": {
"@remix-run/react": {
"optional": true
},
"@sveltejs/kit": {
"optional": true
},
"next": {
"optional": true
},
"react": {
"optional": true
},
"svelte": {
"optional": true
},
"vue": {
"optional": true
},
"vue-router": {
"optional": true
}
}
},
"node_modules/@vitejs/plugin-react": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.7.0.tgz",
@ -2845,6 +2883,7 @@
"name": "@mcp-ui-kit/inspector",
"version": "1.0.0",
"dependencies": {
"@vercel/analytics": "^1.6.1",
"lucide-react": "^0.460.0",
"react": "^19.0.0",
"react-dom": "^19.0.0"

View File

@ -47,8 +47,7 @@
"license": "MIT",
"dependencies": {
"@mcp-ui/server": "^5.15.0",
"esbuild": "^0.25.0",
"esbuild-wasm": "^0.25.0"
"esbuild": "^0.25.0"
},
"peerDependencies": {
"react": ">=18.0.0",

View File

@ -1,4 +1,4 @@
import type * as EsbuildType from 'esbuild';
import * as esbuild from 'esbuild';
// Cache bundled JS per entry path (only used in production)
const bundleCache = new Map<string, string>();
@ -6,47 +6,7 @@ const bundleCache = new Map<string, string>();
// In development mode, skip cache to allow hot-reloading of component changes
const isDev = process.env.NODE_ENV !== 'production';
// Esbuild instance - lazily loaded
let esbuild: typeof EsbuildType | null = null;
let usingWasm = false;
async function getEsbuild(): Promise<typeof EsbuildType> {
if (esbuild) return esbuild;
// Try native esbuild first
try {
console.log('[mcp-ui-kit] Trying native esbuild...');
const native = await import('esbuild');
// Test if native works by running a simple transform
await native.transform('const x = 1', { loader: 'js' });
console.log('[mcp-ui-kit] Native esbuild works!');
esbuild = native;
return esbuild;
} catch (nativeError) {
console.log('[mcp-ui-kit] Native esbuild failed:', nativeError instanceof Error ? nativeError.message : String(nativeError));
// Fall back to wasm
try {
console.log('[mcp-ui-kit] Falling back to esbuild-wasm...');
const wasm = await import('esbuild-wasm');
await wasm.initialize({
wasmURL: `https://unpkg.com/esbuild-wasm@${wasm.version}/esbuild.wasm`,
});
console.log('[mcp-ui-kit] esbuild-wasm initialized!');
esbuild = wasm as typeof EsbuildType;
usingWasm = true;
return esbuild;
} catch (wasmError) {
console.log('[mcp-ui-kit] esbuild-wasm also failed:', wasmError instanceof Error ? wasmError.message : String(wasmError));
throw wasmError;
}
}
}
async function runBuild(entryPath: string): Promise<EsbuildType.BuildResult<{ write: false }>> {
const esbuild = await getEsbuild();
console.log('[mcp-ui-kit] runBuild using', usingWasm ? 'wasm' : 'native', 'for:', entryPath);
async function runBuild(entryPath: string): Promise<esbuild.BuildResult<{ write: false }>> {
return esbuild.build({
entryPoints: [entryPath],
bundle: true,
@ -63,32 +23,25 @@ async function runBuild(entryPath: string): Promise<EsbuildType.BuildResult<{ wr
}
export async function bundleComponent(entryPath: string): Promise<string> {
console.log('[mcp-ui-kit] bundleComponent called for:', entryPath);
if (!isDev && bundleCache.has(entryPath)) {
console.log('[mcp-ui-kit] Returning cached bundle');
return bundleCache.get(entryPath)!;
}
let result: EsbuildType.BuildResult<{ write: false }>;
let result: esbuild.BuildResult<{ write: false }>;
try {
result = await runBuild(entryPath);
console.log('[mcp-ui-kit] Build succeeded');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.log('[mcp-ui-kit] Build failed:', errorMessage);
// Handle service errors for native esbuild in serverless
const isServiceError = !usingWasm && error instanceof Error && (
// Handle service errors in serverless environments (Vercel, Lambda, etc.)
const isServiceError = error instanceof Error && (
errorMessage.includes('service was stopped') ||
errorMessage.includes('service is no longer running') ||
errorMessage.includes('The service')
);
if (isServiceError) {
console.log('[mcp-ui-kit] Service error, stopping and retrying...');
const esbuild = await getEsbuild();
await esbuild.stop();
result = await runBuild(entryPath);
} else {
@ -97,7 +50,6 @@ export async function bundleComponent(entryPath: string): Promise<string> {
}
const bundledJS = result.outputFiles[0].text;
console.log('[mcp-ui-kit] Bundle size:', bundledJS.length, 'bytes');
if (!isDev) {
bundleCache.set(entryPath, bundledJS);

View File

@ -218,6 +218,7 @@
version "1.0.0"
resolved "file:packages/inspector"
dependencies:
"@vercel/analytics" "^1.6.1"
lucide-react "^0.460.0"
react "^19.0.0"
react-dom "^19.0.0"
@ -427,6 +428,11 @@
"@types/node" "*"
"@types/send" "<1"
"@vercel/analytics@^1.6.1":
version "1.6.1"
resolved "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.6.1.tgz"
integrity sha512-oH9He/bEM+6oKlv3chWuOOcp8Y6fo6/PSro8hEkgCW3pu9/OiCXiUpRUogDh3Fs3LH2sosDrx8CxeOLBEE+afg==
"@vitejs/plugin-react@^4.3.4":
version "4.7.0"
resolved "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.7.0.tgz"
@ -1234,7 +1240,7 @@ react-refresh@^0.17.0:
resolved "https://registry.npmjs.org/react-refresh/-/react-refresh-0.17.0.tgz"
integrity sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==
"react@^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0-rc", react@^19.0.0, react@^19.2.0:
"react@^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0-rc", "react@^18 || ^19 || ^19.0.0-rc", react@^19.0.0, react@^19.2.0:
version "19.2.0"
resolved "https://registry.npmjs.org/react/-/react-19.2.0.tgz"
integrity sha512-tmbWg6W31tQLeB5cdIBOicJDJRR2KzXsV7uSK9iNfLWQ5bIZfxuPEHp7M8wiHyHnn0DD1i7w3Zmin0FtkrwoCQ==