@@ -4,10 +4,14 @@ import { z } from "zod";
4
4
import WebSocket from 'ws' ;
5
5
import fetch from 'node-fetch' ;
6
6
7
+ // Disable console output for MCP compatibility
8
+ console . log = function ( ) { } ;
9
+ console . error = function ( ) { } ;
10
+
7
11
// Create an MCP server
8
12
const server = new McpServer ( {
9
13
name : "NodeJS Debugger" ,
10
- version : "0.2.0 " ,
14
+ version : "0.2.2 " ,
11
15
description : `Advanced Node.js debugger for runtime analysis and troubleshooting. This tool connects to Node.js's built-in Inspector Protocol to provide powerful debugging capabilities directly through Claude Code.
12
16
13
17
DEBUGGING STRATEGY:
@@ -54,28 +58,23 @@ class Inspector {
54
58
const debuggerUrl = data [ 0 ] ?. webSocketDebuggerUrl ;
55
59
56
60
if ( ! debuggerUrl ) {
57
- console . error ( 'No WebSocket debugger URL found' ) ;
58
61
this . scheduleRetry ( ) ;
59
62
return ;
60
63
}
61
64
62
- console . log ( `Connecting to debugger at: ${ debuggerUrl } ` ) ;
63
65
this . ws = new WebSocket ( debuggerUrl ) ;
64
66
65
67
this . ws . on ( 'open' , ( ) => {
66
- console . log ( 'WebSocket connection established' ) ;
67
68
this . connected = true ;
68
69
this . retryCount = 0 ;
69
70
this . enableDebugger ( ) ;
70
71
} ) ;
71
72
72
73
this . ws . on ( 'error' , ( error ) => {
73
- console . error ( 'WebSocket error:' , error . message ) ;
74
74
this . scheduleRetry ( ) ;
75
75
} ) ;
76
76
77
77
this . ws . on ( 'close' , ( ) => {
78
- console . log ( 'WebSocket connection closed' ) ;
79
78
this . connected = false ;
80
79
this . scheduleRetry ( ) ;
81
80
} ) ;
@@ -102,7 +101,6 @@ class Inspector {
102
101
}
103
102
} ) ;
104
103
} catch ( error ) {
105
- console . error ( 'Error initializing inspector:' , error . message ) ;
106
104
this . scheduleRetry ( ) ;
107
105
}
108
106
}
@@ -112,53 +110,39 @@ class Inspector {
112
110
if ( this . retryCount < this . retryOptions . maxRetries || this . continuousRetryEnabled ) {
113
111
this . retryCount ++ ;
114
112
115
- // If we're in continuous retry mode and have exceeded the initial retry count
116
- if ( this . continuousRetryEnabled && this . retryCount > this . retryOptions . maxRetries ) {
117
- // Only log every 10 attempts to avoid flooding the console
118
- if ( this . retryCount % 10 === 0 ) {
119
- console . log ( `Waiting for debugger connection... (retry ${ this . retryCount } )` ) ;
120
- }
121
- } else {
122
- console . log ( `Retrying connection (${ this . retryCount } /${ this . retryOptions . maxRetries } )...` ) ;
123
- }
124
-
125
113
// Use a longer interval for continuous retries to reduce resource usage
126
114
const interval = this . continuousRetryEnabled && this . retryCount > this . retryOptions . maxRetries
127
115
? Math . min ( this . retryOptions . retryInterval * 5 , 10000 ) // Max 10 seconds between retries
128
116
: this . retryOptions . retryInterval ;
129
117
130
118
setTimeout ( ( ) => this . initialize ( ) , interval ) ;
131
- } else {
132
- console . error ( `Failed to connect after ${ this . retryOptions . maxRetries } attempts` ) ;
133
119
}
134
120
}
135
121
136
122
async enableDebugger ( ) {
137
- if ( ! this . debuggerEnabled && this . connected ) {
138
- try {
123
+ try {
124
+ if ( ! this . debuggerEnabled && this . connected ) {
125
+
139
126
await this . send ( 'Debugger.enable' , { } ) ;
140
- console . log ( 'Debugger enabled' ) ;
141
127
this . debuggerEnabled = true ;
142
128
143
129
// Setup event listeners
144
130
await this . send ( 'Runtime.enable' , { } ) ;
145
131
146
132
// Also activate possible domains we'll need
147
133
await this . send ( 'Runtime.runIfWaitingForDebugger' , { } ) ;
148
- } catch ( err ) {
149
- console . error ( 'Failed to enable debugger:' , err ) ;
150
134
}
151
- }
135
+ } catch ( error ) {
136
+ this . scheduleRetry ( ) ;
137
+ }
152
138
}
153
139
154
140
handleEvent ( event ) {
155
- // console.log('Event received:', event.method, event.params);
156
141
157
142
switch ( event . method ) {
158
143
case 'Debugger.paused' :
159
144
this . paused = true ;
160
145
this . currentCallFrames = event . params . callFrames ;
161
- console . log ( 'Execution paused at breakpoint' ) ;
162
146
163
147
// Notify any registered callbacks for pause events
164
148
if ( this . callbackHandlers . has ( 'paused' ) ) {
@@ -170,7 +154,6 @@ class Inspector {
170
154
case 'Debugger.resumed' :
171
155
this . paused = false ;
172
156
this . currentCallFrames = [ ] ;
173
- console . log ( 'Execution resumed' ) ;
174
157
175
158
// Notify any registered callbacks for resume events
176
159
if ( this . callbackHandlers . has ( 'resumed' ) ) {
@@ -184,9 +167,6 @@ class Inspector {
184
167
break ;
185
168
186
169
case 'Runtime.exceptionThrown' :
187
- console . log ( 'Exception thrown:' ,
188
- event . params . exceptionDetails . text ,
189
- event . params . exceptionDetails . exception ?. description || '' ) ;
190
170
break ;
191
171
192
172
case 'Runtime.consoleAPICalled' :
@@ -224,7 +204,6 @@ class Inspector {
224
204
this . consoleOutput . shift ( ) ;
225
205
}
226
206
227
- console . log ( `[Console.${ event . params . type } ]` , args ) ;
228
207
break ;
229
208
}
230
209
}
@@ -299,7 +278,6 @@ class Inspector {
299
278
} ) ;
300
279
return response . scriptSource ;
301
280
} catch ( err ) {
302
- console . error ( 'Error getting script source:' , err ) ;
303
281
return null ;
304
282
}
305
283
}
@@ -320,7 +298,6 @@ class Inspector {
320
298
generatePreview : true
321
299
} ) ;
322
300
} catch ( err ) {
323
- console . error ( 'Error evaluating expression:' , err ) ;
324
301
throw err ;
325
302
}
326
303
}
@@ -334,7 +311,6 @@ class Inspector {
334
311
generatePreview : true
335
312
} ) ;
336
313
} catch ( err ) {
337
- console . error ( 'Error getting properties:' , err ) ;
338
314
throw err ;
339
315
}
340
316
}
@@ -372,7 +348,6 @@ server.tool(
372
348
try {
373
349
${ js_code }
374
350
} catch (e) {
375
- console.error('Execution error:', e);
376
351
e; // Return the error
377
352
}
378
353
` ;
@@ -896,7 +871,6 @@ server.tool(
896
871
try {
897
872
${ expression }
898
873
} catch (e) {
899
- console.error('Evaluation error:', e);
900
874
e; // Return the error
901
875
}
902
876
` ;
@@ -1159,7 +1133,6 @@ server.tool(
1159
1133
// If a new port is specified, update the inspector's port
1160
1134
if ( port && port !== inspector . port ) {
1161
1135
inspector . port = port ;
1162
- console . log ( `Updated debugger port to ${ port } ` ) ;
1163
1136
}
1164
1137
1165
1138
// If already connected, disconnect first
@@ -1191,9 +1164,4 @@ server.tool(
1191
1164
1192
1165
// Start receiving messages on stdin and sending messages on stdout
1193
1166
const transport = new StdioServerTransport ( ) ;
1194
- await server . connect ( transport ) ;
1195
-
1196
- console . log ( "Inspector server ready..." ) ;
1197
- console . log ( "MCP Debugger started. Connected to Node.js Inspector protocol." ) ;
1198
- console . log ( "The server will continuously try to connect to any Node.js debugging session on port 9229." ) ;
1199
- console . log ( "You can start a Node.js app with debugging enabled using: node --inspect yourapp.js" ) ;
1167
+ await server . connect ( transport ) ;
0 commit comments