diff --git a/SCIENT.md b/SCIENT.md index ebaeeac3cf..d7e311f1eb 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -14,7 +14,9 @@ Everything else is upstream's code and behavior. | Wording | The agent calls itself Scient Agent, and its command `scient-agent`, wherever a person or a model reads its words: messages, help, sign-in instructions, prompts, the built-in documentation, window and notification titles. The prompts also address researchers: the model's role is a research and coding assistant, and each personality matches its explanations to the reader's expertise instead of assuming a technical reader. `scripts/scient/rename-wording.ts` generates this. | | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | -| Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. | +| Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, on a clock that never runs backwards, whatever the call asks to wait and across a sign-in refresh, a reconnect and a replay of the same call (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID than the plugin now configures (compared exactly) is not used, the sign-in keeps no client secret, so refreshing and re-signing in send only the secret the plugin configures now, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. Its OAuth settings are read as Codex reads them (camelCase aliases giving way to snake_case keys); settings Codex refuses (a client secret without a client ID, or either of them blank; `authorization_server_issuer` or `token_endpoint_auth_method` without `ema_auth`) or a client ID or secret with surrounding whitespace skip the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| MCP sign-in | `scient-agent mcp list` prints each MCP server a conversation loads with its status (connected with its tool count, needs sign-in, failed) and whether a browser sign-in serves it; `scient-agent mcp login ` runs that sign-in and prints the page to open, for a host's own Sign in button (`--runtime-info` lists `mcp` in `hostCommands`). A conversation in RPC mode picks up a sign-in made elsewhere at its next prompt. An authorization server that registers only the apps it approved (Figma) refuses Scient Agent until it approves it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/docs/environment-variables.md b/docs/environment-variables.md index cf89a88a4f..1cb7d747c8 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -11,7 +11,7 @@ It documents only active behavior. ## Host discovery sources -`SCIENT_AGENT_ENABLE_SOURCES` adds foreign user-level discovery sources to the effective `enabledProviders` set without replacing or persisting that setting. Set it in the launch environment, for example `SCIENT_AGENT_ENABLE_SOURCES=claude-plugins` to load Claude Code's installed plugins under `~/.claude/plugins`. Separate multiple provider ids with commas or whitespace; empty and malformed entries are ignored. Ids use lowercase letters, digits and hyphens, starting with a letter; `*` and `all` are also accepted. The normal opt-in semantics apply, including `claude` enabling `claude-plugins`. `disabledProviders` takes precedence. +`SCIENT_AGENT_ENABLE_SOURCES` adds foreign user-level discovery sources to the effective `enabledProviders` set without replacing or persisting that setting. Set it in the launch environment, for example `SCIENT_AGENT_ENABLE_SOURCES=claude-plugins` to load Claude Code's installed plugins under `~/.claude/plugins`, or `codex-plugins` to load the skills and MCP servers of the Codex plugins installed and enabled in `$CODEX_HOME/config.toml` (default `~/.codex`) from Codex's plugin cache. Separate multiple provider ids with commas or whitespace; empty and malformed entries are ignored. Ids use lowercase letters, digits and hyphens, starting with a letter; `*` and `all` are also accepted. The normal opt-in semantics apply, including `claude` enabling `claude-plugins`. `disabledProviders` takes precedence. `--runtime-info` lists the plugin sources the executable can load in `enableSources`. This uses the same environment loading rules as other `SCIENT_AGENT_*` variables: when a host sets `SCIENT_AGENT_ROOT`, project and home `.env` files cannot set it (a `.env` inside the host root can). Standalone use retains the normal `.env` loading rules below. The additions affect discovery only; `getEnabledProviders()`, settings UI values, and source enable/disable writes remain based on settings alone. diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 7420decb64..5784a70102 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -2,6 +2,14 @@ ## [Unreleased] + +### Added + +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, tool calls held to the plugin's `tool_timeout_sec` (five minutes unless set, as in Codex), and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures and signing in again when the plugin changes its OAuth client; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). + +- `scient-agent mcp list` prints each MCP server a conversation loads with its status (connected with its tool count, needs sign-in, failed) and whether a browser sign-in serves it, and `scient-agent mcp login ` runs that sign-in and prints the page to open, for a host's own Sign in button (`--runtime-info` lists `mcp` in `hostCommands`). A conversation in RPC mode picks up a sign-in made elsewhere at its next prompt, without a restart. + + ## [19.0.1] - 2026-11-19 ### Added diff --git a/packages/coding-agent/scripts/compile-binary.ts b/packages/coding-agent/scripts/compile-binary.ts index 31ad55d397..a49b58ed29 100644 --- a/packages/coding-agent/scripts/compile-binary.ts +++ b/packages/coding-agent/scripts/compile-binary.ts @@ -65,6 +65,9 @@ export async function compileCodingAgent(options: CodingAgentCompileOptions): Pr // scient(SA-EXPORT): begin - exports from a binary carry Scient's viewer. "process.env.SCIENT_EXPORT_VIEWER": JSON.stringify(await exportViewerSource(options.repoRoot)), // scient(SA-EXPORT): end + // scient(SA-CODEX-PLUGINS): begin - tells utils env.ts that Bun loaded no dotenv file (autoloadDotenv below). + __SCIENT_AGENT_NO_DOTENV__: "true", + // scient(SA-CODEX-PLUGINS): end }, // Precompiled bytecode skips parsing the ~20 MB bundle at boot: // `omp --version` 256 ms -> 30 ms on M4 Max (+52 MB binary). diff --git a/packages/coding-agent/src/capability/index.ts b/packages/coding-agent/src/capability/index.ts index 69c14e3f1c..00a621230f 100644 --- a/packages/coding-agent/src/capability/index.ts +++ b/packages/coding-agent/src/capability/index.ts @@ -56,6 +56,9 @@ const FOREIGN_USER_PROVIDERS: Record = { codex: true, claude: true, "claude-plugins": true, + // scient(SA-CODEX-PLUGINS): begin - Installed Codex plugins are opt-in like Claude's. + "codex-plugins": true, + // scient(SA-CODEX-PLUGINS): end gemini: true, opencode: true, windsurf: true, diff --git a/packages/coding-agent/src/capability/mcp.ts b/packages/coding-agent/src/capability/mcp.ts index 4b12e9293a..f381618538 100644 --- a/packages/coding-agent/src/capability/mcp.ts +++ b/packages/coding-agent/src/capability/mcp.ts @@ -19,6 +19,10 @@ export interface MCPServer { enabled?: boolean; /** Connection timeout in milliseconds */ timeout?: number; + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls. + /** Hard limit on a tool call in milliseconds, never extended by its arguments (Codex's `tool_timeout_sec`). */ + toolTimeoutMs?: number; + // scient(SA-CODEX-PLUGINS): end /** Encoding for outgoing JSON-RPC request ids (default: `"number"`) */ requestIdFormat?: MCPRequestIdFormat; /** Include server-provided instructions in the system prompt (default: true) */ @@ -36,6 +40,10 @@ export interface MCPServer { envPolicy?: "literal"; /** Env keys whose values are final package data (expanded by the provider). */ envLiteralKeys?: string[]; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + /** When set, the stdio server inherits only these variables from the agent's environment, plus its own `env`. */ + envInherit?: string[]; + // scient(SA-CODEX-PLUGINS): end /** Working directory for stdio transport */ cwd?: string; /** URL (for HTTP/SSE transport) */ @@ -98,6 +106,9 @@ export function isSameMCPConnection(left: MCPServer, right: MCPServer): boolean Bun.deepEquals(left.args, right.args) && Bun.deepEquals(left.env, right.env) && Bun.deepEquals(literalKeysOf(left), literalKeysOf(right)) && + // scient(SA-CODEX-PLUGINS): begin - Inheritance decides what the process sees. + Bun.deepEquals(left.envInherit, right.envInherit) && + // scient(SA-CODEX-PLUGINS): end left.cwd === right.cwd ); } diff --git a/packages/coding-agent/src/cli-commands.ts b/packages/coding-agent/src/cli-commands.ts index 8b44838e51..1584f7f8a6 100644 --- a/packages/coding-agent/src/cli-commands.ts +++ b/packages/coding-agent/src/cli-commands.ts @@ -24,6 +24,9 @@ import { dictateHelp } from "./scient/command-help"; // scient(SA-CHATGPT-APPS): begin - Scient's ChatGPT apps command. import { appsHelp } from "./scient/command-help"; // scient(SA-CHATGPT-APPS): end +// scient(SA-CODEX-PLUGINS): begin - the host's MCP status and sign-in command. +import { mcpHelp } from "./scient/command-help"; +// scient(SA-CODEX-PLUGINS): end function loadLaunchHelp(): typeof LaunchHelp.launchHelp { const module: typeof LaunchHelp = require("./commands/launch-help"); @@ -299,6 +302,13 @@ export const commands: CommandEntry[] = [ help: appsHelp, }, // scient(SA-CHATGPT-APPS): end + // scient(SA-CODEX-PLUGINS): begin - the host's MCP status and sign-in command. + { + name: "mcp", + load: () => import("./scient/mcp-command").then(m => m.default), + help: mcpHelp, + }, + // scient(SA-CODEX-PLUGINS): end { name: "usage", load: () => import("./commands/usage").then(m => m.default), diff --git a/packages/coding-agent/src/cli/auth-broker-cli.ts b/packages/coding-agent/src/cli/auth-broker-cli.ts index 0387ae2b09..0c635a5bcc 100644 --- a/packages/coding-agent/src/cli/auth-broker-cli.ts +++ b/packages/coding-agent/src/cli/auth-broker-cli.ts @@ -124,6 +124,9 @@ export function refreshBrokerOAuthCredential( return refreshManagedMcpOAuthCredential(credential, { serverUrl: mcpOAuthServerUrlFromCredentialId(provider), signal, + // scient(SA-CODEX-PLUGINS): begin - a server-bound row refreshes without a stored secret or redirects. + credentialId: provider, + // scient(SA-CODEX-PLUGINS): end }); } // Non-MCP rows: same per-provider path AuthStorage would take by default diff --git a/packages/coding-agent/src/discovery/index.ts b/packages/coding-agent/src/discovery/index.ts index ff58c99216..46b3d033b8 100644 --- a/packages/coding-agent/src/discovery/index.ts +++ b/packages/coding-agent/src/discovery/index.ts @@ -42,6 +42,9 @@ import "./ssh"; import "./ssh-config"; import "./vscode"; import "./windsurf"; +// scient(SA-CODEX-PLUGINS): begin - Installed, enabled Codex plugins. +import "../scient/codex-plugins"; +// scient(SA-CODEX-PLUGINS): end // Re-export the main API from capability registry export { diff --git a/packages/coding-agent/src/mcp/config.ts b/packages/coding-agent/src/mcp/config.ts index f8224fe4c7..6f1885ca97 100644 --- a/packages/coding-agent/src/mcp/config.ts +++ b/packages/coding-agent/src/mcp/config.ts @@ -12,6 +12,9 @@ import { loadCapability } from "../discovery"; import { readMCPConfigFile } from "./config-writer"; import { builtinDropFor, type MCPDroppedServer, type MCPTransformContext } from "./transforms"; import type { MCPConfigFile, MCPServerConfig } from "./types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { trustedMcpAuth } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): end /** Options for loading MCP configs */ export interface LoadMCPConfigsOptions { @@ -53,7 +56,11 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { timeout: server.timeout, requestIdFormat: server.requestIdFormat, instructions: server.instructions, - auth: server.auth, + // scient(SA-CODEX-PLUGINS): begin - only the codex-plugins source binds a server to its own sign-in, + // and only under the id its name and URL give: any other definition naming one is not bound to it. + auth: trustedMcpAuth(server.name, server.auth, server.url, server._source.provider), + toolTimeoutMs: server.toolTimeoutMs, + // scient(SA-CODEX-PLUGINS): end oauth: server.oauth, }; @@ -67,6 +74,9 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { if (server.env) config.env = server.env; if (server.envPolicy) config.envPolicy = server.envPolicy; if (server.envLiteralKeys) config.envLiteralKeys = server.envLiteralKeys; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + if (server.envInherit) config.envInherit = server.envInherit; + // scient(SA-CODEX-PLUGINS): end if (server.cwd) config.cwd = server.cwd; return config; } diff --git a/packages/coding-agent/src/mcp/manager.ts b/packages/coding-agent/src/mcp/manager.ts index ae1db767ec..a3372817cf 100644 --- a/packages/coding-agent/src/mcp/manager.ts +++ b/packages/coding-agent/src/mcp/manager.ts @@ -40,6 +40,9 @@ import { refreshStoredManagedMcpOAuthCredential, } from "./oauth-credentials"; import type { MCPStoredOAuthCredential } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { withTrustedMcpAuth } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import type { McpConnectionStatusEvent } from "./startup-events"; import { resolveMCPStartupTimeoutMs } from "./timeout"; @@ -972,7 +975,11 @@ export class MCPManager implements MCPManagerApi { // Prepare connection tasks const connectionTasks: ConnectionTask[] = []; - for (const [name, config] of Object.entries(configs)) { + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it, as the + // source supplied with this definition says: a source left registered by an earlier definition never vouches. + for (const [name, declared] of Object.entries(configs)) { + const config = withTrustedMcpAuth(name, declared, sources[name]?.provider); + // scient(SA-CODEX-PLUGINS): end this.#startupServers.add(name); // Connecting a server by name (`/mcp enable`) ends its built-in drop. this.#builtinDrops.delete(name); @@ -1976,6 +1983,9 @@ export class MCPManager implements MCPManagerApi { source: SourceMeta | undefined, reconnectEpoch: number, ): Promise { + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + config = withTrustedMcpAuth(name, config, source?.provider); + // scient(SA-CODEX-PLUGINS): end // Capture before resolving credentials: a disconnectAll during that wait must abort this handshake too. const connectSignal = this.#connectAbort.signal; const resolvedConfig = await this.#resolveAuthConfig(config, { signal: connectSignal }); @@ -2369,6 +2379,9 @@ export class MCPManager implements MCPManagerApi { refreshStoredManagedMcpOAuthCredential(authStorage, credentialId, { serverUrl: config.type === "http" || config.type === "sse" ? config.url : undefined, auth, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in refreshes with the secret configured now. + clientSecret: typeof config.oauth?.clientSecret === "string" ? config.oauth.clientSecret : undefined, + // scient(SA-CODEX-PLUGINS): end forceRefresh: opts?.forceRefresh, keepCredentialOnRefreshFailure: true, onRefreshFailure: refreshError => { diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index d2db7c7b0a..b08499ec72 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -6,6 +6,10 @@ import { expandEnvVarsDeep } from "../discovery/helpers"; import type { AuthStorage } from "../session/auth-storage"; import { isManagedMCPOAuthCredentialId, + // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. + isMcpOAuthServerCredentialId, + mcpOAuthServerCredentialIdMatchesUrl, + // scient(SA-CODEX-PLUGINS): end type MCPStoredOAuthCredential, mcpOAuthCredentialId, mcpOAuthCredentialProfile, @@ -45,6 +49,14 @@ export function lookupMcpOAuthCredentialForServer( if (!authStorage) return undefined; if (auth && auth.type !== "oauth") return undefined; const urlKeyedCredentialIds = mcpOAuthCredentialIdsForServerUrl(serverUrl); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in answers only the URL it was made for. + if ( + isMcpOAuthServerCredentialId(auth?.credentialId) && + !mcpOAuthServerCredentialIdMatchesUrl(auth?.credentialId ?? "", serverUrl) + ) { + return undefined; + } + // scient(SA-CODEX-PLUGINS): end if ( auth?.credentialId && (!auth.credentialId.startsWith("mcp_oauth:profile:") || urlKeyedCredentialIds.includes(auth.credentialId)) @@ -54,6 +66,9 @@ export function lookupMcpOAuthCredentialForServer( return { credentialId: auth.credentialId, credential }; } } + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in never takes another server's by URL. + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return undefined; + // scient(SA-CODEX-PLUGINS): end if (options.allowUrlKeyedFallback === false) return undefined; for (const credentialId of urlKeyedCredentialIds) { const credential = authStorage.credentials.get(credentialId); @@ -73,9 +88,23 @@ export function lookupMcpOAuthCredential( return lookupMcpOAuthCredentialForServer(authStorage, auth, undefined); } if (hasMcpAuthorizationHeader(config)) { + // scient(SA-CODEX-PLUGINS): begin - for a server bound to its own sign-in, the configured + // Authorization header (an imported Codex plugin's bearer) wins over a stored sign-in, as in Codex. + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return undefined; + // scient(SA-CODEX-PLUGINS): end return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url, { allowUrlKeyedFallback: false }); } - return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made for another confidential client than the one + // it now configures is not used or refreshed: Codex compares the exact IDs and asks for a fresh sign-in. + // Its secret is never stored: refresh sends the one the definition configures now. + const lookup = lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); + if (!lookup || !isMcpOAuthServerCredentialId(lookup.credentialId)) return lookup; + const { clientId, clientSecret } = config.oauth ?? {}; + if (typeof clientSecret === "string" && typeof clientId === "string" && lookup.credential.clientId !== clientId) { + return undefined; + } + return lookup; + // scient(SA-CODEX-PLUGINS): end } export function selectMcpOAuthRefreshMaterial( @@ -104,9 +133,23 @@ export function selectMcpOAuthRefreshMaterial( */ export function refreshManagedMcpOAuthCredential( credential: MCPStoredOAuthCredential, - opts: { serverUrl?: string; auth?: MCPAuthConfig; signal?: AbortSignal } = {}, + // scient(SA-CODEX-PLUGINS): begin - every refresh path (manager, broker, `omp token`) names the row, so a + // server-bound sign-in refreshes as Codex does: with the stored client ID and only the secret its current + // definition configures (none when the caller has no definition), and following no redirect. + opts: { + serverUrl?: string; + auth?: MCPAuthConfig; + signal?: AbortSignal; + credentialId?: string; + clientSecret?: string; + } = {}, ): Promise { - const material = selectMcpOAuthRefreshMaterial(credential, opts.auth); + const bound = isMcpOAuthServerCredentialId(opts.credentialId); + const material = selectMcpOAuthRefreshMaterial( + bound ? { ...credential, clientSecret: opts.clientSecret } : credential, + opts.auth, + ); + // scient(SA-CODEX-PLUGINS): end const tokenUrl = material?.tokenUrl; if (!credential.refresh || !tokenUrl) { throw new Error("MCP OAuth credential is missing refresh material"); @@ -118,6 +161,9 @@ export function refreshManagedMcpOAuthCredential( authorizationUrl, stripSameOriginResource: resourceIsFallback, signal: opts.signal, + // scient(SA-CODEX-PLUGINS): begin - a server-bound refresh follows no redirect; others are unchanged. + ...(bound && { rejectRedirects: true }), + // scient(SA-CODEX-PLUGINS): end }); } @@ -167,6 +213,10 @@ export async function refreshStoredManagedMcpOAuthCredential( forceRefresh?: boolean; keepCredentialOnRefreshFailure?: boolean; onRefreshFailure?: (error: unknown) => void; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in's secret comes from its current definition. + /** For a server-bound sign-in, the client secret its definition configures now; a stored one is never sent. */ + clientSecret?: string; + // scient(SA-CODEX-PLUGINS): end } = {}, ): Promise> { const row = authStorage.credentials @@ -178,7 +228,11 @@ export async function refreshStoredManagedMcpOAuthCredential( if (row?.credential.type !== "oauth") { return { credential: undefined, refreshed: false, removed: false }; } + // scient(SA-CODEX-PLUGINS): begin - like Codex, a server-bound sign-in refreshes with the stored client ID and + // the secret configured now, or none; it never sends or keeps a stored secret. + const bound = isMcpOAuthServerCredentialId(provider); const observedCredential: MCPStoredOAuthCredential = row.credential; + // scient(SA-CODEX-PLUGINS): end const serverUrl = opts.serverUrl ?? (opts.recoverServerUrlFromCredentialId ? mcpOAuthServerUrlFromCredentialId(provider) : undefined); @@ -192,6 +246,7 @@ export async function refreshStoredManagedMcpOAuthCredential( const material = selectMcpOAuthRefreshMaterial(current, opts.auth); return Boolean(current.refresh && material?.tokenUrl); }, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in refreshes with the secret configured now. refresh: (current, signal) => current.refresh === REMOTE_REFRESH_SENTINEL ? refreshBrokeredMcpOAuthCredential(authStorage, row.id, provider, signal) @@ -199,7 +254,10 @@ export async function refreshStoredManagedMcpOAuthCredential( serverUrl, auth: opts.auth, signal, + credentialId: provider, + clientSecret: opts.clientSecret, }), + // scient(SA-CODEX-PLUGINS): end mergeRefreshedCredential: (current, refreshed) => { const material = selectMcpOAuthRefreshMaterial(current, opts.auth); const resourceIsFallback = !material?.resource && Boolean(serverUrl); @@ -208,7 +266,9 @@ export async function refreshStoredManagedMcpOAuthCredential( ...refreshed, tokenUrl: material?.tokenUrl, clientId: material?.clientId, - clientSecret: material?.clientSecret, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in keeps no secret. + clientSecret: bound ? undefined : material?.clientSecret, + // scient(SA-CODEX-PLUGINS): end resource: resourceIsFallback ? undefined : material?.resource, authorizationUrl: material && "authorizationUrl" in material ? material.authorizationUrl : undefined, }; diff --git a/packages/coding-agent/src/mcp/oauth-flow.ts b/packages/coding-agent/src/mcp/oauth-flow.ts index 0c5492f114..ed40d8453f 100644 --- a/packages/coding-agent/src/mcp/oauth-flow.ts +++ b/packages/coding-agent/src/mcp/oauth-flow.ts @@ -12,6 +12,9 @@ import type { FetchImpl } from "@oh-my-pi/pi-ai/types"; import { getActiveProfile } from "@oh-my-pi/pi-utils/dirs"; import type { OAuthCredential } from "../session/auth-storage"; import { buildWellKnownUrls } from "./oauth-discovery"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import type { MCPAuthConfig, MCPServerConfig } from "./types"; +// scient(SA-CODEX-PLUGINS): end /** Credential-id prefix for OMP-managed MCP OAuth credentials keyed by profile and server URL. */ const MCP_OAUTH_URL_CREDENTIAL_PREFIX = "mcp_oauth:"; @@ -33,6 +36,72 @@ export function mcpOAuthCredentialId(serverUrl: string, profile: string | undefi return `${MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX}${profile ?? "default"}:${serverUrl}`; } +// scient(SA-CODEX-PLUGINS): begin - a sign-in bound to one server, as Codex keys MCP sign-ins by server name and URL. +/** Credential-id prefix for one server's own sign-in (`mcp_oauth:server:::`). */ +const MCP_OAUTH_SERVER_CREDENTIAL_PREFIX = `${MCP_OAUTH_URL_CREDENTIAL_PREFIX}server:`; + +/** + * Credential id for one server's own sign-in, under its profile. Lookups find it only through the + * server's `auth.credentialId`, never by URL, so no other server at that URL shares it. + */ +export function mcpOAuthServerCredentialId( + serverName: string, + serverUrl: string, + profile: string | undefined = getActiveProfile(), +): string { + return `${MCP_OAUTH_SERVER_CREDENTIAL_PREFIX}${profile ?? "default"}:${encodeURIComponent(serverName)}:${serverUrl}`; +} + +/** Whether a credential id is one server's own sign-in ({@link mcpOAuthServerCredentialId}). */ +export function isMcpOAuthServerCredentialId(credentialId: unknown): boolean { + return typeof credentialId === "string" && credentialId.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX); +} + +/** + * The auth block a server definition may use. A server-bound credential id stays only on the + * server the codex-plugins source imported under it (the id its name and URL give); any other + * definition drops it and finds its sign-in as a native server does. + */ +export function trustedMcpAuth( + name: string, + auth: MCPAuthConfig | undefined, + url: string | undefined, + provider: string | undefined, +): MCPAuthConfig | undefined { + if (!auth || !isMcpOAuthServerCredentialId(auth.credentialId)) return auth; + if ( + provider === "codex-plugins" && + url !== undefined && + auth.credentialId === mcpOAuthServerCredentialId(name, url) + ) { + return auth; + } + return { ...auth, credentialId: undefined }; +} + +/** {@link trustedMcpAuth} applied to a server definition from the source `provider`. */ +export function withTrustedMcpAuth( + name: string, + config: MCPServerConfig, + provider: string | undefined, +): MCPServerConfig { + const url = config.type === "http" || config.type === "sse" ? config.url : undefined; + const auth = trustedMcpAuth(name, config.auth, url, provider); + return auth === config.auth ? config : { ...config, auth }; +} + +/** + * Whether a server's own sign-in id belongs to this server URL under the active profile: a + * definition naming another server's id, or one moved to another URL, gets nothing from it. + */ +export function mcpOAuthServerCredentialIdMatchesUrl(credentialId: string, serverUrl: string | undefined): boolean { + return ( + serverUrl !== undefined && + mcpOAuthCredentialProfile(credentialId) === (getActiveProfile() ?? "default") && + mcpOAuthServerUrlFromCredentialId(credentialId) === serverUrl + ); +} +// scient(SA-CODEX-PLUGINS): end /** Whether a credential id was minted by OMP's MCP OAuth flows (either era). */ export function isManagedMCPOAuthCredentialId(credentialId: string | undefined): credentialId is string { return ( @@ -49,9 +118,14 @@ export function isManagedMCPOAuthCredentialId(credentialId: string | undefined): * is the profile; everything after it is the URL. */ export function mcpOAuthCredentialProfile(credentialId: string): string | undefined { - if (!credentialId.startsWith(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX)) return undefined; - const separator = credentialId.indexOf(":", MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length); - return separator === -1 ? undefined : credentialId.slice(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length, separator); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in carries its profile in the same place. + const prefix = [MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX, MCP_OAUTH_SERVER_CREDENTIAL_PREFIX].find(candidate => + credentialId.startsWith(candidate), + ); + if (prefix === undefined) return undefined; + const separator = credentialId.indexOf(":", prefix.length); + return separator === -1 ? undefined : credentialId.slice(prefix.length, separator); + // scient(SA-CODEX-PLUGINS): end } /** @@ -65,6 +139,13 @@ export function mcpOAuthCredentialProfile(credentialId: string): string | undefi * config — recover the server URL for the RFC 8707 fallback resource on refresh. */ export function mcpOAuthServerUrlFromCredentialId(credentialId: string): string | undefined { + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in: the URL follows its profile and server name. + if (credentialId.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX)) { + const profileEnd = credentialId.indexOf(":", MCP_OAUTH_SERVER_CREDENTIAL_PREFIX.length); + const nameEnd = profileEnd === -1 ? -1 : credentialId.indexOf(":", profileEnd + 1); + return nameEnd === -1 ? undefined : credentialId.slice(nameEnd + 1) || undefined; + } + // scient(SA-CODEX-PLUGINS): end if (credentialId.startsWith(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX)) { const separator = credentialId.indexOf(":", MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length); return separator === -1 ? undefined : credentialId.slice(separator + 1) || undefined; @@ -312,6 +393,10 @@ export interface MCPOAuthConfig { clientId?: string; /** Client secret (optional for PKCE flows) */ clientSecret?: string; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in follows no token-endpoint redirect. + /** Fail the token exchange on a redirect instead of following it with the code, verifier and secret. */ + rejectRedirects?: boolean; + // scient(SA-CODEX-PLUGINS): end /** OAuth scopes (space-separated) */ scopes?: string; /** @@ -535,6 +620,9 @@ export class MCPOAuthFlow extends OAuthCallbackFlow { }, body: params.toString(), signal: this.ctrl.signal, + // scient(SA-CODEX-PLUGINS): begin - as Codex's OAuth client, never forward the exchange elsewhere. + ...(this.config.rejectRedirects && { redirect: "error" as const }), + // scient(SA-CODEX-PLUGINS): end }); if (!response.ok) { @@ -805,6 +893,10 @@ export interface RefreshMCPOAuthTokenOptions { * Preserved advertised resources leave this false/undefined. */ stripSameOriginResource?: boolean; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in follows no token-endpoint redirect. + /** Fail the refresh on a redirect instead of following it with the refresh token and secret. */ + rejectRedirects?: boolean; + // scient(SA-CODEX-PLUGINS): end } /** @@ -819,7 +911,10 @@ export async function refreshMCPOAuthToken( resourceOrOpts?: string | RefreshMCPOAuthTokenOptions, opts?: RefreshMCPOAuthTokenOptions, ): Promise { - const optsFromTrailing = typeof resourceOrOpts === "string" ? opts : resourceOrOpts; + // scient(SA-CODEX-PLUGINS): begin - an undefined resource still reads the trailing options (redirect refusal, + // signal, authorization URL), which keying on a string resource alone dropped. + const optsFromTrailing = typeof resourceOrOpts === "object" ? resourceOrOpts : opts; + // scient(SA-CODEX-PLUGINS): end const fetchImpl: FetchImpl = optsFromTrailing?.fetch ?? fetch; const resource = typeof resourceOrOpts === "string" ? resourceOrOpts : undefined; // Filter against the authorization-server origin when known (RFC 8414 @@ -846,6 +941,9 @@ export async function refreshMCPOAuthToken( headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }, body: params.toString(), signal: optsFromTrailing?.signal, + // scient(SA-CODEX-PLUGINS): begin - as Codex's OAuth client, never forward the refresh elsewhere. + ...(optsFromTrailing?.rejectRedirects && { redirect: "error" as const }), + // scient(SA-CODEX-PLUGINS): end }); if (!response.ok) { diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index f27d74535e..c12ca5b192 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -36,7 +36,14 @@ import { MCPOAuthFlow, type MCPStoredOAuthCredential, mcpOAuthCredentialId, + // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. + isMcpOAuthServerCredentialId, + mcpOAuthServerCredentialId, + // scient(SA-CODEX-PLUGINS): end } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): begin - an imported sign-in a broker could not refresh is not offered. +import { boundSignInNeedsLocalSecret } from "../scient/mcp-sign-in-pickup"; +// scient(SA-CODEX-PLUGINS): end import type { AuthStorage } from "../session/auth-storage"; import type { MCPAuthChallenge, MCPAuthConfig, MCPServerConfig } from "./types"; @@ -316,6 +323,10 @@ export function matchingManagerServerConfig( const managerConfig = manager?.getServerConfig(name); if (!managerConfig || managerConfig.type !== selected.type) return undefined; const urls = [expandEnvVarsDeep(selected.url), selected.url]; + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in matches only the entry holding that sign-in. + const boundIds = [selected.auth?.credentialId, managerConfig.auth?.credentialId]; + if (boundIds.some(isMcpOAuthServerCredentialId) && boundIds[0] !== boundIds[1]) return undefined; + // scient(SA-CODEX-PLUGINS): end return urls.includes(managerConfig.url) ? managerConfig : undefined; } @@ -339,7 +350,11 @@ export async function reconnectMcpManagerServer( if (!manager) return undefined; const managerConfig = matchingManagerServerConfig(manager, name, selected); if (!managerConfig) return undefined; - const pending = manager.updateServerConfig(name, clearMcpClientAuthSurface(managerConfig)); + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in keeps its auth block and headers: + // without them it would reconnect on another server's url-keyed sign-in. + const bound = isMcpOAuthServerCredentialId(managerConfig.auth?.credentialId); + const pending = manager.updateServerConfig(name, bound ? managerConfig : clearMcpClientAuthSurface(managerConfig)); + // scient(SA-CODEX-PLUGINS): end if (pending) await pending; await manager.reconnectServer(name, { manual: true }); return manager.getConnectionStatus(name); @@ -476,6 +491,10 @@ export interface McpOAuthFlowRunOptions extends McpOAuthFlowPresentation { registrationUrl?: string; issuerUrl?: string; resource?: string; + // scient(SA-CODEX-PLUGINS): begin - store the sign-in under a server-bound id. + /** Where to store the credential, instead of the server URL's own row. */ + credentialId?: string; + // scient(SA-CODEX-PLUGINS): end stripSameOriginResource?: boolean; callbackPort?: number; callbackPath?: string; @@ -560,6 +579,9 @@ export async function runMcpOAuthFlow(opts: McpOAuthFlowRunOptions): Promise, + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls (an imported Codex plugin's tool_timeout_sec), + // never past the logical call's deadline. + toolTimeoutCapMs?: number, + deadlineAt?: number, + // scient(SA-CODEX-PLUGINS): end ): MCPRequestDeadline { const configured = envTimeoutMs() ?? configTimeout; if (method !== "tools/call") { const timeoutMs = configured ?? DEFAULT_MCP_TIMEOUT_MS; return { timeoutMs, maxTotalMs: timeoutMs }; } + // scient(SA-CODEX-PLUGINS): begin - Codex keeps tool_timeout_sec as the limit, whatever the call asks for. + if (toolTimeoutCapMs !== undefined) { + let cap = + configured !== undefined && isMCPTimeoutEnabled(configured) + ? Math.min(toolTimeoutCapMs, configured) + : toolTimeoutCapMs; + // The deadline is monotonic; one spent while the request was being prepared still arms a timer (1 ms), as + // 0 would disable it. Callers check it before starting a retry or replay. + if (deadlineAt !== undefined) cap = Math.max(1, Math.floor(Math.min(cap, deadlineAt - performance.now()))); + return { timeoutMs: cap, maxTotalMs: cap }; + } + // scient(SA-CODEX-PLUGINS): end const base = configured ?? DEFAULT_MCP_TOOL_CALL_TIMEOUT_MS; if (!isMCPTimeoutEnabled(base)) return { timeoutMs: base, maxTotalMs: base }; const requested = requestedToolTimeoutMs(params?.arguments); @@ -144,7 +161,10 @@ export function createMCPTimeout( let callerAborted = false; let timeoutId: NodeJS.Timeout | undefined; let cleared = false; - const startedAt = Date.now(); + // scient(SA-CODEX-PLUGINS): begin - progress restarts are measured on the monotonic clock, so setting the + // wall clock back never stretches a hard cap. + const startedAt = performance.now(); + // scient(SA-CODEX-PLUGINS): end function onDeadline(): void { if (callerAborted) return; timerFired = true; @@ -172,7 +192,9 @@ export function createMCPTimeout( }, refresh: () => { if (cleared || timerFired || callerAborted || timeoutId === undefined) return; - const delay = Math.min(timeoutMs, startedAt + maxTotalMs - Date.now()); + // scient(SA-CODEX-PLUGINS): begin - the monotonic clock. + const delay = Math.min(timeoutMs, startedAt + maxTotalMs - performance.now()); + // scient(SA-CODEX-PLUGINS): end if (delay <= 0) return; clearTimeout(timeoutId); timeoutId = setTimeout(onDeadline, delay); diff --git a/packages/coding-agent/src/mcp/tool-bridge.ts b/packages/coding-agent/src/mcp/tool-bridge.ts index 4365e2d796..bc3e0ef1e7 100644 --- a/packages/coding-agent/src/mcp/tool-bridge.ts +++ b/packages/coding-agent/src/mcp/tool-bridge.ts @@ -448,6 +448,15 @@ function getMcpAuthChallenge(result: MCPToolCallResult): MCPAuthChallenge | unde return wwwAuthenticate.length > 0 ? { wwwAuthenticate } : undefined; } +// scient(SA-CODEX-PLUGINS): begin - one deadline for a logical call to a server with a hard tool-call cap +// (an imported Codex plugin's tool_timeout_sec), carried through reconnecting and replaying it. +/** When a logical tool call to this connection's server must end, if its server caps tool calls. */ +function toolCallDeadline(connection: MCPServerConnection): number | undefined { + // Hosts and tests may hand the bridge a connection without its config: no cap then. + const capMs = connection.config?.toolTimeoutMs; + return capMs === undefined ? undefined : performance.now() + capMs; +} + async function callToolWithAuthRetry( connection: MCPServerConnection, toolName: string, @@ -455,14 +464,15 @@ async function callToolWithAuthRetry( reconnect: MCPReconnect | undefined, signal?: AbortSignal, onProgress?: MCPRequestOptions["onProgress"], + deadlineAt?: number, ): Promise { - const result = await callTool(connection, toolName, args, { signal, onProgress }); + const result = await callTool(connection, toolName, args, { signal, onProgress, deadlineAt }); const authChallenge = getMcpAuthChallenge(result); if (!authChallenge || !reconnect) return { connection, result }; let newConnection: MCPServerConnection | null; try { - newConnection = await reconnectWithAbort(reconnect, signal, { authChallenge }); + newConnection = await reconnectWithAbort(reconnect, signal, { authChallenge }, deadlineAt); } catch (error) { rethrowIfAborted(error, signal); return { connection, error }; @@ -472,13 +482,14 @@ async function callToolWithAuthRetry( try { return { connection: newConnection, - result: await callTool(newConnection, toolName, args, { signal, onProgress }), + result: await callTool(newConnection, toolName, args, { signal, onProgress, deadlineAt }), }; } catch (error) { rethrowIfAborted(error, signal); return { connection: newConnection, error }; } } +// scient(SA-CODEX-PLUGINS): end /** Re-throw abort-related errors so they bypass error-result handling. */ function rethrowIfAborted(error: unknown, signal?: AbortSignal): void { @@ -487,18 +498,35 @@ function rethrowIfAborted(error: unknown, signal?: AbortSignal): void { if (signal?.aborted) throw new ToolAbortError(); } +// scient(SA-CODEX-PLUGINS): begin - a reconnect is waited on no longer than the logical call's deadline; +// past it the call ends with its own error. async function reconnectWithAbort( reconnect: MCPReconnect, signal?: AbortSignal, options?: { authChallenge?: MCPAuthChallenge }, + deadlineAt?: number, ): Promise { try { - return await untilAborted(signal, () => reconnect(options)); + // A spent deadline starts no reconnect. + if (deadlineAt !== undefined && performance.now() >= deadlineAt) return null; + const pending = untilAborted(signal, () => reconnect(options)); + if (deadlineAt === undefined) return await pending; + void pending.catch(() => {}); + const { promise: expired, resolve } = Promise.withResolvers(); + const timer = setTimeout(() => resolve(null), Math.max(0, deadlineAt - performance.now())); + try { + const connection = await Promise.race([pending, expired]); + // A reconnect that lands at or past the deadline gets no replay. + return performance.now() < deadlineAt ? connection : null; + } finally { + clearTimeout(timer); + } } catch (error) { rethrowIfAborted(error, signal); return null; } } +// scient(SA-CODEX-PLUGINS): end /** * Create a unique tool name for an MCP tool. @@ -861,6 +889,9 @@ export class MCPTool implements CustomTool { const progress = progressUpdater(onUpdate, this.connection.name, this.tool.name, provider, providerName); const onProgress = progress?.onProgress; + // scient(SA-CODEX-PLUGINS): begin - one deadline for this call, its auth retry, reconnect and replay. + const deadlineAt = toolCallDeadline(this.connection); + // scient(SA-CODEX-PLUGINS): end try { const attempt = await callToolWithAuthRetry( this.connection, @@ -869,6 +900,9 @@ export class MCPTool implements CustomTool { this.reconnect, signal, onProgress, + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + deadlineAt, + // scient(SA-CODEX-PLUGINS): end ); if (attempt.error !== undefined) { return buildErrorResult(attempt.error, this.connection.name, this.tool.name, provider, providerName); @@ -893,14 +927,18 @@ export class MCPTool implements CustomTool { } catch (error) { rethrowIfAborted(error, signal); if (this.reconnect && isRetriableConnectionError(error)) { - const newConn = await reconnectWithAbort(this.reconnect, signal); + // scient(SA-CODEX-PLUGINS): begin - the reconnect and the replay get only what is left. + const newConn = await reconnectWithAbort(this.reconnect, signal, undefined, deadlineAt); + // scient(SA-CODEX-PLUGINS): end if (newConn) { // Rebind so subsequent calls on this instance use the fresh connection this.connection = newConn; const retryProvider = newConn._source?.provider ?? provider; const retryProviderName = newConn._source?.providerName ?? providerName; try { - const result = await callTool(newConn, this.tool.name, args, { signal, onProgress }); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + const result = await callTool(newConn, this.tool.name, args, { signal, onProgress, deadlineAt }); + // scient(SA-CODEX-PLUGINS): end return buildResult(result, newConn.name, this.tool.name, retryProvider, retryProviderName); } catch (retryError) { rethrowIfAborted(retryError, signal); @@ -1026,6 +1064,9 @@ export class DeferredMCPTool implements CustomTool { try { const connection = await untilAborted(signal, () => this.getConnection()); throwIfAborted(signal); + // scient(SA-CODEX-PLUGINS): begin - one deadline for this call, its auth retry, reconnect and replay. + const deadlineAt = toolCallDeadline(connection); + // scient(SA-CODEX-PLUGINS): end try { const attempt = await callToolWithAuthRetry( connection, @@ -1034,6 +1075,9 @@ export class DeferredMCPTool implements CustomTool { this.reconnect, signal, onProgress, + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + deadlineAt, + // scient(SA-CODEX-PLUGINS): end ); if (attempt.error !== undefined) { return buildErrorResult( @@ -1063,12 +1107,16 @@ export class DeferredMCPTool implements CustomTool { } catch (callError) { rethrowIfAborted(callError, signal); if (this.reconnect && isRetriableConnectionError(callError)) { - const newConn = await reconnectWithAbort(this.reconnect, signal); + // scient(SA-CODEX-PLUGINS): begin - the reconnect and the replay get only what is left. + const newConn = await reconnectWithAbort(this.reconnect, signal, undefined, deadlineAt); + // scient(SA-CODEX-PLUGINS): end if (newConn) { const retryProvider = newConn._source?.provider ?? provider; const retryProviderName = newConn._source?.providerName ?? providerName; try { - const result = await callTool(newConn, this.tool.name, args, { signal, onProgress }); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + const result = await callTool(newConn, this.tool.name, args, { signal, onProgress, deadlineAt }); + // scient(SA-CODEX-PLUGINS): end return buildResult(result, this.serverName, this.tool.name, retryProvider, retryProviderName); } catch (retryError) { rethrowIfAborted(retryError, signal); diff --git a/packages/coding-agent/src/mcp/transports/http.ts b/packages/coding-agent/src/mcp/transports/http.ts index e64c4cbe16..62d748ca33 100644 --- a/packages/coding-agent/src/mcp/transports/http.ts +++ b/packages/coding-agent/src/mcp/transports/http.ts @@ -52,6 +52,17 @@ interface SSEResumeState { */ class SSEResumeError extends Error {} +// scient(SA-CODEX-PLUGINS): begin - a tool call that ran out its hard cap. +function toolTimeoutError(capMs: number): MCPTransportError { + return new MCPTransportError({ + transport: "http", + stage: "send", + failure: "timeout", + message: `Request timeout after ${capMs}ms`, + retryable: false, + }); +} +// scient(SA-CODEX-PLUGINS): end /** Wait for the server-provided SSE retry interval while remaining abortable. */ async function waitForSSERetry(ms: number, signal: AbortSignal): Promise { if (signal.aborted) throw signal.reason; @@ -411,8 +422,19 @@ export class HttpTransport implements MCPTransport { params: Record | undefined, options: MCPRequestOptions | undefined, ): Promise { + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap spans the first attempt, the sign-in refresh + // after a 401 or 403 and the retry, within any deadline the caller already set. + const capMs = method === "tools/call" ? this.config.toolTimeoutMs : undefined; + const deadlineAt = + capMs === undefined + ? undefined + : Math.min(options?.deadlineAt ?? Number.POSITIVE_INFINITY, performance.now() + capMs); + const callOptions = deadlineAt === undefined ? options : { ...options, deadlineAt }; + // scient(SA-CODEX-PLUGINS): end try { - return await this.#executeRequest(method, params, options); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + return await this.#executeRequest(method, params, callOptions); + // scient(SA-CODEX-PLUGINS): end } catch (error) { // Retry once on auth failure if onAuthError is wired. Never replay // after an SSE resume failure: the server already accepted the @@ -420,17 +442,47 @@ export class HttpTransport implements MCPTransport { // state-changing tool twice. const status = error instanceof Error ? AIError.status(error) : undefined; if (!(error instanceof SSEResumeError) && this.onAuthError && (status === 401 || status === 403)) { - const newHeaders = await this.onAuthError(); + // scient(SA-CODEX-PLUGINS): begin - the refresh and the retry get only what is left of the cap. + const newHeaders = + capMs === undefined || deadlineAt === undefined + ? await this.onAuthError() + : await this.#beforeToolDeadline( + () => this.onAuthError?.() ?? Promise.resolve(null), + capMs, + deadlineAt, + ); if (newHeaders) { // Persist refreshed headers so subsequent requests use them directly this.config = { ...this.config, headers: newHeaders }; - return this.#executeRequest(method, params, options); + // A deadline spent during the refresh sends no retry. + if (capMs !== undefined && deadlineAt !== undefined && performance.now() >= deadlineAt) { + throw toolTimeoutError(capMs); + } + return this.#executeRequest(method, params, callOptions); } + // scient(SA-CODEX-PLUGINS): end } throw error; } } + // scient(SA-CODEX-PLUGINS): begin - work a capped tool call waits on, started only while time is left and + // ended by the call's monotonic deadline. + async #beforeToolDeadline(start: () => Promise, capMs: number, deadlineAt: number): Promise { + const leftMs = deadlineAt - performance.now(); + if (leftMs <= 0) throw toolTimeoutError(capMs); + const work = start(); + void work.catch(() => {}); + const operation = createMCPTimeout(leftMs); + try { + return await untilAborted(operation.signal, work); + } catch (error) { + throw operation.timedOut() ? toolTimeoutError(capMs) : error; + } finally { + operation.clear(); + } + } + // scient(SA-CODEX-PLUGINS): end async #executeRequest( method: string, params: Record | undefined, @@ -478,7 +530,15 @@ export class HttpTransport implements MCPTransport { } callerSignal = controller.signal; } - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.config.timeout, method, params); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap, never past the logical call's deadline. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.config.timeout, + method, + params, + this.config.toolTimeoutMs, + options?.deadlineAt, + ); + // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(callerSignal), maxTotalMs); let stage: MCPFailureStage = "send"; let traceId: string | undefined; diff --git a/packages/coding-agent/src/mcp/transports/sse.ts b/packages/coding-agent/src/mcp/transports/sse.ts index cab598676f..e28be1601b 100644 --- a/packages/coding-agent/src/mcp/transports/sse.ts +++ b/packages/coding-agent/src/mcp/transports/sse.ts @@ -260,7 +260,15 @@ export class LegacySseTransport implements MCPTransport { method, params: options?.onProgress ? withProgressToken(params, id) : (params ?? {}), }; - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.#config.timeout, method, params); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.#config.timeout, + method, + params, + this.#config.toolTimeoutMs, + options?.deadlineAt, + ); + // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(options?.signal), maxTotalMs); const progressSink = options?.onProgress; const deferred = Promise.withResolvers(); diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index b96a905696..54af24028a 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -592,10 +592,15 @@ export class StdioTransport implements MCPTransport { signal?.throwIfAborted(); if (this.#connected) return; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + const inherited: Record = this.config.envInherit + ? Object.fromEntries(this.config.envInherit.map(name => [name, Bun.env[name]]).filter(([, value]) => value)) + : Bun.env; const env = { - ...Bun.env, + ...inherited, ...this.config.env, }; + // scient(SA-CODEX-PLUGINS): end const cwd = this.config.cwd ?? getProjectDir(); const spawnCommand = await resolveStdioSpawnCommand(this.config, { cwd, @@ -819,8 +824,17 @@ export class StdioTransport implements MCPTransport { params: options?.onProgress ? withProgressToken(params, id) : (params ?? {}), }; - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.config.timeout, method, params); - const startedAt = Date.now(); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.config.timeout, + method, + params, + this.config.toolTimeoutMs, + options?.deadlineAt, + ); + // The monotonic clock, so setting the wall clock back never stretches a hard cap. + const startedAt = performance.now(); + // scient(SA-CODEX-PLUGINS): end const signal = options?.signal; if (signal?.aborted) { @@ -887,7 +901,9 @@ export class StdioTransport implements MCPTransport { // Progress restarts the deadline, never past the request's hard cap. const armTimer = (): void => { - const delay = Math.min(timeout, startedAt + maxTotalMs - Date.now()); + // scient(SA-CODEX-PLUGINS): begin - the monotonic clock. + const delay = Math.min(timeout, startedAt + maxTotalMs - performance.now()); + // scient(SA-CODEX-PLUGINS): end if (delay <= 0) return; const limit = delay < timeout ? maxTotalMs : timeout; if (timer) clearTimeout(timer); @@ -908,6 +924,25 @@ export class StdioTransport implements MCPTransport { }, delay); }; if (isMCPTimeoutEnabled(timeout)) armTimer(); + // scient(SA-CODEX-PLUGINS): begin - a capped tool call whose budget ran out while it was being prepared is + // not written: no timer would be left to end it. + if ( + method === "tools/call" && + this.config.toolTimeoutMs !== undefined && + performance.now() >= startedAt + maxTotalMs + ) { + settle( + new MCPTransportError({ + transport: "stdio", + stage: "send", + failure: "timeout", + message: `Request timeout after ${maxTotalMs}ms`, + retryable: false, + }), + ); + return promise; + } + // scient(SA-CODEX-PLUGINS): end if (signal) { signal.addEventListener("abort", onAbort, { once: true }); diff --git a/packages/coding-agent/src/mcp/types.ts b/packages/coding-agent/src/mcp/types.ts index 8252c3a9f3..40db436b62 100644 --- a/packages/coding-agent/src/mcp/types.ts +++ b/packages/coding-agent/src/mcp/types.ts @@ -76,6 +76,13 @@ interface MCPServerConfigBase { enabled?: boolean; /** MCP request timeout in milliseconds (default: 30000, 0 to disable) */ timeout?: number; + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls. + /** + * Hard limit on a `tools/call`, in milliseconds, which a call's own wait arguments never extend + * (an imported Codex plugin's `tool_timeout_sec`). + */ + toolTimeoutMs?: number; + // scient(SA-CODEX-PLUGINS): end /** * Encoding for outgoing JSON-RPC request ids (default: `"number"`). * @@ -124,6 +131,10 @@ export interface MCPStdioServerConfig extends MCPServerConfigBase { envPolicy?: "literal"; /** Env keys whose values are final package data; auth resolution keeps them verbatim. */ envLiteralKeys?: string[]; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + /** When set, only these variables are inherited from the agent's environment; `env` still applies on top. */ + envInherit?: string[]; + // scient(SA-CODEX-PLUGINS): end cwd?: string; } @@ -305,6 +316,13 @@ export interface MCPRequestOptions { * `notifications/progress` while pending; each one restarts a default tool-call deadline (never a configured one). */ onProgress?: (progress: MCPProgress) => void; + // scient(SA-CODEX-PLUGINS): begin - one deadline for a logical tool call across reconnects and retries. + /** + * Monotonic time (`performance.now()` milliseconds) a capped `tools/call` (a server's `toolTimeoutMs`) + * must end by, so a retry or a replay after reconnecting gets only what is left of the cap. + */ + deadlineAt?: number; + // scient(SA-CODEX-PLUGINS): end } /** Transport interface - abstracts stdio/http */ diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 7f8e0e71b6..2aa5c48539 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -48,6 +48,9 @@ import { removeManagedMcpOAuthCredentials, } from "../../mcp/oauth-credentials"; import type { MCPAuthChallenge, MCPConfigFile, MCPServerConfig, MCPServerConnection } from "../../mcp/types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId, withTrustedMcpAuth } from "../../mcp/oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import { shortenPath } from "@oh-my-pi/pi-tui/render/render-utils"; import { urlHyperlinkAlways } from "@oh-my-pi/pi-tui/render"; import { copyToClipboard } from "../../utils/clipboard"; @@ -902,10 +905,12 @@ export class MCPCommandController { } } - async #syncManagerConnection(name: string, config: MCPServerConfig): Promise { + // scient(SA-CODEX-PLUGINS): begin - the definition connects with its own source, if any. + async #syncManagerConnection(name: string, config: MCPServerConfig, source?: SourceMeta): Promise { if (!this.ctx.mcpManager) return; if (this.ctx.mcpManager.getConnectionStatus(name) !== "disconnected") return; - await this.ctx.mcpManager.connectServers({ [name]: config }, {}); + await this.ctx.mcpManager.connectServers({ [name]: config }, source ? { [name]: source } : {}); + // scient(SA-CODEX-PLUGINS): end if (this.ctx.mcpManager.getConnectionStatus(name) === "connected") { await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools()); } @@ -1304,13 +1309,18 @@ export class MCPCommandController { // Resolve auth config if needed let resolvedConfig: MCPServerConfig; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it: the + // source is the selected definition's own, and a config-file definition has none that binds. + const testSource = found.discovered ? this.ctx.mcpManager?.getSource(name) : undefined; + const testConfig = withTrustedMcpAuth(name, config, testSource?.provider); if (this.ctx.mcpManager) { - resolvedConfig = await this.ctx.mcpManager.prepareConfig(config); + resolvedConfig = await this.ctx.mcpManager.prepareConfig(testConfig); } else { const tempManager = new MCPManager(getProjectDir()); tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage); - resolvedConfig = await tempManager.prepareConfig(config); + resolvedConfig = await tempManager.prepareConfig(testConfig); } + // scient(SA-CODEX-PLUGINS): end // Create temporary connection connection = await connectToServer(name, resolvedConfig, { signal: abortController.signal }); @@ -1336,7 +1346,9 @@ export class MCPCommandController { } lines.push(""); - await this.#syncManagerConnection(name, config); + // scient(SA-CODEX-PLUGINS): begin - the tested definition, with the source it was tested under. + await this.#syncManagerConnection(name, testConfig, testSource); + // scient(SA-CODEX-PLUGINS): end this.#showMessage(lines.join("\n")); } catch (error) { if (abortController.signal.aborted || (error instanceof Error && error.name === "AbortError")) { @@ -1405,7 +1417,12 @@ export class MCPCommandController { const found = await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), }); - if (!found) { + // scient(SA-CODEX-PLUGINS): begin - an imported Codex plugin server lives in no config file: its + // toggle is the disabledServers list, as for any third-party discovered server. + const imported = + found?.discovered === true && this.ctx.mcpManager?.getSource(name)?.provider === "codex-plugins"; + if (!found || imported) { + // scient(SA-CODEX-PLUGINS): end // Check if this is a discovered server from a third-party config const userConfigPath = getMCPConfigPath("user", getProjectDir()); const disabledServers = new Set(await readDisabledServers(userConfigPath)); @@ -1516,6 +1533,24 @@ export class MCPCommandController { const currentAuth = found.config.auth; const authStorage = this.ctx.session.modelRegistry.authStorage; + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in signs out of that one alone: no + // other server's url-keyed sign-in goes with it, and its definition lives in no config file to rewrite. + if (isMcpOAuthServerCredentialId(currentAuth?.credentialId)) { + // Only a discovered server holds its own sign-in, under the id its name and URL give. + const own = + found.discovered && + (found.config.type === "http" || found.config.type === "sse") && + currentAuth?.credentialId === mcpOAuthServerCredentialId(name, found.config.url); + if (!own) { + this.ctx.showError(`Server "${name}" names a sign-in that belongs to another server.`); + return; + } + await removeManagedMcpOAuthCredential(authStorage, currentAuth?.credentialId); + await this.reloadServers(); + this.#showMessage(["", theme.fg("success", `- Cleared auth for "${name}"`), ""].join("\n")); + return; + } + // scient(SA-CODEX-PLUGINS): end if (currentAuth?.type === "oauth") { await removeManagedMcpOAuthCredential(authStorage, currentAuth.credentialId); } @@ -1572,9 +1607,23 @@ export class MCPCommandController { } try { - const found = await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { - enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), - }); + // scient(SA-CODEX-PLUGINS): begin - a challenge from a live imported server is answered for that live + // definition, never a same-named config-file entry. + const live = + options.authChallenge !== undefined && this.ctx.mcpManager?.getSource(name)?.provider === "codex-plugins" + ? this.ctx.mcpManager.getServerConfig(name) + : undefined; + const found = live + ? { + filePath: getMCPConfigPath("user", getProjectDir()), + scope: "user" as const, + config: live, + discovered: true, + } + : await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { + enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), + }); + // scient(SA-CODEX-PLUGINS): end if (!found) { if (!options.silent) this.ctx.showError(`Server "${name}" not found.`); return; diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.ts index 33a118977a..7557c6a94e 100644 --- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts +++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts @@ -109,6 +109,9 @@ import { RpcSessionEventForwarder } from "./rpc-session-events"; import { isSessionSettled } from "../../session/session-settle"; import { RpcSessionSettleWatcher, watchedScheduledTurnProbe } from "./rpc-session-settle"; import { RpcSubagentRegistry, readRpcSubagentTranscript, resolveOwnedLiveSubagent } from "./rpc-subagents"; +// scient(SA-CODEX-PLUGINS): begin - pick up MCP sign-ins made since the last prompt. +import { McpSignInPickup } from "../../scient/mcp-sign-in-pickup"; +// scient(SA-CODEX-PLUGINS): end import type { RpcAbortAndRestoreQueueResult, RpcCommand, @@ -1949,6 +1952,15 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions await emitAvailableCommandsUpdate(); const inputGate = new RpcUserInputGate(); + // scient(SA-CODEX-PLUGINS): begin - pick up MCP sign-ins made elsewhere, starting from this session's own. + const mcpManager = options.mcpManager; + const mcpSignInPickup = mcpManager + ? new McpSignInPickup(mcpManager, session.modelRegistry.authStorage, () => + session.refreshMCPTools(mcpManager.getTools()), + ) + : undefined; + mcpSignInPickup?.seed(); + // scient(SA-CODEX-PLUGINS): end type OrderedUserInput = Extract; type OrderedInputOutcome = "local" | "cancelled" | "admitted" | "builtin-agent"; const dispatchOrderedUserInput = ( @@ -1980,6 +1992,12 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions await session.followUp(text, images); return "admitted"; } + // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one, skills included. + if (mcpSignInPickup) { + await mcpSignInPickup.beforePrompt(); + if (!isCurrent()) return "cancelled"; + } + // scient(SA-CODEX-PLUGINS): end if (command.type === "prompt") { if (!ticket) return "cancelled"; const skillResult = await dispatchRpcSkillPrompt({ diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts new file mode 100644 index 0000000000..6a5c3b2362 --- /dev/null +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -0,0 +1,1357 @@ +/** + * Installed Codex plugins (SA-CODEX-PLUGINS). + * + * Codex lists plugins in `$CODEX_HOME/config.toml` (default `~/.codex`) as + * `[plugins."@"]`, enabled unless the table, or else `[plugins._default]`, + * says `enabled = false`, and runs each from the active version of + * `$CODEX_HOME/plugins/cache////`. Their skills and MCP servers load + * here under Codex's own rules: its manifest precedence, its MCP field validation, and the + * environment and header handling it gives a plugin's servers. App references (`.app.json`) do + * not: a person's ChatGPT apps reach the agent through the ChatGPT sign-in (SA-CHATGPT-APPS). + * Plugins and servers that run only inside OpenAI's own apps are left out, and so are servers + * whose tools Codex narrows or asks about. Nothing loads when an organization manages Codex, since + * its policy cannot be applied here, or when this run cannot tell its launch environment apart. + */ +import type { Dirent, Stats } from "node:fs"; +import * as fs from "node:fs/promises"; +import * as os from "node:os"; +import * as path from "node:path"; +import { filterProcessEnv, getLaunchEnv, getPluginsDir, isRecord, logger, parseFrontmatter } from "@oh-my-pi/pi-utils"; +import { isUserSourceEnabled, registerProvider } from "../capability"; +import { readDirEntries, readFile } from "../capability/fs"; +import { type MCPServer, mcpCapability } from "../capability/mcp"; +import { type Skill, type SkillFrontmatter, skillCapability } from "../capability/skill"; +import type { LoadContext, LoadResult } from "../capability/types"; +import { classifyAgentPluginRoot, parseAgentPluginMcp } from "../discovery/agent-plugin-format"; +import { isContainedResolved, realpathIfExists, resolveContainedPath } from "../discovery/contained-path"; +import { compareSkillOrder, createSourceMeta } from "../discovery/helpers"; +import { mcpOAuthServerCredentialId } from "../mcp/oauth-flow"; + +const PROVIDER_ID = "codex-plugins"; +const DISPLAY_NAME = "Codex Plugins"; +/** Below the `codex` provider (70), so a person's own `~/.codex` skills and servers win. */ +const PRIORITY = 65; + +/** The folder of a plugin Codex links from its source while it is developed; it wins over versions. */ +const LOCAL_VERSION = "local"; + +/** + * Codex's legacy manifests, in its order of precedence after a root Agent Plugins `plugin.json`. + * The first one present decides; a link or non-file there, or a linked folder holding it, stops + * the search, as in Codex. + */ +const LEGACY_MANIFESTS = [".codex-plugin/plugin.json", ".claude-plugin/plugin.json", ".cursor-plugin/plugin.json"]; + +/** + * Bundled plugins that drive OpenAI's apps (the in-app browser, the Chrome extension host, + * computer use, the Codex app's own threads) through services only those apps provide. Their + * skills drive those services too. + */ +const HOST_PLUGINS: Record = { + "browser@openai-bundled": true, + "chrome@openai-bundled": true, + "codex-app-tools@openai-bundled": true, + "computer-use@openai-bundled": true, + "unified-computer-use@openai-bundled": true, +}; + +/** OpenAI's launcher for its signed Computer Use client, which serves only Codex (the Messages plugin). */ +const HOST_LAUNCHER = "computer-use-client-launcher"; + +/** Codex's tool-call limit when a server sets no tool_timeout_sec (codex-mcp DEFAULT_TOOL_TIMEOUT). */ +const CODEX_DEFAULT_TOOL_TIMEOUT_MS = 300_000; + +/** The server names Codex starts (codex-mcp validate_mcp_server_name). */ +const CODEX_SERVER_NAME = /^[a-zA-Z0-9_:@/.-]+$/; + +const IS_WINDOWS = process.platform === "win32"; + +/** + * The variables Codex passes a plugin's local stdio server from its own environment, besides + * those the server names in `env_vars` (codex-rs/rmcp-client `DEFAULT_ENV_VARS`, and + * `WINDOWS_CORE_ENV_VARS` on Windows). Nothing else, API keys included, reaches the server. + */ +const INHERITED_ENV = IS_WINDOWS + ? [ + "PATH", + "PATHEXT", + "SHELL", + "COMSPEC", + "SYSTEMROOT", + "WINDIR", + "SYSTEMDRIVE", + "USERNAME", + "USERDOMAIN", + "USERPROFILE", + "HOMEDRIVE", + "HOMEPATH", + "PROGRAMFILES", + "PROGRAMFILES(X86)", + "PROGRAMW6432", + "PROGRAMDATA", + "LOCALAPPDATA", + "APPDATA", + "TEMP", + "TMP", + "TMPDIR", + "POWERSHELL", + "PWSH", + ] + : ["HOME", "LOGNAME", "PATH", "SHELL", "USER", "__CF_USER_TEXT_ENCODING", "LANG", "LC_ALL", "TERM", "TMPDIR", "TZ"]; +/** Certificate bundles Codex always passes on, as absolute paths (`CUSTOM_CA_ENV_KEYS`). */ +const CA_BUNDLE_ENV = [ + "CODEX_CA_CERTIFICATE", + "SSL_CERT_FILE", + "REQUESTS_CA_BUNDLE", + "CURL_CA_BUNDLE", + "NODE_EXTRA_CA_CERTS", + "GIT_SSL_CAINFO", + "CARGO_HTTP_CAINFO", + "PIP_CERT", + "BUNDLE_SSL_CA_CERT", + "npm_config_cafile", + "NPM_CONFIG_CAFILE", +]; +/** + * Names Codex never lets a model-reachable process have (`NON_INHERITABLE_ENV_VARS`), even from + * `env`, compared without case on every platform, as Codex compares them. + */ +const NON_INHERITABLE_ENV = [ + "CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN", + "NODE_REPL_AUTH_TOKEN", + "CODEX_GUARDIAN_DECISIONS_API_KEY", + "OPENAI_FEDERATION_RULE_ID", + "OPENAI_IDENTITY_TOKEN_FILE", + "OPENAI_IDENTITY_TOKEN_SOURCE", + "OPENAI_SPIFFE_AUDIENCE", + "OPENAI_SPIFFE_ID", + "OPENAI_WORKLOAD_IDENTITY_CONTEXT", +]; + +/** Fields Codex rejects on a stdio server, and on a streamable HTTP server. */ +const NOT_STDIO = [ + "url", + "bearer_token_env_var", + "bearer_token", + "http_headers_helper", + "http_headers", + "env_http_headers", + "oauth", + "oauth_resource", + "auth", +]; +const NOT_HTTP = ["args", "env", "env_vars", "cwd", "bearer_token"]; +/** Header names and values the `http` crate Codex uses accepts: an RFC 9110 token, and tab or visible ASCII. */ +const HTTP_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const HTTP_HEADER_VALUE = /^[\t\x20-\x7e]*$/; + +/** Codex's skill scan limits for a plugin's skill folder (codex-rs/ext/skills loader). */ +const MAX_SKILL_SCAN_DEPTH = 6; +const MAX_SKILL_DIRS = 2000; + +const SEMVER = + /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/; + +interface CodexPlugin { + /** `@`, the plugin's key in config.toml. */ + id: string; + name: string; + marketplace: string; + /** The active version's folder, symlinks resolved; every package path stays inside it. */ + root: string; + /** `standard`: a root Agent Plugins `plugin.json`, read under that standard; `legacy`: a Codex-style manifest. */ + format: "standard" | "legacy"; + manifestPath: string; + manifest: Record; + /** The plugin's `[plugins.""]` table. */ + config: Record; +} + +/** A user `[[skills.config]]` entry Codex applies to loaded skills, its path canonical. */ +interface SkillRule { + selector: { path: string } | { name: string }; + enabled: boolean; +} + +interface PluginList { + plugins: CodexPlugin[]; + warnings: string[]; + /** Scient Agent's launch environment, without any `.env` file's values. */ + env: Record; + /** The user config's skill rules, in Codex's order. */ + skillRules: SkillRule[]; + /** Servers the config names in `[mcp_servers]`, which Codex runs instead of a plugin's same-named one. */ + configuredServers: ReadonlySet; +} + +/** Codex's rule for a cache path segment (plugin, marketplace or version name). */ +function isPathSegment(value: string): boolean { + return value !== "." && value !== ".." && /^[A-Za-z0-9._+-]+$/.test(value); +} + +/** Codex's version order: semantic versions by precedence, anything else by its characters. */ +function compareVersions(a: string, b: string): number { + if (SEMVER.test(a) && SEMVER.test(b)) { + const order = Bun.semver.order(a, b); + if (order !== 0) return order; + } + return a < b ? -1 : a > b ? 1 : 0; +} + +/** The version folder Codex runs: `local` while present, else the highest version; null when not installed. */ +async function activeVersion(base: string): Promise { + const versions = (await readDirEntries(base)) + .filter((entry: Dirent) => entry.isDirectory() && isPathSegment(entry.name)) + .map(entry => entry.name); + if (versions.includes(LOCAL_VERSION)) return LOCAL_VERSION; + return versions.sort(compareVersions).at(-1) ?? null; +} + +async function lstatOrNull(file: string): Promise { + try { + return await fs.lstat(file); + } catch { + return null; + } +} + +/** Environment names compare case-insensitively on Windows, as Codex compares them there. */ +function sameEnvName(left: string, right: string): boolean { + return IS_WINDOWS ? left.toUpperCase() === right.toUpperCase() : left === right; +} + +function envValue(env: Record, name: string): string | undefined { + if (!IS_WINDOWS) return env[name]; + const key = Object.keys(env).find(candidate => sameEnvName(candidate, name)); + return key === undefined ? undefined : env[key]; +} + +/** + * The environment Codex gives a plugin's local stdio server: its variable list and the server's + * `env_vars` from the launch environment, certificate bundles as absolute paths, then the + * server's own `env`, never the names Codex withholds. The server inherits nothing else. + */ +function stdioEnv( + env: Record, + forwarded: readonly string[], + declared: Record | undefined, +): Record { + const result: Record = {}; + const put = (name: string, value: string) => { + for (const key of Object.keys(result)) if (sameEnvName(key, name)) delete result[key]; + result[name] = value; + }; + for (const name of [...INHERITED_ENV, ...forwarded]) { + const value = envValue(env, name); + if (value !== undefined) put(name, value); + } + for (const name of CA_BUNDLE_ENV) { + const value = envValue(env, name); + if (value) put(name, path.resolve(value)); + } + for (const [name, value] of Object.entries(declared ?? {})) put(name, value); + for (const key of Object.keys(result)) { + if (NON_INHERITABLE_ENV.some(name => name.toUpperCase() === key.toUpperCase())) delete result[key]; + } + return result; +} + +/** The first legacy manifest present, read; null (with a warning) when none is usable. */ +async function readLegacyManifest( + root: string, + id: string, + warnings: string[], + manifests: readonly string[] = LEGACY_MANIFESTS, +): Promise<{ manifestPath: string; manifest: Record } | null> { + for (const relative of manifests) { + const manifestPath = path.join(root, relative); + const folder = await lstatOrNull(path.dirname(manifestPath)); + if (folder === null) continue; + if (!folder.isDirectory()) { + warnings.push(`[codex-plugins] Ignoring ${id}: ${path.dirname(relative)} is not a folder`); + return null; + } + const stat = await lstatOrNull(manifestPath); + if (stat === null) continue; + if (!stat.isFile()) { + warnings.push(`[codex-plugins] Ignoring ${id}: ${relative} is not a regular file`); + return null; + } + let manifest: unknown = null; + try { + manifest = JSON.parse((await readFile(manifestPath)) ?? ""); + } catch {} + const mistyped = isRecord(manifest) ? manifestTypeError(manifest) : "the file"; + if (mistyped !== undefined || !isRecord(manifest)) { + warnings.push(`[codex-plugins] Invalid plugin.json for ${id} (${mistyped}): ${manifestPath}`); + return null; + } + return { manifestPath, manifest }; + } + warnings.push(`[codex-plugins] No plugin.json for ${id} in ${root}`); + return null; +} + +async function readPlugin( + home: string, + id: string, + config: Record, + warnings: string[], +): Promise { + const at = id.lastIndexOf("@"); + const name = id.slice(0, at); + const marketplace = id.slice(at + 1); + if (at <= 0 || !isPathSegment(name) || !isPathSegment(marketplace)) { + warnings.push(`[codex-plugins] Ignoring invalid plugin id: ${id}`); + return null; + } + const base = path.join(home, "plugins", "cache", marketplace, name); + const version = await activeVersion(base); + // Listed but not installed (or removed since): Codex skips it as well. + if (version === null) return null; + const root = await realpathIfExists(path.join(base, version)); + if (root === null) return null; + // A root `plugin.json` that targets Agent Plugins wins; one that does not leaves the legacy manifests. + const rootManifest = path.join(root, "plugin.json"); + const rootStat = await lstatOrNull(rootManifest); + if (rootStat !== null && !rootStat.isFile()) { + warnings.push(`[codex-plugins] Ignoring ${id}: plugin.json is not a regular file`); + return null; + } + const standard = await classifyAgentPluginRoot(root); + if (standard.kind === "invalid") { + warnings.push(`[codex-plugins] Ignoring ${id}: ${standard.reason}`); + return null; + } + if (standard.kind === "standard") { + warnings.push(...standard.warnings.map(warning => `[codex-plugins] ${id}: ${warning}`)); + return { + id, + name, + marketplace, + root, + format: "standard", + manifestPath: rootManifest, + manifest: { ...standard.manifest }, + config, + }; + } + const legacy = await readLegacyManifest(root, id, warnings); + if (legacy === null) return null; + return { id, name, marketplace, root, format: "legacy", ...legacy, config }; +} + +async function readToml(file: string, warnings: string[]): Promise | null> { + const raw = await readFile(file); + if (raw === null) return null; + try { + const parsed = Bun.TOML.parse(raw); + return isRecord(parsed) ? parsed : null; + } catch (error) { + warnings.push(`[codex-plugins] Invalid TOML in ${file}: ${String(error)}`); + return null; + } +} + +/** Codex's layer merge: tables merge key by key at every depth, and the higher layer's other values win. */ +function mergeLayers(lower: Record, higher: Record): Record { + const merged: Record = { ...lower }; + for (const [key, value] of Object.entries(higher)) { + const below = merged[key]; + merged[key] = isRecord(below) && isRecord(value) ? mergeLayers(below, value) : value; + } + return merged; +} + +interface WindowsPolicyProbe { + /** The ProgramData known folder. */ + programData: string; + /** The Group Policy `RequirementsPath` value, when set. */ + requirementsPath: string | null; +} + +let windowsProbe: WindowsPolicyProbe | null | undefined; + +/** + * What Codex reads from Windows itself to find its policy: the ProgramData known folder (not the + * `ProgramData` variable anyone can set) and the 64-bit `HKLM\SOFTWARE\Policies\OpenAI\Codex` + * `RequirementsPath`, through .NET's calls to the same system APIs; null when they cannot be read. + */ +function probeWindowsPolicy(): WindowsPolicyProbe | null { + if (windowsProbe !== undefined) return windowsProbe; + // Any error stops the script with no output, so only a lookup that succeeded reports no policy. + const script = [ + "$ErrorActionPreference = 'Stop'", + "try { $key = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry64').OpenSubKey('SOFTWARE\\Policies\\OpenAI\\Codex')", + "$path = if ($key) { $key.GetValue('RequirementsPath') } else { $null }", + "$data = [Environment]::GetFolderPath('CommonApplicationData')", + "[Console]::Out.Write((@{ ok = $true; programData = $data; requirementsPath = $path } | ConvertTo-Json -Compress)) } catch { exit 1 }", + ].join("; "); + let probe: unknown = null; + try { + const query = Bun.spawnSync(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script], { + stdout: "pipe", + stderr: "ignore", + }); + probe = query.exitCode === 0 ? JSON.parse(query.stdout.toString()) : null; + } catch {} + windowsProbe = + isRecord(probe) && + probe.ok === true && + typeof probe.programData === "string" && + path.win32.isAbsolute(probe.programData) && + (probe.requirementsPath == null || typeof probe.requirementsPath === "string") + ? { programData: probe.programData, requirementsPath: (probe.requirementsPath as string | null) ?? null } + : null; + return windowsProbe; +} + +/** Codex's system folder: `/etc/codex`, or `OpenAI\Codex` in the ProgramData known folder on Windows. */ +function systemDir(): string { + if (!IS_WINDOWS) return "/etc/codex"; + return path.join(probeWindowsPolicy()?.programData ?? "C:\\ProgramData", "OpenAI", "Codex"); +} + +/** + * Why Codex may be under an organization policy Scient Agent cannot apply (requirements, a + * managed config, macOS managed preferences, or a Windows Group Policy requirements path), or + * undefined when none is present. A policy location that cannot be checked counts as present, + * as Codex refuses to assume it absent. + */ +async function managedPolicy(): Promise { + const files = [path.join(systemDir(), "requirements.toml")]; + if (IS_WINDOWS) { + const probe = probeWindowsPolicy(); + if (probe === null) return "Windows cannot say where Codex's organization policy is"; + if (probe.requirementsPath !== null) { + return `Codex is managed by an organization policy (Group Policy RequirementsPath ${probe.requirementsPath})`; + } + } else { + // Codex ignores a leftover managed_config.toml on Windows. + files.push("/etc/codex/managed_config.toml"); + } + if (process.platform === "darwin") { + const managed = "/Library/Managed Preferences"; + files.push( + path.join(managed, "com.openai.codex.plist"), + path.join(managed, os.userInfo().username, "com.openai.codex.plist"), + ); + } + for (const file of files) { + try { + await fs.stat(file); + return `Codex is managed by an organization policy (${file})`; + } catch (error) { + const code = (error as NodeJS.ErrnoException).code; + if (code !== "ENOENT") return `Codex's organization policy at ${file} cannot be checked (${code})`; + } + } + return undefined; +} + +/** + * The user config's `[[skills.config]]` rules as Codex reads them: an entry selects by an absolute + * `path` (canonicalized) or a `name`, never both; a later entry for the same selector replaces an + * earlier one. A `skills` table Codex cannot read yields no rules, as in Codex. + */ +async function readSkillRules(user: Record, warnings: string[]): Promise { + const skills = user.skills; + if (skills == null) return []; + const entries = isRecord(skills) ? (skills.config ?? []) : null; + const valid = + isRecord(skills) && + Array.isArray(entries) && + entries.every( + entry => + isRecord(entry) && + isBoolean(entry.enabled) && + (entry.path == null || (isString(entry.path) && path.isAbsolute(entry.path))) && + (entry.name == null || isString(entry.name)), + ) && + (skills.bundled == null || + (isRecord(skills.bundled) && (skills.bundled.enabled == null || isBoolean(skills.bundled.enabled)))) && + (skills.include_instructions == null || isBoolean(skills.include_instructions)) && + (skills.max_context_tokens == null || isPositiveInteger(skills.max_context_tokens)); + if (!valid) { + warnings.push("[codex-plugins] Ignoring Codex's [skills] settings, which Codex cannot read either"); + return []; + } + const rules: SkillRule[] = []; + for (const entry of entries as Record[]) { + // Codex trims a name selector of Unicode whitespace (`str::trim`). + const name = + typeof entry.name === "string" ? entry.name.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "") : undefined; + if ((entry.path == null) === (entry.name == null) || name === "") continue; + const selector = + name !== undefined + ? { name } + : { path: await fs.realpath(entry.path as string).catch(() => entry.path as string) }; + const replaced = rules.findIndex(rule => JSON.stringify(rule.selector) === JSON.stringify(selector)); + if (replaced !== -1) rules.splice(replaced, 1); + rules.push({ selector, enabled: entry.enabled as boolean }); + } + return rules; +} + +/** + * Where an app's settings in a plugin's `apps` table have a type Codex rejects (`AppConfig`), as + * a key path suffix (`""` for the table itself), or undefined. + */ +function appTypeError(settings: unknown): string | undefined { + const isReviewer = (value: unknown) => value === "user" || value === "auto_review" || value === "guardian_subagent"; + if (!isRecord(settings)) return ""; + for (const key of ["enabled", "destructive_enabled", "open_world_enabled", "default_tools_enabled"]) { + if (settings[key] != null && !isBoolean(settings[key])) return `.${key}`; + } + if (settings.omit_tools_from != null && !SERVER_FIELD_TYPES.omit_tools_from(settings.omit_tools_from)) { + return ".omit_tools_from"; + } + if (settings.approvals_reviewer != null && !isReviewer(settings.approvals_reviewer)) return ".approvals_reviewer"; + const mode = settings.default_tools_approval_mode; + if (mode != null && !isApprovalMode(mode)) return ".default_tools_approval_mode"; + for (const [group, fields] of [ + ["tools", { enabled: isBoolean, approval_mode: isApprovalMode, approvals_reviewer: isReviewer }], + ["links", { approvals_reviewer: isReviewer, default_tools_approval_mode: isApprovalMode }], + ] as const) { + const entries = settings[group]; + if (entries == null) continue; + if (!isRecord(entries)) return `.${group}`; + for (const [name, entry] of Object.entries(entries)) { + if (!isRecord(entry)) return `.${group}.${name}`; + for (const [field, valid] of Object.entries(fields)) { + if (entry[field] != null && !(valid as (value: unknown) => boolean)(entry[field])) { + return `.${group}.${name}.${field}`; + } + } + } + } + return undefined; +} + +/** + * The first field of the merged `[plugins]` table whose type Codex rejects (`PluginsConfigToml`), + * or undefined. Codex then reads no plugin settings at all. + */ +function pluginsTypeError(table: Record): string | undefined { + for (const [id, config] of Object.entries(table)) { + if (!isRecord(config)) return id; + if (config.enabled != null && !isBoolean(config.enabled)) return `${id}.enabled`; + for (const key of ["remote_plugin_id", "release_id"]) { + if (config[key] != null && !isString(config[key])) return `${id}.${key}`; + } + if (config.auto_provision != null && !isBoolean(config.auto_provision)) return `${id}.auto_provision`; + const apps = config.apps; + if (apps != null && !isRecord(apps)) return `${id}.apps`; + for (const [app, settings] of Object.entries(apps ?? {})) { + const mistyped = appTypeError(settings); + if (mistyped !== undefined) return `${id}.apps.${app}${mistyped}`; + } + const servers = config.mcp_servers; + if (servers == null) continue; + if (!isRecord(servers)) return `${id}.mcp_servers`; + for (const [name, policy] of Object.entries(servers)) { + const at = `${id}.mcp_servers.${name}`; + if (!isRecord(policy)) return at; + if (policy.enabled != null && !isBoolean(policy.enabled)) return `${at}.enabled`; + const mode = policy.default_tools_approval_mode; + if (mode != null && !isApprovalMode(mode)) return `${at}.default_tools_approval_mode`; + for (const key of ["enabled_tools", "disabled_tools"]) { + if (policy[key] != null && !isStringList(policy[key])) return `${at}.${key}`; + } + if (policy.tools != null && !SERVER_FIELD_TYPES.tools(policy.tools)) return `${at}.tools`; + } + } + return undefined; +} + +/** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ +async function listCodexPlugins(ctx: LoadContext): Promise { + const warnings: string[] = []; + const none = (env: Record): PluginList => ({ + plugins: [], + warnings, + env, + skillRules: [], + configuredServers: new Set(), + }); + // The environment Scient Agent was launched with: no `.env` file, from this project or one the + // agent worked in before, chooses Codex's home or what reaches a plugin's server. + const launch = getLaunchEnv(); + if (launch === undefined) { + warnings.push( + "[codex-plugins] This run cannot tell Scient Agent's launch environment from .env files (start it with --no-env-file); no Codex plugins load", + ); + return none({}); + } + const env = filterProcessEnv(launch); + // Codex's home: `CODEX_HOME` exactly as set, which must be a folder, canonicalized; else `~/.codex`. + let home = path.join(ctx.home, ".codex"); + if (env.CODEX_HOME) { + if (!(await fs.stat(env.CODEX_HOME).catch(() => null))?.isDirectory()) { + warnings.push( + `[codex-plugins] CODEX_HOME points to ${JSON.stringify(env.CODEX_HOME)}, which is not a folder, so Codex does not start; no Codex plugins load`, + ); + return none(env); + } + home = await fs.realpath(env.CODEX_HOME); + } + const policy = await managedPolicy(); + if (policy !== undefined) { + warnings.push(`[codex-plugins] ${policy}, which Scient Agent cannot apply; no Codex plugins load`); + return none(env); + } + const user = await readToml(path.join(home, "config.toml"), warnings); + if (user === null) return none(env); + const system = await readToml(path.join(systemDir(), "config.toml"), warnings); + const merged = mergeLayers(system ?? {}, user); + // `[features] plugins = false` turns every plugin off in Codex. + if (isRecord(merged.features) && "plugins" in merged.features && merged.features.plugins !== true) { + warnings.push("[codex-plugins] Codex's plugins are turned off ([features] plugins); no Codex plugins load"); + return none(env); + } + const table = isRecord(merged.plugins) ? merged.plugins : {}; + // Codex reads no plugin at all from a [plugins] table it cannot read. + const mistyped = pluginsTypeError(table); + if (mistyped !== undefined) { + warnings.push(`[codex-plugins] Codex cannot read its [plugins] settings (${mistyped}); no Codex plugins load`); + return none(env); + } + const defaults = isRecord(table._default) ? table._default : {}; + const enabledByDefault = defaults.enabled !== false; + const read = await Promise.all( + Object.entries(table).map(([id, config]) => { + if (id === "_default" || !isRecord(config) || HOST_PLUGINS[id] === true) return null; + const enabled = typeof config.enabled === "boolean" ? config.enabled : enabledByDefault; + return enabled ? readPlugin(home, id, config, warnings) : null; + }), + ); + return { + plugins: read.filter(plugin => plugin !== null), + warnings, + env, + skillRules: await readSkillRules(user, warnings), + configuredServers: new Set(isRecord(merged.mcp_servers) ? Object.keys(merged.mcp_servers) : []), + }; +} + +/** + * A manifest path's part below `./`, or null when Codex ignores the path: not `./`-relative, + * rooted, or holding `..`. A backslash separates path parts only on Windows, as in Codex. + */ +function manifestRelative(value: unknown): string | null { + if (typeof value !== "string" || !value.startsWith("./")) return null; + const relative = value.slice(2); + if (!relative || relative.startsWith("/")) return null; + if (IS_WINDOWS && (relative.startsWith("\\") || /^[A-Za-z]:/.test(relative))) return null; + return relative.split(IS_WINDOWS ? /[\\/]/ : "/").includes("..") ? null : relative; +} + +/** A manifest path Codex takes, symlinks resolved; null (with a warning when it resolves outside the package). */ +async function packagePath(plugin: CodexPlugin, relative: string, key: string, warnings: string[]) { + const resolved = await resolveContainedPath(plugin.root, path.resolve(plugin.root, relative)); + if (resolved.status === "outside") { + warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ./${relative}`); + return null; + } + return resolved.status === "ok" ? resolved.realPath : null; +} + +/** A plugin's skill, read once its `SKILL.md` is known to stay inside the package; null when it is not a skill. */ +async function readPluginSkill( + plugin: CodexPlugin, + skillPath: string, + realPath: string, + warnings: string[], +): Promise { + const stat = await fs.stat(realPath).catch(() => null); + if (!stat?.isFile()) return null; + const content = await readFile(realPath); + if (!content) return null; + try { + const { frontmatter, body } = parseFrontmatter(content, { source: skillPath }); + if (frontmatter.enabled === false) return null; + // Codex collapses the whitespace of a skill's name, or of the folder name standing in for it + // (`split_whitespace`, Unicode White_Space), so rules match that name. + const collapse = (value: string) => + value + .split(/\p{White_Space}+/u) + .filter(Boolean) + .join(" "); + const name = + (typeof frontmatter.name === "string" ? collapse(frontmatter.name) : "") || + collapse(path.basename(path.dirname(skillPath))); + // `/` is reserved for skill namespaces and skill:// paths. + if (/[\\/]/.test(name)) { + warnings.push(`[codex-plugins] Skill name "${name}" contains a path separator, skipping: ${skillPath}`); + return null; + } + return { + name, + path: skillPath, + content: body, + frontmatter: frontmatter as SkillFrontmatter, + level: "user", + // skill:// resolves every file of the skill inside the package too. + containRoot: plugin.root, + _source: createSourceMeta(PROVIDER_ID, skillPath, "user", undefined, plugin.id), + }; + } catch { + warnings.push(`[codex-plugins] Failed to read skill file: ${skillPath}`); + return null; + } +} + +/** + * Codex's scan of a plugin's skill folder: every `SKILL.md` down to its depth limit for a legacy + * plugin, each direct child's for an Agent Plugins package, hidden folders skipped. Each folder and + * `SKILL.md` must resolve inside the package before it is read. + */ +async function scanPluginSkills(plugin: CodexPlugin, dir: string, warnings: string[]): Promise { + const recursive = plugin.format === "legacy"; + // A SKILL.md sits one level below its folder, so folders go one level less deep than files. + const maxFolderDepth = recursive ? MAX_SKILL_SCAN_DEPTH - 1 : 1; + const skills: Skill[] = []; + const seen = new Set(); + const visit = async (folder: string, depth: number): Promise => { + if (seen.has(folder) || seen.size >= MAX_SKILL_DIRS) return; + seen.add(folder); + if (recursive || depth === 1) { + const skillPath = path.join(folder, "SKILL.md"); + const file = await resolveContainedPath(plugin.root, skillPath); + if (file.status === "outside") + warnings.push(`[codex-plugins] Ignoring skill outside ${plugin.id}: ${skillPath}`); + if (file.status === "ok") { + const skill = await readPluginSkill(plugin, skillPath, file.realPath, warnings); + if (skill !== null) skills.push(skill); + } + } + if (depth >= maxFolderDepth) return; + const children = (await readDirEntries(folder)).filter( + entry => !entry.name.startsWith(".") && (entry.isDirectory() || entry.isSymbolicLink()), + ); + await Promise.all( + children.map(async entry => { + const child = await resolveContainedPath(plugin.root, path.join(folder, entry.name)); + if (child.status === "outside") { + warnings.push( + `[codex-plugins] Ignoring skill folder outside ${plugin.id}: ${path.join(folder, entry.name)}`, + ); + } + if (child.status !== "ok") return; + if ((await fs.stat(child.realPath).catch(() => null))?.isDirectory()) + await visit(child.realPath, depth + 1); + }), + ); + }; + await visit(dir, 0); + return skills; +} + +async function loadSkills(ctx: LoadContext): Promise> { + if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; + const { plugins, warnings, skillRules } = await listCodexPlugins(ctx); + // Each skill with the namespace Codex gives it: its plugin manifest's name. + const found: { skill: Skill; namespace: string }[] = []; + await Promise.all( + plugins.map(async plugin => { + const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; + const listed = declared == null ? [] : Array.isArray(declared) ? declared : [declared]; + // Codex drops the paths it does not take, and falls back to `skills/` when none is left. + const taken = listed.map(manifestRelative).filter(relative => relative !== null); + if (taken.length < listed.length) { + warnings.push(`[codex-plugins] Ignoring skills paths of ${plugin.id} that are not ./paths without ..`); + } + const dirs = new Set(); + for (const relative of taken.length > 0 ? taken : ["skills"]) { + const dir = await packagePath(plugin, relative, "skills", warnings); + if (dir !== null) dirs.add(dir); + } + // Codex names a plugin's skills `:`, the package folder's name standing + // in for a blank manifest name. + const raw = typeof plugin.manifest.name === "string" ? plugin.manifest.name : ""; + const namespace = raw.replace(/\p{White_Space}+/gu, "") ? raw : path.basename(plugin.root); + for (const dir of dirs) { + for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push({ skill, namespace }); + } + }), + ); + // The user's [[skills.config]] rules, in order: a path selects one SKILL.md, a name the skill + // Codex names so. + const realPaths = await Promise.all(found.map(({ skill }) => fs.realpath(skill.path).catch(() => skill.path))); + const disabled = new Set(); + for (const rule of skillRules) { + found.forEach(({ skill, namespace }, index) => { + const selected = + "path" in rule.selector + ? rule.selector.path === realPaths[index] + : rule.selector.name === `${namespace}:${skill.name}`; + if (!selected) return; + if (rule.enabled) disabled.delete(realPaths[index]); + else disabled.add(realPaths[index]); + }); + } + const items = found.filter((_, index) => !disabled.has(realPaths[index])).map(({ skill }) => skill); + // Reads finish in any order; the list does not. + items.sort((a, b) => compareSkillOrder(a.name, a.path, b.name, b.path)); + return { items, warnings }; +} + +/** Why an MCP server runs only inside OpenAI's apps, or undefined when it runs anywhere. */ +function hostOnlyReason(server: Record): string | undefined { + if (typeof server.command === "string" && path.basename(server.command) === HOST_LAUNCHER) { + return "it launches OpenAI's Computer Use client"; + } + if (server.auth === "chatgpt") return "it signs in with Codex's own ChatGPT session"; + return undefined; +} + +/** + * The Codex key that narrows a server's tools, or undefined when it exposes them all. Scient + * Agent's MCP servers carry no tool filter, so such a server would run with more tools than Codex + * allows. An empty denylist narrows nothing. + */ +function toolFilterKey(table: Record): string | undefined { + if (table.enabled_tools != null) return "enabled_tools"; + const denied = table.disabled_tools; + if (denied != null && !(Array.isArray(denied) && denied.length === 0)) return "disabled_tools"; + // Codex hides the server's tools from the model where this lists. + const omitted = table.omit_tools_from; + if (omitted != null && !(Array.isArray(omitted) && omitted.length === 0)) return "omit_tools_from"; + return undefined; +} + +/** + * The Codex key that makes some of a server's tools ask before they run (`prompt`, `writes`, or a + * mode Scient Agent does not know) or caps their output (`output_token_limit`), or undefined when + * neither applies. Scient Agent cannot carry either over, so such a server would run its tools + * unasked or return more than Codex allows. + */ +function approvalKey(table: Record): string | undefined { + const unasked = (mode: unknown) => mode == null || mode === "auto" || mode === "approve"; + if (!unasked(table.default_tools_approval_mode)) return "default_tools_approval_mode"; + if (table.tools == null) return undefined; + if (!isRecord(table.tools)) return "tools"; + for (const [tool, config] of Object.entries(table.tools)) { + if (!isRecord(config) || !unasked(config.approval_mode)) return `tools.${tool}.approval_mode`; + if (config.output_token_limit != null) return `tools.${tool}.output_token_limit`; + } + return undefined; +} + +const isStringList = (value: unknown): value is string[] => + Array.isArray(value) && value.every(item => typeof item === "string"); +const isStringMap = (value: unknown): value is Record => + isRecord(value) && Object.values(value).every(item => typeof item === "string"); +const isString = (value: unknown) => typeof value === "string"; +const isBoolean = (value: unknown) => typeof value === "boolean"; +const isPositiveInteger = (value: unknown) => Number.isInteger(value) && (value as number) > 0; +/** Seconds Rust's `Duration::try_from_secs_f64` takes, as Codex converts them: finite, not negative, under 2^64. */ +const isDurationSeconds = (value: unknown) => + typeof value === "number" && Number.isFinite(value) && value >= 0 && value < 2 ** 64; +const isApprovalMode = (value: unknown) => ["auto", "prompt", "writes", "approve"].includes(value as string); + +/** Codex's string fields of a legacy manifest's `interface` (camelCase, with its URL aliases). */ +const INTERFACE_STRINGS = [ + "displayName", + "shortDescription", + "longDescription", + "developerName", + "category", + "websiteUrl", + "websiteURL", + "privacyPolicyUrl", + "privacyPolicyURL", + "termsOfServiceUrl", + "termsOfServiceURL", + "brandColor", + "composerIcon", + "logo", + "logoDark", +]; + +/** The first legacy manifest field whose type Codex rejects, which makes it skip the plugin, or undefined. */ +function manifestTypeError(manifest: Record): string | undefined { + // `name` and `keywords` cannot be null; the other fields may be. + if ("name" in manifest && !isString(manifest.name)) return "name"; + if ("keywords" in manifest && !isStringList(manifest.keywords)) return "keywords"; + for (const key of ["version", "description", "apps"]) { + if (manifest[key] != null && !isString(manifest[key])) return key; + } + const ui = manifest.interface; + if (ui == null) return undefined; + if (!isRecord(ui)) return "interface"; + for (const key of ["capabilities", "screenshots"]) { + if (key in ui && !isStringList(ui[key])) return `interface.${key}`; + } + for (const key of INTERFACE_STRINGS) if (ui[key] != null && !isString(ui[key])) return `interface.${key}`; + return undefined; +} + +/** + * The types Codex accepts for a legacy server's fields (null counts as absent); it rejects a server + * with any other, so its siblings load without it. + */ +const SERVER_FIELD_TYPES: Record boolean> = { + command: isString, + args: isStringList, + env: isStringMap, + cwd: isString, + http_headers: isStringMap, + env_http_headers: isStringMap, + url: isString, + bearer_token: isString, + bearer_token_env_var: isString, + http_headers_helper: isString, + environment_id: isString, + auth: value => value === "oauth" || value === "chatgpt" || value === "ema_auth", + startup_timeout_sec: isDurationSeconds, + startup_timeout_ms: value => Number.isInteger(value) && (value as number) >= 0 && (value as number) < 2 ** 64, + tool_timeout_sec: isDurationSeconds, + enabled: isBoolean, + required: isBoolean, + supports_parallel_tool_calls: isBoolean, + startup_readiness: value => value === "connection" || value === "catalog", + tool_input_schema_max_bytes: isPositiveInteger, + omit_tools_from: value => + isStringList(value) && value.every(surface => ["code_mode", "deferred", "direct"].includes(surface)), + default_tools_approval_mode: isApprovalMode, + enabled_tools: isStringList, + disabled_tools: isStringList, + scopes: isStringList, + oauth: value => + isRecord(value) && + ["client_id", "client_secret", "callback_url", "authorization_server_issuer", "token_endpoint_auth_method"].every( + key => value[key] == null || isString(value[key]), + ) && + (value.callback_port == null || + (Number.isInteger(value.callback_port) && + (value.callback_port as number) >= 0 && + (value.callback_port as number) <= 65535)), + oauth_resource: isString, + name: isString, + tools: value => + isRecord(value) && + Object.values(value).every( + tool => + isRecord(tool) && + (tool.approval_mode == null || isApprovalMode(tool.approval_mode)) && + (tool.output_token_limit == null || isPositiveInteger(tool.output_token_limit)), + ), +}; + +/** + * The names of Codex's `env_vars` entries (a name, or `{ name, source = "local" }` with no other + * keys); null when one is malformed. + */ +function envVarNames(value: unknown): string[] | null { + if (value == null) return []; + if (!Array.isArray(value)) return null; + const names: string[] = []; + for (const entry of value) { + if (typeof entry === "string") names.push(entry); + else if ( + isRecord(entry) && + typeof entry.name === "string" && + (entry.source ?? "local") === "local" && + Object.keys(entry).every(key => key === "name" || key === "source") + ) { + names.push(entry.name); + } else return null; + } + return names; +} + +type Mapped = { server: Partial } | { error: string }; + +/** Codex's camelCase OAuth aliases, each giving way to its snake_case twin when that key is present, even null. */ +const OAUTH_ALIASES = [ + ["callbackUrl", "callback_url"], + ["callbackPort", "callback_port"], + ["clientId", "client_id"], + ["clientSecret", "client_secret"], +] as const; + +/** A plugin's OAuth table as Codex reads it: aliases folded by key presence before any type is checked. */ +function codexOAuthTable(oauth: Record): Record { + const table = { ...oauth }; + for (const [camel, snake] of OAUTH_ALIASES) { + if (!(camel in table)) continue; + if (!(snake in table)) table[snake] = table[camel]; + delete table[camel]; + } + return table; +} + +/** Blank as Rust's `str::trim` sees it (Unicode White_Space, U+0085 included and U+FEFF not). */ +function isCodexBlank(value: string): boolean { + return !value.replace(/\p{White_Space}+/gu, ""); +} + +/** + * A legacy plugin's server as Codex reads it: exactly one transport with its own fields, package + * paths kept inside the package, values taken literally, and only Codex's variables inherited. + */ +async function mapLegacyServer( + plugin: CodexPlugin, + declared: Record, + env: Record, +): Promise { + const server = isRecord(declared.oauth) ? { ...declared, oauth: codexOAuthTable(declared.oauth) } : declared; + const mistyped = Object.keys(SERVER_FIELD_TYPES).find( + key => server[key] != null && !SERVER_FIELD_TYPES[key](server[key]), + ); + if (mistyped !== undefined) return { error: `invalid ${mistyped}` }; + // Another environment runs the server for Codex; here it would run on this machine. + if (server.environment_id != null && server.environment_id !== "local") { + return { error: `it runs in Codex environment ${server.environment_id}` }; + } + const isStdio = server.command != null; + if (!isStdio && server.url == null) return { error: "it declares neither command nor url" }; + const wrong = (isStdio ? NOT_STDIO : NOT_HTTP).find(key => server[key] != null); + if (wrong !== undefined) return { error: `${wrong} is not supported for ${isStdio ? "stdio" : "streamable HTTP"}` }; + // Codex holds every tool call to tool_timeout_sec, whatever the call's arguments ask; at zero every + // call times out, which no cap here can reproduce. + if (server.tool_timeout_sec === 0) return { error: "Codex times out each of its tool calls (tool_timeout_sec = 0)" }; + const common = { + ...(typeof server.tool_timeout_sec === "number" && { toolTimeoutMs: server.tool_timeout_sec * 1000 }), + }; + + if (isStdio) { + const command = (server.command as string).trim(); + if (!command) return { error: "command is empty" }; + const forwarded = envVarNames(server.env_vars); + if (forwarded === null) return { error: "invalid env_vars" }; + // Codex runs a server from the package unless it names a folder inside it. + const cwd = typeof server.cwd === "string" ? path.resolve(plugin.root, server.cwd) : plugin.root; + if (!(await isContainedResolved(plugin.root, cwd))) return { error: `cwd ${server.cwd} leaves the package` }; + // A bare name runs from PATH and an absolute path as given; a relative path runs from the package. + const relative = !path.isAbsolute(command) && /[\\/]/.test(command); + const resolvedCommand = relative ? path.resolve(cwd, command) : command; + if (relative && !(await isContainedResolved(plugin.root, resolvedCommand))) { + return { error: `command ${command} leaves the package` }; + } + return { + server: { + transport: "stdio", + command: resolvedCommand, + ...(server.args != null && { args: server.args as string[] }), + env: stdioEnv(env, forwarded, server.env as Record | undefined), + envPolicy: "literal", + envInherit: [], + cwd, + ...common, + }, + }; + } + + if (server.http_headers_helper != null) return { error: "it computes its headers with a helper command" }; + if (server.auth != null && server.auth !== "oauth") return { error: `auth ${String(server.auth)} is not supported` }; + const url = server.url as string; + if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return { error: `invalid url ${url}` }; + // Static headers are data; the others name variables of the launch environment, as in Codex, which + // sets them in that order, each replacing a header of the same name in any case, and skips a header + // whose name or value HTTP does not allow, or an environment value that is only whitespace (used + // as is otherwise). A named bearer variable that is unset or empty, or a bearer HTTP does not + // allow, keeps Codex from starting the server. + const headers: Record = {}; + const put = (header: string, value: string) => { + if (!HTTP_TOKEN.test(header) || !HTTP_HEADER_VALUE.test(value)) return false; + for (const key of Object.keys(headers)) if (key.toLowerCase() === header.toLowerCase()) delete headers[key]; + headers[header] = value; + return true; + }; + for (const [header, value] of Object.entries((server.http_headers ?? {}) as Record)) { + put(header, value); + } + for (const [header, name] of Object.entries((server.env_http_headers ?? {}) as Record)) { + const value = envValue(env, name); + if (value?.replace(/\p{White_Space}+/gu, "")) put(header, value); + } + if (server.bearer_token_env_var != null) { + const bearer = envValue(env, server.bearer_token_env_var as string); + if (!bearer || !put("Authorization", `Bearer ${bearer}`)) { + return { + error: `its bearer_token_env_var ${String(server.bearer_token_env_var)} is unset, empty or not a header value`, + }; + } + } + // Codex's OAuth client settings, their camelCase aliases already folded in. + const oauthTable = (server.oauth ?? {}) as Record; + const oauth: NonNullable = {}; + for (const [key, target] of [ + ["client_id", "clientId"], + ["client_secret", "clientSecret"], + ["callback_url", "redirectUri"], + ] as const) { + const value = oauthTable[key]; + if (typeof value !== "string") continue; + // An empty client secret is kept for the check below, as Codex rejects it; an empty ID or URL means none. + if (value || target === "clientSecret") oauth[target] = value; + } + // Codex refuses a client secret without a client ID, or either of them blank, and the enterprise sign-in + // settings on any server but an ema_auth one, which is left out above. + if ( + oauth.clientSecret !== undefined && + (oauth.clientId === undefined || isCodexBlank(oauth.clientId) || isCodexBlank(oauth.clientSecret)) + ) { + return { + error: "its OAuth client_secret has no client_id or one of them is blank, so Codex refuses to start it", + }; + } + // Without a secret, Codex takes a blank client ID for none. + if (oauth.clientId !== undefined && isCodexBlank(oauth.clientId)) delete oauth.clientId; + const enterprise = ["authorization_server_issuer", "token_endpoint_auth_method"].find( + key => oauthTable[key] != null, + ); + if (enterprise !== undefined) + return { error: `its OAuth ${enterprise} needs ema_auth, so Codex refuses to start it` }; + // Codex holds a confidential client's sign-in to its client ID exactly as written and sends its secret as + // written, both of which Scient Agent's sign-in would trim. + if ([oauth.clientId, oauth.clientSecret].some(value => value !== undefined && value !== value.trim())) { + return { + error: "its OAuth client_id or client_secret has surrounding whitespace, which Scient Agent's sign-in would not keep", + }; + } + const port = oauthTable.callback_port; + if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); + // Codex asks for the configured scopes as they are, an empty list meaning none. + if (isStringList(server.scopes)) oauth.scope = server.scopes.join(" "); + const resource = typeof server.oauth_resource === "string" && server.oauth_resource ? server.oauth_resource : null; + return { + server: { + transport: "http", + url, + ...(Object.keys(headers).length > 0 && { headers }), + headerPolicy: "origin-locked", + ...(Object.keys(oauth).length > 0 && { oauth }), + ...(resource !== null && { auth: { type: "oauth" as const, resource } }), + ...common, + }, + }; +} + +/** A legacy server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ +async function readLegacyServerMap( + plugin: CodexPlugin, + manifest: Record, + manifestPath: string, + warnings: string[], +): Promise<{ servers: Record; sourcePath: string } | null> { + const declared = manifest.mcpServers; + let parsed: unknown = declared; + let sourcePath = manifestPath; + if (!isRecord(declared)) { + // Codex drops a path it does not take, or a value of another type, and reads `.mcp.json` instead. + const relative = manifestRelative(declared); + if (declared != null && relative === null) { + warnings.push(`[codex-plugins] Ignoring mcpServers of ${plugin.id}, which is not a ./path without ..`); + } + const file = await packagePath(plugin, relative ?? ".mcp.json", "mcpServers", warnings); + if (file === null) { + if (relative !== null) warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}`); + return null; + } + try { + parsed = JSON.parse((await readFile(file)) ?? ""); + } catch { + warnings.push(`[codex-plugins] Invalid JSON in ${file}`); + return null; + } + sourcePath = file; + } + if (!isRecord(parsed)) return null; + // Codex accepts `{ "mcpServers": { ... } }` and the bare server map. + const servers = "mcpServers" in parsed ? parsed.mcpServers : parsed; + return isRecord(servers) ? { servers, sourcePath } : null; +} + +/** + * An Agent Plugins package's `mcp.json`, read under that standard and Codex's rules for it: the + * file must be a regular file at the root, `sse` servers do not run, and a stdio server takes the + * `env_vars` a valid `.codex-plugin` declaration of the same stdio server adds (dropping the + * `${NAME}` placeholders those variables replace). + */ +async function readStandardServers( + ctx: LoadContext, + plugin: CodexPlugin, + env: Record, + warnings: string[], +): Promise<{ servers: Record>; sourcePath: string } | null> { + const file = path.join(plugin.root, "mcp.json"); + const stat = await lstatOrNull(file); + if (stat === null) return null; + if (!stat.isFile()) { + warnings.push(`[codex-plugins] ${plugin.id}: mcp.json is not a regular file`); + return null; + } + const raw = await readFile(file); + if (raw === null) return null; + const digest = new Bun.CryptoHasher("sha256").update(`codex:${plugin.id}`).digest("hex").slice(0, 16); + const pluginData = path.join(getPluginsDir(ctx.home), "data", `codex-${plugin.name}-${digest}`); + const result = await parseAgentPluginMcp(raw, { pluginRoot: plugin.root, pluginData }); + if (result.status === "disabled") { + warnings.push(`[codex-plugins] ${plugin.id}: MCP disabled: ${result.reason}`); + return null; + } + warnings.push(...result.warnings.map(warning => `[codex-plugins] ${plugin.id}: ${warning}`)); + // Codex overlays only a valid `.codex-plugin` declaration of a valid stdio server. + const codexManifest = await readLegacyManifest(plugin.root, plugin.id, [], LEGACY_MANIFESTS.slice(0, 1)); + const overlay = + codexManifest && + (await readLegacyServerMap(plugin, codexManifest.manifest, codexManifest.manifestPath, []))?.servers; + const servers: Record> = {}; + for (const { name, ...server } of result.servers) { + if (server.transport === "sse") { + warnings.push(`[codex-plugins] Skipping MCP server "${name}" of ${plugin.id}: Codex does not run sse servers`); + continue; + } + if (server.transport !== "stdio") { + servers[name] = { ...server, headerPolicy: "origin-locked" }; + continue; + } + const declared = overlay?.[name]; + let forwarded: string[] = []; + if (isRecord(declared)) { + const mapped = await mapLegacyServer(plugin, declared, env).catch(() => null); + if (mapped !== null && "server" in mapped && mapped.server.transport === "stdio") { + forwarded = envVarNames(declared.env_vars) ?? []; + } + } + // A `${NAME}` placeholder for a forwarded variable gives way to that variable's real value. + const kept = Object.entries(server.env ?? {}).filter( + ([key, value]) => + !forwarded.some(variable => sameEnvName(key, variable) && sameEnvName(value, `\${${variable}}`)), + ); + servers[name] = { + ...server, + env: stdioEnv(env, forwarded, Object.fromEntries(kept)), + envPolicy: "literal", + envInherit: [], + }; + } + if (Object.values(servers).some(server => server.transport === "stdio")) + await fs.mkdir(pluginData, { recursive: true }); + return { servers, sourcePath: file }; +} + +async function loadMCPServers(ctx: LoadContext): Promise> { + if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; + const { plugins, warnings, env, configuredServers } = await listCodexPlugins(ctx); + const items: MCPServer[] = []; + const read: { plugin: CodexPlugin; declared: Record; sourcePath: string; standard: boolean }[] = []; + for (const plugin of plugins) { + const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, env, warnings) : null; + const legacy = + plugin.format === "legacy" + ? await readLegacyServerMap(plugin, plugin.manifest, plugin.manifestPath, warnings) + : null; + read.push({ + plugin, + declared: standard?.servers ?? legacy?.servers ?? {}, + sourcePath: standard?.sourcePath ?? legacy?.sourcePath ?? plugin.manifestPath, + standard: standard !== null, + }); + } + // Codex runs one server per name: when enabled plugins declare the same one, which declaration + // wins (and so whose settings apply) is Codex's call, so none of them runs here. + const declarers = new Map(); + for (const { plugin, declared } of read) { + for (const name of Object.keys(declared)) declarers.set(name, [...(declarers.get(name) ?? []), plugin.id]); + } + for (const { plugin, declared, sourcePath, standard } of read) { + const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; + for (const [serverName, server] of Object.entries(declared)) { + if (!isRecord(server)) continue; + const skip = `[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}`; + const reason = hostOnlyReason(server); + if (reason !== undefined) { + logger.debug(`${skip}: ${reason}`); + continue; + } + // Codex refuses to start a server whose name does not match its pattern (validate_mcp_server_name). + if (!CODEX_SERVER_NAME.test(serverName)) { + warnings.push(`${skip}: Codex refuses its name, which must match ${CODEX_SERVER_NAME.source}`); + continue; + } + // Codex registers the config's own `[mcp_servers.]` over a plugin's server of that name, + // so its settings, `enabled = false` included, are what Codex runs. + if (configuredServers.has(serverName)) { + warnings.push(`${skip}: Codex runs your own [mcp_servers.${serverName}] instead`); + continue; + } + const others = (declarers.get(serverName) ?? []).filter(id => id !== plugin.id); + if (others.length > 0) { + warnings.push(`${skip}: ${others.join(", ")} declares a server of that name too, and Codex runs only one`); + continue; + } + const declaredPolicy = policies[serverName]; + const policy: Record = isRecord(declaredPolicy) ? declaredPolicy : {}; + const restricted = + toolFilterKey(server) ?? toolFilterKey(policy) ?? approvalKey(server) ?? approvalKey(policy); + if (restricted !== undefined) { + warnings.push(`${skip}: Codex restricts its tools (${restricted}), which Scient Agent cannot enforce`); + continue; + } + // An `ema_auth` entry stands for the enterprise sign-in Codex no longer supports there; Codex + // turns the server off for good, so no setting here may turn it back on. + if ("ema_auth" in policy) { + warnings.push(`${skip}: Codex turns it off (ema_auth)`); + continue; + } + // Codex turns a server off for good when its declaration or the plugin's mcp_servers. + // table says `enabled = false`: no setting here may start it. + if (server.enabled === false || policy.enabled === false) { + warnings.push(`${skip}: Codex turns it off (enabled = false)`); + continue; + } + let mapped: Mapped; + try { + mapped = standard ? { server } : await mapLegacyServer(plugin, server, env); + } catch (error) { + mapped = { error: String(error) }; + } + if ("error" in mapped) { + warnings.push(`${skip}: ${mapped.error} (${sourcePath})`); + continue; + } + // The plugin's ID names the server, so no other plugin's arrival renames it. + const name = `${plugin.id}:${serverName}`; + const { server: config } = mapped; + items.push({ + name, + ...config, + // Codex holds each tool call to tool_timeout_sec, five minutes unless set, in both formats. + toolTimeoutMs: config.toolTimeoutMs ?? CODEX_DEFAULT_TOOL_TIMEOUT_MS, + // Its own sign-in, never another server's at the same URL: Codex keys sign-ins by name and URL. A + // server that names its own token (`bearer_token_env_var`) is never signed in with OAuth there, + // so it is bound as a key, which no stored sign-in answers. + ...(config.transport === "http" && + config.url && { + auth: { + ...config.auth, + type: server.bearer_token_env_var != null ? ("apikey" as const) : ("oauth" as const), + credentialId: mcpOAuthServerCredentialId(name, config.url), + }, + }), + _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.id), + }); + } + } + return { items, warnings }; +} + +registerProvider(skillCapability.id, { + id: PROVIDER_ID, + displayName: DISPLAY_NAME, + description: "Load skills from installed, enabled Codex plugins ($CODEX_HOME/plugins/cache)", + priority: PRIORITY, + load: loadSkills, +}); + +registerProvider(mcpCapability.id, { + id: PROVIDER_ID, + displayName: DISPLAY_NAME, + description: "Load MCP servers from installed, enabled Codex plugins ($CODEX_HOME/plugins/cache)", + priority: PRIORITY, + load: loadMCPServers, +}); diff --git a/packages/coding-agent/src/scient/command-help.ts b/packages/coding-agent/src/scient/command-help.ts index acc177c471..c04aaa6e0a 100644 --- a/packages/coding-agent/src/scient/command-help.ts +++ b/packages/coding-agent/src/scient/command-help.ts @@ -8,3 +8,7 @@ export const dictateHelp = { export const appsHelp = { description: "List the ChatGPT apps (Gmail, Google Drive and others) the ChatGPT sign-in has connected", } satisfies CommandMetadata; + +export const mcpHelp = { + description: "List the MCP servers a conversation loads, with their status, or sign in to one (for a host)", +} satisfies CommandMetadata; diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts new file mode 100644 index 0000000000..8622bd32d8 --- /dev/null +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -0,0 +1,332 @@ +/** + * `scient-agent mcp`: the MCP servers a conversation would load, for a host that shows them and + * signs in to them itself. Each subcommand prints JSON lines on stdout. + * + * - `list [--server ]` connects each server as a conversation would and prints + * `{ "servers": [{ "name", "status", "scope"?, "pluginId"?, "toolCount"?, "error"?, "canStartSignIn"? }] }`, + * status one of `connected`, `needs-auth`, `failed` or `pending`. + * - `login ` runs the server's browser sign-in: it prints `{ "type": "authorization_url", "url" }`, + * keeps the local callback listener open, then prints `{ "type": "completed" }`, or + * `{ "type": "failed", "message" }` and exits 1. SIGTERM cancels it. + * + * A failure before any of that prints `{ "error": { "message" } }` and exits 1. Every message is + * written here: a server's or authorization server's own text, which can echo a token, a code or a + * secret, never reaches the output. + */ +import { getMCPConfigPath } from "@oh-my-pi/pi-utils"; +import { Args, Command, Flags } from "@oh-my-pi/pi-utils/cli"; +import { initializeWithSettings } from "../capability"; +import type { SourceMeta } from "../capability/types"; +import { Settings } from "../config/settings"; +import { expandEnvVarsDeep } from "../discovery/helpers"; +import { loadAllMCPConfigs } from "../mcp/config"; +import { readMCPConfigFile } from "../mcp/config-writer"; +import { type MCPLoadResult, MCPManager } from "../mcp/manager"; +import { analyzeAuthError } from "../mcp/oauth-discovery"; +import { MCPOAuthClientIdRequiredError } from "../mcp/oauth-flow"; +import { + type FoundMcpServer, + MCPOAuthCancelledError, + McpReauthRefusalError, + reauthorizeMcpServer, +} from "../mcp/oauth-reauth"; +import { cfgMcpEnableProjectConfig } from "../mcp/settings"; +import type { MCPServerConfig } from "../mcp/types"; +import type { AuthStorage } from "../session/auth-storage"; +import { discoverAuthStorage } from "../sdk"; +import { mcpHelp } from "./command-help"; +import { boundSignInNeedsLocalSecret, canSignIn } from "./mcp-sign-in-pickup"; + +/** How long `list` waits for servers to answer, matching a conversation's startup window. */ +const LIST_WAIT_MS = 15_000; + +export interface McpServerStatusLine { + name: string; + status: "connected" | "needs-auth" | "failed" | "pending"; + scope?: "user" | "project"; + pluginId?: string; + toolCount?: number; + error?: string; + canStartSignIn?: true; +} + +let output: Promise = Promise.resolve(); + +/** Writes one JSON line after the ones before it; `Bun.write` resolves once every byte reached stdout. */ +function print(value: unknown): Promise { + output = output.then(() => Bun.write(Bun.stdout, `${JSON.stringify(value)}\n`)); + return output; +} + +/** Waits until everything printed has reached stdout; a write that failed makes the exit nonzero. */ +async function flushStdout(): Promise { + try { + await output; + } catch { + process.exitCode = 1; + } +} + +/** A connection failure in words written here: the HTTP status at most, never the server's text. */ +function connectionFailure(config: MCPServerConfig | undefined, failure: string): string { + const status = /^HTTP (\d{3})\b/.exec(failure)?.[1]; + if (status) return `The server answered HTTP ${status}.`; + if (/ENOTFOUND|ECONNREFUSED|ECONNRESET|ETIMEDOUT|EAI_AGAIN|timed? ?out/i.test(failure)) { + return "The server could not be reached."; + } + if (config?.type === "stdio" || config?.type === undefined) return "The server's process did not start."; + return "The server could not be connected."; +} + +/** One server's status once the startup window closed, for `list`. */ +export function serverStatusLine( + manager: MCPManager, + name: string, + loaded: MCPLoadResult, + authStorage?: AuthStorage, +): McpServerStatusLine { + const source = manager.getSource(name); + const config = manager.getServerConfig(name); + const base: McpServerStatusLine = { + name, + status: "pending", + ...(source && { scope: source.level === "project" ? "project" : "user" }), + ...(source?.pluginName && { pluginId: source.pluginName }), + }; + if (loaded.connectedServers.includes(name)) { + const toolCount = manager.getConnection(name)?.tools?.length ?? 0; + return { ...base, status: "connected", ...(toolCount > 0 && { toolCount }) }; + } + const failure = loaded.errors.get(name); + if (failure === undefined) return base; + const url = config && (config.type === "http" || config.type === "sse") ? config.url : undefined; + if (url && config && analyzeAuthError(new Error(failure), url).requiresAuth) { + const signIn = + canSignIn(config) && + addressIsPublic(url) && + !(authStorage && boundSignInNeedsLocalSecret(config, authStorage)); + return { + ...base, + status: "needs-auth", + error: "Sign-in needed.", + ...(signIn && { canStartSignIn: true as const }), + }; + } + return { ...base, status: "failed", error: connectionFailure(config, failure) }; +} + +/** A sign-in failure in words written here; the engine's own refusal text can quote a server's arguments. */ +export function loginFailureMessage(name: string, bound: boolean, error: unknown): string { + if (error instanceof McpReauthRefusalError) return `${name} cannot be signed in to with a browser from here.`; + if (error instanceof MCPOAuthCancelledError) return "The sign-in was cancelled."; + if (error instanceof MCPOAuthClientIdRequiredError) { + return bound + ? `${name} lets only the apps it has approved sign in, and it does not accept Scient Agent yet.` + : `${name} needs an OAuth client ID configured before Scient Agent can sign in.`; + } + const message = error instanceof Error ? error.message : ""; + if (message.startsWith("OAuth flow timed out")) return "The sign-in was not finished in time. Try again."; + const status = /^Token exchange failed: (\d{3})\b/.exec(message)?.[1]; + if (status) return `${name}'s sign-in server refused the sign-in (HTTP ${status}). Try again.`; + return `Signing in to ${name} did not complete. Try again.`; +} + +export default class Mcp extends Command { + static description = mcpHelp.description; + static args = { + action: Args.string({ description: "list or login", required: true, options: ["list", "login"] }), + server: Args.string({ description: "Server name (login)" }), + }; + static flags = { + server: Flags.string({ description: "List only this server" }), + }; + static examples = ["scient-agent mcp list", "scient-agent mcp login 'figma@openai-curated:figma'"]; + + async run(): Promise { + const { args, flags } = await this.parse(Mcp); + const cwd = process.cwd(); + const settings = await Settings.loadReadOnly(); + // Discovery honours the user's provider switches, as a conversation's does. + const releaseSettings = initializeWithSettings(settings); + const authStorage = await discoverAuthStorage(undefined, { settings }); + const manager = new MCPManager(cwd, null); + manager.setAuthStorage(authStorage); + const abort = new AbortController(); + const onSignal = (): void => abort.abort(); + process.once("SIGTERM", onSignal); + process.once("SIGINT", onSignal); + try { + const only = args.action === "login" ? args.server : flags.server; + if (args.action === "login" && !only) throw new McpReauthRefusalError("Name the server to sign in to."); + const loaded = await loadAllMCPConfigs(cwd, { enableProjectConfig: cfgMcpEnableProjectConfig.get(settings) }); + const configs = only + ? Object.fromEntries(Object.entries(loaded.configs).filter(([name]) => name === only)) + : loaded.configs; + if (args.action === "login") + await this.#login(only!, configs, loaded.sources, manager, authStorage, abort, cwd); + else await this.#list(configs, loaded.sources, manager, authStorage); + } catch (error) { + const message = + error instanceof McpReauthRefusalError ? error.message : "Scient Agent could not read its MCP servers."; + print({ error: { message } }); + process.exitCode = 1; + } finally { + process.off("SIGTERM", onSignal); + process.off("SIGINT", onSignal); + await manager.disconnectAll().catch(() => {}); + authStorage.close(); + releaseSettings(); + } + // The startup window's timer outlives a startup that settled sooner; once the output is out, nothing is left to do. + await flushStdout(); + process.exit(); + } + + async #list( + configs: Record, + sources: Record, + manager: MCPManager, + authStorage: AuthStorage, + ): Promise { + const loaded = await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); + print({ servers: Object.keys(configs).map(name => serverStatusLine(manager, name, loaded, authStorage)) }); + } + + async #login( + name: string, + configs: Record, + sources: Record, + manager: MCPManager, + authStorage: AuthStorage, + abort: AbortController, + cwd: string, + ): Promise { + const config = configs[name]; + const source = sources[name]; + if (!config) throw new McpReauthRefusalError(`No MCP server named "${name}" is configured.`); + // Cases a browser sign-in cannot serve, refused here in words written here, before any connection. + if (config.enabled === false) throw new McpReauthRefusalError(`${name} is turned off.`); + if (!canSignIn(config)) { + throw new McpReauthRefusalError( + `${name} does not sign in with a browser: it runs on this computer or uses its own key.`, + ); + } + if (boundSignInNeedsLocalSecret(config, authStorage)) { + throw new McpReauthRefusalError( + `${name} needs its client secret at every refresh, which the shared sign-in service cannot supply.`, + ); + } + // A key in the server's own address would reach a sign-in server as its resource, even from a refresh. + if (!addressIsPublic(urlOf(config))) { + throw new McpReauthRefusalError( + `${name}'s address carries values of its own, which Scient Agent does not send to a sign-in page.`, + ); + } + // Registers the definition the way a conversation does, so the sign-in uses the one it selected. + await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); + const found = await selectedServer(name, config, source, manager, cwd); + if (!found) { + throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); + } + const bound = source?.provider === CODEX_PLUGINS_PROVIDER; + let withheld = false; + try { + await reauthorizeMcpServer(name, found, { + authStorage, + signal: abort.signal, + cwd, + mcpManager: manager, + silent: true, + onAuth: info => { + if (!authorizationUrlIsPublic(info.url)) { + withheld = true; + abort.abort(); + return; + } + print({ type: "authorization_url", url: info.url }); + }, + onProgress: () => {}, + }); + print({ type: "completed" }); + } catch (error) { + const message = withheld + ? `${name}'s address carries values of its own, which Scient Agent does not send to a sign-in page.` + : loginFailureMessage(name, bound, error); + print({ type: "failed", message }); + process.exitCode = 1; + } + } +} + +const CODEX_PLUGINS_PROVIDER = "codex-plugins"; + +/** Whether an address carries no user name, password or query, any of which could hold a key. */ +function addressIsPublic(address: string | undefined): boolean { + if (address === undefined) return true; + try { + const url = new URL(address); + return !url.username && !url.password && !url.search; + } catch { + return false; + } +} + +/** + * Whether a sign-in page may be shown: it carries no user name or password, and the resource it + * names (the server's address, when discovery gave no other) no user name, password or query. + * The page's own query is the sign-in server's (OAuth parameters, its own settings) and is kept. + */ +export function authorizationUrlIsPublic(authorizationUrl: string): boolean { + try { + const page = new URL(authorizationUrl); + if (page.username || page.password) return false; + return page.searchParams.getAll("resource").every(resource => addressIsPublic(resource)); + } catch { + return false; + } +} + +const transportOf = (config: MCPServerConfig): string => + config.type ?? ("url" in config && typeof config.url === "string" ? "http" : "stdio"); +const urlOf = (config: MCPServerConfig): string | undefined => + "url" in config && typeof config.url === "string" ? config.url : undefined; + +/** + * The definition discovery selected for `name`, as the sign-in needs it. An imported Codex plugin + * server is answered from the manager, never a same-named config file entry that lost to it. An + * ordinary server comes from the very file discovery read it from, so a sign-in writes back where + * `/mcp reauth` would, and only when that entry, expanded, is the selected one (same transport and + * URL); a definition no readable config file holds (a plugin's) signs in as a discovered server. + */ +export async function selectedServer( + name: string, + config: MCPServerConfig, + source: SourceMeta | undefined, + manager: MCPManager, + cwd: string, +): Promise { + const selected = manager.getServerConfig(name) ?? config; + const discovered: FoundMcpServer = { + filePath: getMCPConfigPath("user", cwd), + scope: "user", + config: selected, + discovered: true, + }; + if (source?.provider === CODEX_PLUGINS_PROVIDER) return discovered; + if (!source) return null; + const entry = await readMCPConfigFile(source.path).then( + file => file.mcpServers?.[name], + () => undefined, + ); + if (!entry) return discovered; + const expanded = expandEnvVarsDeep(entry); + if (transportOf(expanded) !== transportOf(selected) || urlOf(expanded) !== urlOf(selected)) return null; + // The raw entry keeps its placeholders for write-back; a URL-only entry (Cursor, Claude) takes + // the transport discovery inferred, as the sign-in needs it. + return { + filePath: source.path, + scope: source.level === "project" ? "project" : "user", + config: (entry.type ? entry : { ...entry, type: transportOf(selected) }) as MCPServerConfig, + discovered: false, + }; +} diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts new file mode 100644 index 0000000000..00fe6b244f --- /dev/null +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -0,0 +1,154 @@ +import { logger } from "@oh-my-pi/pi-utils"; +import type { MCPManager } from "../mcp/manager"; +import { hasMcpAuthorizationHeader, lookupMcpOAuthCredential } from "../mcp/oauth-credentials"; +import { isMcpOAuthServerCredentialId } from "../mcp/oauth-flow"; +import type { MCPServerConfig } from "../mcp/types"; +import type { AuthStorage } from "../session/auth-storage"; + +/** Whether a browser sign-in can serve this server: http/sse, no token of its own. */ +export function canSignIn(config: MCPServerConfig): boolean { + if (config.type !== "http" && config.type !== "sse") return false; + if (hasMcpAuthorizationHeader(config)) return false; + // A config file can hold `auth: null`, which means no auth block, as the credential lookup reads it. + const auth = config.auth ?? undefined; + // A server bound to its own sign-in (an imported Codex plugin server) signs in only with OAuth. + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return auth?.type === "oauth"; + return auth === undefined || auth.type === "oauth"; +} + +/** + * Whether an imported server's sign-in would need a client secret at every refresh that the + * sign-in store cannot give: a bound sign-in keeps no secret, and a broker refreshes without the + * server's definition, so such a sign-in could not be refreshed and is not offered. + */ +export function boundSignInNeedsLocalSecret(config: MCPServerConfig, authStorage: AuthStorage): boolean { + return ( + isMcpOAuthServerCredentialId(config.auth?.credentialId) && + typeof config.oauth?.clientSecret === "string" && + authStorage.credentials.heldByBroker + ); +} + +/** How long a prompt waits for a pickup; the rest finishes in the background. */ +const PROMPT_BUDGET_MS = 1_500; +/** A broker's sign-ins are re-read at most this often, since each read is a request to it. */ +const BROKER_REREAD_MS = 10_000; + +/** + * Connects the MCP servers a sign-in made elsewhere (the host's Sign in, `scient-agent mcp login`) + * now serves. A session reads sign-ins once, so a server that needed one stays disconnected until + * something reconnects it; a host calls {@link beforePrompt} before each prompt. + * + * Only a disconnected server that signs in with a browser is considered, and only when its stored + * sign-in differs from the one it started with or last tried here, so a refused sign-in is not + * retried every prompt. Each server reconnects on its own and its tools are published as soon as it + * connects, so a stalled server holds up no other; a prompt waits for a pickup only briefly. + */ +export class McpSignInPickup { + readonly #tried = new Map(); + /** Reconnects under way, by server, each settling once its tools are published; later prompts join them. */ + readonly #connecting = new Map>(); + #reread: Promise | undefined; + #lastReread = Number.NEGATIVE_INFINITY; + #published: Promise = Promise.resolve(); + + constructor( + private readonly manager: MCPManager, + private readonly authStorage: AuthStorage, + private readonly onConnected: () => Promise, + ) {} + + /** Records the sign-ins the session started with: its own startup already connected with them. */ + seed(): void { + for (const name of this.manager.getAllServerNames()) { + const config = this.manager.getServerConfig(name); + const signature = config && canSignIn(config) ? this.#signature(config) : undefined; + if (signature) this.#tried.set(name, signature); + } + } + + /** Starts a pickup and waits for it at most a short while; reconnects go on in the background. Never throws. */ + async beforePrompt(budgetMs = PROMPT_BUDGET_MS): Promise { + const { promise, resolve } = Promise.withResolvers(); + const timer = setTimeout(resolve, budgetMs); + try { + await Promise.race([this.#pickUp(), promise]); + } finally { + clearTimeout(timer); + } + } + + /** The stored sign-in a server would use; a malformed definition counts as none, never as a failure. */ + #signature(config: MCPServerConfig): string | undefined { + try { + const lookup = lookupMcpOAuthCredential(this.authStorage, config); + return lookup ? `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}` : undefined; + } catch { + return undefined; + } + } + + /** Re-reads the sign-in store once for concurrent pickups; a broker at most every few seconds. */ + #rereadSignIns(): Promise { + // A read under way is joined, so a later prompt waits for the sign-in it may bring. + if (this.#reread) return this.#reread; + if (this.authStorage.credentials.heldByBroker && performance.now() - this.#lastReread < BROKER_REREAD_MS) { + return Promise.resolve(true); + } + this.#reread = (async () => { + this.#lastReread = performance.now(); + try { + await this.authStorage.credentials.revalidate(); + return true; + } catch (err) { + logger.warn("Re-reading sign-ins for MCP servers failed", { err }); + return false; + } finally { + this.#reread = undefined; + } + })(); + return this.#reread; + } + + async #pickUp(): Promise { + const underWay = [...this.#connecting.values()]; + const candidates = this.manager.getAllServerNames().flatMap(name => { + if (this.#connecting.has(name) || this.manager.getConnectionStatus(name) !== "disconnected") return []; + const config = this.manager.getServerConfig(name); + return config && canSignIn(config) ? [{ name, config }] : []; + }); + const started = + candidates.length > 0 && (await this.#rereadSignIns()) + ? candidates.flatMap(({ name, config }) => { + // Another prompt sharing this read may have started it: wait for that one. + const existing = this.#connecting.get(name); + if (existing) return [existing]; + const signature = this.#signature(config); + if (!signature || this.#tried.get(name) === signature) return []; + this.#tried.set(name, signature); + return [this.#reconnect(name)]; + }) + : []; + await Promise.all([...underWay, ...started]); + } + + /** Reconnects one server and publishes its tools once it connects. Never throws. */ + #reconnect(name: string): Promise { + const attempt = (async () => { + try { + await this.manager.reconnectServer(name, { manual: true }); + if (this.manager.getConnectionStatus(name) !== "connected") return; + this.#published = this.#published.then(() => + this.onConnected().catch(err => logger.warn("Refreshing tools after an MCP sign-in failed", { err })), + ); + await this.#published; + } catch (err) { + logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + } finally { + this.#connecting.delete(name); + } + })(); + this.#connecting.set(name, attempt); + return attempt; + } +} diff --git a/packages/coding-agent/src/scient/runtime-info.ts b/packages/coding-agent/src/scient/runtime-info.ts index aa50b84928..e1008b28fd 100644 --- a/packages/coding-agent/src/scient/runtime-info.ts +++ b/packages/coding-agent/src/scient/runtime-info.ts @@ -24,6 +24,11 @@ export interface RuntimeInfo { * prompt, so a host checks this list instead of guessing from the version. */ hostCommands: string[]; + /** + * Plugin discovery sources a host may name in `SCIENT_AGENT_ENABLE_SOURCES`. An executable + * that does not list a source has no reader for it, so a host checks this list before it offers it. + */ + enableSources: string[]; } export function formatRuntimeInfo(): string { @@ -33,7 +38,8 @@ export function formatRuntimeInfo(): string { upstream: UPSTREAM, rpcProtocolVersions: [1, 2], buildId: BUILD_ID, - hostCommands: ["apps", "dictate"], + hostCommands: ["apps", "dictate", "mcp"], + enableSources: ["claude-plugins", "codex-plugins"], }; return `${JSON.stringify(info)}\n`; } diff --git a/packages/coding-agent/src/slash-commands/helpers/mcp.ts b/packages/coding-agent/src/slash-commands/helpers/mcp.ts index cfd978dcd6..23229d3ad3 100644 --- a/packages/coding-agent/src/slash-commands/helpers/mcp.ts +++ b/packages/coding-agent/src/slash-commands/helpers/mcp.ts @@ -17,6 +17,9 @@ import { reauthorizeMcpServer, } from "../../mcp/oauth-reauth"; import type { MCPServerConfig, MCPServerConnection } from "../../mcp/types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { withTrustedMcpAuth } from "../../mcp/oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import { parseCommandArgs } from "../../utils/command-args"; import { openPath } from "../../utils/open"; import type { ParsedSlashCommand, SlashCommandResult, SlashCommandRuntime } from "../types"; @@ -150,7 +153,10 @@ async function withPreparedMcpConnection( // Without this, `/mcp test|resources|prompts` silently fails for any // server saved by the TUI/reauth path. manager.setAuthStorage(runtime.session.modelRegistry.authStorage); - const resolvedConfig = await manager.prepareConfig(config); + // scient(SA-CODEX-PLUGINS): begin - these definitions come from config files, which never hold an + // imported server's own sign-in: a server-bound id there is dropped. + const resolvedConfig = await manager.prepareConfig(withTrustedMcpAuth(name, config, undefined)); + // scient(SA-CODEX-PLUGINS): end connection = await connectToServer(name, resolvedConfig); return await fn(connection); } finally { diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts new file mode 100644 index 0000000000..954ddb47d6 --- /dev/null +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -0,0 +1,463 @@ +import { Database } from "bun:sqlite"; +import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test"; +import * as path from "node:path"; +import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; +import { initializeWithSettings } from "@oh-my-pi/pi-coding-agent/capability"; +import { refreshBrokerOAuthCredential } from "@oh-my-pi/pi-coding-agent/cli/auth-broker-cli"; +import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; +import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; +import { loadAllMCPConfigs } from "@oh-my-pi/pi-coding-agent/mcp/config"; +import { readDisabledServers } from "@oh-my-pi/pi-coding-agent/mcp/config-writer"; +import { MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; +import { + lookupMcpOAuthCredential, + lookupMcpOAuthCredentialForServer, + refreshStoredManagedMcpOAuthCredential, +} from "@oh-my-pi/pi-coding-agent/mcp/oauth-credentials"; +import * as oauthFlow from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; +import { + McpReauthRefusalError, + reauthorizeMcpServer, + reconnectMcpManagerServer, +} from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; +import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; +import { MCPCommandController } from "@oh-my-pi/pi-coding-agent/modes/controllers/mcp-command-controller"; +import { initTheme } from "@oh-my-pi/pi-tui/theme"; +import { getAgentDir, getMCPConfigPath, getProjectDir, setAgentDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; +import { createInteractiveModeContext, createMcpManagerStub } from "../helpers/interactive-mode-context"; +import { restoreEnvValue } from "../helpers/settings-test-state"; + +const SERVER_URL = "https://mcp.example.test/mcp"; +const NAME = "tools@market:api"; +const placeholder = "$" + "{CODEX_PLUGIN_OAUTH_TEST}"; +const token = { type: "oauth" as const, access: "a", refresh: "r", expires: Date.now() + 3_600_000 }; +const boundConfig = (): MCPServerConfig => ({ + type: "http", + url: SERVER_URL, + headerPolicy: "origin-locked", + auth: { type: "oauth", credentialId: oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL) }, +}); + +beforeAll(() => initTheme()); + +describe("an imported Codex plugin server's own sign-in", () => { + const stores: AuthStorage[] = []; + const freshStorage = () => { + const storage = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + stores.push(storage); + return storage; + }; + const saved = process.env.CODEX_PLUGIN_OAUTH_TEST; + + afterEach(() => { + while (stores.length > 0) stores.pop()?.close(); + vi.restoreAllMocks(); + restoreEnvValue("CODEX_PLUGIN_OAUTH_TEST", saved); + }); + + it("never takes another server's sign-in at the same URL", async () => { + const storage = freshStorage(); + const token = { type: "oauth" as const, access: "a", refresh: "r", expires: Date.now() + 60_000 }; + await storage.credentials.set(oauthFlow.mcpOAuthCredentialId(SERVER_URL), { ...token, access: "write-scope" }); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const auth = { type: "oauth" as const, credentialId }; + expect(lookupMcpOAuthCredentialForServer(storage, auth, SERVER_URL)).toBeUndefined(); + await storage.credentials.set(credentialId, { ...token, access: "own" }); + expect(lookupMcpOAuthCredentialForServer(storage, auth, SERVER_URL)?.credential.access).toBe("own"); + expect(oauthFlow.mcpOAuthServerUrlFromCredentialId(credentialId)).toBe(SERVER_URL); + }); + + it("signs in with the plugin's literal values, configured scope and resource, under its own id", async () => { + // A value an `.env` file could supply; Codex never resolves a plugin header from it. + restoreEnvValue("CODEX_PLUGIN_OAUTH_TEST", "dotenv-secret"); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const config: MCPServerConfig = { + type: "http", + url: SERVER_URL, + headers: { "X-Team": placeholder }, + headerPolicy: "origin-locked", + auth: { type: "oauth", credentialId, resource: "https://resource.example.test/" }, + // The plugin asks for no scopes. + oauth: { scope: "" }, + }; + const probed: MCPServerConfig[] = []; + vi.spyOn(mcpClient, "connectToServer").mockImplementation((async (_name: string, probe: MCPServerConfig) => { + probed.push(probe); + throw new Error( + 'HTTP 401: {"authorization_url":"https://auth.example.test/authorize?scope=admin","token_url":"https://auth.example.test/token","resource":"https://discovered.example.test/","scopes":"admin"}', + ); + }) as never); + const flows: { scopes?: string }[] = []; + vi.spyOn(oauthFlow.MCPOAuthFlow.prototype, "login").mockImplementation( + async function (this: { config: { scopes?: string } }) { + flows.push(this.config); + return { access: "fresh", refresh: "fresh-refresh", expires: Date.now() + 3_600_000 }; + }, + ); + const storage = freshStorage(); + using temp = TempDir.createSync("@scient-codex-plugins-oauth-"); + const outcome = await reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config, discovered: true }, + { authStorage: storage, silent: true }, + ); + expect(probed[0]).toMatchObject({ url: SERVER_URL, headers: { "X-Team": placeholder } }); + expect(flows[0]?.scopes).toBeUndefined(); + // Discovery's `scope=admin` is dropped from the authorization URL. + expect(storage.credentials.get(credentialId)).toMatchObject({ + access: "fresh", + resource: "https://resource.example.test/", + authorizationUrl: "https://auth.example.test/authorize", + }); + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toBeUndefined(); + // Found by its fixed id: nothing is written to a config file, and the auth block stays. + expect(outcome).toMatchObject({ persisted: false, credentialId, config: { auth: { credentialId } } }); + expect(await Bun.file(path.join(temp.path(), "mcp.json")).exists()).toBe(false); + }); + + it("signs in again with the secret configured now, never one stored with an earlier sign-in", async () => { + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const storage = freshStorage(); + // An earlier build's sign-in kept the secret the plugin has since removed. + await storage.credentials.set(credentialId, { + ...token, + tokenUrl: "https://auth.example.test/token", + clientId: "client-a", + clientSecret: "retired", + } as oauthFlow.MCPStoredOAuthCredential); + vi.spyOn(mcpClient, "connectToServer").mockRejectedValue( + new Error( + 'HTTP 401: {"authorization_url":"https://auth.example.test/authorize","token_url":"https://auth.example.test/token"}', + ), + ); + const flows: { clientSecret?: string; rejectRedirects?: boolean }[] = []; + vi.spyOn(oauthFlow.MCPOAuthFlow.prototype, "login").mockImplementation( + async function (this: { config: { clientSecret?: string; rejectRedirects?: boolean } }) { + flows.push(this.config); + return { access: "fresh", refresh: "fresh-refresh", expires: Date.now() + 3_600_000 }; + }, + ); + using temp = TempDir.createSync("@scient-codex-plugins-resign-"); + const config: MCPServerConfig = { ...boundConfig(), oauth: { clientId: "client-a" } }; + await reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config, discovered: true }, + { authStorage: storage, silent: true }, + ); + expect(flows).toHaveLength(1); + expect(flows[0]?.clientSecret).toBeUndefined(); + // Its code exchange follows no redirect, which could carry the code, verifier and secret elsewhere. + expect(flows[0]?.rejectRedirects).toBe(true); + // The new sign-in keeps no secret either. + expect(storage.credentials.get(credentialId)).toMatchObject({ access: "fresh", clientId: "client-a" }); + expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); + }); + + it("reconnects on its own sign-in, and never stands in for another entry at its URL", async () => { + const updateServerConfig = vi.fn(() => undefined); + const manager = createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + updateServerConfig, + reconnectServer: vi.fn(async () => null), + getConnectionStatus: vi.fn(() => "connected" as const), + }); + expect(await reconnectMcpManagerServer(manager, NAME, boundConfig())).toBe("connected"); + expect(updateServerConfig).toHaveBeenCalledWith(NAME, boundConfig()); + // A definition without that sign-in is another server, even at the same URL. + expect(await reconnectMcpManagerServer(manager, NAME, { type: "http", url: SERVER_URL })).toBeUndefined(); + }); + + it("signs out of its own sign-in alone", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + await storage.credentials.set(oauthFlow.mcpOAuthCredentialId(SERVER_URL), { ...token, access: "native" }); + using temp = TempDir.createSync("@scient-codex-plugins-unauth-"); + const previous = getProjectDir(); + setProjectDir(temp.path()); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle(`/mcp unauth ${NAME}`); + expect(ctx.showError).not.toHaveBeenCalled(); + } finally { + setProjectDir(previous); + } + expect(storage.credentials.get(credentialId)).toBeUndefined(); + // Another server's sign-in at the same URL stays. + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toMatchObject({ access: "native" }); + }); + + it("asks for a fresh sign-in once the plugin configures another confidential OAuth client", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const stored: oauthFlow.MCPStoredOAuthCredential = { ...token, clientId: "client-a", clientSecret: "s" }; + await storage.credentials.set(credentialId, stored); + const withClient = (clientId: string, clientSecret?: string): MCPServerConfig => ({ + ...boundConfig(), + oauth: { clientId, ...(clientSecret !== undefined && { clientSecret }) }, + }); + expect(lookupMcpOAuthCredential(storage, withClient("client-a", "s"))?.credentialId).toBe(credentialId); + // Client A's token is neither sent nor refreshed for client B, nor for an ID that differs only in spaces. + expect(lookupMcpOAuthCredential(storage, withClient("client-b", "s"))).toBeUndefined(); + expect(lookupMcpOAuthCredential(storage, withClient("client-a ", "s"))).toBeUndefined(); + // The secret is not part of the match: refresh sends the one configured now (tested below). + expect(lookupMcpOAuthCredential(storage, withClient("client-a", "rotated"))?.credentialId).toBe(credentialId); + // Codex holds only a confidential client (one with a secret) to its ID. + expect(lookupMcpOAuthCredential(storage, withClient("client-b"))?.credentialId).toBe(credentialId); + // A server with no sign-in of its own is looked up as before, whatever its client settings hold. + const urlKeyed = oauthFlow.mcpOAuthCredentialId(SERVER_URL); + await storage.credentials.set(urlKeyed, { + ...token, + clientId: "registered", + } as oauthFlow.MCPStoredOAuthCredential); + const ordinary = (clientId: unknown) => + ({ type: "http", url: SERVER_URL, oauth: { clientId, clientSecret: "s" } }) as never; + expect(lookupMcpOAuthCredential(storage, ordinary("configured"))?.credentialId).toBe(urlKeyed); + expect(lookupMcpOAuthCredential(storage, ordinary(7))?.credentialId).toBe(urlKeyed); + }); + + it("refreshes with the secret the plugin configures now, never a stored or retired one", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const sent: (string | null)[] = []; + const redirects: (string | undefined)[] = []; + vi.spyOn(globalThis, "fetch").mockImplementation((async (_url: string, init?: RequestInit) => { + sent.push(new URLSearchParams(String(init?.body)).get("client_secret")); + redirects.push(init?.redirect); + return Response.json({ access_token: "fresh", refresh_token: "r2", expires_in: 3600 }); + }) as never); + const stored = (): oauthFlow.MCPStoredOAuthCredential => ({ + ...token, + tokenUrl: "https://auth.example.test/token", + clientId: "client-a", + // A row an earlier build kept with the secret the plugin has since retired. + clientSecret: "retired", + }); + await storage.credentials.set(credentialId, stored()); + await refreshStoredManagedMcpOAuthCredential(storage, credentialId, { + clientSecret: "current", + forceRefresh: true, + }); + // The plugin no longer configures a secret: none is sent, as in Codex. + await storage.credentials.set(credentialId, stored()); + const { credential } = await refreshStoredManagedMcpOAuthCredential(storage, credentialId, { + forceRefresh: true, + }); + // The broker, which has no definition, refreshes the same row without the stored secret too. + await refreshBrokerOAuthCredential(credentialId, stored()); + // An ordinary server's sign-in keeps its stored secret and the default redirect handling. + await refreshBrokerOAuthCredential(oauthFlow.mcpOAuthCredentialId(SERVER_URL), stored()); + expect(sent).toEqual(["current", null, null, "retired"]); + // A server-bound exchange follows no redirect, which could carry the token and secret elsewhere. + expect(redirects).toEqual(["error", "error", "error", undefined]); + // Nothing keeps a secret for the next refresh. + expect(credential?.clientSecret).toBeUndefined(); + expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); + }); + + it("binds a definition only by the source supplied with it, never one left registered", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, { ...token, access: "own" }); + const probed: MCPServerConfig[] = []; + vi.spyOn(mcpClient, "connectToServer").mockImplementation((async (_name: string, probe: MCPServerConfig) => { + probed.push(probe); + throw new Error("unreachable"); + }) as never); + using temp = TempDir.createSync("@scient-codex-plugins-provenance-"); + const manager = new MCPManager(temp.path()); + manager.setAuthStorage(storage); + const imported = { + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/p/.mcp.json", + level: "user" as const, + }; + // The imported server fails to connect, which leaves its source registered under the name. + await manager.connectServers({ [NAME]: boundConfig() }, { [NAME]: imported }); + // A definition arriving without a source (a config file's) names the same sign-in. + await manager.connectServers({ [NAME]: boundConfig() }, {}); + expect(probed).toHaveLength(2); + expect(probed[0]).toMatchObject({ headers: { Authorization: "Bearer own" } }); + expect(probed[1]).not.toMatchObject({ headers: { Authorization: expect.anything() } }); + await manager.disconnectAll(); + }); + + it("leaves a server's own token in charge, as Codex does", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + // A configured Authorization header wins over a stored sign-in. + expect( + lookupMcpOAuthCredential(storage, { + type: "http", + url: SERVER_URL, + headers: { Authorization: "Bearer service" }, + auth: { type: "oauth", credentialId }, + }), + ).toBeUndefined(); + // A server declaring bearer_token_env_var is bound as a key: no sign-in answers it, and none starts. + const keyed: MCPServerConfig = { ...boundConfig(), auth: { type: "apikey", credentialId } }; + expect(lookupMcpOAuthCredential(storage, keyed)).toBeUndefined(); + using temp = TempDir.createSync("@scient-codex-plugins-keyed-"); + await expect( + reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config: keyed, discovered: true }, + { authStorage: storage, silent: true }, + ), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + }); + + it("gives a sign-in to no definition but its own server's", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + // Repointed to another URL, the id answers nothing. + expect( + lookupMcpOAuthCredentialForServer(storage, { type: "oauth", credentialId }, "https://other.example.test/mcp"), + ).toBeUndefined(); + using temp = TempDir.createSync("@scient-codex-plugins-claim-"); + const filePath = path.join(temp.path(), "mcp.json"); + // A config-file definition, or one under another name, cannot sign in under it. + for (const [name, discovered] of [ + [NAME, false], + ["copy", true], + ] as const) { + await expect( + reauthorizeMcpServer( + name, + { filePath, scope: "user", config: boundConfig(), discovered }, + { authStorage: storage, silent: true }, + ), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + } + // Nor sign it out. + const previous = getProjectDir(); + setProjectDir(temp.path()); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle("/mcp unauth copy"); + expect(ctx.showError).toHaveBeenCalledWith(expect.stringContaining("belongs to another server")); + } finally { + setProjectDir(previous); + } + expect(storage.credentials.get(credentialId)).toMatchObject({ access: "a" }); + // Only the codex-plugins source binds a definition to a server's own sign-in, even under the id + // the definition's own name and URL give. + const ownId = oauthFlow.mcpOAuthServerCredentialId("imposter", SERVER_URL); + await Bun.write( + path.join(temp.path(), ".mcp.json"), + JSON.stringify({ + mcpServers: { + copy: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId } }, + imposter: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId: ownId } }, + // A malformed id belongs to no one; it must not stop its siblings from loading. + numeric: { type: "http", url: SERVER_URL, auth: { type: "apikey", credentialId: 5 } }, + }, + }), + ); + const release = initializeWithSettings(Settings.isolated()); + try { + const { configs } = await loadAllMCPConfigs(temp.path()); + expect(configs.copy?.auth?.credentialId).toBeUndefined(); + expect(configs.imposter?.auth?.credentialId).toBeUndefined(); + expect(configs.numeric).toMatchObject({ type: "http", url: SERVER_URL }); + } finally { + release(); + } + // Manual connections (/mcp test) apply the same rule to the definition they read. + expect(oauthFlow.withTrustedMcpAuth(NAME, boundConfig(), "codex-plugins").auth?.credentialId).toBe(credentialId); + expect(oauthFlow.withTrustedMcpAuth(NAME, boundConfig(), "mcp-json").auth?.credentialId).toBeUndefined(); + expect(oauthFlow.withTrustedMcpAuth("copy", boundConfig(), "codex-plugins").auth?.credentialId).toBeUndefined(); + }); + + it("toggles an imported server through the disabled-servers list", async () => { + using temp = TempDir.createSync("@scient-codex-plugins-toggle-"); + const previousProject = getProjectDir(); + const previousAgent = getAgentDir(); + setProjectDir(temp.path()); + setAgentDir(temp.join("agent")); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: freshStorage() } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle(`/mcp disable ${NAME}`); + expect(ctx.showError).not.toHaveBeenCalled(); + expect(await readDisabledServers(getMCPConfigPath("user", temp.path()))).toEqual([NAME]); + } finally { + setProjectDir(previousProject); + setAgentDir(previousAgent); + } + }); + + it("answers a live imported server's sign-in challenge for that server, not a same-named file entry", async () => { + using temp = TempDir.createSync("@scient-codex-plugins-challenge-"); + const previousProject = getProjectDir(); + const previousAgent = getAgentDir(); + setProjectDir(temp.path()); + setAgentDir(temp.join("agent")); + // A config-file entry of the same name, which a lookup by name alone would pick. + await Bun.write( + getMCPConfigPath("project", temp.path()), + JSON.stringify({ mcpServers: { [NAME]: { type: "http", url: SERVER_URL } } }), + ); + const connect = vi.spyOn(mcpClient, "connectToServer").mockRejectedValue(new Error("HTTP 401")); + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + // The live server names its own token, so no OAuth sign-in may start for it. + getServerConfig: vi.fn(() => ({ ...boundConfig(), auth: { type: "apikey" as const, credentialId } })), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + const challenge = { wwwAuthenticate: ['Bearer scope="read"'] }; + expect(await new MCPCommandController(ctx).handleMCPAuthChallenge(NAME, challenge)).toBeUndefined(); + } finally { + setProjectDir(previousProject); + setAgentDir(previousAgent); + } + // Refused before any probe: the file entry was never signed in. + expect(connect).not.toHaveBeenCalled(); + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts new file mode 100644 index 0000000000..6d18d29388 --- /dev/null +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -0,0 +1,1109 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test"; +import * as fs from "node:fs/promises"; +import * as os from "node:os"; +import * as path from "node:path"; +import { initializeWithSettings, loadCapability } from "@oh-my-pi/pi-coding-agent/capability"; +import { clearCache as clearFsCache } from "@oh-my-pi/pi-coding-agent/capability/fs"; +import type { MCPServer } from "@oh-my-pi/pi-coding-agent/capability/mcp"; +import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; +import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; +import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; +import { MCPTransportError } from "@oh-my-pi/pi-coding-agent/mcp/errors"; +import { resolveMCPRequestDeadline } from "@oh-my-pi/pi-coding-agent/mcp/timeout"; +import { MCPTool } from "@oh-my-pi/pi-coding-agent/mcp/tool-bridge"; +import { HttpTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/http"; +import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; +import type { MCPRequestOptions, MCPServerConnection, MCPTransport } from "@oh-my-pi/pi-coding-agent/mcp/types"; +import * as piUtils from "@oh-my-pi/pi-utils"; +import { restoreEnvValue } from "../helpers/settings-test-state"; +import "@oh-my-pi/pi-coding-agent/discovery"; + +const ENV_KEYS = [ + "SCIENT_AGENT_ENABLE_SOURCES", + "CODEX_HOME", + "CODEX_PLUGIN_TEST_TOKEN", + "CODEX_PLUGIN_TEST_PAT", + "CODEX_PLUGIN_TEST_SECRET", + "CODEX_PLUGIN_TEST_NEWLINE", +]; + +async function writeSkill(root: string, name: string): Promise { + const skillPath = path.join(root, "skills", name, "SKILL.md"); + await Bun.write(skillPath, `---\nname: ${name}\ndescription: ${name} skill\n---\nUse ${name}.\n`); + return skillPath; +} + +async function writeManifest(root: string, manifest: Record): Promise { + await Bun.write(path.join(root, ".codex-plugin", "plugin.json"), JSON.stringify(manifest)); +} + +describe("installed Codex plugins", () => { + let temp: piUtils.TempDir; + let settings: Settings; + let release: () => void; + let saved: Record; + let cwd: string; + let agentDir: string; + let researchRoot: string; + let researchSkill: string; + + beforeEach(async () => { + saved = Object.fromEntries(ENV_KEYS.map(key => [key, process.env[key]])); + for (const key of ENV_KEYS) restoreEnvValue(key, undefined); + settings = Settings.isolated(); + release = initializeWithSettings(settings); + temp = piUtils.TempDir.createSync("@scient-codex-plugins-"); + const home = temp.join("home"); + const codexHome = temp.join("codex-home"); + cwd = temp.join("project"); + agentDir = temp.join("agent"); + await fs.mkdir(cwd, { recursive: true }); + vi.spyOn(os, "homedir").mockReturnValue(home); + restoreEnvValue("CODEX_HOME", codexHome); + restoreEnvValue("CODEX_PLUGIN_TEST_TOKEN", "forwarded-value"); + restoreEnvValue("CODEX_PLUGIN_TEST_PAT", "pat-value"); + // The loader reads the environment Bun started with, which a test cannot change afterwards; + // the test's live environment stands in for it (the real one is tested in a fresh process). + vi.spyOn(piUtils, "getLaunchEnv").mockImplementation(() => ({ ...process.env }) as Record); + + await Bun.write( + path.join(codexHome, "config.toml"), + [ + '[plugins."research@market"]', + "enabled = true", + '[plugins."research@market".mcp_servers.quiet]', + "enabled = false", + '[plugins."paused@market"]', + "enabled = false", + '[plugins."browser@openai-bundled"]', + "enabled = true", + "", + ].join("\n"), + ); + const cache = path.join(codexHome, "plugins", "cache"); + const research = path.join(cache, "market", "research"); + researchRoot = path.join(research, "1.10.0"); + const olderRoot = path.join(research, "1.2.0"); + for (const root of [researchRoot, olderRoot]) { + await writeManifest(root, { + name: "research", + skills: "./skills/", + mcpServers: "./.mcp.json", + apps: "./.app.json", + interface: { displayName: "Research" }, + }); + } + researchSkill = await writeSkill(researchRoot, "lit-review"); + await writeSkill(olderRoot, "stale-review"); + // Codex ignores links in the cache (bundled plugins ship a `latest` link to a version). + await fs.symlink(olderRoot, path.join(research, "latest"), "dir"); + await Bun.write( + path.join(researchRoot, ".mcp.json"), + JSON.stringify({ + mcpServers: { + papers: { + command: "./bin/papers", + args: ["--stdio"], + cwd: ".", + env_vars: ["CODEX_PLUGIN_TEST_TOKEN"], + tool_timeout_sec: 30, + }, + tracker: { + type: "http", + url: "https://mcp.example.test/mcp", + bearer_token_env_var: "CODEX_PLUGIN_TEST_PAT", + }, + quiet: { command: "node", args: ["./quiet.mjs"] }, + messages: { command: "./bin/computer-use-client-launcher", args: ["messages", "mcp"], cwd: "." }, + }, + }), + ); + await Bun.write( + path.join(researchRoot, ".app.json"), + JSON.stringify({ apps: { research: { id: "asdk_app_x" } } }), + ); + + const paused = path.join(cache, "market", "paused", "1.0.0"); + await writeManifest(paused, { name: "paused", skills: "./skills/" }); + await writeSkill(paused, "paused-skill"); + + const browser = path.join(cache, "openai-bundled", "browser", "26.1117.41037"); + await writeManifest(browser, { name: "browser", skills: "./skills/" }); + await writeSkill(browser, "control-in-app-browser"); + // The loader reports symlink-resolved paths (macOS temp folders live under /private). + researchRoot = await fs.realpath(researchRoot); + researchSkill = await fs.realpath(researchSkill); + clearFsCache(); + }); + + afterEach(() => { + release(); + for (const key of ENV_KEYS) restoreEnvValue(key, saved[key]); + vi.restoreAllMocks(); + clearFsCache(); + temp.removeSync(); + }); + + const skills = async () => + (await loadCapability("skills", { cwd, agentDir })).items.filter( + skill => skill._source.provider === "codex-plugins", + ); + const servers = async () => + (await loadCapability("mcps", { cwd, agentDir })).items.filter( + server => server._source.provider === "codex-plugins", + ); + + it("loads nothing until the host or settings enable codex-plugins", async () => { + cfgEnabledProviders.set(settings, ["codex", "claude"]); + expect(await skills()).toEqual([]); + expect(await servers()).toEqual([]); + }); + + it("loads the active version's skills and MCP servers with package-relative paths", async () => { + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + + // Only the enabled, installed plugin's highest version: not the disabled plugin, the + // host-only browser plugin, the older version or the `latest` link to it. + expect((await skills()).map(skill => [skill.name, skill.path])).toEqual([["lit-review", researchSkill]]); + + const byName = new Map((await servers()).map(server => [server.name, server])); + // The Computer Use client launcher runs only inside Codex. + expect([...byName.keys()].sort()).toEqual(["research@market:papers", "research@market:tracker"]); + expect(byName.get("research@market:papers")).toMatchObject({ + command: path.join(researchRoot, "bin", "papers"), + args: ["--stdio"], + cwd: researchRoot, + envPolicy: "literal", + // tool_timeout_sec is a hard cap on tool calls, not the stretchable request timeout. + toolTimeoutMs: 30_000, + transport: "stdio", + }); + // Codex passes its own short list plus the server's env_vars, never the whole environment. + const papers = byName.get("research@market:papers"); + expect(papers?.envInherit).toEqual([]); + expect(papers?.timeout).toBeUndefined(); + expect(papers?.env).toMatchObject({ PATH: process.env.PATH, CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }); + expect(papers?.env?.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); + expect(byName.get("research@market:tracker")).toMatchObject({ + url: "https://mcp.example.test/mcp", + headers: { Authorization: "Bearer pat-value" }, + headerPolicy: "origin-locked", + transport: "http", + // It names its own token, so it is bound as a key: no OAuth sign-in replaces the bearer. + auth: { type: "apikey" }, + }); + // The plugin's config table turns this server off in Codex for good, so it is not listed. + expect(byName.has("research@market:quiet")).toBe(false); + }); + + /** + * Installs and enables `limits@market` with these servers and config.toml lines after its table; + * returns its `.mcp.json`. + */ + async function installLimits(mcpServers: Record, policy: string[] = []): Promise { + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const config = await Bun.file(configPath).text(); + await Bun.write(configPath, [config, '[plugins."limits@market"]', "enabled = true", ...policy, ""].join("\n")); + const root = path.join(codexHome, "plugins", "cache", "market", "limits", "1.0.0"); + await writeManifest(root, { name: "limits" }); + const mcpPath = path.join(root, ".mcp.json"); + await Bun.write(mcpPath, JSON.stringify({ mcpServers })); + clearFsCache(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + return fs.realpath(mcpPath); + } + const limits = async () => { + const result = await loadCapability("mcps", { cwd, agentDir }); + return { + names: result.items.filter(server => server.name.startsWith("limits@market:")).map(server => server.name), + warnings: result.warnings.filter(warning => warning.includes("of limits@market")), + }; + }; + + it("skips a plugin's server whose tools Codex narrows with enabled_tools", async () => { + await installLimits({ + narrow: { command: "node", args: ["narrow.mjs"], enabled_tools: ["search"] }, + open: { command: "node", args: ["open.mjs"], disabled_tools: [] }, + }); + expect(await limits()).toEqual({ + names: ["limits@market:open"], + warnings: [ + '[Codex Plugins] [codex-plugins] Skipping MCP server "narrow" of limits@market: Codex restricts its tools (enabled_tools), which Scient Agent cannot enforce', + ], + }); + }); + + it("skips a server whose tools Codex asks about before running them", async () => { + await installLimits( + { + writes: { command: "node", args: ["writes.mjs"], tools: { save: { approval_mode: "writes" } } }, + prompted: { command: "node", args: ["prompted.mjs"] }, + approved: { command: "node", args: ["approved.mjs"], default_tools_approval_mode: "approve" }, + }, + ['[plugins."limits@market".mcp_servers.prompted]', 'default_tools_approval_mode = "prompt"'], + ); + const { names, warnings } = await limits(); + expect(names).toEqual(["limits@market:approved"]); + expect(warnings).toContainEqual(expect.stringContaining("tools.save.approval_mode")); + expect(warnings).toContainEqual(expect.stringContaining("default_tools_approval_mode")); + }); + + it("skips a server Codex hides from the model or would not start", async () => { + // A value HTTP does not allow is skipped, so it cannot displace the static header Codex keeps. + restoreEnvValue("CODEX_PLUGIN_TEST_NEWLINE", "account-b\n"); + await installLimits({ + hidden: { command: "node", args: ["hidden.mjs"], omit_tools_from: ["direct", "deferred", "code_mode"] }, + negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, + // One past u64::MAX, which Codex's startup_timeout_ms cannot hold. + huge: { command: "node", args: ["huge.mjs"], startup_timeout_ms: 2 ** 64 }, + // Codex times out every tool call of this one. + instant: { command: "node", args: ["instant.mjs"], tool_timeout_sec: 0 }, + capped: { command: "node", args: ["capped.mjs"], tools: { read: { output_token_limit: 100 } } }, + shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, + // Codex refuses to start a server whose named bearer variable is unset. + unsetbearer: { url: "https://mcp.example.test/a", bearer_token_env_var: "CODEX_PLUGIN_TEST_UNSET" }, + // The bearer replaces a static header of the same name in any case. + cased: { + url: "https://mcp.example.test/b", + http_headers: { authorization: "Basic old" }, + bearer_token_env_var: "CODEX_PLUGIN_TEST_PAT", + }, + kept: { + url: "https://mcp.example.test/c", + http_headers: { "X-Account": "account-a", "Bad Name": "x" }, + env_http_headers: { "X-Account": "CODEX_PLUGIN_TEST_NEWLINE" }, + }, + // Codex compares a confidential client's stored ID with this one exactly, which Scient would trim. + spacedclient: { url: "https://mcp.example.test/d", oauth: { client_id: " id ", client_secret: "s" } }, + // Codex refuses a client secret without a client ID. + secretonly: { url: "https://mcp.example.test/e", oauth: { client_secret: "s" } }, + // Codex rejects an empty secret, which must not pass as no secret at all. + emptysecret: { url: "https://mcp.example.test/f", oauth: { client_id: "id", client_secret: "" } }, + spacedsecret: { url: "https://mcp.example.test/g", oauth: { client_id: "id", client_secret: " s" } }, + // Enterprise sign-in settings Codex allows only with ema_auth. + issuer: { url: "https://mcp.example.test/h", oauth: { authorization_server_issuer: "https://idp.test" } }, + // Codex folds camelCase aliases in by key presence: a null snake_case ID still wins over clientId, + // leaving the secret without an ID; a numeric clientSecret alias fails the type check. + nullsnake: { + url: "https://mcp.example.test/i", + oauth: { client_id: null, clientId: "id", client_secret: "s" }, + }, + numericalias: { url: "https://mcp.example.test/j", oauth: { client_id: "id", clientSecret: 7 } }, + // Rust trims U+0085 (NEL) as whitespace, so Codex finds this secret blank. + nelsecret: { url: "https://mcp.example.test/k", oauth: { client_id: "id", client_secret: "\u0085" } }, + // Codex refuses to start a server whose name has characters outside its pattern. + "my server": { command: "node", args: ["spaced.mjs"] }, + }); + const { names, warnings } = await limits(); + expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); + const byName = (await load()).byName; + expect(byName.get("limits@market:cased")?.headers).toEqual({ Authorization: "Bearer pat-value" }); + expect(byName.get("limits@market:kept")?.headers).toEqual({ "X-Account": "account-a" }); + // Codex's five-minute default holds a server that sets no tool_timeout_sec. + expect(byName.get("limits@market:shown")?.toolTimeoutMs).toBe(300_000); + expect(warnings).toContainEqual(expect.stringContaining('"spacedclient" of limits@market: its OAuth client_id')); + expect(warnings).toContainEqual( + expect.stringContaining('"secretonly" of limits@market: its OAuth client_secret has no client_id'), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"emptysecret" of limits@market: its OAuth client_secret'), + ); + expect(warnings).toContainEqual( + expect.stringContaining( + '"spacedsecret" of limits@market: its OAuth client_id or client_secret has surrounding', + ), + ); + expect(warnings).toContainEqual(expect.stringContaining('"nullsnake" of limits@market: its OAuth client_secret')); + expect(warnings).toContainEqual(expect.stringContaining('"numericalias" of limits@market: invalid oauth')); + expect(warnings).toContainEqual(expect.stringContaining('"nelsecret" of limits@market: its OAuth client_secret')); + expect(warnings).toContainEqual(expect.stringContaining('"my server" of limits@market: Codex refuses its name')); + expect(warnings).toContainEqual( + expect.stringContaining('"issuer" of limits@market: its OAuth authorization_server_issuer needs ema_auth'), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"unsetbearer" of limits@market: its bearer_token_env_var CODEX_PLUGIN_TEST_UNSET'), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"hidden" of limits@market: Codex restricts its tools (omit_tools_from)'), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"negative" of limits@market: invalid startup_timeout_sec'), + ); + expect(warnings).toContainEqual( + expect.stringContaining( + '"capped" of limits@market: Codex restricts its tools (tools.read.output_token_limit)', + ), + ); + expect(warnings).toContainEqual(expect.stringContaining('"instant" of limits@market: Codex times out each')); + }); + + it("holds a tool call to tool_timeout_sec whatever its arguments ask", () => { + // A call asking to wait 10 s still ends at the configured 2 s, as in Codex. + const args = { arguments: { wait_seconds: 10 } }; + expect(resolveMCPRequestDeadline(undefined, "tools/call", args, 2_000)).toEqual({ + timeoutMs: 2_000, + maxTotalMs: 2_000, + }); + // Other requests keep their own timeout. + expect(resolveMCPRequestDeadline(undefined, "tools/list", undefined, 2_000).timeoutMs).not.toBe(2_000); + }); + + // The cap is the transport's own timer on a live socket, which fake timers do not drive: the test + // waits on no fixed delay, only on that cap (300 ms at most) firing. + it("keeps a tool call's cap across an HTTP sign-in refresh and retry", async () => { + let posts = 0; + const server = Bun.serve({ + port: 0, + async fetch(req) { + await req.json(); + posts++; + // The first attempt is refused; the retry is never answered. + return posts === 1 ? new Response("expired", { status: 401 }) : new Promise(() => {}); + }, + }); + const call = async (refresh: () => Promise | null>) => { + posts = 0; + const transport = new HttpTransport({ + type: "http", + url: `http://127.0.0.1:${server.port}/mcp`, + toolTimeoutMs: 300, + }); + await transport.connect(); + transport.onAuthError = refresh; + try { + return await transport.request("tools/call", { name: "slow", arguments: {} }).then( + () => "resolved", + (error: Error) => error.message, + ); + } finally { + await transport.close(); + } + }; + try { + // A refresh that never ends still ends the call at the cap. + expect(await call(() => new Promise(() => {}))).toBe("Request timeout after 300ms"); + // A refresh that takes 200 ms of the cap leaves the retry the rest, not a fresh 300 ms. + const realNow = performance.now.bind(performance); + let skew = 0; + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); + const message = await call(async () => { + skew = 200; + return { Authorization: "Bearer refreshed" }; + }); + expect(Number(/Request timeout after (\d+)ms/.exec(message)?.[1])).toBeLessThanOrEqual(100); + // A refresh that ends past the deadline sends no retry at all. + skew = 0; + const late = await call(async () => { + skew = 400; + return { Authorization: "Bearer refreshed" }; + }); + expect(late).toBe("Request timeout after 300ms"); + expect(posts).toBe(1); + } finally { + server.stop(true); + } + }); + + it("replays a tool call after reconnecting only within the call's own deadline", async () => { + const deadlines: (number | undefined)[] = []; + const transport = (respond: () => Promise): MCPTransport => ({ + connected: true, + request: (async (_method: string, _params: unknown, options?: MCPRequestOptions) => { + deadlines.push(options?.deadlineAt); + return respond(); + }) as MCPTransport["request"], + async notify() {}, + async close() {}, + }); + const connection = (respond: () => Promise): MCPServerConnection => ({ + name: "tools@market:api", + config: { type: "http", url: "https://mcp.example.test/mcp", toolTimeoutMs: 300_000 }, + transport: transport(respond), + serverInfo: { name: "api", version: "1" }, + capabilities: { tools: {} }, + }); + const unavailable = new MCPTransportError({ + transport: "http", + stage: "receive", + failure: "http_status", + message: "HTTP 503: busy", + retryable: true, + code: 503, + }); + const definition = { name: "run", inputSchema: { type: "object" as const } }; + const ctx = {} as Parameters[3]; + // A 503 reconnects and replays the call: the replay keeps the first attempt's deadline. + const replayed = new MCPTool( + connection(async () => Promise.reject(unavailable)), + definition, + async () => connection(async () => ({ content: [{ type: "text", text: "ok" }] })), + ); + await replayed.execute("call", {}, () => {}, ctx); + expect(deadlines).toHaveLength(2); + expect(deadlines[0]).toBeNumber(); + expect(deadlines[1]).toBe(deadlines[0]); + // A reconnect still pending at the deadline ends the call with its own error, never a replay. + deadlines.length = 0; + const stalled = new MCPTool( + { + ...connection(async () => Promise.reject(unavailable)), + config: { type: "http", url: "https://x.test/", toolTimeoutMs: 1 }, + }, + definition, + () => new Promise(() => {}), + ); + const result = await stalled.execute("call", {}, () => {}, ctx); + expect(result.isError).toBe(true); + expect(deadlines).toHaveLength(1); + // A reconnect that lands past the deadline, before its timer fires, gets no replay either. + deadlines.length = 0; + const realNow = performance.now.bind(performance); + let skew = 0; + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); + const late = new MCPTool( + connection(async () => Promise.reject(unavailable)), + definition, + async () => { + skew = 400_000; + return connection(async () => ({ content: [{ type: "text", text: "ok" }] })); + }, + ); + expect((await late.execute("call", {}, () => {}, ctx)).isError).toBe(true); + expect(deadlines).toHaveLength(1); + // A deadline already spent when the call fails starts no reconnect at all. + let reconnects = 0; + const spent = new MCPTool( + connection(async () => { + skew += 400_000; + throw unavailable; + }), + definition, + async () => { + reconnects++; + return connection(async () => ({ content: [] })); + }, + ); + expect((await spent.execute("call", {}, () => {}, ctx)).isError).toBe(true); + expect(reconnects).toBe(0); + }); + + it("never writes a stdio tool call whose cap ran out while it was being prepared", async () => { + const transport = new StdioTransport({ + type: "stdio", + command: process.execPath, + // Stays up and never answers, so only a timer could end a call it was sent. + args: ["-e", "setInterval(() => {}, 1000)"], + toolTimeoutMs: 5_000, + }); + const realNow = performance.now.bind(performance); + let skew = 0; + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); + await transport.connect(); + try { + // Serializing the request spends the whole cap. + const slow = { + toJSON: () => { + skew += 10_000; + return {}; + }, + }; + await expect(transport.request("tools/call", { name: "slow", arguments: slow })).rejects.toThrow( + "Request timeout after 5000ms", + ); + } finally { + await transport.close(); + } + }); + + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { + await installLimits( + { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, + ['[plugins."limits@market".mcp_servers.denied]', 'disabled_tools = ["delete"]'], + ); + expect(await limits()).toEqual({ + names: ["limits@market:open"], + warnings: [ + '[Codex Plugins] [codex-plugins] Skipping MCP server "denied" of limits@market: Codex restricts its tools (disabled_tools), which Scient Agent cannot enforce', + ], + }); + }); + + it("skips a malformed server and keeps its siblings", async () => { + const mcpPath = await installLimits({ + bad: { command: "node", env_vars: "CODEX_PLUGIN_TEST_TOKEN" }, + headers: { url: "https://mcp.example.test/mcp", http_headers: ["X-Bad"] }, + open: { command: "node", args: ["open.mjs"] }, + }); + expect(await limits()).toEqual({ + names: ["limits@market:open"], + warnings: [ + `[Codex Plugins] [codex-plugins] Skipping MCP server "bad" of limits@market: invalid env_vars (${mcpPath})`, + `[Codex Plugins] [codex-plugins] Skipping MCP server "headers" of limits@market: invalid http_headers (${mcpPath})`, + ], + }); + }); + + /** Installs `@` with this manifest file and extra files; returns its real root. */ + async function install( + id: string, + files: Record, + configLines: string[] = ["enabled = true"], + ): Promise { + const [name, marketplace] = id.split("@"); + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const config = await Bun.file(configPath).text(); + await Bun.write(configPath, [config, `[plugins."${id}"]`, ...configLines, ""].join("\n")); + const root = path.join(codexHome, "plugins", "cache", marketplace!, name!, "1.0.0"); + for (const [file, content] of Object.entries(files)) { + await Bun.write(path.join(root, file), typeof content === "string" ? content : JSON.stringify(content)); + } + clearFsCache(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + return fs.realpath(root); + } + const load = async () => { + const result = await loadCapability("mcps", { cwd, agentDir }); + return { + byName: new Map( + result.items.filter(server => server._source.provider === "codex-plugins").map(s => [s.name, s]), + ), + warnings: result.warnings, + }; + }; + + it("follows [plugins._default]: a table without enabled inherits it", async () => { + const server = { mcpServers: { one: { command: "node" } } }; + await install("inherits@market", { ".codex-plugin/plugin.json": { name: "inherits" }, ".mcp.json": server }); + await install("opted@market", { ".codex-plugin/plugin.json": { name: "opted" }, ".mcp.json": server }); + await Bun.write( + path.join(temp.join("codex-home"), "config.toml"), + [ + "[plugins._default]", + "enabled = false", + // A table with only a server policy and no `enabled` takes the default. + '[plugins."inherits@market".mcp_servers.one]', + "enabled = true", + '[plugins."opted@market"]', + "enabled = true", + "", + ].join("\n"), + ); + clearFsCache(); + expect([...(await load()).byName.keys()]).toEqual(["opted@market:one"]); + }); + + it("rejects servers Codex would reject and maps OAuth client settings", async () => { + await install("strict@market", { + ".codex-plugin/plugin.json": { name: "strict" }, + ".mcp.json": { + mcpServers: { + both: { command: "node", url: "https://mcp.example.test/mcp" }, + argsOnHttp: { url: "https://mcp.example.test/mcp", args: ["x"] }, + helper: { url: "https://mcp.example.test/mcp", http_headers_helper: "./headers.sh" }, + ema: { url: "https://mcp.example.test/mcp", auth: "ema_auth" }, + signin: { + type: "http", + url: "https://mcp.example.test/mcp", + oauth_resource: "https://mcp.example.test/mcp", + scopes: ["read", "write"], + oauth: { clientId: "client-1", callback_port: 4567 }, + note: "unknown fields are ignored, as in Codex", + }, + }, + }, + }); + const { byName, warnings } = await load(); + expect([...byName.keys()].filter(name => name.startsWith("strict@market:"))).toEqual(["strict@market:signin"]); + expect(byName.get("strict@market:signin")).toMatchObject({ + oauth: { clientId: "client-1", callbackPort: 4567, scope: "read write" }, + auth: { type: "oauth", resource: "https://mcp.example.test/mcp" }, + }); + expect(warnings.filter(w => w.includes("of strict@market"))).toEqual([ + expect.stringContaining('"both" of strict@market: url is not supported for stdio'), + expect.stringContaining('"argsOnHttp" of strict@market: args is not supported for streamable HTTP'), + expect.stringContaining('"helper" of strict@market: it computes its headers with a helper command'), + expect.stringContaining('"ema" of strict@market: auth ema_auth is not supported'), + ]); + }); + + it("takes plugin values literally and keeps launch paths inside the package", async () => { + const root = await install("paths@market", { + ".codex-plugin/plugin.json": { name: "paths" }, + ".mcp.json": { + mcpServers: { + literal: { url: "https://mcp.example.test/mcp", http_headers: { "X-Run": "!touch /tmp/pwned" } }, + upCwd: { command: "node", cwd: "../.." }, + upCommand: { command: "../../../other/bin/server" }, + fromPath: { command: "node", env: { MODE: "CODEX_PLUGIN_TEST_PAT" } }, + }, + }, + }); + const { byName, warnings } = await load(); + expect([...byName.keys()].filter(name => name.startsWith("paths@market:")).sort()).toEqual([ + "paths@market:fromPath", + "paths@market:literal", + ]); + // Origin-locked headers and literal env are never resolved as variables or `!commands`. + expect(byName.get("paths@market:literal")).toMatchObject({ + headers: { "X-Run": "!touch /tmp/pwned" }, + headerPolicy: "origin-locked", + }); + expect(byName.get("paths@market:fromPath")).toMatchObject({ + command: "node", + cwd: root, + env: { MODE: "CODEX_PLUGIN_TEST_PAT" }, + envPolicy: "literal", + }); + const reasons = warnings.filter(w => w.includes("of paths@market")); + expect(reasons).toEqual([ + expect.stringContaining('"upCwd" of paths@market: cwd ../.. leaves the package'), + expect.stringContaining('"upCommand" of paths@market: command ../../../other/bin/server leaves the package'), + ]); + }); + + it("does not follow a skills or MCP file link out of the package", async () => { + const outside = temp.join("outside"); + await writeSkill(outside, "leaked"); + await Bun.write(path.join(outside, "servers.json"), JSON.stringify({ mcpServers: { x: { command: "node" } } })); + const root = await install("linked@market", { + ".codex-plugin/plugin.json": { name: "linked", mcpServers: "./servers.json" }, + }); + await fs.symlink(path.join(outside, "skills"), path.join(root, "skills"), "dir"); + await fs.symlink(path.join(outside, "servers.json"), path.join(root, "servers.json")); + clearFsCache(); + expect((await skills()).map(skill => skill.name)).toEqual(["lit-review"]); + const { byName, warnings } = await load(); + expect([...byName.keys()].some(name => name.startsWith("linked@market:"))).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path outside linked@market")); + }); + + it("keeps same-named plugins from different marketplaces apart, whatever their names", async () => { + await install("twin@alpha", { + ".codex-plugin/plugin.json": { name: "twin" }, + ".mcp.json": { mcpServers: { a: { command: "node" } } }, + }); + await install("twin@beta", { + ".codex-plugin/plugin.json": { name: "twin" }, + ".mcp.json": { mcpServers: { b: { command: "node" } } }, + }); + // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. + await install("twin.alpha@gamma", { + ".codex-plugin/plugin.json": { name: "twin.alpha" }, + ".mcp.json": { mcpServers: { c: { command: "node" } } }, + }); + expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ + "twin.alpha@gamma:c", + "twin@alpha:a", + "twin@beta:b", + ]); + }); + + it("runs none of the servers two enabled plugins declare under one name", async () => { + const server = { mcpServers: { api: { command: "node" } } }; + await install("first@market", { ".codex-plugin/plugin.json": { name: "first" }, ".mcp.json": server }); + await install("second@market", { ".codex-plugin/plugin.json": { name: "second" }, ".mcp.json": server }); + const { byName, warnings } = await load(); + expect(byName.has("first@market:api")).toBe(false); + expect(byName.has("second@market:api")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"api" of first@market: second@market declares a server of that name too'), + ); + }); + + it("matches skill rules to the name Codex gives a plugin's skills, and only that name", async () => { + for (const id of ["twin@alpha", "twin@beta"]) { + const root = await install(id, { ".codex-plugin/plugin.json": { name: "twin" } }); + await writeSkill(root, "search"); + } + // A blank manifest name leaves Codex the package folder's name, the version. + const blank = await install("blank@market", { ".codex-plugin/plugin.json": { name: " " } }); + await writeSkill(blank, "find"); + // A manifest name of Unicode whitespace alone is blank to Codex too. + const nel = await install("nel@market", { ".codex-plugin/plugin.json": { name: "\u0085" } }); + await writeSkill(nel, "nel-skill"); + // Codex collapses the name's inner whitespace before a rule meets it. + await writeSkill(blank, "Paper Search"); + // A skill without a frontmatter name takes its folder's, collapsed the same way. + await Bun.write(path.join(blank, "skills", "daily review", "SKILL.md"), "---\ndescription: d\n---\nRun.\n"); + const configPath = temp.join("codex-home", "config.toml"); + const rules = [ + "[[skills.config]]", + // Codex trims a selector's Unicode whitespace, U+0085 included. + 'name = "\\u0085twin:search\\u0085"', + "enabled = false", + // Codex names no plugin skill bare `search`, so this enables nothing. + "[[skills.config]]", + 'name = "search"', + "enabled = true", + "[[skills.config]]", + 'name = "1.0.0:find"', + "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:Paper Search"', + "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:daily review"', + "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:nel-skill"', + "enabled = false", + "", + ]; + await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules].join("\n")); + clearFsCache(); + const names = (await skills()).map(skill => skill.name); + expect(names).not.toContain("search"); + expect(names).not.toContain("find"); + expect(names.some(name => name.startsWith("Paper"))).toBe(false); + expect(names.some(name => name.startsWith("daily"))).toBe(false); + expect(names).not.toContain("nel-skill"); + }); + + it("loads no plugin from a [plugins] table Codex cannot read", async () => { + const configPath = temp.join("codex-home", "config.toml"); + const original = await Bun.file(configPath).text(); + for (const [line, field] of [ + ['enabled = "false"', "strict@market.enabled"], + ['auto_provision = "yes"', "strict@market.auto_provision"], + ["apps.connector = false", "strict@market.apps.connector"], + [ + 'apps.connector.tools.list.approval_mode = "sometimes"', + "strict@market.apps.connector.tools.list.approval_mode", + ], + ]) { + await Bun.write(configPath, original); + await install("strict@market", { ".codex-plugin/plugin.json": { name: "strict" } }, [line]); + const { byName, warnings } = await load(); + expect(byName.size).toBe(0); + expect(warnings).toContainEqual(expect.stringContaining(`cannot read its [plugins] settings (${field})`)); + } + }); + + it("follows Codex's home, plugins switch and own server settings", async () => { + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const original = await Bun.file(configPath).text(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + // Codex opens CODEX_HOME exactly as set: with a trailing space it names another folder. + restoreEnvValue("CODEX_HOME", `${codexHome} `); + let result = await load(); + expect(result.byName.size).toBe(0); + expect(result.warnings).toContainEqual(expect.stringContaining("which is not a folder")); + restoreEnvValue("CODEX_HOME", codexHome); + // `[features] plugins = false` turns every plugin off. + await Bun.write(configPath, `${original}\n[features]\nplugins = false\n`); + clearFsCache(); + result = await load(); + expect(result.byName.size).toBe(0); + expect(result.warnings).toContainEqual(expect.stringContaining("plugins are turned off")); + // The config's own [mcp_servers.papers] is what Codex runs, not the plugin's server. + await Bun.write(configPath, `${original}\n[mcp_servers.papers]\ncommand = "node"\nenabled = false\n`); + clearFsCache(); + result = await load(); + expect(result.byName.has("research@market:papers")).toBe(false); + expect(result.byName.has("research@market:tracker")).toBe(true); + expect(result.warnings).toContainEqual( + expect.stringContaining("Codex runs your own [mcp_servers.papers] instead"), + ); + }); + + it.skipIf(process.platform === "win32")( + "reads a backslash as part of a file name outside Windows, as Codex does", + async () => { + await install("slashed@market", { + ".codex-plugin/plugin.json": { name: "slashed", mcpServers: "./sub\\..\\.mcp.json" }, + ".mcp.json": { mcpServers: { fallback: { command: "node" } } }, + }); + const { byName, warnings } = await load(); + // Codex takes the path, finds no such file, and does not fall back to .mcp.json. + expect(byName.has("slashed@market:fallback")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Missing mcpServers file declared by slashed@market")); + }, + ); + + it("leaves out plugin skills the user's Codex config disables", async () => { + const root = await fs.realpath(await install("ruled@market", { ".codex-plugin/plugin.json": { name: "ruled" } })); + for (const name of ["off-by-path", "off-by-name", "back-on", "kept"]) await writeSkill(root, name); + const configPath = temp.join("codex-home", "config.toml"); + const rules = [ + "[[skills.config]]", + `path = ${JSON.stringify(path.join(root, "skills", "off-by-path", "SKILL.md"))}`, + "enabled = false", + "[[skills.config]]", + 'name = "ruled:off-by-name"', + "enabled = false", + // A later entry for the same selector replaces the earlier one. + "[[skills.config]]", + 'name = "back-on"', + "enabled = false", + "[[skills.config]]", + 'name = "back-on"', + "enabled = true", + ]; + await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules, ""].join("\n")); + clearFsCache(); + const names = (await skills()).filter(skill => skill.path.startsWith(root)).map(skill => skill.name); + expect(names.sort()).toEqual(["back-on", "kept"]); + }); + + it("reads the launch environment only where the process can know it", async () => { + const first = temp.join("first-project"); + const second = temp.join("second-project"); + await Bun.write(path.join(first, ".env"), "CODEX_HOME=/from/dotenv\nCODEX_PLUGIN_TEST_SECRET=dotenv-secret\n"); + await Bun.write(path.join(first, ".env.local"), "CODEX_PLUGIN_TEST_LOCAL=local-secret\n"); + await fs.mkdir(second, { recursive: true }); + // A fresh process, so the dotenv files load at its own startup in `first`. + const utils = path.join(import.meta.dir, "../../../utils/src/index.ts"); + const script = [ + `const { getLaunchEnv, setProjectDir } = await import(${JSON.stringify(utils)});`, + `setProjectDir(${JSON.stringify(second)});`, + "const env = getLaunchEnv();", + "console.log(JSON.stringify([Bun.env.CODEX_HOME, env && [env.CODEX_HOME, env.CODEX_PLUGIN_TEST_SECRET, env.CODEX_PLUGIN_TEST_LOCAL, env.CODEX_PLUGIN_TEST_PAT]]));", + ].join("\n"); + // JSON writes a missing value as null. + const launch = [null, null, null, "exported"]; + // With --no-env-file only the agent's own loader reads .env, after the snapshot. Without it + // Bun autoloads .env and .env.local first, as when the agent runs from source; only Linux's + // procfs still holds the launch environment then. + for (const [flags, expected] of [ + [["--no-env-file"], launch], + [[], process.platform === "linux" ? launch : null], + // Bun loads a file it is told to even with --no-env-file. + [["--no-env-file", "--env-file=.env"], null], + ] as const) { + const run = Bun.spawnSync([process.execPath, ...flags, "-e", script], { + cwd: first, + env: { PATH: process.env.PATH, HOME: temp.join("home"), CODEX_PLUGIN_TEST_PAT: "exported" }, + }); + expect(JSON.parse(run.stdout.toString().trim().split("\n").at(-1) ?? "null")).toEqual([ + "/from/dotenv", + expected, + ]); + } + }); + + it("loads nothing while the launch environment is unknown", async () => { + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + vi.spyOn(piUtils, "getLaunchEnv").mockReturnValue(undefined); + const { byName, warnings } = await load(); + expect(byName.size).toBe(0); + expect(await skills()).toEqual([]); + expect(warnings).toContainEqual(expect.stringContaining("start it with --no-env-file")); + }); + + it("follows Codex on disabled declarations, withheld variables and linked package folders", async () => { + await install( + "rules@market", + { + ".codex-plugin/plugin.json": { name: "rules" }, + ".mcp.json": { + mcpServers: { + off: { command: "node", enabled: false }, + retired: { command: "node", args: ["retired.mjs"] }, + numeric: { command: 7, url: "https://mcp.example.test/mcp" }, + withheld: { + command: "node", + env: { NODE_REPL_AUTH_TOKEN: "x", openai_spiffe_id: "lower", KEEP: "y" }, + }, + }, + }, + }, + ["enabled = true", '[plugins."rules@market".mcp_servers.retired]', "ema_auth = true"], + ); + const outside = temp.join("outside-manifest"); + await Bun.write(path.join(outside, "plugin.json"), JSON.stringify({ name: "linkedfolder" })); + const linkedRoot = await install("linkedfolder@market", { + ".mcp.json": { mcpServers: { x: { command: "node" } } }, + }); + await fs.symlink(outside, path.join(linkedRoot, ".codex-plugin"), "dir"); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.has("rules@market:off")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"off" of rules@market: Codex turns it off (enabled = false)'), + ); + // Codex turns an ema_auth server off for good, so it is not listed for a setting to turn on. + expect(byName.has("rules@market:retired")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"retired" of rules@market: Codex turns it off (ema_auth)'), + ); + expect(byName.has("rules@market:numeric")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining('"numeric" of rules@market: invalid command')); + expect(byName.get("rules@market:withheld")?.env).toMatchObject({ KEEP: "y" }); + expect(byName.get("rules@market:withheld")?.env?.NODE_REPL_AUTH_TOKEN).toBeUndefined(); + expect(byName.get("rules@market:withheld")?.env?.openai_spiffe_id).toBeUndefined(); + expect(byName.has("linkedfolder@market:x")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining("Ignoring linkedfolder@market: .codex-plugin is not a folder"), + ); + }); + + it("contains every skill in its package and finds nested ones", async () => { + const outside = temp.join("outside-skill"); + await writeSkill(outside, "escaped"); + const root = await fs.realpath(await install("mixed@market", { ".codex-plugin/plugin.json": { name: "mixed" } })); + await writeSkill(root, "inside"); + await writeSkill(path.join(root, "skills", "group"), "nested"); + await fs.symlink(path.join(outside, "skills", "escaped"), path.join(root, "skills", "escaped"), "dir"); + await fs.mkdir(path.join(root, "skills", "filelink")); + await fs.symlink( + path.join(outside, "skills", "escaped", "SKILL.md"), + path.join(root, "skills", "filelink", "SKILL.md"), + ); + clearFsCache(); + const found = (await skills()).filter(skill => skill.path.startsWith(root)); + expect(found.map(skill => skill.name).sort()).toEqual(["inside", "nested"]); + // skill:// resolves the skill's other files inside the package too. + expect(found.every(skill => skill.containRoot === root)).toBe(true); + }); + + it("applies Codex's rules to an Agent Plugins package's mcp.json", async () => { + restoreEnvValue("CODEX_PLUGIN_TEST_PAT", "pat-value"); + const schema = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json"; + await install("portable2@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable2" }, + "mcp.json": { + $schema: schema, + mcpServers: { + events: { type: "sse", url: "https://mcp.example.test/sse" }, + local: { + type: "stdio", + command: "node", + env: { CODEX_PLUGIN_TEST_PAT: "$" + "{CODEX_PLUGIN_TEST_PAT}", MODE: "x" }, + }, + }, + }, + // The package's legacy declaration forwards the variable the placeholder stands for. + ".codex-plugin/plugin.json": { name: "portable2" }, + ".mcp.json": { mcpServers: { local: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, + }); + const linked = await install("linkedmcp@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "linkedmcp" }, + "real-mcp.json": { $schema: schema, mcpServers: { y: { type: "stdio", command: "node" } } }, + }); + await fs.symlink(path.join(linked, "real-mcp.json"), path.join(linked, "mcp.json")); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.has("portable2@market:events")).toBe(false); + expect(byName.get("portable2@market:local")?.env).toMatchObject({ + CODEX_PLUGIN_TEST_PAT: "pat-value", + MODE: "x", + }); + expect(byName.has("linkedmcp@market:y")).toBe(false); + // mcp.json carries no tool_timeout_sec, so Codex's five-minute default holds its tool calls. + expect(byName.get("portable2@market:local")?.toolTimeoutMs).toBe(300_000); + expect(warnings).toContainEqual(expect.stringContaining("linkedmcp@market: mcp.json is not a regular file")); + }); + + it("forwards a variable only from a valid .codex-plugin declaration of the same stdio server", async () => { + const placeholder = "$" + "{CODEX_PLUGIN_TEST_PAT}"; + const standard = (name: string) => ({ + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name }, + "mcp.json": { + $schema: "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json", + mcpServers: { + [name]: { type: "stdio", command: "node", env: { CODEX_PLUGIN_TEST_PAT: placeholder } }, + }, + }, + }); + await install("viaclaude@market", { + ...standard("viaclaude"), + ".claude-plugin/plugin.json": { name: "viaclaude" }, + ".mcp.json": { mcpServers: { viaclaude: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, + }); + await install("invalidoverlay@market", { + ...standard("invalidoverlay"), + ".codex-plugin/plugin.json": { name: "invalidoverlay" }, + ".mcp.json": { + mcpServers: { + invalidoverlay: { + command: "node", + url: "https://mcp.example.test/mcp", + env_vars: ["CODEX_PLUGIN_TEST_PAT"], + }, + }, + }, + }); + await install("textflag@market", { + ...standard("textflag"), + ".codex-plugin/plugin.json": { name: "textflag" }, + ".mcp.json": { + mcpServers: { textflag: { command: "node", enabled: "false", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } }, + }, + }); + const { byName } = await load(); + expect(byName.get("viaclaude@market:viaclaude")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("invalidoverlay@market:invalidoverlay")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("textflag@market:textflag")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + }); + + it("skips a manifest Codex cannot read and falls back as Codex does from paths it would not take", async () => { + const root = await install("typed@market", { + ".codex-plugin/plugin.json": { name: 5 }, + ".mcp.json": { mcpServers: { x: { command: "node" } } }, + }); + await writeSkill(root, "typed-skill"); + const loose = await install("loose@market", { + ".codex-plugin/plugin.json": { name: "loose", skills: "skills/", mcpServers: "./sub/../.mcp.json" }, + ".mcp.json": { mcpServers: { y: { command: "node" } } }, + }); + await writeSkill(loose, "loose-skill"); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.has("typed@market:x")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Invalid plugin.json for typed@market (name)")); + // Codex drops both paths and reads the package's skills/ and .mcp.json instead. + expect(byName.has("loose@market:y")).toBe(true); + expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers of loose@market")); + const names = (await skills()).map(skill => skill.name); + expect(names).not.toContain("typed-skill"); + expect(names).toContain("loose-skill"); + }); + + it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { + await install("portable@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable" }, + ".codex-plugin/plugin.json": { name: "portable", mcpServers: { legacy: { command: "node" } } }, + "mcp.json": { + $schema: "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json", + mcpServers: { remote: { type: "streamable-http", url: "https://mcp.example.test/mcp" } }, + }, + }); + await install("clauded@market", { + ".claude-plugin/plugin.json": { name: "clauded" }, + ".mcp.json": { mcpServers: { tool: { command: "node" } } }, + }); + const { byName } = await load(); + expect(byName.get("portable@market:remote")).toMatchObject({ transport: "http", headerPolicy: "origin-locked" }); + expect(byName.has("portable@market:legacy")).toBe(false); + expect(byName.has("clauded@market:tool")).toBe(true); + }); + + it("starts a stdio server with only the variables Codex would pass", async () => { + restoreEnvValue("CODEX_PLUGIN_TEST_SECRET", "must-not-leak"); + const out = temp.join("env.json"); + const transport = new StdioTransport({ + type: "stdio", + command: process.execPath, + args: ["-e", `require("fs").writeFileSync(${JSON.stringify(out)}, JSON.stringify(process.env))`], + env: { MODE: "set" }, + envInherit: ["PATH", "CODEX_PLUGIN_TEST_TOKEN"], + }); + // The script writes its environment and exits; the transport reports the exit. + const exited = Promise.withResolvers(); + transport.onClose = () => exited.resolve(); + try { + await transport.connect(); + await exited.promise; + } finally { + await transport.close(); + } + const seen = JSON.parse(await Bun.file(out).text()) as Record; + expect(seen.CODEX_PLUGIN_TEST_TOKEN).toBe("forwarded-value"); + expect(seen.MODE).toBe("set"); + expect(seen.CODEX_PLUGIN_TEST_SECRET).toBeUndefined(); + expect(seen.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts new file mode 100644 index 0000000000..03548e343a --- /dev/null +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -0,0 +1,321 @@ +import { Database } from "bun:sqlite"; +import { afterEach, describe, expect, it, vi } from "bun:test"; +import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; +import type { SourceMeta } from "@oh-my-pi/pi-coding-agent/capability/types"; +import type { MCPLoadResult, MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; +import { mcpOAuthServerCredentialId } from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; +import { McpReauthRefusalError, reauthorizeMcpServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; +import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; +import * as path from "node:path"; +import { TempDir } from "@oh-my-pi/pi-utils"; +import { + authorizationUrlIsPublic, + loginFailureMessage, + selectedServer, + serverStatusLine, +} from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; +import { McpSignInPickup } from "@oh-my-pi/pi-coding-agent/scient/mcp-sign-in-pickup"; + +const URL_A = "https://mcp.example.test/mcp"; +const NAME = "tools@market:api"; +const LEAKED = "sk-live-SECRET"; +const CHALLENGE = `HTTP 401: Unauthorized {"echo":"Bearer ${LEAKED}"} [WWW-Authenticate: Bearer resource_metadata="https://mcp.example.test/.well-known/oauth-protected-resource"]`; +const bound = ( + auth: MCPServerConfig["auth"] = { type: "oauth", credentialId: mcpOAuthServerCredentialId(NAME, URL_A) }, + extra: Partial = {}, +) => ({ type: "http", url: URL_A, auth, ...extra }) as MCPServerConfig; +const token = (access: string) => ({ type: "oauth" as const, access, refresh: "r", expires: Date.now() + 3_600_000 }); + +/** The few manager members these helpers read; the rest of MCPManager is not involved. */ +function fakeManager( + servers: Record; tools?: number }>, + reconnect: (name: string) => Promise = async () => {}, +) { + const connected = new Set(); + const reconnects: string[] = []; + const manager = { + getAllServerNames: () => Object.keys(servers), + getConnectionStatus: (name: string) => (connected.has(name) ? "connected" : "disconnected"), + getServerConfig: (name: string) => servers[name]?.config, + getSource: (name: string) => servers[name]?.source, + getConnection: (name: string) => + servers[name]?.tools ? { tools: Array.from({ length: servers[name]?.tools ?? 0 }) } : undefined, + reconnectServer: async (name: string) => { + reconnects.push(name); + await reconnect(name); + connected.add(name); + return null; + }, + }; + // Structural fake: only the members above are called by the code under test. + return { + manager: manager as unknown as MCPManager, + reconnects, + disconnect: (name: string) => connected.delete(name), + }; +} + +const loadResult = (connected: string[], errors: Record): MCPLoadResult => ({ + tools: [], + errors: new Map(Object.entries(errors)), + connectedServers: connected, + exaApiKeys: [], +}); + +describe("scient-agent mcp list", () => { + it("reports a server refusing an anonymous call as needing sign-in, and whether a browser sign-in serves it", () => { + const { manager } = fakeManager({ + [NAME]: { config: bound(), source: { level: "user", pluginName: "tools@market" } }, + "own-token": { config: bound({ type: "bearer" } as unknown as MCPServerConfig["auth"]) }, + header: { config: { type: "http", url: URL_A, headers: { Authorization: "Bearer x" } } }, + keyed: { config: { type: "http", url: `${URL_A}?key=${LEAKED}` } as MCPServerConfig }, + }); + const loaded = loadResult([], { [NAME]: CHALLENGE, "own-token": CHALLENGE, header: CHALLENGE, keyed: CHALLENGE }); + expect(serverStatusLine(manager, NAME, loaded)).toEqual({ + name: NAME, + status: "needs-auth", + scope: "user", + pluginId: "tools@market", + error: "Sign-in needed.", + canStartSignIn: true, + }); + expect(serverStatusLine(manager, "own-token", loaded)).not.toHaveProperty("canStartSignIn"); + expect(serverStatusLine(manager, "header", loaded)).not.toHaveProperty("canStartSignIn"); + // A key in the server's address would ride along as the sign-in's resource: no button for it. + expect(serverStatusLine(manager, "keyed", loaded)).not.toHaveProperty("canStartSignIn"); + }); + + it("never prints what a server sent back, which can echo a token", () => { + const { manager } = fakeManager({ [NAME]: { config: bound() }, down: { config: bound() } }); + const loaded = loadResult([], { + [NAME]: CHALLENGE, + down: `HTTP 500: Internal Server Error {"authorization":"Bearer ${LEAKED}"}`, + }); + const lines = [serverStatusLine(manager, NAME, loaded), serverStatusLine(manager, "down", loaded)]; + expect(JSON.stringify(lines)).not.toContain(LEAKED); + expect(lines[1]).toMatchObject({ status: "failed", error: "The server answered HTTP 500." }); + }); + + it("reports a connected server's tool count and a server still starting as pending", () => { + const { manager } = fakeManager({ ok: { config: bound(), tools: 3 }, slow: { config: bound() } }); + const loaded = loadResult(["ok"], {}); + expect(serverStatusLine(manager, "ok", loaded)).toMatchObject({ status: "connected", toolCount: 3 }); + expect(serverStatusLine(manager, "slow", loaded).status).toBe("pending"); + }); +}); + +describe("scient-agent mcp login", () => { + it("never prints what the sign-in server sent back", () => { + const exchange = new Error(`Token exchange failed: 400 {"code":"${LEAKED}","client_secret":"${LEAKED}"}`); + const message = loginFailureMessage(NAME, true, exchange); + expect(message).not.toContain(LEAKED); + expect(message).toContain("HTTP 400"); + expect(loginFailureMessage(NAME, true, new Error(`boom ${LEAKED}`))).not.toContain(LEAKED); + // The engine's refusal for a local server quotes its expanded URL argument. + const refusal = new McpReauthRefusalError(`use https://remote.example.test/mcp?token=${LEAKED} instead`); + expect(loginFailureMessage(NAME, false, refusal)).not.toContain(LEAKED); + }); + + it("does not offer an imported sign-in a broker could not refresh", async () => { + // A broker-held store: refresh happens without the server's definition, so without its secret. + const authStorage = { credentials: { heldByBroker: true } } as unknown as AuthStorage; + const config = bound(undefined, { + oauth: { clientId: "id", clientSecret: "secret" }, + } as Partial); + const fetchSpy = vi.spyOn(globalThis, "fetch"); + try { + await expect( + reauthorizeMcpServer(NAME, { filePath: "", scope: "user", config, discovered: true }, { authStorage }), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + expect(fetchSpy).not.toHaveBeenCalled(); + } finally { + fetchSpy.mockRestore(); + } + }); +}); + +describe("a conversation picking up a sign-in made elsewhere", () => { + const stores: AuthStorage[] = []; + afterEach(() => { + while (stores.length > 0) stores.pop()?.close(); + }); + const storage = () => { + const store = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + stores.push(store); + return store; + }; + + it("connects a server once its sign-in appears, and retries only when that sign-in changes", async () => { + const auth = storage(); + const { manager, reconnects, disconnect } = fakeManager({ [NAME]: { config: bound() } }); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + + await pickup.beforePrompt(); + expect(reconnects).toEqual([]); + + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("first")); + await pickup.beforePrompt(); + expect(reconnects).toEqual([NAME]); + expect(refreshed).toBe(1); + + // The server dropped again, e.g. its sign-in was refused: the same sign-in is not retried each prompt. + disconnect(NAME); + await pickup.beforePrompt(); + expect(reconnects).toEqual([NAME]); + + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("second")); + await pickup.beforePrompt(); + expect(reconnects).toEqual([NAME, NAME]); + }); + + it("does not retry the sign-in the session started with", async () => { + const auth = storage(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("startup")); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }); + const pickup = new McpSignInPickup(manager, auth, async () => {}); + pickup.seed(); + await pickup.beforePrompt(); + expect(reconnects).toEqual([]); + }); + + it("holds a prompt only briefly while a reconnect is slow, and finishes it in the background", async () => { + const auth = storage(); + const gate = Promise.withResolvers(); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }, () => gate.promise); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("new")); + + await pickup.beforePrompt(10); + expect(reconnects).toEqual([NAME]); + expect(refreshed).toBe(0); + // A second prompt starts no second attempt for a server still reconnecting. + await pickup.beforePrompt(10); + expect(reconnects).toEqual([NAME]); + + // A later prompt joins the reconnect under way and sees its tools if it lands within its own wait. + setTimeout(() => gate.resolve(), 20); + await pickup.beforePrompt(1_000); + expect(reconnects).toEqual([NAME]); + expect(refreshed).toBe(1); + }); + + it("lets a prompt that arrives during the sign-in re-read wait for the reconnect that read starts", async () => { + const auth = storage(); + const gate = Promise.withResolvers(); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }, () => gate.promise); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("new")); + + const first = pickup.beforePrompt(1_000); + const second = pickup.beforePrompt(1_000); + setTimeout(() => gate.resolve(), 20); + await second; + expect(refreshed).toBe(1); + await first; + expect(reconnects).toEqual([NAME]); + }); + + it("connects every newly signed-in server even while another one stalls", async () => { + const auth = storage(); + const OTHER = "other@market:api"; + const URL_B = "https://other.example.test/mcp"; + const stalled = Promise.withResolvers(); + const { manager, reconnects } = fakeManager( + { + [NAME]: { config: bound() }, + [OTHER]: { + config: bound({ type: "oauth", credentialId: mcpOAuthServerCredentialId(OTHER, URL_B) }, { + url: URL_B, + } as Partial), + }, + }, + name => (name === NAME ? stalled.promise : Promise.resolve()), + ); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("a")); + await auth.credentials.set(mcpOAuthServerCredentialId(OTHER, URL_B), token("b")); + + await pickup.beforePrompt(); + expect(reconnects.toSorted()).toEqual([NAME, OTHER].toSorted()); + expect(refreshed).toBe(1); + stalled.resolve(); + }); + + it("never reconnects a server a browser sign-in does not serve", async () => { + const auth = storage(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("t")); + const { manager, reconnects } = fakeManager({ + header: { config: bound(undefined, { headers: { Authorization: "Bearer x" } } as Partial) }, + local: { config: { type: "stdio", command: "server" } as MCPServerConfig }, + }); + await new McpSignInPickup(manager, auth, async () => {}).beforePrompt(); + expect(reconnects).toEqual([]); + }); +}); + +describe("which server mcp login signs in to", () => { + it("never shows a sign-in page whose resource carries the server's own key", () => { + const page = (resource: string) => + `https://auth.example.test/authorize?client_id=c&resource=${encodeURIComponent(resource)}`; + expect(authorizationUrlIsPublic(page("https://mcp.example.test/mcp"))).toBe(true); + expect(authorizationUrlIsPublic(page(`https://mcp.example.test/mcp?key=${LEAKED}`))).toBe(false); + expect(authorizationUrlIsPublic(page(`https://user:${LEAKED}@mcp.example.test/mcp`))).toBe(false); + expect(authorizationUrlIsPublic(`https://u:${LEAKED}@auth.example.test/authorize?client_id=c`)).toBe(false); + // The sign-in server's own settings in its page address are its own and kept. + expect(authorizationUrlIsPublic(`https://auth.example.test/authorize?tenant=t&client_id=c`)).toBe(true); + }); + + it("signs an ordinary server in from the file discovery read, only when that entry is the selected one", async () => { + using temp = TempDir.createSync("@scient-mcp-login-select-"); + const file = path.join(temp.path(), ".mcp.json"); + await Bun.write(file, JSON.stringify({ mcpServers: { api: { type: "http", url: URL_A } } })); + const source = { provider: "mcp-json", providerName: "", path: file, level: "project" } as SourceMeta; + const selected = { type: "http", url: URL_A } as MCPServerConfig; + const { manager } = fakeManager({ api: { config: selected } }); + expect(await selectedServer("api", selected, source, manager, temp.path())).toMatchObject({ + filePath: file, + scope: "project", + discovered: false, + }); + // The file's entry now points elsewhere than the server discovery selected: no sign-in for it. + const other = { type: "http", url: "https://elsewhere.example.test/mcp" } as MCPServerConfig; + const { manager: otherManager } = fakeManager({ api: { config: other } }); + expect(await selectedServer("api", other, source, otherManager, temp.path())).toBeNull(); + }); + + it("signs in to a URL-only entry (Cursor, Claude) over the transport discovery inferred", async () => { + using temp = TempDir.createSync("@scient-mcp-login-urlonly-"); + const file = path.join(temp.path(), "mcp.json"); + await Bun.write(file, JSON.stringify({ mcpServers: { api: { url: URL_A } } })); + const source = { provider: "cursor", providerName: "", path: file, level: "user" } as SourceMeta; + const selected = { type: "http", url: URL_A } as MCPServerConfig; + const { manager } = fakeManager({ api: { config: selected } }); + const found = await selectedServer("api", selected, source, manager, temp.path()); + expect(found?.config).toMatchObject({ type: "http", url: URL_A }); + expect(found?.filePath).toBe(file); + }); +}); + +it("a malformed sign-in id never stops a conversation from starting", () => { + const auth = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + try { + const malformed = { type: "http", url: URL_A, auth: { type: "oauth", credentialId: 42 } }; + // A config file's `auth: null` on an ordinary server means no auth block. + const nullAuth = { type: "http", url: URL_A, auth: null }; + const { manager } = fakeManager({ + broken: { config: malformed as unknown as MCPServerConfig }, + plain: { config: nullAuth as unknown as MCPServerConfig }, + }); + expect(() => new McpSignInPickup(manager, auth, async () => {}).seed()).not.toThrow(); + } finally { + auth.close(); + } +}); diff --git a/packages/coding-agent/test/scient/runtime-info.test.ts b/packages/coding-agent/test/scient/runtime-info.test.ts index 8a1c6eed9f..4acde1697f 100644 --- a/packages/coding-agent/test/scient/runtime-info.test.ts +++ b/packages/coding-agent/test/scient/runtime-info.test.ts @@ -15,7 +15,8 @@ describe("--runtime-info", () => { upstream: UPSTREAM, rpcProtocolVersions: [1, 2], buildId: BUILD_ID, - hostCommands: ["apps", "dictate"], + hostCommands: ["apps", "dictate", "mcp"], + enableSources: ["claude-plugins", "codex-plugins"], }); }); }); diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index a6d5b777e5..4a8651252e 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -383,6 +383,38 @@ export function withOmpOtelEnv(fn: () => T): T { } } +// scient(SA-CODEX-PLUGINS): begin - Codex plugins read only the environment the agent was launched with. +/** Defined `true` only by the compiled binary's build (coding-agent `scripts/compile-binary.ts`), which turns Bun's dotenv loading off. */ +declare const __SCIENT_AGENT_NO_DOTENV__: boolean | undefined; + +/** Bun loads a dotenv file it is told to (`--env-file`, directly or through `BUN_OPTIONS`) even with `--no-env-file`. */ +const explicitEnvFile = [...process.execArgv, ...(Bun.env.BUN_OPTIONS?.split(/\s+/) ?? [])].some( + arg => arg === "--env-file" || arg.startsWith("--env-file="), +); + +/** + * The exec environment, when this process can know it: procfs or `--no-env-file` (see + * {@link readLaunchEnv}) without an explicit env file, or, in the compiled binary, `Bun.env` + * here, before the agent's own `.env` files load below. + */ +const launchEnvSnapshot: ReadonlyMap | undefined = explicitEnvFile + ? undefined + : (launchEnvValues ?? + (typeof __SCIENT_AGENT_NO_DOTENV__ !== "undefined" && __SCIENT_AGENT_NO_DOTENV__ === true + ? new Map(Object.entries(Bun.env).filter((entry): entry is [string, string] => entry[1] !== undefined)) + : undefined)); + +/** + * The environment the agent was launched with, whichever folder it works in now, or undefined + * when this run cannot tell it apart from what dotenv files added (Bun autoloaded them, as when + * the agent runs from source without `--no-env-file`, and there is no procfs). Codex reads its + * home, its plugins' credentials and their servers' variables from its launch environment alone; + * so does the agent when it runs Codex's plugins. + */ +export function getLaunchEnv(): Record | undefined { + return launchEnvSnapshot && Object.fromEntries(launchEnvSnapshot); +} +// scient(SA-CODEX-PLUGINS): end for (const file of [projectEnv, agentEnv, piEnv, homeEnv]) { for (const key in file) { // scient(SA-ROOT): begin - A host-assigned root owns the agent's own variables. diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 49188463ad..32ea71c0de 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -69,7 +69,7 @@ }, "SA-PRODUCT": { "status": "active", - "contract": "Scient Agent reports its own release: scient.json names the product, its version and the exact Oh My Pi release; --version, the startup screen, completions and the User-Agent report the version, and --runtime-info reports all of it with the build id Scient's workflow stamps and the Scient commands a host may run (hostCommands).", + "contract": "Scient Agent reports its own release: scient.json names the product, its version and the exact Oh My Pi release; --version, the startup screen, completions and the User-Agent report the version, and --runtime-info reports all of it with the build id Scient's workflow stamps, the Scient commands a host may run (hostCommands) and the plugin sources a host may add with SCIENT_AGENT_ENABLE_SOURCES (enableSources).", "owned": [ "scient.json", "scripts/scient/stamp-build-id.ts", @@ -517,6 +517,45 @@ "verification": [ "bun test packages/coding-agent/test/scient/xdev-catalog.test.ts packages/coding-agent/test/scient/eval-docs.test.ts" ] + }, + "SA-CODEX-PLUGINS": { + "status": "active", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it. A host shows and signs in to MCP servers through scient-agent mcp (scient/mcp-command.ts, registered in cli-commands.ts and listed in --runtime-info hostCommands), which discovers with the user's settings bound (initializeWithSettings), so enabledProviders and disabledProviders apply as in a conversation: mcp list [--server ] connects each enabled server as a conversation would and prints one JSON line of its status (connected with its tool count, needs-auth when an http or sse server answers with an auth challenge, failed, or pending when the startup window closes first), scope, plugin ID and whether a browser sign-in serves it (canSignIn in scient/mcp-sign-in-pickup.ts: http or sse, no Authorization header, and for a bound server only auth.type oauth); mcp login runs the reauthorization /mcp reauth runs on the definition discovery selected (an imported server's from the manager, never a same-named config-file entry it shadowed; an ordinary server's from the file holding that same definition), printing the authorization URL as a JSON line for the host to open (it opens no browser itself), then completed or failed. Every message either command prints is written there, an HTTP status at most, never a server's or authorization server's own text, which can echo a token, a code or a secret. While a broker holds the sign-ins, a bound server that configures a client secret is neither offered (no canStartSignIn) nor signed in to (reauthorization refuses it in mcp/oauth-reauth.ts), since the broker refreshes without the definition and so without the secret. In RPC mode (modes/rpc/rpc-mode.ts) an McpSignInPickup, seeded with the sign-ins the session started with, runs before each prompt and reconnects a disconnected server a browser sign-in serves once its stored sign-in differs from the one it started with or last tried, so a refused sign-in is not retried every prompt; one pickup runs at a time, a prompt waits for it at most 1.5 seconds while the rest finishes in the background, a broker's sign-ins are re-read at most every 10 seconds, and the session's tools are refreshed when a server connects. Unchanged from Oh My Pi and the same for every MCP server: the transport's connection error and the token exchange's error carry the response text, and a failed refresh is logged with its error.", + "owned": [ + "packages/coding-agent/src/scient/codex-plugins.ts", + "packages/coding-agent/test/scient/codex-plugins.test.ts", + "packages/coding-agent/test/scient/codex-plugins-oauth.test.ts", + "packages/coding-agent/src/scient/mcp-command.ts", + "packages/coding-agent/src/scient/mcp-sign-in-pickup.ts", + "packages/coding-agent/test/scient/mcp-host-command.test.ts" + ], + "upstream": [ + "packages/coding-agent/scripts/compile-binary.ts", + "packages/coding-agent/src/capability/index.ts", + "packages/coding-agent/src/capability/mcp.ts", + "packages/coding-agent/src/cli/auth-broker-cli.ts", + "packages/coding-agent/src/cli-commands.ts", + "packages/coding-agent/src/discovery/index.ts", + "packages/coding-agent/src/mcp/config.ts", + "packages/coding-agent/src/mcp/manager.ts", + "packages/coding-agent/src/mcp/oauth-credentials.ts", + "packages/coding-agent/src/mcp/oauth-flow.ts", + "packages/coding-agent/src/mcp/oauth-reauth.ts", + "packages/coding-agent/src/mcp/timeout.ts", + "packages/coding-agent/src/mcp/tool-bridge.ts", + "packages/coding-agent/src/mcp/transports/http.ts", + "packages/coding-agent/src/mcp/transports/sse.ts", + "packages/coding-agent/src/mcp/transports/stdio.ts", + "packages/coding-agent/src/mcp/types.ts", + "packages/coding-agent/src/modes/controllers/mcp-command-controller.ts", + "packages/coding-agent/src/modes/rpc/rpc-mode.ts", + "packages/coding-agent/src/slash-commands/helpers/mcp.ts", + "packages/coding-agent/CHANGELOG.md", + "packages/utils/src/env.ts" + ], + "verification": [ + "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts test/scient/codex-plugins-oauth.test.ts test/scient/mcp-host-command.test.ts" + ] } } }