Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 865e9a3

Browse files
committed
fix: increase MAX_TOOL_NAME_LENGTH from 64 to 128 per MCP spec
1 parent a1cfe4c commit 865e9a3

2 files changed

Lines changed: 34 additions & 33 deletions

File tree

‎src/utils/__tests__/mcp-name.spec.ts‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("mcp-name utilities", () => {
3232
})
3333

3434
it("should have correct max tool name length", () => {
35-
expect(MAX_TOOL_NAME_LENGTH).toBe(64)
35+
expect(MAX_TOOL_NAME_LENGTH).toBe(128)
3636
})
3737

3838
it("should have correct hash suffix length", () => {
@@ -164,20 +164,20 @@ describe("mcp-name utilities", () => {
164164
expect(buildMcpToolName("server@name", "tool!name")).toBe("mcp--servername--toolname")
165165
})
166166

167-
it("should truncate long names to 64 characters with hash suffix", () => {
168-
const longServer = "a".repeat(50)
169-
const longTool = "b".repeat(50)
167+
it("should truncate long names to 128 characters with hash suffix", () => {
168+
const longServer = "a".repeat(80)
169+
const longTool = "b".repeat(80)
170170
const result = buildMcpToolName(longServer, longTool)
171-
expect(result.length).toBeLessThanOrEqual(64)
172-
expect(result.length).toBe(64)
171+
expect(result.length).toBeLessThanOrEqual(128)
172+
expect(result.length).toBe(128)
173173
expect(result.startsWith("mcp--")).toBe(true)
174174
// Should end with underscore + 8 char hash suffix
175175
expect(result).toMatch(/_[a-f0-9]{8}$/)
176176
})
177177

178178
it("should use hash suffix for long names and register them", () => {
179-
const longServer = "a".repeat(50)
180-
const longTool = "b".repeat(50)
179+
const longServer = "a".repeat(80)
180+
const longTool = "b".repeat(80)
181181
const result = buildMcpToolName(longServer, longTool)
182182

183183
// The shortened name should be registered
@@ -190,8 +190,8 @@ describe("mcp-name utilities", () => {
190190
})
191191

192192
it("should produce deterministic hash suffixes", () => {
193-
const longServer = "a".repeat(50)
194-
const longTool = "b".repeat(50)
193+
const longServer = "a".repeat(80)
194+
const longTool = "b".repeat(80)
195195
// Build the same name twice
196196
clearMcpToolNameRegistry()
197197
const result1 = buildMcpToolName(longServer, longTool)
@@ -202,12 +202,12 @@ describe("mcp-name utilities", () => {
202202
})
203203

204204
it("should produce unique hash suffixes for different tools", () => {
205-
const longServer = "a".repeat(50)
206-
const result1 = buildMcpToolName(longServer, "tool1_" + "x".repeat(40))
207-
const result2 = buildMcpToolName(longServer, "tool2_" + "y".repeat(40))
205+
const longServer = "a".repeat(80)
206+
const result1 = buildMcpToolName(longServer, "tool1_" + "x".repeat(70))
207+
const result2 = buildMcpToolName(longServer, "tool2_" + "y".repeat(70))
208208
// Both should be truncated
209-
expect(result1.length).toBe(64)
210-
expect(result2.length).toBe(64)
209+
expect(result1.length).toBe(128)
210+
expect(result2.length).toBe(128)
211211
// Should have different hash suffixes
212212
expect(result1).not.toBe(result2)
213213
})
@@ -429,15 +429,15 @@ describe("mcp-name utilities", () => {
429429
describe("hash suffix roundtrip - fixes issue #10766", () => {
430430
it("should preserve original names through roundtrip with long hyphenated tool names", () => {
431431
// This is the exact scenario from issue #10766
432-
// Tool name with many hyphens that exceeds 64 chars when encoded
433-
const serverName = "abcdefghij-kl-mnop-qrs-tuv"
434-
const toolName = "wxyz-abcd-efghijk-lmno"
432+
// Tool name with many hyphens that exceeds 128 chars when encoded
433+
const serverName = "abcdefghij-kl-mnop-qrs-tuv-with-extra-long-suffix-to-exceed-limit"
434+
const toolName = "wxyz-abcd-efghijk-lmno-plus-additional-long-suffix-here"
435435

436436
// Build the tool name
437437
const builtName = buildMcpToolName(serverName, toolName)
438438

439-
// Should be truncated to 64 chars with hash suffix
440-
expect(builtName.length).toBe(64)
439+
// Should be truncated to 128 chars with hash suffix
440+
expect(builtName.length).toBe(128)
441441
expect(builtName).toMatch(/_[a-f0-9]{8}$/)
442442

443443
// The critical fix: parsing should return the ORIGINAL names
@@ -450,14 +450,14 @@ describe("mcp-name utilities", () => {
450450

451451
it("should not corrupt hyphen encoding when truncation is needed", () => {
452452
// Long server and tool names that would cause truncation mid-encoding
453-
const serverName = "very-long-server-name-with-many-hyphens"
454-
const toolName = "another-long-tool-name-with-hyphens"
453+
const serverName = "very-long-server-name-with-many-hyphens-and-extra-content-to-exceed"
454+
const toolName = "another-long-tool-name-with-hyphens-and-extra-content-to-exceed"
455455

456456
// Build the tool name
457457
const builtName = buildMcpToolName(serverName, toolName)
458458

459-
// Should be truncated to 64 chars
460-
expect(builtName.length).toBe(64)
459+
// Should be truncated to 128 chars
460+
expect(builtName.length).toBe(128)
461461

462462
// Parse should return original names (via registry lookup)
463463
const parsed = parseMcpToolName(builtName)
@@ -476,7 +476,7 @@ describe("mcp-name utilities", () => {
476476

477477
// Should NOT have hash suffix
478478
expect(builtName).toBe("mcp--server--get___data")
479-
expect(builtName.length).toBeLessThan(64)
479+
expect(builtName.length).toBeLessThan(128)
480480

481481
// Normal decode path should work
482482
const parsed = parseMcpToolName(builtName)
@@ -487,8 +487,8 @@ describe("mcp-name utilities", () => {
487487
})
488488

489489
it("should handle the registry lookup for shortened names", () => {
490-
const serverName = "a".repeat(30)
491-
const toolName = "b".repeat(30) + "-hyphen"
490+
const serverName = "a".repeat(80)
491+
const toolName = "b".repeat(80) + "-hyphen"
492492

493493
// Build registers the shortened name
494494
const builtName = buildMcpToolName(serverName, toolName)
@@ -508,8 +508,8 @@ describe("mcp-name utilities", () => {
508508
describe("clearMcpToolNameRegistry", () => {
509509
it("should clear all registered tool names", () => {
510510
// Register some tool names via buildMcpToolName
511-
const longServer = "a".repeat(50)
512-
const longTool = "b".repeat(50)
511+
const longServer = "a".repeat(80)
512+
const longTool = "b".repeat(80)
513513
buildMcpToolName(longServer, longTool)
514514

515515
expect(mcpToolNameRegistry.size).toBeGreaterThan(0)

‎src/utils/mcp-name.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ export const MCP_TOOL_PREFIX = "mcp"
3333
export const HYPHEN_ENCODING = "___"
3434

3535
/**
36-
* Maximum length for tool names (Gemini's limit).
36+
* Maximum length for tool names (MCP spec limit).
37+
* See: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool-names
3738
*/
38-
export const MAX_TOOL_NAME_LENGTH = 64
39+
export const MAX_TOOL_NAME_LENGTH = 128
3940

4041
/**
4142
* Length of hash suffix used when truncation is needed.
@@ -164,7 +165,7 @@ export function sanitizeMcpName(name: string): string {
164165
* Build a full MCP tool function name from server and tool names.
165166
* The format is: mcp--{sanitized_server_name}--{sanitized_tool_name}
166167
*
167-
* The total length is capped at 64 characters to conform to API limits.
168+
* The total length is capped at 128 characters per MCP spec.
168169
* When truncation is needed, a hash suffix is appended to preserve uniqueness
169170
* and the mapping is stored in the registry for later lookup.
170171
*
@@ -188,7 +189,7 @@ export function buildMcpToolName(serverName: string, toolName: string): string {
188189
// Format: truncated_name_HASHSUFFIX (underscore + 8 hex chars = 9 chars for suffix)
189190
const hashSuffix = computeHashSuffix(serverName, toolName)
190191
const suffixWithSeparator = `_${hashSuffix}` // "_" + 8 chars = 9 chars
191-
const maxTruncatedLength = MAX_TOOL_NAME_LENGTH - suffixWithSeparator.length // 64 - 9 = 55
192+
const maxTruncatedLength = MAX_TOOL_NAME_LENGTH - suffixWithSeparator.length // 128 - 9 = 119
192193

193194
// Truncate the full name and append hash suffix
194195
const truncatedBase = fullName.slice(0, maxTruncatedLength)

0 commit comments

Comments
 (0)