toolsplice 0.0.1 → 0.0.2
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/README.md +62 -330
- package/dist/abortable.d.ts +6 -0
- package/dist/abortable.js +50 -0
- package/dist/adaptive-read.d.ts +50 -0
- package/dist/adaptive-read.js +780 -0
- package/dist/answer-wait.d.ts +7 -0
- package/dist/answer-wait.js +45 -0
- package/dist/cache-reads.d.ts +18 -0
- package/dist/cache-reads.js +89 -0
- package/dist/calibration-ledger.d.ts +37 -0
- package/dist/calibration-ledger.js +148 -0
- package/dist/calibration-marker.d.ts +4 -0
- package/dist/calibration-marker.js +77 -0
- package/dist/catalog-setup.d.ts +33 -0
- package/dist/catalog-setup.js +89 -0
- package/dist/catalog.d.ts +10 -0
- package/dist/catalog.js +42 -0
- package/dist/checkpoint-files.d.ts +15 -0
- package/dist/checkpoint-files.js +99 -0
- package/dist/cli.js +545 -14
- package/dist/coalesce-reads.d.ts +6 -0
- package/dist/coalesce-reads.js +33 -0
- package/dist/contract-caller.d.ts +2 -1
- package/dist/contract-caller.js +35 -16
- package/dist/contract-schema.d.ts +3 -1
- package/dist/contract-schema.js +34 -4
- package/dist/contracts.d.ts +1 -0
- package/dist/contracts.js +5 -1
- package/dist/core.d.ts +31 -1
- package/dist/core.js +27 -0
- package/dist/db-final-answer.d.ts +9 -0
- package/dist/db-final-answer.js +62 -0
- package/dist/define.d.ts +78 -0
- package/dist/define.js +14 -0
- package/dist/final-answer-stores.d.ts +71 -0
- package/dist/final-answer-stores.js +72 -0
- package/dist/final-answer.d.ts +32 -0
- package/dist/final-answer.js +104 -0
- package/dist/impact.d.ts +48 -0
- package/dist/impact.js +71 -0
- package/dist/index.d.ts +32 -1
- package/dist/index.js +28 -0
- package/dist/local-tool.d.ts +18 -0
- package/dist/local-tool.js +32 -0
- package/dist/mcp-apps.d.ts +53 -0
- package/dist/mcp-apps.js +32 -0
- package/dist/mcp-event-delivery.d.ts +98 -0
- package/dist/mcp-event-delivery.js +211 -0
- package/dist/mcp-extensions.d.ts +46 -0
- package/dist/mcp-extensions.js +200 -0
- package/dist/mcp-peer.d.ts +45 -0
- package/dist/mcp-peer.js +207 -0
- package/dist/mcp-server-card.d.ts +21 -0
- package/dist/mcp-server-card.js +54 -0
- package/dist/page-checkpoint.d.ts +71 -0
- package/dist/page-checkpoint.js +286 -0
- package/dist/page-read-gate.d.ts +15 -0
- package/dist/page-read-gate.js +48 -0
- package/dist/pattern.d.ts +1 -0
- package/dist/pattern.js +27 -5
- package/dist/probe.js +6 -19
- package/dist/read-pages.d.ts +25 -0
- package/dist/read-pages.js +85 -0
- package/dist/read-policy.d.ts +6 -0
- package/dist/read-policy.js +16 -0
- package/dist/read-steps.d.ts +12 -0
- package/dist/read-steps.js +36 -0
- package/dist/result-session.d.ts +109 -0
- package/dist/result-session.js +620 -0
- package/dist/route-checkpoint.d.ts +3 -0
- package/dist/route-checkpoint.js +80 -0
- package/dist/route-driver-cli.d.ts +3 -0
- package/dist/route-driver-cli.js +43 -0
- package/dist/route-evaluation.d.ts +141 -0
- package/dist/route-evaluation.js +329 -0
- package/dist/row-groups.d.ts +17 -0
- package/dist/row-groups.js +79 -0
- package/dist/row-query.d.ts +62 -0
- package/dist/row-query.js +172 -0
- package/dist/rules.d.ts +30 -0
- package/dist/rules.js +72 -0
- package/dist/run-diagnostics.d.ts +75 -0
- package/dist/run-diagnostics.js +212 -0
- package/dist/search-fetch.d.ts +50 -0
- package/dist/search-fetch.js +264 -0
- package/dist/skill-files.d.ts +9 -0
- package/dist/skill-files.js +49 -0
- package/dist/splice-diagnostics.d.ts +4 -0
- package/dist/splice-diagnostics.js +24 -16
- package/dist/splice-measure.d.ts +33 -0
- package/dist/splice-measure.js +55 -0
- package/dist/splice-project.d.ts +12 -6
- package/dist/splice-project.js +72 -20
- package/dist/splice-recording.d.ts +29 -2
- package/dist/splice-recording.js +22 -5
- package/dist/splice.d.ts +98 -1
- package/dist/splice.js +225 -27
- package/dist/sqlite-checkpoint-store.d.ts +5 -0
- package/dist/sqlite-checkpoint-store.js +91 -0
- package/dist/sqlite-checkpoint-worker.d.ts +1 -0
- package/dist/sqlite-checkpoint-worker.js +115 -0
- package/dist/test-difference.d.ts +13 -0
- package/dist/test-difference.js +42 -0
- package/dist/testing.d.ts +122 -0
- package/dist/testing.js +261 -0
- package/dist/text-pages.d.ts +24 -0
- package/dist/text-pages.js +36 -0
- package/dist/tool-discovery.d.ts +59 -0
- package/dist/tool-discovery.js +136 -0
- package/dist/unevaluated.d.ts +7 -0
- package/dist/unevaluated.js +156 -0
- package/dist/v2.js +25 -8
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflow-json.js +1 -1
- package/docs/AGENT-FRAMEWORKS.md +431 -0
- package/docs/BENCHMARKS.md +43 -0
- package/docs/INTEGRATION-QUICKSTART.md +859 -0
- package/docs/LEARNING-AND-STORAGE.md +393 -0
- package/docs/MCP.md +204 -0
- package/docs/PROJECTS-AND-UPGRADES.md +67 -9
- package/docs/PYTHON-REFERENCE.md +348 -0
- package/docs/PYTHON.md +120 -0
- package/docs/RECORDING-AND-CORRECTIONS.md +30 -1
- package/docs/TESTING.md +259 -0
- package/examples/mcp-app/build.mjs +11 -0
- package/examples/mcp-app/view.mjs +22 -0
- package/examples/quickstart.mjs +43 -0
- package/examples/route-driver.mjs +67 -0
- package/integrations/ai.d.mts +36 -0
- package/integrations/ai.mjs +178 -0
- package/integrations/call-options.mjs +9 -0
- package/integrations/langchain.d.mts +31 -0
- package/integrations/langchain.mjs +114 -0
- package/integrations/managed.mjs +154 -0
- package/integrations/types.d.mts +13 -0
- package/package.json +47 -3
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { createGuardCompiler } from "./contract-caller.js";
|
|
2
|
+
import { copyJson, JSON_LIMIT, jsonObject } from "./workflow-json.js";
|
|
3
|
+
/** The extension permits an omitted cursor everywhere a null cursor is accepted. */
|
|
4
|
+
export function eventCursor(value) {
|
|
5
|
+
if (value === undefined || value === null)
|
|
6
|
+
return null;
|
|
7
|
+
if (typeof value === "string")
|
|
8
|
+
return value;
|
|
9
|
+
throw new Error("Expected an opaque event cursor or null.");
|
|
10
|
+
}
|
|
11
|
+
export function prepareEvent(descriptor, args, mode) {
|
|
12
|
+
const d = copyJson(descriptor), argsCopy = copyJson(args);
|
|
13
|
+
if (!jsonObject(d) ||
|
|
14
|
+
typeof d.name !== "string" ||
|
|
15
|
+
!d.name ||
|
|
16
|
+
!Array.isArray(d.delivery) ||
|
|
17
|
+
!d.delivery.includes(mode) ||
|
|
18
|
+
!jsonObject(d.inputSchema) ||
|
|
19
|
+
!jsonObject(d.payloadSchema) ||
|
|
20
|
+
!jsonObject(argsCopy))
|
|
21
|
+
throw new Error("Expected an event descriptor supporting " +
|
|
22
|
+
mode +
|
|
23
|
+
" and subscription arguments.");
|
|
24
|
+
const name = d.name, compile = createGuardCompiler();
|
|
25
|
+
const input = compile(d.inputSchema, name, "input"), payload = compile(d.payloadSchema, name, "payload");
|
|
26
|
+
if (!input(argsCopy))
|
|
27
|
+
throw new Error("Event arguments do not match inputSchema.");
|
|
28
|
+
return {
|
|
29
|
+
params: { name, arguments: argsCopy },
|
|
30
|
+
occurrence(value) {
|
|
31
|
+
if (!jsonObject(value) ||
|
|
32
|
+
typeof value.eventId !== "string" ||
|
|
33
|
+
!value.eventId ||
|
|
34
|
+
value.name !== name ||
|
|
35
|
+
typeof value.timestamp !== "string" ||
|
|
36
|
+
!value.timestamp ||
|
|
37
|
+
!jsonObject(value.data) ||
|
|
38
|
+
!payload(value.data))
|
|
39
|
+
throw new Error("Invalid event occurrence or payload.");
|
|
40
|
+
eventCursor(value.cursor);
|
|
41
|
+
return value;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function protocolError(value) {
|
|
46
|
+
if (!jsonObject(value) ||
|
|
47
|
+
typeof value.code !== "number" ||
|
|
48
|
+
!Number.isSafeInteger(value.code) ||
|
|
49
|
+
typeof value.message !== "string")
|
|
50
|
+
throw new Error("Invalid MCP event error.");
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
export function pushReceiver(descriptor, args, requestId) {
|
|
54
|
+
if (typeof requestId !== "string" && !Number.isSafeInteger(requestId))
|
|
55
|
+
throw new Error("Provide the JSON-RPC request ID for this event stream.");
|
|
56
|
+
const event = prepareEvent(descriptor, args, "push");
|
|
57
|
+
let active = false, ended = false;
|
|
58
|
+
return {
|
|
59
|
+
params: event.params,
|
|
60
|
+
receive(message) {
|
|
61
|
+
const frame = copyJson(message);
|
|
62
|
+
if (ended ||
|
|
63
|
+
!jsonObject(frame) ||
|
|
64
|
+
frame.jsonrpc !== "2.0" ||
|
|
65
|
+
Object.hasOwn(frame, "id") ||
|
|
66
|
+
!jsonObject(frame.params) ||
|
|
67
|
+
!jsonObject(frame.params._meta) ||
|
|
68
|
+
frame.params._meta["io.modelcontextprotocol/subscriptionId"] !==
|
|
69
|
+
requestId)
|
|
70
|
+
throw new Error("Expected a notification for this open event stream.");
|
|
71
|
+
const p = frame.params;
|
|
72
|
+
if (frame.method === "notifications/events/active") {
|
|
73
|
+
const cursor = eventCursor(p.cursor);
|
|
74
|
+
if (typeof p.truncated !== "boolean" ||
|
|
75
|
+
typeof p.heartbeatMs !== "number" ||
|
|
76
|
+
!Number.isSafeInteger(p.heartbeatMs) ||
|
|
77
|
+
p.heartbeatMs < 1)
|
|
78
|
+
throw new Error("Invalid MCP event stream confirmation.");
|
|
79
|
+
active = true;
|
|
80
|
+
return {
|
|
81
|
+
type: "active",
|
|
82
|
+
cursor,
|
|
83
|
+
truncated: p.truncated,
|
|
84
|
+
heartbeatMs: p.heartbeatMs,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (!active)
|
|
88
|
+
throw new Error("The event stream has not been acknowledged.");
|
|
89
|
+
switch (frame.method) {
|
|
90
|
+
case "notifications/events/event":
|
|
91
|
+
return {
|
|
92
|
+
type: "event",
|
|
93
|
+
event: event.occurrence(p),
|
|
94
|
+
cursor: eventCursor(p.cursor),
|
|
95
|
+
};
|
|
96
|
+
case "notifications/events/heartbeat":
|
|
97
|
+
return { type: "heartbeat", cursor: eventCursor(p.cursor) };
|
|
98
|
+
case "notifications/events/error":
|
|
99
|
+
return { type: "error", error: protocolError(p.error) };
|
|
100
|
+
case "notifications/events/terminated": {
|
|
101
|
+
const error = protocolError(p.error);
|
|
102
|
+
ended = true;
|
|
103
|
+
return { type: "terminated", error };
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
throw new Error("Unknown MCP event stream notification.");
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function signedHeaders(input) {
|
|
112
|
+
const headers = Object.create(null);
|
|
113
|
+
const names = [
|
|
114
|
+
"webhook-id",
|
|
115
|
+
"webhook-timestamp",
|
|
116
|
+
"webhook-signature",
|
|
117
|
+
"x-mcp-subscription-id",
|
|
118
|
+
];
|
|
119
|
+
const entries = input instanceof Headers ? input.entries() : Object.entries(input);
|
|
120
|
+
for (const [key, value] of entries) {
|
|
121
|
+
const lower = key.toLowerCase();
|
|
122
|
+
if (!names.includes(lower))
|
|
123
|
+
continue;
|
|
124
|
+
if (typeof value !== "string" || !value || Object.hasOwn(headers, lower))
|
|
125
|
+
throw new Error("Invalid MCP webhook headers.");
|
|
126
|
+
headers[lower] = value;
|
|
127
|
+
}
|
|
128
|
+
if (!names.every((name) => Object.hasOwn(headers, name)) ||
|
|
129
|
+
!/^[a-zA-Z0-9_-]+$/.test(headers["webhook-id"]) ||
|
|
130
|
+
!/^(?:0|[1-9][0-9]*)$/.test(headers["webhook-timestamp"]))
|
|
131
|
+
throw new Error("Invalid MCP webhook headers.");
|
|
132
|
+
return headers;
|
|
133
|
+
}
|
|
134
|
+
export async function webhookReceiver(descriptor, args, options) {
|
|
135
|
+
const event = prepareEvent(descriptor, args, "webhook");
|
|
136
|
+
const secret = options?.secret, subscriptionId = options?.subscriptionId;
|
|
137
|
+
if (typeof secret !== "string" ||
|
|
138
|
+
!/^whsec_[A-Za-z0-9+/]+={0,2}$/.test(secret) ||
|
|
139
|
+
(subscriptionId !== undefined &&
|
|
140
|
+
(typeof subscriptionId !== "string" || !subscriptionId)))
|
|
141
|
+
throw new Error("Expected an MCP webhook signing secret and optional subscription ID.");
|
|
142
|
+
const encoded = secret.slice(6), key = Buffer.from(encoded, "base64");
|
|
143
|
+
const canonical = key.toString("base64");
|
|
144
|
+
if (key.length < 24 ||
|
|
145
|
+
key.length > 64 ||
|
|
146
|
+
(encoded !== canonical && encoded !== canonical.replace(/=+$/, "")))
|
|
147
|
+
throw new Error("MCP webhook secrets must encode 24 to 64 bytes.");
|
|
148
|
+
const pkg = "standardwebhooks";
|
|
149
|
+
const { Webhook } = await import(pkg).catch(() => {
|
|
150
|
+
throw new Error("Install standardwebhooks to verify MCP webhook deliveries.");
|
|
151
|
+
});
|
|
152
|
+
const verifier = new Webhook(key, { format: "raw" });
|
|
153
|
+
return (rawBody, input) => {
|
|
154
|
+
if (!(rawBody instanceof Uint8Array) || rawBody.byteLength > JSON_LIMIT)
|
|
155
|
+
throw new Error("Provide the raw MCP webhook request body, up to 5 MiB.");
|
|
156
|
+
const headers = signedHeaders(input);
|
|
157
|
+
const id = headers["x-mcp-subscription-id"], messageId = headers["webhook-id"];
|
|
158
|
+
if (subscriptionId !== undefined && id !== subscriptionId)
|
|
159
|
+
throw new Error("Webhook subscription ID does not match.");
|
|
160
|
+
try {
|
|
161
|
+
verifier.verify(rawBody, headers, { jsonParse: false });
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
throw new Error("MCP webhook signature or timestamp failed verification.");
|
|
165
|
+
}
|
|
166
|
+
let body;
|
|
167
|
+
try {
|
|
168
|
+
body = copyJson(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(rawBody)));
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
throw new Error("Invalid MCP webhook JSON body.");
|
|
172
|
+
}
|
|
173
|
+
if (!jsonObject(body))
|
|
174
|
+
throw new Error("Expected an MCP webhook message.");
|
|
175
|
+
const identity = { subscriptionId: id, messageId };
|
|
176
|
+
if (body.type === "verification") {
|
|
177
|
+
if (typeof body.challenge !== "string" || !body.challenge)
|
|
178
|
+
throw new Error("Missing webhook challenge.");
|
|
179
|
+
return {
|
|
180
|
+
...identity,
|
|
181
|
+
type: "verification",
|
|
182
|
+
challenge: body.challenge,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (subscriptionId === undefined)
|
|
186
|
+
throw new Error("Bind the confirmed subscription ID before receiving webhook events.");
|
|
187
|
+
switch (body.type) {
|
|
188
|
+
case undefined:
|
|
189
|
+
return {
|
|
190
|
+
...identity,
|
|
191
|
+
type: "event",
|
|
192
|
+
event: event.occurrence(body),
|
|
193
|
+
cursor: eventCursor(body.cursor),
|
|
194
|
+
};
|
|
195
|
+
case "gap":
|
|
196
|
+
return {
|
|
197
|
+
...identity,
|
|
198
|
+
type: "gap",
|
|
199
|
+
cursor: eventCursor(body.cursor),
|
|
200
|
+
};
|
|
201
|
+
case "terminated":
|
|
202
|
+
return {
|
|
203
|
+
...identity,
|
|
204
|
+
type: "terminated",
|
|
205
|
+
error: protocolError(body.error),
|
|
206
|
+
};
|
|
207
|
+
default:
|
|
208
|
+
throw new Error("Unknown MCP webhook control message.");
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { BindingCallOptions } from "./execution.js";
|
|
2
|
+
import { pushReceiver, webhookReceiver, type McpEventOccurrence } from "./mcp-event-delivery.js";
|
|
3
|
+
type JsonMap = Record<string, unknown>;
|
|
4
|
+
export interface McpRequestPeer {
|
|
5
|
+
resolve?(method: string, params: JsonMap, options?: BindingCallOptions): Promise<JsonMap>;
|
|
6
|
+
request(method: string, params?: JsonMap, options?: BindingCallOptions): Promise<JsonMap>;
|
|
7
|
+
}
|
|
8
|
+
/** Discover and verify skill files on their originating peer. The application owns activation. */
|
|
9
|
+
export declare function openMcpSkills(peer: McpRequestPeer, options?: BindingCallOptions): Promise<{
|
|
10
|
+
directoryPage(uri: string, cursor?: string, opts?: BindingCallOptions): Promise<JsonMap & {
|
|
11
|
+
resources: JsonMap[];
|
|
12
|
+
nextCursor?: string;
|
|
13
|
+
}>;
|
|
14
|
+
listPage(cursor?: string, opts?: BindingCallOptions): Promise<JsonMap & {
|
|
15
|
+
skills: JsonMap[];
|
|
16
|
+
nextCursor?: string;
|
|
17
|
+
}>;
|
|
18
|
+
open(uri: string, parseFrontmatter?: (markdown: string) => unknown, opts?: BindingCallOptions & {
|
|
19
|
+
refresh?: boolean;
|
|
20
|
+
}): Promise<{
|
|
21
|
+
entry: JsonMap;
|
|
22
|
+
read(reference?: string, callOptions?: BindingCallOptions): Promise<Uint8Array>;
|
|
23
|
+
}>;
|
|
24
|
+
}>;
|
|
25
|
+
export interface McpPollOptions extends BindingCallOptions {
|
|
26
|
+
maxAgeMs?: number;
|
|
27
|
+
maxEvents?: number;
|
|
28
|
+
}
|
|
29
|
+
export interface McpEventBatch extends JsonMap {
|
|
30
|
+
events: McpEventOccurrence[];
|
|
31
|
+
cursor: string | null;
|
|
32
|
+
hasMore: boolean;
|
|
33
|
+
truncated: boolean;
|
|
34
|
+
nextPollMs: number;
|
|
35
|
+
}
|
|
36
|
+
/** Poll once per explicit request. The application commits its cursor after handling the batch. */
|
|
37
|
+
export declare function openMcpEvents(peer: McpRequestPeer, options?: BindingCallOptions): Promise<{
|
|
38
|
+
listPage(cursor?: string, opts?: BindingCallOptions): Promise<JsonMap & {
|
|
39
|
+
events: JsonMap[];
|
|
40
|
+
nextCursor?: string;
|
|
41
|
+
}>;
|
|
42
|
+
pushReceiver: typeof pushReceiver;
|
|
43
|
+
webhookReceiver: typeof webhookReceiver;
|
|
44
|
+
poller(descriptor: JsonMap, args: JsonMap): (cursor?: string | null, opts?: McpPollOptions) => Promise<McpEventBatch>;
|
|
45
|
+
}>;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { skillFile, defaultSkillFrontmatter } from "./skill-files.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { prepareEvent, eventCursor, pushReceiver, webhookReceiver, } from "./mcp-event-delivery.js";
|
|
5
|
+
import { copyJson, jsonObject } from "./workflow-json.js";
|
|
6
|
+
async function extension(peer, name, opts) {
|
|
7
|
+
const discovery = await peer.request("server/discover", {}, opts);
|
|
8
|
+
const caps = discovery.capabilities;
|
|
9
|
+
if ((Object.hasOwn(discovery, "resultType") &&
|
|
10
|
+
discovery.resultType !== "complete") ||
|
|
11
|
+
!jsonObject(caps) ||
|
|
12
|
+
!jsonObject(caps.extensions) ||
|
|
13
|
+
!jsonObject(caps.extensions["io.modelcontextprotocol/" + name]))
|
|
14
|
+
throw new Error("Server did not advertise the MCP " + name + " extension.");
|
|
15
|
+
return caps;
|
|
16
|
+
}
|
|
17
|
+
function checkedPage(page, key) {
|
|
18
|
+
if ((Object.hasOwn(page, "resultType") && page.resultType !== "complete") ||
|
|
19
|
+
!Array.isArray(page[key]) ||
|
|
20
|
+
(page.nextCursor !== undefined && typeof page.nextCursor !== "string"))
|
|
21
|
+
throw new Error("Invalid MCP extension page.");
|
|
22
|
+
return page[key];
|
|
23
|
+
}
|
|
24
|
+
const skillBytes = 16 * 1024 * 1024;
|
|
25
|
+
function skillManifest(value, requestedUri) {
|
|
26
|
+
if (!jsonObject(value) ||
|
|
27
|
+
value.uri !== requestedUri ||
|
|
28
|
+
!jsonObject(value.frontmatter) ||
|
|
29
|
+
typeof value.frontmatter.name !== "string" ||
|
|
30
|
+
!value.frontmatter.name ||
|
|
31
|
+
typeof value.frontmatter.description !== "string" ||
|
|
32
|
+
!value.frontmatter.description)
|
|
33
|
+
throw new Error("Invalid MCP skill descriptor.");
|
|
34
|
+
if (!Array.isArray(value.resources))
|
|
35
|
+
throw new Error("Verified skill reads require a static manifest.");
|
|
36
|
+
const entry = skillFile(requestedUri);
|
|
37
|
+
if (entry.path.at(-1) !== "SKILL.md" || entry.name !== value.frontmatter.name)
|
|
38
|
+
throw new Error("MCP skill name must match its entry directory.");
|
|
39
|
+
const root = entry.path.slice(0, -1);
|
|
40
|
+
const files = new Map();
|
|
41
|
+
let size = 0;
|
|
42
|
+
for (const file of value.resources) {
|
|
43
|
+
if (!jsonObject(file) ||
|
|
44
|
+
typeof file.uri !== "string" ||
|
|
45
|
+
!file.uri ||
|
|
46
|
+
typeof file.digest !== "string" ||
|
|
47
|
+
!/^sha256:[a-f0-9]{64}$/.test(file.digest) ||
|
|
48
|
+
typeof file.size !== "number" ||
|
|
49
|
+
!Number.isSafeInteger(file.size) ||
|
|
50
|
+
file.size < 0)
|
|
51
|
+
throw new Error("Invalid or duplicate MCP skill manifest entry.");
|
|
52
|
+
size += file.size;
|
|
53
|
+
if (size > skillBytes)
|
|
54
|
+
throw new Error("Skill exceeds the 16 MiB resource limit.");
|
|
55
|
+
const resource = skillFile(file.uri);
|
|
56
|
+
if (resource.origin !== entry.origin ||
|
|
57
|
+
resource.path.length <= root.length ||
|
|
58
|
+
!root.every((part, i) => part === resource.path[i]) ||
|
|
59
|
+
files.has(resource.key))
|
|
60
|
+
throw new Error("Expected distinct manifest files inside the skill directory.");
|
|
61
|
+
files.set(resource.key, {
|
|
62
|
+
uri: file.uri,
|
|
63
|
+
digest: file.digest,
|
|
64
|
+
size: file.size,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (files.get(entry.key)?.uri !== requestedUri)
|
|
68
|
+
throw new Error("Skill manifest omits its entry file.");
|
|
69
|
+
return { files, frontmatter: copyJson(value.frontmatter) };
|
|
70
|
+
}
|
|
71
|
+
/** Discover and verify skill files on their originating peer. The application owns activation. */
|
|
72
|
+
export async function openMcpSkills(peer, options) {
|
|
73
|
+
const caps = await extension(peer, "skills", options);
|
|
74
|
+
if (!jsonObject(caps.resources))
|
|
75
|
+
throw new Error("MCP Skills also requires resources support.");
|
|
76
|
+
const config = caps.extensions["io.modelcontextprotocol/skills"];
|
|
77
|
+
let listed = new Map(), freshUntil = 0;
|
|
78
|
+
return {
|
|
79
|
+
async directoryPage(uri, cursor, opts) {
|
|
80
|
+
if (config.directoryRead !== true)
|
|
81
|
+
throw new Error("Server did not advertise skill directory reads.");
|
|
82
|
+
const result = await peer.request("resources/directory/read", { uri, ...(cursor === undefined ? {} : { cursor }) }, opts);
|
|
83
|
+
checkedPage(result, "resources");
|
|
84
|
+
return result;
|
|
85
|
+
},
|
|
86
|
+
async listPage(cursor, opts) {
|
|
87
|
+
const page = await peer.request("skills/list", cursor === undefined ? {} : { cursor }, opts);
|
|
88
|
+
const next = new Map();
|
|
89
|
+
for (const entry of checkedPage(page, "skills")) {
|
|
90
|
+
if (!jsonObject(entry) ||
|
|
91
|
+
typeof entry.uri !== "string" ||
|
|
92
|
+
next.has(entry.uri))
|
|
93
|
+
throw new Error("Expected distinct skill entry URIs.");
|
|
94
|
+
next.set(entry.uri, copyJson(entry));
|
|
95
|
+
}
|
|
96
|
+
listed = next;
|
|
97
|
+
freshUntil =
|
|
98
|
+
performance.now() +
|
|
99
|
+
(Number.isSafeInteger(page.ttlMs) && Number(page.ttlMs) > 0
|
|
100
|
+
? Number(page.ttlMs)
|
|
101
|
+
: 0);
|
|
102
|
+
return page;
|
|
103
|
+
},
|
|
104
|
+
async open(uri, parseFrontmatter, opts) {
|
|
105
|
+
let entry = !opts?.refresh && performance.now() < freshUntil
|
|
106
|
+
? listed.get(uri)
|
|
107
|
+
: undefined;
|
|
108
|
+
if (!entry) {
|
|
109
|
+
const result = await peer.request("skills/get", { uri }, opts);
|
|
110
|
+
if ((Object.hasOwn(result, "resultType") &&
|
|
111
|
+
result.resultType !== "complete") ||
|
|
112
|
+
!jsonObject(result.skill))
|
|
113
|
+
throw new Error("Expected completed skills/get.");
|
|
114
|
+
entry = result.skill;
|
|
115
|
+
}
|
|
116
|
+
const { files, frontmatter } = skillManifest(entry, uri);
|
|
117
|
+
return {
|
|
118
|
+
entry: copyJson(entry),
|
|
119
|
+
async read(reference = uri, callOptions) {
|
|
120
|
+
const file = files.get(skillFile(reference, uri).key);
|
|
121
|
+
if (!file)
|
|
122
|
+
throw new Error("File is outside the pinned MCP skill manifest.");
|
|
123
|
+
const fileUri = file.uri;
|
|
124
|
+
const result = peer.resolve
|
|
125
|
+
? await peer.resolve("resources/read", { uri: fileUri }, callOptions)
|
|
126
|
+
: await peer.request("resources/read", { uri: fileUri }, callOptions);
|
|
127
|
+
const contents = checkedPage(result, "contents");
|
|
128
|
+
const item = contents[0];
|
|
129
|
+
if (contents.length !== 1 ||
|
|
130
|
+
!jsonObject(item) ||
|
|
131
|
+
item.uri !== fileUri ||
|
|
132
|
+
(typeof item.text === "string") === (typeof item.blob === "string"))
|
|
133
|
+
throw new Error("Expected one matching skill resource.");
|
|
134
|
+
const data = typeof item.text === "string"
|
|
135
|
+
? Buffer.from(item.text)
|
|
136
|
+
: Buffer.from(item.blob, "base64");
|
|
137
|
+
if ((typeof item.blob === "string" &&
|
|
138
|
+
data.toString("base64") !== item.blob) ||
|
|
139
|
+
data.length !== file.size ||
|
|
140
|
+
"sha256:" + createHash("sha256").update(data).digest("hex") !==
|
|
141
|
+
file.digest)
|
|
142
|
+
throw new Error("MCP skill content does not match its pinned manifest.");
|
|
143
|
+
if (fileUri === uri) {
|
|
144
|
+
const actual = copyJson(await (parseFrontmatter ?? defaultSkillFrontmatter)(new TextDecoder("utf-8", { fatal: true }).decode(data)));
|
|
145
|
+
if (!isDeepStrictEqual(actual, frontmatter))
|
|
146
|
+
throw new Error("MCP skill frontmatter changed.");
|
|
147
|
+
}
|
|
148
|
+
return data;
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** Poll once per explicit request. The application commits its cursor after handling the batch. */
|
|
155
|
+
export async function openMcpEvents(peer, options) {
|
|
156
|
+
await extension(peer, "events", options);
|
|
157
|
+
return {
|
|
158
|
+
async listPage(cursor, opts) {
|
|
159
|
+
const page = await peer.request("events/list", cursor === undefined ? {} : { cursor }, opts);
|
|
160
|
+
checkedPage(page, "events");
|
|
161
|
+
return page;
|
|
162
|
+
},
|
|
163
|
+
pushReceiver,
|
|
164
|
+
webhookReceiver,
|
|
165
|
+
poller(descriptor, args) {
|
|
166
|
+
const event = prepareEvent(descriptor, args, "poll");
|
|
167
|
+
return async (cursor = null, opts = {}) => {
|
|
168
|
+
if (cursor !== null && typeof cursor !== "string")
|
|
169
|
+
throw new Error("Expected an opaque event cursor or null.");
|
|
170
|
+
const { maxAgeMs, maxEvents, ...callOptions } = opts;
|
|
171
|
+
if ((maxAgeMs !== undefined &&
|
|
172
|
+
(!Number.isSafeInteger(maxAgeMs) || maxAgeMs < 0)) ||
|
|
173
|
+
(maxEvents !== undefined &&
|
|
174
|
+
(!Number.isSafeInteger(maxEvents) || maxEvents < 1)))
|
|
175
|
+
throw new Error("Invalid MCP event poll limits.");
|
|
176
|
+
const params = {
|
|
177
|
+
...event.params,
|
|
178
|
+
cursor,
|
|
179
|
+
...(maxAgeMs === undefined ? {} : { maxAgeMs }),
|
|
180
|
+
...(maxEvents === undefined ? {} : { maxEvents }),
|
|
181
|
+
};
|
|
182
|
+
const result = await peer.request("events/poll", params, callOptions);
|
|
183
|
+
const events = checkedPage(result, "events");
|
|
184
|
+
if (typeof result.hasMore !== "boolean" ||
|
|
185
|
+
typeof result.truncated !== "boolean" ||
|
|
186
|
+
typeof result.nextPollMs !== "number" ||
|
|
187
|
+
!Number.isSafeInteger(result.nextPollMs) ||
|
|
188
|
+
result.nextPollMs < 0 ||
|
|
189
|
+
(maxEvents !== undefined && events.length > maxEvents))
|
|
190
|
+
throw new Error("Invalid MCP event poll batch.");
|
|
191
|
+
for (const occurrence of events)
|
|
192
|
+
event.occurrence(occurrence);
|
|
193
|
+
return {
|
|
194
|
+
...result,
|
|
195
|
+
cursor: eventCursor(result.cursor),
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type BindingCallOptions } from "./execution.js";
|
|
2
|
+
import type { ToolResultLike } from "./extract.js";
|
|
3
|
+
export declare const MCP_PROTOCOL_VERSION: "2026-07-28";
|
|
4
|
+
type JsonMap = Record<string, unknown>;
|
|
5
|
+
export type McpExchange = (method: string, params: JsonMap, options: BindingCallOptions) => Promise<unknown>;
|
|
6
|
+
export interface McpPeerOptions {
|
|
7
|
+
clientInfo: {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
};
|
|
11
|
+
capabilities?: JsonMap;
|
|
12
|
+
/** The application presents requests and returns the response for each original key. */
|
|
13
|
+
onInputRequired?: (requests: JsonMap, options: BindingCallOptions) => Promise<JsonMap>;
|
|
14
|
+
/** Persist the handle with this endpoint and authenticated principal before polling. */
|
|
15
|
+
onTaskCreated?: (taskId: string) => void | Promise<void>;
|
|
16
|
+
maxInputRounds?: number;
|
|
17
|
+
maxPolls?: number;
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
}
|
|
20
|
+
/** Add the stateless request envelope to an application-owned, authenticated RPC transport.
|
|
21
|
+
* The exchange assigns a fresh JSON-RPC id per invocation and handles wire headers and streams. */
|
|
22
|
+
export declare function createMcpPeer(exchange: McpExchange, options: McpPeerOptions): {
|
|
23
|
+
request: (method: string, params?: JsonMap, opts?: BindingCallOptions) => Promise<Record<string, unknown>>;
|
|
24
|
+
resolve: (method: string, args: JsonMap, opts?: BindingCallOptions) => Promise<JsonMap & ToolResultLike & {
|
|
25
|
+
resultType: "complete";
|
|
26
|
+
}>;
|
|
27
|
+
callTool(tool: {
|
|
28
|
+
name: string;
|
|
29
|
+
arguments: JsonMap;
|
|
30
|
+
_meta?: JsonMap;
|
|
31
|
+
}, opts?: BindingCallOptions): Promise<JsonMap & ToolResultLike & {
|
|
32
|
+
resultType: "complete";
|
|
33
|
+
}>;
|
|
34
|
+
discover: (opts?: BindingCallOptions) => Promise<Record<string, unknown>>;
|
|
35
|
+
catalog: (opts?: BindingCallOptions) => Promise<{
|
|
36
|
+
tools: import("./store.js").ToolListEntry[];
|
|
37
|
+
}>;
|
|
38
|
+
resumeTask: (id: string, opts?: BindingCallOptions) => Promise<JsonMap & ToolResultLike & {
|
|
39
|
+
resultType: "complete";
|
|
40
|
+
}>;
|
|
41
|
+
cancelTask: (id: string, opts?: BindingCallOptions) => Promise<JsonMap & ToolResultLike & {
|
|
42
|
+
resultType: "complete";
|
|
43
|
+
}>;
|
|
44
|
+
};
|
|
45
|
+
export {};
|