@isoloop/webmcp 0.0.0 → 0.1.0-next.4
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/dist/index.d.ts +36 -0
- package/dist/index.js +111 -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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Kernel } from "@isoloop/host";
|
|
2
|
+
/**
|
|
3
|
+
* Registers every tool `kernel.tools()` lists with WebMCP's
|
|
4
|
+
* `document.modelContext.registerTool`, with its name, title, description,
|
|
5
|
+
* JSON Schemas and hints (`readOnlyHint`, `consequentialHint`,
|
|
6
|
+
* `untrustedContentHint`). A call runs
|
|
7
|
+
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
8
|
+
* and resolves with the reply; a failed call rejects with the kernel's
|
|
9
|
+
* `BusError` (its `code` and `message`), for example `timeout` or
|
|
10
|
+
* `no_responder`. Browsers show the agent their own generic error.
|
|
11
|
+
*
|
|
12
|
+
* - Where `document.modelContext` is missing (no WebMCP, or the flag off),
|
|
13
|
+
* it does nothing and returns a function that does nothing.
|
|
14
|
+
* - Tools stay registered while their isolates restart.
|
|
15
|
+
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
16
|
+
* 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
|
+
* - The returned function unregisters every tool and aborts the calls still
|
|
19
|
+
* in flight.
|
|
20
|
+
*
|
|
21
|
+
* There is no shim for browsers without WebMCP; during development the dev
|
|
22
|
+
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
23
|
+
* what it does; asking the user before a consequential call is the agent's
|
|
24
|
+
* job.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { connectWebMCP } from '@isoloop/webmcp';
|
|
28
|
+
* const disconnect = connectWebMCP(kernel);
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
32
|
+
* `request()` are used).
|
|
33
|
+
* @returns A function that unregisters the tools.
|
|
34
|
+
* @experimental
|
|
35
|
+
*/
|
|
36
|
+
export declare function connectWebMCP(kernel: Pick<Kernel, "tools" | "request">): () => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
/** `document.modelContext`, or `undefined` where WebMCP is missing or off. */
|
|
3
|
+
function modelContext() {
|
|
4
|
+
if (typeof document === "undefined") return void 0;
|
|
5
|
+
const mc = Reflect.get(document, "modelContext");
|
|
6
|
+
if (typeof mc !== "object" || mc === null) return void 0;
|
|
7
|
+
if (typeof Reflect.get(mc, "registerTool") !== "function") return void 0;
|
|
8
|
+
if (typeof Reflect.get(mc, "addEventListener") !== "function") return void 0;
|
|
9
|
+
return mc;
|
|
10
|
+
}
|
|
11
|
+
/** Aborts `target` when any of `signals` aborts; returns the cleanup. */
|
|
12
|
+
function follow(target, signals) {
|
|
13
|
+
const abort = () => target.abort();
|
|
14
|
+
for (const s of signals) if (s.aborted) target.abort();
|
|
15
|
+
else s.addEventListener("abort", abort, { once: true });
|
|
16
|
+
return () => {
|
|
17
|
+
for (const s of signals) s.removeEventListener("abort", abort);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Registers every tool `kernel.tools()` lists with WebMCP's
|
|
22
|
+
* `document.modelContext.registerTool`, with its name, title, description,
|
|
23
|
+
* JSON Schemas and hints (`readOnlyHint`, `consequentialHint`,
|
|
24
|
+
* `untrustedContentHint`). A call runs
|
|
25
|
+
* `kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
|
|
26
|
+
* and resolves with the reply; a failed call rejects with the kernel's
|
|
27
|
+
* `BusError` (its `code` and `message`), for example `timeout` or
|
|
28
|
+
* `no_responder`. Browsers show the agent their own generic error.
|
|
29
|
+
*
|
|
30
|
+
* - Where `document.modelContext` is missing (no WebMCP, or the flag off),
|
|
31
|
+
* it does nothing and returns a function that does nothing.
|
|
32
|
+
* - Tools stay registered while their isolates restart.
|
|
33
|
+
* - A call is aborted (`BusError('aborted')`) when the browser cancels it:
|
|
34
|
+
* 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
|
+
* - The returned function unregisters every tool and aborts the calls still
|
|
37
|
+
* in flight.
|
|
38
|
+
*
|
|
39
|
+
* There is no shim for browsers without WebMCP; during development the dev
|
|
40
|
+
* MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
|
|
41
|
+
* what it does; asking the user before a consequential call is the agent's
|
|
42
|
+
* job.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { connectWebMCP } from '@isoloop/webmcp';
|
|
46
|
+
* const disconnect = connectWebMCP(kernel);
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param kernel A kernel created with `agents.expose` (only `tools()` and
|
|
50
|
+
* `request()` are used).
|
|
51
|
+
* @returns A function that unregisters the tools.
|
|
52
|
+
* @experimental
|
|
53
|
+
*/
|
|
54
|
+
function connectWebMCP(kernel) {
|
|
55
|
+
const mc = modelContext();
|
|
56
|
+
if (!mc) return () => {};
|
|
57
|
+
const registrations = new AbortController();
|
|
58
|
+
const all = registrations.signal;
|
|
59
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
60
|
+
mc.addEventListener("toolcancel", (e) => {
|
|
61
|
+
const name = Reflect.get(e, "toolName");
|
|
62
|
+
if (typeof name !== "string") return;
|
|
63
|
+
for (const call of inFlight.get(name) ?? []) call.abort();
|
|
64
|
+
}, { signal: all });
|
|
65
|
+
const call = async (tool, input, client) => {
|
|
66
|
+
const own = typeof client === "object" && client !== null ? Reflect.get(client, "signal") : void 0;
|
|
67
|
+
const ctl = new AbortController();
|
|
68
|
+
const unfollow = follow(ctl, own instanceof AbortSignal ? [all, own] : [all]);
|
|
69
|
+
let calls = inFlight.get(tool.name);
|
|
70
|
+
if (!calls) inFlight.set(tool.name, calls = /* @__PURE__ */ new Set());
|
|
71
|
+
calls.add(ctl);
|
|
72
|
+
try {
|
|
73
|
+
return await kernel.request(tool.topic, input, {
|
|
74
|
+
timeoutMs: tool.timeoutMs,
|
|
75
|
+
signal: ctl.signal,
|
|
76
|
+
via: "webmcp"
|
|
77
|
+
});
|
|
78
|
+
} finally {
|
|
79
|
+
unfollow();
|
|
80
|
+
calls.delete(ctl);
|
|
81
|
+
if (calls.size === 0) inFlight.delete(tool.name);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
for (const tool of kernel.tools()) {
|
|
85
|
+
let registered;
|
|
86
|
+
try {
|
|
87
|
+
registered = mc.registerTool({
|
|
88
|
+
name: tool.name,
|
|
89
|
+
...tool.title === void 0 ? {} : { title: tool.title },
|
|
90
|
+
description: tool.description,
|
|
91
|
+
inputSchema: tool.inputSchema,
|
|
92
|
+
outputSchema: tool.outputSchema,
|
|
93
|
+
annotations: {
|
|
94
|
+
readOnlyHint: tool.readOnly,
|
|
95
|
+
consequentialHint: tool.consequential,
|
|
96
|
+
untrustedContentHint: tool.untrustedContent
|
|
97
|
+
},
|
|
98
|
+
execute: (input, client) => call(tool, input, client)
|
|
99
|
+
}, { signal: all });
|
|
100
|
+
} catch (e) {
|
|
101
|
+
warn(tool.name, e);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (registered instanceof Promise) registered.catch((e) => warn(tool.name, e));
|
|
105
|
+
}
|
|
106
|
+
return () => registrations.abort();
|
|
107
|
+
}
|
|
108
|
+
function warn(name, e) {
|
|
109
|
+
console.warn(`[isoloop] WebMCP refused the tool ${name}: ${e instanceof Error ? e.message : String(e)}`);
|
|
110
|
+
}
|
|
111
|
+
export { connectWebMCP };
|
package/package.json
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
"
|
|
3
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "@isoloop/webmcp",
|
|
3
|
+
"version": "0.1.0-next.4",
|
|
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.4"
|
|
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
|
+
}
|