77 type Tool as GcliTool ,
88 type ToolResult ,
99 GeminiChat ,
10- WebFetchTool , // <--- 添加这个导入
11- WebSearchTool , // <--- 添加这个导入
10+ WebFetchTool ,
11+ WebSearchTool ,
1212} from '@google/gemini-cli-core' ;
1313import {
1414 type CallToolResult ,
@@ -27,7 +27,6 @@ export class GcliMcpBridge {
2727 private readonly config : Config ;
2828 private readonly cliVersion : string ;
2929 private readonly debugMode : boolean ;
30- // **修改 2: 更新 transports 的类型以存储每个会话的 McpServer 和 Transport**
3130 private readonly sessions : Record <
3231 string ,
3332 { mcpServer : McpServer ; transport : StreamableHTTPServerTransport }
@@ -39,7 +38,6 @@ export class GcliMcpBridge {
3938 this . debugMode = debugMode ;
4039 }
4140
42- // **辅助方法:创建一个新的 McpServer 实例**
4341 private async createNewMcpServer ( ) : Promise < McpServer > {
4442 const server = new McpServer (
4543 {
@@ -48,7 +46,6 @@ export class GcliMcpBridge {
4846 } ,
4947 { capabilities : { logging : { } } } ,
5048 ) ;
51- // 在这里立即注册所有工具
5249 await this . registerAllGcliTools ( server ) ;
5350 return server ;
5451 }
@@ -57,7 +54,6 @@ export class GcliMcpBridge {
5754 app . all ( '/mcp' , async ( req : Request , res : Response ) => {
5855 const sessionId = req . headers [ 'mcp-session-id' ] as string | undefined ;
5956
60- // **修改 5: 从 `sessions` map 中获取会话对象**
6157 let session = sessionId ? this . sessions [ sessionId ] : undefined ;
6258
6359 if ( ! session ) {
@@ -68,7 +64,6 @@ export class GcliMcpBridge {
6864 ) ;
6965
7066 try {
71- // **修改 6: 为新会话创建独立的 McpServer 和 Transport**
7267 const newMcpServer = await this . createNewMcpServer ( ) ;
7368 const newTransport = new StreamableHTTPServerTransport ( {
7469 sessionIdGenerator : ( ) => randomUUID ( ) ,
@@ -77,7 +72,6 @@ export class GcliMcpBridge {
7772 this . debugMode ,
7873 `Session initialized: ${ newSessionId } ` ,
7974 ) ;
80- // 存储新的会话对象
8175 this . sessions [ newSessionId ] = {
8276 mcpServer : newMcpServer ,
8377 transport : newTransport ,
@@ -96,7 +90,6 @@ export class GcliMcpBridge {
9690 }
9791 } ;
9892
99- // 将新的 transport 连接到新的 McpServer 实例
10093 await newMcpServer . connect ( newTransport ) ;
10194
10295 session = { mcpServer : newMcpServer , transport : newTransport } ;
@@ -130,7 +123,6 @@ export class GcliMcpBridge {
130123 }
131124
132125 try {
133- // **修改 7: 使用会话特定的 transport 来处理请求**
134126 await session . transport . handleRequest ( req , res , req . body ) ;
135127 } catch ( e ) {
136128 logger . error ( 'Error handling request:' , e ) ;
@@ -150,33 +142,30 @@ export class GcliMcpBridge {
150142 }
151143
152144 private registerGcliTool ( tool : GcliTool , mcpServer : McpServer ) {
153- let toolInstanceForExecution = tool ; // 默认使用从 ToolRegistry 传入的原始工具实例
145+ let toolInstanceForExecution = tool ;
154146
155- // 检查是否是需要特殊处理的网页工具
147+ // For web tools, check if a custom model is specified via environment variable.
148+ // If so, create a new tool instance with a proxied config to use that model.
156149 if ( tool . name === 'google_web_search' || tool . name === 'web_fetch' ) {
157150 const toolModel = process . env . GEMINI_TOOLS_DEFAULT_MODEL ;
158151
159- // 如果为这些工具设置了专用的模型,则创建一个新的配置和工具实例
160152 if ( toolModel ) {
161153 logger . debug (
162154 this . debugMode ,
163155 `Using custom model "${ toolModel } " for tool "${ tool . name } "` ,
164156 ) ;
165157
166- // 步骤 1: 创建一个 this.config 的代理。
167- // 这个代理对象会拦截对 getModel 方法的调用。
158+ // Create a proxy for this.config to override getModel.
168159 const proxyConfig = new Proxy ( this . config , {
169160 get : ( target , prop , receiver ) => {
170- // 如果调用的方法是 getModel,则返回我们指定的工具模型
171161 if ( prop === 'getModel' ) {
172162 return ( ) => toolModel ;
173163 }
174- // 对于所有其他属性和方法的调用,都代理到原始的 config 对象
175164 return Reflect . get ( target , prop , receiver ) ;
176165 } ,
177166 } ) as Config ;
178167
179- // 步骤 2: 根据工具名称,使用这个代理配置来创建新的工具实例
168+ // Create a new tool instance with the proxied config.
180169 if ( tool . name === 'google_web_search' ) {
181170 toolInstanceForExecution = new WebSearchTool ( proxyConfig ) ;
182171 } else {
@@ -199,8 +188,7 @@ export class GcliMcpBridge {
199188 const startTime = Date . now ( ) ;
200189 logger . info ( 'MCP tool call started' , { toolName : tool . name , args } ) ;
201190 try {
202- // *** 关键:现在所有工具都通过这个统一的路径执行 ***
203- // toolInstanceForExecution 要么是原始工具,要么是带有自定义模型配置的新实例
191+ // toolInstanceForExecution is either the original tool or a new instance with a custom model config.
204192 const result = await toolInstanceForExecution . execute (
205193 args ,
206194 extra . signal ,
@@ -218,7 +206,8 @@ export class GcliMcpBridge {
218206 toolName : tool . name ,
219207 durationMs,
220208 } ) ;
221- throw e ; // 重新抛出错误,让 MCP SDK 处理
209+ // Re-throw the error to be handled by the MCP SDK.
210+ throw e ;
222211 }
223212 } ,
224213 ) ;
@@ -242,7 +231,7 @@ export class GcliMcpBridge {
242231 case 'boolean' :
243232 return z . boolean ( ) . describe ( prop . description || '' ) ;
244233 case 'array' :
245- // This is the key fix: recursively call the converter for `items`.
234+ // Recursively call the converter for `items`.
246235 if ( ! prop . items ) {
247236 // A valid array schema MUST have `items`. Fallback to `any` if missing.
248237 return z . array ( z . any ( ) ) . describe ( prop . description || '' ) ;
@@ -261,7 +250,6 @@ export class GcliMcpBridge {
261250 }
262251 } ;
263252
264- // If no schema or properties, return an empty shape object.
265253 if ( ! jsonSchema || ! jsonSchema . properties ) {
266254 return { } ;
267255 }
@@ -275,7 +263,7 @@ export class GcliMcpBridge {
275263 }
276264 shape [ key ] = fieldSchema ;
277265 }
278- return shape ; // Directly return the shape object.
266+ return shape ;
279267 }
280268
281269 private convertGcliResultToMcpResult (
0 commit comments