π OpenHands: AI-Driven Development
fix: allow hyphenated MCP server names (#1620)
Graham Neubig7c580d6b50c1
diff --git a/__tests__/components/features/mcp-page/install-server-modal.test.tsx b/__tests__/components/features/mcp-page/install-server-modal.test.tsxindex e35e41c5fe..99e62d1a59 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,7 @@ describe("InstallServerModal", () => {mcp_config: Record<string, unknown>;};expect(sent.mcp_config).toMatchObject({- synthetic_oauth: {+ "synthetic-oauth": {url: "https://mcp.example.com/mcp",auth: {strategy: "oauth2",@@ -338,7 +338,7 @@ describe("InstallServerModal", () => {mcp_config: Record<string, unknown>;};expect(sent.mcp_config).toMatchObject({- datadog_style: {+ "datadog-style": {url: "https://mcp.example.com/mcp",auth: {strategy: "header",@@ -404,7 +404,7 @@ describe("InstallServerModal", () => {mcp_config: Record<string, unknown>;};expect(sent.mcp_config).toMatchObject({- datadog_style: {+ "datadog-style": {url: "https://mcp.us5.example.com/v1/mcp",auth: {strategy: "header",diff --git a/__tests__/components/features/settings/mcp-settings/mcp-server-form.validation.test.tsx b/__tests__/components/features/settings/mcp-settings/mcp-server-form.validation.test.tsxindex c3d2f6e236..82cd10f9ca 100644--- a/__tests__/components/features/settings/mcp-settings/mcp-server-form.validation.test.tsx+++ b/__tests__/components/features/settings/mcp-settings/mcp-server-form.validation.test.tsx@@ -86,7 +86,37 @@ describe("MCPServerForm validation", () => {});});- it("rejects hyphenated server names because they become tool prefixes", () => {+ it("allows hyphenated local stdio server names", () => {+ const onSubmit = vi.fn();++ render(+ <MCPServerForm+ mode="add"+ server={{ id: "tmp", type: "stdio" }}+ existingServers={[]}+ onSubmit={onSubmit}+ onCancel={noop}+ />,+ );++ fireEvent.change(screen.getByTestId("name-input"), {+ target: { value: "integrations-hub" },+ });+ fireEvent.change(screen.getByTestId("command-input"), {+ target: { value: "npx" },+ });++ fireEvent.click(screen.getByTestId("submit-button"));++ expect(onSubmit).toHaveBeenCalledTimes(1);+ expect(onSubmit.mock.calls[0][0]).toMatchObject({+ type: "stdio",+ name: "integrations-hub",+ command: "npx",+ });+ });++ it("allows hyphenated remote server names", () => {const onSubmit = vi.fn();render(@@ -108,10 +138,12 @@ describe("MCPServerForm validation", () => {fireEvent.click(screen.getByTestId("submit-button"));- expect(- screen.getByText("SETTINGS$MCP_ERROR_NAME_INVALID"),- ).toBeInTheDocument();- expect(onSubmit).not.toHaveBeenCalled();+ expect(onSubmit).toHaveBeenCalledTimes(1);+ expect(onSubmit.mock.calls[0][0]).toMatchObject({+ type: "shttp",+ name: "integrations-hub",+ url: "https://api.example.com/mcp",+ });});it("submits header authentication as a tagged auth credential", () => {diff --git a/__tests__/utils/mcp-config.test.ts b/__tests__/utils/mcp-config.test.tsindex 56c87501b7..828e743205 100644--- a/__tests__/utils/mcp-config.test.ts+++ b/__tests__/utils/mcp-config.test.ts@@ -117,7 +117,7 @@ describe("toSdkMcpConfig", () => {});});- it("normalizes MCP server names before using them as SDK keys", () => {+ it("preserves valid hyphenated MCP server names as SDK keys", () => {const config: MCPConfig = {sse_servers: [{ name: "integrations-hub", url: "https://hub.example/mcp" },@@ -129,8 +129,8 @@ describe("toSdkMcpConfig", () => {const out = toSdkMcpConfig(config);expect(Object.keys(out!)).toEqual([- "integrations_hub",- "docs_server",+ "integrations-hub",+ "docs-server",]);});diff --git a/src/utils/mcp-server-name.ts b/src/utils/mcp-server-name.tsindex f156c22fe3..a4984d33ed 100644--- a/src/utils/mcp-server-name.ts+++ b/src/utils/mcp-server-name.ts@@ -1,4 +1,4 @@-export const MCP_SERVER_NAME_PATTERN = /^[a-zA-Z0-9_]+$/;+export const MCP_SERVER_NAME_PATTERN = /^[a-zA-Z0-9_-]+$/;export function isValidMcpServerName(name: string): boolean {return MCP_SERVER_NAME_PATTERN.test(name);@@ -7,7 +7,7 @@ export function isValidMcpServerName(name: string): boolean {export function toMcpServerName(value: string, fallback = "mcp"): string {const normalized = value.trim()- .replace(/[^a-zA-Z0-9_]+/g, "_")+ .replace(/[^a-zA-Z0-9_-]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");