@isoloop/webmcp 0.1.0-next.4 → 0.1.0-next.5
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/index.d.ts +21 -10
- package/dist/index.js +78 -26
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
import { Kernel } from "@isoloop/host";
|
|
2
2
|
/**
|
|
3
|
-
* Registers every tool `kernel.tools()` lists with WebMCP's
|
|
4
|
-
* `document.modelContext
|
|
5
|
-
* JSON Schemas and hints
|
|
6
|
-
* `
|
|
3
|
+
* Registers every tool `kernel.tools()` lists with WebMCP's `registerTool`
|
|
4
|
+
* (on `document.modelContext`, or on `navigator.modelContext` where only
|
|
5
|
+
* that exists), with its name, title, description, JSON Schemas and hints
|
|
6
|
+
* (`readOnlyHint`, `consequentialHint`, `untrustedContentHint`, each sent
|
|
7
|
+
* only when true). A call runs
|
|
7
8
|
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
8
9
|
* and resolves with the reply; a failed call rejects with the kernel's
|
|
9
10
|
* `BusError` (its `code` and `message`), for example `timeout` or
|
|
10
|
-
* `no_responder`.
|
|
11
|
+
* `no_responder`. Chromium shows the agent its own generic error; Firefox
|
|
12
|
+
* passes the message on.
|
|
11
13
|
*
|
|
12
|
-
* - Where
|
|
13
|
-
*
|
|
14
|
+
* - Where neither exists (no WebMCP, or the flag off), it does nothing and
|
|
15
|
+
* returns a function that does nothing.
|
|
14
16
|
* - Tools stay registered while their isolates restart.
|
|
15
17
|
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
16
18
|
* through the call's own signal, or a `toolcancel` event, which names only
|
|
17
|
-
* the tool, so it aborts every call of that tool in flight.
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* the tool, so it aborts every call of that tool in flight. Where the
|
|
20
|
+
* browser gives neither (Firefox 159), a call runs until its reply or its
|
|
21
|
+
* `timeoutMs`.
|
|
22
|
+
* - A tool name it already registered on this page and hasn't unregistered
|
|
23
|
+
* (a second `connectWebMCP` before the first disconnects) throws
|
|
24
|
+
* `KernelError('invalid_config')` naming the tool, and nothing is
|
|
25
|
+
* registered. Names other scripts registered aren't seen.
|
|
26
|
+
* - The returned function unregisters every tool (it aborts the registration
|
|
27
|
+
* signal, and calls `unregisterTool` where that exists) and aborts the
|
|
28
|
+
* calls still in flight.
|
|
20
29
|
*
|
|
21
30
|
* There is no shim for browsers without WebMCP; during development the dev
|
|
22
31
|
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
@@ -31,6 +40,8 @@ import { Kernel } from "@isoloop/host";
|
|
|
31
40
|
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
32
41
|
* `request()` are used).
|
|
33
42
|
* @returns A function that unregisters the tools.
|
|
43
|
+
* @throws {KernelError} `invalid_config` when one of the tools is already
|
|
44
|
+
* registered by an earlier `connectWebMCP` on this page.
|
|
34
45
|
* @experimental
|
|
35
46
|
*/
|
|
36
47
|
export declare function connectWebMCP(kernel: Pick<Kernel, "tools" | "request">): () => void;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
|
+
import { KernelError } from "@isoloop/host";
|
|
1
2
|
// src/index.ts
|
|
2
|
-
/** `
|
|
3
|
-
function
|
|
4
|
-
if (
|
|
5
|
-
const mc = Reflect.get(
|
|
3
|
+
/** `owner.modelContext` if it's an object with a `registerTool` method. */
|
|
4
|
+
function contextOn(owner) {
|
|
5
|
+
if (owner === void 0) return void 0;
|
|
6
|
+
const mc = Reflect.get(owner, "modelContext");
|
|
6
7
|
if (typeof mc !== "object" || mc === null) return void 0;
|
|
7
8
|
if (typeof Reflect.get(mc, "registerTool") !== "function") return void 0;
|
|
8
|
-
if (typeof Reflect.get(mc, "addEventListener") !== "function") return void 0;
|
|
9
9
|
return mc;
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* `document.modelContext` (the draft, Chromium), else
|
|
13
|
+
* `navigator.modelContext` (Firefox 159), or `undefined` where WebMCP is
|
|
14
|
+
* missing or off.
|
|
15
|
+
*/
|
|
16
|
+
function modelContext() {
|
|
17
|
+
return contextOn(typeof document === "undefined" ? void 0 : document) ?? contextOn(typeof navigator === "undefined" ? void 0 : navigator);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The names `connectWebMCP` registered on each `ModelContext`, so a duplicate
|
|
21
|
+
* fails the same way in every browser: Chromium rejects it, but Firefox 159
|
|
22
|
+
* silently replaces the tool (ADR 0018, decision 8, "Browsers that differ").
|
|
23
|
+
*/
|
|
24
|
+
const ours = /* @__PURE__ */ new WeakMap();
|
|
11
25
|
/** Aborts `target` when any of `signals` aborts; returns the cleanup. */
|
|
12
26
|
function follow(target, signals) {
|
|
13
27
|
const abort = () => target.abort();
|
|
@@ -18,23 +32,32 @@ function follow(target, signals) {
|
|
|
18
32
|
};
|
|
19
33
|
}
|
|
20
34
|
/**
|
|
21
|
-
* Registers every tool `kernel.tools()` lists with WebMCP's
|
|
22
|
-
* `document.modelContext
|
|
23
|
-
* JSON Schemas and hints
|
|
24
|
-
* `
|
|
35
|
+
* Registers every tool `kernel.tools()` lists with WebMCP's `registerTool`
|
|
36
|
+
* (on `document.modelContext`, or on `navigator.modelContext` where only
|
|
37
|
+
* that exists), with its name, title, description, JSON Schemas and hints
|
|
38
|
+
* (`readOnlyHint`, `consequentialHint`, `untrustedContentHint`, each sent
|
|
39
|
+
* only when true). A call runs
|
|
25
40
|
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
26
41
|
* and resolves with the reply; a failed call rejects with the kernel's
|
|
27
42
|
* `BusError` (its `code` and `message`), for example `timeout` or
|
|
28
|
-
* `no_responder`.
|
|
43
|
+
* `no_responder`. Chromium shows the agent its own generic error; Firefox
|
|
44
|
+
* passes the message on.
|
|
29
45
|
*
|
|
30
|
-
* - Where
|
|
31
|
-
*
|
|
46
|
+
* - Where neither exists (no WebMCP, or the flag off), it does nothing and
|
|
47
|
+
* returns a function that does nothing.
|
|
32
48
|
* - Tools stay registered while their isolates restart.
|
|
33
49
|
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
34
50
|
* through the call's own signal, or a `toolcancel` event, which names only
|
|
35
|
-
* the tool, so it aborts every call of that tool in flight.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
51
|
+
* the tool, so it aborts every call of that tool in flight. Where the
|
|
52
|
+
* browser gives neither (Firefox 159), a call runs until its reply or its
|
|
53
|
+
* `timeoutMs`.
|
|
54
|
+
* - A tool name it already registered on this page and hasn't unregistered
|
|
55
|
+
* (a second `connectWebMCP` before the first disconnects) throws
|
|
56
|
+
* `KernelError('invalid_config')` naming the tool, and nothing is
|
|
57
|
+
* registered. Names other scripts registered aren't seen.
|
|
58
|
+
* - The returned function unregisters every tool (it aborts the registration
|
|
59
|
+
* signal, and calls `unregisterTool` where that exists) and aborts the
|
|
60
|
+
* calls still in flight.
|
|
38
61
|
*
|
|
39
62
|
* There is no shim for browsers without WebMCP; during development the dev
|
|
40
63
|
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
@@ -49,15 +72,22 @@ function follow(target, signals) {
|
|
|
49
72
|
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
50
73
|
* `request()` are used).
|
|
51
74
|
* @returns A function that unregisters the tools.
|
|
75
|
+
* @throws {KernelError} `invalid_config` when one of the tools is already
|
|
76
|
+
* registered by an earlier `connectWebMCP` on this page.
|
|
52
77
|
* @experimental
|
|
53
78
|
*/
|
|
54
79
|
function connectWebMCP(kernel) {
|
|
55
80
|
const mc = modelContext();
|
|
56
81
|
if (!mc) return () => {};
|
|
82
|
+
const tools = kernel.tools();
|
|
83
|
+
let names = ours.get(mc);
|
|
84
|
+
if (!names) ours.set(mc, names = /* @__PURE__ */ new Set());
|
|
85
|
+
const taken = names;
|
|
86
|
+
for (const tool of tools) if (taken.has(tool.name)) throw new KernelError("invalid_config", `connectWebMCP: the tool ${tool.name} is already registered with WebMCP on this page`);
|
|
57
87
|
const registrations = new AbortController();
|
|
58
88
|
const all = registrations.signal;
|
|
59
89
|
const inFlight = /* @__PURE__ */ new Map();
|
|
60
|
-
mc.addEventListener("toolcancel", (e) => {
|
|
90
|
+
if (typeof mc.addEventListener === "function") mc.addEventListener("toolcancel", (e) => {
|
|
61
91
|
const name = Reflect.get(e, "toolName");
|
|
62
92
|
if (typeof name !== "string") return;
|
|
63
93
|
for (const call of inFlight.get(name) ?? []) call.abort();
|
|
@@ -81,29 +111,51 @@ function connectWebMCP(kernel) {
|
|
|
81
111
|
if (calls.size === 0) inFlight.delete(tool.name);
|
|
82
112
|
}
|
|
83
113
|
};
|
|
84
|
-
|
|
85
|
-
|
|
114
|
+
const registered = [];
|
|
115
|
+
for (const tool of tools) {
|
|
116
|
+
let result;
|
|
86
117
|
try {
|
|
87
|
-
|
|
118
|
+
result = mc.registerTool({
|
|
88
119
|
name: tool.name,
|
|
89
120
|
...tool.title === void 0 ? {} : { title: tool.title },
|
|
90
121
|
description: tool.description,
|
|
91
122
|
inputSchema: tool.inputSchema,
|
|
92
123
|
outputSchema: tool.outputSchema,
|
|
93
|
-
annotations:
|
|
94
|
-
readOnlyHint: tool.readOnly,
|
|
95
|
-
consequentialHint: tool.consequential,
|
|
96
|
-
untrustedContentHint: tool.untrustedContent
|
|
97
|
-
},
|
|
124
|
+
annotations: hints(tool),
|
|
98
125
|
execute: (input, client) => call(tool, input, client)
|
|
99
126
|
}, { signal: all });
|
|
100
127
|
} catch (e) {
|
|
101
128
|
warn(tool.name, e);
|
|
102
129
|
continue;
|
|
103
130
|
}
|
|
104
|
-
|
|
131
|
+
registered.push(tool.name);
|
|
132
|
+
taken.add(tool.name);
|
|
133
|
+
if (result instanceof Promise) result.catch((e) => warn(tool.name, e));
|
|
105
134
|
}
|
|
106
|
-
|
|
135
|
+
let disconnected = false;
|
|
136
|
+
return () => {
|
|
137
|
+
if (disconnected) return;
|
|
138
|
+
disconnected = true;
|
|
139
|
+
registrations.abort();
|
|
140
|
+
for (const name of registered) taken.delete(name);
|
|
141
|
+
const unregister = mc.unregisterTool;
|
|
142
|
+
if (typeof unregister !== "function") return;
|
|
143
|
+
for (const name of registered) try {
|
|
144
|
+
Reflect.apply(unregister, mc, [name]);
|
|
145
|
+
} catch {}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The tool's hints, only those that are true: the draft's defaults are false,
|
|
150
|
+
* and Firefox 159 lists a tool as read-only whenever `readOnlyHint` is
|
|
151
|
+
* present, even as `false` (ADR 0018, decision 8, "Browsers that differ").
|
|
152
|
+
*/
|
|
153
|
+
function hints(tool) {
|
|
154
|
+
return {
|
|
155
|
+
...tool.readOnly ? { readOnlyHint: true } : {},
|
|
156
|
+
...tool.consequential ? { consequentialHint: true } : {},
|
|
157
|
+
...tool.untrustedContent ? { untrustedContentHint: true } : {}
|
|
158
|
+
};
|
|
107
159
|
}
|
|
108
160
|
function warn(name, e) {
|
|
109
161
|
console.warn(`[isoloop] WebMCP refused the tool ${name}: ${e instanceof Error ? e.message : String(e)}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/webmcp",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.5",
|
|
4
4
|
"description": "Registers an Isoloop app's exposed tools with the browser's WebMCP API (document.modelContext).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@isoloop/host": "0.1.0-next.
|
|
23
|
+
"@isoloop/host": "0.1.0-next.5"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"typescript": "7.0.2"
|