|
| 1 | +import { sanitizeMcpName, buildMcpToolName, parseMcpToolName, MCP_TOOL_SEPARATOR, MCP_TOOL_PREFIX } from "../mcp-name" |
| 2 | + |
| 3 | +describe("mcp-name utilities", () => { |
| 4 | + describe("constants", () => { |
| 5 | + it("should have correct separator and prefix", () => { |
| 6 | + expect(MCP_TOOL_SEPARATOR).toBe("--") |
| 7 | + expect(MCP_TOOL_PREFIX).toBe("mcp") |
| 8 | + }) |
| 9 | + }) |
| 10 | + |
| 11 | + describe("sanitizeMcpName", () => { |
| 12 | + it("should return underscore placeholder for empty input", () => { |
| 13 | + expect(sanitizeMcpName("")).toBe("_") |
| 14 | + }) |
| 15 | + |
| 16 | + it("should replace spaces with underscores", () => { |
| 17 | + expect(sanitizeMcpName("my server")).toBe("my_server") |
| 18 | + expect(sanitizeMcpName("server name here")).toBe("server_name_here") |
| 19 | + }) |
| 20 | + |
| 21 | + it("should remove invalid characters", () => { |
| 22 | + expect(sanitizeMcpName("server@name!")).toBe("servername") |
| 23 | + expect(sanitizeMcpName("test#$%^&*()")).toBe("test") |
| 24 | + }) |
| 25 | + |
| 26 | + it("should keep valid characters (alphanumeric, underscore, dot, colon, dash)", () => { |
| 27 | + expect(sanitizeMcpName("server_name")).toBe("server_name") |
| 28 | + expect(sanitizeMcpName("server.name")).toBe("server.name") |
| 29 | + expect(sanitizeMcpName("server:name")).toBe("server:name") |
| 30 | + expect(sanitizeMcpName("server-name")).toBe("server-name") |
| 31 | + expect(sanitizeMcpName("Server123")).toBe("Server123") |
| 32 | + }) |
| 33 | + |
| 34 | + it("should prepend underscore if name starts with non-letter/underscore", () => { |
| 35 | + expect(sanitizeMcpName("123server")).toBe("_123server") |
| 36 | + expect(sanitizeMcpName("-server")).toBe("_-server") |
| 37 | + expect(sanitizeMcpName(".server")).toBe("_.server") |
| 38 | + }) |
| 39 | + |
| 40 | + it("should not modify names that start with letter or underscore", () => { |
| 41 | + expect(sanitizeMcpName("server")).toBe("server") |
| 42 | + expect(sanitizeMcpName("_server")).toBe("_server") |
| 43 | + expect(sanitizeMcpName("Server")).toBe("Server") |
| 44 | + }) |
| 45 | + |
| 46 | + it("should replace double-hyphen sequences with single hyphen to avoid separator conflicts", () => { |
| 47 | + expect(sanitizeMcpName("server--name")).toBe("server-name") |
| 48 | + expect(sanitizeMcpName("test---server")).toBe("test-server") |
| 49 | + expect(sanitizeMcpName("my----tool")).toBe("my-tool") |
| 50 | + }) |
| 51 | + |
| 52 | + it("should handle complex names with multiple issues", () => { |
| 53 | + expect(sanitizeMcpName("My Server @ Home!")).toBe("My_Server__Home") |
| 54 | + expect(sanitizeMcpName("123-test server")).toBe("_123-test_server") |
| 55 | + }) |
| 56 | + |
| 57 | + it("should return placeholder for names that become empty after sanitization", () => { |
| 58 | + expect(sanitizeMcpName("@#$%")).toBe("_unnamed") |
| 59 | + // Spaces become underscores, which is a valid character, so it returns "_" |
| 60 | + expect(sanitizeMcpName(" ")).toBe("_") |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe("buildMcpToolName", () => { |
| 65 | + it("should build tool name with mcp-- prefix and -- separators", () => { |
| 66 | + expect(buildMcpToolName("server", "tool")).toBe("mcp--server--tool") |
| 67 | + }) |
| 68 | + |
| 69 | + it("should sanitize both server and tool names", () => { |
| 70 | + expect(buildMcpToolName("my server", "my tool")).toBe("mcp--my_server--my_tool") |
| 71 | + }) |
| 72 | + |
| 73 | + it("should handle names with special characters", () => { |
| 74 | + expect(buildMcpToolName("server@name", "tool!name")).toBe("mcp--servername--toolname") |
| 75 | + }) |
| 76 | + |
| 77 | + it("should truncate long names to 64 characters", () => { |
| 78 | + const longServer = "a".repeat(50) |
| 79 | + const longTool = "b".repeat(50) |
| 80 | + const result = buildMcpToolName(longServer, longTool) |
| 81 | + expect(result.length).toBeLessThanOrEqual(64) |
| 82 | + expect(result.startsWith("mcp--")).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it("should handle names starting with numbers", () => { |
| 86 | + expect(buildMcpToolName("123server", "456tool")).toBe("mcp--_123server--_456tool") |
| 87 | + }) |
| 88 | + |
| 89 | + it("should preserve underscores in server and tool names", () => { |
| 90 | + expect(buildMcpToolName("my_server", "my_tool")).toBe("mcp--my_server--my_tool") |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe("parseMcpToolName", () => { |
| 95 | + it("should parse valid mcp tool names", () => { |
| 96 | + expect(parseMcpToolName("mcp--server--tool")).toEqual({ |
| 97 | + serverName: "server", |
| 98 | + toolName: "tool", |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it("should return null for non-mcp tool names", () => { |
| 103 | + expect(parseMcpToolName("server--tool")).toBeNull() |
| 104 | + expect(parseMcpToolName("tool")).toBeNull() |
| 105 | + }) |
| 106 | + |
| 107 | + it("should return null for old underscore format", () => { |
| 108 | + expect(parseMcpToolName("mcp_server_tool")).toBeNull() |
| 109 | + }) |
| 110 | + |
| 111 | + it("should handle tool names with underscores", () => { |
| 112 | + expect(parseMcpToolName("mcp--server--tool_name")).toEqual({ |
| 113 | + serverName: "server", |
| 114 | + toolName: "tool_name", |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it("should correctly handle server names with underscores (fixed from old behavior)", () => { |
| 119 | + // With the new -- separator, server names with underscores work correctly |
| 120 | + expect(parseMcpToolName("mcp--my_server--tool")).toEqual({ |
| 121 | + serverName: "my_server", |
| 122 | + toolName: "tool", |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it("should handle both server and tool names with underscores", () => { |
| 127 | + expect(parseMcpToolName("mcp--my_server--get_forecast")).toEqual({ |
| 128 | + serverName: "my_server", |
| 129 | + toolName: "get_forecast", |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it("should return null for malformed names", () => { |
| 134 | + expect(parseMcpToolName("mcp--")).toBeNull() |
| 135 | + expect(parseMcpToolName("mcp--server")).toBeNull() |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + describe("roundtrip behavior", () => { |
| 140 | + it("should be able to parse names that were built", () => { |
| 141 | + const toolName = buildMcpToolName("server", "tool") |
| 142 | + const parsed = parseMcpToolName(toolName) |
| 143 | + expect(parsed).toEqual({ |
| 144 | + serverName: "server", |
| 145 | + toolName: "tool", |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + it("should preserve sanitized names through roundtrip with underscores", () => { |
| 150 | + // Names with underscores now work correctly through roundtrip |
| 151 | + const toolName = buildMcpToolName("my_server", "my_tool") |
| 152 | + const parsed = parseMcpToolName(toolName) |
| 153 | + expect(parsed).toEqual({ |
| 154 | + serverName: "my_server", |
| 155 | + toolName: "my_tool", |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + it("should handle spaces that get converted to underscores", () => { |
| 160 | + // "my server" becomes "my_server" after sanitization |
| 161 | + const toolName = buildMcpToolName("my server", "get tool") |
| 162 | + const parsed = parseMcpToolName(toolName) |
| 163 | + expect(parsed).toEqual({ |
| 164 | + serverName: "my_server", |
| 165 | + toolName: "get_tool", |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + it("should handle complex server and tool names", () => { |
| 170 | + const toolName = buildMcpToolName("Weather API", "get_current_forecast") |
| 171 | + const parsed = parseMcpToolName(toolName) |
| 172 | + expect(parsed).toEqual({ |
| 173 | + serverName: "Weather_API", |
| 174 | + toolName: "get_current_forecast", |
| 175 | + }) |
| 176 | + }) |
| 177 | + }) |
| 178 | +}) |
0 commit comments