|
1 | 1 | 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'; |
3 | 4 | import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; |
4 | 5 | import { z } from 'zod'; |
5 | 6 | import { |
@@ -39,12 +40,14 @@ const requestLogger = (req: Request, res: Response, next: NextFunction) => { |
39 | 40 | export class GcliMcpBridge { |
40 | 41 | private readonly config: Config; |
41 | 42 | private readonly cliVersion: string; |
42 | | - private readonly mcpServer: McpServer; |
| 43 | + // [FIX] Use the correct class name 'Server' |
| 44 | + private readonly mcpServer: Server; |
43 | 45 |
|
44 | 46 | constructor(config: Config, cliVersion: string) { |
45 | 47 | this.config = config; |
46 | 48 | this.cliVersion = cliVersion; |
47 | | - this.mcpServer = new McpServer( |
| 49 | + // [FIX] Instantiate the correct class 'Server' |
| 50 | + this.mcpServer = new Server( |
48 | 51 | { |
49 | 52 | name: 'gemini-cli-mcp-server', |
50 | 53 | version: this.cliVersion, |
@@ -141,9 +144,32 @@ export class GcliMcpBridge { |
141 | 144 | description: tool.description, |
142 | 145 | inputSchema: inputSchema, |
143 | 146 | }, |
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 | + } |
147 | 173 | }, |
148 | 174 | ); |
149 | 175 | } |
|
0 commit comments