-
Notifications
You must be signed in to change notification settings - Fork 42
[Fix] Gemini requests fail when user enables the full MCP tool set #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edelauna
merged 8 commits into
main
from
feature/add-gemini-provider-e2e-tests-1ke9f2v2afjjv
May 21, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3169891
test: add Gemini provider e2e coverage
roomote 757dc44
fix(gemini): INVALID_ARGUMENT when loaded too many MCPs
edelauna 822eb3b
fix(gemini): resolve $ref, deep-merge allOf, align e2e fixtures
edelauna 9046ac5
refactor: dropping extra command
edelauna 7697975
fix: preserve top-level Gemini schema fields with allOf
roomote ff1fc8e
fix: guard recursive Gemini schema refs
roomote 9ca6a65
fix(gemini): preserve keyword-named tool parameters during schema san…
edelauna 8cc2e7e
test(gemini-e2e): wire aimock recording and use real model id
edelauna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { | ||
| "fixtures": [ | ||
| { | ||
| "match": { | ||
| "model": "gemini-3-flash-preview", | ||
| "userMessage": "gemini-e2e:reasoning-high: what is 2+2? Reply with only the number." | ||
| }, | ||
| "response": { | ||
| "toolCalls": [ | ||
| { | ||
| "name": "attempt_completion", | ||
| "arguments": "{\"result\":\"4\"}", | ||
| "id": "call_gemini_reasoning_high_done" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "match": { | ||
| "model": "gemini-3-flash-preview", | ||
| "userMessage": "gemini-e2e:reasoning-low: what is 2+2? Reply with only the number." | ||
| }, | ||
| "response": { | ||
| "toolCalls": [ | ||
| { | ||
| "name": "attempt_completion", | ||
| "arguments": "{\"result\":\"4\"}", | ||
| "id": "call_gemini_reasoning_low_done" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "match": { | ||
| "model": "gemini-3-flash-preview", | ||
| "userMessage": "gemini-e2e:reasoning-disable: what is 2+2? Reply with only the number." | ||
| }, | ||
| "response": { | ||
| "toolCalls": [ | ||
| { | ||
| "name": "attempt_completion", | ||
| "arguments": "{\"result\":\"4\"}", | ||
| "id": "call_gemini_reasoning_disable_done" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,288 @@ | ||
| import * as assert from "assert" | ||
|
|
||
| import { RooCodeEventName, type ClineMessage } from "@roo-code/types" | ||
|
|
||
| import { setDefaultSuiteTimeout } from "../test-utils" | ||
| import { waitUntilCompleted } from "../utils" | ||
|
|
||
| const GEMINI_API_KEY = process.env.GEMINI_API_KEY ?? process.env.GOOGLE_API_KEY | ||
| const GEMINI_MODEL_ID = process.env.GEMINI_MODEL_ID ?? "gemini-3-flash-preview" | ||
|
|
||
| type FunctionDeclaration = { | ||
| name: string | ||
| parametersJsonSchema?: Record<string, unknown> | ||
| } | ||
|
|
||
| type GeminiToolConfig = { | ||
| functionCallingConfig?: { | ||
| mode?: string | ||
| allowedFunctionNames?: string[] | ||
| } | ||
| } | ||
|
|
||
| type CapturedGeminiRequest = { | ||
| model?: string | ||
| lastUserMessage: string | ||
| thinkingConfig?: Record<string, unknown> | ||
| toolConfig?: GeminiToolConfig | ||
| hasTools: boolean | ||
| toolDeclarationCount: number | ||
| functionDeclarations: FunctionDeclaration[] | ||
| } | ||
|
|
||
| function findInvalidSchemaPatterns(schema: unknown, path = ""): string[] { | ||
| if (!schema || typeof schema !== "object" || Array.isArray(schema)) { | ||
| return [] | ||
| } | ||
|
|
||
| const obj = schema as Record<string, unknown> | ||
| const violations: string[] = [] | ||
|
|
||
| if ("additionalProperties" in obj) { | ||
| violations.push(`${path}.additionalProperties (stripped for Gemini compatibility)`) | ||
| } | ||
|
|
||
| if ("default" in obj) { | ||
| violations.push(`${path}.default (stripped for Gemini compatibility)`) | ||
| } | ||
|
|
||
| if ("$schema" in obj) { | ||
| violations.push(`${path}.$schema (JSON Schema metadata stripped for Gemini compatibility)`) | ||
| } | ||
|
|
||
| if ("type" in obj && Array.isArray(obj.type)) { | ||
| violations.push(`${path}.type is an array ${JSON.stringify(obj.type)} (Gemini requires a single string type)`) | ||
| } | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| if (key === "properties" && value && typeof value === "object") { | ||
| for (const [propName, propSchema] of Object.entries(value as Record<string, unknown>)) { | ||
| violations.push(...findInvalidSchemaPatterns(propSchema, `${path}.properties.${propName}`)) | ||
| } | ||
| } else if (key === "items") { | ||
| violations.push(...findInvalidSchemaPatterns(value, `${path}.items`)) | ||
| } else if (key === "anyOf" || key === "oneOf" || key === "allOf") { | ||
| violations.push(`${path}.${key} (collapsed for Gemini compatibility)`) | ||
| if (Array.isArray(value)) { | ||
| value.forEach((item, i) => violations.push(...findInvalidSchemaPatterns(item, `${path}.${key}[${i}]`))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return violations | ||
| } | ||
|
|
||
| function getRequestUrl(input: RequestInfo | URL): string { | ||
| return typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url | ||
| } | ||
|
|
||
| function isUrlWithOrigin(rawUrl: string, expectedOrigin: string): boolean { | ||
| try { | ||
| return new URL(rawUrl).origin === expectedOrigin | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function isGeminiGenerateContentUrl(rawUrl: string): boolean { | ||
| try { | ||
| const pathname = new URL(rawUrl).pathname | ||
| return pathname.includes(":streamGenerateContent") || pathname.includes(":generateContent") | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function extractGeminiModel(rawUrl: string): string | undefined { | ||
| try { | ||
| const pathname = new URL(rawUrl).pathname | ||
| const match = pathname.match(/\/models\/([^:]+):(streamGenerateContent|generateContent)$/) | ||
| return match?.[1] | ||
| } catch { | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| function extractLastUserMessage( | ||
| contents?: Array<{ | ||
| role?: string | ||
| parts?: Array<{ text?: string }> | ||
| }>, | ||
| ): string { | ||
| const lastUser = [...(contents ?? [])].reverse().find((content) => content.role === "user") | ||
|
|
||
| if (!lastUser?.parts) { | ||
| return "" | ||
| } | ||
|
|
||
| return lastUser.parts | ||
| .map((part) => (typeof part?.text === "string" ? part.text : JSON.stringify(part ?? ""))) | ||
| .join("") | ||
| } | ||
|
|
||
| function installGeminiRequestCapture(capture: CapturedGeminiRequest[], baseUrl: string): () => void { | ||
| const originalFetch = globalThis.fetch | ||
| const targetOrigin = new URL(baseUrl).origin | ||
|
|
||
| globalThis.fetch = async function (input: RequestInfo | URL, init?: RequestInit): Promise<Response> { | ||
| const url = getRequestUrl(input) | ||
|
|
||
| if (isUrlWithOrigin(url, targetOrigin) && isGeminiGenerateContentUrl(url)) { | ||
| const body = init?.body && typeof init.body === "string" ? JSON.parse(init.body) : {} | ||
| const tools = Array.isArray(body.tools) ? body.tools : [] | ||
| const functionDeclarations: FunctionDeclaration[] = tools.flatMap( | ||
| (tool: { functionDeclarations?: FunctionDeclaration[] }) => | ||
| Array.isArray(tool.functionDeclarations) ? tool.functionDeclarations : [], | ||
| ) | ||
|
|
||
| capture.push({ | ||
| model: extractGeminiModel(url), | ||
| lastUserMessage: extractLastUserMessage(body.contents), | ||
| thinkingConfig: | ||
| body.generationConfig && typeof body.generationConfig === "object" | ||
| ? (body.generationConfig.thinkingConfig as Record<string, unknown> | undefined) | ||
| : undefined, | ||
| toolConfig: | ||
| body.toolConfig && typeof body.toolConfig === "object" | ||
| ? (body.toolConfig as GeminiToolConfig) | ||
| : undefined, | ||
| hasTools: tools.length > 0, | ||
| toolDeclarationCount: functionDeclarations.length, | ||
| functionDeclarations, | ||
| }) | ||
| } | ||
|
|
||
| return originalFetch.call(globalThis, input, init as RequestInit) | ||
| } as typeof globalThis.fetch | ||
|
|
||
| return () => { | ||
| globalThis.fetch = originalFetch | ||
| } | ||
| } | ||
|
|
||
| suite("Gemini provider", function () { | ||
| setDefaultSuiteTimeout(this) | ||
|
|
||
| let restoreFetch: (() => void) | undefined | ||
| const requests: CapturedGeminiRequest[] = [] | ||
|
|
||
| setup(function () { | ||
| const aimockUrl = process.env.AIMOCK_URL | ||
| const isReplay = aimockUrl && process.env.AIMOCK_RECORD !== "true" | ||
| const isRecordRun = aimockUrl && process.env.AIMOCK_RECORD === "true" && !!GEMINI_API_KEY | ||
| // Live runs without aimock are not supported — GEMINI_MODEL_ID must match the fixture. | ||
| if (!isReplay && !isRecordRun) { | ||
| this.skip() | ||
| } | ||
| }) | ||
|
|
||
| suiteSetup(() => { | ||
| restoreFetch = installGeminiRequestCapture( | ||
| requests, | ||
| process.env.AIMOCK_URL || "https://generativelanguage.googleapis.com", | ||
| ) | ||
| }) | ||
|
|
||
| suiteTeardown(async () => { | ||
| restoreFetch?.() | ||
| restoreFetch = undefined | ||
|
|
||
| const aimockUrl = process.env.AIMOCK_URL | ||
| const isRecord = process.env.AIMOCK_RECORD === "true" | ||
| await globalThis.api.setConfiguration({ | ||
| apiProvider: "openrouter" as const, | ||
| openRouterApiKey: aimockUrl && !isRecord ? "mock-key" : process.env.OPENROUTER_API_KEY!, | ||
| openRouterModelId: "openai/gpt-4.1", | ||
| ...(aimockUrl && { openRouterBaseUrl: `${aimockUrl}/v1` }), | ||
| }) | ||
| }) | ||
|
|
||
| for (const reasoningEffort of ["high", "low", "disable"] as const) { | ||
| test(`Should complete a task end-to-end using ${GEMINI_MODEL_ID} via Gemini provider with reasoning effort "${reasoningEffort}"`, async () => { | ||
| requests.length = 0 | ||
|
|
||
| const api = globalThis.api | ||
| const aimockUrl = process.env.AIMOCK_URL | ||
| const isRecord = process.env.AIMOCK_RECORD === "true" | ||
| const promptTag = `gemini-e2e:reasoning-${reasoningEffort}` | ||
|
|
||
| await api.setConfiguration({ | ||
| apiProvider: "gemini" as const, | ||
| geminiApiKey: aimockUrl && !isRecord ? "mock-key" : GEMINI_API_KEY!, | ||
|
roomote[bot] marked this conversation as resolved.
|
||
| apiModelId: GEMINI_MODEL_ID, | ||
| enableReasoningEffort: reasoningEffort !== "disable", | ||
| reasoningEffort: reasoningEffort, | ||
| ...(aimockUrl && { googleGeminiBaseUrl: aimockUrl }), | ||
| }) | ||
|
|
||
| const messages: ClineMessage[] = [] | ||
| const messageHandler = ({ message }: { message: ClineMessage }) => { | ||
| if (message.type === "say" && message.partial === false) { | ||
| messages.push(message) | ||
| } | ||
| } | ||
|
|
||
| api.on(RooCodeEventName.Message, messageHandler) | ||
|
|
||
| try { | ||
| const taskId = await api.startNewTask({ | ||
| configuration: { mode: "ask", alwaysAllowModeSwitch: true, autoApprovalEnabled: true }, | ||
| text: `${promptTag}: what is 2+2? Reply with only the number.`, | ||
| }) | ||
|
|
||
| await waitUntilCompleted({ api, taskId }) | ||
| } finally { | ||
| api.off(RooCodeEventName.Message, messageHandler) | ||
| } | ||
|
|
||
| const firstRequest = requests.find((request) => request.lastUserMessage.includes(promptTag)) | ||
| assert.ok(firstRequest, "Gemini provider should issue a generate content request for the task prompt") | ||
| assert.strictEqual(firstRequest.model, GEMINI_MODEL_ID) | ||
| assert.ok(firstRequest.hasTools, "Gemini provider should include tool declarations in the request") | ||
| assert.ok( | ||
| firstRequest.toolDeclarationCount > 0, | ||
| "Gemini provider should declare at least one callable tool", | ||
| ) | ||
| assert.strictEqual( | ||
| firstRequest.toolConfig?.functionCallingConfig?.allowedFunctionNames, | ||
| undefined, | ||
| "Gemini requests should not send allowedFunctionNames; the Gemini backend returns generic INVALID_ARGUMENT for larger or history-incompatible restriction lists", | ||
| ) | ||
|
|
||
| // Verify tool schemas are sanitized for Gemini compatibility. Gemini documents | ||
| // function declaration schemas as a selected OpenAPI-style subset with | ||
| // single-value `type` plus `nullable`; live testing also showed opaque | ||
| // INVALID_ARGUMENT failures from broader third-party MCP schema metadata. | ||
| for (const decl of firstRequest.functionDeclarations) { | ||
| const violations = findInvalidSchemaPatterns( | ||
| decl.parametersJsonSchema, | ||
| `${decl.name}.parametersJsonSchema`, | ||
| ) | ||
| assert.strictEqual( | ||
| violations.length, | ||
| 0, | ||
| `Tool "${decl.name}" has Gemini-incompatible schema: ${violations.join("; ")}`, | ||
| ) | ||
| } | ||
|
|
||
| if (reasoningEffort === "disable") { | ||
| assert.strictEqual( | ||
| firstRequest.thinkingConfig, | ||
| undefined, | ||
| "Reasoning-disabled Gemini requests should omit thinkingConfig", | ||
| ) | ||
| } else { | ||
| assert.ok( | ||
| firstRequest.thinkingConfig, | ||
| `Gemini requests with reasoningEffort="${reasoningEffort}" should include thinkingConfig`, | ||
| ) | ||
| } | ||
|
|
||
| const completionMessage = messages.find( | ||
| ({ say, text }) => (say === "completion_result" || say === "text") && text?.trim() === "4", | ||
| ) | ||
|
|
||
| assert.ok(completionMessage, "Task should complete with the expected Gemini provider response") | ||
| }) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.