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

Commit ef21f94

Browse files
committed
fix: revert MAX_TOOL_NAME_LENGTH to 64 for Gemini compatibility
The MCP spec recommends 128 characters, but Gemini enforces a 64-character limit for function names. Since buildMcpToolName() is provider-agnostic (called without provider context), we must use the lowest common denominator. This ensures compatibility with all supported API providers.
1 parent 7be724f commit ef21f94

2 files changed

Lines changed: 28 additions & 28 deletions

File tree

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

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

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

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

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

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

184184
// The shortened name should be deterministic
185-
expect(result.length).toBe(128)
185+
expect(result.length).toBe(64)
186186
expect(result).toMatch(/_[a-f0-9]{8}$/)
187187

188188
// Building again should return the same result (from cache)
@@ -191,8 +191,8 @@ describe("mcp-name utilities", () => {
191191
})
192192

193193
it("should produce deterministic hash suffixes", () => {
194-
const longServer = "a".repeat(80)
195-
const longTool = "b".repeat(80)
194+
const longServer = "a".repeat(50)
195+
const longTool = "b".repeat(50)
196196
// Build the same name twice with cache cleared between
197197
clearEncodedNameCache()
198198
const result1 = buildMcpToolName(longServer, longTool)
@@ -203,12 +203,12 @@ describe("mcp-name utilities", () => {
203203
})
204204

205205
it("should produce unique hash suffixes for different tools", () => {
206-
const longServer = "a".repeat(80)
207-
const result1 = buildMcpToolName(longServer, "tool1_" + "x".repeat(70))
208-
const result2 = buildMcpToolName(longServer, "tool2_" + "y".repeat(70))
206+
const longServer = "a".repeat(50)
207+
const result1 = buildMcpToolName(longServer, "tool1_" + "x".repeat(40))
208+
const result2 = buildMcpToolName(longServer, "tool2_" + "y".repeat(40))
209209
// Both should be truncated
210-
expect(result1.length).toBe(128)
211-
expect(result2.length).toBe(128)
210+
expect(result1.length).toBe(64)
211+
expect(result2.length).toBe(64)
212212
// Should have different hash suffixes
213213
expect(result1).not.toBe(result2)
214214
})
@@ -437,7 +437,7 @@ describe("mcp-name utilities", () => {
437437

438438
// Should NOT have hash suffix
439439
expect(builtName).toBe("mcp--server--get___data")
440-
expect(builtName.length).toBeLessThan(128)
440+
expect(builtName.length).toBeLessThan(64)
441441

442442
// Normal decode path should work
443443
const parsed = parseMcpToolName(builtName)
@@ -456,7 +456,7 @@ describe("mcp-name utilities", () => {
456456
const encodedName = buildMcpToolName(serverName, toolName)
457457

458458
// Should be truncated to 128 chars with hash suffix
459-
expect(encodedName.length).toBe(128)
459+
expect(encodedName.length).toBe(64)
460460
expect(encodedName).toMatch(/_[a-f0-9]{8}$/)
461461

462462
// The new approach: use findToolByEncodedMcpName to find the matching tool
@@ -487,8 +487,8 @@ describe("mcp-name utilities", () => {
487487
})
488488

489489
it("should find tool for shortened names with hash suffix", () => {
490-
const serverName = "a".repeat(80)
491-
const toolName = "b".repeat(80) + "-hyphen"
490+
const serverName = "a".repeat(50)
491+
const toolName = "b".repeat(50) + "-hyphen"
492492
const encodedName = buildMcpToolName(serverName, toolName)
493493

494494
// The encoded name should have a hash suffix
@@ -541,8 +541,8 @@ describe("mcp-name utilities", () => {
541541
describe("clearEncodedNameCache", () => {
542542
it("should clear the encoded name cache", () => {
543543
// Build some names to populate cache
544-
const longServer = "a".repeat(80)
545-
const longTool = "b".repeat(80)
544+
const longServer = "a".repeat(50)
545+
const longTool = "b".repeat(50)
546546
buildMcpToolName(longServer, longTool)
547547
buildMcpToolName("server", "tool")
548548

@@ -552,7 +552,7 @@ describe("mcp-name utilities", () => {
552552
// Verify cache is cleared by checking that rebuilding takes the same path
553553
// (we can't directly access the cache, but the function should work)
554554
const result = buildMcpToolName(longServer, longTool)
555-
expect(result.length).toBe(128)
555+
expect(result.length).toBe(64)
556556
expect(result).toMatch(/_[a-f0-9]{8}$/)
557557
})
558558
})

‎src/utils/mcp-name.ts‎

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

3535
/**
36-
* Maximum length for tool names (MCP spec limit).
37-
* See: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool-names
36+
* Maximum length for tool names (Gemini's function name limit).
37+
* * The MCP spec recommends 128, but Gemini enforces 64 characters.
3838
*/
39-
export const MAX_TOOL_NAME_LENGTH = 128
39+
export const MAX_TOOL_NAME_LENGTH = 64
4040

4141
/**
4242
* Length of hash suffix used when truncation is needed.
@@ -192,7 +192,7 @@ export function sanitizeMcpName(name: string): string {
192192
* Build a full MCP tool function name from server and tool names.
193193
* The format is: mcp--{sanitized_server_name}--{sanitized_tool_name}
194194
*
195-
* The total length is capped at 128 characters per MCP spec.
195+
* The total length is capped at 64 characters for Gemini compatibility.
196196
* When truncation is needed, a hash suffix is appended to preserve uniqueness.
197197
* The result is cached for efficient repeated lookups.
198198
*
@@ -224,7 +224,7 @@ export function buildMcpToolName(serverName: string, toolName: string): string {
224224
// Format: truncated_name_HASHSUFFIX (underscore + 8 hex chars = 9 chars for suffix)
225225
const hashSuffix = computeHashSuffix(serverName, toolName)
226226
const suffixWithSeparator = `_${hashSuffix}` // "_" + 8 chars = 9 chars
227-
const maxTruncatedLength = MAX_TOOL_NAME_LENGTH - suffixWithSeparator.length // 128 - 9 = 119
227+
const maxTruncatedLength = MAX_TOOL_NAME_LENGTH - suffixWithSeparator.length // 64 - 9 = 55
228228

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

0 commit comments

Comments
 (0)