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 {