π OpenHands: AI-Driven Development
feat(acp): allow MCP server config for ACP agents (#14613)
simonrosenberg70b8365196ea
diff --git a/frontend/__tests__/hooks/use-settings-nav-items.test.tsx b/frontend/__tests__/hooks/use-settings-nav-items.test.tsxindex 0bab1ba320..28ceb94439 100644--- a/frontend/__tests__/hooks/use-settings-nav-items.test.tsx+++ b/frontend/__tests__/hooks/use-settings-nav-items.test.tsx@@ -552,3 +552,30 @@ describe("useSettingsNavItems", () => {});});});++describe("disabledByAcp flags (ACP-incompatible settings surfaces)", () => {+ // The ACP sub-agent owns its own LLM and condenser, so those pages stay+ // greyed-out/redirected under ACP. MCP is intentionally NOT gated: servers+ // configured on the MCP page are forwarded to the ACP subprocess at session+ // creation, so the page is meaningful for ACP agents too.+ it.each([+ ["SAAS_NAV_ITEMS", SAAS_NAV_ITEMS],+ ["OSS_NAV_ITEMS", OSS_NAV_ITEMS],+ ])("does not gate the MCP page for ACP in %s", (_name, items) => {+ const mcp = items.find((item) => item.to === "/settings/mcp");+ expect(mcp).toBeDefined();+ expect(mcp?.disabledByAcp ?? false).toBe(false);+ });++ it.each([+ ["SAAS_NAV_ITEMS", SAAS_NAV_ITEMS],+ ["OSS_NAV_ITEMS", OSS_NAV_ITEMS],+ ])("still gates LLM and condenser for ACP in %s", (_name, items) => {+ expect(items.find((item) => item.to === "/settings")?.disabledByAcp).toBe(+ true,+ );+ expect(+ items.find((item) => item.to === "/settings/condenser")?.disabledByAcp,+ ).toBe(true);+ });+});diff --git a/frontend/__tests__/utils/mcp-config.test.ts b/frontend/__tests__/utils/mcp-config.test.tsindex 721d9be869..73fdf51fdb 100644--- a/frontend/__tests__/utils/mcp-config.test.ts+++ b/frontend/__tests__/utils/mcp-config.test.ts@@ -223,13 +223,17 @@ describe("toSdkMcpConfig", () => {expect(result?.mcpServers["sse_2"].url).toBe("https://example3.com");});- it("should include api_key as auth field", () => {+ it("should serialize api_key as an Authorization bearer header", () => {+ // The SDK only redacts/encrypts ``headers`` (and ``env``), not ``auth`` β+ // writing the key to ``auth`` would persist it in plaintext.const config: MCPConfig = {sse_servers: [{ name: "secure", url: "https://example.com", api_key: "my-secret" },],stdio_servers: [],- shttp_servers: [],+ shttp_servers: [+ { name: "shttp", url: "https://shttp.example", api_key: "shttp-secret" },+ ],};const result = toSdkMcpConfig(config);@@ -237,7 +241,52 @@ describe("toSdkMcpConfig", () => {expect(result?.mcpServers["secure"]).toEqual({url: "https://example.com",transport: "sse",- auth: "my-secret",+ headers: { Authorization: "Bearer my-secret" },+ });+ expect(result?.mcpServers["shttp"]).toEqual({+ url: "https://shttp.example",+ headers: { Authorization: "Bearer shttp-secret" },+ });+ });++ it("round-trips persisted Authorization headers back to 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([+ { name: "shttp", url: "https://shttp.example", api_key: "shttp-secret" },+ ]);+ });++ it("migrates legacy auth credentials to Authorization headers on re-serialize", () => {+ const persisted = {+ mcpServers: {+ sse: {+ url: "https://example.com",+ transport: "sse",+ auth: "legacy-secret",+ },+ },+ };++ const written = toSdkMcpConfig(parseMcpConfig(persisted));++ expect(written).toEqual({+ mcpServers: {+ sse: {+ url: "https://example.com",+ transport: "sse",+ headers: { Authorization: "Bearer legacy-secret" },+ },+ },});});diff --git a/frontend/src/constants/settings-nav.tsx b/frontend/src/constants/settings-nav.tsxindex 6c2e8777fb..938defa682 100644--- a/frontend/src/constants/settings-nav.tsx+++ b/frontend/src/constants/settings-nav.tsx@@ -25,10 +25,12 @@ export interface SettingsNavItem {section?: SettingsNavSection;// When true, this item is greyed out (and its route redirects to// ``/settings/agent``) while the personal-scope active agent is ACP.- // The ACP sub-agent manages its own LLM / condenser / MCP, so these- // OpenHands-side surfaces have no useful content. Drives both the- // navigation disable in ``use-settings-nav-items.ts`` and the- // server-side redirect in ``routes/settings.tsx`` from one source.+ // The ACP sub-agent manages its own LLM and condenser, so those+ // OpenHands-side surfaces have no useful content. (MCP is intentionally+ // NOT flagged: MCP servers configured here are forwarded to the ACP+ // subprocess at session creation, so the page is meaningful under ACP.)+ // Drives both the navigation disable in ``use-settings-nav-items.ts``+ // and the server-side redirect in ``routes/settings.tsx`` from one source.disabledByAcp?: boolean;}@@ -106,7 +108,6 @@ export const SAAS_NAV_ITEMS: SettingsNavItem[] = [to: "/settings/mcp",text: "SETTINGS$NAV_MCP",section: "personal",- disabledByAcp: true,},{icon: <UserIcon width={22} height={22} />,@@ -167,7 +168,6 @@ export const OSS_NAV_ITEMS: SettingsNavItem[] = [icon: <ServerProcessIcon width={22} height={22} />,to: "/settings/mcp",text: "SETTINGS$NAV_MCP",- disabledByAcp: true,},{icon: <LightbulbIcon width={22} height={22} />,diff --git a/frontend/src/utils/mcp-config.ts b/frontend/src/utils/mcp-config.tsindex 020829d031..e50cbfc569 100644--- a/frontend/src/utils/mcp-config.ts+++ b/frontend/src/utils/mcp-config.ts@@ -15,6 +15,53 @@ const EMPTY_MCP_CONFIG: MCPConfig = {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;+}++/**+ * Recover a remote server's API key from either the canonical+ * ``headers.Authorization`` bearer token or the legacy ``auth`` field (kept+ * for back-compat with settings persisted before the header migration).+ */+function apiKeyFromServerConfig(+ serverConfig: Record<string, unknown>,+): string | undefined {+ const { headers } = serverConfig;+ 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;+ return typeof auth === "string" && auth !== "oauth" ? auth : undefined;+}++/**+ * Serialize an API key as an ``Authorization`` bearer header. The SDK only+ * redacts/encrypts ``headers`` (and ``env``), not ``auth``, so a key written+ * to ``auth`` would persist in plaintext β write the header form instead.+ */+function getAuthorizationHeaders(apiKey: string | undefined) {+ if (!apiKey) return {};+ return {+ headers: {+ Authorization: `Bearer ${apiKey}`,+ },+ };+}+/*** Generate a unique name for an MCP server, avoiding collisions with existing names.* Only adds a suffix if there's an actual collision.@@ -64,9 +111,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 (transport === "sse") {const server: MCPSSEServer = {@@ -124,7 +169,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";@@ -142,7 +187,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;}