@webmcp-auto-ui/core 2.5.36 → 2.5.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/multi-client.d.ts +0 -19
- package/dist/multi-client.d.ts.map +1 -1
- package/dist/multi-client.js +11 -72
- package/dist/multi-client.js.map +1 -1
- package/dist/webmcp-server.d.ts.map +1 -1
- package/dist/webmcp-server.js +7 -0
- package/dist/webmcp-server.js.map +1 -1
- package/package.json +1 -1
- package/src/index.d.ts +0 -2
- package/src/index.ts +0 -4
- package/src/multi-client.d.ts +0 -19
- package/src/multi-client.ts +11 -80
- package/src/webmcp-server.ts +5 -0
- package/dist/multi-mcp-bridge.d.ts +0 -61
- package/dist/multi-mcp-bridge.d.ts.map +0 -1
- package/dist/multi-mcp-bridge.js +0 -294
- package/dist/multi-mcp-bridge.js.map +0 -1
- package/src/multi-mcp-bridge.d.ts +0 -60
- package/src/multi-mcp-bridge.ts +0 -311
package/dist/multi-mcp-bridge.js
DELETED
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
// ---------------------------------------------------------------------------
|
|
2
|
-
// @webmcp-auto-ui/core — MultiMcpBridge
|
|
3
|
-
// Observes a canvas store with a `dataServers` field and reconciles the real
|
|
4
|
-
// MCP connection state with the user intent (`enabled`). Populates tools and
|
|
5
|
-
// recipes metadata back into the store. Framework-agnostic.
|
|
6
|
-
// ---------------------------------------------------------------------------
|
|
7
|
-
import { McpMultiClient } from './multi-client.js';
|
|
8
|
-
// ---------------------------------------------------------------------------
|
|
9
|
-
// Helpers
|
|
10
|
-
// ---------------------------------------------------------------------------
|
|
11
|
-
/**
|
|
12
|
-
* Extract recipes from an MCP tool response. Expects `res.content` as the
|
|
13
|
-
* standard MCP content array; looks for a text chunk whose payload parses as
|
|
14
|
-
* JSON and contains an array of `{ name, description?, body? }` items (or an
|
|
15
|
-
* object with an `items`/`recipes` array).
|
|
16
|
-
*/
|
|
17
|
-
export function parseRecipesFromToolResponse(res) {
|
|
18
|
-
if (!res || typeof res !== 'object')
|
|
19
|
-
return null;
|
|
20
|
-
const content = res.content;
|
|
21
|
-
if (!Array.isArray(content))
|
|
22
|
-
return null;
|
|
23
|
-
for (const chunk of content) {
|
|
24
|
-
if (!chunk || typeof chunk !== 'object')
|
|
25
|
-
continue;
|
|
26
|
-
if (chunk.type !== 'text' || typeof chunk.text !== 'string')
|
|
27
|
-
continue;
|
|
28
|
-
const text = chunk.text;
|
|
29
|
-
let parsed;
|
|
30
|
-
try {
|
|
31
|
-
parsed = JSON.parse(text);
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const items = extractItems(parsed);
|
|
37
|
-
if (items)
|
|
38
|
-
return items;
|
|
39
|
-
}
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
function extractItems(parsed) {
|
|
43
|
-
const candidate = Array.isArray(parsed)
|
|
44
|
-
? parsed
|
|
45
|
-
: Array.isArray(parsed?.items)
|
|
46
|
-
? parsed.items
|
|
47
|
-
: Array.isArray(parsed?.recipes)
|
|
48
|
-
? parsed.recipes
|
|
49
|
-
: null;
|
|
50
|
-
if (!candidate)
|
|
51
|
-
return null;
|
|
52
|
-
const out = [];
|
|
53
|
-
for (const it of candidate) {
|
|
54
|
-
if (!it || typeof it !== 'object')
|
|
55
|
-
continue;
|
|
56
|
-
const name = typeof it.name === 'string' ? it.name : typeof it.id === 'string' ? it.id : null;
|
|
57
|
-
if (!name)
|
|
58
|
-
continue;
|
|
59
|
-
const entry = { name };
|
|
60
|
-
if (typeof it.description === 'string')
|
|
61
|
-
entry.description = it.description;
|
|
62
|
-
if (typeof it.body === 'string')
|
|
63
|
-
entry.body = it.body;
|
|
64
|
-
out.push(entry);
|
|
65
|
-
}
|
|
66
|
-
return out.length > 0 ? out : null;
|
|
67
|
-
}
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
// MultiMcpBridge
|
|
70
|
-
// ---------------------------------------------------------------------------
|
|
71
|
-
export class MultiMcpBridge {
|
|
72
|
-
client;
|
|
73
|
-
unsub = null;
|
|
74
|
-
/** server name -> url (for reverse lookup, since McpMultiClient keys by url) */
|
|
75
|
-
nameToUrl = new Map();
|
|
76
|
-
/** server names currently connected */
|
|
77
|
-
connected = new Set();
|
|
78
|
-
/** server names whose handshake is in-flight */
|
|
79
|
-
connecting = new Set();
|
|
80
|
-
options;
|
|
81
|
-
started = false;
|
|
82
|
-
constructor(options) {
|
|
83
|
-
this.options = options;
|
|
84
|
-
this.client = new McpMultiClient();
|
|
85
|
-
}
|
|
86
|
-
// -------------------------------------------------------------------------
|
|
87
|
-
// Lifecycle
|
|
88
|
-
// -------------------------------------------------------------------------
|
|
89
|
-
start() {
|
|
90
|
-
if (this.started)
|
|
91
|
-
return;
|
|
92
|
-
const canvas = this.options.getCanvas();
|
|
93
|
-
if (canvas && typeof canvas.subscribe === 'function') {
|
|
94
|
-
this.started = true;
|
|
95
|
-
this.unsub = canvas.subscribe(() => {
|
|
96
|
-
void this.reconcile();
|
|
97
|
-
});
|
|
98
|
-
void this.reconcile();
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
// Canvas not ready yet — retry shortly. Without this the bridge would
|
|
102
|
-
// stay dead forever because no subscription was ever established.
|
|
103
|
-
setTimeout(() => this.start(), 50);
|
|
104
|
-
}
|
|
105
|
-
stop() {
|
|
106
|
-
if (!this.started)
|
|
107
|
-
return;
|
|
108
|
-
this.started = false;
|
|
109
|
-
if (this.unsub) {
|
|
110
|
-
try {
|
|
111
|
-
this.unsub();
|
|
112
|
-
}
|
|
113
|
-
catch { /* ignore */ }
|
|
114
|
-
this.unsub = null;
|
|
115
|
-
}
|
|
116
|
-
void this.client.disconnectAll().catch(() => { });
|
|
117
|
-
this.connected.clear();
|
|
118
|
-
this.connecting.clear();
|
|
119
|
-
this.nameToUrl.clear();
|
|
120
|
-
}
|
|
121
|
-
// -------------------------------------------------------------------------
|
|
122
|
-
// Imperative helpers
|
|
123
|
-
// -------------------------------------------------------------------------
|
|
124
|
-
/** Ensure a server is in the store with enabled=true; reconcile picks it up. */
|
|
125
|
-
async connect(name, url) {
|
|
126
|
-
const canvas = this.options.getCanvas();
|
|
127
|
-
if (!canvas)
|
|
128
|
-
return;
|
|
129
|
-
canvas.addDataServer?.({ name, url });
|
|
130
|
-
canvas.setDataServerEnabled?.(name, true);
|
|
131
|
-
await this.reconcile();
|
|
132
|
-
}
|
|
133
|
-
/** Call a tool on a named server. */
|
|
134
|
-
async callTool(serverName, toolName, args) {
|
|
135
|
-
const url = this.nameToUrl.get(serverName);
|
|
136
|
-
if (!url)
|
|
137
|
-
throw new Error(`MultiMcpBridge: server "${serverName}" is not connected`);
|
|
138
|
-
return this.client.callToolOn(url, toolName, (args ?? {}));
|
|
139
|
-
}
|
|
140
|
-
/** Direct access to the underlying multi-client (read-only usage preferred). */
|
|
141
|
-
get multiClient() {
|
|
142
|
-
return this.client;
|
|
143
|
-
}
|
|
144
|
-
/** True if a server with this name has completed its handshake. */
|
|
145
|
-
hasServer(serverName) {
|
|
146
|
-
return this.connected.has(serverName);
|
|
147
|
-
}
|
|
148
|
-
/** Snapshot of currently connected server names. */
|
|
149
|
-
connectedServers() {
|
|
150
|
-
return Array.from(this.connected);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Wait until every enabled data server in the canvas is connected, or the
|
|
154
|
-
* timeout elapses. Resolves either way (no throw) — caller inspects
|
|
155
|
-
* `connectedServers()` to decide what's reachable.
|
|
156
|
-
*/
|
|
157
|
-
async waitForEnabledServers(timeoutMs = 5000) {
|
|
158
|
-
const canvas = this.options.getCanvas();
|
|
159
|
-
if (!canvas)
|
|
160
|
-
return;
|
|
161
|
-
const deadline = Date.now() + Math.max(0, timeoutMs);
|
|
162
|
-
while (Date.now() < deadline) {
|
|
163
|
-
const enabled = (Array.isArray(canvas.dataServers) ? canvas.dataServers : [])
|
|
164
|
-
.filter((s) => s?.enabled !== false)
|
|
165
|
-
.map((s) => s.name);
|
|
166
|
-
if (enabled.length === 0)
|
|
167
|
-
return;
|
|
168
|
-
const allReady = enabled.every((n) => this.connected.has(n));
|
|
169
|
-
if (allReady)
|
|
170
|
-
return;
|
|
171
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
// -------------------------------------------------------------------------
|
|
175
|
-
// Reconciliation
|
|
176
|
-
// -------------------------------------------------------------------------
|
|
177
|
-
async reconcile() {
|
|
178
|
-
const canvas = this.options.getCanvas();
|
|
179
|
-
if (!canvas)
|
|
180
|
-
return;
|
|
181
|
-
const servers = (canvas.dataServers ?? []);
|
|
182
|
-
if (!Array.isArray(servers))
|
|
183
|
-
return;
|
|
184
|
-
const seenNames = new Set();
|
|
185
|
-
for (const srv of servers) {
|
|
186
|
-
if (!srv || typeof srv.name !== 'string' || typeof srv.url !== 'string')
|
|
187
|
-
continue;
|
|
188
|
-
// Empty URL means a legacy placeholder entry (see canvas.ensurePrimary).
|
|
189
|
-
// Handshaking with '' resolves `fetch('')` against the current page origin,
|
|
190
|
-
// producing a POST storm on the app root (405 loop).
|
|
191
|
-
if (srv.url === '')
|
|
192
|
-
continue;
|
|
193
|
-
seenNames.add(srv.name);
|
|
194
|
-
const key = srv.name;
|
|
195
|
-
if (srv.enabled && !this.connected.has(key) && !this.connecting.has(key)) {
|
|
196
|
-
// Mark as connecting synchronously before the async handshake runs,
|
|
197
|
-
// so a concurrent reconcile() can't slip past the guard and spawn
|
|
198
|
-
// a second handshake for the same server.
|
|
199
|
-
this.connecting.add(key);
|
|
200
|
-
void this.handshake(srv);
|
|
201
|
-
}
|
|
202
|
-
else if (!srv.enabled && this.connected.has(key)) {
|
|
203
|
-
void this.disconnect(srv);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
// Disconnect servers that were removed from the store entirely
|
|
207
|
-
for (const name of Array.from(this.connected)) {
|
|
208
|
-
if (!seenNames.has(name)) {
|
|
209
|
-
void this.disconnect({ name, url: this.nameToUrl.get(name) ?? '' });
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
async handshake(srv) {
|
|
214
|
-
const canvas = this.options.getCanvas();
|
|
215
|
-
// connecting.add is performed by reconcile() synchronously; don't re-add here.
|
|
216
|
-
this.options.log?.(`[bridge] handshake start: ${srv.name}`, { url: srv.url });
|
|
217
|
-
try {
|
|
218
|
-
const { name: actualName, tools } = await this.client.addServer(srv.url);
|
|
219
|
-
// The MCP server may return a different name than the one stored in the
|
|
220
|
-
// canvas. Key the bridge by the canvas name so callers stay consistent.
|
|
221
|
-
this.nameToUrl.set(srv.name, srv.url);
|
|
222
|
-
this.connected.add(srv.name);
|
|
223
|
-
// Try to fetch recipes via tool `list_recipes` if exposed.
|
|
224
|
-
let recipes = [];
|
|
225
|
-
const hasListRecipes = tools.some((t) => t.name === 'list_recipes');
|
|
226
|
-
if (hasListRecipes) {
|
|
227
|
-
try {
|
|
228
|
-
const res = await this.client.callToolOn(srv.url, 'list_recipes', {});
|
|
229
|
-
recipes = parseRecipesFromToolResponse(res) ?? [];
|
|
230
|
-
}
|
|
231
|
-
catch (err) {
|
|
232
|
-
this.options.log?.(`[bridge] list_recipes failed for ${srv.name}`, err);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
canvas.setDataServerMeta?.(srv.name, {
|
|
236
|
-
connected: true,
|
|
237
|
-
tools,
|
|
238
|
-
recipes,
|
|
239
|
-
error: undefined,
|
|
240
|
-
serverName: actualName,
|
|
241
|
-
});
|
|
242
|
-
this.options.log?.(`[bridge] connected: ${srv.name}`, { tools: tools.length, recipes: recipes.length });
|
|
243
|
-
}
|
|
244
|
-
catch (err) {
|
|
245
|
-
const message = err?.message ? String(err.message) : String(err);
|
|
246
|
-
canvas.setDataServerMeta?.(srv.name, {
|
|
247
|
-
connected: false,
|
|
248
|
-
tools: [],
|
|
249
|
-
recipes: [],
|
|
250
|
-
error: message,
|
|
251
|
-
});
|
|
252
|
-
this.options.log?.(`[bridge] handshake failed: ${srv.name}`, message);
|
|
253
|
-
}
|
|
254
|
-
finally {
|
|
255
|
-
this.connecting.delete(srv.name);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
async disconnect(srv) {
|
|
259
|
-
const canvas = this.options.getCanvas();
|
|
260
|
-
const url = srv.url || this.nameToUrl.get(srv.name);
|
|
261
|
-
if (url) {
|
|
262
|
-
try {
|
|
263
|
-
await this.client.removeServer(url);
|
|
264
|
-
}
|
|
265
|
-
catch { /* ignore */ }
|
|
266
|
-
}
|
|
267
|
-
this.connected.delete(srv.name);
|
|
268
|
-
this.nameToUrl.delete(srv.name);
|
|
269
|
-
canvas?.setDataServerMeta?.(srv.name, { connected: false });
|
|
270
|
-
this.options.log?.(`[bridge] disconnected: ${srv.name}`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
// ---------------------------------------------------------------------------
|
|
274
|
-
// Singleton installer
|
|
275
|
-
// ---------------------------------------------------------------------------
|
|
276
|
-
/**
|
|
277
|
-
* Install a singleton bridge on globalThis.__multiMcp. If a previous bridge
|
|
278
|
-
* exists, it is stopped first (idempotent).
|
|
279
|
-
*/
|
|
280
|
-
export function installMultiMcpBridge(options) {
|
|
281
|
-
const g = globalThis;
|
|
282
|
-
const existing = g.__multiMcp;
|
|
283
|
-
if (existing && typeof existing.stop === 'function') {
|
|
284
|
-
try {
|
|
285
|
-
existing.stop();
|
|
286
|
-
}
|
|
287
|
-
catch { /* ignore */ }
|
|
288
|
-
}
|
|
289
|
-
const bridge = new MultiMcpBridge(options);
|
|
290
|
-
g.__multiMcp = bridge;
|
|
291
|
-
bridge.start();
|
|
292
|
-
return bridge;
|
|
293
|
-
}
|
|
294
|
-
//# sourceMappingURL=multi-mcp-bridge.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":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,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { McpMultiClient } from './multi-client.js';
|
|
2
|
-
import type { McpToolResult } from './types.js';
|
|
3
|
-
export interface MultiMcpBridgeOptions {
|
|
4
|
-
/** Accessor for the canvas store. Typically returns globalThis.__canvasVanilla. */
|
|
5
|
-
getCanvas: () => any;
|
|
6
|
-
/** Optional logger. */
|
|
7
|
-
log?: (msg: string, data?: any) => void;
|
|
8
|
-
}
|
|
9
|
-
interface RecipeItem {
|
|
10
|
-
name: string;
|
|
11
|
-
description?: string;
|
|
12
|
-
body?: string;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Extract recipes from an MCP tool response. Expects `res.content` as the
|
|
16
|
-
* standard MCP content array; looks for a text chunk whose payload parses as
|
|
17
|
-
* JSON and contains an array of `{ name, description?, body? }` items (or an
|
|
18
|
-
* object with an `items`/`recipes` array).
|
|
19
|
-
*/
|
|
20
|
-
export declare function parseRecipesFromToolResponse(res: unknown): RecipeItem[] | null;
|
|
21
|
-
export declare class MultiMcpBridge {
|
|
22
|
-
private client;
|
|
23
|
-
private unsub;
|
|
24
|
-
/** server name -> url (for reverse lookup, since McpMultiClient keys by url) */
|
|
25
|
-
private nameToUrl;
|
|
26
|
-
/** server names currently connected */
|
|
27
|
-
private connected;
|
|
28
|
-
/** server names whose handshake is in-flight */
|
|
29
|
-
private connecting;
|
|
30
|
-
private options;
|
|
31
|
-
private started;
|
|
32
|
-
constructor(options: MultiMcpBridgeOptions);
|
|
33
|
-
start(): void;
|
|
34
|
-
stop(): void;
|
|
35
|
-
/** Ensure a server is in the store with enabled=true; reconcile picks it up. */
|
|
36
|
-
connect(name: string, url: string): Promise<void>;
|
|
37
|
-
/** Call a tool on a named server. */
|
|
38
|
-
callTool(serverName: string, toolName: string, args: unknown): Promise<McpToolResult>;
|
|
39
|
-
/** Direct access to the underlying multi-client (read-only usage preferred). */
|
|
40
|
-
get multiClient(): McpMultiClient;
|
|
41
|
-
/** True if a server with this name has completed its handshake. */
|
|
42
|
-
hasServer(serverName: string): boolean;
|
|
43
|
-
/** Snapshot of currently connected server names. */
|
|
44
|
-
connectedServers(): string[];
|
|
45
|
-
/**
|
|
46
|
-
* Wait until every enabled data server in the canvas is connected, or the
|
|
47
|
-
* timeout elapses. Resolves either way (no throw) — caller inspects
|
|
48
|
-
* `connectedServers()` to decide what's reachable.
|
|
49
|
-
*/
|
|
50
|
-
waitForEnabledServers(timeoutMs?: number): Promise<void>;
|
|
51
|
-
private reconcile;
|
|
52
|
-
private handshake;
|
|
53
|
-
private disconnect;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Install a singleton bridge on globalThis.__multiMcp. If a previous bridge
|
|
57
|
-
* exists, it is stopped first (idempotent).
|
|
58
|
-
*/
|
|
59
|
-
export declare function installMultiMcpBridge(options: MultiMcpBridgeOptions): MultiMcpBridge;
|
|
60
|
-
export {};
|
package/src/multi-mcp-bridge.ts
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
// ---------------------------------------------------------------------------
|
|
2
|
-
// @webmcp-auto-ui/core — MultiMcpBridge
|
|
3
|
-
// Observes a canvas store with a `dataServers` field and reconciles the real
|
|
4
|
-
// MCP connection state with the user intent (`enabled`). Populates tools and
|
|
5
|
-
// recipes metadata back into the store. Framework-agnostic.
|
|
6
|
-
// ---------------------------------------------------------------------------
|
|
7
|
-
|
|
8
|
-
import { McpMultiClient } from './multi-client.js';
|
|
9
|
-
import type { McpTool, McpToolResult } from './types.js';
|
|
10
|
-
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
// Types
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
|
|
15
|
-
export interface MultiMcpBridgeOptions {
|
|
16
|
-
/** Accessor for the canvas store. Typically returns globalThis.__canvasVanilla. */
|
|
17
|
-
getCanvas: () => any;
|
|
18
|
-
/** Optional logger. */
|
|
19
|
-
log?: (msg: string, data?: any) => void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
interface DataServerLike {
|
|
23
|
-
name: string;
|
|
24
|
-
url: string;
|
|
25
|
-
enabled?: boolean;
|
|
26
|
-
connected?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface RecipeItem {
|
|
30
|
-
name: string;
|
|
31
|
-
description?: string;
|
|
32
|
-
body?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
// Helpers
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Extract recipes from an MCP tool response. Expects `res.content` as the
|
|
41
|
-
* standard MCP content array; looks for a text chunk whose payload parses as
|
|
42
|
-
* JSON and contains an array of `{ name, description?, body? }` items (or an
|
|
43
|
-
* object with an `items`/`recipes` array).
|
|
44
|
-
*/
|
|
45
|
-
export function parseRecipesFromToolResponse(res: unknown): RecipeItem[] | null {
|
|
46
|
-
if (!res || typeof res !== 'object') return null;
|
|
47
|
-
const content = (res as any).content;
|
|
48
|
-
if (!Array.isArray(content)) return null;
|
|
49
|
-
|
|
50
|
-
for (const chunk of content) {
|
|
51
|
-
if (!chunk || typeof chunk !== 'object') continue;
|
|
52
|
-
if (chunk.type !== 'text' || typeof chunk.text !== 'string') continue;
|
|
53
|
-
const text: string = chunk.text;
|
|
54
|
-
let parsed: any;
|
|
55
|
-
try {
|
|
56
|
-
parsed = JSON.parse(text);
|
|
57
|
-
} catch {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
const items = extractItems(parsed);
|
|
61
|
-
if (items) return items;
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function extractItems(parsed: any): RecipeItem[] | null {
|
|
67
|
-
const candidate = Array.isArray(parsed)
|
|
68
|
-
? parsed
|
|
69
|
-
: Array.isArray(parsed?.items)
|
|
70
|
-
? parsed.items
|
|
71
|
-
: Array.isArray(parsed?.recipes)
|
|
72
|
-
? parsed.recipes
|
|
73
|
-
: null;
|
|
74
|
-
if (!candidate) return null;
|
|
75
|
-
const out: RecipeItem[] = [];
|
|
76
|
-
for (const it of candidate) {
|
|
77
|
-
if (!it || typeof it !== 'object') continue;
|
|
78
|
-
const name = typeof it.name === 'string' ? it.name : typeof it.id === 'string' ? it.id : null;
|
|
79
|
-
if (!name) continue;
|
|
80
|
-
const entry: RecipeItem = { name };
|
|
81
|
-
if (typeof it.description === 'string') entry.description = it.description;
|
|
82
|
-
if (typeof it.body === 'string') entry.body = it.body;
|
|
83
|
-
out.push(entry);
|
|
84
|
-
}
|
|
85
|
-
return out.length > 0 ? out : null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// ---------------------------------------------------------------------------
|
|
89
|
-
// MultiMcpBridge
|
|
90
|
-
// ---------------------------------------------------------------------------
|
|
91
|
-
|
|
92
|
-
export class MultiMcpBridge {
|
|
93
|
-
private client: McpMultiClient;
|
|
94
|
-
private unsub: (() => void) | null = null;
|
|
95
|
-
/** server name -> url (for reverse lookup, since McpMultiClient keys by url) */
|
|
96
|
-
private nameToUrl = new Map<string, string>();
|
|
97
|
-
/** server names currently connected */
|
|
98
|
-
private connected = new Set<string>();
|
|
99
|
-
/** server names whose handshake is in-flight */
|
|
100
|
-
private connecting = new Set<string>();
|
|
101
|
-
private options: MultiMcpBridgeOptions;
|
|
102
|
-
private started = false;
|
|
103
|
-
|
|
104
|
-
constructor(options: MultiMcpBridgeOptions) {
|
|
105
|
-
this.options = options;
|
|
106
|
-
this.client = new McpMultiClient();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// -------------------------------------------------------------------------
|
|
110
|
-
// Lifecycle
|
|
111
|
-
// -------------------------------------------------------------------------
|
|
112
|
-
|
|
113
|
-
start(): void {
|
|
114
|
-
if (this.started) return;
|
|
115
|
-
const canvas = this.options.getCanvas();
|
|
116
|
-
if (canvas && typeof canvas.subscribe === 'function') {
|
|
117
|
-
this.started = true;
|
|
118
|
-
this.unsub = canvas.subscribe(() => {
|
|
119
|
-
void this.reconcile();
|
|
120
|
-
});
|
|
121
|
-
void this.reconcile();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
// Canvas not ready yet — retry shortly. Without this the bridge would
|
|
125
|
-
// stay dead forever because no subscription was ever established.
|
|
126
|
-
setTimeout(() => this.start(), 50);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
stop(): void {
|
|
130
|
-
if (!this.started) return;
|
|
131
|
-
this.started = false;
|
|
132
|
-
if (this.unsub) {
|
|
133
|
-
try { this.unsub(); } catch { /* ignore */ }
|
|
134
|
-
this.unsub = null;
|
|
135
|
-
}
|
|
136
|
-
void this.client.disconnectAll().catch(() => { /* ignore */ });
|
|
137
|
-
this.connected.clear();
|
|
138
|
-
this.connecting.clear();
|
|
139
|
-
this.nameToUrl.clear();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// -------------------------------------------------------------------------
|
|
143
|
-
// Imperative helpers
|
|
144
|
-
// -------------------------------------------------------------------------
|
|
145
|
-
|
|
146
|
-
/** Ensure a server is in the store with enabled=true; reconcile picks it up. */
|
|
147
|
-
async connect(name: string, url: string): Promise<void> {
|
|
148
|
-
const canvas = this.options.getCanvas();
|
|
149
|
-
if (!canvas) return;
|
|
150
|
-
canvas.addDataServer?.({ name, url });
|
|
151
|
-
canvas.setDataServerEnabled?.(name, true);
|
|
152
|
-
await this.reconcile();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Call a tool on a named server. */
|
|
156
|
-
async callTool(serverName: string, toolName: string, args: unknown): Promise<McpToolResult> {
|
|
157
|
-
const url = this.nameToUrl.get(serverName);
|
|
158
|
-
if (!url) throw new Error(`MultiMcpBridge: server "${serverName}" is not connected`);
|
|
159
|
-
return this.client.callToolOn(url, toolName, (args ?? {}) as Record<string, unknown>);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** Direct access to the underlying multi-client (read-only usage preferred). */
|
|
163
|
-
get multiClient(): McpMultiClient {
|
|
164
|
-
return this.client;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** True if a server with this name has completed its handshake. */
|
|
168
|
-
hasServer(serverName: string): boolean {
|
|
169
|
-
return this.connected.has(serverName);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Snapshot of currently connected server names. */
|
|
173
|
-
connectedServers(): string[] {
|
|
174
|
-
return Array.from(this.connected);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Wait until every enabled data server in the canvas is connected, or the
|
|
179
|
-
* timeout elapses. Resolves either way (no throw) — caller inspects
|
|
180
|
-
* `connectedServers()` to decide what's reachable.
|
|
181
|
-
*/
|
|
182
|
-
async waitForEnabledServers(timeoutMs = 5000): Promise<void> {
|
|
183
|
-
const canvas = this.options.getCanvas();
|
|
184
|
-
if (!canvas) return;
|
|
185
|
-
const deadline = Date.now() + Math.max(0, timeoutMs);
|
|
186
|
-
while (Date.now() < deadline) {
|
|
187
|
-
const enabled = (Array.isArray(canvas.dataServers) ? canvas.dataServers : [])
|
|
188
|
-
.filter((s: { enabled?: boolean }) => s?.enabled !== false)
|
|
189
|
-
.map((s: { name: string }) => s.name);
|
|
190
|
-
if (enabled.length === 0) return;
|
|
191
|
-
const allReady = enabled.every((n: string) => this.connected.has(n));
|
|
192
|
-
if (allReady) return;
|
|
193
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// -------------------------------------------------------------------------
|
|
198
|
-
// Reconciliation
|
|
199
|
-
// -------------------------------------------------------------------------
|
|
200
|
-
|
|
201
|
-
private async reconcile(): Promise<void> {
|
|
202
|
-
const canvas = this.options.getCanvas();
|
|
203
|
-
if (!canvas) return;
|
|
204
|
-
const servers = (canvas.dataServers ?? []) as DataServerLike[];
|
|
205
|
-
if (!Array.isArray(servers)) return;
|
|
206
|
-
|
|
207
|
-
const seenNames = new Set<string>();
|
|
208
|
-
for (const srv of servers) {
|
|
209
|
-
if (!srv || typeof srv.name !== 'string' || typeof srv.url !== 'string') continue;
|
|
210
|
-
// Empty URL means a legacy placeholder entry (see canvas.ensurePrimary).
|
|
211
|
-
// Handshaking with '' resolves `fetch('')` against the current page origin,
|
|
212
|
-
// producing a POST storm on the app root (405 loop).
|
|
213
|
-
if (srv.url === '') continue;
|
|
214
|
-
seenNames.add(srv.name);
|
|
215
|
-
const key = srv.name;
|
|
216
|
-
if (srv.enabled && !this.connected.has(key) && !this.connecting.has(key)) {
|
|
217
|
-
// Mark as connecting synchronously before the async handshake runs,
|
|
218
|
-
// so a concurrent reconcile() can't slip past the guard and spawn
|
|
219
|
-
// a second handshake for the same server.
|
|
220
|
-
this.connecting.add(key);
|
|
221
|
-
void this.handshake(srv);
|
|
222
|
-
} else if (!srv.enabled && this.connected.has(key)) {
|
|
223
|
-
void this.disconnect(srv);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Disconnect servers that were removed from the store entirely
|
|
228
|
-
for (const name of Array.from(this.connected)) {
|
|
229
|
-
if (!seenNames.has(name)) {
|
|
230
|
-
void this.disconnect({ name, url: this.nameToUrl.get(name) ?? '' });
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private async handshake(srv: DataServerLike): Promise<void> {
|
|
236
|
-
const canvas = this.options.getCanvas();
|
|
237
|
-
// connecting.add is performed by reconcile() synchronously; don't re-add here.
|
|
238
|
-
this.options.log?.(`[bridge] handshake start: ${srv.name}`, { url: srv.url });
|
|
239
|
-
try {
|
|
240
|
-
const { name: actualName, tools } = await this.client.addServer(srv.url);
|
|
241
|
-
// The MCP server may return a different name than the one stored in the
|
|
242
|
-
// canvas. Key the bridge by the canvas name so callers stay consistent.
|
|
243
|
-
this.nameToUrl.set(srv.name, srv.url);
|
|
244
|
-
this.connected.add(srv.name);
|
|
245
|
-
|
|
246
|
-
// Try to fetch recipes via tool `list_recipes` if exposed.
|
|
247
|
-
let recipes: RecipeItem[] = [];
|
|
248
|
-
const hasListRecipes = tools.some((t: McpTool) => t.name === 'list_recipes');
|
|
249
|
-
if (hasListRecipes) {
|
|
250
|
-
try {
|
|
251
|
-
const res = await this.client.callToolOn(srv.url, 'list_recipes', {});
|
|
252
|
-
recipes = parseRecipesFromToolResponse(res) ?? [];
|
|
253
|
-
} catch (err) {
|
|
254
|
-
this.options.log?.(`[bridge] list_recipes failed for ${srv.name}`, err);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
canvas.setDataServerMeta?.(srv.name, {
|
|
259
|
-
connected: true,
|
|
260
|
-
tools,
|
|
261
|
-
recipes,
|
|
262
|
-
error: undefined,
|
|
263
|
-
serverName: actualName,
|
|
264
|
-
});
|
|
265
|
-
this.options.log?.(`[bridge] connected: ${srv.name}`, { tools: tools.length, recipes: recipes.length });
|
|
266
|
-
} catch (err: any) {
|
|
267
|
-
const message = err?.message ? String(err.message) : String(err);
|
|
268
|
-
canvas.setDataServerMeta?.(srv.name, {
|
|
269
|
-
connected: false,
|
|
270
|
-
tools: [],
|
|
271
|
-
recipes: [],
|
|
272
|
-
error: message,
|
|
273
|
-
});
|
|
274
|
-
this.options.log?.(`[bridge] handshake failed: ${srv.name}`, message);
|
|
275
|
-
} finally {
|
|
276
|
-
this.connecting.delete(srv.name);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
private async disconnect(srv: { name: string; url: string }): Promise<void> {
|
|
281
|
-
const canvas = this.options.getCanvas();
|
|
282
|
-
const url = srv.url || this.nameToUrl.get(srv.name);
|
|
283
|
-
if (url) {
|
|
284
|
-
try { await this.client.removeServer(url); } catch { /* ignore */ }
|
|
285
|
-
}
|
|
286
|
-
this.connected.delete(srv.name);
|
|
287
|
-
this.nameToUrl.delete(srv.name);
|
|
288
|
-
canvas?.setDataServerMeta?.(srv.name, { connected: false });
|
|
289
|
-
this.options.log?.(`[bridge] disconnected: ${srv.name}`);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// ---------------------------------------------------------------------------
|
|
294
|
-
// Singleton installer
|
|
295
|
-
// ---------------------------------------------------------------------------
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Install a singleton bridge on globalThis.__multiMcp. If a previous bridge
|
|
299
|
-
* exists, it is stopped first (idempotent).
|
|
300
|
-
*/
|
|
301
|
-
export function installMultiMcpBridge(options: MultiMcpBridgeOptions): MultiMcpBridge {
|
|
302
|
-
const g = globalThis as any;
|
|
303
|
-
const existing = g.__multiMcp;
|
|
304
|
-
if (existing && typeof existing.stop === 'function') {
|
|
305
|
-
try { existing.stop(); } catch { /* ignore */ }
|
|
306
|
-
}
|
|
307
|
-
const bridge = new MultiMcpBridge(options);
|
|
308
|
-
g.__multiMcp = bridge;
|
|
309
|
-
bridge.start();
|
|
310
|
-
return bridge;
|
|
311
|
-
}
|