1- import { sanitizeMcpName , buildMcpToolName , parseMcpToolName } from "../mcp-name"
1+ import { sanitizeMcpName , buildMcpToolName , parseMcpToolName , MCP_TOOL_SEPARATOR , MCP_TOOL_PREFIX } from "../mcp-name"
22
33describe ( "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+
411 describe ( "sanitizeMcpName" , ( ) => {
512 it ( "should return underscore placeholder for empty input" , ( ) => {
613 expect ( sanitizeMcpName ( "" ) ) . toBe ( "_" )
@@ -36,6 +43,12 @@ describe("mcp-name utilities", () => {
3643 expect ( sanitizeMcpName ( "Server" ) ) . toBe ( "Server" )
3744 } )
3845
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+
3952 it ( "should handle complex names with multiple issues" , ( ) => {
4053 expect ( sanitizeMcpName ( "My Server @ Home!" ) ) . toBe ( "My_Server__Home" )
4154 expect ( sanitizeMcpName ( "123-test server" ) ) . toBe ( "_123-test_server" )
@@ -49,65 +62,77 @@ describe("mcp-name utilities", () => {
4962 } )
5063
5164 describe ( "buildMcpToolName" , ( ) => {
52- it ( "should build tool name with mcp_ prefix" , ( ) => {
53- expect ( buildMcpToolName ( "server" , "tool" ) ) . toBe ( "mcp_server_tool " )
65+ it ( "should build tool name with mcp-- prefix and -- separators " , ( ) => {
66+ expect ( buildMcpToolName ( "server" , "tool" ) ) . toBe ( "mcp--server--tool " )
5467 } )
5568
5669 it ( "should sanitize both server and tool names" , ( ) => {
57- expect ( buildMcpToolName ( "my server" , "my tool" ) ) . toBe ( "mcp_my_server_my_tool " )
70+ expect ( buildMcpToolName ( "my server" , "my tool" ) ) . toBe ( "mcp--my_server--my_tool " )
5871 } )
5972
6073 it ( "should handle names with special characters" , ( ) => {
61- expect ( buildMcpToolName ( "server@name" , "tool!name" ) ) . toBe ( "mcp_servername_toolname " )
74+ expect ( buildMcpToolName ( "server@name" , "tool!name" ) ) . toBe ( "mcp--servername--toolname " )
6275 } )
6376
6477 it ( "should truncate long names to 64 characters" , ( ) => {
6578 const longServer = "a" . repeat ( 50 )
6679 const longTool = "b" . repeat ( 50 )
6780 const result = buildMcpToolName ( longServer , longTool )
6881 expect ( result . length ) . toBeLessThanOrEqual ( 64 )
69- expect ( result . startsWith ( "mcp_ " ) ) . toBe ( true )
82+ expect ( result . startsWith ( "mcp-- " ) ) . toBe ( true )
7083 } )
7184
7285 it ( "should handle names starting with numbers" , ( ) => {
73- expect ( buildMcpToolName ( "123server" , "456tool" ) ) . toBe ( "mcp__123server__456tool" )
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" )
7491 } )
7592 } )
7693
7794 describe ( "parseMcpToolName" , ( ) => {
7895 it ( "should parse valid mcp tool names" , ( ) => {
79- expect ( parseMcpToolName ( "mcp_server_tool " ) ) . toEqual ( {
96+ expect ( parseMcpToolName ( "mcp--server--tool " ) ) . toEqual ( {
8097 serverName : "server" ,
8198 toolName : "tool" ,
8299 } )
83100 } )
84101
85102 it ( "should return null for non-mcp tool names" , ( ) => {
86- expect ( parseMcpToolName ( "server_tool " ) ) . toBeNull ( )
103+ expect ( parseMcpToolName ( "server--tool " ) ) . toBeNull ( )
87104 expect ( parseMcpToolName ( "tool" ) ) . toBeNull ( )
88105 } )
89106
107+ it ( "should return null for old underscore format" , ( ) => {
108+ expect ( parseMcpToolName ( "mcp_server_tool" ) ) . toBeNull ( )
109+ } )
110+
90111 it ( "should handle tool names with underscores" , ( ) => {
91- expect ( parseMcpToolName ( "mcp_server_tool_name " ) ) . toEqual ( {
112+ expect ( parseMcpToolName ( "mcp--server--tool_name " ) ) . toEqual ( {
92113 serverName : "server" ,
93114 toolName : "tool_name" ,
94115 } )
95116 } )
96117
97- it ( "should handle server names with underscores (edge case)" , ( ) => {
98- // Note: parseMcpToolName uses simple split, so it can't distinguish
99- // server_name_tool from server + name_tool
100- // The first underscore after 'mcp_' is treated as the server/tool separator
101- const result = parseMcpToolName ( "mcp_my_server_tool" )
102- expect ( result ) . toEqual ( {
103- serverName : "my" ,
104- toolName : "server_tool" ,
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" ,
105130 } )
106131 } )
107132
108133 it ( "should return null for malformed names" , ( ) => {
109- expect ( parseMcpToolName ( "mcp_ " ) ) . toBeNull ( )
110- expect ( parseMcpToolName ( "mcp_server " ) ) . toBeNull ( )
134+ expect ( parseMcpToolName ( "mcp-- " ) ) . toBeNull ( )
135+ expect ( parseMcpToolName ( "mcp--server " ) ) . toBeNull ( )
111136 } )
112137 } )
113138
@@ -121,14 +146,32 @@ describe("mcp-name utilities", () => {
121146 } )
122147 } )
123148
124- it ( "should preserve sanitized names through roundtrip" , ( ) => {
125- // Names with spaces become underscores, but roundtrip still works
126- // for the sanitized version
149+ it ( "should preserve sanitized names through roundtrip with underscores" , ( ) => {
150+ // Names with underscores now work correctly through roundtrip
127151 const toolName = buildMcpToolName ( "my_server" , "my_tool" )
128152 const parsed = parseMcpToolName ( toolName )
129153 expect ( parsed ) . toEqual ( {
130- serverName : "my" ,
131- toolName : "server_my_tool" ,
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" ,
132175 } )
133176 } )
134177 } )
0 commit comments