fix: ensure MCP tool name prefix starts with a letter for Gemini compatibility (fixes #874)

Math.random().toString(36) can produce strings starting with a digit (e.g.
'4fzyo8'), causing Gemini to reject the request with INVALID_ARGUMENT.
Gemini requires function names to start with a letter or underscore.

Prepend 'm' to the random prefix so generated names always start with a
letter, satisfying Gemini's constraint while preserving uniqueness.
This commit is contained in:
octo-patch 2026-04-23 10:30:28 +08:00
parent b68d519314
commit 1519aaf8cc

View file

@ -230,7 +230,9 @@ export class MCPChannel implements IServerChannel {
}
private _addUniquePrefix(base: string) {
return `${Math.random().toString(36).slice(2, 8)}_${base}`;
// Prefix must start with a letter to satisfy Gemini's function name requirements:
// "Must start with a letter or an underscore" (alphanumeric, _, ., - only; max 64 chars)
return `m${Math.random().toString(36).slice(2, 8)}_${base}`;
}
private async _createClient(serverConfig: MCPConfigFileEntryJSON, serverName: string, isOn = true): Promise<ClientInfo> {