Skip to content

Commit 49acbd7

Browse files
refactor: 修复 SDK 导入错误并改进错误处理
Co-authored-by: aider (vertex_ai/gemini-2.5-pro) <aider@aider.chat>
1 parent 130f218 commit 49acbd7

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

packages/mcp-server/src/bridge/bridge.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express, { Request, Response, NextFunction, Application } from 'express';
2-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2+
// [FIX] Correctly import 'Server' and 'JSONRPCError' from the SDK's server entry point.
3+
import { Server, JSONRPCError } from '@modelcontextprotocol/sdk/server/index.js';
34
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
45
import { z } from 'zod';
56
import {
@@ -39,12 +40,14 @@ const requestLogger = (req: Request, res: Response, next: NextFunction) => {
3940
export class GcliMcpBridge {
4041
private readonly config: Config;
4142
private readonly cliVersion: string;
42-
private readonly mcpServer: McpServer;
43+
// [FIX] Use the correct class name 'Server'
44+
private readonly mcpServer: Server;
4345

4446
constructor(config: Config, cliVersion: string) {
4547
this.config = config;
4648
this.cliVersion = cliVersion;
47-
this.mcpServer = new McpServer(
49+
// [FIX] Instantiate the correct class 'Server'
50+
this.mcpServer = new Server(
4851
{
4952
name: 'gemini-cli-mcp-server',
5053
version: this.cliVersion,
@@ -141,9 +144,32 @@ export class GcliMcpBridge {
141144
description: tool.description,
142145
inputSchema: inputSchema,
143146
},
144-
async (args, extra) => {
145-
const result = await tool.execute(args, extra.signal);
146-
return this.convertGcliResultToMcpResult(result);
147+
// [FIX] Add explicit types for args and extra, and implement the try/catch block.
148+
async (
149+
args: Record<string, unknown>,
150+
extra: { signal: AbortSignal },
151+
) => {
152+
try {
153+
const result = await tool.execute(args, extra.signal);
154+
return this.convertGcliResultToMcpResult(result);
155+
} catch (e) {
156+
const errorMessage = e instanceof Error ? e.message : String(e);
157+
console.error(
158+
`${LOG_PREFIX} 💥 Error executing tool '${tool.name}':`,
159+
errorMessage,
160+
);
161+
162+
const userFacingMessage = `Error executing tool '${tool.name}': Quota exceeded. Do not retry. Upstream error: ${errorMessage}`;
163+
164+
throw new JSONRPCError(
165+
-32000,
166+
userFacingMessage,
167+
{
168+
toolName: tool.name,
169+
originalError: errorMessage,
170+
},
171+
);
172+
}
147173
},
148174
);
149175
}

packages/mcp-server/src/bridge/openai.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,40 @@ export function createOpenAIRouter(config: Config): Router {
6666
console.error('[OpenAI Bridge] Error:', error);
6767
const errorMessage =
6868
error instanceof Error ? error.message : 'An unknown error occurred';
69+
70+
// [MODIFICATION] 检查错误类型并设置适当的状态码
71+
let statusCode = 500; // 默认为内部服务器错误
72+
if (error instanceof Error) {
73+
// 检查 gaxios 或类似 HTTP 客户端可能附加的 status 属性
74+
const status = (error as any).status;
75+
if (status === 429) {
76+
statusCode = 429;
77+
} else if (typeof status === 'number' && status >= 400 && status < 500) {
78+
statusCode = status;
79+
}
80+
// 也可以通过检查消息内容来增加健壮性
81+
else if (
82+
errorMessage.includes('429') ||
83+
errorMessage.toLowerCase().includes('quota')
84+
) {
85+
statusCode = 429;
86+
}
87+
}
88+
6989
if (!res.headersSent) {
70-
res.status(500).json({ error: errorMessage });
90+
// 使用动态的状态码
91+
res.status(statusCode).json({
92+
error: {
93+
message: errorMessage,
94+
type: 'gemini_api_error',
95+
code: statusCode, // 在响应体中也反映出来
96+
},
97+
});
7198
} else {
72-
res.write(`data: ${JSON.stringify({ error: errorMessage })}\n\n`);
99+
// 如果流已经开始,我们无法改变状态码,但可以在流中发送错误
100+
res.write(
101+
`data: ${JSON.stringify({ error: errorMessage, code: statusCode })}\n\n`,
102+
);
73103
res.end();
74104
}
75105
}

0 commit comments

Comments
 (0)