|
|
@@ -0,0 +1,178 @@
|
|
|
+import { sanitizeMcpName, buildMcpToolName, parseMcpToolName, MCP_TOOL_SEPARATOR, MCP_TOOL_PREFIX } from "../mcp-name"
|
|
|
+
|
|
|
+describe("mcp-name utilities", () => {
|
|
|
+ describe("constants", () => {
|
|
|
+ it("should have correct separator and prefix", () => {
|
|
|
+ expect(MCP_TOOL_SEPARATOR).toBe("--")
|
|
|
+ expect(MCP_TOOL_PREFIX).toBe("mcp")
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe("sanitizeMcpName", () => {
|
|
|
+ it("should return underscore placeholder for empty input", () => {
|
|
|
+ expect(sanitizeMcpName("")).toBe("_")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should replace spaces with underscores", () => {
|
|
|
+ expect(sanitizeMcpName("my server")).toBe("my_server")
|
|
|
+ expect(sanitizeMcpName("server name here")).toBe("server_name_here")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should remove invalid characters", () => {
|
|
|
+ expect(sanitizeMcpName("server@name!")).toBe("servername")
|
|
|
+ expect(sanitizeMcpName("test#$%^&*()")).toBe("test")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should keep valid characters (alphanumeric, underscore, dot, colon, dash)", () => {
|
|
|
+ expect(sanitizeMcpName("server_name")).toBe("server_name")
|
|
|
+ expect(sanitizeMcpName("server.name")).toBe("server.name")
|
|
|
+ expect(sanitizeMcpName("server:name")).toBe("server:name")
|
|
|
+ expect(sanitizeMcpName("server-name")).toBe("server-name")
|
|
|
+ expect(sanitizeMcpName("Server123")).toBe("Server123")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should prepend underscore if name starts with non-letter/underscore", () => {
|
|
|
+ expect(sanitizeMcpName("123server")).toBe("_123server")
|
|
|
+ expect(sanitizeMcpName("-server")).toBe("_-server")
|
|
|
+ expect(sanitizeMcpName(".server")).toBe("_.server")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should not modify names that start with letter or underscore", () => {
|
|
|
+ expect(sanitizeMcpName("server")).toBe("server")
|
|
|
+ expect(sanitizeMcpName("_server")).toBe("_server")
|
|
|
+ expect(sanitizeMcpName("Server")).toBe("Server")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should replace double-hyphen sequences with single hyphen to avoid separator conflicts", () => {
|
|
|
+ expect(sanitizeMcpName("server--name")).toBe("server-name")
|
|
|
+ expect(sanitizeMcpName("test---server")).toBe("test-server")
|
|
|
+ expect(sanitizeMcpName("my----tool")).toBe("my-tool")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle complex names with multiple issues", () => {
|
|
|
+ expect(sanitizeMcpName("My Server @ Home!")).toBe("My_Server__Home")
|
|
|
+ expect(sanitizeMcpName("123-test server")).toBe("_123-test_server")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should return placeholder for names that become empty after sanitization", () => {
|
|
|
+ expect(sanitizeMcpName("@#$%")).toBe("_unnamed")
|
|
|
+ // Spaces become underscores, which is a valid character, so it returns "_"
|
|
|
+ expect(sanitizeMcpName(" ")).toBe("_")
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe("buildMcpToolName", () => {
|
|
|
+ it("should build tool name with mcp-- prefix and -- separators", () => {
|
|
|
+ expect(buildMcpToolName("server", "tool")).toBe("mcp--server--tool")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should sanitize both server and tool names", () => {
|
|
|
+ expect(buildMcpToolName("my server", "my tool")).toBe("mcp--my_server--my_tool")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle names with special characters", () => {
|
|
|
+ expect(buildMcpToolName("server@name", "tool!name")).toBe("mcp--servername--toolname")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should truncate long names to 64 characters", () => {
|
|
|
+ const longServer = "a".repeat(50)
|
|
|
+ const longTool = "b".repeat(50)
|
|
|
+ const result = buildMcpToolName(longServer, longTool)
|
|
|
+ expect(result.length).toBeLessThanOrEqual(64)
|
|
|
+ expect(result.startsWith("mcp--")).toBe(true)
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle names starting with numbers", () => {
|
|
|
+ expect(buildMcpToolName("123server", "456tool")).toBe("mcp--_123server--_456tool")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should preserve underscores in server and tool names", () => {
|
|
|
+ expect(buildMcpToolName("my_server", "my_tool")).toBe("mcp--my_server--my_tool")
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe("parseMcpToolName", () => {
|
|
|
+ it("should parse valid mcp tool names", () => {
|
|
|
+ expect(parseMcpToolName("mcp--server--tool")).toEqual({
|
|
|
+ serverName: "server",
|
|
|
+ toolName: "tool",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should return null for non-mcp tool names", () => {
|
|
|
+ expect(parseMcpToolName("server--tool")).toBeNull()
|
|
|
+ expect(parseMcpToolName("tool")).toBeNull()
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should return null for old underscore format", () => {
|
|
|
+ expect(parseMcpToolName("mcp_server_tool")).toBeNull()
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle tool names with underscores", () => {
|
|
|
+ expect(parseMcpToolName("mcp--server--tool_name")).toEqual({
|
|
|
+ serverName: "server",
|
|
|
+ toolName: "tool_name",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should correctly handle server names with underscores (fixed from old behavior)", () => {
|
|
|
+ // With the new -- separator, server names with underscores work correctly
|
|
|
+ expect(parseMcpToolName("mcp--my_server--tool")).toEqual({
|
|
|
+ serverName: "my_server",
|
|
|
+ toolName: "tool",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle both server and tool names with underscores", () => {
|
|
|
+ expect(parseMcpToolName("mcp--my_server--get_forecast")).toEqual({
|
|
|
+ serverName: "my_server",
|
|
|
+ toolName: "get_forecast",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should return null for malformed names", () => {
|
|
|
+ expect(parseMcpToolName("mcp--")).toBeNull()
|
|
|
+ expect(parseMcpToolName("mcp--server")).toBeNull()
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe("roundtrip behavior", () => {
|
|
|
+ it("should be able to parse names that were built", () => {
|
|
|
+ const toolName = buildMcpToolName("server", "tool")
|
|
|
+ const parsed = parseMcpToolName(toolName)
|
|
|
+ expect(parsed).toEqual({
|
|
|
+ serverName: "server",
|
|
|
+ toolName: "tool",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should preserve sanitized names through roundtrip with underscores", () => {
|
|
|
+ // Names with underscores now work correctly through roundtrip
|
|
|
+ const toolName = buildMcpToolName("my_server", "my_tool")
|
|
|
+ const parsed = parseMcpToolName(toolName)
|
|
|
+ expect(parsed).toEqual({
|
|
|
+ serverName: "my_server",
|
|
|
+ toolName: "my_tool",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle spaces that get converted to underscores", () => {
|
|
|
+ // "my server" becomes "my_server" after sanitization
|
|
|
+ const toolName = buildMcpToolName("my server", "get tool")
|
|
|
+ const parsed = parseMcpToolName(toolName)
|
|
|
+ expect(parsed).toEqual({
|
|
|
+ serverName: "my_server",
|
|
|
+ toolName: "get_tool",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle complex server and tool names", () => {
|
|
|
+ const toolName = buildMcpToolName("Weather API", "get_current_forecast")
|
|
|
+ const parsed = parseMcpToolName(toolName)
|
|
|
+ expect(parsed).toEqual({
|
|
|
+ serverName: "Weather_API",
|
|
|
+ toolName: "get_current_forecast",
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+})
|