forked from wso2/vscode-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCPInspectorViewProvider.ts
More file actions
433 lines (398 loc) · 15.5 KB
/
MCPInspectorViewProvider.ts
File metadata and controls
433 lines (398 loc) · 15.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import * as vscode from 'vscode';
import { Views, WebviewConfig, Ports } from './constants';
import { Logger } from './utils/logger';
import type { MCPInspectorManager } from './MCPInspectorManager';
import type { ServerParams } from './types';
/**
* Provides the webview content for the MCP Inspector
*/
export class MCPInspectorViewProvider implements vscode.WebviewViewProvider {
public static readonly viewType = Views.INSPECTOR_VIEW;
constructor(private readonly _inspectorManager?: MCPInspectorManager) {}
/**
* Resolves the webview view when it's shown
*/
public resolveWebviewView(
webviewView: vscode.WebviewView,
_context: vscode.WebviewViewResolveContext,
_token: vscode.CancellationToken
): void {
try {
webviewView.webview.options = {
enableScripts: WebviewConfig.ENABLE_SCRIPTS,
};
const clipboardBridge = MCPInspectorViewProvider.attachClipboardBridge(webviewView.webview);
webviewView.onDidDispose(() => clipboardBridge.dispose());
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
} catch (error) {
Logger.error('Failed to resolve webview view', error);
throw error;
}
}
/**
* Wires up the parent-webview side of the clipboard paste bridge.
* The iframe cannot read the system clipboard directly because it's cross-origin
* to the VS Code webview shell; we relay the request through the extension host.
*/
public static attachClipboardBridge(webview: vscode.Webview): vscode.Disposable {
return webview.onDidReceiveMessage(async (msg) => {
if (!msg) return;
if (msg.type === 'mcp-inspector-request-clipboard-text') {
try {
const text = await vscode.env.clipboard.readText();
webview.postMessage({
type: 'mcp-inspector-clipboard-text',
requestId: msg.requestId,
text,
});
} catch (error) {
Logger.error('Failed to read clipboard for inspector paste', error);
webview.postMessage({
type: 'mcp-inspector-clipboard-text',
requestId: msg.requestId,
text: '',
});
}
} else if (msg.type === 'mcp-inspector-request-clipboard-write') {
try {
await vscode.env.clipboard.writeText(typeof msg.text === 'string' ? msg.text : '');
webview.postMessage({
type: 'mcp-inspector-clipboard-write-result',
requestId: msg.requestId,
ok: true,
});
} catch (error) {
Logger.error('Failed to write clipboard for inspector copy', error);
webview.postMessage({
type: 'mcp-inspector-clipboard-write-result',
requestId: msg.requestId,
ok: false,
error: error instanceof Error ? error.message : String(error),
});
}
}
});
}
/**
* Get HTML content for the webview
*/
public getHtmlForWebview(webview: vscode.Webview, serverParams?: ServerParams): string {
return this._getHtmlForWebview(webview, serverParams);
}
/**
* Get loading HTML while processes are starting
*/
public getLoadingHtml(): string {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MCP Inspector - Loading</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background: var(--vscode-editor-background);
}
.loading-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
font-family: var(--vscode-font-family);
color: var(--vscode-foreground);
gap: 20px;
}
.spinner {
width: 50px;
height: 50px;
border: 5px solid var(--vscode-button-background);
border-top-color: transparent;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.status {
font-size: 16px;
opacity: 0.8;
}
</style>
</head>
<body>
<div class="loading-container">
<div class="spinner"></div>
<div class="status">Starting MCP Inspector...</div>
<div style="font-size: 12px; opacity: 0.6;">This may take a few seconds</div>
</div>
</body>
</html>`;
}
/**
* Get error HTML when startup fails
*/
public getErrorHtml(errorMessage: string): string {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MCP Inspector - Error</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background: var(--vscode-editor-background);
}
.error-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
font-family: var(--vscode-font-family);
color: var(--vscode-foreground);
gap: 16px;
padding: 20px;
text-align: center;
}
.error-icon {
font-size: 48px;
color: var(--vscode-errorForeground);
}
.error-message {
font-size: 14px;
max-width: 500px;
}
</style>
</head>
<body>
<div class="error-container">
<div class="error-icon">⚠️</div>
<div style="font-size: 18px;">Failed to Start MCP Inspector</div>
<div class="error-message">${errorMessage}</div>
<div style="font-size: 12px; opacity: 0.6;">Try closing and reopening the panel</div>
</div>
</body>
</html>`;
}
/**
* Generate HTML for the webview
*/
private _getHtmlForWebview(_webview: vscode.Webview, serverParams?: ServerParams): string {
// Get auth token from inspector manager
const authToken = this._inspectorManager?.getAuthToken() || '';
// Build URL with auth token and optional server parameters
const urlParams = new URLSearchParams({ MCP_PROXY_AUTH_TOKEN: authToken });
if (serverParams?.serverUrl) {
urlParams.set('serverUrl', serverParams.serverUrl);
}
if (serverParams?.serverCommand) {
urlParams.set('serverCommand', serverParams.serverCommand);
}
if (serverParams?.serverArgs) {
urlParams.set('serverArgs', serverParams.serverArgs);
}
if (serverParams?.transport) {
urlParams.set('transport', serverParams.transport);
}
const inspectorUrl = `http://localhost:${Ports.CLIENT}/?${urlParams.toString()}`;
// CSP policy that allows iframe to localhost
const csp = [
`default-src 'none'`,
`frame-src http://localhost:${Ports.CLIENT}`,
`style-src 'unsafe-inline'`,
`script-src 'unsafe-inline'`,
].join('; ');
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="${csp}">
<title>MCP Inspector</title>
<style>
html, body, iframe {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: none;
overflow: hidden;
background: var(--vscode-editor-background);
}
</style>
</head>
<body>
<iframe id="inspector-iframe" src="${inspectorUrl}" sandbox="allow-scripts allow-forms allow-same-origin" allow="clipboard-read; clipboard-write"></iframe>
<script>
(function() {
const iframe = document.getElementById('inspector-iframe');
let retryCount = 0;
const maxRetries = 3;
// Function to send theme colors to iframe
function sendThemeColors() {
// Get VSCode theme colors
const computedStyle = getComputedStyle(document.documentElement);
const editorBg = computedStyle.getPropertyValue('--vscode-editor-background').trim();
const editorFg = computedStyle.getPropertyValue('--vscode-editor-foreground').trim();
const fg = computedStyle.getPropertyValue('--vscode-foreground').trim() || editorFg;
const buttonBg = computedStyle.getPropertyValue('--vscode-button-background').trim();
const buttonFg = computedStyle.getPropertyValue('--vscode-button-foreground').trim() || fg;
const buttonSecBg = computedStyle.getPropertyValue('--vscode-button-secondaryBackground').trim();
const buttonSecFg = computedStyle.getPropertyValue('--vscode-button-secondaryForeground').trim() || fg;
const descriptionFg = computedStyle.getPropertyValue('--vscode-descriptionForeground').trim() || fg;
const panelBorder = computedStyle.getPropertyValue('--vscode-panel-border').trim();
const widgetBorder = computedStyle.getPropertyValue('--vscode-widget-border').trim();
const contrastBorder = computedStyle.getPropertyValue('--vscode-contrastBorder').trim();
const activityBarBorder = computedStyle.getPropertyValue('--vscode-activityBar-border').trim();
const inputBorder = computedStyle.getPropertyValue('--vscode-input-border').trim();
const bodyClass = document.body.className || '';
const isHighContrast = bodyClass.includes('vscode-high-contrast');
const isDark = bodyClass.includes('vscode-dark') || (isHighContrast && !bodyClass.includes('vscode-high-contrast-light'));
// Prefer contrastBorder in HC: surfaces blend with body there, so the border carries differentiation.
const border = isHighContrast
? (contrastBorder || panelBorder || widgetBorder || activityBarBorder)
: (panelBorder || widgetBorder || contrastBorder || activityBarBorder);
const inputBorderResolved = inputBorder || border;
const errorBg = computedStyle.getPropertyValue('--vscode-inputValidation-errorBackground').trim();
const errorFg = computedStyle.getPropertyValue('--vscode-inputValidation-errorForeground').trim() || fg;
const inactiveTabBg = computedStyle.getPropertyValue('--vscode-tab-inactiveBackground').trim();
// Drop transparent / low-alpha values so they don't degrade to gray in colorToHsl.
const opaque = (c) => {
if (!c) return '';
if (/^\\s*transparent\\s*$/i.test(c)) return '';
let m = c.match(/^#[0-9a-f]{6}([0-9a-f]{2})$/i);
if (m && parseInt(m[1], 16) < 128) return '';
m = c.match(/^[a-z]+\\(\\s*[\\d.]+%?\\s*,\\s*[\\d.]+%?\\s*,\\s*[\\d.]+%?\\s*,\\s*([\\d.]+)\\s*\\)\\s*$/i);
if (m && parseFloat(m[1]) < 0.5) return '';
m = c.match(/^[a-z]+\\([^)]*\\/\\s*([\\d.]+)\\s*\\)\\s*$/i);
if (m && parseFloat(m[1]) < 0.5) return '';
return c;
};
const widgetBg = opaque(computedStyle.getPropertyValue('--vscode-editorWidget-background').trim());
const listHoverBg = opaque(computedStyle.getPropertyValue('--vscode-list-hoverBackground').trim());
// In HC themes, surfaces should blend with the editor and rely on borders (contrastBorder)
// for differentiation — coloured fills wash out or render invisible.
let subtleSurface;
let subtleSurfaceFg = fg;
if (isHighContrast) {
subtleSurface = editorBg;
} else if (widgetBg) {
subtleSurface = widgetBg;
} else if (listHoverBg) {
subtleSurface = listHoverBg;
} else if (buttonSecBg) {
subtleSurface = buttonSecBg;
subtleSurfaceFg = buttonSecFg;
} else {
subtleSurface = inactiveTabBg;
}
const subtleSurfaceMuted = isHighContrast ? editorBg : (widgetBg || listHoverBg || inactiveTabBg);
// Send all theme colors (in same order as inject-theme.js uses them)
const themeColors = {
// Main colors
background: editorBg,
foreground: editorFg,
// Card colors
card: editorBg,
cardForeground: editorFg,
// Popover colors
popover: editorBg,
popoverForeground: editorFg,
// Primary colors
primary: buttonBg,
primaryForeground: buttonFg,
// Secondary colors
secondary: subtleSurface,
secondaryForeground: subtleSurfaceFg,
// Muted colors
muted: subtleSurfaceMuted,
mutedForeground: descriptionFg,
// Accent colors
accent: subtleSurface,
accentForeground: subtleSurfaceFg,
// Destructive colors
destructive: errorBg,
destructiveForeground: errorFg,
// Input/Border/Ring
border: border,
input: inputBorderResolved,
ring: editorFg,
// Body styles
bodyBg: editorBg,
bodyColor: editorFg
};
if (iframe.contentWindow) {
iframe.contentWindow.postMessage({
type: 'vscode-theme-colors',
colors: themeColors,
isDark: isDark
}, 'http://localhost:${Ports.CLIENT}');
}
}
// Handle iframe errors and implement retry logic
iframe.onerror = function(error) {
console.error('MCP Inspector iframe error:', error);
if (retryCount < maxRetries) {
retryCount++;
console.log('Retrying to load iframe... Attempt ' + retryCount + '/' + maxRetries);
setTimeout(function() {
iframe.src = iframe.src; // Reload the iframe
}, 1000 * retryCount); // Exponential backoff
}
};
// Wait for iframe to load then send theme colors immediately
iframe.onload = function() {
retryCount = 0; // Reset retry count on successful load
// Send immediately and retry after a short delay to ensure it's applied
sendThemeColors();
setTimeout(sendThemeColors, 50);
setTimeout(sendThemeColors, 200);
};
// === Clipboard bridge (parent webview side) ===
// Iframe asks us for clipboard read/write -> we ask the extension -> we forward back to iframe.
const vscodeApi = acquireVsCodeApi();
window.addEventListener('message', function(e) {
const msg = e.data;
if (!msg || typeof msg !== 'object') return;
if (msg.type === 'mcp-inspector-request-paste' && e.source === iframe.contentWindow) {
vscodeApi.postMessage({ type: 'mcp-inspector-request-clipboard-text', requestId: msg.requestId });
} else if (msg.type === 'mcp-inspector-request-clipboard-write' && e.source === iframe.contentWindow) {
vscodeApi.postMessage({ type: 'mcp-inspector-request-clipboard-write', requestId: msg.requestId, text: msg.text });
} else if (msg.type === 'mcp-inspector-clipboard-text' && iframe.contentWindow) {
iframe.contentWindow.postMessage({ type: 'mcp-inspector-paste-response', requestId: msg.requestId, text: msg.text }, 'http://localhost:${Ports.CLIENT}');
} else if (msg.type === 'mcp-inspector-clipboard-write-result' && iframe.contentWindow) {
iframe.contentWindow.postMessage({ type: 'mcp-inspector-clipboard-write-result', requestId: msg.requestId, ok: msg.ok, error: msg.error }, 'http://localhost:${Ports.CLIENT}');
}
});
// Listen for VSCode theme changes
const observer = new MutationObserver((mutations) => {
sendThemeColors();
});
// Observe both <html> (CSS variable updates) and <body> (theme class flips).
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['style', 'class']
});
if (document.body) {
observer.observe(document.body, {
attributes: true,
attributeFilter: ['class']
});
}
})();
</script>
</body>
</html>`;
}
}