Skip to content

Commit 24459e3

Browse files
fix: 修复 gemini-client 类型错误并改进响应处理
Co-authored-by: aider (vertex_ai/gemini-2.5-pro) <aider@aider.chat>
1 parent 221c64e commit 24459e3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,31 @@ export class GeminiApiClient {
7777

7878
if (msg.role === 'tool') {
7979
const functionName = this.parseFunctionNameFromId(msg.tool_call_id || '');
80-
let responsePayload: object;
80+
let responsePayload: Record<string, unknown>;
8181

8282
try {
83-
// 尝试将工具返回的内容解析为 JSON 对象
84-
responsePayload = JSON.parse(msg.content as string);
83+
const parsed = JSON.parse(msg.content as string);
84+
// The Gemini API expects an object for the response.
85+
// If the parsed content is a primitive (string, number, boolean),
86+
// it must be wrapped in an object.
87+
if (typeof parsed === 'object' && parsed !== null) {
88+
responsePayload = parsed as Record<string, unknown>;
89+
} else {
90+
responsePayload = { output: parsed };
91+
}
8592
} catch (e) {
86-
// 如果解析失败,说明工具返回的可能是一个简单的字符串
87-
// 在这种情况下,我们遵循 Gemini API 的惯例,将其包装在 { "output": "..." } 中
93+
// If parsing fails, it's a plain string. Wrap it.
8894
responsePayload = { output: msg.content };
8995
}
9096

9197
return {
92-
role: 'user', // Gemini 使用 'user' role 来承载 functionResponse
98+
role: 'user', // Gemini uses 'user' role to hold a functionResponse
9399
parts: [
94100
{
95101
functionResponse: {
96102
id: msg.tool_call_id,
97103
name: functionName,
98-
// 直接将解析后的对象或包装后的对象作为 response 的值
104+
// Pass the parsed or wrapped object as the response value.
99105
response: responsePayload,
100106
},
101107
},

0 commit comments

Comments
 (0)