Context7 Platform -- Up-to-date docs for AI agents
fix(cli): strip --api-key=<value> when setup rewrites a stdio entry (#3441)
Abhishek B R7f6943de1926
diff --git a/.changeset/strip-inline-api-key.md b/.changeset/strip-inline-api-key.mdnew file mode 100644index 00000000..9dfc1c70--- /dev/null+++ b/.changeset/strip-inline-api-key.md@@ -0,0 +1,5 @@+---+"ctx7": patch+---++`ctx7 setup` now removes an existing `--api-key=<value>` arg when it rewrites a stdio MCP entry, so switching to OAuth or rotating the key no longer leaves the old key in the config.diff --git a/packages/cli/src/__tests__/setup.test.ts b/packages/cli/src/__tests__/setup.test.tsindex 88f10cc1..4bad5b2e 100644--- a/packages/cli/src/__tests__/setup.test.ts+++ b/packages/cli/src/__tests__/setup.test.ts@@ -993,6 +993,22 @@ describe("agent config integration", () => {expect(patched.args).toEqual(["-y", "@upstash/context7-mcp"]);});+ test("replaces an inline --api-key=<value> arg", () => {+ const patched = patchStdioApiKey(+ { command: "npx", args: ["-y", "@upstash/context7-mcp", "--api-key=OLD"] },+ "NEW"+ );+ expect(patched.args).toEqual(["-y", "@upstash/context7-mcp", "--api-key", "NEW"]);+ });++ test("removes an inline --api-key=<value> arg when new key is undefined (oauth)", () => {+ const patched = patchStdioApiKey(+ { command: "npx", args: ["-y", "@upstash/context7-mcp", "--api-key=OLD"] },+ undefined+ );+ expect(patched.args).toEqual(["-y", "@upstash/context7-mcp"]);+ });+test("preserves other args (e.g. --debug) untouched", () => {const patched = patchStdioApiKey({diff --git a/packages/cli/src/__tests__/toml-editor.test.ts b/packages/cli/src/__tests__/toml-editor.test.tsindex ed8125c8..9b72a8b8 100644--- a/packages/cli/src/__tests__/toml-editor.test.ts+++ b/packages/cli/src/__tests__/toml-editor.test.ts@@ -133,6 +133,24 @@ const mutations: MutationCase[] = [apiKey: undefined,expectedArgs: `["${PACKAGE}"]`,},+ {+ name: "replaces an inline --api-key= key",+ args: `["${PACKAGE}", "--api-key=OLD"]`,+ apiKey: "NEW",+ expectedArgs: `["${PACKAGE}","--api-key","NEW"]`,+ },+ {+ name: "removes an inline --api-key= key",+ args: `["${PACKAGE}", "--api-key=OLD"]`,+ apiKey: undefined,+ expectedArgs: `["${PACKAGE}"]`,+ },+ {+ name: "drops an inline key next to a separate one",+ args: `["${PACKAGE}", "--api-key=A", "--api-key", "B"]`,+ apiKey: "NEW",+ expectedArgs: `["${PACKAGE}","--api-key","NEW"]`,+ },{name: "normalizes duplicate keys",args: `["${PACKAGE}", "--api-key", "A", "--api-key", "B"]`,diff --git a/packages/cli/src/setup/mcp-writer.ts b/packages/cli/src/setup/mcp-writer.tsindex f17a2ff1..d6829f7c 100644--- a/packages/cli/src/setup/mcp-writer.ts+++ b/packages/cli/src/setup/mcp-writer.ts@@ -145,6 +145,7 @@ function stripApiKeyPair(args: string[]): string[] {i++; // skip the value toocontinue;}+ if (args[i].startsWith("--api-key=")) continue;result.push(args[i]);}return result;diff --git a/packages/cli/src/setup/toml-editor.ts b/packages/cli/src/setup/toml-editor.tsindex e2065a42..8c385cdb 100644--- a/packages/cli/src/setup/toml-editor.ts+++ b/packages/cli/src/setup/toml-editor.ts@@ -155,6 +155,7 @@ function withoutApiKey(args: string[]): string[] {index++;continue;}+ if (args[index].startsWith("--api-key=")) continue;result.push(args[index]);}return result;@@ -196,7 +197,13 @@ export async function patchTomlStdioApiKey(const apiKeyIndexes = args.flatMap((arg, index) => (arg === "--api-key" ? [index] : []));let content: string;- if (apiKey && apiKeyIndexes.length === 1 && apiKeyIndexes[0] + 1 < range.tokens.length) {+ const hasInlineApiKey = args.some((arg) => arg.startsWith("--api-key="));+ if (+ apiKey &&+ !hasInlineApiKey &&+ apiKeyIndexes.length === 1 &&+ apiKeyIndexes[0] + 1 < range.tokens.length+ ) {const valueToken = range.tokens[apiKeyIndexes[0] + 1];content = raw.slice(0, valueToken.start) + JSON.stringify(apiKey) + raw.slice(valueToken.end);} else {