inspector/core/mcp/node/config.ts
Cliff Hall 0ead1deea6
feat(core): port v1.5 InspectorClient + transports + auth subsystem (#1302) (#1306)
* wip(core): bulk-copy v1.5 InspectorClient runtime files (#1302)

Verbatim ports from origin/v1.5/main into v2/main core/:

- logging/ (2 files)
- storage/ + storage/adapters/ (4 files)
- auth/ + auth/browser/ + auth/node/ + auth/remote/ (16 files)
- mcp/config.ts + mcp/node/ (4 files, stdio transport)
- mcp/remote/ + mcp/remote/node/ (12 files, HTTP/SSE transport)
- mcp/inspectorClient.ts (~2169 LOC), messageTrackingTransport,
  fetchTracking, oauthManager, index, state/index

Total: ~7,800 LOC of source files, copied unmodified via
`git show origin/v1.5/main:<path> > <dest>`.

Also:
- Add deps: pino, hono, @hono/node-server, atomically, zustand, @types/node
- tsconfig.app.json: extend paths to resolve new node-only deps from
  core/, add "node" to types, disable erasableSyntaxOnly (v1.5 code
  uses parameter properties — TODO to re-enable in follow-up)

Compiles down to 47 type errors, all in three categories:
- 24 TS2305: v2 dropped v1.5 type exports (InspectorClientOptions,
  CreateTransport, InspectorClientEnvironment, AppRendererClient,
  SamplingCreateMessage class, ElicitationCreateMessage class,
  jsonUtils helpers, OAuth events). Per user direction, will re-add
  these to v2's diverged files (types.ts, samplingCreateMessage.ts,
  etc.) so the ported code stays verbatim.
- 15 TS7006: implicit-any params in v1.5 code (v1.5 tsconfig was
  looser than v2's noImplicitAny). Need explicit annotations.
- 8 other TS errors: ../package.json import, Transport|null narrowing,
  hono Context generic mismatch, one unused parameter.

Tests not yet ported (19 files, ~11,170 LOC).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* wip(core): integrate ported v1.5 InspectorClient + port tests (#1302)

Source integration (typecheck + build now pass):
- Re-add v1.5 types/classes dropped during v2 evolution:
  - types.ts: CreateTransport*, AppRendererClient, InspectorClient{Environment,Options}
  - samplingCreateMessage.ts / elicitationCreateMessage.ts: classes + keep v2's
    InspectorPending{Sampling,Elicitation} interface
  - inspectorClientEventTarget.ts: oauth* event keys
  - json/jsonUtils.ts: convertParameterValue / convertToolParameters /
    convertPromptArguments + ParameterSchema
- inspectorClient.ts: replace `../package.json` import (v2 has no
  core/package.json) with a hardcoded identity constant
- remote/node/server.ts: cast hono Context for streamSSE (hono's
  generic typing tightened since v1.5)
- oauth-callback-server.ts: drop unused hostname field
- tsconfig: enable @types/node, add paths for new bare-module deps
  (pino, zustand, hono, atomically) in app + test configs
- vite.config.ts: mirror those module aliases for the unit project
  (which runs from repoRoot)

Tests ported (19 files, ~11K LOC) under clients/web/src/test/core/:
- All core/__tests__/auth/* (7 files)
- inspectorClient.test.ts + 4 OAuth variants
- mcp/oauthManager.test.ts, jsonUtils.test.ts
- transport, remote-transport, remote-server-config, storage-adapters
- helpers/oauth-client-fixtures.ts
Bulk-rewrote `from "../mcp/…"` → `from "@inspector/core/mcp/…"` etc.

Current state:
- typecheck: 0 errors
- build: passes (vite v8, 788 modules)
- unit tests: 1014 / 1034 pass (937 pre-existing + 77 new core tests)
- 20 failures in 13 files; failure modes:
  - 13 file-load failures needing the v1.5 `test-servers/` package
    (@modelcontextprotocol/inspector-test-server) which isn't in v2 yet
    or real fs/network not available in happy-dom
  - 7 assertion failures in discovery.test.ts + state-machine.test.ts
    (SDK 1.29 vs 1.25 API drift — mocks don't match new shape)

Coverage gate not yet checked against new files.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* wip(core): port test-servers, more types, partial test integration (#1302)

- Port v1.5 test-servers/ package (12 files, ~5K LOC) under
  repo-root/test-servers/, wire @modelcontextprotocol/inspector-test-server
  alias in both vite.config.ts (regex form) and tsconfig.test.json.
- Add deps: express ^5, yaml ^2, @types/express ^5.
- Re-export InspectorClientOptions/Environment/Transport* from inspectorClient.ts
  so v1.5 tests that import them from that module path keep resolving.
- Switch InspectorClientEventMap to use SamplingCreateMessage /
  ElicitationCreateMessage classes (matches v1.5 event payload contracts)
  while keeping the InspectorPending{Sampling,Elicitation} interfaces in
  the *.ts modules for v2 consumers that still use them.
- Fix verbatim-module-syntax issue in test-servers and drop unused stdio
  config field.
- Adjust inspectorClient.test.ts task types to TaskWithOptionalCreatedAt
  (SDK 1.29 tightened Task shape vs v1.5's 1.25).
- Drop spurious maxMessages/maxFetchRequests/maxStderrLogEvents from
  remote-transport.test.ts (never on InspectorClientOptions).

State:
- typecheck: 0 errors
- build: passes
- unit tests: 1099 / 1270 pass
- 171 failures in 10 test files, almost all integration tests that need
  real HTTP/fs/network (happy-dom can't host them). Next: add a node-env
  vitest project to run these properly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(core): port v1.5 InspectorClient + transports + auth subsystem (#1302)

Bulk-port of the v1.5 core runtime (~7,800 LOC source + ~16K LOC tests +
~5K LOC test-servers/) into v2. Files copied verbatim from origin/v1.5/main
via `git show >`; only minimal adapters added on the v2 side.

Subsystems landed
-----------------
- core/logging/, core/storage/, core/auth/ (browser + node + remote)
- core/mcp/ runtime: inspectorClient.ts (2169 LOC), oauthManager,
  messageTrackingTransport, fetchTracking, config, index
- core/mcp/node/ (stdio transport)
- core/mcp/remote/ + remote/node/ (HTTP/SSE transport + Hono server)
- test-servers/ package (composable MCP test servers, fixtures, harness)

v2 adapter layer
----------------
- types.ts: append CreateTransport*, AppRendererClient,
  InspectorClient{Environment,Options} (v2 had pruned these — v1.5 needs them)
- samplingCreateMessage / elicitationCreateMessage: keep v1.5 classes AND
  v2's Inspector Pending* interfaces in the same files
- inspectorClientEventTarget: add oauth* event keys; switch pending
  sampling/elicitation payloads to the classes (matches v1.5 contracts)
- json/jsonUtils: append convertParameterValue/convertToolParameters/
  convertPromptArguments + ParameterSchema
- inspectorClient.ts: re-export Options/Environment/Transport* so tests
  that import these from `inspectorClient.js` keep resolving; replace
  `../package.json` import with a hardcoded identity (v2 has no core/package.json)
- remote/node/server.ts: cast hono Context for streamSSE (hono generic
  tightened since v1.5)
- oauth-callback-server: drop unused hostname field
- tsconfig.app.json / tsconfig.test.json: add @types/node + paths for
  pino/zustand/hono/@hono/node-server/atomically/express/yaml, disable
  erasableSyntaxOnly (parameter-property syntax from v1.5)
- vite.config.ts: regex-form module aliases (so the package's exports
  field handles subpaths like `hono/streaming`, `zustand/middleware`),
  alias for @modelcontextprotocol/inspector-test-server → test-servers/src

Tests
-----
- 19 v1.5 test files copied under clients/web/src/test/core/,
  imports rewired to @inspector/core/* and @modelcontextprotocol/inspector-test-server
- 1004 unit tests pass; 298 storybook tests pass; coverage gate passes

Known follow-ups (out-of-scope here, tracked in TODOs)
------------------------------------------------------
- 13 integration test files are excluded from the unit project (spawn real
  HTTP/stdio servers; run real OAuth flows; mock SDK auth → happy-dom CORS).
  Needs a node-env vitest project. Files listed under `test.projects[0].test.exclude`.
- Source files exercised only by those integration tests are excluded from
  the coverage gate pending the node-env project. Listed under
  `test.coverage.exclude` with comment.
- Manual smoke tests against real stdio / HTTP / OAuth server per the AC
  on #1302 still need verification locally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs/fix: address #1306 review (AGENTS tree, #1307 links, @types/node, .substr)

- AGENTS.md: expand the project-structure tree to include all v1.5-ported
  subsystems (core/auth/{browser,node,remote}, core/storage, core/logging,
  core/mcp/{node,remote/{,node}}, core/mcp/state) plus the new top-level
  test-servers/ directory (with a note about its @modelcontextprotocol/
  inspector-test-server alias).
- vite.config.ts: reference #1307 from both the test exclude block and the
  coverage exclude block so the next person editing either sees the
  marching order for shrinking the lists.
- package.json: restore @types/node to ^24 (an earlier `npm install
  --save-dev @types/node@^22` accidentally pinned it lower than the repo
  was using; nothing here required ^22).
- core/mcp/fetchTracking.ts: replace deprecated .substr(2, 9) with
  .slice(2, 11) to match the convention in core/mcp/inspectorClient.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 15:36:17 -04:00

357 lines
11 KiB
TypeScript

import { existsSync, readFileSync } from "fs";
import { resolve } from "path";
import type {
MCPConfig,
MCPServerConfig,
ServerType,
StdioServerConfig,
SseServerConfig,
StreamableHttpServerConfig,
} from "../types.js";
/**
* Options object passed to resolveServerConfigs by runners (parsed from argv).
* Core exports this type so runners can type the subset they pass in.
*/
export interface ServerConfigOptions {
configPath?: string;
serverName?: string;
/** Command + args for stdio, or [url] for SSE/HTTP. Positional / args after -- */
target?: string[];
transport?: "stdio" | "sse" | "http";
serverUrl?: string;
cwd?: string;
env?: Record<string, string>;
headers?: Record<string, string>;
}
/**
* Parse KEY=VALUE into a record. Used as Commander option coerce/accumulator for -e.
* Pure function; no Commander dependency.
*/
export function parseKeyValuePair(
value: string,
previous: Record<string, string> = {},
): Record<string, string> {
const parts = value.split("=");
const key = parts[0] ?? "";
const val = parts.slice(1).join("=");
if (!key || val === undefined || val === "") {
throw new Error(
`Invalid parameter format: ${value}. Use key=value format.`,
);
}
return { ...previous, [key]: val };
}
/**
* Parse "HeaderName: Value" into a record. Used as Commander option coerce/accumulator for --header.
* Pure function; no Commander dependency.
*/
export function parseHeaderPair(
value: string,
previous: Record<string, string> = {},
): Record<string, string> {
const colonIndex = value.indexOf(":");
if (colonIndex === -1) {
throw new Error(
`Invalid header format: ${value}. Use "HeaderName: Value" format.`,
);
}
const key = value.slice(0, colonIndex).trim();
const val = value.slice(colonIndex + 1).trim();
if (key === "" || val === "") {
throw new Error(
`Invalid header format: ${value}. Use "HeaderName: Value" format.`,
);
}
return { ...previous, [key]: val };
}
/**
* Normalizes server type: missing → "stdio", "http" → "streamable-http".
* Returns a new object; input may be parsed JSON with type omitted or "http".
*/
function normalizeServerType(
config: Record<string, unknown> & { type?: string },
): MCPServerConfig {
const type = config.type;
const normalizedType: ServerType =
type === undefined
? "stdio"
: type === "http"
? "streamable-http"
: (type as ServerType);
return { ...config, type: normalizedType } as MCPServerConfig;
}
/**
* Loads and validates an MCP servers configuration file.
* Checks file existence before reading. Normalizes each server's type
* (missing → "stdio", "http" → "streamable-http").
*
* @param configPath - Path to the config file (relative to process.cwd() or absolute)
* @returns The parsed MCPConfig with normalized server types
* @throws Error if the file is missing, cannot be loaded, parsed, or is invalid
*/
function loadMcpServersConfig(configPath: string): MCPConfig {
try {
const resolvedPath = resolve(process.cwd(), configPath);
if (!existsSync(resolvedPath)) {
throw new Error(`Config file not found: ${resolvedPath}`);
}
const configContent = readFileSync(resolvedPath, "utf-8");
const config = JSON.parse(configContent) as MCPConfig;
if (!config.mcpServers) {
throw new Error("Configuration file must contain an mcpServers element");
}
const normalizedServers: Record<string, MCPServerConfig> = {};
for (const [name, raw] of Object.entries(config.mcpServers)) {
normalizedServers[name] = normalizeServerType(
raw as unknown as Record<string, unknown> & { type?: string },
);
}
return { ...config, mcpServers: normalizedServers };
} catch (error) {
if (error instanceof Error) {
throw new Error(`Error loading configuration: ${error.message}`);
}
throw new Error("Error loading configuration: Unknown error");
}
}
/**
* Loads a single server config from an MCP config file by name.
* Delegates to loadMcpServersConfig (file existence and type normalization are done there).
*/
function loadServerFromConfig(
configPath: string,
serverName: string,
): MCPServerConfig {
const config = loadMcpServersConfig(configPath);
if (!config.mcpServers[serverName]) {
const available = Object.keys(config.mcpServers).join(", ");
throw new Error(
`Server '${serverName}' not found in config file. Available servers: ${available}`,
);
}
return config.mcpServers[serverName];
}
/** Build one MCPServerConfig from ad-hoc options (no config file). */
function buildConfigFromOptions(options: ServerConfigOptions): MCPServerConfig {
const target = options.target ?? [];
const first = target[0];
const rest = target.slice(1);
const urlFromTarget =
first && (first.startsWith("http://") || first.startsWith("https://"))
? first
: null;
const url = urlFromTarget ?? options.serverUrl ?? null;
if (url) {
if (rest.length > 0 && urlFromTarget) {
throw new Error("Arguments cannot be passed to a URL-based MCP server.");
}
let transportType: "sse" | "streamable-http";
const t =
options.transport === "http" ? "streamable-http" : options.transport;
if (t === "sse" || t === "streamable-http") {
transportType = t;
} else {
const u = new URL(url);
if (u.pathname.endsWith("/mcp")) {
transportType = "streamable-http";
} else if (u.pathname.endsWith("/sse")) {
transportType = "sse";
} else {
throw new Error(
`Transport type not specified and could not be determined from URL: ${url}.`,
);
}
}
if (transportType === "sse") {
const config: SseServerConfig = { type: "sse", url };
if (options.headers && Object.keys(options.headers).length > 0) {
config.headers = options.headers;
}
return config;
}
const config: StreamableHttpServerConfig = { type: "streamable-http", url };
if (options.headers && Object.keys(options.headers).length > 0) {
config.headers = options.headers;
}
return config;
}
if (target.length === 0 || !first) {
throw new Error(
"Target is required. Specify a URL or a command to execute.",
);
}
if (options.transport && options.transport !== "stdio") {
throw new Error("Only stdio transport can be used with local commands.");
}
const config: StdioServerConfig = { type: "stdio", command: first };
if (rest.length > 0) config.args = rest;
if (options.env && Object.keys(options.env).length > 0)
config.env = options.env;
if (options.cwd?.trim()) config.cwd = options.cwd.trim();
return config;
}
/** Apply env/cwd overrides to a stdio config; headers to sse/streamable-http. */
function applyOverrides(
config: MCPServerConfig,
overrides: {
env?: Record<string, string>;
cwd?: string;
headers?: Record<string, string>;
},
): MCPServerConfig {
if (config.type === "stdio") {
const c = { ...config } as StdioServerConfig;
if (overrides.env && Object.keys(overrides.env).length > 0) {
c.env = { ...(c.env ?? {}), ...overrides.env };
}
if (overrides.cwd) c.cwd = overrides.cwd;
return c;
}
if (config.type === "sse" || config.type === "streamable-http") {
const c = { ...config };
if (overrides.headers && Object.keys(overrides.headers).length > 0) {
c.headers = { ...(c.headers ?? {}), ...overrides.headers };
}
return c;
}
return config;
}
export type ResolveServerConfigsMode = "single" | "multi";
/**
* Resolves server config(s) from options and mode. Used by all runners.
* Single mode: one config (from file + overrides, or from args).
* Multi mode: all servers from file (with optional env/cwd/headers overrides), or one from args; errors if config path + transport/serverUrl/positional.
*/
export function resolveServerConfigs(
options: ServerConfigOptions,
mode: ResolveServerConfigsMode,
): MCPServerConfig[] {
const hasConfigPath = Boolean(options.configPath?.trim());
const hasAdHoc =
(options.target && options.target.length > 0) ||
Boolean(options.transport) ||
Boolean(options.serverUrl);
if (mode === "single") {
if (hasConfigPath && options.serverName) {
const config = loadServerFromConfig(
options.configPath!,
options.serverName,
);
return [
applyOverrides(config, {
env: options.env,
cwd: options.cwd,
headers: options.headers,
}),
];
}
if (hasConfigPath && !options.serverName) {
const configPath = options.configPath!;
const mcpConfig = loadMcpServersConfig(configPath);
const servers = Object.keys(mcpConfig.mcpServers);
if (servers.length === 0)
throw new Error("No servers found in config file");
if (servers.length > 1) {
throw new Error(
`Multiple servers found in config file. Please specify one with --server. Available servers: ${servers.join(", ")}`,
);
}
const serverName = servers[0];
if (!serverName) throw new Error("No servers found in config file");
const config = loadServerFromConfig(configPath, serverName);
return [
applyOverrides(config, {
env: options.env,
cwd: options.cwd,
headers: options.headers,
}),
];
}
return [buildConfigFromOptions(options)];
}
if (mode === "multi") {
if (hasConfigPath && hasAdHoc) {
throw new Error(
"In multi-server mode with a config file, do not pass --transport, --server-url, or positional command/URL. Use only --config with optional -e, --cwd, --header.",
);
}
if (hasConfigPath && options.configPath) {
const configPath = options.configPath;
const mcpConfig = loadMcpServersConfig(configPath);
const configs = Object.values(mcpConfig.mcpServers).map((c) =>
applyOverrides({ ...c } as MCPServerConfig, {
env: options.env,
cwd: options.cwd,
headers: options.headers,
}),
);
return configs;
}
return [buildConfigFromOptions(options)];
}
return [];
}
/**
* Returns named server configs from a config file (multi-server). Use when the caller
* needs server names (e.g. TUI). Errors if config path is missing or if ad-hoc options
* (target, transport, serverUrl) are also provided.
*/
export function getNamedServerConfigs(
options: ServerConfigOptions,
): Record<string, MCPServerConfig> {
const hasConfigPath = Boolean(options.configPath?.trim());
const hasAdHoc =
(options.target && options.target.length > 0) ||
Boolean(options.transport) ||
Boolean(options.serverUrl);
if (!hasConfigPath) {
throw new Error("Config path is required for getNamedServerConfigs.");
}
if (hasAdHoc) {
throw new Error(
"With a config file, do not pass --transport, --server-url, or positional command/URL. Use only --config with optional -e, --cwd, --header.",
);
}
const mcpConfig = loadMcpServersConfig(options.configPath!);
const result: Record<string, MCPServerConfig> = {};
for (const [name, config] of Object.entries(mcpConfig.mcpServers)) {
result[name] = applyOverrides(
{ ...config },
{
env: options.env,
cwd: options.cwd,
headers: options.headers,
},
);
}
return result;
}