From 987efee9eebc1d42b9450e15439bdb41d7978bf3 Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 2 Dec 2026 12:40:37 -0500 Subject: [PATCH] fix(ruvocal): send Gemini's thought_signature back with tool calls (#5237) After a tool ran, the follow-up completion to Gemini's OpenAI-compatible endpoint failed with 400 "Function call is missing a thought_signature", and the chat showed "No output was generated". Gemini attaches the signature to each streamed tool call in extra_content and requires it on the assistant message that carries those calls. runMcpFlow now keeps extra_content from streamed and non-streamed tool calls and echoes it on the assistant tool message. Providers that send no extra_content are unaffected. Reproduced against gemini-2.5-flash: the request shape ruvocal sent got 400; the same request with extra_content got 200 and an answer. Co-authored-by: r --- .../server/textGeneration/mcp/runMcpFlow.ts | 31 +++++++++++++------ .../textGeneration/mcp/toolInvocation.ts | 6 ++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts index b30636851d..689cbfa129 100644 --- a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts +++ b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts @@ -503,7 +503,10 @@ export async function* runMcpFlow({ logger.debug({ provider: providerHeader }, "[mcp] provider metadata emitted"); } - const toolCallState: Record = {}; + const toolCallState: Record< + number, + { id?: string; name?: string; arguments: string; extraContent?: Record } + > = {}; let firstToolDeltaLogged = false; let sawToolCall = false; let tokenCount = 0; @@ -520,12 +523,14 @@ export async function* runMcpFlow({ index?: number; id?: string; function?: { name?: string; arguments?: string }; + extra_content?: Record; }; const index = toolCall.index ?? 0; const current = toolCallState[index] ?? { arguments: "" }; if (toolCall.id) current.id = toolCall.id; if (toolCall.function?.name) current.name = toolCall.function.name; if (toolCall.function?.arguments) current.arguments += toolCall.function.arguments; + if (toolCall.extra_content) current.extraContent = toolCall.extra_content; toolCallState[index] = current; } if (!firstToolDeltaLogged) { @@ -651,11 +656,15 @@ export async function* runMcpFlow({ } ); const tc = nonStream.choices?.[0]?.message?.tool_calls ?? []; - calls = tc.map((t) => ({ - id: t.id, - name: t.function?.name ?? "", - arguments: t.function?.arguments ?? "", - })); + calls = tc.map((t) => { + const extra = (t as { extra_content?: Record }).extra_content; + return { + id: t.id, + name: t.function?.name ?? "", + arguments: t.function?.arguments ?? "", + ...(extra ? { extraContent: extra } : {}), + }; + }); } else { calls = Object.values(toolCallState) .map((c) => (c?.id && c?.name ? c : undefined)) @@ -664,16 +673,20 @@ export async function* runMcpFlow({ id: c?.id ?? "", name: c?.name ?? "", arguments: c?.arguments ?? "", + ...(c?.extraContent ? { extraContent: c.extraContent } : {}), })) as NormalizedToolCall[]; } // Include the assistant message with tool_calls so the next round // sees both the calls and their outputs, matching MCP branch behavior. - const toolCalls: ChatCompletionMessageToolCall[] = calls.map((call) => ({ + // Echo provider extras (Gemini thought_signature) or the provider + // rejects the follow-up request. + const toolCalls = calls.map((call) => ({ id: call.id, - type: "function", + type: "function" as const, function: { name: call.name, arguments: call.arguments }, - })); + ...(call.extraContent ? { extra_content: call.extraContent } : {}), + })) as ChatCompletionMessageToolCall[]; // Avoid sending content back to the model alongside tool_calls // to prevent confusing follow-up reasoning. Strip any think blocks. diff --git a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts index f7d4406ffd..d63dd23400 100644 --- a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts +++ b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts @@ -27,6 +27,12 @@ export interface NormalizedToolCall { id: string; name: string; arguments: string; + /** + * Provider data attached to the call that must be sent back unchanged with + * the follow-up request. Gemini puts the thought_signature here and rejects + * the next turn with a 400 when it is missing. + */ + extraContent?: Record; } export interface ExecuteToolCallsParams {