π OpenHands: AI-Driven Development
[codex] Fix ACP MCP auth header forwarding (#1164)
Graham Neubiga289be75931e
diff --git a/__tests__/components/features/mcp-page/install-server-modal.test.tsx b/__tests__/components/features/mcp-page/install-server-modal.test.tsxindex b7dcf2cac1..dab1ce66c0 100644--- a/__tests__/components/features/mcp-page/install-server-modal.test.tsx+++ b/__tests__/components/features/mcp-page/install-server-modal.test.tsx@@ -251,7 +251,10 @@ describe("InstallServerModal", () => {mcp_config: { mcpServers: Record<string, unknown> };};expect(sent.mcp_config.mcpServers).toMatchObject({- shttp: { url: "https://mcp.linear.app/mcp", auth: "lin_api_secret" },+ shttp: {+ url: "https://mcp.linear.app/mcp",+ headers: { Authorization: "Bearer lin_api_secret" },+ },});});diff --git a/__tests__/utils/mcp-config.test.ts b/__tests__/utils/mcp-config.test.tsindex 691018fbec..61d2dceec7 100644--- a/__tests__/utils/mcp-config.test.ts+++ b/__tests__/utils/mcp-config.test.ts@@ -107,6 +107,49 @@ describe("toSdkMcpConfig", () => {).toBeNull();});+ it("serializes remote API keys as authorization headers for SDK/ACP forwarding", () => {+ const config: MCPConfig = {+ sse_servers: [{ url: "https://sse.example", api_key: "sse-secret" }],+ shttp_servers: [+ { url: "https://shttp.example", api_key: "shttp-secret" },+ ],+ stdio_servers: [],+ };++ const out = toSdkMcpConfig(config);++ expect(out).toEqual({+ mcpServers: {+ sse: {+ url: "https://sse.example",+ transport: "sse",+ headers: { Authorization: "Bearer sse-secret" },+ },+ shttp: {+ url: "https://shttp.example",+ headers: { Authorization: "Bearer shttp-secret" },+ },+ },+ });+ });++ it("round-trips persisted authorization headers back to frontend api_key fields", () => {+ const persisted = {+ mcpServers: {+ shttp: {+ url: "https://shttp.example",+ headers: { Authorization: "Bearer shttp-secret" },+ },+ },+ };++ const parsed = parseMcpConfig(persisted);++ expect(parsed.shttp_servers).toEqual([+ { url: "https://shttp.example", api_key: "shttp-secret" },+ ]);+ });+it("keeps names stable across a parse β write round trip", () => {// Simulates loading the user's persisted settings, parsing them,// and re-serializing on save (which is what happens on every edit).@@ -249,4 +292,27 @@ describe("parseMcpConfig β deprecated Linear SSE migration", () => {mcpServers: { shttp: { url: "https://mcp.linear.app/mcp" } },});});++ it("persists migrated legacy Linear credentials as authorization headers", () => {+ const persisted = {+ mcpServers: {+ sse: {+ url: "https://mcp.linear.app/sse",+ transport: "sse",+ auth: "lin_api_secret",+ },+ },+ };++ const written = toSdkMcpConfig(parseMcpConfig(persisted));++ expect(written).toEqual({+ mcpServers: {+ shttp: {+ url: "https://mcp.linear.app/mcp",+ headers: { Authorization: "Bearer lin_api_secret" },+ },+ },+ });+ });});diff --git a/src/utils/mcp-config.ts b/src/utils/mcp-config.tsindex 14e36f729e..c97e37d4b3 100644--- a/src/utils/mcp-config.ts+++ b/src/utils/mcp-config.ts@@ -34,6 +34,43 @@ function isDeprecatedLinearSse(type SdkMcpServerConfig = Record<string, SettingsValue>;type SdkMcpConfig = { mcpServers: Record<string, SdkMcpServerConfig> };+function apiKeyFromAuthorizationHeader(value: unknown): string | undefined {+ if (Array.isArray(value)) {+ return value+ .map(apiKeyFromAuthorizationHeader)+ .find((apiKey) => apiKey !== undefined);+ }++ if (typeof value !== "string" || value.length === 0) return undefined;+ const bearer = value.match(/^Bearer\s+(.+)$/i);+ return bearer ? bearer[1] : value;+}++function apiKeyFromServerConfig(+ serverConfig: Record<string, unknown>,+): string | undefined {+ const headers = serverConfig.headers;+ const authorization =+ headers && typeof headers === "object"+ ? ((headers as Record<string, unknown>).Authorization ??+ (headers as Record<string, unknown>).authorization)+ : undefined;+ const headerApiKey = apiKeyFromAuthorizationHeader(authorization);+ if (headerApiKey) return headerApiKey;++ const auth = serverConfig.auth;+ return typeof auth === "string" && auth !== "oauth" ? auth : undefined;+}++function getAuthorizationHeaders(apiKey: string | undefined) {+ if (!apiKey) return {};+ return {+ headers: {+ Authorization: `Bearer ${apiKey}`,+ },+ };+}+/*** Parse an SDK mcp_config value ({ mcpServers: { ... } }) and convert it* to the frontend MCPConfig format used by UI components.@@ -70,9 +107,7 @@ export function parseMcpConfig(value: unknown): MCPConfig {if (url) {const transport = serverConfig.transport as string | undefined;- const auth = serverConfig.auth as string | undefined;- const apiKey =- typeof auth === "string" && auth !== "oauth" ? auth : undefined;+ const apiKey = apiKeyFromServerConfig(serverConfig);if (isDeprecatedLinearSse(url, transport)) {const server: MCPSHTTPServer = { url: LINEAR_SHTTP_URL };@@ -149,7 +184,7 @@ export function toSdkMcpConfig(config: MCPConfig): SdkMcpConfig | null {server.url = entry;} else {server.url = entry.url;- if (entry.api_key) server.auth = entry.api_key;+ Object.assign(server, getAuthorizationHeaders(entry.api_key));}server.transport = "sse";mcpServers[reserve("sse")] = server;@@ -161,7 +196,7 @@ export function toSdkMcpConfig(config: MCPConfig): SdkMcpConfig | null {server.url = entry;} else {server.url = entry.url;- if (entry.api_key) server.auth = entry.api_key;+ Object.assign(server, getAuthorizationHeaders(entry.api_key));if (entry.timeout != null) server.timeout = entry.timeout;}mcpServers[reserve("shttp")] = server;diff --git a/tests/e2e/mock-llm/mcp/mock-llm-mcp-github.spec.ts b/tests/e2e/mock-llm/mcp/mock-llm-mcp-github.spec.tsindex 814de1ae7c..85b4a387ed 100644--- a/tests/e2e/mock-llm/mcp/mock-llm-mcp-github.spec.ts+++ b/tests/e2e/mock-llm/mcp/mock-llm-mcp-github.spec.ts@@ -152,13 +152,16 @@ test.describe("MCP GitHub server install flow", () => {const mcpConfig = settings?.agent_settings?.mcp_config;expect(mcpConfig).toBeTruthy();- // The GitHub server should be stored as a hosted streamable HTTP server- // with the PAT saved as its auth credential.+ // The GitHub server should be stored as a hosted streamable HTTP server.+ // The settings API redacts persisted secrets, so the raw PAT must not be+ // readable after installation.const mcpServers = mcpConfig?.mcpServers ?? mcpConfig?.shttp_servers;expect(mcpServers).toBeTruthy();expect(mcpServers?.shttp).toMatchObject({url: GITHUB_HOSTED_MCP_URL,- auth: FAKE_PAT,+ headers: {+ Authorization: "<redacted>",+ },});});@@ -177,7 +180,9 @@ test.describe("MCP GitHub server install flow", () => {mcpServers: {shttp: {url: GITHUB_HOSTED_MCP_URL,- auth: FAKE_PAT,+ headers: {+ Authorization: `Bearer ${FAKE_PAT}`,+ },},},},