-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathtool-invoker.ts
More file actions
519 lines (457 loc) · 15.3 KB
/
Copy pathtool-invoker.ts
File metadata and controls
519 lines (457 loc) · 15.3 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { Effect, Match, Option, Predicate } from "effect";
import * as Cause from "effect/Cause";
import type {
Executor,
ToolId,
Tool,
ToolSchema,
InvokeOptions,
Source,
} from "@executor-js/sdk/core";
import type { SandboxToolInvoker } from "@executor-js/codemode-core";
import { ExecutionToolError } from "./errors";
/**
* Extract the source namespace from a tool path. Tool paths look like
* "<sourceId>.<op>" or "<sourceId>.<group>.<op>" — we take the first
* segment as a cheap, non-lookup stand-in for the source id so the span
* attribute is always populated without hitting `executor.sources.list()`
* per call.
*/
const extractSourceNamespace = (path: string): string => {
const idx = path.indexOf(".");
return idx === -1 ? path : path.slice(0, idx);
};
const hasStringMessage = (value: unknown): value is { readonly message: string } =>
value !== null &&
typeof value === "object" &&
"message" in value &&
typeof value.message === "string";
const messageFromErrorLike = (value: unknown): string | undefined => {
if (hasStringMessage(value)) {
return value.message;
}
return undefined;
};
const renderToolErrorMessage = (error: unknown): string =>
messageFromErrorLike(error) ??
(typeof error === "undefined" ? "Tool execution failed" : renderUnknownPrimitive(error));
const renderUnknownPrimitive = (value: unknown): string =>
Match.value(value).pipe(
Match.when(Match.string, (s) => s),
Match.whenOr(Match.number, Match.boolean, Match.bigint, Match.symbol, (x) => x.toString()),
Match.option,
Option.getOrElse(() => "Tool execution failed"),
);
type ToolResultEnvelope = {
readonly error?: unknown;
readonly data?: unknown;
};
const isToolResultEnvelope = (value: unknown): value is ToolResultEnvelope =>
value !== null && typeof value === "object" && ("error" in value || "data" in value);
const hasToolResultError = (
value: ToolResultEnvelope,
): value is ToolResultEnvelope & { readonly error: unknown } =>
value.error !== null && value.error !== undefined;
/**
* Bridges QuickJS `tools.someSource.someOp(args)` calls into
* `executor.tools.invoke(toolId, args)`.
*
* Wrapped in `Effect.fn("mcp.tool.dispatch")` so every tool call becomes a
* span in the Effect tracer. Attributes:
* - `mcp.tool.name` — full tool path (e.g. "github.repos.get")
* - `mcp.tool.source_id` — first segment of the path (namespace)
*
* `mcp.tool.kind` (openapi | mcp | graphql | code) is NOT annotated here
* because it would require a `sources.list()` lookup on every invocation.
* Callers that already know the source kind can annotate at their own span.
*/
export const makeExecutorToolInvoker = (
executor: Executor,
options: { readonly invokeOptions: InvokeOptions },
): SandboxToolInvoker => ({
invoke: Effect.fn("mcp.tool.dispatch")(function* ({ path, args }) {
yield* Effect.annotateCurrentSpan({
"mcp.tool.name": path,
"mcp.tool.source_id": extractSourceNamespace(path),
});
const result = yield* executor.tools.invoke(path as ToolId, args, options.invokeOptions).pipe(
Effect.catchCause((cause): Effect.Effect<never, ExecutionToolError> => {
const err = cause.reasons.find(Cause.isFailReason)?.error;
if (!isElicitationDeclinedError(err)) {
return Effect.fail(
new ExecutionToolError({
message: renderToolErrorMessage(err),
cause: err ?? cause,
}),
);
}
return Effect.fail(
new ExecutionToolError({
message: `Tool "${err.toolId}" requires approval but the request was ${err.action === "cancel" ? "cancelled" : "declined"} by the user.`,
cause: err,
}),
);
}),
);
if (!isToolResultEnvelope(result)) {
return result;
}
if (hasToolResultError(result)) {
return yield* new ExecutionToolError({
message: renderToolErrorMessage(result.error),
cause: result.error,
});
}
if ("data" in result) {
return result.data;
}
return result;
}),
});
const isElicitationDeclinedError = (
value: unknown,
): value is {
readonly _tag: "ElicitationDeclinedError";
readonly toolId: string;
readonly action: "cancel" | "decline";
} =>
Predicate.isTagged(value, "ElicitationDeclinedError") &&
value !== null &&
typeof value === "object" &&
"toolId" in value &&
typeof value.toolId === "string" &&
"action" in value &&
(value.action === "cancel" || value.action === "decline");
export type ToolDiscoveryResult = {
readonly path: string;
readonly name: string;
readonly description?: string;
readonly sourceId: string;
readonly score: number;
};
export type ExecutorSourceListItem = {
readonly id: string;
readonly name: string;
readonly kind: string;
readonly runtime?: boolean;
readonly canRemove?: boolean;
readonly canRefresh?: boolean;
readonly toolCount: number;
};
/**
* Page of results from a list-style discovery tool. Shared by
* `tools.search` and `tools.executor.sources.list` so the model sees one
* consistent shape:
*
* - `items` — the page (slice).
* - `total` — count after filtering, before pagination. The model
* can use this to detect truncation.
* - `hasMore` — convenience flag for `(offset + items.length) < total`.
* - `nextOffset` — concrete offset for the next page when `hasMore`,
* `null` otherwise. Pre-computing it removes a class of
* off-by-one mistakes when the model paginates.
*/
export type PagedResult<T> = {
readonly items: readonly T[];
readonly total: number;
readonly hasMore: boolean;
readonly nextOffset: number | null;
};
const paginate = <T>(all: readonly T[], offset: number, limit: number): PagedResult<T> => {
const total = all.length;
const start = Math.min(Math.max(offset, 0), total);
const items = all.slice(start, start + limit);
const consumed = start + items.length;
const hasMore = consumed < total;
return {
items,
total,
hasMore,
nextOffset: hasMore ? consumed : null,
};
};
type SearchableTool = Pick<Tool, "id" | "sourceId" | "name" | "description">;
type PreparedField = {
readonly raw: string;
readonly tokens: readonly string[];
};
const SEARCH_FIELD_WEIGHTS = {
path: 12,
sourceId: 8,
name: 10,
description: 5,
} as const;
const normalizeSearchText = (value: string): string =>
value
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/[_./:-]+/g, " ")
.toLowerCase()
.trim();
const tokenizeSearchText = (value: string): string[] =>
normalizeSearchText(value)
.split(/[^a-z0-9]+/)
.map((token) => token.trim())
.filter(Boolean);
const prepareField = (value?: string): PreparedField => ({
raw: normalizeSearchText(value ?? ""),
tokens: tokenizeSearchText(value ?? ""),
});
const scorePreparedField = (
query: string,
queryTokens: readonly string[],
field: PreparedField,
weight: number,
): {
readonly score: number;
readonly matchedTokens: ReadonlySet<string>;
readonly exactPhraseMatch: boolean;
} => {
if (field.raw.length === 0) {
return {
score: 0,
matchedTokens: new Set<string>(),
exactPhraseMatch: false,
};
}
let score = 0;
const matchedTokens = new Set<string>();
const exactPhraseMatch = query.length > 0 && field.raw.includes(query);
if (query.length > 0) {
if (field.raw === query) {
score += weight * 14;
} else if (field.raw.startsWith(query)) {
score += weight * 9;
} else if (exactPhraseMatch) {
score += weight * 6;
}
}
for (const token of queryTokens) {
if (field.tokens.includes(token)) {
score += weight * 4;
matchedTokens.add(token);
continue;
}
if (
field.tokens.some((candidate) => candidate.startsWith(token) || token.startsWith(candidate))
) {
score += weight * 2;
matchedTokens.add(token);
continue;
}
if (field.raw.includes(token)) {
score += weight;
matchedTokens.add(token);
}
}
return {
score,
matchedTokens,
exactPhraseMatch,
};
};
const matchesNamespace = (tool: SearchableTool, namespace?: string): boolean => {
if (!namespace || normalizeSearchText(namespace).length === 0) {
return true;
}
const namespaceTokens = tokenizeSearchText(namespace);
if (namespaceTokens.length === 0) {
return true;
}
const sourceTokens = tokenizeSearchText(tool.sourceId);
const pathTokens = tokenizeSearchText(tool.id);
const isPrefixMatch = (tokens: readonly string[]): boolean =>
namespaceTokens.every((token, index) => tokens[index] === token);
return isPrefixMatch(sourceTokens) || isPrefixMatch(pathTokens);
};
const scoreToolMatch = (tool: SearchableTool, query: string): ToolDiscoveryResult | null => {
const normalizedQuery = normalizeSearchText(query);
const queryTokens = tokenizeSearchText(query);
if (normalizedQuery.length === 0 || queryTokens.length === 0) {
return null;
}
const path = prepareField(tool.id);
const sourceId = prepareField(tool.sourceId);
const name = prepareField(tool.name);
const description = prepareField(tool.description);
const fieldScores = [
scorePreparedField(normalizedQuery, queryTokens, path, SEARCH_FIELD_WEIGHTS.path),
scorePreparedField(normalizedQuery, queryTokens, sourceId, SEARCH_FIELD_WEIGHTS.sourceId),
scorePreparedField(normalizedQuery, queryTokens, name, SEARCH_FIELD_WEIGHTS.name),
scorePreparedField(normalizedQuery, queryTokens, description, SEARCH_FIELD_WEIGHTS.description),
];
const matchedTokens = new Set<string>();
let score = 0;
let exactPhraseMatch = false;
for (const fieldScore of fieldScores) {
score += fieldScore.score;
exactPhraseMatch ||= fieldScore.exactPhraseMatch;
for (const token of fieldScore.matchedTokens) {
matchedTokens.add(token);
}
}
if (matchedTokens.size === 0) {
return null;
}
const coverage = matchedTokens.size / queryTokens.length;
const minimumCoverage = queryTokens.length <= 2 ? 1 : 0.6;
if (coverage < minimumCoverage && !exactPhraseMatch) {
return null;
}
if (coverage === 1) {
score += 25;
} else {
score += Math.round(coverage * 10);
}
if (path.tokens[0] === queryTokens[0] || name.tokens[0] === queryTokens[0]) {
score += 8;
}
if (
normalizeSearchText(tool.id) === normalizedQuery ||
normalizeSearchText(tool.name) === normalizedQuery
) {
score += 20;
}
return {
path: tool.id,
name: tool.name,
description: tool.description,
sourceId: tool.sourceId,
score,
};
};
/** What `tools.search()` calls inside the sandbox. */
export const searchTools = Effect.fn("executor.tools.search")(function* (
executor: Executor,
query: string,
limit = 12,
options?: { readonly namespace?: string; readonly offset?: number },
) {
const offset = options?.offset ?? 0;
yield* Effect.annotateCurrentSpan({
"executor.search.query_length": query.length,
"executor.search.limit": limit,
"executor.search.offset": offset,
...(options?.namespace ? { "executor.search.namespace": options.namespace } : {}),
});
const empty: PagedResult<ToolDiscoveryResult> = {
items: [],
total: 0,
hasMore: false,
nextOffset: null,
};
if (normalizeSearchText(query).length === 0) {
return empty;
}
const all = yield* executor.tools.list({ includeAnnotations: false }).pipe(
Effect.mapError(
(cause) =>
new ExecutionToolError({
message: "Failed to list tools for search",
cause,
}),
),
);
const ranked = all
.filter((tool: Tool) => matchesNamespace(tool, options?.namespace))
.map((tool: Tool) => scoreToolMatch(tool, query))
.filter(Predicate.isNotNull)
.sort((left, right) => right.score - left.score || left.path.localeCompare(right.path));
const page = paginate(ranked, offset, limit);
yield* Effect.annotateCurrentSpan({
"executor.search.candidate_count": all.length,
"executor.search.match_count": ranked.length,
"executor.search.result_count": page.items.length,
"executor.search.has_more": page.hasMore,
});
return page;
});
/** What `tools.executor.sources.list()` calls inside the sandbox. */
export const listExecutorSources = Effect.fn("executor.sources.list")(function* (
executor: Executor,
options?: {
readonly query?: string;
readonly limit?: number;
readonly offset?: number;
},
) {
const normalizedQuery = normalizeSearchText(options?.query ?? "");
const limit = options?.limit ?? 50;
const offset = options?.offset ?? 0;
const sources = yield* executor.sources.list().pipe(
Effect.mapError(
(cause) =>
new ExecutionToolError({
message: "Failed to list executor sources",
cause,
}),
),
);
const filtered =
normalizedQuery.length === 0
? sources
: sources.filter((source: Source) => {
const haystack = normalizeSearchText([source.id, source.name, source.kind].join(" "));
return tokenizeSearchText(normalizedQuery).every((token) => haystack.includes(token));
});
// Single query for all tools, then count per source in memory.
const allTools = yield* executor.tools.list({ includeAnnotations: false }).pipe(
Effect.mapError(
(cause) =>
new ExecutionToolError({
message: "Failed to list tools for source counts",
cause,
}),
),
);
const toolCountBySource = new Map<string, number>();
for (const tool of allTools) {
toolCountBySource.set(tool.sourceId, (toolCountBySource.get(tool.sourceId) ?? 0) + 1);
}
const sortedWithCounts = filtered
.map(
(source: Source) =>
({
id: source.id,
name: source.name,
kind: source.kind,
runtime: source.runtime,
canRemove: source.canRemove,
canRefresh: source.canRefresh,
toolCount: toolCountBySource.get(source.id) ?? 0,
}) satisfies ExecutorSourceListItem,
)
.sort((left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id));
const page = paginate(sortedWithCounts, offset, limit);
yield* Effect.annotateCurrentSpan({
"executor.sources.candidate_count": sources.length,
"executor.sources.match_count": sortedWithCounts.length,
"executor.sources.result_count": page.items.length,
"executor.sources.has_more": page.hasMore,
});
return page;
});
/** What `tools.describe.tool()` calls inside the sandbox. */
export const describeTool = Effect.fn("executor.tools.describe")(function* (
executor: Executor,
path: string,
) {
yield* Effect.annotateCurrentSpan({ "mcp.tool.name": path });
// Single tools.schema() call — it already fetches the tool row
// internally. No need to also call tools.list() just for name/description.
const schema: ToolSchema | null = yield* executor.tools.schema(path);
// tools.schema() returns null if the tool doesn't exist. Fall back to
// a minimal stub so callers can still render something.
if (schema === null) {
return { path, name: path };
}
// The schema's id is the tool path; name/description come from the
// tool row which tools.schema() already loaded.
return {
path,
name: schema.name ?? path,
description: schema.description,
inputTypeScript: schema.inputTypeScript,
outputTypeScript: schema.outputTypeScript,
typeScriptDefinitions: schema.typeScriptDefinitions,
};
});