@@ -21,15 +21,7 @@ import {
21
21
type Content ,
22
22
} from '@google/genai' ;
23
23
import { randomUUID } from 'node:crypto' ;
24
-
25
- const LOG_PREFIX = '[MCP SERVER]' ;
26
-
27
- const requestLogger = ( debugMode : boolean ) => ( req : Request , res : Response , next : NextFunction ) => {
28
- if ( debugMode ) {
29
- console . log ( `${ LOG_PREFIX } ${ req . method } ${ req . url } ` ) ;
30
- }
31
- next ( ) ;
32
- } ;
24
+ import { logger } from '../utils/logger.js' ;
33
25
34
26
export class GcliMcpBridge {
35
27
private readonly config : Config ;
@@ -62,11 +54,6 @@ export class GcliMcpBridge {
62
54
}
63
55
64
56
public async start ( app : Application ) {
65
-
66
- if ( this . debugMode ) {
67
- app . use ( requestLogger ( this . debugMode ) ) ;
68
- }
69
-
70
57
app . all ( '/mcp' , async ( req : Request , res : Response ) => {
71
58
const sessionId = req . headers [ 'mcp-session-id' ] as string | undefined ;
72
59
@@ -75,23 +62,21 @@ export class GcliMcpBridge {
75
62
76
63
if ( ! session ) {
77
64
if ( isInitializeRequest ( req . body ) ) {
78
- if ( this . debugMode ) {
79
- console . log (
80
- `${ LOG_PREFIX } Creating new session and transport for initialize request` ,
81
- ) ;
82
- }
65
+ logger . debug (
66
+ this . debugMode ,
67
+ 'Creating new session and transport for initialize request' ,
68
+ ) ;
83
69
84
70
try {
85
71
// **修改 6: 为新会话创建独立的 McpServer 和 Transport**
86
72
const newMcpServer = await this . createNewMcpServer ( ) ;
87
73
const newTransport = new StreamableHTTPServerTransport ( {
88
74
sessionIdGenerator : ( ) => randomUUID ( ) ,
89
- onsessioninitialized : ( newSessionId ) => {
90
- if ( this . debugMode ) {
91
- console . log (
92
- `${ LOG_PREFIX } Session initialized: ${ newSessionId } ` ,
93
- ) ;
94
- }
75
+ onsessioninitialized : newSessionId => {
76
+ logger . debug (
77
+ this . debugMode ,
78
+ `Session initialized: ${ newSessionId } ` ,
79
+ ) ;
95
80
// 存储新的会话对象
96
81
this . sessions [ newSessionId ] = {
97
82
mcpServer : newMcpServer ,
@@ -103,11 +88,10 @@ export class GcliMcpBridge {
103
88
newTransport . onclose = ( ) => {
104
89
const sid = newTransport . sessionId ;
105
90
if ( sid && this . sessions [ sid ] ) {
106
- if ( this . debugMode ) {
107
- console . log (
108
- `${ LOG_PREFIX } Session ${ sid } closed, removing session object.` ,
109
- ) ;
110
- }
91
+ logger . debug (
92
+ this . debugMode ,
93
+ `Session ${ sid } closed, removing session object.` ,
94
+ ) ;
111
95
delete this . sessions [ sid ] ;
112
96
}
113
97
} ;
@@ -118,15 +102,15 @@ export class GcliMcpBridge {
118
102
session = { mcpServer : newMcpServer , transport : newTransport } ;
119
103
} catch ( e ) {
120
104
// Handle errors during server creation
121
- console . error ( ` ${ LOG_PREFIX } Error creating new MCP session:` , e ) ;
105
+ logger . error ( ' Error creating new MCP session:' , e ) ;
122
106
if ( ! res . headersSent ) {
123
107
res . status ( 500 ) . json ( { error : 'Failed to create session' } ) ;
124
108
}
125
109
return ;
126
110
}
127
111
} else {
128
- console . error (
129
- ` ${ LOG_PREFIX } Bad Request: Missing session ID for non-initialize request.` ,
112
+ logger . error (
113
+ ' Bad Request: Missing session ID for non-initialize request.' ,
130
114
) ;
131
115
res . status ( 400 ) . json ( {
132
116
jsonrpc : '2.0' ,
@@ -138,17 +122,18 @@ export class GcliMcpBridge {
138
122
} ) ;
139
123
return ;
140
124
}
141
- } else if ( this . debugMode ) {
142
- console . log (
143
- `${ LOG_PREFIX } Reusing transport and server for session: ${ sessionId } ` ,
125
+ } else {
126
+ logger . debug (
127
+ this . debugMode ,
128
+ `Reusing transport and server for session: ${ sessionId } ` ,
144
129
) ;
145
130
}
146
131
147
132
try {
148
133
// **修改 7: 使用会话特定的 transport 来处理请求**
149
134
await session . transport . handleRequest ( req , res , req . body ) ;
150
135
} catch ( e ) {
151
- console . error ( ` ${ LOG_PREFIX } Error handling request:` , e ) ;
136
+ logger . error ( ' Error handling request:' , e ) ;
152
137
if ( ! res . headersSent ) {
153
138
res . status ( 500 ) . end ( ) ;
154
139
}
@@ -173,11 +158,10 @@ export class GcliMcpBridge {
173
158
174
159
// 如果为这些工具设置了专用的模型,则创建一个新的配置和工具实例
175
160
if ( toolModel ) {
176
- if ( this . debugMode ) {
177
- console . log (
178
- `[MCP SERVER] Using custom model "${ toolModel } " for tool "${ tool . name } "` ,
179
- ) ;
180
- }
161
+ logger . debug (
162
+ this . debugMode ,
163
+ `Using custom model "${ toolModel } " for tool "${ tool . name } "` ,
164
+ ) ;
181
165
182
166
// 步骤 1: 创建一个 this.config 的代理。
183
167
// 这个代理对象会拦截对 getModel 方法的调用。
@@ -212,22 +196,29 @@ export class GcliMcpBridge {
212
196
args : Record < string , unknown > ,
213
197
extra : { signal : AbortSignal } ,
214
198
) => {
199
+ const startTime = Date . now ( ) ;
200
+ logger . info ( 'MCP tool call started' , { toolName : tool . name , args } ) ;
215
201
try {
216
202
// *** 关键:现在所有工具都通过这个统一的路径执行 ***
217
203
// toolInstanceForExecution 要么是原始工具,要么是带有自定义模型配置的新实例
218
204
const result = await toolInstanceForExecution . execute (
219
205
args ,
220
206
extra . signal ,
221
207
) ;
208
+ const durationMs = Date . now ( ) - startTime ;
209
+ logger . info ( 'MCP tool call finished' , {
210
+ toolName : tool . name ,
211
+ status : 'success' ,
212
+ durationMs,
213
+ } ) ;
222
214
return this . convertGcliResultToMcpResult ( result ) ;
223
215
} catch ( e ) {
224
- const errorMessage = e instanceof Error ? e . message : String ( e ) ;
225
- console . error (
226
- `${ LOG_PREFIX } Error executing tool '${ tool . name } ': ${ errorMessage } ` ,
227
- ) ;
228
- throw new Error (
229
- `Error executing tool '${ tool . name } ': ${ errorMessage } ` ,
230
- ) ;
216
+ const durationMs = Date . now ( ) - startTime ;
217
+ logger . error ( 'MCP tool call failed' , e as Error , {
218
+ toolName : tool . name ,
219
+ durationMs,
220
+ } ) ;
221
+ throw e ; // 重新抛出错误,让 MCP SDK 处理
231
222
}
232
223
} ,
233
224
) ;
0 commit comments