-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathroutingDebuggingHandler.ts
More file actions
230 lines (207 loc) · 7.41 KB
/
Copy pathroutingDebuggingHandler.ts
File metadata and controls
230 lines (207 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Microsoft Corporation.
import * as http from 'http';
import { IDebuggingHandler } from './debuggingHandler';
import { WorkspaceRegistry, WindowRegistration } from './utils/workspaceRegistry';
import { logger } from './utils/logger';
/**
* Router-window handler (one instance per MCP session) that forwards every
* operation to the ControlServer of the window owning the requested workspace.
*
* The target is resolved from a path hint (`workingDirectory`/`fileFullPath`)
* and cached for the session so later hint-less calls (step/continue/inspect)
* reach the same window. Per-session instances let concurrent agent sessions
* drive debuggers in different repos at once.
*/
export class RoutingDebuggingHandler implements IDebuggingHandler {
private target: WindowRegistration | undefined;
// Bound the router->worker round-trip so a hung worker can't hang the
// forward forever. Kept above the worker's own timeout to avoid preempting
// slow-but-progressing operations. `forwardTimeoutMs` can be passed
// explicitly (mainly for tests) to override the derived value.
private readonly forwardTimeoutMs: number;
constructor(
private readonly registry: WorkspaceRegistry,
timeoutInSeconds: number = 180,
forwardTimeoutMs?: number
) {
this.forwardTimeoutMs = forwardTimeoutMs ?? timeoutInSeconds * 1000 + 15_000;
}
/**
* Resolve (and cache) the target from an optional path hint; a hint always
* re-resolves, otherwise the cached target is reused.
*/
private resolveTarget(pathHint?: string): WindowRegistration {
if (pathHint) {
const found = this.registry.findByPath(pathHint);
const candidates = this.registry
.list()
.map((w) => `pid=${w.pid} port=${w.controlPort} folders=[${w.workspaceFolders.join(', ') || 'none'}]`)
.join(' | ');
logger.info(
`Routing hint "${pathHint}" -> ${found ? `pid=${found.pid} port=${found.controlPort}` : 'no match'}. ` +
`Registered windows: ${candidates || '(none)'}`
);
if (found) {
this.target = found;
}
}
if (!this.target) {
throw new Error(this.noTargetMessage(pathHint));
}
return this.target;
}
private noTargetMessage(pathHint?: string): string {
const windows = this.registry.list();
const openList = windows
.map((w) => (w.workspaceFolders.length ? w.workspaceFolders.join(', ') : '(no folder)'))
.join('; ');
if (pathHint) {
return (
`DebugMCP could not find an open VS Code window whose workspace contains "${pathHint}". ` +
(openList
? `Open the correct folder in VS Code. Currently registered workspaces: ${openList}.`
: 'No DebugMCP-enabled VS Code windows are currently registered.')
);
}
return (
'DebugMCP has no active debug target for this session. ' +
'Call start_debugging (or add_breakpoint) with a file path first so DebugMCP can route to the right VS Code window.'
);
}
private async forward(op: string, args: unknown, pathHint?: string): Promise<string> {
const target = this.resolveTarget(pathHint);
try {
return await this.post(target, op, args);
} catch (error) {
// Failed round-trip usually means the window closed; drop the cache
// so the next path-bearing call re-resolves.
this.target = undefined;
throw error;
}
}
private post(target: WindowRegistration, op: string, args: unknown): Promise<string> {
return new Promise<string>((resolve, reject) => {
const payload = JSON.stringify({ op, args });
let settled = false;
const req = http.request(
{
hostname: '127.0.0.1',
port: target.controlPort,
path: '/op',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload),
'x-debugmcp-token': target.controlToken
}
},
(res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (settled) {
return;
}
settled = true;
try {
const parsed = JSON.parse(body || '{}') as { result?: string; error?: string };
if (res.statusCode === 200 && typeof parsed.result === 'string') {
resolve(parsed.result);
} else {
reject(new Error(parsed.error || `Window control server returned HTTP ${res.statusCode}`));
}
} catch {
reject(new Error(`Invalid response from window control server: ${body}`));
}
});
}
);
// Abort if the worker accepts the connection but never responds;
// otherwise the forward (and its MCP request) would hang forever.
req.setTimeout(this.forwardTimeoutMs, () => {
if (settled) {
return;
}
settled = true;
logger.warn(
`Control server for window pid ${target.pid} did not respond within ${this.forwardTimeoutMs}ms; aborting.`
);
req.destroy();
reject(
new Error(
`The VS Code window (pid ${target.pid}) hosting this workspace did not respond within ` +
`${Math.round(this.forwardTimeoutMs / 1000)}s. Its debugger may be unresponsive. ` +
'Try stop_debugging and retry, or reload that window.'
)
);
});
req.on('error', (err) => {
if (settled) {
return;
}
settled = true;
logger.warn(`Failed to reach control server for window pid ${target.pid}: ${err.message}`);
reject(
new Error(
`Failed to reach the VS Code window (pid ${target.pid}) hosting this workspace. ` +
'It may have been closed. Re-run start_debugging in the intended window.'
)
);
});
req.write(payload);
req.end();
});
}
public handleStartDebugging(args: {
fileFullPath: string;
workingDirectory: string;
testName?: string;
configurationName?: string;
}): Promise<string> {
return this.forward('handleStartDebugging', args, args.workingDirectory || args.fileFullPath);
}
public handleStopDebugging(): Promise<string> {
return this.forward('handleStopDebugging', {});
}
public handleStepOver(): Promise<string> {
return this.forward('handleStepOver', {});
}
public handleStepInto(): Promise<string> {
return this.forward('handleStepInto', {});
}
public handleStepOut(): Promise<string> {
return this.forward('handleStepOut', {});
}
public handleContinue(): Promise<string> {
return this.forward('handleContinue', {});
}
public handlePause(): Promise<string> {
return this.forward('handlePause', {});
}
public handleRestart(): Promise<string> {
return this.forward('handleRestart', {});
}
public handleAddBreakpoint(args: { fileFullPath: string; line: number; condition?: string }): Promise<string> {
return this.forward('handleAddBreakpoint', args, args.fileFullPath);
}
public handleAddLogpoint(args: { fileFullPath: string; line: number; logMessage: string; condition?: string }): Promise<string> {
return this.forward('handleAddLogpoint', args, args.fileFullPath);
}
public handleRemoveBreakpoint(args: { fileFullPath: string; line: number }): Promise<string> {
return this.forward('handleRemoveBreakpoint', args, args.fileFullPath);
}
public handleClearAllBreakpoints(): Promise<string> {
return this.forward('handleClearAllBreakpoints', {});
}
public handleListBreakpoints(): Promise<string> {
return this.forward('handleListBreakpoints', {});
}
public handleGetVariables(args: { scope?: 'local' | 'global' | 'all' }): Promise<string> {
return this.forward('handleGetVariables', args);
}
public handleEvaluateExpression(args: { expression: string }): Promise<string> {
return this.forward('handleEvaluateExpression', args);
}
}