Skip to content

Commit 26a6e40

Browse files
committed
fix(mcp): truncate tool names exceeding 64 chars to fit provider limit
OpenAI's tool-use API rejects tools[].function.name strings longer than 64 chars. When an MCP server's combined <sanitized-server>_<sanitized-tool> overflows, the provider returns 400 and opencode silently dies because the error never reaches the TUI — exactly the symptom in anomalyco#3523. Add buildToolName() next to sanitize/toolName in mcp/catalog.ts. Combined names <=64 chars pass through byte-for-byte; overflows truncate to prefix(55) + '_' + hash(8) where hash is Hash.fast(combined).slice(0,8). Hash.fast is already imported elsewhere in the package, so no new dependency. The hash suffix is the collision-safety point: two tools whose first 64 chars match do NOT silently collapse into a single registry key. Every truncation emits Effect.logWarning. Apply at the two provider-facing toolName consumers in mcp/index.ts (instructions() and tools()); the ':'-separated keys in fetch() for prompts/resources aren't subject to the 64-char limit and stay alone. Refs: anomalyco#3523 Closes: anomalyco#3523
1 parent e00890c commit 26a6e40

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/opencode/src/mcp/catalog.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@modelcontextprotocol/sdk/types.js"
88
import { dynamicTool, jsonSchema, type JSONSchema7, type Tool } from "ai"
99
import { Effect } from "effect"
10+
import { Hash } from "@opencode-ai/core/util/hash"
1011

1112
const DEFAULT_TIMEOUT = 30_000
1213
const MAX_LIST_PAGES = 1_000
@@ -118,6 +119,35 @@ export const sanitize = (value: string) => value.replace(/[^a-zA-Z0-9_-]/g, "_")
118119

119120
export const toolName = (clientName: string, name: string) => sanitize(clientName) + "_" + sanitize(name)
120121

122+
// OpenAI's tool-use API rejects `tools[].function.name` strings longer than
123+
// 64 chars. When `<server>_<tool>` exceeds the limit, opencode silently
124+
// dies because the provider returns 400 and the error never reaches the
125+
// TUI. Truncate to 64 with a content-derived suffix so colliding prefixes
126+
// stay distinct (issue #3523).
127+
const MAX_TOOL_NAME_LENGTH = 64
128+
const TOOL_NAME_PREFIX_LENGTH = 55
129+
const TOOL_NAME_HASH_LENGTH = 8
130+
131+
export type BuiltToolName = {
132+
name: string
133+
truncated: boolean
134+
combined: string
135+
}
136+
137+
export function buildToolName(clientName: string, tool: string): BuiltToolName {
138+
const combined = toolName(clientName, tool)
139+
if (combined.length <= MAX_TOOL_NAME_LENGTH) {
140+
return { name: combined, truncated: false, combined }
141+
}
142+
const hash = Hash.fast(combined).slice(0, TOOL_NAME_HASH_LENGTH)
143+
const prefix = combined.slice(0, TOOL_NAME_PREFIX_LENGTH)
144+
return {
145+
name: prefix + "_" + hash,
146+
truncated: true,
147+
combined,
148+
}
149+
}
150+
121151
export function prompts(client: Client, timeout?: number) {
122152
if (!client.getServerCapabilities()?.prompts) return Promise.resolve([])
123153
return paginate(

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,26 @@ const layer = Layer.effect(
614614

615615
const instructions = Effect.fn("MCP.instructions")(function* () {
616616
const s = yield* InstanceState.get(state)
617-
return Object.entries(s.instructions)
617+
const entries = Object.entries(s.instructions)
618618
.filter(([name]) => s.status[name]?.status === "connected")
619619
.sort(([a], [b]) => a.localeCompare(b))
620-
.map(([name, item]) => ({
621-
name,
622-
instructions: item,
623-
tools: (s.defs[name] ?? []).map((tool) => McpCatalog.toolName(name, tool.name)),
624-
}))
620+
const result: ServerInstructions[] = []
621+
for (const [name, item] of entries) {
622+
const tools: string[] = []
623+
for (const tool of s.defs[name] ?? []) {
624+
const built = McpCatalog.buildToolName(name, tool.name)
625+
if (built.truncated) {
626+
yield* Effect.logWarning("MCP tool name exceeds 64 chars, truncating with hash suffix", {
627+
server: name,
628+
tool: tool.name,
629+
truncated: built.name,
630+
})
631+
}
632+
tools.push(built.name)
633+
}
634+
result.push({ name, instructions: item, tools })
635+
}
636+
return result
625637
})
626638

627639
const createAndStore = Effect.fn("MCP.createAndStore")(function* (name: string, mcp: ConfigMCPV1.Info) {
@@ -681,7 +693,15 @@ const layer = Layer.effect(
681693
}
682694
const timeout = requestTimeout(s, clientName, mcpConfig, defaultTimeout)
683695
for (const def of listed) {
684-
result[McpCatalog.toolName(clientName, def.name)] = { def, client, timeout }
696+
const built = McpCatalog.buildToolName(clientName, def.name)
697+
if (built.truncated) {
698+
yield* Effect.logWarning("MCP tool name exceeds 64 chars, truncating with hash suffix", {
699+
server: clientName,
700+
tool: def.name,
701+
truncated: built.name,
702+
})
703+
}
704+
result[built.name] = { def, client, timeout }
685705
}
686706
}
687707
return result

0 commit comments

Comments
 (0)