@isoloop/webmcp 0.0.0 → 0.1.0-next.11
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/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +164 -0
- package/package.json +32 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sabin Pandelovitch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @isoloop/webmcp
|
|
2
|
+
|
|
3
|
+
**Experimental.** Registers the tools an [Isoloop](https://github.com/isoloop/isoloop) app exposes (`agents.expose`) with the browser's [WebMCP](https://webmachinelearning.github.io/webmcp/) API, so an agent built into the browser can call them.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @isoloop/webmcp@next @isoloop/host@next
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createKernel } from '@isoloop/host';
|
|
11
|
+
import { connectWebMCP } from '@isoloop/webmcp';
|
|
12
|
+
|
|
13
|
+
const kernel = createKernel({ contracts, workers, agents: { expose: ['markets.quote.v1'] } });
|
|
14
|
+
const disconnect = connectWebMCP(kernel);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
What gets registered, the browsers that have WebMCP, the dev MCP for the others, and how to make a topic a tool are in the [agents guide](https://github.com/isoloop/isoloop/blob/main/docs/guide/agents.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Kernel } from "@isoloop/host";
|
|
2
|
+
/**
|
|
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
|
|
8
|
+
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
9
|
+
* and resolves with the reply; a failed call rejects with the kernel's
|
|
10
|
+
* `BusError` (its `code` and `message`), for example `timeout`,
|
|
11
|
+
* `no_responder` or `dropped`. Chromium shows the agent its own generic error; Firefox
|
|
12
|
+
* passes the message on.
|
|
13
|
+
*
|
|
14
|
+
* - Where neither exists (no WebMCP, or the flag off), it does nothing and
|
|
15
|
+
* returns a function that does nothing.
|
|
16
|
+
* - Tools stay registered while their isolates restart.
|
|
17
|
+
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
18
|
+
* through the call's own signal, or a `toolcancel` event, which names only
|
|
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.
|
|
29
|
+
*
|
|
30
|
+
* There is no shim for browsers without WebMCP; during development the dev
|
|
31
|
+
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
32
|
+
* what it does; asking the user before a consequential call is the agent's
|
|
33
|
+
* job.
|
|
34
|
+
*
|
|
35
|
+
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
36
|
+
* `request()` are used).
|
|
37
|
+
* @returns A function that unregisters the tools.
|
|
38
|
+
* @throws {KernelError} `invalid_config` when one of the tools is already
|
|
39
|
+
* registered by an earlier `connectWebMCP` on this page.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { connectWebMCP } from '@isoloop/webmcp';
|
|
43
|
+
*
|
|
44
|
+
* const disconnect = connectWebMCP(kernel);
|
|
45
|
+
* ```
|
|
46
|
+
* @experimental
|
|
47
|
+
*/
|
|
48
|
+
export declare function connectWebMCP(kernel: Pick<Kernel, "tools" | "request">): () => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { KernelError } from "@isoloop/host";
|
|
2
|
+
// src/index.ts
|
|
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");
|
|
7
|
+
if (typeof mc !== "object" || mc === null) return void 0;
|
|
8
|
+
if (typeof Reflect.get(mc, "registerTool") !== "function") return void 0;
|
|
9
|
+
return mc;
|
|
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();
|
|
25
|
+
/** Aborts `target` when any of `signals` aborts; returns the cleanup. */
|
|
26
|
+
function follow(target, signals) {
|
|
27
|
+
const abort = () => target.abort();
|
|
28
|
+
for (const s of signals) if (s.aborted) target.abort();
|
|
29
|
+
else s.addEventListener("abort", abort, { once: true });
|
|
30
|
+
return () => {
|
|
31
|
+
for (const s of signals) s.removeEventListener("abort", abort);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
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
|
|
40
|
+
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
41
|
+
* and resolves with the reply; a failed call rejects with the kernel's
|
|
42
|
+
* `BusError` (its `code` and `message`), for example `timeout`,
|
|
43
|
+
* `no_responder` or `dropped`. Chromium shows the agent its own generic error; Firefox
|
|
44
|
+
* passes the message on.
|
|
45
|
+
*
|
|
46
|
+
* - Where neither exists (no WebMCP, or the flag off), it does nothing and
|
|
47
|
+
* returns a function that does nothing.
|
|
48
|
+
* - Tools stay registered while their isolates restart.
|
|
49
|
+
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
50
|
+
* through the call's own signal, or a `toolcancel` event, which names only
|
|
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.
|
|
61
|
+
*
|
|
62
|
+
* There is no shim for browsers without WebMCP; during development the dev
|
|
63
|
+
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
64
|
+
* what it does; asking the user before a consequential call is the agent's
|
|
65
|
+
* job.
|
|
66
|
+
*
|
|
67
|
+
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
68
|
+
* `request()` are used).
|
|
69
|
+
* @returns A function that unregisters the tools.
|
|
70
|
+
* @throws {KernelError} `invalid_config` when one of the tools is already
|
|
71
|
+
* registered by an earlier `connectWebMCP` on this page.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { connectWebMCP } from '@isoloop/webmcp';
|
|
75
|
+
*
|
|
76
|
+
* const disconnect = connectWebMCP(kernel);
|
|
77
|
+
* ```
|
|
78
|
+
* @experimental
|
|
79
|
+
*/
|
|
80
|
+
function connectWebMCP(kernel) {
|
|
81
|
+
const mc = modelContext();
|
|
82
|
+
if (!mc) return () => {};
|
|
83
|
+
const tools = kernel.tools();
|
|
84
|
+
let names = ours.get(mc);
|
|
85
|
+
if (!names) ours.set(mc, names = /* @__PURE__ */ new Set());
|
|
86
|
+
const taken = names;
|
|
87
|
+
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`);
|
|
88
|
+
const registrations = new AbortController();
|
|
89
|
+
const all = registrations.signal;
|
|
90
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
91
|
+
if (typeof mc.addEventListener === "function") mc.addEventListener("toolcancel", (e) => {
|
|
92
|
+
const name = Reflect.get(e, "toolName");
|
|
93
|
+
if (typeof name !== "string") return;
|
|
94
|
+
for (const call of inFlight.get(name) ?? []) call.abort();
|
|
95
|
+
}, { signal: all });
|
|
96
|
+
const call = async (tool, input, client) => {
|
|
97
|
+
const own = typeof client === "object" && client !== null ? Reflect.get(client, "signal") : void 0;
|
|
98
|
+
const ctl = new AbortController();
|
|
99
|
+
const unfollow = follow(ctl, own instanceof AbortSignal ? [all, own] : [all]);
|
|
100
|
+
let calls = inFlight.get(tool.name);
|
|
101
|
+
if (!calls) inFlight.set(tool.name, calls = /* @__PURE__ */ new Set());
|
|
102
|
+
calls.add(ctl);
|
|
103
|
+
try {
|
|
104
|
+
return await kernel.request(tool.topic, input, {
|
|
105
|
+
timeoutMs: tool.timeoutMs,
|
|
106
|
+
signal: ctl.signal,
|
|
107
|
+
via: "webmcp"
|
|
108
|
+
});
|
|
109
|
+
} finally {
|
|
110
|
+
unfollow();
|
|
111
|
+
calls.delete(ctl);
|
|
112
|
+
if (calls.size === 0) inFlight.delete(tool.name);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const registered = [];
|
|
116
|
+
for (const tool of tools) {
|
|
117
|
+
let result;
|
|
118
|
+
try {
|
|
119
|
+
result = mc.registerTool({
|
|
120
|
+
name: tool.name,
|
|
121
|
+
...tool.title === void 0 ? {} : { title: tool.title },
|
|
122
|
+
description: tool.description,
|
|
123
|
+
inputSchema: tool.inputSchema,
|
|
124
|
+
outputSchema: tool.outputSchema,
|
|
125
|
+
annotations: hints(tool),
|
|
126
|
+
execute: (input, client) => call(tool, input, client)
|
|
127
|
+
}, { signal: all });
|
|
128
|
+
} catch (e) {
|
|
129
|
+
warn(tool.name, e);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
registered.push(tool.name);
|
|
133
|
+
taken.add(tool.name);
|
|
134
|
+
if (result instanceof Promise) result.catch((e) => warn(tool.name, e));
|
|
135
|
+
}
|
|
136
|
+
let disconnected = false;
|
|
137
|
+
return () => {
|
|
138
|
+
if (disconnected) return;
|
|
139
|
+
disconnected = true;
|
|
140
|
+
registrations.abort();
|
|
141
|
+
for (const name of registered) taken.delete(name);
|
|
142
|
+
const unregister = mc.unregisterTool;
|
|
143
|
+
if (typeof unregister !== "function") return;
|
|
144
|
+
for (const name of registered) try {
|
|
145
|
+
Reflect.apply(unregister, mc, [name]);
|
|
146
|
+
} catch {}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The tool's hints, only those that are true: the draft's defaults are false,
|
|
151
|
+
* and Firefox 159 lists a tool as read-only whenever `readOnlyHint` is
|
|
152
|
+
* present, even as `false` (ADR 0018, decision 8, "Browsers that differ").
|
|
153
|
+
*/
|
|
154
|
+
function hints(tool) {
|
|
155
|
+
return {
|
|
156
|
+
...tool.readOnly ? { readOnlyHint: true } : {},
|
|
157
|
+
...tool.consequential ? { consequentialHint: true } : {},
|
|
158
|
+
...tool.untrustedContent ? { untrustedContentHint: true } : {}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function warn(name, e) {
|
|
162
|
+
console.warn(`[isoloop] WebMCP refused the tool ${name}: ${e instanceof Error ? e.message : String(e)}`);
|
|
163
|
+
}
|
|
164
|
+
export { connectWebMCP };
|
package/package.json
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
"
|
|
3
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "@isoloop/webmcp",
|
|
3
|
+
"version": "0.1.0-next.11",
|
|
4
|
+
"description": "Registers an Isoloop app's exposed tools with the browser's WebMCP API (document.modelContext).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/isoloop/isoloop.git",
|
|
11
|
+
"directory": "packages/webmcp"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@isoloop/host": "0.1.0-next.11"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"typescript": "7.0.2"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsdown src/index.ts --platform browser --dts",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
}
|
|
32
|
+
}
|