inspector/core/client/runner.ts
Cliff Hall 373f346e5f
Some checks failed
Run install, format, lint, build, and test on every push / build (push) Has been cancelled
Run install, format, lint, build, and test on every push / publish (push) Has been cancelled
Run install, format, lint, build, and test on every push / publish-github-container-registry (push) Has been cancelled
chore: close quality-gate coverage gaps (#1689) (#1766)
Closes #1689. Typecheck cli/tui src, add a core/ format+lint gate, and gate core/json + core/client coverage. See PR #1766 for the full review history.
2026-07-24 17:20:12 -04:00

127 lines
3.8 KiB
TypeScript

/**
* Install-level client config loading and InspectorClient auth option wiring
* for Node runners (TUI, CLI).
*/
import {
KeyringSecretStore,
type SecretStore,
} from "../auth/node/secret-store.js";
import type {
InspectorClientOptions,
InspectorServerSettings,
} from "../mcp/types.js";
import { loadClientConfig } from "./config.js";
import type { ClientConfig } from "./types.js";
import {
getActiveCimdClientMetadataUrl,
getActiveEnterpriseManagedAuthIdp,
} from "./types.js";
export interface LoadRunnerClientConfigOptions {
/** Explicit path from `--client-config` (or MCP_CLIENT_CONFIG_PATH when unset). */
clientConfigPath?: string;
/** Secret store for the IdP clientSecret; defaults to the OS keychain. Tests
* inject an in-memory store for determinism. */
secretStore?: SecretStore;
}
/** Load install-level client.json with keychain-backed IdP secrets. */
export async function loadRunnerClientConfig(
options?: LoadRunnerClientConfigOptions,
): Promise<ClientConfig> {
const customPath =
options?.clientConfigPath?.trim() ||
process.env.MCP_CLIENT_CONFIG_PATH?.trim() ||
undefined;
const secretStore = options?.secretStore ?? new KeyringSecretStore();
return loadClientConfig({ filePath: customPath, secretStore });
}
export interface RunnerClientConfigOverrides {
clientId?: string;
clientSecret?: string;
clientMetadataUrl?: string;
}
/** HTTP transports (SSE, streamable-http) can use OAuth. */
export function isOAuthCapableServerConfig(
config: { type?: string } | null | undefined,
): boolean {
if (!config) return false;
return config.type === "sse" || config.type === "streamable-http";
}
/**
* Derive OAuth / EMA / CIMD InspectorClient options from install client.json,
* per-server settings, and CLI flag overrides (flags win over client.json).
*/
export function buildRunnerClientAuthOptions(
clientConfig: ClientConfig,
savedSettings?: InspectorServerSettings,
cliOverrides?: RunnerClientConfigOverrides,
): Pick<
InspectorClientOptions,
| "oauth"
| "enterpriseManagedAuth"
| "installEnterpriseManagedAuth"
| "directAuthRecovery"
> {
const activeIdp = getActiveEnterpriseManagedAuthIdp(clientConfig);
const activeCimdUrl = getActiveCimdClientMetadataUrl(clientConfig);
const oauthFromServer =
savedSettings &&
(savedSettings.oauthClientId ||
savedSettings.oauthClientSecret ||
savedSettings.oauthScopes ||
savedSettings.enterpriseManaged)
? {
...(savedSettings.oauthClientId && {
clientId: savedSettings.oauthClientId,
}),
...(savedSettings.oauthClientSecret && {
clientSecret: savedSettings.oauthClientSecret,
}),
...(savedSettings.oauthScopes && {
scope: savedSettings.oauthScopes,
}),
...(savedSettings.enterpriseManaged && {
enterpriseManaged: true,
}),
}
: undefined;
const clientMetadataUrl =
cliOverrides?.clientMetadataUrl?.trim() || activeCimdUrl;
const oauthFromCli =
cliOverrides?.clientId || cliOverrides?.clientSecret || clientMetadataUrl
? {
...(cliOverrides?.clientId && { clientId: cliOverrides.clientId }),
...(cliOverrides?.clientSecret && {
clientSecret: cliOverrides.clientSecret,
}),
...(clientMetadataUrl && { clientMetadataUrl }),
}
: undefined;
const oauth =
oauthFromServer || oauthFromCli
? {
...(oauthFromServer ?? {}),
...(oauthFromCli ?? {}),
}
: undefined;
return {
...(oauth && { oauth }),
...(activeIdp && {
enterpriseManagedAuth: { idp: activeIdp },
}),
...(clientConfig.enterpriseManagedAuth && {
installEnterpriseManagedAuth: clientConfig.enterpriseManagedAuth,
}),
...(oauth && { directAuthRecovery: true }),
};
}