@opentabs-dev/mcp-server 0.0.22 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter-files.d.ts +53 -0
- package/dist/adapter-files.d.ts.map +1 -0
- package/dist/adapter-files.js +176 -0
- package/dist/adapter-files.js.map +1 -0
- package/dist/audit-disk.d.ts.map +1 -1
- package/dist/audit-disk.js +5 -6
- package/dist/audit-disk.js.map +1 -1
- package/dist/browser-tool-names.d.ts +14 -0
- package/dist/browser-tool-names.d.ts.map +1 -0
- package/dist/browser-tool-names.js +52 -0
- package/dist/browser-tool-names.js.map +1 -0
- package/dist/browser-tools/analyze-site/detect-auth.d.ts.map +1 -1
- package/dist/browser-tools/analyze-site/detect-auth.js +2 -1
- package/dist/browser-tools/analyze-site/detect-auth.js.map +1 -1
- package/dist/browser-tools/analyze-site/index.d.ts.map +1 -1
- package/dist/browser-tools/analyze-site/index.js +111 -15
- package/dist/browser-tools/analyze-site/index.js.map +1 -1
- package/dist/browser-tools/execute-script.js +1 -1
- package/dist/browser-tools/execute-script.js.map +1 -1
- package/dist/browser-tools/extension-get-logs.d.ts +2 -2
- package/dist/browser-tools/focus-tab.js +1 -1
- package/dist/browser-tools/focus-tab.js.map +1 -1
- package/dist/browser-tools/get-console-logs.d.ts +2 -2
- package/dist/browser-tools/index.d.ts +4 -0
- package/dist/browser-tools/index.d.ts.map +1 -1
- package/dist/browser-tools/index.js +19 -0
- package/dist/browser-tools/index.js.map +1 -1
- package/dist/browser-tools/select-option.d.ts.map +1 -1
- package/dist/browser-tools/select-option.js +11 -6
- package/dist/browser-tools/select-option.js.map +1 -1
- package/dist/config.d.ts +13 -16
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +43 -109
- package/dist/config.js.map +1 -1
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +9 -12
- package/dist/discovery.js.map +1 -1
- package/dist/extension-handlers.d.ts +102 -0
- package/dist/extension-handlers.d.ts.map +1 -0
- package/dist/extension-handlers.js +443 -0
- package/dist/extension-handlers.js.map +1 -0
- package/dist/extension-protocol.d.ts +6 -38
- package/dist/extension-protocol.d.ts.map +1 -1
- package/dist/extension-protocol.js +47 -535
- package/dist/extension-protocol.js.map +1 -1
- package/dist/file-watcher.d.ts.map +1 -1
- package/dist/file-watcher.js +106 -104
- package/dist/file-watcher.js.map +1 -1
- package/dist/http-routes.d.ts +9 -7
- package/dist/http-routes.d.ts.map +1 -1
- package/dist/http-routes.js +275 -265
- package/dist/http-routes.js.map +1 -1
- package/dist/index.js +17 -10
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +14 -3
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +42 -22
- package/dist/loader.js.map +1 -1
- package/dist/log-buffer.d.ts.map +1 -1
- package/dist/log-buffer.js +17 -6
- package/dist/log-buffer.js.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +5 -2
- package/dist/logger.js.map +1 -1
- package/dist/mcp-setup.d.ts +3 -18
- package/dist/mcp-setup.d.ts.map +1 -1
- package/dist/mcp-setup.js +13 -407
- package/dist/mcp-setup.js.map +1 -1
- package/dist/mcp-tool-dispatch.d.ts +51 -0
- package/dist/mcp-tool-dispatch.d.ts.map +1 -0
- package/dist/mcp-tool-dispatch.js +410 -0
- package/dist/mcp-tool-dispatch.js.map +1 -0
- package/dist/plugin-management.d.ts +123 -0
- package/dist/plugin-management.d.ts.map +1 -0
- package/dist/plugin-management.js +340 -0
- package/dist/plugin-management.js.map +1 -0
- package/dist/registry.d.ts +2 -13
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +2 -24
- package/dist/registry.js.map +1 -1
- package/dist/reload.d.ts.map +1 -1
- package/dist/reload.js +13 -2
- package/dist/reload.js.map +1 -1
- package/dist/resolver.d.ts.map +1 -1
- package/dist/resolver.js +33 -25
- package/dist/resolver.js.map +1 -1
- package/dist/state.d.ts +30 -21
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +14 -10
- package/dist/state.js.map +1 -1
- package/dist/version-check.d.ts.map +1 -1
- package/dist/version-check.js +36 -14
- package/dist/version-check.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool dispatch handlers for browser tools and plugin tools.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from the monolith tools/call handler in mcp-setup.ts so each
|
|
5
|
+
* dispatch path can be tested independently. The tools/call handler in
|
|
6
|
+
* mcp-setup.ts delegates to these functions after resolving the tool name.
|
|
7
|
+
*/
|
|
8
|
+
import { dispatchToExtension, isDispatchError, sendInvocationStart, sendInvocationEnd, sendConfirmationRequest, } from './extension-protocol.js';
|
|
9
|
+
import { log } from './logger.js';
|
|
10
|
+
import { evaluatePermission } from './permissions.js';
|
|
11
|
+
import { sanitizeErrorMessage } from './sanitize-error.js';
|
|
12
|
+
import { isBrowserToolEnabled, appendAuditEntry, isSessionAllowed } from './state.js';
|
|
13
|
+
import { toErrorMessage } from '@opentabs-dev/shared';
|
|
14
|
+
/** Maximum concurrent tool dispatches per plugin to prevent tab performance degradation */
|
|
15
|
+
const MAX_CONCURRENT_DISPATCHES_PER_PLUGIN = 5;
|
|
16
|
+
/** Keys that could trigger prototype pollution in JSON deserialization */
|
|
17
|
+
const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
18
|
+
/**
|
|
19
|
+
* Recursively remove dangerous keys from objects to prevent prototype pollution
|
|
20
|
+
* in MCP clients that use naive JSON deserialization.
|
|
21
|
+
*/
|
|
22
|
+
const sanitizeOutput = (obj, depth = 0) => {
|
|
23
|
+
if (obj === null || obj === undefined || typeof obj !== 'object')
|
|
24
|
+
return obj;
|
|
25
|
+
if (depth > 50)
|
|
26
|
+
return '[Object too deep]';
|
|
27
|
+
if (Array.isArray(obj))
|
|
28
|
+
return obj.map(item => sanitizeOutput(item, depth + 1));
|
|
29
|
+
const result = {};
|
|
30
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
31
|
+
if (!DANGEROUS_KEYS.has(key))
|
|
32
|
+
result[key] = sanitizeOutput(value, depth + 1);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Extract the target domain hostname for a browser tool call.
|
|
38
|
+
*
|
|
39
|
+
* - Tools with a `url` param (get_cookies, set_cookie, delete_cookies, open_tab):
|
|
40
|
+
* parse the hostname from the URL
|
|
41
|
+
* - Tools with a `tabId` param: dispatch browser.getTabInfo to get the tab's URL,
|
|
42
|
+
* then parse the hostname
|
|
43
|
+
* - Tools with neither: return null (observe-tier tools, extension diagnostics)
|
|
44
|
+
*/
|
|
45
|
+
const resolveToolDomain = async (toolName, args, state) => {
|
|
46
|
+
// URL-based tools: parse domain from the url parameter
|
|
47
|
+
const urlArg = args.url;
|
|
48
|
+
if (typeof urlArg === 'string' && urlArg !== '') {
|
|
49
|
+
try {
|
|
50
|
+
return new URL(urlArg).hostname;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Tab-based tools: get the tab's URL via a lightweight dispatch
|
|
57
|
+
const tabIdArg = args.tabId;
|
|
58
|
+
if (typeof tabIdArg === 'number') {
|
|
59
|
+
try {
|
|
60
|
+
const tabInfo = (await dispatchToExtension(state, 'browser.getTabInfo', { tabId: tabIdArg }));
|
|
61
|
+
if (typeof tabInfo.url === 'string' && tabInfo.url !== '') {
|
|
62
|
+
return new URL(tabInfo.url).hostname;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Tab may be closed or unreachable — domain resolution is best-effort
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Truncate tool parameters into a short preview for the confirmation dialog.
|
|
74
|
+
* Shows the first ~200 characters of the JSON-stringified args.
|
|
75
|
+
*/
|
|
76
|
+
const truncateParamsPreview = (args) => {
|
|
77
|
+
const json = JSON.stringify(args, null, 2);
|
|
78
|
+
if (json.length <= 200)
|
|
79
|
+
return json;
|
|
80
|
+
return json.slice(0, 200) + '…';
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Format a structured error response for MCP clients.
|
|
84
|
+
*
|
|
85
|
+
* When the error data contains structured fields (category, retryable, retryAfterMs),
|
|
86
|
+
* produces a human-readable prefix line followed by a machine-readable JSON block.
|
|
87
|
+
* When only the code is present (legacy), produces [CODE] message.
|
|
88
|
+
*/
|
|
89
|
+
const formatStructuredError = (code, message, data) => {
|
|
90
|
+
const category = typeof data?.category === 'string' ? data.category : undefined;
|
|
91
|
+
const retryable = typeof data?.retryable === 'boolean' ? data.retryable : undefined;
|
|
92
|
+
const retryAfterMs = typeof data?.retryAfterMs === 'number' ? data.retryAfterMs : undefined;
|
|
93
|
+
const hasStructuredFields = category !== undefined || retryable !== undefined || retryAfterMs !== undefined;
|
|
94
|
+
if (!hasStructuredFields) {
|
|
95
|
+
return `[${code}] ${message}`;
|
|
96
|
+
}
|
|
97
|
+
// Build the human-readable prefix with only present fields
|
|
98
|
+
const parts = [`code=${code}`];
|
|
99
|
+
if (category !== undefined)
|
|
100
|
+
parts.push(`category=${category}`);
|
|
101
|
+
if (retryable !== undefined)
|
|
102
|
+
parts.push(`retryable=${String(retryable)}`);
|
|
103
|
+
if (retryAfterMs !== undefined)
|
|
104
|
+
parts.push(`retryAfterMs=${retryAfterMs}`);
|
|
105
|
+
const prefix = `[ERROR ${parts.join(' ')}] ${message}`;
|
|
106
|
+
// Build the machine-readable JSON block with only present fields
|
|
107
|
+
const jsonObj = { code };
|
|
108
|
+
if (category !== undefined)
|
|
109
|
+
jsonObj.category = category;
|
|
110
|
+
if (retryable !== undefined)
|
|
111
|
+
jsonObj.retryable = retryable;
|
|
112
|
+
if (retryAfterMs !== undefined)
|
|
113
|
+
jsonObj.retryAfterMs = retryAfterMs;
|
|
114
|
+
return `${prefix}\n\n\`\`\`json\n${JSON.stringify(jsonObj)}\n\`\`\``;
|
|
115
|
+
};
|
|
116
|
+
/** Format a ZodError into a readable validation message listing each failing field */
|
|
117
|
+
const formatZodError = (err) => {
|
|
118
|
+
const issues = err.issues.map(issue => {
|
|
119
|
+
const path = issue.path.length > 0 ? issue.path.join('.') : '(root)';
|
|
120
|
+
return ` - ${path}: ${issue.message}`;
|
|
121
|
+
});
|
|
122
|
+
return `Invalid arguments:\n${issues.join('\n')}`;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Handle a browser tool call: Zod validation, permission evaluation,
|
|
126
|
+
* confirmation flow, execution, output sanitization, and audit logging.
|
|
127
|
+
*/
|
|
128
|
+
const handleBrowserToolCall = async (state, toolName, args, cachedBt, extra) => {
|
|
129
|
+
if (!isBrowserToolEnabled(state, toolName)) {
|
|
130
|
+
return {
|
|
131
|
+
content: [{ type: 'text', text: `Tool ${toolName} is disabled via configuration` }],
|
|
132
|
+
isError: true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// Validate args through the tool's Zod input schema
|
|
136
|
+
const parseResult = cachedBt.tool.input.safeParse(args);
|
|
137
|
+
if (!parseResult.success) {
|
|
138
|
+
return {
|
|
139
|
+
content: [{ type: 'text', text: formatZodError(parseResult.error) }],
|
|
140
|
+
isError: true,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// Permission evaluation: resolve domain, check session permissions,
|
|
144
|
+
// evaluate against policy, and hold for confirmation if needed.
|
|
145
|
+
const parsedArgs = parseResult.data;
|
|
146
|
+
const domain = await resolveToolDomain(toolName, parsedArgs, state);
|
|
147
|
+
// Check session permissions first (set by previous "Allow Always" actions)
|
|
148
|
+
const permission = isSessionAllowed(state.sessionPermissions, toolName, domain)
|
|
149
|
+
? 'allow'
|
|
150
|
+
: evaluatePermission(toolName, domain, state);
|
|
151
|
+
if (permission === 'deny') {
|
|
152
|
+
return {
|
|
153
|
+
content: [
|
|
154
|
+
{
|
|
155
|
+
type: 'text',
|
|
156
|
+
text: `PERMISSION_DENIED: Tool "${toolName}" is denied${domain ? ` for domain "${domain}"` : ''} by permission policy. Ask the user to update their OpenTabs permission configuration if this tool is needed.`,
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
isError: true,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (permission === 'ask') {
|
|
163
|
+
// Send progress notification to MCP client (if progressToken is available)
|
|
164
|
+
const progressToken = extra._meta?.progressToken;
|
|
165
|
+
if (progressToken !== undefined) {
|
|
166
|
+
extra
|
|
167
|
+
.sendNotification({
|
|
168
|
+
method: 'notifications/progress',
|
|
169
|
+
params: {
|
|
170
|
+
progressToken,
|
|
171
|
+
progress: 0,
|
|
172
|
+
total: 1,
|
|
173
|
+
message: 'Waiting for human approval in the OpenTabs side panel...',
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
.catch(() => {
|
|
177
|
+
// Fire-and-forget
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
const paramsPreview = truncateParamsPreview(parsedArgs);
|
|
182
|
+
const tabIdArg = parsedArgs.tabId;
|
|
183
|
+
const decision = await sendConfirmationRequest(state, toolName, domain, typeof tabIdArg === 'number' ? tabIdArg : undefined, paramsPreview);
|
|
184
|
+
if (decision === 'deny') {
|
|
185
|
+
return {
|
|
186
|
+
content: [
|
|
187
|
+
{
|
|
188
|
+
type: 'text',
|
|
189
|
+
text: `PERMISSION_DENIED: The user denied "${toolName}"${domain ? ` on "${domain}"` : ''}. Inform the user that the operation was blocked by their decision.`,
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
isError: true,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
// decision is 'allow_once' or 'allow_always' — proceed with dispatch
|
|
196
|
+
// (allow_always session rules are handled by handleConfirmationResponse in extension-protocol)
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
const msg = toErrorMessage(err);
|
|
200
|
+
if (msg === 'CONFIRMATION_TIMEOUT') {
|
|
201
|
+
return {
|
|
202
|
+
content: [
|
|
203
|
+
{
|
|
204
|
+
type: 'text',
|
|
205
|
+
text: `CONFIRMATION_TIMEOUT: Human approval for "${toolName}"${domain ? ` on "${domain}"` : ''} timed out after 30 seconds. The user did not respond in the OpenTabs side panel. Ask the user to try again.`,
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
isError: true,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
content: [{ type: 'text', text: `Confirmation error: ${msg}` }],
|
|
213
|
+
isError: true,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const btStartTs = Date.now();
|
|
218
|
+
let btSuccess = true;
|
|
219
|
+
let btErrorInfo;
|
|
220
|
+
try {
|
|
221
|
+
const result = await cachedBt.tool.handler(parseResult.data, state);
|
|
222
|
+
const cleaned = sanitizeOutput(result);
|
|
223
|
+
return {
|
|
224
|
+
content: [{ type: 'text', text: JSON.stringify(cleaned, null, 2) }],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
btSuccess = false;
|
|
229
|
+
const msg = toErrorMessage(err);
|
|
230
|
+
btErrorInfo = { code: 'UNKNOWN', message: msg };
|
|
231
|
+
return {
|
|
232
|
+
content: [{ type: 'text', text: `Browser tool error: ${msg}` }],
|
|
233
|
+
isError: true,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
appendAuditEntry(state, {
|
|
238
|
+
timestamp: new Date(btStartTs).toISOString(),
|
|
239
|
+
tool: toolName,
|
|
240
|
+
plugin: 'browser',
|
|
241
|
+
success: btSuccess,
|
|
242
|
+
durationMs: Date.now() - btStartTs,
|
|
243
|
+
error: btErrorInfo,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* Handle a plugin tool call: Ajv validation, concurrency limiting,
|
|
249
|
+
* dispatch to extension, error formatting, and audit logging.
|
|
250
|
+
*
|
|
251
|
+
* The caller must have already verified the tool is callable via checkToolCallable.
|
|
252
|
+
*/
|
|
253
|
+
const handlePluginToolCall = async (state, toolName, args, pluginName, toolBaseName, lookup, extra) => {
|
|
254
|
+
// Validate args against the tool's JSON Schema before dispatching.
|
|
255
|
+
// The validator is pre-compiled at discovery time for performance.
|
|
256
|
+
// If schema compilation failed, reject the call entirely — unvalidated
|
|
257
|
+
// input must never reach plugin handlers.
|
|
258
|
+
if (!lookup.validate) {
|
|
259
|
+
return {
|
|
260
|
+
content: [
|
|
261
|
+
{
|
|
262
|
+
type: 'text',
|
|
263
|
+
text: `Tool "${toolName}" cannot be called: schema compilation failed. ${lookup.validationErrors()}`,
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
isError: true,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
// Wrap validation in try-catch: compiled Ajv validators can throw on
|
|
270
|
+
// pathological input (e.g., regex catastrophic backtracking from a
|
|
271
|
+
// community plugin's pattern keyword). Normal schemas complete in
|
|
272
|
+
// microseconds; this guard catches the unexpected edge case.
|
|
273
|
+
let valid;
|
|
274
|
+
try {
|
|
275
|
+
valid = lookup.validate(args);
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
log.warn(`Schema validation threw for tool "${toolName}":`, err);
|
|
279
|
+
return {
|
|
280
|
+
content: [
|
|
281
|
+
{
|
|
282
|
+
type: 'text',
|
|
283
|
+
text: `Tool "${toolName}" validation failed unexpectedly. The tool's schema may be invalid.`,
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
isError: true,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
if (!valid) {
|
|
290
|
+
return {
|
|
291
|
+
content: [
|
|
292
|
+
{
|
|
293
|
+
type: 'text',
|
|
294
|
+
text: `Invalid arguments for tool "${toolName}":\n${lookup.validationErrors()}`,
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
isError: true,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
log.debug('tool.call: input validated for', toolName);
|
|
301
|
+
// Concurrency limit: prevent a runaway MCP client from flooding a single
|
|
302
|
+
// plugin's tab with simultaneous executeScript calls. Each dispatch runs
|
|
303
|
+
// in the page's MAIN world, so too many concurrent dispatches can degrade
|
|
304
|
+
// the target tab's performance.
|
|
305
|
+
const currentDispatches = state.activeDispatches.get(pluginName) ?? 0;
|
|
306
|
+
if (currentDispatches >= MAX_CONCURRENT_DISPATCHES_PER_PLUGIN) {
|
|
307
|
+
return {
|
|
308
|
+
content: [
|
|
309
|
+
{
|
|
310
|
+
type: 'text',
|
|
311
|
+
text: `Too many concurrent dispatches for plugin "${pluginName}" (limit: ${MAX_CONCURRENT_DISPATCHES_PER_PLUGIN}). Wait for in-flight requests to complete.`,
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
isError: true,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
// Send invocation start notification to extension (for side panel)
|
|
318
|
+
sendInvocationStart(state, pluginName, toolBaseName);
|
|
319
|
+
const startTs = Date.now();
|
|
320
|
+
let success = true;
|
|
321
|
+
let errorInfo;
|
|
322
|
+
try {
|
|
323
|
+
state.activeDispatches.set(pluginName, currentDispatches + 1);
|
|
324
|
+
if (!state.extensionWs) {
|
|
325
|
+
success = false;
|
|
326
|
+
return {
|
|
327
|
+
content: [
|
|
328
|
+
{
|
|
329
|
+
type: 'text',
|
|
330
|
+
text: 'Extension not connected. Please ensure the OpenTabs Chrome extension is running.',
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
isError: true,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
log.debug('tool.call: dispatching', pluginName + '/' + toolBaseName);
|
|
337
|
+
// Extract progressToken from MCP request _meta and build onProgress callback
|
|
338
|
+
const progressToken = extra._meta?.progressToken;
|
|
339
|
+
const onProgress = progressToken !== undefined
|
|
340
|
+
? (progress, total, message) => {
|
|
341
|
+
const params = { progressToken, progress, total };
|
|
342
|
+
if (message !== undefined)
|
|
343
|
+
params.message = message;
|
|
344
|
+
extra.sendNotification({ method: 'notifications/progress', params }).catch(() => {
|
|
345
|
+
// Fire-and-forget — errors in the progress chain must not affect tool execution
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
: undefined;
|
|
349
|
+
const result = await dispatchToExtension(state, 'tool.dispatch', { plugin: pluginName, tool: toolBaseName, input: args }, { label: `${pluginName}/${toolBaseName}`, progressToken, onProgress });
|
|
350
|
+
const rawOutput = result.output ?? result;
|
|
351
|
+
const cleaned = sanitizeOutput(rawOutput);
|
|
352
|
+
return {
|
|
353
|
+
content: [{ type: 'text', text: JSON.stringify(cleaned, null, 2) }],
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
catch (err) {
|
|
357
|
+
success = false;
|
|
358
|
+
if (isDispatchError(err)) {
|
|
359
|
+
const code = err.code;
|
|
360
|
+
let errorMsg = err.message;
|
|
361
|
+
if (code === -32001) {
|
|
362
|
+
errorMsg = `Tab closed: ${errorMsg}`;
|
|
363
|
+
}
|
|
364
|
+
else if (code === -32002) {
|
|
365
|
+
errorMsg = `Tab unavailable: ${errorMsg}`;
|
|
366
|
+
}
|
|
367
|
+
const toolErrorCode = err.data?.code;
|
|
368
|
+
const category = typeof err.data?.category === 'string' ? err.data.category : undefined;
|
|
369
|
+
if (typeof toolErrorCode === 'string') {
|
|
370
|
+
errorMsg = formatStructuredError(toolErrorCode, errorMsg, err.data);
|
|
371
|
+
errorInfo = { code: toolErrorCode, message: err.message, category };
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
errorInfo = { code: String(code), message: err.message };
|
|
375
|
+
}
|
|
376
|
+
return {
|
|
377
|
+
content: [{ type: 'text', text: errorMsg }],
|
|
378
|
+
isError: true,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const msg = sanitizeErrorMessage(toErrorMessage(err));
|
|
382
|
+
errorInfo = { code: 'UNKNOWN', message: msg };
|
|
383
|
+
return {
|
|
384
|
+
content: [{ type: 'text', text: `Tool dispatch error: ${msg}` }],
|
|
385
|
+
isError: true,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
finally {
|
|
389
|
+
const prev = state.activeDispatches.get(pluginName) ?? 1;
|
|
390
|
+
if (prev <= 1) {
|
|
391
|
+
state.activeDispatches.delete(pluginName);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
state.activeDispatches.set(pluginName, prev - 1);
|
|
395
|
+
}
|
|
396
|
+
const durationMs = Date.now() - startTs;
|
|
397
|
+
log.debug('tool.call:', pluginName + '/' + toolBaseName, 'completed in', `${durationMs}ms`);
|
|
398
|
+
sendInvocationEnd(state, pluginName, toolBaseName, durationMs, success);
|
|
399
|
+
appendAuditEntry(state, {
|
|
400
|
+
timestamp: new Date(startTs).toISOString(),
|
|
401
|
+
tool: toolName,
|
|
402
|
+
plugin: pluginName,
|
|
403
|
+
success,
|
|
404
|
+
durationMs,
|
|
405
|
+
error: errorInfo,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
export { sanitizeOutput, formatZodError, handleBrowserToolCall, handlePluginToolCall };
|
|
410
|
+
//# sourceMappingURL=mcp-tool-dispatch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-tool-dispatch.js","sourceRoot":"","sources":["../src/mcp-tool-dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EA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AK,CAAC,YAAY,EAAE,UAAU,GAAG,GAAG,GAAG,YAAY,EAAE,cAAc,EAAE,GAAG,UAAU,IAAI,CAAC,CAAC;QAC5F,iBAAiB,CAAC,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;QACxE,gBAAgB,CAAC,KAAK,EAAE;YACtB,SAAS,EAAE,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE;YAC1C,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,UAAU;YAClB,OAAO;YACP,UAAU;YACV,KAAK,EAAE,SAAS;SACjB,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC;AAGF,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin management operations for the extension WebSocket protocol.
|
|
3
|
+
*
|
|
4
|
+
* Handles npm registry search, plugin installation, name normalization,
|
|
5
|
+
* and validation. These operations are invoked via JSON-RPC methods
|
|
6
|
+
* from the side panel (relayed through the Chrome extension).
|
|
7
|
+
*/
|
|
8
|
+
import { normalizePluginName, isValidPluginPackageName } from '@opentabs-dev/shared';
|
|
9
|
+
import type { ServerState, RegisteredPlugin } from './state.js';
|
|
10
|
+
interface PluginSearchResult {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
version: string;
|
|
14
|
+
author: string;
|
|
15
|
+
isOfficial: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface PluginInstallResult {
|
|
18
|
+
ok: true;
|
|
19
|
+
plugin: {
|
|
20
|
+
name: string;
|
|
21
|
+
displayName: string;
|
|
22
|
+
version: string;
|
|
23
|
+
toolCount: number;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Search the npm registry for opentabs plugins.
|
|
28
|
+
* Returns up to 20 results matching `keywords:opentabs-plugin` + optional query.
|
|
29
|
+
*
|
|
30
|
+
* @throws Error with `code` property for structured JSON-RPC error handling:
|
|
31
|
+
* - code -32603: registry unreachable or unexpected error
|
|
32
|
+
* - code -32603 with retryAfterMs: rate limited (HTTP 429)
|
|
33
|
+
*/
|
|
34
|
+
declare const searchNpmPlugins: (query?: string) => Promise<PluginSearchResult[]>;
|
|
35
|
+
/**
|
|
36
|
+
* Install a plugin from npm globally and trigger server rediscovery.
|
|
37
|
+
*
|
|
38
|
+
* @param name - Plugin name (shorthand or full npm package name)
|
|
39
|
+
* @param state - Server state (used to look up installed plugin after rediscovery)
|
|
40
|
+
* @param onReload - Callback to trigger plugin rediscovery
|
|
41
|
+
* @returns Install result with plugin metadata
|
|
42
|
+
*
|
|
43
|
+
* @throws Error with `code` property for structured JSON-RPC error handling:
|
|
44
|
+
* - code -32602: invalid params or naming convention violation
|
|
45
|
+
* - code -32603: npm install failure
|
|
46
|
+
*/
|
|
47
|
+
declare const installPlugin: (name: string, state: ServerState, onReload: () => Promise<{
|
|
48
|
+
plugins: number;
|
|
49
|
+
durationMs: number;
|
|
50
|
+
}>) => Promise<PluginInstallResult>;
|
|
51
|
+
/**
|
|
52
|
+
* Update result type returned to callers
|
|
53
|
+
*/
|
|
54
|
+
interface PluginUpdateResult {
|
|
55
|
+
ok: true;
|
|
56
|
+
plugin: {
|
|
57
|
+
name: string;
|
|
58
|
+
displayName: string;
|
|
59
|
+
version: string;
|
|
60
|
+
toolCount: number;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Find a registered plugin by name (shorthand or full npm package name).
|
|
65
|
+
*
|
|
66
|
+
* Tries all candidate package names for shorthand inputs (official scoped first,
|
|
67
|
+
* then community unscoped) to match against both `npmPackageName` and the
|
|
68
|
+
* derived internal name.
|
|
69
|
+
*/
|
|
70
|
+
declare const findPlugin: (state: ServerState, name: string) => RegisteredPlugin | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Update a plugin from npm and trigger server rediscovery.
|
|
73
|
+
*
|
|
74
|
+
* @param name - Plugin name (shorthand or full npm package name)
|
|
75
|
+
* @param state - Server state (used to verify plugin exists and look up result)
|
|
76
|
+
* @param onReload - Callback to trigger plugin rediscovery
|
|
77
|
+
* @returns Update result with plugin metadata
|
|
78
|
+
*
|
|
79
|
+
* @throws Error with `code` property for structured JSON-RPC error handling:
|
|
80
|
+
* - code -32602: plugin not currently installed
|
|
81
|
+
* - code -32603: npm update failure
|
|
82
|
+
*/
|
|
83
|
+
declare const updatePlugin: (name: string, state: ServerState, onReload: () => Promise<{
|
|
84
|
+
plugins: number;
|
|
85
|
+
durationMs: number;
|
|
86
|
+
}>) => Promise<PluginUpdateResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Remove a plugin (npm or local) and trigger server rediscovery.
|
|
89
|
+
*
|
|
90
|
+
* @param name - Plugin name (shorthand or full npm package name)
|
|
91
|
+
* @param state - Server state
|
|
92
|
+
* @param onReload - Callback to trigger plugin rediscovery
|
|
93
|
+
* @returns `{ ok: true }` on success
|
|
94
|
+
*
|
|
95
|
+
* @throws Error with `code` property for structured JSON-RPC error handling:
|
|
96
|
+
* - code -32602: plugin not currently installed
|
|
97
|
+
* - code -32603: npm uninstall failure
|
|
98
|
+
*/
|
|
99
|
+
declare const removePlugin: (name: string, state: ServerState, onReload: () => Promise<{
|
|
100
|
+
plugins: number;
|
|
101
|
+
durationMs: number;
|
|
102
|
+
}>) => Promise<{
|
|
103
|
+
ok: true;
|
|
104
|
+
}>;
|
|
105
|
+
/**
|
|
106
|
+
* Check outdated plugins result type
|
|
107
|
+
*/
|
|
108
|
+
interface CheckUpdatesResult {
|
|
109
|
+
outdatedPlugins: Array<{
|
|
110
|
+
name: string;
|
|
111
|
+
currentVersion: string;
|
|
112
|
+
latestVersion: string;
|
|
113
|
+
updateCommand: string;
|
|
114
|
+
}>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Check all npm-installed plugins for updates using the existing version-check logic.
|
|
118
|
+
* Calls checkForUpdates to populate state.outdatedPlugins, then returns the results.
|
|
119
|
+
*/
|
|
120
|
+
declare const checkPluginUpdates: (state: ServerState) => Promise<CheckUpdatesResult>;
|
|
121
|
+
export type { PluginSearchResult, PluginInstallResult, PluginUpdateResult, CheckUpdatesResult };
|
|
122
|
+
export { normalizePluginName, isValidPluginPackageName, searchNpmPlugins, installPlugin, updatePlugin, removePlugin, checkPluginUpdates, findPlugin, };
|
|
123
|
+
//# sourceMappingURL=plugin-management.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-management.d.ts","sourceRoot":"","sources":["../src/plugin-management.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAKL,mBAAmB,EACnB,wBAAwB,EAGzB,MAAM,sBAAsB,CAAC;AAI9B,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAqBhE,UAAU,kBAAkB;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,OAAO,CAAC;CACrB;AAMD,UAAU,mBAAmB;IAC3B,EAAE,EAAE,IAAI,CAAC;IACT,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAqDD;;;;;;;GAOG;AACH,QAAA,MAAM,gBAAgB,GAAU,QAAQ,MAAM,KAAG,OAAO,CAAC,kBAAkB,EAAE,CAyC5E,CAAC;AAMF;;;;;;;;;;;GAWG;AACH,QAAA,MAAM,aAAa,GACjB,MAAM,MAAM,EACZ,OAAO,WAAW,EAClB,UAAU,MAAM,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAA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