@assistant-ui/react-mcp 0.0.16 → 0.0.17

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.
@@ -19,12 +19,14 @@ type MCPConnector = {
19
19
  url: string;
20
20
  icon?: string | undefined;
21
21
  auth: MCPAuthConfig;
22
+ connectionTimeout?: number | undefined;
22
23
  };
23
24
  type MCPCustomServerRecord = {
24
25
  id: string;
25
26
  name: string;
26
27
  url: string;
27
28
  auth: MCPAuthConfig;
29
+ connectionTimeout?: number | undefined;
28
30
  createdAt: number;
29
31
  };
30
32
  type MCPServerKind = "connector" | "custom";
@@ -84,6 +86,7 @@ type MCPManagerMethods = {
84
86
  name: string;
85
87
  url: string;
86
88
  auth: MCPAuthConfig;
89
+ connectionTimeout?: number | undefined;
87
90
  }) => Promise<string>;
88
91
  removeServer: (id: string) => Promise<void>;
89
92
  };
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-scope.d.ts","names":[],"sources":["../src/mcp-scope.ts"],"mappings":";KAEY,aAAA;EACN,IAAA;AAAA;EACA,IAAA;EAAgB,KAAA;AAAA;EAEhB,IAAA;EACA,MAAA;EACA,qBAAA;EACA,aAAA;EACA,oBAAA;EACA,QAAA;EACA,YAAA;AAAA;AAAA,KAGM,YAAA;EACV,EAAA;EACA,IAAA;EACA,GAAA;EACA,IAAA;EACA,IAAA,EAAM,aAAa;AAAA;AAAA,KAGT,qBAAA;EACV,EAAA;EACA,IAAA;EACA,GAAA;EACA,IAAA,EAAM,aAAa;EACnB,SAAA;AAAA;AAAA,KAGU,aAAA;AAAA,KAEA,kBAAA;AAAA,KAQA,WAAA;EACV,IAAA;EACA,WAAA;EACA,WAAA;AAAA;AAAA,KAGU,cAAA;EACV,EAAA;EACA,IAAA,EAAM,aAAA;EACN,IAAA;EACA,GAAA;EACA,IAAA;EACA,eAAA,EAAiB,kBAAA;EACjB,SAAA;IAAa,OAAA;EAAA;EACb,KAAA,EAAO,WAAA;EACP,gBAAA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,IAAA,EAAM,aAAA;IACN,iBAAA;EAAA,MACI,OAAA;EACN,YAAA,GAAe,EAAA,aAAe,OAAA;AAAA;AAAA;EAAA,UAIpB,aAAA;IACR,GAAA;MACE,OAAA,EAAS,iBAAA;IAAA;IAEX,SAAA;MACE,OAAA,EAAS,gBAAA;MACT,IAAA;QAAQ,MAAA;QAAe,KAAA,EAAO,cAAA;MAAA;IAAA;EAAA;AAAA"}
@@ -7,7 +7,8 @@ type McpManagerResourceProps = {
7
7
  connectors?: MCPConnector[] | undefined;
8
8
  storage?: MCPStorageElement | undefined; /** OAuth redirect target. Defaults to `${origin}/mcp/callback`. */
9
9
  oauthRedirectUri?: string | undefined; /** Connect on mount when usable auth exists. Default true. */
10
- autoConnect?: boolean | undefined;
10
+ autoConnect?: boolean | undefined; /** Optional timeout in milliseconds for connect/listTools calls. Disabled by default. */
11
+ connectionTimeout?: number | undefined;
11
12
  };
12
13
  declare const McpManagerResource: import("@assistant-ui/tap").Resource<ClientOutput<"mcp">, [props: McpManagerResourceProps]>;
13
14
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"McpManagerResource.d.ts","names":[],"sources":["../../src/resources/McpManagerResource.ts"],"mappings":";;;;;KAoBY,uBAAA;EACV,UAAA,GAAa,YAAA;EACb,OAAA,GAAU,iBAAiB,cAFM;EAIjC,gBAAA,uBAF2B;EAI3B,WAAA;AAAA;AAAA,cA4NW,kBAAA,8BAAkB,QAAA,CAAA,YAAA,UAAA,KAAA,EAAA,uBAAA"}
1
+ {"version":3,"file":"McpManagerResource.d.ts","names":[],"sources":["../../src/resources/McpManagerResource.ts"],"mappings":";;;;;KAqBY,uBAAA;EACV,UAAA,GAAa,YAAA;EACb,OAAA,GAAU,iBAAiB,cAFM;EAIjC,gBAAA,uBAF2B;EAI3B,WAAA,wBALa;EAOb,iBAAA;AAAA;AAAA,cA4OW,kBAAA,8BAAkB,QAAA,CAAA,YAAA,UAAA,KAAA,EAAA,uBAAA"}
@@ -1,3 +1,4 @@
1
+ import { assertUniqueServerIds } from "../utils/serverId.js";
1
2
  import { McpServerResource } from "./McpServerResource.js";
2
3
  import { McpLocalStorage } from "./storage/McpLocalStorage.js";
3
4
  import { attachTransformScopes, useAssistantClientRef, useClientLookup } from "@assistant-ui/store";
@@ -12,7 +13,7 @@ function defaultRedirectUri() {
12
13
  }
13
14
  const NO_CONNECTORS = [];
