Skip to content

Commit 130f218

Browse files
fix: 清理 Gemini API 工具定义中的不兼容字段
Co-authored-by: aider (vertex_ai/gemini-2.5-pro) <aider@aider.chat>
1 parent f62a0dd commit 130f218

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

packages/mcp-server/src/gemini-client.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ import {
2121
type ReasoningData,
2222
} from './types.js';
2323

24+
/**
25+
* Recursively removes fields from a JSON schema that are not supported by the
26+
* Gemini API.
27+
* @param schema The JSON schema to sanitize.
28+
* @returns A new schema object without the unsupported fields.
29+
*/
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
function sanitizeGeminiSchema(schema: any): any {
32+
if (typeof schema !== 'object' || schema === null) {
33+
return schema;
34+
}
35+
36+
// Create a new object, filtering out unsupported keys at the current level.
37+
const newSchema: { [key: string]: any } = {};
38+
for (const key in schema) {
39+
if (key !== '$schema' && key !== 'additionalProperties') {
40+
newSchema[key] = schema[key];
41+
}
42+
}
43+
44+
// Recurse into nested 'properties' and 'items'.
45+
if (newSchema.properties) {
46+
const newProperties: { [key: string]: any } = {};
47+
for (const key in newSchema.properties) {
48+
newProperties[key] = sanitizeGeminiSchema(newSchema.properties[key]);
49+
}
50+
newSchema.properties = newProperties;
51+
}
52+
53+
if (newSchema.items) {
54+
newSchema.items = sanitizeGeminiSchema(newSchema.items);
55+
}
56+
57+
return newSchema;
58+
}
59+
2460
export class GeminiApiClient {
2561
private readonly config: Config;
2662
private readonly contentGenerator;
@@ -42,11 +78,16 @@ export class GeminiApiClient {
4278

4379
const functionDeclarations: FunctionDeclaration[] = openAITools
4480
.filter(tool => tool.type === 'function' && tool.function)
45-
.map(tool => ({
46-
name: tool.function.name,
47-
description: tool.function.description,
48-
parameters: tool.function.parameters,
49-
}));
81+
.map(tool => {
82+
const sanitizedParameters = sanitizeGeminiSchema(
83+
tool.function.parameters,
84+
);
85+
return {
86+
name: tool.function.name,
87+
description: tool.function.description,
88+
parameters: sanitizedParameters,
89+
};
90+
});
5091

5192
if (functionDeclarations.length === 0) {
5293
return undefined;

0 commit comments

Comments
 (0)