@ractive-ch/tab-mcp-client 0.3.0 → 0.5.0
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 +131 -11
- package/cdn/connection-by0y4k0a.min.js +10 -0
- package/cdn/{connection-5ehqkmw1.min.js.map → connection-by0y4k0a.min.js.map} +4 -4
- package/cdn/tab-mcp-client.min.js +3 -3
- package/cdn/tab-mcp-client.min.js.map +5 -5
- package/cdn/ui-xh7shp5z.min.js +4 -0
- package/cdn/{ui-dv7pxk0b.min.js.map → ui-xh7shp5z.min.js.map} +3 -3
- package/{connection-ag7eyd36.js → connection-p2kxtegw.js} +23 -9
- package/{connection-ag7eyd36.js.map → connection-p2kxtegw.js.map} +4 -4
- package/index.d.ts +1 -1
- package/index.js +214 -10
- package/index.js.map +5 -5
- package/internal.d.ts +3 -0
- package/package.json +15 -2
- package/protocol.d.ts +16 -1
- package/react.d.ts +92 -0
- package/react.js +236 -0
- package/react.js.map +10 -0
- package/secret.d.ts +1 -1
- package/tab-mcp.d.ts +32 -2
- package/types.d.ts +75 -2
- package/{ui-g1k2g5hm.js → ui-rtahm62h.js} +12 -4
- package/{ui-g1k2g5hm.js.map → ui-rtahm62h.js.map} +3 -3
- package/version.d.ts +1 -1
- package/cdn/connection-5ehqkmw1.min.js +0 -10
- package/cdn/ui-dv7pxk0b.min.js +0 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ractive-ch/tab-mcp-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Browser library: offer a web page's tools to AI agents through a tab-mcp relay",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"types": "./index.d.ts",
|
|
9
9
|
"default": "./index.js"
|
|
10
10
|
},
|
|
11
|
+
"./react": {
|
|
12
|
+
"types": "./react.d.ts",
|
|
13
|
+
"default": "./react.js"
|
|
14
|
+
},
|
|
11
15
|
"./cdn": "./cdn/tab-mcp-client.min.js"
|
|
12
16
|
},
|
|
13
17
|
"types": "./index.d.ts",
|
|
@@ -25,11 +29,20 @@
|
|
|
25
29
|
"mcp",
|
|
26
30
|
"model-context-protocol",
|
|
27
31
|
"browser",
|
|
28
|
-
"webmcp"
|
|
32
|
+
"webmcp",
|
|
33
|
+
"react"
|
|
29
34
|
],
|
|
30
35
|
"dependencies": {
|
|
31
36
|
"@cfworker/json-schema": "4.1.1"
|
|
32
37
|
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=18.3.0 <20"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"react": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
33
46
|
"publishConfig": {
|
|
34
47
|
"access": "public"
|
|
35
48
|
}
|
package/protocol.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface SessionMessage {
|
|
|
16
16
|
address: string;
|
|
17
17
|
origin: string;
|
|
18
18
|
appName?: string;
|
|
19
|
+
/** How AI apps pair: `code` or `oidc` (identity binding). Older relays omit it. */
|
|
20
|
+
pairing?: "code" | "oidc";
|
|
19
21
|
expiresAt: string;
|
|
20
22
|
limits: Limits;
|
|
21
23
|
grants: Grant[];
|
|
@@ -56,13 +58,20 @@ export interface GrantRemovedMessage {
|
|
|
56
58
|
grantId: string;
|
|
57
59
|
reason: string;
|
|
58
60
|
}
|
|
61
|
+
export interface IdentityBoundMessage {
|
|
62
|
+
type: "identity.bound";
|
|
63
|
+
/** The `round` of the `identity` this answers. */
|
|
64
|
+
round?: number;
|
|
65
|
+
}
|
|
59
66
|
export interface ErrorMessage {
|
|
60
67
|
type: "error";
|
|
61
68
|
code: string;
|
|
62
69
|
message: string;
|
|
63
70
|
id?: string;
|
|
71
|
+
/** On `identity_refused`: the `round` of the `identity` it answers. */
|
|
72
|
+
round?: number;
|
|
64
73
|
}
|
|
65
|
-
export type RelayMessage = SessionMessage | PingMessage | ByeMessage | HttpRequestMessage | HttpCancelMessage | PairingCodeMessage | GrantAddedMessage | GrantRemovedMessage | ErrorMessage;
|
|
74
|
+
export type RelayMessage = SessionMessage | PingMessage | ByeMessage | HttpRequestMessage | HttpCancelMessage | PairingCodeMessage | GrantAddedMessage | GrantRemovedMessage | IdentityBoundMessage | ErrorMessage;
|
|
66
75
|
export type TabMessage = {
|
|
67
76
|
type: "hello";
|
|
68
77
|
library: {
|
|
@@ -109,6 +118,12 @@ export type TabMessage = {
|
|
|
109
118
|
grantId: string;
|
|
110
119
|
} | {
|
|
111
120
|
type: "grants.revokeAll";
|
|
121
|
+
} | {
|
|
122
|
+
type: "identity";
|
|
123
|
+
assertion: string;
|
|
124
|
+
round: number;
|
|
125
|
+
} | {
|
|
126
|
+
type: "identity.logout";
|
|
112
127
|
} | {
|
|
113
128
|
type: "error";
|
|
114
129
|
code: string;
|
package/react.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@ractive-ch/tab-mcp-client/react`: React bindings for single-page apps
|
|
3
|
+
* (iteration 9). One `TabMcp` per `<TabMcpProvider>`, hooks for its state,
|
|
4
|
+
* its tools and the calls of AI apps, and a button that opens the connect
|
|
5
|
+
* dialog. `react` (18.3 or 19) is an optional peer dependency; the core
|
|
6
|
+
* entry stays free of it.
|
|
7
|
+
*/
|
|
8
|
+
import { type ButtonHTMLAttributes, type DependencyList, type ReactElement, type ReactNode } from "react";
|
|
9
|
+
import { type ConnectionState, type Grant, type IdentityState, type PairingCode, type ReconnectingInfo, type SessionInfo, TabMcp, type TabMcpOptions, type Tool, type ToolCallEnd, type ToolCallStart } from "./index.js";
|
|
10
|
+
/** What `useTabMcpState()` returns: one object per change, the same object otherwise. */
|
|
11
|
+
export interface TabMcpSnapshot {
|
|
12
|
+
state: ConnectionState;
|
|
13
|
+
/** The MCP address to give to AI apps, while connected. */
|
|
14
|
+
address: string | undefined;
|
|
15
|
+
session: SessionInfo | undefined;
|
|
16
|
+
/** The AI apps connected to this address. */
|
|
17
|
+
grants: readonly Grant[];
|
|
18
|
+
/** The pairing code to show, while the connect dialog is open. */
|
|
19
|
+
pairingCode: PairingCode | undefined;
|
|
20
|
+
/** Who the relay knows as the page's user (identity binding). */
|
|
21
|
+
identity: IdentityState;
|
|
22
|
+
/** Why the relay refused the identity (`identity: "refused"`). */
|
|
23
|
+
identityMessage: string | undefined;
|
|
24
|
+
/** While the library connects again by itself after the connection broke. */
|
|
25
|
+
reconnecting: ReconnectingInfo | undefined;
|
|
26
|
+
/** Names of the registered tools. */
|
|
27
|
+
tools: readonly string[];
|
|
28
|
+
}
|
|
29
|
+
/** Props of `TabMcpProvider`: the `TabMcp` options, or an instance of your own. */
|
|
30
|
+
export type TabMcpProviderProps = ((TabMcpOptions & {
|
|
31
|
+
client?: undefined;
|
|
32
|
+
}) | {
|
|
33
|
+
client: TabMcp;
|
|
34
|
+
}) & {
|
|
35
|
+
children?: ReactNode;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Makes one `TabMcp` for its tree from the props of its first render. A
|
|
39
|
+
* changed `relay` or `server.name` makes a new instance (and warns); other
|
|
40
|
+
* changes are ignored, but `onConfirm` and `identityAssertion` are always
|
|
41
|
+
* the latest ones (also when added or removed later). Pass `client` to use an instance of your own instead. The
|
|
42
|
+
* provider's own instance is disconnected when the provider unmounts; it
|
|
43
|
+
* never connects on the server.
|
|
44
|
+
*/
|
|
45
|
+
export declare function TabMcpProvider(props: TabMcpProviderProps): ReactElement;
|
|
46
|
+
/** The provider's `TabMcp`, for everything the hooks do not cover. */
|
|
47
|
+
export declare function useTabMcp(): TabMcp;
|
|
48
|
+
/**
|
|
49
|
+
* The connection state, address, grants, pairing code, identity and
|
|
50
|
+
* reconnecting of the provider's `TabMcp`. Re-renders only when one of them
|
|
51
|
+
* changes; the server and hydration see `disconnected`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function useTabMcpState(): TabMcpSnapshot;
|
|
54
|
+
/**
|
|
55
|
+
* Registers a tool while the component is mounted (with an `AbortSignal`,
|
|
56
|
+
* aborted on unmount). `execute` is always the latest one, so a new closure
|
|
57
|
+
* on each render does not register again; the tool is registered again only
|
|
58
|
+
* when its name, title, description, input schema or annotations change, or
|
|
59
|
+
* one of `deps`. A duplicate name or an invalid tool is thrown from the
|
|
60
|
+
* component's next render, for an error boundary.
|
|
61
|
+
*/
|
|
62
|
+
export declare function useTool(tool: Tool, deps?: DependencyList): void;
|
|
63
|
+
/** A tool call of an AI app, as `useToolCalls()` lists it. */
|
|
64
|
+
export interface ToolCallRecord extends ToolCallStart {
|
|
65
|
+
/** When it started, `Date.now()`. */
|
|
66
|
+
startedAt: number;
|
|
67
|
+
/** How it ended; missing while it runs. */
|
|
68
|
+
end?: ToolCallEnd;
|
|
69
|
+
}
|
|
70
|
+
export interface UseToolCallsOptions {
|
|
71
|
+
/** How many calls to keep; default 20. */
|
|
72
|
+
limit?: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The recent tool calls of AI apps since the component mounted, newest
|
|
76
|
+
* first, each with its end once it is over: for an activity view.
|
|
77
|
+
*/
|
|
78
|
+
export declare function useToolCalls(options?: UseToolCallsOptions): readonly ToolCallRecord[];
|
|
79
|
+
/** Props of `ConnectButton`: a button's, with `children` that may depend on the state. */
|
|
80
|
+
export interface ConnectButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "children" | "type"> {
|
|
81
|
+
/** The label; default: the state ("Connect your AI app", "AI app connected"). */
|
|
82
|
+
children?: ReactNode | ((state: TabMcpSnapshot) => ReactNode);
|
|
83
|
+
}
|
|
84
|
+
/** The default label of `ConnectButton`. */
|
|
85
|
+
export declare function connectLabel(state: TabMcpSnapshot): string;
|
|
86
|
+
/**
|
|
87
|
+
* A button that opens the library's "Connect your AI app" dialog
|
|
88
|
+
* (`showConnectDialog()`), labelled with the state. `data-state` is
|
|
89
|
+
* `disconnected`, `connecting`, `connected` or `reconnecting`, for styling.
|
|
90
|
+
* An `onClick` that calls `event.preventDefault()` keeps the dialog closed.
|
|
91
|
+
*/
|
|
92
|
+
export declare function ConnectButton({ children, onClick, ...rest }: ConnectButtonProps): ReactElement;
|
package/react.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/react.ts
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
createElement,
|
|
7
|
+
useContext,
|
|
8
|
+
useEffect,
|
|
9
|
+
useInsertionEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
useState,
|
|
13
|
+
useSyncExternalStore
|
|
14
|
+
} from "react";
|
|
15
|
+
import {
|
|
16
|
+
TabMcp
|
|
17
|
+
} from "./index.js";
|
|
18
|
+
"use client";
|
|
19
|
+
var EMPTY = Object.freeze([]);
|
|
20
|
+
var SERVER_SNAPSHOT = Object.freeze({
|
|
21
|
+
state: "disconnected",
|
|
22
|
+
address: undefined,
|
|
23
|
+
session: undefined,
|
|
24
|
+
grants: EMPTY,
|
|
25
|
+
pairingCode: undefined,
|
|
26
|
+
identity: "none",
|
|
27
|
+
identityMessage: undefined,
|
|
28
|
+
reconnecting: undefined,
|
|
29
|
+
tools: EMPTY
|
|
30
|
+
});
|
|
31
|
+
var STATE_EVENTS = [
|
|
32
|
+
"statechange",
|
|
33
|
+
"connected",
|
|
34
|
+
"disconnected",
|
|
35
|
+
"grantschange",
|
|
36
|
+
"pairingcode",
|
|
37
|
+
"identitychange",
|
|
38
|
+
"reconnecting",
|
|
39
|
+
"toolschange"
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
class Store {
|
|
43
|
+
#mcp;
|
|
44
|
+
#listeners = new Set;
|
|
45
|
+
#snapshot;
|
|
46
|
+
constructor(mcp) {
|
|
47
|
+
this.#mcp = mcp;
|
|
48
|
+
this.#snapshot = this.#read(SERVER_SNAPSHOT);
|
|
49
|
+
const update = () => {
|
|
50
|
+
const next = this.#read(this.#snapshot);
|
|
51
|
+
if (next === this.#snapshot)
|
|
52
|
+
return;
|
|
53
|
+
this.#snapshot = next;
|
|
54
|
+
for (const listener of [...this.#listeners])
|
|
55
|
+
listener();
|
|
56
|
+
};
|
|
57
|
+
for (const type of STATE_EVENTS)
|
|
58
|
+
mcp.addEventListener(type, update);
|
|
59
|
+
}
|
|
60
|
+
subscribe = (listener) => {
|
|
61
|
+
this.#listeners.add(listener);
|
|
62
|
+
return () => this.#listeners.delete(listener);
|
|
63
|
+
};
|
|
64
|
+
getSnapshot = () => this.#snapshot;
|
|
65
|
+
#read(previous) {
|
|
66
|
+
const mcp = this.#mcp;
|
|
67
|
+
const tools = mcp.tools;
|
|
68
|
+
const next = {
|
|
69
|
+
state: mcp.state,
|
|
70
|
+
address: mcp.address,
|
|
71
|
+
session: mcp.session,
|
|
72
|
+
grants: mcp.grants,
|
|
73
|
+
pairingCode: mcp.pairingCode,
|
|
74
|
+
identity: mcp.identity,
|
|
75
|
+
identityMessage: mcp.identityMessage,
|
|
76
|
+
reconnecting: mcp.reconnecting,
|
|
77
|
+
tools: tools.length === previous.tools.length && tools.every((t, i) => t === previous.tools[i]) ? previous.tools : tools
|
|
78
|
+
};
|
|
79
|
+
const same = Object.keys(next).every((key) => next[key] === previous[key]);
|
|
80
|
+
return same ? previous : next;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
var stores = new WeakMap;
|
|
84
|
+
function storeOf(mcp) {
|
|
85
|
+
let store = stores.get(mcp);
|
|
86
|
+
if (store === undefined) {
|
|
87
|
+
store = new Store(mcp);
|
|
88
|
+
stores.set(mcp, store);
|
|
89
|
+
}
|
|
90
|
+
return store;
|
|
91
|
+
}
|
|
92
|
+
var Context = createContext(undefined);
|
|
93
|
+
function TabMcpProvider(props) {
|
|
94
|
+
const latest = useRef(props);
|
|
95
|
+
useInsertionEffect(() => {
|
|
96
|
+
latest.current = props;
|
|
97
|
+
});
|
|
98
|
+
const own = props.client === undefined ? props : undefined;
|
|
99
|
+
const relay = own?.relay;
|
|
100
|
+
const serverName = own?.server?.name;
|
|
101
|
+
const mcp = useMemo(() => {
|
|
102
|
+
if (props.client !== undefined)
|
|
103
|
+
return props.client;
|
|
104
|
+
const {
|
|
105
|
+
children: _,
|
|
106
|
+
client: __,
|
|
107
|
+
...options
|
|
108
|
+
} = props;
|
|
109
|
+
const handlers = () => latest.current;
|
|
110
|
+
const instance = new TabMcp({
|
|
111
|
+
...options,
|
|
112
|
+
onConfirm: (request) => {
|
|
113
|
+
const ask = handlers().onConfirm;
|
|
114
|
+
return ask === undefined ? instance.confirmInPage(request) : ask(request);
|
|
115
|
+
},
|
|
116
|
+
identityAssertion: (request) => handlers().identityAssertion?.(request) ?? null
|
|
117
|
+
});
|
|
118
|
+
return instance;
|
|
119
|
+
}, [props.client, relay, serverName]);
|
|
120
|
+
const previous = useRef(undefined);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (previous.current !== undefined && previous.current !== mcp && props.client === undefined) {
|
|
123
|
+
console.warn("TabMcpProvider: relay or server.name changed, so the page has a new TabMcp; its tools and connection start over");
|
|
124
|
+
}
|
|
125
|
+
previous.current = mcp;
|
|
126
|
+
if (props.client !== undefined)
|
|
127
|
+
return;
|
|
128
|
+
return () => mcp.disconnect();
|
|
129
|
+
}, [mcp, props.client]);
|
|
130
|
+
return createElement(Context.Provider, { value: mcp }, props.children);
|
|
131
|
+
}
|
|
132
|
+
function useTabMcp() {
|
|
133
|
+
const mcp = useContext(Context);
|
|
134
|
+
if (mcp === undefined)
|
|
135
|
+
throw new Error("useTabMcp: no <TabMcpProvider> above this component");
|
|
136
|
+
return mcp;
|
|
137
|
+
}
|
|
138
|
+
function useTabMcpState() {
|
|
139
|
+
const store = storeOf(useTabMcp());
|
|
140
|
+
return useSyncExternalStore(store.subscribe, store.getSnapshot, () => SERVER_SNAPSHOT);
|
|
141
|
+
}
|
|
142
|
+
function useTool(tool, deps = EMPTY) {
|
|
143
|
+
const mcp = useTabMcp();
|
|
144
|
+
const latest = useRef(tool);
|
|
145
|
+
useInsertionEffect(() => {
|
|
146
|
+
latest.current = tool;
|
|
147
|
+
});
|
|
148
|
+
const [failure, setFailure] = useState();
|
|
149
|
+
if (failure !== undefined)
|
|
150
|
+
throw failure.error;
|
|
151
|
+
const shape = JSON.stringify([
|
|
152
|
+
tool.name,
|
|
153
|
+
tool.title,
|
|
154
|
+
tool.description,
|
|
155
|
+
tool.inputSchema,
|
|
156
|
+
tool.annotations
|
|
157
|
+
]);
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
const controller = new AbortController;
|
|
160
|
+
const { execute: _, ...fields } = latest.current;
|
|
161
|
+
mcp.registerTool({
|
|
162
|
+
...fields,
|
|
163
|
+
execute: (input, options) => latest.current.execute(input, options)
|
|
164
|
+
}, { signal: controller.signal }).catch((error) => {
|
|
165
|
+
if (!controller.signal.aborted)
|
|
166
|
+
setFailure({ error });
|
|
167
|
+
});
|
|
168
|
+
return () => controller.abort();
|
|
169
|
+
}, [mcp, shape, ...deps]);
|
|
170
|
+
}
|
|
171
|
+
function useToolCalls(options = {}) {
|
|
172
|
+
const mcp = useTabMcp();
|
|
173
|
+
const limit = Math.max(0, options.limit ?? 20);
|
|
174
|
+
const [calls, setCalls] = useState(EMPTY);
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
const started = (event) => {
|
|
177
|
+
const record = { ...event.detail, startedAt: Date.now() };
|
|
178
|
+
setCalls((list) => [record, ...list].slice(0, limit));
|
|
179
|
+
};
|
|
180
|
+
const ended = (event) => {
|
|
181
|
+
const end = event.detail;
|
|
182
|
+
setCalls((list) => list.some((r) => r.id === end.id) ? list.map((r) => r.id === end.id ? { ...r, end } : r) : list);
|
|
183
|
+
};
|
|
184
|
+
mcp.addEventListener("toolcall", started);
|
|
185
|
+
mcp.addEventListener("toolcallend", ended);
|
|
186
|
+
setCalls((list) => list.length > limit ? list.slice(0, limit) : list);
|
|
187
|
+
return () => {
|
|
188
|
+
mcp.removeEventListener("toolcall", started);
|
|
189
|
+
mcp.removeEventListener("toolcallend", ended);
|
|
190
|
+
};
|
|
191
|
+
}, [mcp, limit]);
|
|
192
|
+
return calls;
|
|
193
|
+
}
|
|
194
|
+
function connectLabel(state) {
|
|
195
|
+
if (state.state === "connected" && state.grants.length > 0)
|
|
196
|
+
return state.grants.length === 1 ? "AI app connected" : `${state.grants.length} AI apps connected`;
|
|
197
|
+
if (state.reconnecting !== undefined)
|
|
198
|
+
return "Reconnecting…";
|
|
199
|
+
if (state.state === "connecting")
|
|
200
|
+
return "Connecting…";
|
|
201
|
+
return "Connect your AI app";
|
|
202
|
+
}
|
|
203
|
+
function ConnectButton({
|
|
204
|
+
children,
|
|
205
|
+
onClick,
|
|
206
|
+
...rest
|
|
207
|
+
}) {
|
|
208
|
+
const mcp = useTabMcp();
|
|
209
|
+
const state = useTabMcpState();
|
|
210
|
+
const label = children === undefined ? connectLabel(state) : typeof children === "function" ? children(state) : children;
|
|
211
|
+
return createElement("button", {
|
|
212
|
+
type: "button",
|
|
213
|
+
"data-state": state.reconnecting !== undefined && state.state !== "connected" ? "reconnecting" : state.state,
|
|
214
|
+
...rest,
|
|
215
|
+
onClick: (event) => {
|
|
216
|
+
onClick?.(event);
|
|
217
|
+
if (event.defaultPrevented)
|
|
218
|
+
return;
|
|
219
|
+
mcp.showConnectDialog().catch((error) => {
|
|
220
|
+
console.error(error);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}, label);
|
|
224
|
+
}
|
|
225
|
+
export {
|
|
226
|
+
ConnectButton,
|
|
227
|
+
TabMcpProvider,
|
|
228
|
+
connectLabel,
|
|
229
|
+
useTabMcp,
|
|
230
|
+
useTabMcpState,
|
|
231
|
+
useTool,
|
|
232
|
+
useToolCalls
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
//# debugId=3FC41B1BC6F8BEB164756E2164756E21
|
|
236
|
+
//# sourceMappingURL=react.js.map
|
package/react.js.map
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/react.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"\"use client\";\n/**\n * `@ractive-ch/tab-mcp-client/react`: React bindings for single-page apps\n * (iteration 9). One `TabMcp` per `<TabMcpProvider>`, hooks for its state,\n * its tools and the calls of AI apps, and a button that opens the connect\n * dialog. `react` (18.3 or 19) is an optional peer dependency; the core\n * entry stays free of it.\n */\n\nimport {\n\ttype ButtonHTMLAttributes,\n\tcreateContext,\n\tcreateElement,\n\ttype DependencyList,\n\ttype MouseEvent,\n\ttype ReactElement,\n\ttype ReactNode,\n\tuseContext,\n\tuseEffect,\n\tuseInsertionEffect,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseSyncExternalStore,\n} from \"react\";\nimport {\n\ttype ConfirmRequest,\n\ttype ConnectionState,\n\ttype Grant,\n\ttype IdentityAssertionRequest,\n\ttype IdentityState,\n\ttype PairingCode,\n\ttype ReconnectingInfo,\n\ttype SessionInfo,\n\tTabMcp,\n\ttype TabMcpEventMap,\n\ttype TabMcpOptions,\n\ttype Tool,\n\ttype ToolCallEnd,\n\ttype ToolCallStart,\n} from \"./index.ts\";\n\n/** What `useTabMcpState()` returns: one object per change, the same object otherwise. */\nexport interface TabMcpSnapshot {\n\tstate: ConnectionState;\n\t/** The MCP address to give to AI apps, while connected. */\n\taddress: string | undefined;\n\tsession: SessionInfo | undefined;\n\t/** The AI apps connected to this address. */\n\tgrants: readonly Grant[];\n\t/** The pairing code to show, while the connect dialog is open. */\n\tpairingCode: PairingCode | undefined;\n\t/** Who the relay knows as the page's user (identity binding). */\n\tidentity: IdentityState;\n\t/** Why the relay refused the identity (`identity: \"refused\"`). */\n\tidentityMessage: string | undefined;\n\t/** While the library connects again by itself after the connection broke. */\n\treconnecting: ReconnectingInfo | undefined;\n\t/** Names of the registered tools. */\n\ttools: readonly string[];\n}\n\nconst EMPTY: readonly never[] = Object.freeze([]);\n\n/** What the server renders, and what hydration starts from. */\nconst SERVER_SNAPSHOT: TabMcpSnapshot = Object.freeze({\n\tstate: \"disconnected\",\n\taddress: undefined,\n\tsession: undefined,\n\tgrants: EMPTY,\n\tpairingCode: undefined,\n\tidentity: \"none\",\n\tidentityMessage: undefined,\n\treconnecting: undefined,\n\ttools: EMPTY,\n});\n\nconst STATE_EVENTS = [\n\t\"statechange\",\n\t\"connected\",\n\t\"disconnected\",\n\t\"grantschange\",\n\t\"pairingcode\",\n\t\"identitychange\",\n\t\"reconnecting\",\n\t\"toolschange\",\n] as const satisfies readonly (keyof TabMcpEventMap)[];\n\n/** The external store of one `TabMcp`: a snapshot that changes only with the instance. */\nclass Store {\n\treadonly #mcp: TabMcp;\n\treadonly #listeners = new Set<() => void>();\n\t#snapshot: TabMcpSnapshot;\n\n\tconstructor(mcp: TabMcp) {\n\t\tthis.#mcp = mcp;\n\t\tthis.#snapshot = this.#read(SERVER_SNAPSHOT);\n\t\tconst update = () => {\n\t\t\tconst next = this.#read(this.#snapshot);\n\t\t\tif (next === this.#snapshot) return;\n\t\t\tthis.#snapshot = next;\n\t\t\tfor (const listener of [...this.#listeners]) listener();\n\t\t};\n\t\tfor (const type of STATE_EVENTS) mcp.addEventListener(type, update);\n\t}\n\n\treadonly subscribe = (listener: () => void): (() => void) => {\n\t\tthis.#listeners.add(listener);\n\t\treturn () => this.#listeners.delete(listener);\n\t};\n\n\treadonly getSnapshot = (): TabMcpSnapshot => this.#snapshot;\n\n\t/** The current values; `previous` itself when none changed. */\n\t#read(previous: TabMcpSnapshot): TabMcpSnapshot {\n\t\tconst mcp = this.#mcp;\n\t\tconst tools = mcp.tools;\n\t\tconst next: TabMcpSnapshot = {\n\t\t\tstate: mcp.state,\n\t\t\taddress: mcp.address,\n\t\t\tsession: mcp.session,\n\t\t\tgrants: mcp.grants,\n\t\t\tpairingCode: mcp.pairingCode,\n\t\t\tidentity: mcp.identity,\n\t\t\tidentityMessage: mcp.identityMessage,\n\t\t\treconnecting: mcp.reconnecting,\n\t\t\ttools:\n\t\t\t\ttools.length === previous.tools.length &&\n\t\t\t\ttools.every((t, i) => t === previous.tools[i])\n\t\t\t\t\t? previous.tools\n\t\t\t\t\t: tools,\n\t\t};\n\t\tconst same = (Object.keys(next) as (keyof TabMcpSnapshot)[]).every(\n\t\t\t(key) => next[key] === previous[key],\n\t\t);\n\t\treturn same ? previous : next;\n\t}\n}\n\nconst stores = new WeakMap<TabMcp, Store>();\n\nfunction storeOf(mcp: TabMcp): Store {\n\tlet store = stores.get(mcp);\n\tif (store === undefined) {\n\t\tstore = new Store(mcp);\n\t\tstores.set(mcp, store);\n\t}\n\treturn store;\n}\n\nconst Context = createContext<TabMcp | undefined>(undefined);\n\n/** Props of `TabMcpProvider`: the `TabMcp` options, or an instance of your own. */\nexport type TabMcpProviderProps = (\n\t| (TabMcpOptions & { client?: undefined })\n\t| { client: TabMcp }\n) & { children?: ReactNode };\n\n/**\n * Makes one `TabMcp` for its tree from the props of its first render. A\n * changed `relay` or `server.name` makes a new instance (and warns); other\n * changes are ignored, but `onConfirm` and `identityAssertion` are always\n * the latest ones (also when added or removed later). Pass `client` to use an instance of your own instead. The\n * provider's own instance is disconnected when the provider unmounts; it\n * never connects on the server.\n */\nexport function TabMcpProvider(props: TabMcpProviderProps): ReactElement {\n\tconst latest = useRef(props);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = props;\n\t});\n\tconst own = props.client === undefined ? props : undefined;\n\tconst relay = own?.relay;\n\tconst serverName = own?.server?.name;\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: only the relay and the server name make a new instance\n\tconst mcp = useMemo((): TabMcp => {\n\t\tif (props.client !== undefined) return props.client;\n\t\tconst {\n\t\t\tchildren: _,\n\t\t\tclient: __,\n\t\t\t...options\n\t\t} = props as TabMcpOptions & {\n\t\t\tchildren?: ReactNode;\n\t\t\tclient?: undefined;\n\t\t};\n\t\t// Always installed, so that a handler added, changed or removed in a\n\t\t// later render counts: without one, the library's own behaviour.\n\t\tconst handlers = () => latest.current as Partial<TabMcpOptions>;\n\t\tconst instance: TabMcp = new TabMcp({\n\t\t\t...options,\n\t\t\tonConfirm: (request: ConfirmRequest) => {\n\t\t\t\tconst ask = handlers().onConfirm;\n\t\t\t\treturn ask === undefined\n\t\t\t\t\t? instance.confirmInPage(request)\n\t\t\t\t\t: ask(request);\n\t\t\t},\n\t\t\tidentityAssertion: (request: IdentityAssertionRequest) =>\n\t\t\t\thandlers().identityAssertion?.(request) ?? null,\n\t\t});\n\t\treturn instance;\n\t}, [props.client, relay, serverName]);\n\n\tconst previous = useRef<TabMcp | undefined>(undefined);\n\tuseEffect(() => {\n\t\tif (\n\t\t\tprevious.current !== undefined &&\n\t\t\tprevious.current !== mcp &&\n\t\t\tprops.client === undefined\n\t\t) {\n\t\t\tconsole.warn(\n\t\t\t\t\"TabMcpProvider: relay or server.name changed, so the page has a new TabMcp; its tools and connection start over\",\n\t\t\t);\n\t\t}\n\t\tprevious.current = mcp;\n\t\tif (props.client !== undefined) return;\n\t\treturn () => mcp.disconnect();\n\t}, [mcp, props.client]);\n\n\treturn createElement(Context.Provider, { value: mcp }, props.children);\n}\n\n/** The provider's `TabMcp`, for everything the hooks do not cover. */\nexport function useTabMcp(): TabMcp {\n\tconst mcp = useContext(Context);\n\tif (mcp === undefined)\n\t\tthrow new Error(\"useTabMcp: no <TabMcpProvider> above this component\");\n\treturn mcp;\n}\n\n/**\n * The connection state, address, grants, pairing code, identity and\n * reconnecting of the provider's `TabMcp`. Re-renders only when one of them\n * changes; the server and hydration see `disconnected`.\n */\nexport function useTabMcpState(): TabMcpSnapshot {\n\tconst store = storeOf(useTabMcp());\n\treturn useSyncExternalStore(\n\t\tstore.subscribe,\n\t\tstore.getSnapshot,\n\t\t() => SERVER_SNAPSHOT,\n\t);\n}\n\n/**\n * Registers a tool while the component is mounted (with an `AbortSignal`,\n * aborted on unmount). `execute` is always the latest one, so a new closure\n * on each render does not register again; the tool is registered again only\n * when its name, title, description, input schema or annotations change, or\n * one of `deps`. A duplicate name or an invalid tool is thrown from the\n * component's next render, for an error boundary.\n */\nexport function useTool(tool: Tool, deps: DependencyList = EMPTY): void {\n\tconst mcp = useTabMcp();\n\tconst latest = useRef(tool);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = tool;\n\t});\n\tconst [failure, setFailure] = useState<{ error: unknown } | undefined>();\n\tif (failure !== undefined) throw failure.error;\n\tconst shape = JSON.stringify([\n\t\ttool.name,\n\t\ttool.title,\n\t\ttool.description,\n\t\ttool.inputSchema,\n\t\ttool.annotations,\n\t]);\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: `shape` and `deps` stand for the tool; `execute` is read through the ref\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\t\tconst { execute: _, ...fields } = latest.current;\n\t\tmcp\n\t\t\t.registerTool(\n\t\t\t\t{\n\t\t\t\t\t...fields,\n\t\t\t\t\texecute: (input, options) => latest.current.execute(input, options),\n\t\t\t\t},\n\t\t\t\t{ signal: controller.signal },\n\t\t\t)\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (!controller.signal.aborted) setFailure({ error });\n\t\t\t});\n\t\treturn () => controller.abort();\n\t}, [mcp, shape, ...deps]);\n}\n\n/** A tool call of an AI app, as `useToolCalls()` lists it. */\nexport interface ToolCallRecord extends ToolCallStart {\n\t/** When it started, `Date.now()`. */\n\tstartedAt: number;\n\t/** How it ended; missing while it runs. */\n\tend?: ToolCallEnd;\n}\n\nexport interface UseToolCallsOptions {\n\t/** How many calls to keep; default 20. */\n\tlimit?: number;\n}\n\n/**\n * The recent tool calls of AI apps since the component mounted, newest\n * first, each with its end once it is over: for an activity view.\n */\nexport function useToolCalls(\n\toptions: UseToolCallsOptions = {},\n): readonly ToolCallRecord[] {\n\tconst mcp = useTabMcp();\n\tconst limit = Math.max(0, options.limit ?? 20);\n\tconst [calls, setCalls] = useState<readonly ToolCallRecord[]>(EMPTY);\n\tuseEffect(() => {\n\t\tconst started = (event: TabMcpEventMap[\"toolcall\"]) => {\n\t\t\tconst record: ToolCallRecord = { ...event.detail, startedAt: Date.now() };\n\t\t\tsetCalls((list) => [record, ...list].slice(0, limit));\n\t\t};\n\t\tconst ended = (event: TabMcpEventMap[\"toolcallend\"]) => {\n\t\t\tconst end = event.detail;\n\t\t\tsetCalls((list) =>\n\t\t\t\tlist.some((r) => r.id === end.id)\n\t\t\t\t\t? list.map((r) => (r.id === end.id ? { ...r, end } : r))\n\t\t\t\t\t: list,\n\t\t\t);\n\t\t};\n\t\tmcp.addEventListener(\"toolcall\", started);\n\t\tmcp.addEventListener(\"toolcallend\", ended);\n\t\tsetCalls((list) => (list.length > limit ? list.slice(0, limit) : list));\n\t\treturn () => {\n\t\t\tmcp.removeEventListener(\"toolcall\", started);\n\t\t\tmcp.removeEventListener(\"toolcallend\", ended);\n\t\t};\n\t}, [mcp, limit]);\n\treturn calls;\n}\n\n/** Props of `ConnectButton`: a button's, with `children` that may depend on the state. */\nexport interface ConnectButtonProps\n\textends Omit<ButtonHTMLAttributes<HTMLButtonElement>, \"children\" | \"type\"> {\n\t/** The label; default: the state (\"Connect your AI app\", \"AI app connected\"). */\n\tchildren?: ReactNode | ((state: TabMcpSnapshot) => ReactNode);\n}\n\n/** The default label of `ConnectButton`. */\nexport function connectLabel(state: TabMcpSnapshot): string {\n\tif (state.state === \"connected\" && state.grants.length > 0)\n\t\treturn state.grants.length === 1\n\t\t\t? \"AI app connected\"\n\t\t\t: `${state.grants.length} AI apps connected`;\n\tif (state.reconnecting !== undefined) return \"Reconnecting…\";\n\tif (state.state === \"connecting\") return \"Connecting…\";\n\treturn \"Connect your AI app\";\n}\n\n/**\n * A button that opens the library's \"Connect your AI app\" dialog\n * (`showConnectDialog()`), labelled with the state. `data-state` is\n * `disconnected`, `connecting`, `connected` or `reconnecting`, for styling.\n * An `onClick` that calls `event.preventDefault()` keeps the dialog closed.\n */\nexport function ConnectButton({\n\tchildren,\n\tonClick,\n\t...rest\n}: ConnectButtonProps): ReactElement {\n\tconst mcp = useTabMcp();\n\tconst state = useTabMcpState();\n\tconst label =\n\t\tchildren === undefined\n\t\t\t? connectLabel(state)\n\t\t\t: typeof children === \"function\"\n\t\t\t\t? children(state)\n\t\t\t\t: children;\n\treturn createElement(\n\t\t\"button\",\n\t\t{\n\t\t\ttype: \"button\",\n\t\t\t\"data-state\":\n\t\t\t\tstate.reconnecting !== undefined && state.state !== \"connected\"\n\t\t\t\t\t? \"reconnecting\"\n\t\t\t\t\t: state.state,\n\t\t\t...rest,\n\t\t\tonClick: (event: MouseEvent<HTMLButtonElement>) => {\n\t\t\t\tonClick?.(event);\n\t\t\t\tif (event.defaultPrevented) return;\n\t\t\t\tmcp.showConnectDialog().catch((error: unknown) => {\n\t\t\t\t\tconsole.error(error);\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t\tlabel,\n\t);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;AASA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBA;AAAA;AAAA;AAzBA;AA8DA,IAAM,QAA0B,OAAO,OAAO,CAAC,CAAC;AAGhD,IAAM,kBAAkC,OAAO,OAAO;AAAA,EACrD,OAAO;AAAA,EACP,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,aAAa;AAAA,EACb,UAAU;AAAA,EACV,iBAAiB;AAAA,EACjB,cAAc;AAAA,EACd,OAAO;AACR,CAAC;AAED,IAAM,eAAe;AAAA,EACpB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAAA;AAGA,MAAM,MAAM;AAAA,EACF;AAAA,EACA,aAAa,IAAI;AAAA,EAC1B;AAAA,EAEA,WAAW,CAAC,KAAa;AAAA,IACxB,KAAK,OAAO;AAAA,IACZ,KAAK,YAAY,KAAK,MAAM,eAAe;AAAA,IAC3C,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AA,EACpC,MAAM,MAAM,UAAU;AAAA,EACtB,MAAM,QAAQ,eAAe;AAAA,EAC7B,MAAM,QACL,aAAa,YACV,aAAa,KAAK,IAClB,OAAO,aAAa,aACnB,SAAS,KAAK,IACd;AAAA,EACL,OAAO,cACN,UACA;AAAA,IACC,MAAM;AAAA,IACN,cACC,MAAM,iBAAiB,aAAa,MAAM,UAAU,cACjD,iBACA,MAAM;AAAA,OACP;AAAA,IACH,SAAS,CAAC,UAAyC;AAAA,MAClD,UAAU,KAAK;AAAA,MACf,IAAI,MAAM;AAAA,QAAkB;AAAA,MAC5B,IAAI,kBAAkB,EAAE,MAAM,CAAC,UAAmB;AAAA,QACjD,QAAQ,MAAM,KAAK;AAAA,OACnB;AAAA;AAAA,EAEH,GACA,KACD;AAAA;",
|
|
8
|
+
"debugId": "3FC41B1BC6F8BEB164756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/secret.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare function isSecret(value: unknown): value is string;
|
|
|
10
10
|
/** Reads, creates and rotates the secret; falls back to memory without storage. */
|
|
11
11
|
export declare class SecretStore {
|
|
12
12
|
#private;
|
|
13
|
-
constructor(storage: SecretStorage | null | undefined, key: string);
|
|
13
|
+
constructor(storage: SecretStorage | null | undefined, key: string, oldKey?: string);
|
|
14
14
|
/** The current secret; storage first, so a rotation in another tab is seen. */
|
|
15
15
|
get(): string;
|
|
16
16
|
/** Make and keep a new secret (a new address). */
|
package/tab-mcp.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* events, the pairing state and the address secret; `connect()` loads the
|
|
4
4
|
* connection and the MCP handler with a dynamic `import()`.
|
|
5
5
|
*/
|
|
6
|
-
import type { ConnectionState, ConnectOptions, Grant, PairingCode, RegisterToolOptions, SessionInfo, TabMcpEventMap, TabMcpOptions, Tool } from "./types.js";
|
|
6
|
+
import type { ConfirmRequest, ConnectionState, ConnectOptions, Grant, IdentityState, PairingCode, ReconnectingInfo, RegisterToolOptions, SessionInfo, TabMcpEventMap, TabMcpOptions, Tool } from "./types.js";
|
|
7
7
|
/** `wss://host/tab` for a relay given as `https://host`, `wss://host` or with `/tab`. */
|
|
8
8
|
export declare function tabUrl(relay: string): {
|
|
9
9
|
url: string;
|
|
@@ -39,6 +39,30 @@ export declare class TabMcp extends EventTarget {
|
|
|
39
39
|
get appName(): string | undefined;
|
|
40
40
|
/** The AI apps connected to this address. */
|
|
41
41
|
get grants(): readonly Grant[];
|
|
42
|
+
/**
|
|
43
|
+
* While the library connects again by itself after the connection broke:
|
|
44
|
+
* the next attempt and when it starts. `undefined` otherwise.
|
|
45
|
+
*/
|
|
46
|
+
get reconnecting(): ReconnectingInfo | undefined;
|
|
47
|
+
/** Who the relay knows as the page's user (identity binding). */
|
|
48
|
+
get identity(): IdentityState;
|
|
49
|
+
/** Why the relay refused the identity, while `identity` is `refused`. */
|
|
50
|
+
get identityMessage(): string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Ask the `identityAssertion` hook again and send the result, e.g. after
|
|
53
|
+
* the user signed in without a page load. Resolves `true` once the relay
|
|
54
|
+
* accepted it; `false` when nobody is signed in, it was refused, or the
|
|
55
|
+
* page is not connected.
|
|
56
|
+
*/
|
|
57
|
+
refreshIdentity(): Promise<boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* The page's user signed out: the relay ends every AI connection of this
|
|
60
|
+
* address at once (identity binding). Call it from the web app's logout.
|
|
61
|
+
* Returns `false` when it could not be sent (not connected); the grants
|
|
62
|
+
* then stay, but work only while a page signed in as the same person is
|
|
63
|
+
* open.
|
|
64
|
+
*/
|
|
65
|
+
signOut(): boolean;
|
|
42
66
|
/** Whether pairing is on (`startPairing()`). */
|
|
43
67
|
get pairing(): boolean;
|
|
44
68
|
/** The pairing code to show, if the relay sent one. */
|
|
@@ -48,7 +72,7 @@ export declare class TabMcp extends EventTarget {
|
|
|
48
72
|
* WebSocket, resolves with the session. Call it from a user action (a
|
|
49
73
|
* click): where browsers track user activation it is refused otherwise,
|
|
50
74
|
* unless `options.resume` says the user already chose to keep AI access on.
|
|
51
|
-
*
|
|
75
|
+
* While reconnecting (`reconnecting`), it makes the next attempt at once.
|
|
52
76
|
*/
|
|
53
77
|
connect(options?: ConnectOptions): Promise<SessionInfo>;
|
|
54
78
|
/** End the session (`bye`). Grants stay: AI apps work again after the next `connect()`. */
|
|
@@ -80,5 +104,11 @@ export declare class TabMcp extends EventTarget {
|
|
|
80
104
|
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
|
|
81
105
|
removeEventListener<K extends keyof TabMcpEventMap>(type: K, listener: Listener<K> | null, options?: boolean | EventListenerOptions): void;
|
|
82
106
|
removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
|
|
107
|
+
/**
|
|
108
|
+
* The library's own confirmation: the dialog in the page with Allow,
|
|
109
|
+
* Always allow and Deny (what runs without `onConfirm`). An `onConfirm`
|
|
110
|
+
* may hand some calls to it. Resolves `false` where there is no document.
|
|
111
|
+
*/
|
|
112
|
+
confirmInPage(request: ConfirmRequest): Promise<boolean>;
|
|
83
113
|
}
|
|
84
114
|
export {};
|
package/types.d.ts
CHANGED
|
@@ -110,9 +110,70 @@ export interface TabMcpOptions {
|
|
|
110
110
|
* only for the page's lifetime (a new address on every visit).
|
|
111
111
|
*/
|
|
112
112
|
storage?: SecretStorage | null;
|
|
113
|
-
/**
|
|
113
|
+
/**
|
|
114
|
+
* Storage key of the secret; default `tab-mcp:secret:<relay origin>:<server.name>`
|
|
115
|
+
* (the name URI-encoded), so pages of one site with different server names get
|
|
116
|
+
* different addresses. Changing `server.name` therefore changes the address: set
|
|
117
|
+
* `storageKey` to keep it. Without it, a secret kept under 0.4's default
|
|
118
|
+
* `tab-mcp:secret:<relay origin>` is copied to the new key by the first page that
|
|
119
|
+
* connects, so that page keeps its address.
|
|
120
|
+
*/
|
|
114
121
|
storageKey?: string;
|
|
122
|
+
/**
|
|
123
|
+
* Identity binding: returns a tab assertion from the web app's backend for
|
|
124
|
+
* the page's signed-in user and this MCP address (`request.address` is its
|
|
125
|
+
* `aud`), or `null` when nobody is signed in. Asked after every connect
|
|
126
|
+
* and by `refreshIdentity()`; only for relays whose site pairs by `oidc`.
|
|
127
|
+
*/
|
|
128
|
+
identityAssertion?: IdentityAssertionHandler;
|
|
129
|
+
/**
|
|
130
|
+
* How long to wait for the relay's answer to an assertion before
|
|
131
|
+
* `identity` goes back to `none`, in milliseconds; default 15000.
|
|
132
|
+
*/
|
|
133
|
+
identityTimeoutMs?: number;
|
|
134
|
+
/**
|
|
135
|
+
* Connect again by itself after the connection broke (reason `network`,
|
|
136
|
+
* `heartbeat` or `shutdown`): after 5, 10, 20 and 40 seconds, then every
|
|
137
|
+
* 60 seconds, and at once when the tab comes back to the front or the
|
|
138
|
+
* network returns. On by default; `false` leaves it to the page.
|
|
139
|
+
*/
|
|
140
|
+
reconnect?: boolean | ReconnectOptions;
|
|
115
141
|
}
|
|
142
|
+
/** Options of reconnecting (`TabMcpOptions.reconnect`). */
|
|
143
|
+
export interface ReconnectOptions {
|
|
144
|
+
/**
|
|
145
|
+
* The waits before each attempt in milliseconds; the last one repeats.
|
|
146
|
+
* Default `[5000, 10000, 20000, 40000, 60000]`.
|
|
147
|
+
*/
|
|
148
|
+
delaysMs?: readonly number[];
|
|
149
|
+
}
|
|
150
|
+
/** The library is reconnecting by itself (`reconnecting`). */
|
|
151
|
+
export interface ReconnectingInfo {
|
|
152
|
+
/** Why the connection ended. */
|
|
153
|
+
reason: DisconnectReason;
|
|
154
|
+
/** The number of the next (or running) attempt, from 1. */
|
|
155
|
+
attempt: number;
|
|
156
|
+
/** When that attempt starts, in milliseconds since the epoch (`Date.now()`). */
|
|
157
|
+
at: number;
|
|
158
|
+
}
|
|
159
|
+
/** What the page's identity hook gets. */
|
|
160
|
+
export interface IdentityAssertionRequest {
|
|
161
|
+
/** The MCP address: the assertion's `aud`. */
|
|
162
|
+
address: string;
|
|
163
|
+
/** The session the assertion is for. */
|
|
164
|
+
sessionId: string;
|
|
165
|
+
}
|
|
166
|
+
export type IdentityAssertionHandler = (request: IdentityAssertionRequest) => string | null | undefined | Promise<string | null | undefined>;
|
|
167
|
+
/** Who the relay knows as the page's user (identity binding). */
|
|
168
|
+
export type IdentityState =
|
|
169
|
+
/** No assertion was sent (not asked, signed out, or a `code` relay). */
|
|
170
|
+
"none"
|
|
171
|
+
/** Asked the hook or waiting for the relay. */
|
|
172
|
+
| "pending"
|
|
173
|
+
/** The relay accepted the assertion. */
|
|
174
|
+
| "bound"
|
|
175
|
+
/** The relay refused it (`message` says why). */
|
|
176
|
+
| "refused";
|
|
116
177
|
/** The session's limits, as the relay announces them. */
|
|
117
178
|
export interface Limits {
|
|
118
179
|
maxMessageBytes: number;
|
|
@@ -142,6 +203,8 @@ export interface SessionInfo {
|
|
|
142
203
|
origin: string;
|
|
143
204
|
/** The app name the relay shows when pairing, if any. */
|
|
144
205
|
appName?: string;
|
|
206
|
+
/** How AI apps pair: a code shown here, or the same sign-in (`oidc`). */
|
|
207
|
+
pairing: "code" | "oidc";
|
|
145
208
|
expiresAt: string;
|
|
146
209
|
limits: Limits;
|
|
147
210
|
}
|
|
@@ -155,7 +218,7 @@ export interface ConnectOptions {
|
|
|
155
218
|
/**
|
|
156
219
|
* Connect without a user action, for a page whose user turned on something
|
|
157
220
|
* like "Keep AI access on" earlier (with a click): e.g. on page load or
|
|
158
|
-
* after a `disconnected` event
|
|
221
|
+
* after a `disconnected` event without `reconnect`.
|
|
159
222
|
*/
|
|
160
223
|
resume?: boolean;
|
|
161
224
|
}
|
|
@@ -232,9 +295,19 @@ export interface TabMcpEventMap {
|
|
|
232
295
|
grantschange: CustomEvent<{
|
|
233
296
|
grants: readonly Grant[];
|
|
234
297
|
}>;
|
|
298
|
+
/** The relay accepted or refused the page's identity, or it was cleared. */
|
|
299
|
+
identitychange: CustomEvent<{
|
|
300
|
+
state: IdentityState;
|
|
301
|
+
message?: string;
|
|
302
|
+
}>;
|
|
235
303
|
toolschange: CustomEvent<{
|
|
236
304
|
tools: readonly string[];
|
|
237
305
|
}>;
|
|
306
|
+
/**
|
|
307
|
+
* Reconnecting by itself: the next attempt and when it starts, or `null`
|
|
308
|
+
* when it stopped (connected again, or `disconnect()`).
|
|
309
|
+
*/
|
|
310
|
+
reconnecting: CustomEvent<ReconnectingInfo | null>;
|
|
238
311
|
/** An AI app called a tool (before any confirmation). */
|
|
239
312
|
toolcall: CustomEvent<ToolCallStart>;
|
|
240
313
|
/** That call ended; also when the user declined it or the AI app cancelled it. */
|