@panyam/mcpkit-rooms 0.0.0-stage → 0.8.6
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 +104 -2
- package/dist/app.d.ts +22 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +21 -0
- package/dist/app.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/room.d.ts +138 -0
- package/dist/room.d.ts.map +1 -0
- package/dist/room.js +362 -0
- package/dist/room.js.map +1 -0
- package/dist/tool.d.ts +55 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +17 -0
- package/dist/tool.js.map +1 -0
- package/package.json +44 -4
package/README.md
CHANGED
|
@@ -1,3 +1,105 @@
|
|
|
1
|
-
#
|
|
1
|
+
# experimental/ext/rooms/ts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The page side of the room bridge (`experimental/ext/rooms`), for browsers. A page joins the relay
|
|
4
|
+
room for what it shows and offers tools there. An agent attached to the room through the bridge
|
|
5
|
+
calls them over MCP, and every call goes to one page. Pre-SEP and experimental, like the bridge. The
|
|
6
|
+
design is in issue [#1581](https://github.com/panyam/mcpkit/issues/1581).
|
|
7
|
+
|
|
8
|
+
It speaks to a [massrelay](https://github.com/panyam/massrelay) relay (v0.2.0 or later) and depends
|
|
9
|
+
on `@panyam/massrelay` (0.2.4 or later, whose `CollabClient` it joins through) and
|
|
10
|
+
`@panyam/servicekit-client` (0.1.9 or later).
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
npm install @panyam/mcpkit-rooms
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
It's published at mcpkit's release version, so `@panyam/mcpkit-rooms@X.Y.Z` is the page side of the
|
|
17
|
+
bridge that the Go modules carry at `vX.Y.Z`. Use the same version on both sides.
|
|
18
|
+
|
|
19
|
+
## Joining a room
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { joinToolRoom } from "@panyam/mcpkit-rooms";
|
|
23
|
+
|
|
24
|
+
const room = await joinToolRoom({
|
|
25
|
+
relay: "https://relay.example.com",
|
|
26
|
+
room: "design:demo/board.kicad_sch",
|
|
27
|
+
token: relayToken, // minted by your app for its signed-in user
|
|
28
|
+
});
|
|
29
|
+
room.registerTool({
|
|
30
|
+
name: "show_sheet",
|
|
31
|
+
description: "Open a sheet of the design on screen",
|
|
32
|
+
inputSchema: { type: "object", properties: { sheet: { type: "string" } }, required: ["sheet"] },
|
|
33
|
+
async execute({ sheet }, { signal }) {
|
|
34
|
+
await viewer.openSheet(String(sheet), { signal });
|
|
35
|
+
return `showing ${sheet}`;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
room.setLabel("board.kicad_sch");
|
|
39
|
+
room.setAgentsAllowed(true); // "let my agents drive this tab", off by default
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The tool shape is WebMCP's `registerTool` descriptor, so a tool written for WebMCP registers here
|
|
43
|
+
unchanged. `execute` may return a full MCP `CallToolResult`, a string, or any JSON value, and a
|
|
44
|
+
thrown error becomes an `isError` result. Its second argument carries:
|
|
45
|
+
|
|
46
|
+
- `signal`, aborted when the agent cancels or the page leaves. MCP sends no reply for a cancelled call.
|
|
47
|
+
- `progress(done, total?, message?)`, which reaches the agent under its own progress token.
|
|
48
|
+
- `from`, the bridge's peer id.
|
|
49
|
+
|
|
50
|
+
`annotations.readOnlyHint: true` lets an agent holding only `pages:read` call the tool. Anything
|
|
51
|
+
else needs `pages:act`. A tool may not declare an input property named `page`, which the bridge
|
|
52
|
+
adds so an agent can pick a page.
|
|
53
|
+
|
|
54
|
+
## Who may call
|
|
55
|
+
|
|
56
|
+
The relay validates `token` and reports its `sub` as the page's user. The bridge lets a session call
|
|
57
|
+
the page only when the page agreed:
|
|
58
|
+
|
|
59
|
+
- `setAgentsAllowed(true)` agrees to every agent session acting for the page's own user.
|
|
60
|
+
- `acceptPairing(code)` agrees to the session that got `code` back from `attach`, typically another
|
|
61
|
+
user's. `onPairingRequest(fn)` reports who is asking, so the page can show its user.
|
|
62
|
+
- `offerPairingCode({ ttlMs? })` makes up a code for the page to show; the user gives it to the
|
|
63
|
+
agent, which enters it with the bridge's `pair` tool. `onPaired(fn)` reports which agent it was,
|
|
64
|
+
and the code is withdrawn once used. Two minutes by default, at most ten.
|
|
65
|
+
- `revoke(session?)` ends one pairing, or all of them when called with no argument.
|
|
66
|
+
|
|
67
|
+
## Telling agents what happened
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
room.ask("why is SDA flagged?"); // room.ask
|
|
71
|
+
room.selectionChanged({ net: "SDA" }); // page.selection_changed
|
|
72
|
+
room.emit("viewer.sheet_opened", { sheet }); // a name of your own
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Agents subscribed to the room's events hear these, if this page agreed to their session (the same
|
|
76
|
+
agreement as calls). Names are one to four dot-separated lowercase segments (`validEventName`), and
|
|
77
|
+
`data` is capped at `MAX_EVENT_DATA_BYTES` (16 KiB). `emit` throws on either before anything is sent.
|
|
78
|
+
An event goes only to bridges that have already listed the page, so one emitted straight after
|
|
79
|
+
`joinToolRoom` may reach nobody. Agents whose host speaks no events hear the same events through the
|
|
80
|
+
bridge's `next_request` tool or its `rooms://events/{resource}` resource, when the bridge enables
|
|
81
|
+
them (`rooms.WithEventFallbacks`).
|
|
82
|
+
|
|
83
|
+
## The same tool in an MCP Apps frame
|
|
84
|
+
|
|
85
|
+
`registerOnApp(app, tool)` registers a tool with an MCP Apps bridge, such as mcpkit's
|
|
86
|
+
`window.MCPApp` from ext/ui, so one tool object serves a host that frames the app and a room an agent
|
|
87
|
+
attaches to:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
registerOnApp(window.MCPApp, showSheet);
|
|
91
|
+
(await joinToolRoom({ relay, room, token })).registerTool(showSheet);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`modelContext: true` also registers each tool with the browser's own WebMCP context
|
|
95
|
+
(`navigator.modelContext`, then `document.modelContext`), when one is there.
|
|
96
|
+
|
|
97
|
+
## Developing
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
pnpm install
|
|
101
|
+
pnpm check # tsc
|
|
102
|
+
pnpm test # needs Go: the tests start the Go bridge on a real relay
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`make test-experimental-rooms-ts` runs the same from the repo root, and CI runs it in the `test-rooms` job.
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type PageTool } from "./tool.js";
|
|
2
|
+
/**
|
|
3
|
+
* The part of an MCP Apps bridge `registerOnApp` needs: mcpkit's
|
|
4
|
+
* `window.MCPApp` (ext/ui's mcp-app-bridge) has this shape.
|
|
5
|
+
*/
|
|
6
|
+
export interface AppToolRegistry {
|
|
7
|
+
registerTool(name: string, config: {
|
|
8
|
+
description?: string;
|
|
9
|
+
inputSchema?: unknown;
|
|
10
|
+
}, handler: (args: Record<string, unknown>) => unknown | Promise<unknown>): unknown;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Registers a PageTool with an MCP Apps bridge, so the same tool object an
|
|
14
|
+
* app frame offers its host can be offered to a room with `joinToolRoom`.
|
|
15
|
+
* Returns whatever the bridge's `registerTool` returns (ext/ui's handle has
|
|
16
|
+
* `remove()`).
|
|
17
|
+
*
|
|
18
|
+
* The app bridge has no per-call cancellation, so `context.signal` is never
|
|
19
|
+
* aborted here.
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerOnApp(app: AppToolRegistry, tool: PageTool): unknown;
|
|
22
|
+
//# sourceMappingURL=app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEzE;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,YAAY,CACV,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE,EACvD,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,GACrE,OAAO,CAAC;CACZ;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAY3E"}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { errorResult, toCallToolResult } from "./tool.js";
|
|
2
|
+
/**
|
|
3
|
+
* Registers a PageTool with an MCP Apps bridge, so the same tool object an
|
|
4
|
+
* app frame offers its host can be offered to a room with `joinToolRoom`.
|
|
5
|
+
* Returns whatever the bridge's `registerTool` returns (ext/ui's handle has
|
|
6
|
+
* `remove()`).
|
|
7
|
+
*
|
|
8
|
+
* The app bridge has no per-call cancellation, so `context.signal` is never
|
|
9
|
+
* aborted here.
|
|
10
|
+
*/
|
|
11
|
+
export function registerOnApp(app, tool) {
|
|
12
|
+
return app.registerTool(tool.name, { description: tool.description, inputSchema: tool.inputSchema ?? { type: "object" } }, async (args) => {
|
|
13
|
+
try {
|
|
14
|
+
return toCallToolResult(await tool.execute(args ?? {}, { signal: new AbortController().signal, progress() { }, from: "app-host" }));
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
return errorResult(err);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=app.js.map
|
package/dist/app.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAiB,MAAM,WAAW,CAAC;AAczE;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,GAAoB,EAAE,IAAc;IAChE,OAAO,GAAG,CAAC,YAAY,CACrB,IAAI,CAAC,IAAI,EACT,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EACtF,KAAK,EAAE,IAAI,EAAE,EAAE;QACb,IAAI,CAAC;YACH,OAAO,gBAAgB,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,eAAe,EAAE,CAAC,MAAM,EAAE,QAAQ,KAAI,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;QACrI,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { joinToolRoom, wsUrl, MESSAGE_KIND, validEventName, EVENT_ASK, EVENT_SELECTION_CHANGED, MAX_EVENT_DATA_BYTES, DEFAULT_PAIRING_CODE_TTL_MS, } from "./room.js";
|
|
2
|
+
export type { JoinToolRoomOptions, ToolRoom, PairingRequest, PairedByCode, ModelContextLike } from "./room.js";
|
|
3
|
+
export { registerOnApp } from "./app.js";
|
|
4
|
+
export type { AppToolRegistry } from "./app.js";
|
|
5
|
+
export { toCallToolResult } from "./tool.js";
|
|
6
|
+
export type { PageTool, ToolCallContext, CallToolResult } from "./tool.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,cAAc,EACd,SAAS,EACT,uBAAuB,EACvB,oBAAoB,EACpB,2BAA2B,GAC5B,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,mBAAmB,EAAE,QAAQ,EAAE,cAAc,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC/G,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,YAAY,EAAE,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { joinToolRoom, wsUrl, MESSAGE_KIND, validEventName, EVENT_ASK, EVENT_SELECTION_CHANGED, MAX_EVENT_DATA_BYTES, DEFAULT_PAIRING_CODE_TTL_MS, } from "./room.js";
|
|
2
|
+
export { registerOnApp } from "./app.js";
|
|
3
|
+
export { toCallToolResult } from "./tool.js";
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,cAAc,EACd,SAAS,EACT,uBAAuB,EACvB,oBAAoB,EACpB,2BAA2B,GAC5B,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/room.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type PageTool } from "./tool.js";
|
|
2
|
+
/** The AppMessage kind that carries MCP JSON-RPC between a page and the bridge. */
|
|
3
|
+
export declare const MESSAGE_KIND = "mcp/1";
|
|
4
|
+
/** How long an offered pairing code stays valid by default, matching `rooms.DefaultPairingCodeTTL`. */
|
|
5
|
+
export declare const DEFAULT_PAIRING_CODE_TTL_MS: number;
|
|
6
|
+
/** Event name `ask()` emits: the page's user asked the agents something. */
|
|
7
|
+
export declare const EVENT_ASK = "room.ask";
|
|
8
|
+
/** Event name `selectionChanged()` emits. */
|
|
9
|
+
export declare const EVENT_SELECTION_CHANGED = "page.selection_changed";
|
|
10
|
+
/** Largest encoded `data` one event may carry, matching the bridge's cap. */
|
|
11
|
+
export declare const MAX_EVENT_DATA_BYTES: number;
|
|
12
|
+
/**
|
|
13
|
+
* Reports whether `name` is one a page may emit: one to four dot-separated
|
|
14
|
+
* lowercase segments, each starting with a letter, at most 64 characters.
|
|
15
|
+
* Mirrors `rooms.ValidEventName`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function validEventName(name: string): boolean;
|
|
18
|
+
/** A session asking to drive the page, as the bridge describes it. */
|
|
19
|
+
export interface PairingRequest {
|
|
20
|
+
session: string;
|
|
21
|
+
/** The session's subject, from its validated token. */
|
|
22
|
+
subject: string;
|
|
23
|
+
/** The agent client that asked, when it said. */
|
|
24
|
+
client?: string;
|
|
25
|
+
/** True when it is the page's own user, so the agents switch is enough. */
|
|
26
|
+
sameSubject: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** A session that paired with the page by entering the code it offered. */
|
|
29
|
+
export interface PairedByCode extends PairingRequest {
|
|
30
|
+
/** The code it entered, now spent. */
|
|
31
|
+
code: string;
|
|
32
|
+
}
|
|
33
|
+
/** Where `registerTool` also registers when `modelContext` is set. */
|
|
34
|
+
export interface ModelContextLike {
|
|
35
|
+
registerTool(tool: PageTool): unknown;
|
|
36
|
+
}
|
|
37
|
+
/** Options for {@link joinToolRoom}. */
|
|
38
|
+
export interface JoinToolRoomOptions {
|
|
39
|
+
/** The relay's base URL: http(s) or ws(s), with any path it is mounted under. */
|
|
40
|
+
relay: string;
|
|
41
|
+
/** The room, which is the resource the page shows, e.g. `design:demo/board.kicad_sch`. */
|
|
42
|
+
room: string;
|
|
43
|
+
/**
|
|
44
|
+
* The relay token the app minted for its user. The relay validates it and
|
|
45
|
+
* reports its `sub` as the page's user, which is what pairing compares.
|
|
46
|
+
* Sent as the `token` query parameter, since a browser WebSocket cannot set
|
|
47
|
+
* headers.
|
|
48
|
+
*/
|
|
49
|
+
token?: string;
|
|
50
|
+
/** The name other peers see. Pairing never reads it. */
|
|
51
|
+
username?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Also register each tool with the browser's own WebMCP context, so an
|
|
54
|
+
* agent built into the browser reaches the same handlers. `true` looks for
|
|
55
|
+
* `navigator.modelContext`, then `document.modelContext`; or pass the
|
|
56
|
+
* object. Missing is not an error.
|
|
57
|
+
*/
|
|
58
|
+
modelContext?: boolean | ModelContextLike;
|
|
59
|
+
/** A WebSocket implementation, for runtimes without a global one. */
|
|
60
|
+
WebSocket?: typeof WebSocket;
|
|
61
|
+
/** How long to wait for the relay to admit the page. Default 10s. */
|
|
62
|
+
joinTimeoutMs?: number;
|
|
63
|
+
}
|
|
64
|
+
/** A page's membership in a tool room. */
|
|
65
|
+
export interface ToolRoom {
|
|
66
|
+
/** The page's peer id, which agents pass as a tool's `page` argument. */
|
|
67
|
+
readonly id: string;
|
|
68
|
+
/** The room joined. */
|
|
69
|
+
readonly room: string;
|
|
70
|
+
/** Resolves when the page has left, by `leave()` or the relay dropping it. */
|
|
71
|
+
readonly closed: Promise<void>;
|
|
72
|
+
/** Adds or replaces a tool and tells the bridges the list changed. */
|
|
73
|
+
registerTool(tool: PageTool): {
|
|
74
|
+
unregister(): void;
|
|
75
|
+
};
|
|
76
|
+
/** Removes a tool and tells the bridges the list changed. */
|
|
77
|
+
unregisterTool(name: string): void;
|
|
78
|
+
/** Sets what `list_pages` shows for this page. */
|
|
79
|
+
setLabel(label: string): void;
|
|
80
|
+
/** Reports that the page's user is looking at it now. */
|
|
81
|
+
focus(): void;
|
|
82
|
+
/**
|
|
83
|
+
* The "let my agents drive this tab" switch, off by default. On pairs the
|
|
84
|
+
* page with every agent session acting for the page's own user.
|
|
85
|
+
*/
|
|
86
|
+
setAgentsAllowed(allowed: boolean): void;
|
|
87
|
+
/** Accepts the code an agent session got back from `attach`. */
|
|
88
|
+
acceptPairing(code: string): void;
|
|
89
|
+
/** Ends the pairing with one session, or with all of them when omitted. */
|
|
90
|
+
revoke(session?: string): void;
|
|
91
|
+
/** Called when a session asks to drive the page. */
|
|
92
|
+
onPairingRequest(fn: (req: PairingRequest) => void): void;
|
|
93
|
+
/**
|
|
94
|
+
* Makes up a code for the page's user to give an agent, which enters it
|
|
95
|
+
* with the bridge's `pair` tool, and offers it to the bridges. Replaces any
|
|
96
|
+
* code offered before. Single-use: once a session pairs with it the page
|
|
97
|
+
* withdraws it and `onPaired` runs. Bridges honour at most ten minutes.
|
|
98
|
+
*/
|
|
99
|
+
offerPairingCode(opts?: {
|
|
100
|
+
ttlMs?: number;
|
|
101
|
+
}): string;
|
|
102
|
+
/** Withdraws the code `offerPairingCode` made, if any. */
|
|
103
|
+
withdrawPairingCode(): void;
|
|
104
|
+
/** Called when a session pairs by the offered code: where the page shows its user which agent it was. */
|
|
105
|
+
onPaired(fn: (paired: PairedByCode) => void): void;
|
|
106
|
+
/**
|
|
107
|
+
* Tells the bridges something happened on the page. Agents subscribed to
|
|
108
|
+
* the room's events hear it, if this page agreed to their session. Throws
|
|
109
|
+
* on a name `validEventName` refuses or data over `MAX_EVENT_DATA_BYTES`.
|
|
110
|
+
* Bridges hear only once they have greeted the page, so an event emitted
|
|
111
|
+
* before any bridge listed its tools goes nowhere.
|
|
112
|
+
*/
|
|
113
|
+
emit(name: string, data?: unknown): void;
|
|
114
|
+
/** Emits `room.ask`: the page's user asked the agents `text`. */
|
|
115
|
+
ask(text: string): void;
|
|
116
|
+
/** Emits `page.selection_changed` with whatever the page counts as its selection. */
|
|
117
|
+
selectionChanged(selection: unknown): void;
|
|
118
|
+
/** Leaves the room. Calls in flight are aborted. */
|
|
119
|
+
leave(): void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Joins a room on a massrelay relay as a page that offers tools to agents
|
|
123
|
+
* through the mcpkit room bridge. Resolves once the relay admits the page,
|
|
124
|
+
* and rejects with the relay's code (`ROOM_FULL`, `ROOM_FORBIDDEN`, ...) or
|
|
125
|
+
* the socket's failure otherwise.
|
|
126
|
+
*
|
|
127
|
+
* The page answers `tools/list` and `tools/call` from any bridge in the room,
|
|
128
|
+
* sends `notifications/tools/list_changed` when its tools change, and aborts a
|
|
129
|
+
* call's `signal` on `notifications/cancelled`. It reads only messages
|
|
130
|
+
* addressed to it, never room-wide ones.
|
|
131
|
+
*/
|
|
132
|
+
export declare function joinToolRoom(opts: JoinToolRoomOptions): Promise<ToolRoom>;
|
|
133
|
+
/**
|
|
134
|
+
* The relay's WebSocket URL for a room. The room is one path segment, so a
|
|
135
|
+
* `/` in it is escaped.
|
|
136
|
+
*/
|
|
137
|
+
export declare function wsUrl(relay: string, room: string, token?: string): string;
|
|
138
|
+
//# sourceMappingURL=room.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"room.d.ts","sourceRoot":"","sources":["../src/room.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiC,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEzE,mFAAmF;AACnF,eAAO,MAAM,YAAY,UAAU,CAAC;AAYpC,uGAAuG;AACvG,eAAO,MAAM,2BAA2B,QAAgB,CAAC;AAIzD,4EAA4E;AAC5E,eAAO,MAAM,SAAS,aAAa,CAAC;AACpC,6CAA6C;AAC7C,eAAO,MAAM,uBAAuB,2BAA2B,CAAC;AAChE,6EAA6E;AAC7E,eAAO,MAAM,oBAAoB,QAAW,CAAC;AAG7C;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD;AAID,sEAAsE;AACtE,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAA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} from "@panyam/massrelay";
|
|
2
|
+
import { GRPCWSClient } from "@panyam/servicekit-client";
|
|
3
|
+
import { errorResult, toCallToolResult } from "./tool.js";
|
|
4
|
+
/** The AppMessage kind that carries MCP JSON-RPC between a page and the bridge. */
|
|
5
|
+
export const MESSAGE_KIND = "mcp/1";
|
|
6
|
+
// Methods beyond MCP's own, mirroring experimental/ext/rooms/wire.go.
|
|
7
|
+
const PAGE_STATE = "notifications/rooms/page_state";
|
|
8
|
+
const AGENTS_ALLOWED = "notifications/rooms/agents_allowed";
|
|
9
|
+
const ACCEPT_PAIRING = "notifications/rooms/accept_pairing";
|
|
10
|
+
const REVOKE = "notifications/rooms/revoke";
|
|
11
|
+
const PAIRING_REQUESTED = "notifications/rooms/pairing_requested";
|
|
12
|
+
const PAGE_EVENT = "notifications/rooms/event";
|
|
13
|
+
const PAIRING_OFFER = "notifications/rooms/pairing_offer";
|
|
14
|
+
const PAIRED_BY_CODE = "notifications/rooms/paired_by_code";
|
|
15
|
+
/** How long an offered pairing code stays valid by default, matching `rooms.DefaultPairingCodeTTL`. */
|
|
16
|
+
export const DEFAULT_PAIRING_CODE_TTL_MS = 2 * 60 * 1000;
|
|
17
|
+
// Leaves out 0, O, 1 and I, which a user reading a code aloud mixes up.
|
|
18
|
+
const PAIRING_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
19
|
+
/** Event name `ask()` emits: the page's user asked the agents something. */
|
|
20
|
+
export const EVENT_ASK = "room.ask";
|
|
21
|
+
/** Event name `selectionChanged()` emits. */
|
|
22
|
+
export const EVENT_SELECTION_CHANGED = "page.selection_changed";
|
|
23
|
+
/** Largest encoded `data` one event may carry, matching the bridge's cap. */
|
|
24
|
+
export const MAX_EVENT_DATA_BYTES = 16 << 10;
|
|
25
|
+
const EVENT_NAME = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*){0,3}$/;
|
|
26
|
+
/**
|
|
27
|
+
* Reports whether `name` is one a page may emit: one to four dot-separated
|
|
28
|
+
* lowercase segments, each starting with a letter, at most 64 characters.
|
|
29
|
+
* Mirrors `rooms.ValidEventName`.
|
|
30
|
+
*/
|
|
31
|
+
export function validEventName(name) {
|
|
32
|
+
return name.length <= 64 && EVENT_NAME.test(name);
|
|
33
|
+
}
|
|
34
|
+
const METHOD_NOT_FOUND = -32601;
|
|
35
|
+
const INVALID_PARAMS = -32602;
|
|
36
|
+
/**
|
|
37
|
+
* Joins a room on a massrelay relay as a page that offers tools to agents
|
|
38
|
+
* through the mcpkit room bridge. Resolves once the relay admits the page,
|
|
39
|
+
* and rejects with the relay's code (`ROOM_FULL`, `ROOM_FORBIDDEN`, ...) or
|
|
40
|
+
* the socket's failure otherwise.
|
|
41
|
+
*
|
|
42
|
+
* The page answers `tools/list` and `tools/call` from any bridge in the room,
|
|
43
|
+
* sends `notifications/tools/list_changed` when its tools change, and aborts a
|
|
44
|
+
* call's `signal` on `notifications/cancelled`. It reads only messages
|
|
45
|
+
* addressed to it, never room-wide ones.
|
|
46
|
+
*/
|
|
47
|
+
export async function joinToolRoom(opts) {
|
|
48
|
+
const page = new Page(opts);
|
|
49
|
+
await page.join();
|
|
50
|
+
return page;
|
|
51
|
+
}
|
|
52
|
+
class Page {
|
|
53
|
+
opts;
|
|
54
|
+
id = "";
|
|
55
|
+
room;
|
|
56
|
+
closed;
|
|
57
|
+
resolveClosed;
|
|
58
|
+
tools = new Map();
|
|
59
|
+
native = new Map();
|
|
60
|
+
bridges = new Set();
|
|
61
|
+
running = new Map();
|
|
62
|
+
label = "";
|
|
63
|
+
allowed = false;
|
|
64
|
+
onPairing;
|
|
65
|
+
onPairedFn;
|
|
66
|
+
offer;
|
|
67
|
+
left = false;
|
|
68
|
+
modelContext;
|
|
69
|
+
client;
|
|
70
|
+
constructor(opts) {
|
|
71
|
+
this.opts = opts;
|
|
72
|
+
this.room = opts.room;
|
|
73
|
+
this.closed = new Promise((resolve) => (this.resolveClosed = resolve));
|
|
74
|
+
this.modelContext = findModelContext(opts.modelContext);
|
|
75
|
+
}
|
|
76
|
+
join() {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
let joined = false;
|
|
79
|
+
const refuse = (err) => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
reject(err);
|
|
82
|
+
};
|
|
83
|
+
const timer = setTimeout(() => {
|
|
84
|
+
if (joined)
|
|
85
|
+
return;
|
|
86
|
+
reject(new Error(`joinToolRoom: relay did not admit the page within ${this.opts.joinTimeoutMs ?? 10000}ms`));
|
|
87
|
+
this.client.disconnect();
|
|
88
|
+
}, this.opts.joinTimeoutMs ?? 10000);
|
|
89
|
+
const WebSocketImpl = this.opts.WebSocket;
|
|
90
|
+
this.client = new CollabClient({
|
|
91
|
+
token: this.opts.token,
|
|
92
|
+
// A reconnect would rejoin under a new peer id, which no pairing
|
|
93
|
+
// knows, so a dropped page ends and `closed` resolves instead.
|
|
94
|
+
maxRetries: 0,
|
|
95
|
+
// CollabClient takes a WebSocket implementation only through the
|
|
96
|
+
// GRPCWSClient it builds on.
|
|
97
|
+
_grpcFactory: WebSocketImpl ? () => new GRPCWSClient({ WebSocket: WebSocketImpl }) : undefined,
|
|
98
|
+
onConnect: (clientId) => {
|
|
99
|
+
joined = true;
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
this.id = clientId;
|
|
102
|
+
resolve();
|
|
103
|
+
},
|
|
104
|
+
// Before the join an ErrorEvent is the relay refusing it. After the
|
|
105
|
+
// join it refuses one message, typically PEER_NOT_FOUND for a reply
|
|
106
|
+
// to a bridge that has just left, and the page stays in the room.
|
|
107
|
+
onErrorEvent: (code, message) => {
|
|
108
|
+
if (!joined)
|
|
109
|
+
refuse(new Error(`joinToolRoom: ${code}: ${message}`));
|
|
110
|
+
},
|
|
111
|
+
onError: (err) => {
|
|
112
|
+
if (!joined)
|
|
113
|
+
refuse(err);
|
|
114
|
+
},
|
|
115
|
+
onDisconnect: () => this.shutdown(),
|
|
116
|
+
onPeerLeft: (clientId) => this.bridges.delete(clientId),
|
|
117
|
+
onAppMessage: (kind, payload, from, to) => {
|
|
118
|
+
// Only messages addressed to this page; a room-wide one is someone
|
|
119
|
+
// else's traffic.
|
|
120
|
+
if (kind !== MESSAGE_KIND || to !== this.id)
|
|
121
|
+
return;
|
|
122
|
+
let m;
|
|
123
|
+
try {
|
|
124
|
+
m = JSON.parse(new TextDecoder().decode(payload));
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.handle(from, m);
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
try {
|
|
133
|
+
this.client.connect(relayBase(this.opts.relay), this.opts.room, this.opts.username ?? "page", {});
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
refuse(err instanceof Error ? err : new Error(String(err)));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
registerTool(tool) {
|
|
141
|
+
this.tools.set(tool.name, tool);
|
|
142
|
+
if (this.modelContext)
|
|
143
|
+
this.native.set(tool.name, this.modelContext.registerTool(tool));
|
|
144
|
+
this.notifyBridges("notifications/tools/list_changed");
|
|
145
|
+
return { unregister: () => this.unregisterTool(tool.name) };
|
|
146
|
+
}
|
|
147
|
+
unregisterTool(name) {
|
|
148
|
+
if (!this.tools.delete(name))
|
|
149
|
+
return;
|
|
150
|
+
const handle = this.native.get(name);
|
|
151
|
+
handle?.unregister?.();
|
|
152
|
+
this.native.delete(name);
|
|
153
|
+
this.notifyBridges("notifications/tools/list_changed");
|
|
154
|
+
}
|
|
155
|
+
setLabel(label) {
|
|
156
|
+
this.label = label;
|
|
157
|
+
this.notifyBridges(PAGE_STATE, { label });
|
|
158
|
+
}
|
|
159
|
+
focus() {
|
|
160
|
+
this.notifyBridges(PAGE_STATE, { label: this.label, focused: true });
|
|
161
|
+
}
|
|
162
|
+
setAgentsAllowed(allowed) {
|
|
163
|
+
this.allowed = allowed;
|
|
164
|
+
this.notifyBridges(AGENTS_ALLOWED, { allowed });
|
|
165
|
+
}
|
|
166
|
+
acceptPairing(code) {
|
|
167
|
+
this.notifyBridges(ACCEPT_PAIRING, { code });
|
|
168
|
+
}
|
|
169
|
+
revoke(session = "") {
|
|
170
|
+
this.notifyBridges(REVOKE, { session });
|
|
171
|
+
}
|
|
172
|
+
emit(name, data = null) {
|
|
173
|
+
if (!validEventName(name))
|
|
174
|
+
throw new Error(`invalid page event name ${JSON.stringify(name)}`);
|
|
175
|
+
const encoded = JSON.stringify(data ?? null);
|
|
176
|
+
const size = new TextEncoder().encode(encoded).length;
|
|
177
|
+
if (size > MAX_EVENT_DATA_BYTES) {
|
|
178
|
+
throw new Error(`page event data is ${size} bytes, over ${MAX_EVENT_DATA_BYTES}`);
|
|
179
|
+
}
|
|
180
|
+
this.notifyBridges(PAGE_EVENT, { name, data: data ?? null });
|
|
181
|
+
}
|
|
182
|
+
ask(text) {
|
|
183
|
+
this.emit(EVENT_ASK, { text });
|
|
184
|
+
}
|
|
185
|
+
selectionChanged(selection) {
|
|
186
|
+
this.emit(EVENT_SELECTION_CHANGED, { selection });
|
|
187
|
+
}
|
|
188
|
+
onPairingRequest(fn) {
|
|
189
|
+
this.onPairing = fn;
|
|
190
|
+
}
|
|
191
|
+
offerPairingCode(opts = {}) {
|
|
192
|
+
const ttlMs = opts.ttlMs && opts.ttlMs > 0 ? opts.ttlMs : DEFAULT_PAIRING_CODE_TTL_MS;
|
|
193
|
+
const code = newPairingCode();
|
|
194
|
+
this.offer = { code, expires: Date.now() + ttlMs };
|
|
195
|
+
this.notifyBridges(PAIRING_OFFER, { code, ttlMs });
|
|
196
|
+
return code;
|
|
197
|
+
}
|
|
198
|
+
withdrawPairingCode() {
|
|
199
|
+
this.offer = undefined;
|
|
200
|
+
this.notifyBridges(PAIRING_OFFER, { code: "" });
|
|
201
|
+
}
|
|
202
|
+
onPaired(fn) {
|
|
203
|
+
this.onPairedFn = fn;
|
|
204
|
+
}
|
|
205
|
+
leave() {
|
|
206
|
+
if (this.left)
|
|
207
|
+
return;
|
|
208
|
+
this.client.disconnect();
|
|
209
|
+
this.shutdown();
|
|
210
|
+
}
|
|
211
|
+
shutdown() {
|
|
212
|
+
if (this.left)
|
|
213
|
+
return;
|
|
214
|
+
this.left = true;
|
|
215
|
+
for (const ctl of this.running.values())
|
|
216
|
+
ctl.abort();
|
|
217
|
+
this.running.clear();
|
|
218
|
+
this.resolveClosed();
|
|
219
|
+
}
|
|
220
|
+
handle(from, m) {
|
|
221
|
+
switch (m.method) {
|
|
222
|
+
case "tools/list":
|
|
223
|
+
this.reply(from, m.id, { tools: this.descriptors() });
|
|
224
|
+
this.greet(from);
|
|
225
|
+
return;
|
|
226
|
+
case "tools/call":
|
|
227
|
+
this.greet(from);
|
|
228
|
+
void this.call(from, m);
|
|
229
|
+
return;
|
|
230
|
+
case "notifications/cancelled":
|
|
231
|
+
this.running.get(`${from}/${JSON.stringify(m.params?.requestId)}`)?.abort();
|
|
232
|
+
return;
|
|
233
|
+
case PAIRING_REQUESTED:
|
|
234
|
+
this.onPairing?.(m.params);
|
|
235
|
+
return;
|
|
236
|
+
case PAIRED_BY_CODE: {
|
|
237
|
+
const paired = m.params;
|
|
238
|
+
// The code is spent, so withdraw it from every bridge.
|
|
239
|
+
if (paired?.code && paired.code === this.offer?.code)
|
|
240
|
+
this.withdrawPairingCode();
|
|
241
|
+
this.onPairedFn?.(paired);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (m.id !== undefined && m.method) {
|
|
246
|
+
this.send(from, { id: m.id, error: { code: METHOD_NOT_FOUND, message: `method not found: ${m.method}` } });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// A page cannot address a bridge before hearing from it, so the first time
|
|
250
|
+
// one speaks it gets the state it missed.
|
|
251
|
+
greet(from) {
|
|
252
|
+
if (this.bridges.has(from))
|
|
253
|
+
return;
|
|
254
|
+
this.bridges.add(from);
|
|
255
|
+
if (this.label)
|
|
256
|
+
this.send(from, { method: PAGE_STATE, params: { label: this.label } });
|
|
257
|
+
if (this.allowed)
|
|
258
|
+
this.send(from, { method: AGENTS_ALLOWED, params: { allowed: true } });
|
|
259
|
+
const left = this.offer ? this.offer.expires - Date.now() : 0;
|
|
260
|
+
if (this.offer && left > 0)
|
|
261
|
+
this.send(from, { method: PAIRING_OFFER, params: { code: this.offer.code, ttlMs: left } });
|
|
262
|
+
}
|
|
263
|
+
descriptors() {
|
|
264
|
+
return [...this.tools.keys()].sort().map((name) => {
|
|
265
|
+
const t = this.tools.get(name);
|
|
266
|
+
return {
|
|
267
|
+
name: t.name,
|
|
268
|
+
description: t.description ?? "",
|
|
269
|
+
inputSchema: t.inputSchema ?? { type: "object" },
|
|
270
|
+
annotations: { ...t.annotations, readOnlyHint: t.annotations?.readOnlyHint ?? false },
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async call(from, m) {
|
|
275
|
+
const name = m.params?.name;
|
|
276
|
+
const tool = this.tools.get(name);
|
|
277
|
+
if (!tool) {
|
|
278
|
+
this.send(from, { id: m.id, error: { code: INVALID_PARAMS, message: `unknown tool: ${name}` } });
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const key = `${from}/${JSON.stringify(m.id)}`;
|
|
282
|
+
const ctl = new AbortController();
|
|
283
|
+
this.running.set(key, ctl);
|
|
284
|
+
const token = m.params?._meta?.progressToken;
|
|
285
|
+
const progress = (done, total, message) => {
|
|
286
|
+
if (token === undefined || ctl.signal.aborted)
|
|
287
|
+
return;
|
|
288
|
+
this.send(from, { method: "notifications/progress", params: { progressToken: token, progress: done, total, message } });
|
|
289
|
+
};
|
|
290
|
+
let result;
|
|
291
|
+
try {
|
|
292
|
+
result = toCallToolResult(await tool.execute(m.params?.arguments ?? {}, { signal: ctl.signal, progress, from }));
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
result = errorResult(err);
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
this.running.delete(key);
|
|
299
|
+
}
|
|
300
|
+
// MCP sends no response for a cancelled request.
|
|
301
|
+
if (ctl.signal.aborted)
|
|
302
|
+
return;
|
|
303
|
+
this.reply(from, m.id, result);
|
|
304
|
+
}
|
|
305
|
+
notifyBridges(method, params) {
|
|
306
|
+
for (const id of [...this.bridges].sort())
|
|
307
|
+
this.send(id, params === undefined ? { method } : { method, params });
|
|
308
|
+
}
|
|
309
|
+
reply(to, id, result) {
|
|
310
|
+
this.send(to, { id, result });
|
|
311
|
+
}
|
|
312
|
+
send(to, m) {
|
|
313
|
+
if (this.left)
|
|
314
|
+
return;
|
|
315
|
+
const payload = new TextEncoder().encode(JSON.stringify({ jsonrpc: "2.0", ...m }));
|
|
316
|
+
this.client.sendAppMessage(MESSAGE_KIND, payload, { to });
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function newPairingCode() {
|
|
320
|
+
const bytes = new Uint8Array(8);
|
|
321
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
322
|
+
// 256 is a multiple of the 32-letter alphabet, so this is unbiased.
|
|
323
|
+
return [...bytes].map((b) => PAIRING_ALPHABET[b % PAIRING_ALPHABET.length]).join("");
|
|
324
|
+
}
|
|
325
|
+
function findModelContext(opt) {
|
|
326
|
+
if (!opt)
|
|
327
|
+
return undefined;
|
|
328
|
+
if (opt !== true)
|
|
329
|
+
return opt;
|
|
330
|
+
const g = globalThis;
|
|
331
|
+
return g.navigator?.modelContext ?? g.document?.modelContext ?? undefined;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* The base URL CollabClient appends its room path to: ws(s), with any mount
|
|
335
|
+
* path, and no trailing slash or query.
|
|
336
|
+
*/
|
|
337
|
+
function relayBase(relay) {
|
|
338
|
+
const u = new URL(wsUrl(relay, "_"));
|
|
339
|
+
u.pathname = u.pathname.replace(/\/ws\/v1\/_\/sync$/, "");
|
|
340
|
+
u.search = "";
|
|
341
|
+
return u.toString().replace(/\/$/, "");
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The relay's WebSocket URL for a room. The room is one path segment, so a
|
|
345
|
+
* `/` in it is escaped.
|
|
346
|
+
*/
|
|
347
|
+
export function wsUrl(relay, room, token) {
|
|
348
|
+
const u = new URL(relay);
|
|
349
|
+
if (u.protocol === "http:")
|
|
350
|
+
u.protocol = "ws:";
|
|
351
|
+
else if (u.protocol === "https:")
|
|
352
|
+
u.protocol = "wss:";
|
|
353
|
+
else if (u.protocol !== "ws:" && u.protocol !== "wss:") {
|
|
354
|
+
throw new Error(`joinToolRoom: relay url ${relay}: scheme must be http, https, ws or wss`);
|
|
355
|
+
}
|
|
356
|
+
const base = u.pathname.replace(/\/$/, "");
|
|
357
|
+
u.pathname = `${base}/ws/v1/${encodeURIComponent(room)}/sync`;
|
|
358
|
+
if (token)
|
|
359
|
+
u.searchParams.set("token", token);
|
|
360
|
+
return u.toString();
|
|
361
|
+
}
|
|
362
|
+
//# sourceMappingURL=room.js.map
|
package/dist/room.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"room.js","sourceRoot":"","sources":["../src/room.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAiB,MAAM,WAAW,CAAC;AAEzE,mFAAmF;AACnF,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAC;AAEpC,sEAAsE;AACtE,MAAM,UAAU,GAAG,gCAAgC,CAAC;AACpD,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAC5D,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAC5D,MAAM,MAAM,GAAG,4BAA4B,CAAC;AAC5C,MAAM,iBAAiB,GAAG,uCAAuC,CAAC;AAClE,MAAM,UAAU,GAAG,2BAA2B,CAAC;AAC/C,MAAM,aAAa,GAAG,mCAAm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AC,CAAC;IAC9C,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC;AACtB,CAAC"}
|
package/dist/tool.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool shape the room client takes. It follows WebMCP's `registerTool`
|
|
3
|
+
* descriptor (name, description, inputSchema, annotations, execute), so a
|
|
4
|
+
* tool written for WebMCP registers here unchanged and the reverse.
|
|
5
|
+
*/
|
|
6
|
+
export interface PageTool {
|
|
7
|
+
/** The tool's name as agents see it. `page` is reserved by the bridge. */
|
|
8
|
+
name: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
/** JSON Schema for the arguments. Defaults to `{type: "object"}`. */
|
|
11
|
+
inputSchema?: Record<string, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* MCP tool annotations. `readOnlyHint: true` lets an agent holding only
|
|
14
|
+
* `pages:read` call the tool; anything else needs `pages:act`.
|
|
15
|
+
*/
|
|
16
|
+
annotations?: {
|
|
17
|
+
readOnlyHint?: boolean;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Runs the tool. The result may be a full MCP `CallToolResult`, a string
|
|
22
|
+
* (sent as one text block), or any other JSON value (sent as text and as
|
|
23
|
+
* `structuredContent`). A thrown error becomes an `isError` result.
|
|
24
|
+
*/
|
|
25
|
+
execute(args: Record<string, unknown>, context: ToolCallContext): unknown | Promise<unknown>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* What a call gets besides its arguments. Every field is optional so a tool
|
|
29
|
+
* written for a host that passes something else (WebMCP passes its own
|
|
30
|
+
* client object) still type-checks.
|
|
31
|
+
*/
|
|
32
|
+
export interface ToolCallContext {
|
|
33
|
+
/** Aborted when the agent cancels the call or the room is left. */
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
/** Reports progress to the agent. A no-op when the agent asked for none. */
|
|
36
|
+
progress?(progress: number, total?: number, message?: string): void;
|
|
37
|
+
/** The peer id of the bridge that forwarded the call. */
|
|
38
|
+
from?: string;
|
|
39
|
+
}
|
|
40
|
+
/** An MCP `CallToolResult`, the subset the client produces. */
|
|
41
|
+
export interface CallToolResult {
|
|
42
|
+
content: Array<{
|
|
43
|
+
type: string;
|
|
44
|
+
text?: string;
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
}>;
|
|
47
|
+
structuredContent?: unknown;
|
|
48
|
+
isError?: boolean;
|
|
49
|
+
[key: string]: unknown;
|
|
50
|
+
}
|
|
51
|
+
/** Turns whatever `execute` returned into a `CallToolResult`. */
|
|
52
|
+
export declare function toCallToolResult(value: unknown): CallToolResult;
|
|
53
|
+
/** The `isError` result a thrown error becomes. */
|
|
54
|
+
export declare function errorResult(err: unknown): CallToolResult;
|
|
55
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB,0EAA0E;IAC1E,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qEAAqE;IACrE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACtC;;;OAGG;IACH,WAAW,CAAC,EAAE;QAAE,YAAY,CAAC,EAAE,OAAO,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IACjE;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9F;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,mEAAmE;IACnE,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,4EAA4E;IAC5E,QAAQ,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpE,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,+DAA+D;AAC/D,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC,CAAC;IACxE,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,iEAAiE;AACjE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,CAO/D;AAED,mDAAmD;AACnD,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,cAAc,CAGxD"}
|
package/dist/tool.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Turns whatever `execute` returned into a `CallToolResult`. */
|
|
2
|
+
export function toCallToolResult(value) {
|
|
3
|
+
if (typeof value === "string")
|
|
4
|
+
return { content: [{ type: "text", text: value }] };
|
|
5
|
+
if (value && typeof value === "object" && Array.isArray(value.content)) {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
if (value === undefined)
|
|
9
|
+
return { content: [] };
|
|
10
|
+
return { content: [{ type: "text", text: JSON.stringify(value) }], structuredContent: value };
|
|
11
|
+
}
|
|
12
|
+
/** The `isError` result a thrown error becomes. */
|
|
13
|
+
export function errorResult(err) {
|
|
14
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
15
|
+
return { content: [{ type: "text", text: message }], isError: true };
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=tool.js.map
|
package/dist/tool.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AA8CA,iEAAiE;AACjE,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;IACnF,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAE,KAAwB,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3F,OAAO,KAAuB,CAAC;IACjC,CAAC;IACD,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IAChD,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAAC;AAChG,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACjE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AACvE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@panyam/mcpkit-rooms",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.8.6",
|
|
4
|
+
"description": "Page side of the mcpkit room bridge: offer WebMCP-shaped tools to agents through a massrelay room",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/panyam/mcpkit.git",
|
|
21
|
+
"directory": "experimental/ext/rooms/ts"
|
|
22
|
+
},
|
|
23
|
+
"license": "Apache-2.0",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.build.json",
|
|
29
|
+
"check": "tsc --noEmit",
|
|
30
|
+
"check:pack": "node scripts/check-pack.mjs",
|
|
31
|
+
"test": "vitest run"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@bufbuild/protobuf": "^2.11.0",
|
|
35
|
+
"@panyam/massrelay": "^0.2.5",
|
|
36
|
+
"@panyam/servicekit-client": "^0.1.11"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@modelcontextprotocol/client": "^2.3.1",
|
|
40
|
+
"@types/jsdom": "^30.0.0",
|
|
41
|
+
"@types/node": "^26.6.5",
|
|
42
|
+
"jsdom": "^30.1.3",
|
|
43
|
+
"typescript": "7.0.2",
|
|
44
|
+
"vitest": "^5.0.5"
|
|
45
|
+
}
|
|
46
|
+
}
|