* feat(auth): inject MCP_INSPECTOR_API_TOKEN into served index.html (#1378) Reloading the web client at the bare URL (no `?MCP_INSPECTOR_API_TOKEN=…` query string) with empty sessionStorage made every `/api/*` request 401 — the browser had no way to recover the backend's auth token. Embed the token into `index.html` on every page load so the browser no longer depends on the query string surviving navigation: - New shared helper `clients/web/server/inject-auth-token.ts` embeds `<script>window.__INSPECTOR_API_TOKEN__ = "…"</script>` (escaped against `</script>` injection; no-op when auth is dangerously omitted). - Dev: the Vite plugin injects via `transformIndexHtml`. - Prod: the Hono server injects on the `/` route. - `App.tsx` `getAuthToken()` now reads the injected global first, then the query string, then sessionStorage (both fallbacks preserved). - Shared global name lives in `INSPECTOR_API_TOKEN_GLOBAL` (`core/mcp/remote/constants.ts`). Tests: helper unit coverage + an integration test exercising the real prod server's `/` → `/api/*` flow (injected token authenticates; missing token 401s). AGENTS.md documents the token-recovery order. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(auth): inject token into prod SPA fallback + prime sessionStorage + no-store (#1378) Addresses code-review feedback on the token-injection PR: - Prod `server.ts` SPA deep-link fallback (e.g. `/oauth/callback`) previously served the raw index.html off disk via serveStatic, bypassing injection — so a bookmark/reload at a non-`/` route with empty sessionStorage would 401. Route the SPA fallback through the same `serveIndexHtml` (inject) helper; real static assets (paths with a dot) still serve verbatim. Dev already injected on every HTML serve via Vite `transformIndexHtml`. - `getAuthToken()` now persists the injected `window.__INSPECTOR_API_TOKEN__` to sessionStorage (not just the URL-param branch), priming the backstop for any later navigation that loses the global. - Injected HTML responses now send `Cache-Control: no-store`, so a page carrying a token isn't cached and served stale after a restart regenerates the token. Integration tests added: SPA fallback (`/oauth/callback`) carries the token, `Cache-Control: no-store` on injected HTML, real assets served verbatim, and unknown `/api` routes 404 rather than falling through to the HTML shell. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
168 lines
5.3 KiB
TypeScript
168 lines
5.3 KiB
TypeScript
/**
|
|
* Hono production server. Export startHonoServer(config) for in-process use by the runner.
|
|
* When run as the main module (e.g. node dist/server.js), build config from env and start.
|
|
*/
|
|
|
|
import { readFileSync } from "node:fs";
|
|
import { join, dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { randomBytes } from "node:crypto";
|
|
import open from "open";
|
|
import { serve } from "@hono/node-server";
|
|
import { serveStatic } from "@hono/node-server/serve-static";
|
|
import { Hono } from "hono";
|
|
import type { Context } from "hono";
|
|
import { createRemoteApp } from "../../../core/mcp/remote/node/server.ts";
|
|
import { createSandboxController } from "./sandbox-controller.js";
|
|
import { injectAuthToken } from "./inject-auth-token.js";
|
|
import type { WebServerConfig } from "./web-server-config.js";
|
|
import {
|
|
webServerConfigToInitialPayload,
|
|
buildWebServerConfigFromEnv,
|
|
printServerBanner,
|
|
} from "./web-server-config.js";
|
|
import type { WebServerHandle } from "./types.js";
|
|
|
|
export type { WebServerHandle };
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
/**
|
|
* Start the Hono production server in-process. Returns a handle that closes sandbox then HTTP server.
|
|
* Caller owns SIGINT/SIGTERM; do not register signal handlers here.
|
|
*/
|
|
export async function startHonoServer(
|
|
config: WebServerConfig,
|
|
): Promise<WebServerHandle> {
|
|
const sandboxController = createSandboxController({
|
|
port: config.sandboxPort,
|
|
host: config.sandboxHost,
|
|
});
|
|
await sandboxController.start();
|
|
|
|
const resolvedAuthToken =
|
|
config.authToken ||
|
|
(config.dangerouslyOmitAuth ? "" : randomBytes(32).toString("hex"));
|
|
|
|
const rootPath = config.staticRoot ?? __dirname;
|
|
|
|
const { app: apiApp, close: closeApi } = createRemoteApp({
|
|
authToken: config.dangerouslyOmitAuth ? undefined : resolvedAuthToken,
|
|
dangerouslyOmitAuth: config.dangerouslyOmitAuth,
|
|
storageDir: config.storageDir,
|
|
allowedOrigins: config.allowedOrigins,
|
|
sandboxUrl: sandboxController.getUrl() ?? undefined,
|
|
logger: config.logger,
|
|
initialConfig: webServerConfigToInitialPayload(config),
|
|
});
|
|
|
|
const app = new Hono();
|
|
app.use("/api/*", async (c) => {
|
|
return apiApp.fetch(c.req.raw);
|
|
});
|
|
|
|
// Serve index.html with the API token injected so a reload at any bare URL
|
|
// (no `?MCP_INSPECTOR_API_TOKEN=…`) still authenticates against /api/*.
|
|
// No-op when auth is dangerously omitted (empty token). The dev Vite plugin
|
|
// applies the same injection via `transformIndexHtml`. `Cache-Control:
|
|
// no-store` keeps a browser/proxy from serving a page that carries a stale
|
|
// token after a server restart regenerates it (randomBytes per start).
|
|
const serveIndexHtml = (c: Context) => {
|
|
const indexPath = join(rootPath, "index.html");
|
|
const html = readFileSync(indexPath, "utf-8");
|
|
c.header("Cache-Control", "no-store");
|
|
return c.html(injectAuthToken(html, resolvedAuthToken));
|
|
};
|
|
|
|
app.get("/", async (c) => {
|
|
try {
|
|
return serveIndexHtml(c);
|
|
} catch (error) {
|
|
console.error("Error serving index.html:", error);
|
|
return c.notFound();
|
|
}
|
|
});
|
|
|
|
// Real static assets (paths with a file extension). Missing files fall
|
|
// through to the SPA fallback below via `next()`.
|
|
app.use("/*", serveStatic({ root: rootPath }));
|
|
|
|
// SPA deep-link fallback: any non-/api route that didn't resolve to a static
|
|
// asset (e.g. `/oauth/callback`, the OAuth landing URL) serves the *injected*
|
|
// index.html — not the raw file — so bookmarks and hand-typed reloads at
|
|
// those paths get the token global too, matching the `/` route.
|
|
app.get("*", async (c) => {
|
|
if (c.req.path.startsWith("/api")) {
|
|
return c.notFound();
|
|
}
|
|
try {
|
|
return serveIndexHtml(c);
|
|
} catch (error) {
|
|
console.error("Error serving index.html:", error);
|
|
return c.notFound();
|
|
}
|
|
});
|
|
|
|
const httpServer = serve(
|
|
{
|
|
fetch: app.fetch,
|
|
port: config.port,
|
|
hostname: config.hostname,
|
|
},
|
|
(info) => {
|
|
const sandboxUrl = sandboxController.getUrl();
|
|
const url = printServerBanner(
|
|
config,
|
|
info.port,
|
|
resolvedAuthToken,
|
|
sandboxUrl ?? undefined,
|
|
);
|
|
if (config.autoOpen) {
|
|
open(url);
|
|
}
|
|
},
|
|
);
|
|
|
|
httpServer.on("error", (err: Error) => {
|
|
if (err.message.includes("EADDRINUSE")) {
|
|
console.error(
|
|
`MCP Inspector PORT IS IN USE at http://${config.hostname}:${config.port}`,
|
|
);
|
|
process.exit(1);
|
|
} else {
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
return {
|
|
async close(): Promise<void> {
|
|
await closeApi();
|
|
await sandboxController.close();
|
|
if ("closeAllConnections" in httpServer) {
|
|
httpServer.closeAllConnections();
|
|
}
|
|
await new Promise<void>((resolve, reject) => {
|
|
httpServer.close((err) => (err ? reject(err) : resolve()));
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Run when this file is executed as the main module (e.g. node dist/server.js). */
|
|
async function runStandalone(): Promise<void> {
|
|
const config = buildWebServerConfigFromEnv();
|
|
const handle = await startHonoServer(config);
|
|
const shutdown = () => {
|
|
void handle.close().then(() => process.exit(0));
|
|
};
|
|
process.on("SIGINT", shutdown);
|
|
process.on("SIGTERM", shutdown);
|
|
}
|
|
|
|
const isMain =
|
|
process.argv[1] !== undefined &&
|
|
resolve(process.argv[1]) === resolve(__filename);
|
|
if (isMain) {
|
|
void runStandalone();
|
|
}
|