@procedures/mcp-server 0.60.1 → 0.62.0
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/CHANGELOG.md +46 -0
- package/README.md +7 -0
- package/dist/host.d.ts +3 -0
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +1 -0
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/procedures/mcp/serve.d.ts +2 -0
- package/dist/procedures/mcp/serve.d.ts.map +1 -1
- package/dist/procedures/mcp/serve.js +3 -0
- package/dist/procedures/mcp/serve.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts +5 -3
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +54 -34
- package/dist/transport/mcp-transport.js.map +1 -1
- package/dist/transport/search-listing.d.ts +48 -0
- package/dist/transport/search-listing.d.ts.map +1 -0
- package/dist/transport/search-listing.js +134 -0
- package/dist/transport/search-listing.js.map +1 -0
- package/dist/transport/untrusted-sites.d.ts +39 -0
- package/dist/transport/untrusted-sites.d.ts.map +1 -0
- package/dist/transport/untrusted-sites.js +110 -0
- package/dist/transport/untrusted-sites.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/host.ts +4 -0
- package/src/index.ts +3 -0
- package/src/procedures/mcp/serve.ts +5 -0
- package/src/transport/mcp-transport.ts +53 -34
- package/src/transport/search-listing.ts +153 -0
- package/src/transport/untrusted-sites.ts +132 -0
- package/src/types.ts +9 -0
|
@@ -60,6 +60,8 @@ const McpServeInputSchema = z.object({
|
|
|
60
60
|
readOnly: z.boolean().default(false).describe("True: the tools are only the procedures that change nothing, and each call has that capability."),
|
|
61
61
|
/** The instructions of the server: the MCP client gives them to the model. */
|
|
62
62
|
instructions: z.string().optional().describe("The instructions of the server: the MCP client gives them to the model."),
|
|
63
|
+
/** How the server lists its tools: each tool (the default), or three search tools (direction 40). */
|
|
64
|
+
listing: z.enum(["each", "search"]).optional().describe("How the server lists its tools: each tool (the default), or three search tools (direction 40)."),
|
|
63
65
|
});
|
|
64
66
|
|
|
65
67
|
type McpServeInput = z.infer<typeof McpServeInputSchema>;
|
|
@@ -97,6 +99,8 @@ export interface ServeOptions {
|
|
|
97
99
|
version?: string | undefined;
|
|
98
100
|
/** The instructions of the server. */
|
|
99
101
|
instructions?: string | undefined;
|
|
102
|
+
/** How the server lists its tools: `"each"` (the default) or `"search"`. */
|
|
103
|
+
listing?: "each" | "search" | undefined;
|
|
100
104
|
/** True: debug lines go to stderr. */
|
|
101
105
|
debug?: boolean | undefined;
|
|
102
106
|
/** The faces of the host. The default is stdio. */
|
|
@@ -155,6 +159,7 @@ export async function prepareServe(
|
|
|
155
159
|
...(input.name !== undefined ? { name: input.name } : {}),
|
|
156
160
|
...(input.version !== undefined ? { version: input.version } : {}),
|
|
157
161
|
...(input.instructions !== undefined ? { instructions: input.instructions } : {}),
|
|
162
|
+
...(input.listing !== undefined ? { listing: input.listing } : {}),
|
|
158
163
|
...(input.debug !== undefined ? { debug: input.debug } : {}),
|
|
159
164
|
faces: input.faces ?? { stdio: true },
|
|
160
165
|
});
|
|
@@ -33,7 +33,7 @@ import type {
|
|
|
33
33
|
ReportProgress,
|
|
34
34
|
Logger,
|
|
35
35
|
} from "@procedures/client";
|
|
36
|
-
import { LOCAL_CREDENTIALS, NARROWING_METADATA_KEY, PROCEDURE_REGISTRY, addNarrowing, pathToKey, pathToMethod, createElicit, elicitWithPolicy, lastResortError, requestCredentials, retryableOf,
|
|
36
|
+
import { LOCAL_CREDENTIALS, NARROWING_METADATA_KEY, PROCEDURE_REGISTRY, addNarrowing, pathToKey, pathToMethod, createElicit, elicitWithPolicy, lastResortError, requestCredentials, retryableOf, writeLog, type UntrustedText } from "@procedures/client";
|
|
37
37
|
import {
|
|
38
38
|
proceduresToMcpTools,
|
|
39
39
|
toToolDefinition,
|
|
@@ -44,8 +44,10 @@ import { createStdioTransport } from "./stdio.js";
|
|
|
44
44
|
import { createSseTransport } from "./sse.js";
|
|
45
45
|
import { createHttpTransport, requestCredentialsOf, type McpHttpTransport } from "./http.js";
|
|
46
46
|
import { optionalMs } from "@procedures/client";
|
|
47
|
-
import { createUntrustedEnvelopes } from "../untrusted.js";
|
|
47
|
+
import { createUntrustedEnvelopes, type UntrustedEnvelopes } from "../untrusted.js";
|
|
48
|
+
import { separateUntrusted } from "./untrusted-sites.js";
|
|
48
49
|
import { taskStoreOf, type McpTaskStore } from "../tasks.js";
|
|
50
|
+
import { SEARCH_LISTING_NAMES, SEARCH_LISTING_TOOLS, callTarget, describeToolResult, searchToolsResult, toolNotFound } from "./search-listing.js";
|
|
49
51
|
|
|
50
52
|
/**
|
|
51
53
|
* The tools with the names of a `toolName` function. Without the function, the tools keep the
|
|
@@ -323,6 +325,9 @@ export class McpServerTransport implements ServerTransport {
|
|
|
323
325
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
324
326
|
this.log("ListToolsRequest received");
|
|
325
327
|
|
|
328
|
+
// The search listing gives three tools, and the model finds the others by a search
|
|
329
|
+
if (this.options.listing === "search") return { tools: [...SEARCH_LISTING_TOOLS] };
|
|
330
|
+
|
|
326
331
|
// Without a task store, no tool runs as a task: the list does not say that it can
|
|
327
332
|
const tools = Array.from(this.tools.values(), (tool) => {
|
|
328
333
|
const definition = toToolDefinition(tool);
|
|
@@ -337,20 +342,30 @@ export class McpServerTransport implements ServerTransport {
|
|
|
337
342
|
|
|
338
343
|
// CallToolRequestSchema -> Execute procedure
|
|
339
344
|
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
340
|
-
const { name,
|
|
345
|
+
const { name, task } = request.params;
|
|
346
|
+
let args = request.params.arguments;
|
|
341
347
|
|
|
342
348
|
this.log(`CallToolRequest: ${name}`);
|
|
343
349
|
|
|
350
|
+
// The three tools of the search listing. call_tool calls its tool as a direct call does.
|
|
351
|
+
let tool = this.tools.get(name);
|
|
352
|
+
if (this.options.listing === "search") {
|
|
353
|
+
if (name === SEARCH_LISTING_NAMES.search) return searchToolsResult(this.tools.values(), args, this.options.json);
|
|
354
|
+
if (name === SEARCH_LISTING_NAMES.describe) return describeToolResult(this.tools, args, { tasks: this.taskStore !== undefined, json: this.options.json });
|
|
355
|
+
if (name === SEARCH_LISTING_NAMES.call) {
|
|
356
|
+
const target = callTarget(this.tools, args);
|
|
357
|
+
if ("refused" in target) return target.refused;
|
|
358
|
+
tool = target.tool;
|
|
359
|
+
args = target.input;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
344
363
|
// Find tool
|
|
345
|
-
const tool = this.tools.get(name);
|
|
346
364
|
if (!tool) {
|
|
347
365
|
this.log(`Tool not found: ${name}`);
|
|
348
|
-
return
|
|
349
|
-
content: [{ type: "text" as const, text: `Tool not found: ${name}` }],
|
|
350
|
-
isError: true,
|
|
351
|
-
_meta: { code: "NOT_FOUND", retryable: false },
|
|
352
|
-
};
|
|
366
|
+
return toolNotFound(name);
|
|
353
367
|
}
|
|
368
|
+
const toolName = tool.name;
|
|
354
369
|
|
|
355
370
|
// MCP tasks (CLI-35): a call as a task needs a tool that can run as one, and a call of a
|
|
356
371
|
// tool that runs only as a task needs a task. Without a task store each call is a request.
|
|
@@ -358,12 +373,12 @@ export class McpServerTransport implements ServerTransport {
|
|
|
358
373
|
const callExtra = extra as CallExtra;
|
|
359
374
|
if (task !== undefined) {
|
|
360
375
|
if (!this.taskStore || (support !== "optional" && support !== "required")) {
|
|
361
|
-
throw new McpError(ErrorCode.MethodNotFound, `The tool ${
|
|
376
|
+
throw new McpError(ErrorCode.MethodNotFound, `The tool ${toolName} does not run as a task`);
|
|
362
377
|
}
|
|
363
378
|
return this.startTask(server, this.taskStore, tool, args, callExtra, { method: request.method, params: request.params });
|
|
364
379
|
}
|
|
365
380
|
if (support === "required") {
|
|
366
|
-
throw new McpError(ErrorCode.MethodNotFound, `The tool ${
|
|
381
|
+
throw new McpError(ErrorCode.MethodNotFound, `The tool ${toolName} runs only as a task: call it with a task`);
|
|
367
382
|
}
|
|
368
383
|
return this.callTool(server, tool, args, callExtra);
|
|
369
384
|
});
|
|
@@ -592,12 +607,12 @@ export class McpServerTransport implements ServerTransport {
|
|
|
592
607
|
break;
|
|
593
608
|
}
|
|
594
609
|
// An item with untrusted fields gives its data, then its envelopes (CLI-10)
|
|
595
|
-
const rendered =
|
|
596
|
-
const text = rendered
|
|
610
|
+
const rendered = this.render(item, tool, false).content;
|
|
611
|
+
const text = rendered.map((block) => block.text).join("\n");
|
|
597
612
|
blocks.push({ type: "text", text });
|
|
598
613
|
if (blocks.length > MAX_STREAM_BLOCKS) blocks.shift();
|
|
599
614
|
if (progressToken !== undefined && !ownProgress()) {
|
|
600
|
-
const message = rendered
|
|
615
|
+
const message = rendered[0]!.text;
|
|
601
616
|
await extra
|
|
602
617
|
.sendNotification({
|
|
603
618
|
method: "notifications/progress",
|
|
@@ -624,33 +639,36 @@ export class McpServerTransport implements ServerTransport {
|
|
|
624
639
|
}
|
|
625
640
|
|
|
626
641
|
/**
|
|
627
|
-
* The content of a successful value.
|
|
628
|
-
*
|
|
629
|
-
*
|
|
642
|
+
* The content of a successful value. The untrusted fields are the fields that the tool declares
|
|
643
|
+
* (`metadata.untrusted`) and the fields of the marked objects at each depth (`markUntrusted()`).
|
|
644
|
+
* With such a field, the first block has the data as JSON, and the second block has the texts in
|
|
645
|
+
* envelopes. In the structured content, a declared field is the text of its envelopes, and each
|
|
646
|
+
* text of another field is in its envelope, with the same shape.
|
|
630
647
|
*/
|
|
631
|
-
private render(value: unknown, tool: McpTool): McpToolContent {
|
|
632
|
-
const
|
|
633
|
-
if (fields.length === 0 || value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
634
|
-
const structured = tool.outputSchema ? structuredContentOf(value) : undefined;
|
|
635
|
-
return { content: [{ type: "text", text: toText(value, this.options.json) }], ...(structured ? { structuredContent: structured } : {}) };
|
|
636
|
-
}
|
|
637
|
-
const data: Record<string, unknown> = { ...(value as Record<string, unknown>) };
|
|
638
|
-
const envelopes = createUntrustedEnvelopes(this.options.untrusted ?? {});
|
|
648
|
+
private render(value: unknown, tool: McpTool | undefined, structuredOutput = true): McpToolContent {
|
|
649
|
+
const name = tool?.name ?? "";
|
|
639
650
|
const wrap = this.options.untrusted?.wrap;
|
|
651
|
+
// One answer has one id for all of its envelopes
|
|
652
|
+
let envelopes: UntrustedEnvelopes | undefined;
|
|
653
|
+
const seal = (texts: readonly UntrustedText[], field: string): string =>
|
|
654
|
+
wrap ? wrap(texts, { tool: name, field }) : (envelopes ??= createUntrustedEnvelopes(this.options.untrusted ?? {})).text(texts);
|
|
655
|
+
const { data, structured, sites } = separateUntrusted(value, tool?.untrusted ?? [], (text, label) => seal([text], label));
|
|
656
|
+
const schema = structuredOutput && tool?.outputSchema !== undefined;
|
|
657
|
+
if (sites.length === 0) {
|
|
658
|
+
const plain = schema ? structuredContentOf(value) : undefined;
|
|
659
|
+
return { content: [{ type: "text", text: toText(value, this.options.json) }], ...(plain ? { structuredContent: plain } : {}) };
|
|
660
|
+
}
|
|
640
661
|
const texts: Record<string, string> = {};
|
|
641
662
|
const blocks: string[] = [];
|
|
642
|
-
for (const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
delete data[field];
|
|
646
|
-
const text = wrap ? wrap(items, { tool: tool.name, field }) : envelopes.text(items);
|
|
647
|
-
texts[field] = text;
|
|
663
|
+
for (const site of sites) {
|
|
664
|
+
const text = seal(site.texts, site.label);
|
|
665
|
+
if (site.declared) texts[site.label] = text;
|
|
648
666
|
if (text !== "") blocks.push(text);
|
|
649
667
|
}
|
|
650
668
|
const content: McpTextBlock[] = [{ type: "text", text: toText(data, this.options.json) }];
|
|
651
669
|
if (blocks.length > 0) content.push({ type: "text", text: blocks.join("\n") });
|
|
652
|
-
const
|
|
653
|
-
return { content, ...(
|
|
670
|
+
const object = schema ? structuredContentOf(structured) : undefined;
|
|
671
|
+
return { content, ...(object ? { structuredContent: { ...object, ...texts } } : {}) };
|
|
654
672
|
}
|
|
655
673
|
|
|
656
674
|
/**
|
|
@@ -769,7 +787,8 @@ export class McpServerTransport implements ServerTransport {
|
|
|
769
787
|
}
|
|
770
788
|
|
|
771
789
|
this.log(`Refreshed tools: ${this.tools.size} available`);
|
|
772
|
-
|
|
790
|
+
// The search listing does not change: its three tools find the new tools
|
|
791
|
+
if (this.running && this.options.listing !== "search" && this.toolSignature() !== before) this.scheduleListChanged();
|
|
773
792
|
}
|
|
774
793
|
|
|
775
794
|
/** The tool names and descriptions: a change of this text is a change of the tool list. */
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The search listing of an MCP server (direction 40, the tool surface). A server with thousands
|
|
3
|
+
* of tools cannot give each tool to the context of the model. With `listing: "search"`, the list
|
|
4
|
+
* of tools has three tools: `search_tools`, `describe_tool` and `call_tool`. The model finds a
|
|
5
|
+
* tool with a search, reads its input schema, and calls it. The expose rule and the call policy
|
|
6
|
+
* of the server stay the same: the search finds only the tools of the server, and `call_tool`
|
|
7
|
+
* calls a tool as a direct call does.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CallToolResult, Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
11
|
+
import { SEARCH_LIMIT, SEARCH_MAX_LIMIT, searchDocuments, type SearchDocument } from "@procedures/client";
|
|
12
|
+
import { toToolDefinition, type McpTool } from "@procedures/mcp";
|
|
13
|
+
|
|
14
|
+
/** How the server lists its tools: `"each"` lists each tool, `"search"` lists the three search tools. */
|
|
15
|
+
export type McpToolListing = "each" | "search";
|
|
16
|
+
|
|
17
|
+
/** The names of the three tools of the search listing. */
|
|
18
|
+
export const SEARCH_LISTING_NAMES = {
|
|
19
|
+
search: "search_tools",
|
|
20
|
+
describe: "describe_tool",
|
|
21
|
+
call: "call_tool",
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
/** The three tools of the search listing. */
|
|
25
|
+
export const SEARCH_LISTING_TOOLS: readonly Tool[] = [
|
|
26
|
+
{
|
|
27
|
+
name: SEARCH_LISTING_NAMES.search,
|
|
28
|
+
description:
|
|
29
|
+
"Search the tools of this server by words. Each hit gives the name of a tool and the first line of its description. Read the input schema of a hit with describe_tool, then call it with call_tool.",
|
|
30
|
+
inputSchema: {
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: {
|
|
33
|
+
query: { type: "string", minLength: 1, description: 'The words to find, for example "median interval"' },
|
|
34
|
+
limit: { type: "integer", minimum: 1, maximum: SEARCH_MAX_LIMIT, description: `The most hits. The default is ${SEARCH_LIMIT}.` },
|
|
35
|
+
},
|
|
36
|
+
required: ["query"],
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
},
|
|
39
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: SEARCH_LISTING_NAMES.describe,
|
|
43
|
+
description: "Give the description, the input schema and the output schema of one tool of this server, by the name that search_tools gives.",
|
|
44
|
+
inputSchema: {
|
|
45
|
+
type: "object",
|
|
46
|
+
properties: { name: { type: "string", minLength: 1, description: "The name of the tool" } },
|
|
47
|
+
required: ["name"],
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
},
|
|
50
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: SEARCH_LISTING_NAMES.call,
|
|
54
|
+
description: "Call one tool of this server by its name. The input must agree with the input schema that describe_tool gives.",
|
|
55
|
+
inputSchema: {
|
|
56
|
+
type: "object",
|
|
57
|
+
properties: {
|
|
58
|
+
name: { type: "string", minLength: 1, description: "The name of the tool" },
|
|
59
|
+
input: { type: "object", description: "The input of the tool", additionalProperties: true },
|
|
60
|
+
},
|
|
61
|
+
required: ["name"],
|
|
62
|
+
additionalProperties: false,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
/** The text of a value for a text block: JSON with an indent of 2, or JSON on one line. */
|
|
68
|
+
function jsonText(value: unknown, json: "pretty" | "compact" | undefined): string {
|
|
69
|
+
return json === "compact" ? JSON.stringify(value) : JSON.stringify(value, null, 2);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A result with one text block and the value as structured content. */
|
|
73
|
+
function valueResult(value: Record<string, unknown>, json: "pretty" | "compact" | undefined): CallToolResult {
|
|
74
|
+
return { content: [{ type: "text", text: jsonText(value, json) }], structuredContent: value };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The result of a refused input of a search tool. */
|
|
78
|
+
function invalidInput(message: string): CallToolResult {
|
|
79
|
+
return { content: [{ type: "text", text: message }], isError: true, _meta: { code: "INVALID_INPUT", retryable: false } };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The result of a name that names no tool of the server. */
|
|
83
|
+
export function toolNotFound(name: string): CallToolResult {
|
|
84
|
+
return { content: [{ type: "text", text: `Tool not found: ${name}` }], isError: true, _meta: { code: "NOT_FOUND", retryable: false } };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
88
|
+
|
|
89
|
+
/** The name in the arguments of `describe_tool` or `call_tool`, or the result that refuses the arguments. */
|
|
90
|
+
function nameOf(args: Record<string, unknown> | undefined): string | CallToolResult {
|
|
91
|
+
const name = args?.["name"];
|
|
92
|
+
return typeof name === "string" && name.length > 0 ? name : invalidInput("The input needs a name: the name of a tool from search_tools");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* This function gives the search document of each tool: its name, the segments of its path, its
|
|
97
|
+
* description and its tags. `search_tools` ranks these documents, and the quality gate measures them.
|
|
98
|
+
*/
|
|
99
|
+
export function toolSearchDocuments(tools: Iterable<McpTool>): SearchDocument[] {
|
|
100
|
+
return Array.from(tools, (tool) => ({
|
|
101
|
+
name: tool.name,
|
|
102
|
+
words: tool.procedurePath,
|
|
103
|
+
description: tool.description,
|
|
104
|
+
tags: tool.tags,
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** This function gives the result of `search_tools`: the best hits of the query over the tools. */
|
|
109
|
+
export function searchToolsResult(tools: Iterable<McpTool>, args: Record<string, unknown> | undefined, json?: "pretty" | "compact"): CallToolResult {
|
|
110
|
+
const query = args?.["query"];
|
|
111
|
+
if (typeof query !== "string" || query.trim().length === 0) return invalidInput("The input needs a query: the words to find");
|
|
112
|
+
const limit = args?.["limit"];
|
|
113
|
+
if (limit !== undefined && (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > SEARCH_MAX_LIMIT)) {
|
|
114
|
+
return invalidInput(`The limit is a whole number from 1 to ${SEARCH_MAX_LIMIT}`);
|
|
115
|
+
}
|
|
116
|
+
const result = searchDocuments(toolSearchDocuments(tools), query, { limit: limit as number | undefined });
|
|
117
|
+
// A hit with no description has no field description in JSON
|
|
118
|
+
return valueResult({ hits: result.hits, matched: result.matched, total: result.total }, json);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This function gives the result of `describe_tool`: the definition of the tool, as the list of
|
|
123
|
+
* each tool gives it. Without tasks, the definition does not say that the tool can run as one.
|
|
124
|
+
*/
|
|
125
|
+
export function describeToolResult(
|
|
126
|
+
tools: ReadonlyMap<string, McpTool>,
|
|
127
|
+
args: Record<string, unknown> | undefined,
|
|
128
|
+
options: { readonly tasks: boolean; readonly json?: "pretty" | "compact" | undefined },
|
|
129
|
+
): CallToolResult {
|
|
130
|
+
const name = nameOf(args);
|
|
131
|
+
if (typeof name !== "string") return name;
|
|
132
|
+
const tool = tools.get(name);
|
|
133
|
+
if (!tool) return toolNotFound(name);
|
|
134
|
+
const definition = toToolDefinition(tool) as Tool & Record<string, unknown>;
|
|
135
|
+
if (!options.tasks) delete definition.execution;
|
|
136
|
+
return valueResult(definition, options.json);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The target of a `call_tool` call: its tool and input, or the result that refuses its arguments. */
|
|
140
|
+
export type CallTarget =
|
|
141
|
+
| { readonly tool: McpTool; readonly input: Record<string, unknown> | undefined }
|
|
142
|
+
| { readonly refused: CallToolResult };
|
|
143
|
+
|
|
144
|
+
/** This function gives the tool and the input of a `call_tool` call, or the result that refuses its arguments. */
|
|
145
|
+
export function callTarget(tools: ReadonlyMap<string, McpTool>, args: Record<string, unknown> | undefined): CallTarget {
|
|
146
|
+
const name = nameOf(args);
|
|
147
|
+
if (typeof name !== "string") return { refused: name };
|
|
148
|
+
const tool = tools.get(name);
|
|
149
|
+
if (!tool) return { refused: toolNotFound(name) };
|
|
150
|
+
const input = args?.["input"];
|
|
151
|
+
if (input !== undefined && !isRecord(input)) return { refused: invalidInput("The input of the tool is an object") };
|
|
152
|
+
return { tool, input };
|
|
153
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The untrusted fields of an output, at each depth (CLI-10).
|
|
3
|
+
*
|
|
4
|
+
* A field is untrusted when the tool declares it (`metadata.untrusted`), or when the invocation
|
|
5
|
+
* marked its object (`markUntrusted()` of `@procedures/client`). The mark goes with the output of
|
|
6
|
+
* a procedure into the output of each caller: `client.chain`, a procedure of `procedure.define`, or
|
|
7
|
+
* a procedure of code that gives the object.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { untrustedMarkOf, untrustedTextsOf, type UntrustedText } from "@procedures/client";
|
|
11
|
+
|
|
12
|
+
/** One untrusted field of an output. */
|
|
13
|
+
export interface UntrustedSite {
|
|
14
|
+
/** The path of the field from the top of the output, with dots: for example `steps.0.web_content`. */
|
|
15
|
+
readonly label: string;
|
|
16
|
+
/** True for a field at the top of the output that the tool declares. */
|
|
17
|
+
readonly declared: boolean;
|
|
18
|
+
/** The texts of the field, each with its source. */
|
|
19
|
+
readonly texts: readonly UntrustedText[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** An output with its untrusted fields apart. */
|
|
23
|
+
export interface SeparatedOutput {
|
|
24
|
+
/** The output without its untrusted fields. Without a site, the output itself. */
|
|
25
|
+
readonly data: unknown;
|
|
26
|
+
/**
|
|
27
|
+
* The output for the structured content. A declared field is absent. Each text of another
|
|
28
|
+
* untrusted field is in its envelope, with the same shape: a text stays a text.
|
|
29
|
+
*/
|
|
30
|
+
readonly structured: unknown;
|
|
31
|
+
/** The untrusted fields: the declared fields first, in the order of the tool. */
|
|
32
|
+
readonly sites: readonly UntrustedSite[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A function that gives one text in its envelope. It gets the text and the label of its field. */
|
|
36
|
+
export type Enclose = (text: UntrustedText, label: string) => string;
|
|
37
|
+
|
|
38
|
+
/** True for an object of the JSON shape: its prototype is `Object.prototype` or null. */
|
|
39
|
+
function isPlain(value: object): boolean {
|
|
40
|
+
const prototype = Object.getPrototypeOf(value) as unknown;
|
|
41
|
+
return prototype === Object.prototype || prototype === null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The text with the path of its field as an attribute of its envelope. */
|
|
45
|
+
function labelled(text: UntrustedText, label: string): UntrustedText {
|
|
46
|
+
return { text: text.text, source: { ...text.source, field: label } };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The value of an untrusted field with each text in its envelope. A value with no text is undefined. */
|
|
50
|
+
function enclosedValue(value: unknown, label: string, enclose: Enclose): unknown {
|
|
51
|
+
const one = (item: unknown): unknown => {
|
|
52
|
+
if (Array.isArray(item)) return undefined;
|
|
53
|
+
const [text] = untrustedTextsOf(item);
|
|
54
|
+
if (!text) return undefined;
|
|
55
|
+
const sealed = enclose(labelled(text, label), label);
|
|
56
|
+
return typeof item === "string" ? sealed : { ...(item as object), text: sealed };
|
|
57
|
+
};
|
|
58
|
+
if (Array.isArray(value)) return value.map(one).filter((item) => item !== undefined);
|
|
59
|
+
return one(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The two copies of one part of an output. */
|
|
63
|
+
interface Part {
|
|
64
|
+
readonly data: unknown;
|
|
65
|
+
readonly structured: unknown;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* This function separates the untrusted fields of an output. It copies only the objects on the
|
|
70
|
+
* path to an untrusted field, so an output without one stays the same object. A shared object
|
|
71
|
+
* gets one copy, and its fields one site, with the path where the walk first finds it.
|
|
72
|
+
*/
|
|
73
|
+
export function separateUntrusted(value: unknown, declared: readonly string[], enclose: Enclose): SeparatedOutput {
|
|
74
|
+
const sites: UntrustedSite[] = [];
|
|
75
|
+
const parts = new Map<object, Part>();
|
|
76
|
+
const walk = (node: unknown, path: readonly string[], top: boolean): Part => {
|
|
77
|
+
if (node === null || typeof node !== "object") return { data: node, structured: node };
|
|
78
|
+
const known = parts.get(node);
|
|
79
|
+
if (known) return known;
|
|
80
|
+
const same: Part = { data: node, structured: node };
|
|
81
|
+
// A cycle meets the object itself, as the JSON text does
|
|
82
|
+
parts.set(node, same);
|
|
83
|
+
let part = same;
|
|
84
|
+
if (Array.isArray(node)) {
|
|
85
|
+
const data: unknown[] = [];
|
|
86
|
+
const structured: unknown[] = [];
|
|
87
|
+
let changed = false;
|
|
88
|
+
node.forEach((item: unknown, index) => {
|
|
89
|
+
const inner = walk(item, [...path, String(index)], false);
|
|
90
|
+
data.push(inner.data);
|
|
91
|
+
structured.push(inner.structured);
|
|
92
|
+
if (inner.data !== item || inner.structured !== item) changed = true;
|
|
93
|
+
});
|
|
94
|
+
if (changed) part = { data, structured };
|
|
95
|
+
} else {
|
|
96
|
+
const marks = untrustedMarkOf(node);
|
|
97
|
+
const fields = top ? [...new Set([...declared, ...marks])] : marks;
|
|
98
|
+
if (fields.length > 0 || isPlain(node)) {
|
|
99
|
+
const record = node as Record<string, unknown>;
|
|
100
|
+
const data: Record<string, unknown> = { ...record };
|
|
101
|
+
const structured: Record<string, unknown> = { ...record };
|
|
102
|
+
let changed = false;
|
|
103
|
+
for (const field of fields) {
|
|
104
|
+
if (!Object.hasOwn(record, field)) continue;
|
|
105
|
+
changed = true;
|
|
106
|
+
// path-check: the label of a field path in the result (`items.0.text`), not a procedure path
|
|
107
|
+
const label = [...path, field].join(".");
|
|
108
|
+
const own = top && declared.includes(field);
|
|
109
|
+
const texts = untrustedTextsOf(record[field]);
|
|
110
|
+
sites.push({ label, declared: own, texts: own ? texts : texts.map((text) => labelled(text, label)) });
|
|
111
|
+
delete data[field];
|
|
112
|
+
const sealed = own ? undefined : enclosedValue(record[field], label, enclose);
|
|
113
|
+
if (sealed === undefined) delete structured[field];
|
|
114
|
+
else structured[field] = sealed;
|
|
115
|
+
}
|
|
116
|
+
for (const key of Object.keys(record)) {
|
|
117
|
+
if (fields.includes(key)) continue;
|
|
118
|
+
const inner = walk(record[key], [...path, key], false);
|
|
119
|
+
if (inner.data === record[key] && inner.structured === record[key]) continue;
|
|
120
|
+
changed = true;
|
|
121
|
+
data[key] = inner.data;
|
|
122
|
+
structured[key] = inner.structured;
|
|
123
|
+
}
|
|
124
|
+
if (changed) part = { data, structured };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
parts.set(node, part);
|
|
128
|
+
return part;
|
|
129
|
+
};
|
|
130
|
+
const { data, structured } = walk(value, [], true);
|
|
131
|
+
return { data, structured, sites };
|
|
132
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Configuration options for the MCP server transport.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import type { McpToolListing } from "./transport/search-listing.js";
|
|
7
8
|
import type { Readable, Writable } from "node:stream";
|
|
8
9
|
import type { Transport as SdkTransport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
9
10
|
import type { BrowserGuardOptions, ElicitationPolicy, ProcedureRegistry } from "@procedures/client";
|
|
@@ -171,6 +172,14 @@ export interface McpServerTransportOptions {
|
|
|
171
172
|
/** Tool filtering options */
|
|
172
173
|
toolFilter?: McpToolFilter;
|
|
173
174
|
|
|
175
|
+
/**
|
|
176
|
+
* How the server lists its tools (direction 40, the tool surface). `"each"` (the default) lists
|
|
177
|
+
* each tool. `"search"` lists three tools: `search_tools`, `describe_tool` and `call_tool`. Then
|
|
178
|
+
* the model loads the schema of a tool only when it needs the tool. The expose rule and the call
|
|
179
|
+
* policy do not change.
|
|
180
|
+
*/
|
|
181
|
+
listing?: McpToolListing | undefined;
|
|
182
|
+
|
|
174
183
|
/**
|
|
175
184
|
* The name of the tool of a procedure. The default joins the segments of the path with dots
|
|
176
185
|
* (`git.status`). A server that moves to this transport keeps its tool names with it, for
|