14
15
  const useMcpManagerResource = (props) => {
15
- const $ = c(69);
16
+ const $ = c(72);
16
17
  const connectors = props.connectors ?? NO_CONNECTORS;
17
18
  const autoConnect = props.autoConnect ?? true;
18
19
  let t0;
@@ -22,6 +23,7 @@ const useMcpManagerResource = (props) => {
22
23
  $[1] = t0;
23
24
  } else t0 = $[1];
24
25
  const redirectUri = t0;
26
+ const connectionTimeout = props.connectionTimeout;
25
27
  let t1;
26
28
  if ($[2] !== props.storage) {
27
29
  t1 = props.storage ?? McpLocalStorage();
@@ -110,88 +112,94 @@ const useMcpManagerResource = (props) => {
110
112
  $[16] = t8;
111
113
  } else t8 = $[16];
112
114
  useEffect(t7, t8);
115
+ assertUniqueServerIds([...connectors.map(_temp2), ...customServers.map(_temp3)]);
113
116
  let t9;
114
- if ($[17] !== autoConnect || $[18] !== connectors || $[19] !== customServers || $[20] !== redirectUri || $[21] !== storage) {
117
+ if ($[17] !== autoConnect || $[18] !== connectionTimeout || $[19] !== connectors || $[20] !== customServers || $[21] !== redirectUri || $[22] !== storage) {
115
118
  let t10;
116
- if ($[23] !== autoConnect || $[24] !== redirectUri || $[25] !== storage) {
117
- t10 = (c) => withKey(c.id, McpServerResource({
118
- id: c.id,
119
+ if ($[24] !== autoConnect || $[25] !== connectionTimeout || $[26] !== redirectUri || $[27] !== storage) {
120
+ t10 = (c_0) => withKey(c_0.id, McpServerResource({
121
+ id: c_0.id,
119
122
  kind: "connector",
120
- name: c.name,
121
- url: c.url,
122
- icon: c.icon,
123
- auth: c.auth,
123
+ name: c_0.name,
124
+ url: c_0.url,
125
+ icon: c_0.icon,
126
+ auth: c_0.auth,
124
127
  storage,
125
128
  redirectUri,
126
129
  autoConnect,
127
- onRemove: _temp2
130
+ connectionTimeout: c_0.connectionTimeout ?? connectionTimeout,
131
+ onRemove: _temp4
128
132
  }));
129
- $[23] = autoConnect;
130
- $[24] = redirectUri;
131
- $[25] = storage;
132
- $[26] = t10;
133
- } else t10 = $[26];
133
+ $[24] = autoConnect;
134
+ $[25] = connectionTimeout;
135
+ $[26] = redirectUri;
136
+ $[27] = storage;
137
+ $[28] = t10;
138
+ } else t10 = $[28];
134
139
  const connectorElements = connectors.map(t10);
135
140
  let t11;
136
- if ($[27] !== autoConnect || $[28] !== redirectUri || $[29] !== storage) {
137
- t11 = (s) => withKey(s.id, McpServerResource({
138
- id: s.id,
141
+ if ($[29] !== autoConnect || $[30] !== connectionTimeout || $[31] !== redirectUri || $[32] !== storage) {
142
+ t11 = (s_0) => withKey(s_0.id, McpServerResource({
143
+ id: s_0.id,
139
144
  kind: "custom",
140
- name: s.name,
141
- url: s.url,
142
- auth: s.auth,
145
+ name: s_0.name,
146
+ url: s_0.url,
147
+ auth: s_0.auth,
143
148
  storage,
144
149
  redirectUri,
145
150
  autoConnect,
151
+ connectionTimeout: s_0.connectionTimeout ?? connectionTimeout,
146
152
  onRemove: async () => {
147
- setCustomServers((prev_0) => prev_0.filter((x) => x.id !== s.id));
153
+ setCustomServers((prev_0) => prev_0.filter((x) => x.id !== s_0.id));
148
154
  }
149
155
  }));
150
- $[27] = autoConnect;
151
- $[28] = redirectUri;
152
- $[29] = storage;
153
- $[30] = t11;
154
- } else t11 = $[30];
156
+ $[29] = autoConnect;
157
+ $[30] = connectionTimeout;
158
+ $[31] = redirectUri;
159
+ $[32] = storage;
160
+ $[33] = t11;
161
+ } else t11 = $[33];
155
162
  const customElements = customServers.map(t11);
156
163
  t9 = [...connectorElements, ...customElements];
157
164
  $[17] = autoConnect;
158
- $[18] = connectors;
159
- $[19] = customServers;
160
- $[20] = redirectUri;
161
- $[21] = storage;
162
- $[22] = t9;
163
- } else t9 = $[22];
165
+ $[18] = connectionTimeout;
166
+ $[19] = connectors;
167
+ $[20] = customServers;
168
+ $[21] = redirectUri;
169
+ $[22] = storage;
170
+ $[23] = t9;
171
+ } else t9 = $[23];
164
172
  const lookup = useClientLookup(t9);
165
173
  const all = lookup.state;
166
174
  let t10;
167
- if ($[31] !== all) {
168
- t10 = all.filter(_temp3);
169
- $[31] = all;
170
- $[32] = t10;
171
- } else t10 = $[32];
175
+ if ($[34] !== all) {
176
+ t10 = all.filter(_temp5);
177
+ $[34] = all;
178
+ $[35] = t10;
179
+ } else t10 = $[35];
172
180
  let t11;
173
- if ($[33] !== all) {
174
- t11 = all.filter(_temp4);
175
- $[33] = all;
176
- $[34] = t11;
177
- } else t11 = $[34];
181
+ if ($[36] !== all) {
182
+ t11 = all.filter(_temp6);
183
+ $[36] = all;
184
+ $[37] = t11;
185
+ } else t11 = $[37];
178
186
  let t12;
179
- if ($[35] !== all || $[36] !== isHydrated || $[37] !== t10 || $[38] !== t11) {
187
+ if ($[38] !== all || $[39] !== isHydrated || $[40] !== t10 || $[41] !== t11) {
180
188
  t12 = {
181
189
  servers: all,
182
190
  connectors: t10,
183
191
  customServers: t11,
184
192
  isHydrated
185
193
  };
186
- $[35] = all;
187
- $[36] = isHydrated;
188
- $[37] = t10;
189
- $[38] = t11;
190
- $[39] = t12;
191
- } else t12 = $[39];
194
+ $[38] = all;
195
+ $[39] = isHydrated;
196
+ $[40] = t10;
197
+ $[41] = t11;
198
+ $[42] = t12;
199
+ } else t12 = $[42];
192
200
  const state = t12;
193
201
  let out;
194
- if ($[40] !== lookup || $[41] !== state.servers) {
202
+ if ($[43] !== lookup || $[44] !== state.servers) {
195
203
  out = {};
196
204
  for (const server of state.servers) {
197
205
  if (server.connectionState !== "connected") continue;
@@ -205,62 +213,62 @@ const useMcpManagerResource = (props) => {
205
213
  };
206
214
  }
207
215
  }
208
- $[40] = lookup;
209
- $[41] = state.servers;
210
- $[42] = out;
211
- } else out = $[42];
216
+ $[43] = lookup;
217
+ $[44] = state.servers;
218
+ $[45] = out;
219
+ } else out = $[45];
212
220
  const toolkit = out;
213
221
  const clientRef = useAssistantClientRef();
214
222
  let t13;
215
223
  let t14;
216
- if ($[43] !== clientRef || $[44] !== toolkit) {
224
+ if ($[46] !== clientRef || $[47] !== toolkit) {
217
225
  t13 = () => {
218
226
  const client = clientRef.current;
219
227
  if (!client) return;
220
228
  return client.modelContext().register({ getModelContext: () => ({ tools: toolkit }) });
221
229
  };
222
230
  t14 = [toolkit, clientRef];
223
- $[43] = clientRef;
224
- $[44] = toolkit;
225
- $[45] = t13;
226
- $[46] = t14;
231
+ $[46] = clientRef;
232
+ $[47] = toolkit;
233
+ $[48] = t13;
234
+ $[49] = t14;
227
235
  } else {
228
- t13 = $[45];
229
- t14 = $[46];
236
+ t13 = $[48];
237
+ t14 = $[49];
230
238
  }
231
239
  useEffect(t13, t14);
232
240
  let t15;
233
- if ($[47] !== lookup || $[48] !== state) {
241
+ if ($[50] !== lookup || $[51] !== state) {
234
242
  t15 = (kind, index) => {
235
243
  const list = kind === "connector" ? state.connectors : state.customServers;
236
244
  const entry = list[index];
237
245
  if (!entry) throw new Error(`McpManagerResource: no ${kind} at index ${index} (length ${list.length})`);
238
246
  return lookup.get({ key: entry.id });
239
247
  };
240
- $[47] = lookup;
241
- $[48] = state;
242
- $[49] = t15;
243
- } else t15 = $[49];
248
+ $[50] = lookup;
249
+ $[51] = state;
250
+ $[52] = t15;
251
+ } else t15 = $[52];
244
252
  const serverByKind = t15;
245
253
  let t16;
246
- if ($[50] !== state) {
254
+ if ($[53] !== state) {
247
255
  t16 = () => state;
248
- $[50] = state;
249
- $[51] = t16;
250
- } else t16 = $[51];
256
+ $[53] = state;
257
+ $[54] = t16;
258
+ } else t16 = $[54];
251
259
  let t17;
252
- if ($[52] !== lookup || $[53] !== serverByKind) {
260
+ if ($[55] !== lookup || $[56] !== serverByKind) {
253
261
  t17 = (query) => {
254
262
  if ("id" in query) return lookup.get({ key: query.id });
255
263
  return serverByKind(query.kind, query.index);
256
264
  };
257
- $[52] = lookup;
258
- $[53] = serverByKind;
259
- $[54] = t17;
260
- } else t17 = $[54];
265
+ $[55] = lookup;
266
+ $[56] = serverByKind;
267
+ $[57] = t17;
268
+ } else t17 = $[57];
261
269
  let t18;
262
270
  let t19;
263
- if ($[55] !== serverByKind) {
271
+ if ($[58] !== serverByKind) {
264
272
  t18 = (t20) => {
265
273
  const { index: index_0 } = t20;
266
274
  return serverByKind("connector", index_0);
@@ -269,47 +277,48 @@ const useMcpManagerResource = (props) => {
269
277
  const { index: index_1 } = t21;
270
278
  return serverByKind("custom", index_1);
271
279
  };
272
- $[55] = serverByKind;
273
- $[56] = t18;
274
- $[57] = t19;
280
+ $[58] = serverByKind;
281
+ $[59] = t18;
282
+ $[60] = t19;
275
283
  } else {
276
- t18 = $[56];
277
- t19 = $[57];
284
+ t18 = $[59];
285
+ t19 = $[60];
278
286
  }
279
287
  let t20;
280
- if ($[58] === Symbol.for("react.memo_cache_sentinel")) {
288
+ if ($[61] === Symbol.for("react.memo_cache_sentinel")) {
281
289
  t20 = async (t21) => {
282
- const { name, url, auth } = t21;
290
+ const { name, url, auth, connectionTimeout: connectionTimeout_0 } = t21;
283
291
  const record = {
284
292
  id: typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : `mcp-${Date.now()}-${Math.random().toString(36).slice(2)}`,
285
293
  name,
286
294
  url,
287
295
  auth,
296
+ connectionTimeout: connectionTimeout_0,
288
297
  createdAt: Date.now()
289
298
  };
290
299
  setCustomServers((prev_1) => [...prev_1, record]);
291
300
  return record.id;
292
301
  };
293
- $[58] = t20;
294
- } else t20 = $[58];
302
+ $[61] = t20;
303
+ } else t20 = $[61];
295
304
  let t21;
296
- if ($[59] !== lookup || $[60] !== state || $[61] !== storage) {
305
+ if ($[62] !== lookup || $[63] !== state || $[64] !== storage) {
297
306
  t21 = async (id) => {
298
- if (state.connectors.some((c_0) => c_0.id === id)) throw new Error(`Cannot remove connector "${id}" — connectors are app-defined and not removable. Use a custom server id instead.`);
307
+ if (state.connectors.some((c_1) => c_1.id === id)) throw new Error(`Cannot remove connector "${id}" — connectors are app-defined and not removable. Use a custom server id instead.`);
299
308
  try {
300
309
  await lookup.get({ key: id }).remove();
301
310
  } catch {
302
311
  await storage.clearAuthState(id);
303
- setCustomServers((prev_2) => prev_2.filter((s_2) => s_2.id !== id));
312
+ setCustomServers((prev_2) => prev_2.filter((s_3) => s_3.id !== id));
304
313
  }
305
314
  };
306
- $[59] = lookup;
307
- $[60] = state;
308
- $[61] = storage;
309
- $[62] = t21;
310
- } else t21 = $[62];
315
+ $[62] = lookup;
316
+ $[63] = state;
317
+ $[64] = storage;
318
+ $[65] = t21;
319
+ } else t21 = $[65];
311
320
  let t22;
312
- if ($[63] !== t16 || $[64] !== t17 || $[65] !== t18 || $[66] !== t19 || $[67] !== t21) {
321
+ if ($[66] !== t16 || $[67] !== t17 || $[68] !== t18 || $[69] !== t19 || $[70] !== t21) {
313
322
  t22 = {
314
323
  getState: t16,
315
324
  server: t17,
@@ -318,13 +327,13 @@ const useMcpManagerResource = (props) => {
318
327
  addCustomServer: t20,
319
328
  removeServer: t21
320
329
  };
321
- $[63] = t16;
322
- $[64] = t17;
323
- $[65] = t18;
324
- $[66] = t19;
325
- $[67] = t21;
326
- $[68] = t22;
327
- } else t22 = $[68];
330
+ $[66] = t16;
331
+ $[67] = t17;
332
+ $[68] = t18;
333
+ $[69] = t19;
334
+ $[70] = t21;
335
+ $[71] = t22;
336
+ } else t22 = $[71];
328
337
  return t22;
329
338
  };
330
339
  const McpManagerResource = resource(useMcpManagerResource);
@@ -334,12 +343,18 @@ attachTransformScopes(useMcpManagerResource, (scopes, parent) => {
334
343
  function _temp(r) {
335
344
  return r.id;
336
345
  }
337
- async function _temp2() {}
338
- function _temp3(s_0) {
339
- return s_0.kind === "connector";
346
+ function _temp2(c) {
347
+ return c.id;
340
348
  }
341
- function _temp4(s_1) {
342
- return s_1.kind === "custom";
349
+ function _temp3(s) {
350
+ return s.id;
351
+ }
352
+ async function _temp4() {}
353
+ function _temp5(s_1) {
354
+ return s_1.kind === "connector";
355
+ }
356
+ function _temp6(s_2) {
357
+ return s_2.kind === "custom";
343
358
  }
344
359
  //#endregion
345
360
  export { McpManagerResource };
@@ -1 +1 @@
1
- {"version":3,"file":"McpManagerResource.js","names":["useState","useEffect","useMemo","useEffectEvent","useRef","useResource","resource","withKey","useClientLookup","useAssistantClientRef","attachTransformScopes","ClientOutput","ModelContext","Tool","McpServerResource","McpLocalStorage","MCPStorageElement","MCPAuthConfig","MCPConnector","MCPCustomServerRecord","MCPManagerState","McpManagerResourceProps","connectors","storage","oauthRedirectUri","autoConnect","defaultRedirectUri","window","location","origin","NO_CONNECTORS","useMcpManagerResource","props","$","_c","t0","redirectUri","t1","storageElement","t2","Symbol","for","customServers","setCustomServers","isHydrated","setIsHydrated","hydratedRef","t3","signal","markHydrated","cancelled","current","records","loadCustomServers","t4","error","prev","length","persistedIds","Set","map","_temp","filter","r_0","has","r","id","hydrate","signal_0","t5","t6","records_0","saveCustomServers","persistCustomServers","t7","t8","t9","t10","c","kind","name","url","icon","auth","onRemove","_temp2","connectorElements","t11","s","prev_0","x","customElements","serverElements","lookup","all","state","_temp3","_temp4","t12","servers","out","server","connectionState","tool","tools","fullName","type","description","undefined","parameters","inputSchema","execute","args","get","key","callTool","toolkit","clientRef","t13","t14","client","modelContext","register","getModelContext","t15","index","list","entry","Error","serverByKind","t16","t17","query","t18","t19","t20","index_0","t21","index_1","record","crypto","randomUUID","Date","now","Math","random","toString","slice","createdAt","prev_1","some","c_0","remove","clearAuthState","prev_2","s_2","t22","getState","connector","customServer","addCustomServer","removeServer","McpManagerResource","scopes","parent","source","s_0","s_1"],"sources":["../../src/resources/McpManagerResource.ts"],"sourcesContent":["import { useState, useEffect, useMemo, useEffectEvent, useRef } from \"react\";\nimport { useResource, resource, withKey } from \"@assistant-ui/tap\";\nimport {\n useClientLookup,\n useAssistantClientRef,\n attachTransformScopes,\n type ClientOutput,\n} from \"@assistant-ui/store\";\nimport { ModelContext } from \"@assistant-ui/core/store\";\nimport type { Tool } from \"assistant-stream\";\nimport { McpServerResource } from \"./McpServerResource\";\nimport { McpLocalStorage } from \"./storage/McpLocalStorage\";\nimport type { MCPStorageElement } from \"./storage/types\";\nimport type {\n MCPAuthConfig,\n MCPConnector,\n MCPCustomServerRecord,\n MCPManagerState,\n} from \"../mcp-scope\";\n\nexport type McpManagerResourceProps = {\n connectors?: MCPConnector[] | undefined;\n storage?: MCPStorageElement | undefined;\n /** OAuth redirect target. Defaults to `${origin}/mcp/callback`. */\n oauthRedirectUri?: string | undefined;\n /** Connect on mount when usable auth exists. Default true. */\n autoConnect?: boolean | undefined;\n};\n\nfunction defaultRedirectUri(): string {\n if (typeof window === \"undefined\") return \"\";\n return `${window.location.origin}/mcp/callback`;\n}\n\n// Stable empty fallback so an absent `connectors` prop doesn't produce a fresh\n// array each render (which would invalidate the serverElements memo below).\nconst NO_CONNECTORS: MCPConnector[] = [];\n\nconst useMcpManagerResource = (\n props: McpManagerResourceProps,\n): ClientOutput<\"mcp\"> => {\n const connectors = props.connectors ?? NO_CONNECTORS;\n const autoConnect = props.autoConnect ?? true;\n const redirectUri = props.oauthRedirectUri ?? defaultRedirectUri();\n\n const storageElement = props.storage ?? McpLocalStorage();\n const storage = useResource(storageElement);\n\n const [customServers, setCustomServers] = useState<MCPCustomServerRecord[]>(\n [],\n );\n const [isHydrated, setIsHydrated] = useState(false);\n\n const hydratedRef = useRef(false);\n\n const hydrate = useEffectEvent(async (signal: { cancelled: boolean }) => {\n const markHydrated = () => {\n if (!signal.cancelled) {\n hydratedRef.current = true;\n setIsHydrated(true);\n }\n };\n\n let records: Awaited<ReturnType<typeof storage.loadCustomServers>>;\n try {\n records = await storage.loadCustomServers();\n } catch (error) {\n markHydrated();\n throw error;\n }\n\n if (signal.cancelled) return;\n // Merge rather than replace so any addCustomServer calls that\n // happened before hydration resolved aren't silently overwritten.\n // Persisted order wins; pre-hydration locals append.\n setCustomServers((prev) => {\n if (prev.length === 0) return records;\n const persistedIds = new Set(records.map((r) => r.id));\n return [...records, ...prev.filter((r) => !persistedIds.has(r.id))];\n });\n markHydrated();\n });\n\n useEffect(() => {\n const signal = { cancelled: false };\n void hydrate(signal);\n return () => {\n signal.cancelled = true;\n };\n }, []);\n\n const persistCustomServers = useEffectEvent(\n async (records: MCPCustomServerRecord[]) => {\n if (!hydratedRef.current) return;\n await storage.saveCustomServers(records);\n },\n );\n\n useEffect(() => {\n void persistCustomServers(customServers);\n }, [customServers]);\n\n const serverElements = useMemo(() => {\n const connectorElements = connectors.map((c) =>\n withKey(\n c.id,\n McpServerResource({\n id: c.id,\n kind: \"connector\",\n name: c.name,\n url: c.url,\n icon: c.icon,\n auth: c.auth,\n storage,\n redirectUri,\n autoConnect,\n onRemove: async () => {\n // connectors cannot be removed\n },\n }),\n ),\n );\n const customElements = customServers.map((s) =>\n withKey(\n s.id,\n McpServerResource({\n id: s.id,\n kind: \"custom\",\n name: s.name,\n url: s.url,\n auth: s.auth,\n storage,\n redirectUri,\n autoConnect,\n onRemove: async () => {\n setCustomServers((prev) => prev.filter((x) => x.id !== s.id));\n },\n }),\n ),\n );\n return [...connectorElements, ...customElements];\n }, [connectors, customServers, storage, redirectUri, autoConnect]);\n\n const lookup = useClientLookup(serverElements);\n\n const state = useMemo<MCPManagerState>(() => {\n const all = lookup.state;\n return {\n servers: all,\n connectors: all.filter((s) => s.kind === \"connector\"),\n customServers: all.filter((s) => s.kind === \"custom\"),\n isHydrated,\n };\n }, [lookup.state, isHydrated]);\n\n // ─── Auto-register MCP tools as frontend tools in modelContext ─────\n // Build the toolkit from connected servers; re-register when the visible\n // tool surface changes. Tool names are prefixed with the server id to\n // avoid collisions across connected servers — connector ids must not\n // contain `__` (enforced by `defineConnector`); `addCustomServer`\n // generates UUIDs that satisfy the constraint by construction.\n const toolkit = useMemo<Record<string, Tool<any, any>>>(() => {\n const out: Record<string, Tool<any, any>> = {};\n for (const server of state.servers) {\n if (server.connectionState !== \"connected\") continue;\n for (const tool of server.tools) {\n const fullName = `${server.id}__${tool.name}`;\n out[fullName] = {\n type: \"frontend\",\n ...(tool.description !== undefined\n ? { description: tool.description }\n : {}),\n parameters: tool.inputSchema as never,\n execute: (args) =>\n lookup.get({ key: server.id }).callTool(tool.name, args as unknown),\n };\n }\n }\n return out;\n }, [state, lookup]);\n\n const clientRef = useAssistantClientRef();\n\n useEffect(() => {\n const client = clientRef.current;\n if (!client) return;\n return client.modelContext().register({\n getModelContext: () => ({ tools: toolkit }),\n });\n }, [toolkit, clientRef]);\n\n const serverByKind = (kind: \"connector\" | \"custom\", index: number) => {\n const list = kind === \"connector\" ? state.connectors : state.customServers;\n const entry = list[index];\n if (!entry) {\n throw new Error(\n `McpManagerResource: no ${kind} at index ${index} (length ${list.length})`,\n );\n }\n return lookup.get({ key: entry.id });\n };\n\n return {\n getState: () => state,\n server: (query) => {\n if (\"id\" in query) return lookup.get({ key: query.id });\n return serverByKind(query.kind, query.index);\n },\n connector: ({ index }) => serverByKind(\"connector\", index),\n customServer: ({ index }) => serverByKind(\"custom\", index),\n addCustomServer: async ({ name, url, auth }) => {\n const record: MCPCustomServerRecord = {\n id:\n typeof crypto !== \"undefined\" && \"randomUUID\" in crypto\n ? crypto.randomUUID()\n : `mcp-${Date.now()}-${Math.random().toString(36).slice(2)}`,\n name,\n url,\n auth: auth as MCPAuthConfig,\n createdAt: Date.now(),\n };\n setCustomServers((prev) => [...prev, record]);\n return record.id;\n },\n removeServer: async (id) => {\n // removeServer is custom-server only — connectors are app-defined\n // and not user-removable. Refuse rather than silently no-op.\n if (state.connectors.some((c) => c.id === id)) {\n throw new Error(\n `Cannot remove connector \"${id}\" — connectors are app-defined and not removable. Use a custom server id instead.`,\n );\n }\n // Delegate to McpServerResource.remove() which disconnects,\n // clears auth state, and unregisters from customServers in one\n // place. Fallback to manual cleanup if the lookup is empty\n // (server already gone).\n try {\n await lookup.get({ key: id }).remove();\n } catch {\n await storage.clearAuthState(id);\n setCustomServers((prev) => prev.filter((s) => s.id !== id));\n }\n },\n };\n};\n\nexport const McpManagerResource = resource(useMcpManagerResource);\n\n// Ensure modelContext exists as a sibling when the manager mounts. If an\n// ancestor (e.g. a chat runtime) already provides modelContext, this is a\n// no-op; otherwise it's auto-mounted alongside `mcp`.\nattachTransformScopes(useMcpManagerResource, (scopes, parent) => {\n if (!scopes.modelContext && parent.modelContext.source === null) {\n scopes.modelContext = ModelContext();\n }\n});\n"],"mappings":";;;;;;;;AA6BA,SAAS0B,qBAA6B;CACpC,IAAI,OAAOC,WAAW,aAAa,OAAO;CAC1C,OAAO,GAAGA,OAAOC,SAASC,OAAM;AAClC;AAIA,MAAMC,gBAAgC,CAAA;AAEtC,MAAMC,yBAAwBC,UAAA;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAG5B,MAAAZ,aAAmBU,MAAKV,cAALQ;CACnB,MAAAL,cAAoBO,MAAKP,eAAL;CAA0B,IAAAU;CAAA,IAAAF,EAAA,OAAAD,MAAAR,kBAAA;EAC1BW,KAAAH,MAAKR,oBAAqBE,mBAAmB;EAACO,EAAA,KAAAD,MAAAR;EAAAS,EAAA,KAAAE;CAAA,OAAAA,KAAAF,EAAA;CAAlE,MAAAG,cAAoBD;CAA+C,IAAAE;CAAA,IAAAJ,EAAA,OAAAD,MAAAT,SAAA;EAE5Cc,KAAAL,MAAKT,WAAYR,gBAAgB;EAACkB,EAAA,KAAAD,MAAAT;EAAAU,EAAA,KAAAI;CAAA,OAAAA,KAAAJ,EAAA;CACzD,MAAAV,UAAgBlB,YAAYiC,EAAc;CAAE,IAAAC;CAAA,IAAAN,EAAA,OAAAO,OAAAC,IAAA,2BAAA,GAAA;EAG1CF,KAAA,CAAA;EAAEN,EAAA,KAAAM;CAAA,OAAAA,KAAAN,EAAA;CADJ,MAAA,CAAAS,eAAAC,oBAA0C3C,SACxCuC,EACF;CACA,MAAA,CAAAK,YAAAC,iBA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,CAAC;GAAC;EAC5D;EACFjC,EAAA,MAAA4D;EAAA5D,EAAA,MAAA8D;EAAA9D,EAAA,MAAAV;EAAAU,EAAA,MAAAwG;CAAA,OAAAA,MAAAxG,EAAA;CAAA,IAAA2H;CAAA,IAAA3H,EAAA,QAAAiG,OAAAjG,EAAA,QAAAkG,OAAAlG,EAAA,QAAAoG,OAAApG,EAAA,QAAAqG,OAAArG,EAAA,QAAAwG,KAAA;EAxCImB,MAAA;GAAAC,UACK3B;GAAW7B,QACb8B;GAGP2B,WACUzB;GAA+C0B,cAC5CzB;GAA4C0B,iBACzCzB;GAahB0B,cACaxB;EAmBhB;EAACxG,EAAA,MAAAiG;EAAAjG,EAAA,MAAAkG;EAAAlG,EAAA,MAAAoG;EAAApG,EAAA,MAAAqG;EAAArG,EAAA,MAAAwG;EAAAxG,EAAA,MAAA2H;CAAA,OAAAA,MAAA3H,EAAA;CAAA,OAzCM2H;AAyCN;AAGH,MAAaM,qBAAqB5J,SAASyB,qBAAqB;AAKhErB,sBAAsBqB,wBAAwBoI,QAAQC,WAAW;CAC/D,IAAI,CAACD,OAAO1C,gBAAgB2C,OAAO3C,aAAa4C,WAAW,MACzDF,OAAO1C,eAAe7G,aAAa;AAEvC,CAAC;AAzN6B,SAAAiD,MAAAI,GAAA;CAAA,OAuCwBA,EAACC;AAAG;AAvC5B,eAAAmB,SAAA,CAAA;AAAA,SAAAW,OAAAsE,KAAA;CAAA,OA+GM9E,IAACT,SAAU;AAAW;AA/G5B,SAAAkB,OAAAsE,KAAA;CAAA,OAgHS/E,IAACT,SAAU;AAAQ"}
1
+ {"version":3,"file":"McpManagerResource.js","names":["useState","useEffect","useMemo","useEffectEvent","useRef","useResource","resource","withKey","useClientLookup","useAssistantClientRef","attachTransformScopes","ClientOutput","ModelContext","Tool","McpServerResource","McpLocalStorage","MCPStorageElement","assertUniqueServerIds","MCPAuthConfig","MCPConnector","MCPCustomServerRecord","MCPManagerState","McpManagerResourceProps","connectors","storage","oauthRedirectUri","autoConnect","connectionTimeout","defaultRedirectUri","window","location","origin","NO_CONNECTORS","useMcpManagerResource","props","$","_c","t0","redirectUri","t1","storageElement","t2","Symbol","for","customServers","setCustomServers","isHydrated","setIsHydrated","hydratedRef","t3","signal","markHydrated","cancelled","current","records","loadCustomServers","t4","error","prev","length","persistedIds","Set","map","_temp","filter","r_0","has","r","id","hydrate","signal_0","t5","t6","records_0","saveCustomServers","persistCustomServers","t7","t8","_temp2","_temp3","t9","t10","c_0","c","kind","name","url","icon","auth","onRemove","_temp4","connectorElements","t11","s_0","s","prev_0","x","customElements","serverElements","lookup","all","state","_temp5","_temp6","t12","servers","out","server","connectionState","tool","tools","fullName","type","description","undefined","parameters","inputSchema","execute","args","get","key","callTool","toolkit","clientRef","t13","t14","client","modelContext","register","getModelContext","t15","index","list","entry","Error","serverByKind","t16","t17","query","t18","t19","t20","index_0","t21","index_1","connectionTimeout_0","record","crypto","randomUUID","Date","now","Math","random","toString","slice","createdAt","prev_1","some","c_1","remove","clearAuthState","prev_2","s_3","t22","getState","connector","customServer","addCustomServer","removeServer","McpManagerResource","scopes","parent","source","s_1","s_2"],"sources":["../../src/resources/McpManagerResource.ts"],"sourcesContent":["import { useState, useEffect, useMemo, useEffectEvent, useRef } from \"react\";\nimport { useResource, resource, withKey } from \"@assistant-ui/tap\";\nimport {\n useClientLookup,\n useAssistantClientRef,\n attachTransformScopes,\n type ClientOutput,\n} from \"@assistant-ui/store\";\nimport { ModelContext } from \"@assistant-ui/core/store\";\nimport type { Tool } from \"assistant-stream\";\nimport { McpServerResource } from \"./McpServerResource\";\nimport { McpLocalStorage } from \"./storage/McpLocalStorage\";\nimport type { MCPStorageElement } from \"./storage/types\";\nimport { assertUniqueServerIds } from \"../utils/serverId\";\nimport type {\n MCPAuthConfig,\n MCPConnector,\n MCPCustomServerRecord,\n MCPManagerState,\n} from \"../mcp-scope\";\n\nexport type McpManagerResourceProps = {\n connectors?: MCPConnector[] | undefined;\n storage?: MCPStorageElement | undefined;\n /** OAuth redirect target. Defaults to `${origin}/mcp/callback`. */\n oauthRedirectUri?: string | undefined;\n /** Connect on mount when usable auth exists. Default true. */\n autoConnect?: boolean | undefined;\n /** Optional timeout in milliseconds for connect/listTools calls. Disabled by default. */\n connectionTimeout?: number | undefined;\n};\n\nfunction defaultRedirectUri(): string {\n if (typeof window === \"undefined\") return \"\";\n return `${window.location.origin}/mcp/callback`;\n}\n\n// Stable empty fallback so an absent `connectors` prop doesn't produce a fresh\n// array each render (which would invalidate the serverElements memo below).\nconst NO_CONNECTORS: MCPConnector[] = [];\n\nconst useMcpManagerResource = (\n props: McpManagerResourceProps,\n): ClientOutput<\"mcp\"> => {\n const connectors = props.connectors ?? NO_CONNECTORS;\n const autoConnect = props.autoConnect ?? true;\n const redirectUri = props.oauthRedirectUri ?? defaultRedirectUri();\n const connectionTimeout = props.connectionTimeout;\n\n const storageElement = props.storage ?? McpLocalStorage();\n const storage = useResource(storageElement);\n\n const [customServers, setCustomServers] = useState<MCPCustomServerRecord[]>(\n [],\n );\n const [isHydrated, setIsHydrated] = useState(false);\n\n const hydratedRef = useRef(false);\n\n const hydrate = useEffectEvent(async (signal: { cancelled: boolean }) => {\n const markHydrated = () => {\n if (!signal.cancelled) {\n hydratedRef.current = true;\n setIsHydrated(true);\n }\n };\n\n let records: Awaited<ReturnType<typeof storage.loadCustomServers>>;\n try {\n records = await storage.loadCustomServers();\n } catch (error) {\n markHydrated();\n throw error;\n }\n\n if (signal.cancelled) return;\n // Merge rather than replace so any addCustomServer calls that\n // happened before hydration resolved aren't silently overwritten.\n // Persisted order wins; pre-hydration locals append.\n setCustomServers((prev) => {\n if (prev.length === 0) return records;\n const persistedIds = new Set(records.map((r) => r.id));\n return [...records, ...prev.filter((r) => !persistedIds.has(r.id))];\n });\n markHydrated();\n });\n\n useEffect(() => {\n const signal = { cancelled: false };\n void hydrate(signal);\n return () => {\n signal.cancelled = true;\n };\n }, []);\n\n const persistCustomServers = useEffectEvent(\n async (records: MCPCustomServerRecord[]) => {\n if (!hydratedRef.current) return;\n await storage.saveCustomServers(records);\n },\n );\n\n useEffect(() => {\n void persistCustomServers(customServers);\n }, [customServers]);\n\n const serverElements = useMemo(() => {\n assertUniqueServerIds([\n ...connectors.map((c) => c.id),\n ...customServers.map((s) => s.id),\n ]);\n\n const connectorElements = connectors.map((c) =>\n withKey(\n c.id,\n McpServerResource({\n id: c.id,\n kind: \"connector\",\n name: c.name,\n url: c.url,\n icon: c.icon,\n auth: c.auth,\n storage,\n redirectUri,\n autoConnect,\n connectionTimeout: c.connectionTimeout ?? connectionTimeout,\n onRemove: async () => {\n // connectors cannot be removed\n },\n }),\n ),\n );\n const customElements = customServers.map((s) =>\n withKey(\n s.id,\n McpServerResource({\n id: s.id,\n kind: \"custom\",\n name: s.name,\n url: s.url,\n auth: s.auth,\n storage,\n redirectUri,\n autoConnect,\n connectionTimeout: s.connectionTimeout ?? connectionTimeout,\n onRemove: async () => {\n setCustomServers((prev) => prev.filter((x) => x.id !== s.id));\n },\n }),\n ),\n );\n return [...connectorElements, ...customElements];\n }, [\n connectors,\n customServers,\n storage,\n redirectUri,\n autoConnect,\n connectionTimeout,\n ]);\n\n const lookup = useClientLookup(serverElements);\n\n const state = useMemo<MCPManagerState>(() => {\n const all = lookup.state;\n return {\n servers: all,\n connectors: all.filter((s) => s.kind === \"connector\"),\n customServers: all.filter((s) => s.kind === \"custom\"),\n isHydrated,\n };\n }, [lookup.state, isHydrated]);\n\n // ─── Auto-register MCP tools as frontend tools in modelContext ─────\n // Build the toolkit from connected servers; re-register when the visible\n // tool surface changes. Tool names are prefixed with the server id to\n // avoid collisions across connected servers — connector ids must not\n // contain `__` (enforced by `defineConnector`); `addCustomServer`\n // generates UUIDs that satisfy the constraint by construction.\n const toolkit = useMemo<Record<string, Tool<any, any>>>(() => {\n const out: Record<string, Tool<any, any>> = {};\n for (const server of state.servers) {\n if (server.connectionState !== \"connected\") continue;\n for (const tool of server.tools) {\n const fullName = `${server.id}__${tool.name}`;\n out[fullName] = {\n type: \"frontend\",\n ...(tool.description !== undefined\n ? { description: tool.description }\n : {}),\n parameters: tool.inputSchema as never,\n execute: (args) =>\n lookup.get({ key: server.id }).callTool(tool.name, args as unknown),\n };\n }\n }\n return out;\n }, [state, lookup]);\n\n const clientRef = useAssistantClientRef();\n\n useEffect(() => {\n const client = clientRef.current;\n if (!client) return;\n return client.modelContext().register({\n getModelContext: () => ({ tools: toolkit }),\n });\n }, [toolkit, clientRef]);\n\n const serverByKind = (kind: \"connector\" | \"custom\", index: number) => {\n const list = kind === \"connector\" ? state.connectors : state.customServers;\n const entry = list[index];\n if (!entry) {\n throw new Error(\n `McpManagerResource: no ${kind} at index ${index} (length ${list.length})`,\n );\n }\n return lookup.get({ key: entry.id });\n };\n\n return {\n getState: () => state,\n server: (query) => {\n if (\"id\" in query) return lookup.get({ key: query.id });\n return serverByKind(query.kind, query.index);\n },\n connector: ({ index }) => serverByKind(\"connector\", index),\n customServer: ({ index }) => serverByKind(\"custom\", index),\n addCustomServer: async ({ name, url, auth, connectionTimeout }) => {\n const record: MCPCustomServerRecord = {\n id:\n typeof crypto !== \"undefined\" && \"randomUUID\" in crypto\n ? crypto.randomUUID()\n : `mcp-${Date.now()}-${Math.random().toString(36).slice(2)}`,\n name,\n url,\n auth: auth as MCPAuthConfig,\n connectionTimeout,\n createdAt: Date.now(),\n };\n setCustomServers((prev) => [...prev, record]);\n return record.id;\n },\n removeServer: async (id) => {\n // removeServer is custom-server only — connectors are app-defined\n // and not user-removable. Refuse rather than silently no-op.\n if (state.connectors.some((c) => c.id === id)) {\n throw new Error(\n `Cannot remove connector \"${id}\" — connectors are app-defined and not removable. Use a custom server id instead.`,\n );\n }\n // Delegate to McpServerResource.remove() which disconnects,\n // clears auth state, and unregisters from customServers in one\n // place. Fallback to manual cleanup if the lookup is empty\n // (server already gone).\n try {\n await lookup.get({ key: id }).remove();\n } catch {\n await storage.clearAuthState(id);\n setCustomServers((prev) => prev.filter((s) => s.id !== id));\n }\n },\n };\n};\n\nexport const McpManagerResource = resource(useMcpManagerResource);\n\n// Ensure modelContext exists as a sibling when the manager mounts. If an\n// ancestor (e.g. a chat runtime) already provides modelContext, this is a\n// no-op; otherwise it's auto-mounted alongside `mcp`.\nattachTransformScopes(useMcpManagerResource, (scopes, parent) => {\n if (!scopes.modelContext && parent.modelContext.source === null) {\n scopes.modelContext = ModelContext();\n }\n});\n"],"mappings":";;;;;;;;;AAgCA,SAAS4B,qBAA6B;CACpC,IAAI,OAAOC,WAAW,aAAa,OAAO;CAC1C,OAAO,GAAGA,OAAOC,SAASC,OAAM;AAClC;AAIA,MAAMC,gBAAgC,CAAA;AAEtC,MAAMC,yBAAwBC,UAAA;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAG5B,MAAAb,aAAmBW,MAAKX,cAALS;CACnB,MAAAN,cAAoBQ,MAAKR,eAAL;CAA0B,IAAAW;CAAA,IAAAF,EAAA,OAAAD,MAAAT,kBAAA;EAC1BY,KAAAH,MAAKT,oBAAqBG,mBAAmB;EAACO,EAAA,KAAAD,MAAAT;EAAAU,EAAA,KAAAE;CAAA,OAAAA,KAAAF,EAAA;CAAlE,MAAAG,cAAoBD;CACpB,MAAAV,oBAA0BO,MAAKP;CAAmB,IAAAY;CAAA,IAAAJ,EAAA,OAAAD,MAAAV,SAAA;EAE3Be,KAAAL,MAAKV,WAAYT,gBAAgB;EAACoB,EAAA,KAAAD,MAAAV;EAAAW,EAAA,KAAAI;CAAA,OAAAA,KAAAJ,EAAA;CACzD,MAAAX,UAAgBnB,YAAYmC,EAAc;CAAE,IAAAC;CAAA,IAAAN,EAAA,OAAAO,OAAAC,IAAA,2BAAA,GAAA;EAG1CF,KAAA,CAAA;EAAEN,EAAA,KAAAM;CAAA,OAAAA,KAAAN,EAAA;CADJ,MAAA,CAAAS,eAAAC,oBAA0C7C,SACxCyC,EACF;CACA,MAAA,CAAAK,YAAAC,iBAAoC/C,SAAS,KAAK;CAElD,MAAAgD,cAAoB5C,OAAO,KAAK;CA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AAsB,SAAA,CAAA;AAAA,SAAAY,OAAAuE,KAAA;CAAA,OA8HM/E,IAACV,SAAU;AAAW;AA9H5B,SAAAmB,OAAAuE,KAAA;CAAA,OA+HShF,IAACV,SAAU;AAAQ"}
@@ -13,6 +13,7 @@ type McpServerResourceProps = {
13
13
  storage: MCPStorage;
14
14
  redirectUri: string;
15
15
  autoConnect: boolean;
16
+ connectionTimeout?: number | undefined;
16
17
  onRemove: () => Promise<void>;
17
18
  };
18
19
  declare const McpServerResource: import("@assistant-ui/tap").Resource<ClientOutput<"mcpServer">, [props: McpServerResourceProps]>;
@@ -1 +1 @@
1
- {"version":3,"file":"McpServerResource.d.ts","names":[],"sources":["../../src/resources/McpServerResource.ts"],"mappings":";;;;;KAsBY,sBAAA;EACV,EAAA;EACA,IAAA,EAAM,aAAA;EACN,IAAA;EACA,GAAA;EACA,IAAA;EACA,IAAA,EAAM,aAAA;EACN,OAAA,EAAS,UAAA;EACT,WAAA;EACA,WAAA;EACA,QAAA,QAAgB,OAAA;AAAA;AAAA,cA0PL,iBAAA,8BAAiB,QAAA,CAAA,YAAA,gBAAA,KAAA,EAAA,sBAAA"}
1
+ {"version":3,"file":"McpServerResource.d.ts","names":[],"sources":["../../src/resources/McpServerResource.ts"],"mappings":";;;;;KAsBY,sBAAA;EACV,EAAA;EACA,IAAA,EAAM,aAAA;EACN,IAAA;EACA,GAAA;EACA,IAAA;EACA,IAAA,EAAM,aAAA;EACN,OAAA,EAAS,UAAA;EACT,WAAA;EACA,WAAA;EACA,iBAAA;EACA,QAAA,QAAgB,OAAA;AAAA;AAAA,cAgSL,iBAAA,8BAAiB,QAAA,CAAA,YAAA,gBAAA,KAAA,EAAA,sBAAA"}
@@ -15,6 +15,21 @@ const useMcpServerResource = (props) => {
15
15
  const [authorizationUrl, setAuthorizationUrl] = useState(null);
16
16
  const clientRef = useRef(null);
17
17
  const transportRef = useRef(null);
18
+ const withConnectionTimeout = useEffectEvent(async (promise, phase, startedAt) => {
19
+ const timeoutMs = props.connectionTimeout;
20
+ if (timeoutMs === void 0) return await promise;
21
+ const remainingMs = timeoutMs - (Date.now() - startedAt);
22
+ const timeoutError = () => /* @__PURE__ */ new Error(`MCP server "${props.id}" timed out while ${phase} after ${timeoutMs}ms.`);
23
+ if (remainingMs <= 0) throw timeoutError();
24
+ let timeout;
25
+ try {
26
+ return await Promise.race([promise, new Promise((_, reject) => {
27
+ timeout = setTimeout(() => reject(timeoutError()), remainingMs);
28
+ })]);
29
+ } finally {
30
+ if (timeout !== void 0) clearTimeout(timeout);
31
+ }
32
+ });
18
33
  const buildTransport = useEffectEvent(async () => {
19
34
  if (props.auth.type === "oauth") {
20
35
  const authProvider = createOAuthProvider({
@@ -40,8 +55,9 @@ const useMcpServerResource = (props) => {
40
55
  name: "assistant-ui-mcp",
41
56
  version: "0.0.0"
42
57
  });
43
- await client.connect(transport);
44
- const list = await client.listTools();
58
+ const startedAt_0 = Date.now();
59
+ await withConnectionTimeout(client.connect(transport), "connecting", startedAt_0);
60
+ const list = await withConnectionTimeout(client.listTools(), "listing tools", startedAt_0);
45
61
  clientRef.current = client;
46
62
  transportRef.current = transport;
47
63
  setTools(list.tools.map((t) => {
@@ -1 +1 @@
1
- {"version":3,"file":"McpServerResource.js","names":["useState","useRef","useEffect","useMemo","useEffectEvent","resource","ClientOutput","Client","StreamableHTTPClientTransport","StreamableHTTPClientTransportOptions","UnauthorizedError","Transport","createOAuthProvider","buildHeaders","assertValidServerId","MCPStorage","MCPAuthConfig","MCPConnectionState","MCPServerKind","MCPServerState","MCPToolInfo","McpServerResourceProps","id","kind","name","url","icon","auth","storage","redirectUri","autoConnect","onRemove","Promise","useMcpServerResource","props","connectionState","setConnectionState","tools","setTools","lastError","setLastError","message","authorizationUrl","setAuthorizationUrl","clientRef","transportRef","buildTransport","type","authProvider","serverId","config","onAuthorizationUrl","toString","URL","persisted","loadAuthState","headers","transportOpts","requestInit","finalizeConnect","transport","client","version","connect","list","listTools","current","map","t","info","inputSchema","description","undefined","closeTransport","close","doConnect","err","Error","String","doDisconnect","doCompleteAuth","callbackUrl","code","searchParams","get","finishAuth","tryAutoConnect","signal","cancelled","token","tokens","catch","state","getState","disconnect","remove","clearAuthState","callTool","args","arguments","Record","readResource","uri","completeAuth","McpServerResource"],"sources":["../../src/resources/McpServerResource.ts"],"sourcesContent":["import { useState, useRef, useEffect, useMemo, useEffectEvent } from \"react\";\nimport { resource } from \"@assistant-ui/tap\";\nimport type { ClientOutput } from \"@assistant-ui/store\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport {\n StreamableHTTPClientTransport,\n type StreamableHTTPClientTransportOptions,\n} from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport { UnauthorizedError } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport type { Transport } from \"@modelcontextprotocol/sdk/shared/transport.js\";\nimport { createOAuthProvider } from \"../auth/createOAuthProvider\";\nimport { buildHeaders } from \"../auth/buildHeaders\";\nimport { assertValidServerId } from \"../utils/serverId\";\nimport type { MCPStorage } from \"./storage/types\";\nimport type {\n MCPAuthConfig,\n MCPConnectionState,\n MCPServerKind,\n MCPServerState,\n MCPToolInfo,\n} from \"../mcp-scope\";\n\nexport type McpServerResourceProps = {\n id: string;\n kind: MCPServerKind;\n name: string;\n url: string;\n icon?: string | undefined;\n auth: MCPAuthConfig;\n storage: MCPStorage;\n redirectUri: string;\n autoConnect: boolean;\n onRemove: () => Promise<void>;\n};\n\nconst useMcpServerResource = (\n props: McpServerResourceProps,\n): ClientOutput<\"mcpServer\"> => {\n assertValidServerId(props.id);\n const [connectionState, setConnectionState] =\n useState<MCPConnectionState>(\"disconnected\");\n const [tools, setTools] = useState<MCPToolInfo[]>([]);\n const [lastError, setLastError] = useState<{ message: string } | null>(null);\n const [authorizationUrl, setAuthorizationUrl] = useState<string | null>(null);\n\n const clientRef = useRef<Client | null>(null);\n const transportRef = useRef<StreamableHTTPClientTransport | null>(null);\n\n const buildTransport = useEffectEvent(\n async (): Promise<StreamableHTTPClientTransport> => {\n if (props.auth.type === \"oauth\") {\n const authProvider = createOAuthProvider({\n serverId: props.id,\n config: props.auth,\n storage: props.storage,\n redirectUri: props.redirectUri,\n onAuthorizationUrl: (url) => setAuthorizationUrl(url.toString()),\n });\n return new StreamableHTTPClientTransport(new URL(props.url), {\n authProvider,\n });\n }\n if (props.auth.type === \"bearer\") {\n const persisted = await props.storage.loadAuthState(props.id);\n const headers = buildHeaders(props.auth, persisted);\n const transportOpts: StreamableHTTPClientTransportOptions = {};\n if (headers) transportOpts.requestInit = { headers };\n return new StreamableHTTPClientTransport(\n new URL(props.url),\n transportOpts,\n );\n }\n return new StreamableHTTPClientTransport(new URL(props.url));\n },\n );\n\n const finalizeConnect = useEffectEvent(\n async (transport: StreamableHTTPClientTransport) => {\n const client = new Client({\n name: \"assistant-ui-mcp\",\n version: \"0.0.0\",\n });\n // SDK's StreamableHTTPClientTransport.sessionId is `string | undefined`\n // but Transport.sessionId is declared `string?` — under\n // exactOptionalPropertyTypes the SDK's own classes don't satisfy its\n // Transport interface. Cast to bridge the gap.\n await client.connect(transport as unknown as Transport);\n // Defer ref assignment until listTools() also succeeds — otherwise a\n // post-connect failure leaves stale refs that `callTool()` would\n // happily walk into, producing confusing SDK errors instead of\n // \"not connected\".\n const list = await client.listTools();\n clientRef.current = client;\n transportRef.current = transport;\n setTools(\n list.tools.map((t) => {\n const info: MCPToolInfo = {\n name: t.name,\n inputSchema: t.inputSchema,\n };\n if (t.description !== undefined) info.description = t.description;\n return info;\n }),\n );\n setConnectionState(\"connected\");\n },\n );\n\n const closeTransport = async () => {\n const t = transportRef.current;\n transportRef.current = null;\n clientRef.current = null;\n if (t) {\n try {\n await t.close();\n } catch {\n // ignore close errors\n }\n }\n };\n\n const doConnect = useEffectEvent(async () => {\n // Close any prior transport/client so a re-connect doesn't leak.\n await closeTransport();\n setConnectionState(\"connecting\");\n setLastError(null);\n setAuthorizationUrl(null);\n // Clear tools so a reconnect (error → connect, or authRequired\n // → connect) doesn't expose the previous attempt's tool list.\n setTools([]);\n let transport: StreamableHTTPClientTransport | null = null;\n try {\n transport = await buildTransport();\n // Don't assign to transportRef until connect succeeds — otherwise a\n // failed `listTools()` leaves an orphaned transport that future\n // doConnect / doDisconnect calls treat as live.\n await finalizeConnect(transport);\n } catch (err) {\n if (err instanceof UnauthorizedError) {\n // OAuth: keep the transport alive so completeAuth can call\n // finishAuth on it. Closing it before storing would leave a\n // closed transport on transportRef.\n transportRef.current = transport;\n setConnectionState(\"authRequired\");\n } else {\n if (transport) {\n try {\n await transport.close();\n } catch {\n // ignore close errors\n }\n }\n setLastError({\n message: err instanceof Error ? err.message : String(err),\n });\n setConnectionState(\"error\");\n }\n }\n });\n\n const doDisconnect = useEffectEvent(async () => {\n setTools([]);\n setAuthorizationUrl(null);\n setConnectionState(\"disconnected\");\n await closeTransport();\n });\n\n const doCompleteAuth = useEffectEvent(async (callbackUrl: string) => {\n setConnectionState(\"authPending\");\n setLastError(null);\n try {\n const url = new URL(callbackUrl);\n const code = url.searchParams.get(\"code\");\n if (!code) throw new Error(\"missing authorization code in callback URL\");\n let transport = transportRef.current;\n if (!transport) {\n transport = await buildTransport();\n transportRef.current = transport;\n }\n await transport.finishAuth(code);\n setAuthorizationUrl(null);\n await finalizeConnect(transport);\n } catch (err) {\n await closeTransport();\n setLastError({\n message: err instanceof Error ? err.message : String(err),\n });\n setConnectionState(\"error\");\n }\n });\n\n const tryAutoConnect = useEffectEvent(\n async (signal: { cancelled: boolean }) => {\n if (!props.autoConnect) return;\n if (props.auth.type === \"none\") {\n void doConnect();\n return;\n }\n // Static connector token short-circuits the storage read.\n if (props.auth.type === \"bearer\" && props.auth.token) {\n void doConnect();\n return;\n }\n const persisted = await props.storage.loadAuthState(props.id);\n if (signal.cancelled) return;\n if (props.auth.type === \"oauth\") {\n if (!persisted?.tokens) return;\n } else if (!persisted?.token) {\n return;\n }\n void doConnect();\n },\n );\n\n // Auto-connect on mount when usable auth exists.\n useEffect(() => {\n const signal = { cancelled: false };\n void tryAutoConnect(signal);\n return () => {\n signal.cancelled = true;\n const t = transportRef.current;\n transportRef.current = null;\n clientRef.current = null;\n if (t) t.close().catch(() => {});\n };\n }, []);\n\n const state = useMemo<MCPServerState>(\n () => ({\n id: props.id,\n kind: props.kind,\n name: props.name,\n url: props.url,\n icon: props.icon,\n connectionState,\n lastError,\n tools,\n authorizationUrl,\n }),\n [\n props.id,\n props.kind,\n props.name,\n props.url,\n props.icon,\n connectionState,\n lastError,\n tools,\n authorizationUrl,\n ],\n );\n\n return {\n getState: () => state,\n connect: doConnect,\n disconnect: doDisconnect,\n remove: async () => {\n await doDisconnect();\n await props.storage.clearAuthState(props.id);\n await props.onRemove();\n },\n callTool: async (name, args) => {\n const client = clientRef.current;\n if (!client) {\n throw new Error(`MCP server \"${props.id}\" is not connected`);\n }\n return await client.callTool({\n name,\n arguments: args as Record<string, unknown> | undefined,\n });\n },\n readResource: async (uri) => {\n const client = clientRef.current;\n if (!client) {\n throw new Error(`MCP server \"${props.id}\" is not connected`);\n }\n return await client.readResource({ uri });\n },\n completeAuth: doCompleteAuth,\n };\n};\n\nexport const McpServerResource = resource(useMcpServerResource);\n"],"mappings":";;;;;;;;;AAmCA,MAAMiC,wBACJC,UAC8B;CAC9BpB,oBAAoBoB,MAAMZ,EAAE;CAC5B,MAAM,CAACa,iBAAiBC,sBACtBpC,SAA6B,cAAc;CAC7C,MAAM,CAACqC,OAAOC,YAAYtC,SAAwB,CAAA,CAAE;CACpD,MAAM,CAACuC,WAAWC,gBAAgBxC,SAAqC,IAAI;CAC3E,MAAM,CAAC0C,kBAAkBC,uBAAuB3C,SAAwB,IAAI;CAE5E,MAAM4C,YAAY3C,OAAsB,IAAI;CAC5C,MAAM4C,eAAe5C,OAA6C,IAAI;CAEtE,MAAM6C,iBAAiB1C,eACrB,YAAoD;EAClD,IAAI8B,MAAMP,KAAKoB,SAAS,SAAS;GAC/B,MAAMC,eAAepC,oBAAoB;IACvCqC,UAAUf,MAAM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,MAAMY,MAAMH,SAAS;EACvB;EACAkE,UAAU,OAAOzE,MAAM0E,SAAS;GAC9B,MAAMrC,WAASjB,UAAUsB;GACzB,IAAI,CAACL,UACH,MAAM,IAAIgB,MAAM,eAAe3C,MAAMZ,GAAE,mBAAoB;GAE7D,OAAO,MAAMuC,SAAOoC,SAAS;IAC3BzE;IACA2E,WAAWD;GACb,CAAC;EACH;EACAG,cAAc,OAAOC,QAAQ;GAC3B,MAAMzC,WAASjB,UAAUsB;GACzB,IAAI,CAACL,UACH,MAAM,IAAIgB,MAAM,eAAe3C,MAAMZ,GAAE,mBAAoB;GAE7D,OAAO,MAAMuC,SAAOwC,aAAa,EAAEC,IAAI,CAAC;EAC1C;EACAC,cAAcvB;CAChB;AACF;AAEA,MAAawB,oBAAoBnG,SAAS4B,oBAAoB"}
1
+ {"version":3,"file":"McpServerResource.js","names":["useState","useRef","useEffect","useMemo","useEffectEvent","resource","ClientOutput","Client","StreamableHTTPClientTransport","StreamableHTTPClientTransportOptions","UnauthorizedError","Transport","createOAuthProvider","buildHeaders","assertValidServerId","MCPStorage","MCPAuthConfig","MCPConnectionState","MCPServerKind","MCPServerState","MCPToolInfo","McpServerResourceProps","id","kind","name","url","icon","auth","storage","redirectUri","autoConnect","connectionTimeout","onRemove","Promise","useMcpServerResource","props","connectionState","setConnectionState","tools","setTools","lastError","setLastError","message","authorizationUrl","setAuthorizationUrl","clientRef","transportRef","withConnectionTimeout","promise","T","phase","startedAt","timeoutMs","undefined","remainingMs","Date","now","timeoutError","Error","timeout","ReturnType","setTimeout","race","_","reject","clearTimeout","buildTransport","type","authProvider","serverId","config","onAuthorizationUrl","toString","URL","persisted","loadAuthState","headers","transportOpts","requestInit","finalizeConnect","transport","client","version","connect","list","listTools","current","map","t","info","inputSchema","description","closeTransport","close","doConnect","err","String","doDisconnect","doCompleteAuth","callbackUrl","code","searchParams","get","finishAuth","tryAutoConnect","signal","cancelled","token","tokens","catch","state","getState","disconnect","remove","clearAuthState","callTool","args","arguments","Record","readResource","uri","completeAuth","McpServerResource"],"sources":["../../src/resources/McpServerResource.ts"],"sourcesContent":["import { useState, useRef, useEffect, useMemo, useEffectEvent } from \"react\";\nimport { resource } from \"@assistant-ui/tap\";\nimport type { ClientOutput } from \"@assistant-ui/store\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport {\n StreamableHTTPClientTransport,\n type StreamableHTTPClientTransportOptions,\n} from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport { UnauthorizedError } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport type { Transport } from \"@modelcontextprotocol/sdk/shared/transport.js\";\nimport { createOAuthProvider } from \"../auth/createOAuthProvider\";\nimport { buildHeaders } from \"../auth/buildHeaders\";\nimport { assertValidServerId } from \"../utils/serverId\";\nimport type { MCPStorage } from \"./storage/types\";\nimport type {\n MCPAuthConfig,\n MCPConnectionState,\n MCPServerKind,\n MCPServerState,\n MCPToolInfo,\n} from \"../mcp-scope\";\n\nexport type McpServerResourceProps = {\n id: string;\n kind: MCPServerKind;\n name: string;\n url: string;\n icon?: string | undefined;\n auth: MCPAuthConfig;\n storage: MCPStorage;\n redirectUri: string;\n autoConnect: boolean;\n connectionTimeout?: number | undefined;\n onRemove: () => Promise<void>;\n};\n\nconst useMcpServerResource = (\n props: McpServerResourceProps,\n): ClientOutput<\"mcpServer\"> => {\n assertValidServerId(props.id);\n const [connectionState, setConnectionState] =\n useState<MCPConnectionState>(\"disconnected\");\n const [tools, setTools] = useState<MCPToolInfo[]>([]);\n const [lastError, setLastError] = useState<{ message: string } | null>(null);\n const [authorizationUrl, setAuthorizationUrl] = useState<string | null>(null);\n\n const clientRef = useRef<Client | null>(null);\n const transportRef = useRef<StreamableHTTPClientTransport | null>(null);\n\n const withConnectionTimeout = useEffectEvent(\n async <T>(\n promise: Promise<T>,\n phase: \"connecting\" | \"listing tools\",\n startedAt: number,\n ): Promise<T> => {\n const timeoutMs = props.connectionTimeout;\n if (timeoutMs === undefined) return await promise;\n const remainingMs = timeoutMs - (Date.now() - startedAt);\n const timeoutError = () =>\n new Error(\n `MCP server \"${props.id}\" timed out while ${phase} after ${timeoutMs}ms.`,\n );\n if (remainingMs <= 0) throw timeoutError();\n\n let timeout: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n promise,\n new Promise<never>((_, reject) => {\n timeout = setTimeout(() => reject(timeoutError()), remainingMs);\n }),\n ]);\n } finally {\n if (timeout !== undefined) clearTimeout(timeout);\n }\n },\n );\n\n const buildTransport = useEffectEvent(\n async (): Promise<StreamableHTTPClientTransport> => {\n if (props.auth.type === \"oauth\") {\n const authProvider = createOAuthProvider({\n serverId: props.id,\n config: props.auth,\n storage: props.storage,\n redirectUri: props.redirectUri,\n onAuthorizationUrl: (url) => setAuthorizationUrl(url.toString()),\n });\n return new StreamableHTTPClientTransport(new URL(props.url), {\n authProvider,\n });\n }\n if (props.auth.type === \"bearer\") {\n const persisted = await props.storage.loadAuthState(props.id);\n const headers = buildHeaders(props.auth, persisted);\n const transportOpts: StreamableHTTPClientTransportOptions = {};\n if (headers) transportOpts.requestInit = { headers };\n return new StreamableHTTPClientTransport(\n new URL(props.url),\n transportOpts,\n );\n }\n return new StreamableHTTPClientTransport(new URL(props.url));\n },\n );\n\n const finalizeConnect = useEffectEvent(\n async (transport: StreamableHTTPClientTransport) => {\n const client = new Client({\n name: \"assistant-ui-mcp\",\n version: \"0.0.0\",\n });\n const startedAt = Date.now();\n // SDK's StreamableHTTPClientTransport.sessionId is `string | undefined`\n // but Transport.sessionId is declared `string?` — under\n // exactOptionalPropertyTypes the SDK's own classes don't satisfy its\n // Transport interface. Cast to bridge the gap.\n await withConnectionTimeout(\n client.connect(transport as unknown as Transport),\n \"connecting\",\n startedAt,\n );\n // Defer ref assignment until listTools() also succeeds — otherwise a\n // post-connect failure leaves stale refs that `callTool()` would\n // happily walk into, producing confusing SDK errors instead of\n // \"not connected\".\n const list = await withConnectionTimeout(\n client.listTools(),\n \"listing tools\",\n startedAt,\n );\n clientRef.current = client;\n transportRef.current = transport;\n setTools(\n list.tools.map((t) => {\n const info: MCPToolInfo = {\n name: t.name,\n inputSchema: t.inputSchema,\n };\n if (t.description !== undefined) info.description = t.description;\n return info;\n }),\n );\n setConnectionState(\"connected\");\n },\n );\n\n const closeTransport = async () => {\n const t = transportRef.current;\n transportRef.current = null;\n clientRef.current = null;\n if (t) {\n try {\n await t.close();\n } catch {\n // ignore close errors\n }\n }\n };\n\n const doConnect = useEffectEvent(async () => {\n // Close any prior transport/client so a re-connect doesn't leak.\n await closeTransport();\n setConnectionState(\"connecting\");\n setLastError(null);\n setAuthorizationUrl(null);\n // Clear tools so a reconnect (error → connect, or authRequired\n // → connect) doesn't expose the previous attempt's tool list.\n setTools([]);\n let transport: StreamableHTTPClientTransport | null = null;\n try {\n transport = await buildTransport();\n // Don't assign to transportRef until connect succeeds — otherwise a\n // failed `listTools()` leaves an orphaned transport that future\n // doConnect / doDisconnect calls treat as live.\n await finalizeConnect(transport);\n } catch (err) {\n if (err instanceof UnauthorizedError) {\n // OAuth: keep the transport alive so completeAuth can call\n // finishAuth on it. Closing it before storing would leave a\n // closed transport on transportRef.\n transportRef.current = transport;\n setConnectionState(\"authRequired\");\n } else {\n if (transport) {\n try {\n await transport.close();\n } catch {\n // ignore close errors\n }\n }\n setLastError({\n message: err instanceof Error ? err.message : String(err),\n });\n setConnectionState(\"error\");\n }\n }\n });\n\n const doDisconnect = useEffectEvent(async () => {\n setTools([]);\n setAuthorizationUrl(null);\n setConnectionState(\"disconnected\");\n await closeTransport();\n });\n\n const doCompleteAuth = useEffectEvent(async (callbackUrl: string) => {\n setConnectionState(\"authPending\");\n setLastError(null);\n try {\n const url = new URL(callbackUrl);\n const code = url.searchParams.get(\"code\");\n if (!code) throw new Error(\"missing authorization code in callback URL\");\n let transport = transportRef.current;\n if (!transport) {\n transport = await buildTransport();\n transportRef.current = transport;\n }\n await transport.finishAuth(code);\n setAuthorizationUrl(null);\n await finalizeConnect(transport);\n } catch (err) {\n await closeTransport();\n setLastError({\n message: err instanceof Error ? err.message : String(err),\n });\n setConnectionState(\"error\");\n }\n });\n\n const tryAutoConnect = useEffectEvent(\n async (signal: { cancelled: boolean }) => {\n if (!props.autoConnect) return;\n if (props.auth.type === \"none\") {\n void doConnect();\n return;\n }\n // Static connector token short-circuits the storage read.\n if (props.auth.type === \"bearer\" && props.auth.token) {\n void doConnect();\n return;\n }\n const persisted = await props.storage.loadAuthState(props.id);\n if (signal.cancelled) return;\n if (props.auth.type === \"oauth\") {\n if (!persisted?.tokens) return;\n } else if (!persisted?.token) {\n return;\n }\n void doConnect();\n },\n );\n\n // Auto-connect on mount when usable auth exists.\n useEffect(() => {\n const signal = { cancelled: false };\n void tryAutoConnect(signal);\n return () => {\n signal.cancelled = true;\n const t = transportRef.current;\n transportRef.current = null;\n clientRef.current = null;\n if (t) t.close().catch(() => {});\n };\n }, []);\n\n const state = useMemo<MCPServerState>(\n () => ({\n id: props.id,\n kind: props.kind,\n name: props.name,\n url: props.url,\n icon: props.icon,\n connectionState,\n lastError,\n tools,\n authorizationUrl,\n }),\n [\n props.id,\n props.kind,\n props.name,\n props.url,\n props.icon,\n connectionState,\n lastError,\n tools,\n authorizationUrl,\n ],\n );\n\n return {\n getState: () => state,\n connect: doConnect,\n disconnect: doDisconnect,\n remove: async () => {\n await doDisconnect();\n await props.storage.clearAuthState(props.id);\n await props.onRemove();\n },\n callTool: async (name, args) => {\n const client = clientRef.current;\n if (!client) {\n throw new Error(`MCP server \"${props.id}\" is not connected`);\n }\n return await client.callTool({\n name,\n arguments: args as Record<string, unknown> | undefined,\n });\n },\n readResource: async (uri) => {\n const client = clientRef.current;\n if (!client) {\n throw new Error(`MCP server \"${props.id}\" is not connected`);\n }\n return await client.readResource({ uri });\n },\n completeAuth: doCompleteAuth,\n };\n};\n\nexport const McpServerResource = resource(useMcpServerResource);\n"],"mappings":";;;;;;;;;AAoCA,MAAMkC,wBACJC,UAC8B;CAC9BrB,oBAAoBqB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B,MAAMb,GAAE,mBAAoB;GAE7D,OAAO,MAAM2D,SAAOkC,SAAS;IAC3B3F;IACA6F,WAAWD;GACb,CAAC;EACH;EACAG,cAAc,OAAOC,QAAQ;GAC3B,MAAMvC,WAASpC,UAAUyC;GACzB,IAAI,CAACL,UACH,MAAM,IAAIvB,MAAM,eAAevB,MAAMb,GAAE,mBAAoB;GAE7D,OAAO,MAAM2D,SAAOsC,aAAa,EAAEC,IAAI,CAAC;EAC1C;EACAC,cAAcvB;CAChB;AACF;AAEA,MAAawB,oBAAoBrH,SAAS6B,oBAAoB"}
@@ -1,5 +1,6 @@
1
1
  //#region src/utils/serverId.d.ts
2
2
  declare function assertValidServerId(id: string): void;
3
+ declare function assertUniqueServerIds(ids: Iterable<string>): void;
3
4
  //#endregion
4
- export { assertValidServerId };
5
+ export { assertUniqueServerIds, assertValidServerId };
5
6
  //# sourceMappingURL=serverId.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serverId.d.ts","names":[],"sources":["../../src/utils/serverId.ts"],"mappings":";iBAIgB,mBAAA,CAAoB,EAAU"}
1
+ {"version":3,"file":"serverId.d.ts","names":[],"sources":["../../src/utils/serverId.ts"],"mappings":";iBAIgB,mBAAA,CAAoB,EAAU;AAAA,iBAgB9B,qBAAA,CAAsB,GAAqB,EAAhB,QAAQ"}
@@ -4,7 +4,14 @@ function assertValidServerId(id) {
4
4
  if (id.includes("__")) throw new Error(`MCP server id "${id}" must not contain "__" — that sequence is reserved for separating server id from tool name.`);
5
5
  if (id.includes("")) throw new Error(`MCP server id "${id}" must not contain the ASCII Unit Separator (\\x1f) — it is reserved as the list delimiter in primitive id-list selectors.`);
6
6
  }
7
+ function assertUniqueServerIds(ids) {
8
+ const seen = /* @__PURE__ */ new Set();
9
+ for (const id of ids) {
10
+ if (seen.has(id)) throw new Error(`McpManagerResource received duplicate MCP server id "${id}". Server ids must be unique because they are used for lookups, OAuth routing, and tool name prefixes.`);
11
+ seen.add(id);
12
+ }
13
+ }
7
14
  //#endregion
8
- export { assertValidServerId };
15
+ export { assertUniqueServerIds, assertValidServerId };
9
16
 
10
17
  //# sourceMappingURL=serverId.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"serverId.js","names":["assertValidServerId","id","length","Error","includes"],"sources":["../../src/utils/serverId.ts"],"sourcesContent":["// Server ids are joined with tool names via `__` to form unique tool\n// names for the AI SDK. The separator must not appear inside ids,\n// otherwise two distinct `(serverId, toolName)` pairs could produce\n// the same composite name.\nexport function assertValidServerId(id: string): void {\n if (id.length === 0) {\n throw new Error(\"MCP server id must not be empty.\");\n }\n if (id.includes(\"__\")) {\n throw new Error(\n `MCP server id \"${id}\" must not contain \"__\" — that sequence is reserved for separating server id from tool name.`,\n );\n }\n if (id.includes(\"\\x1f\")) {\n throw new Error(\n `MCP server id \"${id}\" must not contain the ASCII Unit Separator (\\\\x1f) — it is reserved as the list delimiter in primitive id-list selectors.`,\n );\n }\n}\n"],"mappings":";AAIA,SAAgBA,oBAAoBC,IAAkB;CACpD,IAAIA,GAAGC,WAAW,GAChB,MAAM,IAAIC,MAAM,kCAAkC;CAEpD,IAAIF,GAAGG,SAAS,IAAI,GAClB,MAAM,IAAID,MACR,kBAAkBF,GAAE,6FACtB;CAEF,IAAIA,GAAGG,SAAS,GAAM,GACpB,MAAM,IAAID,MACR,kBAAkBF,GAAE,2HACtB;AAEJ"}
1
+ {"version":3,"file":"serverId.js","names":["assertValidServerId","id","length","Error","includes","assertUniqueServerIds","ids","Iterable","seen","Set","has","add"],"sources":["../../src/utils/serverId.ts"],"sourcesContent":["// Server ids are joined with tool names via `__` to form unique tool\n// names for the AI SDK. The separator must not appear inside ids,\n// otherwise two distinct `(serverId, toolName)` pairs could produce\n// the same composite name.\nexport function assertValidServerId(id: string): void {\n if (id.length === 0) {\n throw new Error(\"MCP server id must not be empty.\");\n }\n if (id.includes(\"__\")) {\n throw new Error(\n `MCP server id \"${id}\" must not contain \"__\" — that sequence is reserved for separating server id from tool name.`,\n );\n }\n if (id.includes(\"\\x1f\")) {\n throw new Error(\n `MCP server id \"${id}\" must not contain the ASCII Unit Separator (\\\\x1f) — it is reserved as the list delimiter in primitive id-list selectors.`,\n );\n }\n}\n\nexport function assertUniqueServerIds(ids: Iterable<string>): void {\n const seen = new Set<string>();\n for (const id of ids) {\n if (seen.has(id)) {\n throw new Error(\n `McpManagerResource received duplicate MCP server id \"${id}\". Server ids must be unique because they are used for lookups, OAuth routing, and tool name prefixes.`,\n );\n }\n seen.add(id);\n }\n}\n"],"mappings":";AAIA,SAAgBA,oBAAoBC,IAAkB;CACpD,IAAIA,GAAGC,WAAW,GAChB,MAAM,IAAIC,MAAM,kCAAkC;CAEpD,IAAIF,GAAGG,SAAS,IAAI,GAClB,MAAM,IAAID,MACR,kBAAkBF,GAAE,6FACtB;CAEF,IAAIA,GAAGG,SAAS,GAAM,GACpB,MAAM,IAAID,MACR,kBAAkBF,GAAE,2HACtB;AAEJ;AAEA,SAAgBI,sBAAsBC,KAA6B;CACjE,MAAME,uBAAO,IAAIC,IAAY;CAC7B,KAAK,MAAMR,MAAMK,KAAK;EACpB,IAAIE,KAAKE,IAAIT,EAAE,GACb,MAAM,IAAIE,MACR,wDAAwDF,GAAE,uGAC5D;EAEFO,KAAKG,IAAIV,EAAE;CACb;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/react-mcp",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "MCP server configuration and connection primitives for @assistant-ui",
5
5
  "keywords": [
6
6
  "mcp",
@@ -29,10 +29,10 @@
29
29
  "dependencies": {
30
30
  "@modelcontextprotocol/sdk": "^1.29.0",
31
31
  "@radix-ui/react-primitive": "^2.1.6",
32
- "@assistant-ui/core": "0.2.19",
32
+ "@assistant-ui/core": "0.2.20",
33
33
  "@assistant-ui/store": "0.2.19",
34
34
  "@assistant-ui/tap": "0.9.3",
35
- "assistant-stream": "0.3.24"
35
+ "assistant-stream": "0.3.25"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@types/react": "*",
@@ -46,7 +46,8 @@
46
46
  "devDependencies": {
47
47
  "@types/react": "^19.2.17",
48
48
  "react": "^19.2.7",
49
- "@assistant-ui/x-buildutils": "0.0.16"
49
+ "vitest": "^4.1.9",
50
+ "@assistant-ui/x-buildutils": "0.0.17"
50
51
  },
51
52
  "publishConfig": {
52
53
  "access": "public",
@@ -62,6 +63,8 @@
62
63
  "url": "https://github.com/assistant-ui/assistant-ui/issues"
63
64
  },
64
65
  "scripts": {
65
- "build": "aui-build"
66
+ "build": "aui-build",
67
+ "test": "vitest run",
68
+ "test:watch": "vitest"
66
69
  }
67
70
  }
package/src/mcp-scope.ts CHANGED
@@ -19,6 +19,7 @@ export type MCPConnector = {
19
19
  url: string;
20
20
  icon?: string | undefined;
21
21
  auth: MCPAuthConfig;
22
+ connectionTimeout?: number | undefined;
22
23
  };
23
24
 
24
25
  export type MCPCustomServerRecord = {
@@ -26,6 +27,7 @@ export type MCPCustomServerRecord = {
26
27
  name: string;
27
28
  url: string;
28
29
  auth: MCPAuthConfig;
30
+ connectionTimeout?: number | undefined;
29
31
  createdAt: number;
30
32
  };
31
33
 
@@ -93,6 +95,7 @@ export type MCPManagerMethods = {
93
95
  name: string;
94
96
  url: string;
95
97
  auth: MCPAuthConfig;
98
+ connectionTimeout?: number | undefined;
96
99
  }) => Promise<string>;
97
100
  removeServer: (id: string) => Promise<void>;
98
101
  };
@@ -0,0 +1,40 @@
1
+ import { createTapRoot, useResource } from "@assistant-ui/tap";
2
+ import { describe, expect, it } from "vitest";
3
+ import { defineConnector } from "../connector";
4
+ import type { MCPConnector } from "../mcp-scope";
5
+ import { assertUniqueServerIds } from "../utils/serverId";
6
+ import { McpManagerResource } from "./McpManagerResource";
7
+ import { McpMemoryStorage } from "./storage/McpMemoryStorage";
8
+
9
+ const connector = (id: string, name = id): MCPConnector =>
10
+ defineConnector({
11
+ id,
12
+ name,
13
+ url: `https://example.com/${id}/mcp`,
14
+ auth: { type: "none" },
15
+ });
16
+
17
+ const mount = (connectors: MCPConnector[]) =>
18
+ createTapRoot(function Root() {
19
+ return useResource(
20
+ McpManagerResource({
21
+ connectors,
22
+ storage: McpMemoryStorage(),
23
+ autoConnect: false,
24
+ }),
25
+ );
26
+ });
27
+
28
+ describe("McpManagerResource server ids", () => {
29
+ it("throws when connectors reuse an id", () => {
30
+ expect(() =>
31
+ mount([connector("docs", "Docs"), connector("docs", "Internal Docs")]),
32
+ ).toThrow(
33
+ 'McpManagerResource received duplicate MCP server id "docs". Server ids must be unique because they are used for lookups, OAuth routing, and tool name prefixes.',
34
+ );
35
+ });
36
+
37
+ it("allows distinct ids", () => {
38
+ expect(() => assertUniqueServerIds(["docs", "linear"])).not.toThrow();
39
+ });
40
+ });
@@ -11,6 +11,7 @@ import type { Tool } from "assistant-stream";
11
11
  import { McpServerResource } from "./McpServerResource";
12
12
  import { McpLocalStorage } from "./storage/McpLocalStorage";
13
13
  import type { MCPStorageElement } from "./storage/types";
14
+ import { assertUniqueServerIds } from "../utils/serverId";
14
15
  import type {
15
16
  MCPAuthConfig,
16
17
  MCPConnector,
@@ -25,6 +26,8 @@ export type McpManagerResourceProps = {
25
26
  oauthRedirectUri?: string | undefined;
26
27
  /** Connect on mount when usable auth exists. Default true. */
27
28
  autoConnect?: boolean | undefined;
29
+ /** Optional timeout in milliseconds for connect/listTools calls. Disabled by default. */
30
+ connectionTimeout?: number | undefined;
28
31
  };
29
32
 
30
33
  function defaultRedirectUri(): string {
@@ -42,6 +45,7 @@ const useMcpManagerResource = (
42
45
  const connectors = props.connectors ?? NO_CONNECTORS;
43
46
  const autoConnect = props.autoConnect ?? true;
44
47
  const redirectUri = props.oauthRedirectUri ?? defaultRedirectUri();
48
+ const connectionTimeout = props.connectionTimeout;
45
49
 
46
50
  const storageElement = props.storage ?? McpLocalStorage();
47
51
  const storage = useResource(storageElement);
@@ -101,6 +105,11 @@ const useMcpManagerResource = (
101
105
  }, [customServers]);
102
106
 
103
107
  const serverElements = useMemo(() => {
108
+ assertUniqueServerIds([
109
+ ...connectors.map((c) => c.id),
110
+ ...customServers.map((s) => s.id),
111
+ ]);
112
+
104
113
  const connectorElements = connectors.map((c) =>
105
114
  withKey(
106
115
  c.id,
@@ -114,6 +123,7 @@ const useMcpManagerResource = (
114
123
  storage,
115
124
  redirectUri,
116
125
  autoConnect,
126
+ connectionTimeout: c.connectionTimeout ?? connectionTimeout,
117
127
  onRemove: async () => {
118
128
  // connectors cannot be removed
119
129
  },
@@ -132,6 +142,7 @@ const useMcpManagerResource = (
132
142
  storage,
133
143
  redirectUri,
134
144
  autoConnect,
145
+ connectionTimeout: s.connectionTimeout ?? connectionTimeout,
135
146
  onRemove: async () => {
136
147
  setCustomServers((prev) => prev.filter((x) => x.id !== s.id));
137
148
  },
@@ -139,7 +150,14 @@ const useMcpManagerResource = (
139
150
  ),
140
151
  );
141
152
  return [...connectorElements, ...customElements];
142
- }, [connectors, customServers, storage, redirectUri, autoConnect]);
153
+ }, [
154
+ connectors,
155
+ customServers,
156
+ storage,
157
+ redirectUri,
158
+ autoConnect,
159
+ connectionTimeout,
160
+ ]);
143
161
 
144
162
  const lookup = useClientLookup(serverElements);
145
163
 
@@ -208,7 +226,7 @@ const useMcpManagerResource = (
208
226
  },
209
227
  connector: ({ index }) => serverByKind("connector", index),
210
228
  customServer: ({ index }) => serverByKind("custom", index),
211
- addCustomServer: async ({ name, url, auth }) => {
229
+ addCustomServer: async ({ name, url, auth, connectionTimeout }) => {
212
230
  const record: MCPCustomServerRecord = {
213
231
  id:
214
232
  typeof crypto !== "undefined" && "randomUUID" in crypto
@@ -217,6 +235,7 @@ const useMcpManagerResource = (
217
235
  name,
218
236
  url,
219
237
  auth: auth as MCPAuthConfig,
238
+ connectionTimeout,
220
239
  createdAt: Date.now(),
221
240
  };
222
241
  setCustomServers((prev) => [...prev, record]);
@@ -0,0 +1,221 @@
1
+ import { createTapRoot, useResource } from "@assistant-ui/tap";
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
+ import type { MCPStorage } from "./storage/types";
4
+
5
+ const mocks = vi.hoisted(() => {
6
+ const clients: any[] = [];
7
+ const transports: any[] = [];
8
+ const connectResults: Array<() => Promise<void>> = [];
9
+ const listToolsResults: Array<
10
+ () => Promise<{
11
+ tools: Array<{
12
+ name: string;
13
+ description?: string;
14
+ inputSchema: unknown;
15
+ }>;
16
+ }>
17
+ > = [];
18
+
19
+ const Client = vi.fn().mockImplementation(function Client(this: any) {
20
+ const index = clients.length;
21
+ this.connect = vi.fn(() => connectResults[index]?.() ?? Promise.resolve());
22
+ this.listTools = vi.fn(
23
+ () => listToolsResults[index]?.() ?? Promise.resolve({ tools: [] }),
24
+ );
25
+ this.callTool = vi.fn();
26
+ this.readResource = vi.fn();
27
+ clients.push(this);
28
+ });
29
+
30
+ const StreamableHTTPClientTransport = vi
31
+ .fn()
32
+ .mockImplementation(function StreamableHTTPClientTransport(this: any) {
33
+ this.close = vi.fn(() => Promise.resolve());
34
+ this.finishAuth = vi.fn(() => Promise.resolve());
35
+ transports.push(this);
36
+ });
37
+
38
+ return {
39
+ Client,
40
+ StreamableHTTPClientTransport,
41
+ clients,
42
+ transports,
43
+ connectResults,
44
+ listToolsResults,
45
+ };
46
+ });
47
+
48
+ vi.mock("@modelcontextprotocol/sdk/client/index.js", () => ({
49
+ Client: mocks.Client,
50
+ }));
51
+
52
+ vi.mock("@modelcontextprotocol/sdk/client/streamableHttp.js", () => ({
53
+ StreamableHTTPClientTransport: mocks.StreamableHTTPClientTransport,
54
+ }));
55
+
56
+ const { McpServerResource } = await import("./McpServerResource");
57
+
58
+ const never = <T>() => new Promise<T>(() => {});
59
+
60
+ const tick = async () => {
61
+ await Promise.resolve();
62
+ };
63
+
64
+ const waitFor = async (predicate: () => boolean) => {
65
+ for (let i = 0; i < 20; i++) {
66
+ if (predicate()) return;
67
+ await tick();
68
+ }
69
+ expect(predicate()).toBe(true);
70
+ };
71
+
72
+ const createStorage = (): MCPStorage => ({
73
+ loadCustomServers: vi.fn(async () => []),
74
+ saveCustomServers: vi.fn(async () => {}),
75
+ loadAuthState: vi.fn(async () => null),
76
+ saveAuthState: vi.fn(async () => {}),
77
+ clearAuthState: vi.fn(async () => {}),
78
+ });
79
+
80
+ const mount = (props?: { connectionTimeout?: number | undefined }) => {
81
+ const connectionTimeout =
82
+ props && "connectionTimeout" in props ? props.connectionTimeout : 10_000;
83
+
84
+ return createTapRoot(function Root() {
85
+ return useResource(
86
+ McpServerResource({
87
+ id: "docs",
88
+ kind: "connector",
89
+ name: "Docs",
90
+ url: "https://example.com/mcp",
91
+ auth: { type: "none" },
92
+ storage: createStorage(),
93
+ redirectUri: "https://example.com/callback",
94
+ autoConnect: false,
95
+ connectionTimeout,
96
+ onRemove: vi.fn(async () => {}),
97
+ }),
98
+ );
99
+ });
100
+ };
101
+
102
+ describe("McpServerResource connectionTimeout", () => {
103
+ beforeEach(() => {
104
+ vi.useFakeTimers();
105
+ mocks.clients.length = 0;
106
+ mocks.transports.length = 0;
107
+ mocks.connectResults.length = 0;
108
+ mocks.listToolsResults.length = 0;
109
+ mocks.Client.mockClear();
110
+ mocks.StreamableHTTPClientTransport.mockClear();
111
+ });
112
+
113
+ afterEach(() => {
114
+ vi.useRealTimers();
115
+ });
116
+
117
+ it("fails the connection when client.connect hangs", async () => {
118
+ mocks.connectResults.push(() => never());
119
+ const root = mount();
120
+
121
+ try {
122
+ const connectPromise = root.getValue().connect();
123
+ await waitFor(() => mocks.clients[0]?.connect.mock.calls.length === 1);
124
+
125
+ await vi.advanceTimersByTimeAsync(10_000);
126
+ await connectPromise;
127
+
128
+ expect(root.getValue().getState()).toMatchObject({
129
+ connectionState: "error",
130
+ tools: [],
131
+ lastError: {
132
+ message:
133
+ 'MCP server "docs" timed out while connecting after 10000ms.',
134
+ },
135
+ });
136
+ expect(mocks.transports[0].close).toHaveBeenCalledTimes(1);
137
+ } finally {
138
+ root.unmount();
139
+ }
140
+ });
141
+
142
+ it("fails the connection when client.listTools hangs", async () => {
143
+ mocks.listToolsResults.push(() => never());
144
+ const root = mount();
145
+
146
+ try {
147
+ const connectPromise = root.getValue().connect();
148
+ await waitFor(() => mocks.clients[0]?.listTools.mock.calls.length === 1);
149
+
150
+ await vi.advanceTimersByTimeAsync(10_000);
151
+ await connectPromise;
152
+
153
+ expect(root.getValue().getState()).toMatchObject({
154
+ connectionState: "error",
155
+ tools: [],
156
+ lastError: {
157
+ message:
158
+ 'MCP server "docs" timed out while listing tools after 10000ms.',
159
+ },
160
+ });
161
+ expect(mocks.transports[0].close).toHaveBeenCalledTimes(1);
162
+ } finally {
163
+ root.unmount();
164
+ }
165
+ });
166
+
167
+ it("uses one timeout budget for connect and listTools", async () => {
168
+ mocks.connectResults.push(
169
+ () => new Promise<void>((resolve) => setTimeout(resolve, 9_000)),
170
+ );
171
+ mocks.listToolsResults.push(() => never());
172
+ const root = mount();
173
+
174
+ try {
175
+ const connectPromise = root.getValue().connect();
176
+ await waitFor(() => mocks.clients[0]?.connect.mock.calls.length === 1);
177
+
178
+ await vi.advanceTimersByTimeAsync(9_000);
179
+ await waitFor(() => mocks.clients[0]?.listTools.mock.calls.length === 1);
180
+ await vi.advanceTimersByTimeAsync(999);
181
+ await tick();
182
+ expect(root.getValue().getState().connectionState).toBe("connecting");
183
+
184
+ await vi.advanceTimersByTimeAsync(1);
185
+ await connectPromise;
186
+
187
+ expect(root.getValue().getState()).toMatchObject({
188
+ connectionState: "error",
189
+ tools: [],
190
+ lastError: {
191
+ message:
192
+ 'MCP server "docs" timed out while listing tools after 10000ms.',
193
+ },
194
+ });
195
+ expect(mocks.transports[0].close).toHaveBeenCalledTimes(1);
196
+ } finally {
197
+ root.unmount();
198
+ }
199
+ });
200
+
201
+ it("keeps waiting when connectionTimeout is undefined", async () => {
202
+ mocks.connectResults.push(() => never());
203
+ const root = mount({ connectionTimeout: undefined });
204
+
205
+ try {
206
+ void root.getValue().connect();
207
+ await waitFor(() => mocks.clients[0]?.connect.mock.calls.length === 1);
208
+ await vi.advanceTimersByTimeAsync(10_000);
209
+ await tick();
210
+
211
+ expect(root.getValue().getState()).toMatchObject({
212
+ connectionState: "connecting",
213
+ tools: [],
214
+ lastError: null,
215
+ });
216
+ expect(mocks.transports[0].close).not.toHaveBeenCalled();
217
+ } finally {
218
+ root.unmount();
219
+ }
220
+ });
221
+ });
@@ -30,6 +30,7 @@ export type McpServerResourceProps = {
30
30
  storage: MCPStorage;
31
31
  redirectUri: string;
32
32
  autoConnect: boolean;
33
+ connectionTimeout?: number | undefined;
33
34
  onRemove: () => Promise<void>;
34
35
  };
35
36
 
@@ -46,6 +47,35 @@ const useMcpServerResource = (
46
47
  const clientRef = useRef<Client | null>(null);
47
48
  const transportRef = useRef<StreamableHTTPClientTransport | null>(null);
48
49
 
50
+ const withConnectionTimeout = useEffectEvent(
51
+ async <T>(
52
+ promise: Promise<T>,
53
+ phase: "connecting" | "listing tools",
54
+ startedAt: number,
55
+ ): Promise<T> => {
56
+ const timeoutMs = props.connectionTimeout;
57
+ if (timeoutMs === undefined) return await promise;
58
+ const remainingMs = timeoutMs - (Date.now() - startedAt);
59
+ const timeoutError = () =>
60
+ new Error(
61
+ `MCP server "${props.id}" timed out while ${phase} after ${timeoutMs}ms.`,
62
+ );
63
+ if (remainingMs <= 0) throw timeoutError();
64
+
65
+ let timeout: ReturnType<typeof setTimeout> | undefined;
66
+ try {
67
+ return await Promise.race([
68
+ promise,
69
+ new Promise<never>((_, reject) => {
70
+ timeout = setTimeout(() => reject(timeoutError()), remainingMs);
71
+ }),
72
+ ]);
73
+ } finally {
74
+ if (timeout !== undefined) clearTimeout(timeout);
75
+ }
76
+ },
77
+ );
78
+
49
79
  const buildTransport = useEffectEvent(
50
80
  async (): Promise<StreamableHTTPClientTransport> => {
51
81
  if (props.auth.type === "oauth") {
@@ -80,16 +110,25 @@ const useMcpServerResource = (
80
110
  name: "assistant-ui-mcp",
81
111
  version: "0.0.0",
82
112
  });
113
+ const startedAt = Date.now();
83
114
  // SDK's StreamableHTTPClientTransport.sessionId is `string | undefined`
84
115
  // but Transport.sessionId is declared `string?` — under
85
116
  // exactOptionalPropertyTypes the SDK's own classes don't satisfy its
86
117
  // Transport interface. Cast to bridge the gap.
87
- await client.connect(transport as unknown as Transport);
118
+ await withConnectionTimeout(
119
+ client.connect(transport as unknown as Transport),
120
+ "connecting",
121
+ startedAt,
122
+ );
88
123
  // Defer ref assignment until listTools() also succeeds — otherwise a
89
124
  // post-connect failure leaves stale refs that `callTool()` would
90
125
  // happily walk into, producing confusing SDK errors instead of
91
126
  // "not connected".
92
- const list = await client.listTools();
127
+ const list = await withConnectionTimeout(
128
+ client.listTools(),
129
+ "listing tools",
130
+ startedAt,
131
+ );
93
132
  clientRef.current = client;
94
133
  transportRef.current = transport;
95
134
  setTools(
@@ -17,3 +17,15 @@ export function assertValidServerId(id: string): void {
17
17
  );
18
18
  }
19
19
  }
20
+
21
+ export function assertUniqueServerIds(ids: Iterable<string>): void {
22
+ const seen = new Set<string>();
23
+ for (const id of ids) {
24
+ if (seen.has(id)) {
25
+ throw new Error(
26
+ `McpManagerResource received duplicate MCP server id "${id}". Server ids must be unique because they are used for lookups, OAuth routing, and tool name prefixes.`,
27
+ );
28
+ }
29
+ seen.add(id);
30
+ }
31
+ }