Skip to content

Commit e3fe112

Browse files
committed
chore: refactor MCP resolution logic by removing caching mechanism and simplifying return values in Next.js and Nuxt.js handlers
1 parent 2a7ce29 commit e3fe112

3 files changed

Lines changed: 6 additions & 26 deletions

File tree

packages/next/src/route.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ function color(level: BrowserLogLevel, msg: string) {
7575
}
7676
function dim(s: string) { return c.dim + s + c.reset; }
7777

78-
let __mcpProjectCache: { url: string; routeLogs?: `/${string}` } | null = null;
79-
8078
async function __resolveMcpFromProject(): Promise<{ url: string; routeLogs?: `/${string}` }> {
81-
// Only cache positive resolutions; always retry if unresolved/empty
82-
if (__mcpProjectCache && __mcpProjectCache.url) return __mcpProjectCache;
8379
try {
8480
const { readFileSync, existsSync } = await import('node:fs');
8581
const { join } = await import('node:path');
@@ -91,13 +87,11 @@ async function __resolveMcpFromProject(): Promise<{ url: string; routeLogs?: `/$
9187
const url = rawUrl.replace(/\/$/, '').replace(/\/mcp$/i, '');
9288
const routeLogs = (data?.route ? String(data.route) : '/__client-logs') as `/${string}`;
9389
if (url && await __pingHealth(`${url}/health`, 250)) {
94-
__mcpProjectCache = { url, routeLogs };
95-
return __mcpProjectCache;
90+
return { url, routeLogs };
9691
}
9792
}
9893
} catch {}
99-
__mcpProjectCache = { url: '' } as any;
100-
return __mcpProjectCache;
94+
return { url: '' } as any;
10195
}
10296

10397
async function __pingHealth(url: string, timeoutMs: number): Promise<boolean> {

packages/nuxt/src/runtime/server/handler.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ function color(level: Level, msg: string) {
7676
}
7777
function dim(s: string) { return c.dim + s + c.reset; }
7878

79-
let __mcpProjectCacheNuxt: { url: string; routeLogs?: `/${string}` } | null = null;
80-
8179
async function __resolveMcpFromProjectNuxt(): Promise<{ url: string; routeLogs?: `/${string}` }> {
82-
// Only cache positive resolutions; always retry if unresolved/empty
83-
if (__mcpProjectCacheNuxt && __mcpProjectCacheNuxt.url) return __mcpProjectCacheNuxt;
8480
try {
8581
const { readFileSync, existsSync } = await import('node:fs');
8682
const { join } = await import('node:path');
@@ -91,12 +87,11 @@ async function __resolveMcpFromProjectNuxt(): Promise<{ url: string; routeLogs?:
9187
const url = (data?.url ? String(data.url) : '').replace(/\/$/, '').replace(/\/mcp$/i, '');
9288
const routeLogs = (data?.route ? String(data.route) as `/${string}` : '/__client-logs');
9389
if (url && await __pingHealthNuxt(`${url}/health`, 300)) {
94-
__mcpProjectCacheNuxt = { url, routeLogs };
95-
return __mcpProjectCacheNuxt;
90+
return { url, routeLogs };
9691
}
9792
}
9893
} catch {}
99-
__mcpProjectCacheNuxt = { url: '' } as any; return __mcpProjectCacheNuxt;
94+
return { url: '' } as any;
10095
}
10196

10297
async function __pingHealthNuxt(url: string, timeoutMs: number): Promise<boolean> {

packages/vite/src/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Avoid exporting Vite types to prevent cross-version type mismatches in consumers
22
import ansis from 'ansis';
33
import type { BrowserLogLevel } from '@browser-echo/core';
4-
import { mkdirSync, appendFileSync, existsSync, readFileSync, watch as fsWatch } from 'node:fs';
4+
import { mkdirSync, appendFileSync, existsSync, readFileSync } from 'node:fs';
55
import { join as joinPath, dirname } from 'node:path';
66

77
export interface BrowserLogsToTerminalOptions {
@@ -162,17 +162,8 @@ function attachMiddleware(server: any, options: ResolvedOptions) {
162162
resolvedIngest = '';
163163
}
164164

165-
// Resolve once at startup and watch for project json changes to re-resolve
165+
// Resolve once at startup
166166
resolveOnce();
167-
try {
168-
const cfgPath = joinPath(process.cwd(), '.browser-echo-mcp.json');
169-
if (existsSync(cfgPath)) {
170-
try { fsWatch(cfgPath, { persistent: false }, () => { try { resolveOnce(); } catch {} }); } catch {}
171-
} else {
172-
const parent = process.cwd();
173-
try { fsWatch(parent, { persistent: false }, (_evt: string, file?: string) => { if (String(file || '') === '.browser-echo-mcp.json') { try { resolveOnce(); } catch {} } }); } catch {}
174-
}
175-
} catch {}
176167

177168
server.middlewares.use(options.route, (req: import('http').IncomingMessage, res: import('http').ServerResponse, next: Function) => {
178169
if (req.method !== 'POST') return next();

0 commit comments

Comments
 (0)