@assistant-ui/react-mcp 0.1.3 → 0.1.5

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.
Files changed (30) hide show
  1. package/dist/context/McpConnectorByIndexProvider.js +16 -19
  2. package/dist/context/McpConnectorByIndexProvider.js.map +1 -1
  3. package/dist/context/McpCustomServerByIndexProvider.js +16 -19
  4. package/dist/context/McpCustomServerByIndexProvider.js.map +1 -1
  5. package/dist/context/McpServerByIdProvider.js +16 -19
  6. package/dist/context/McpServerByIdProvider.js.map +1 -1
  7. package/dist/hooks/useMcpOAuthCallback.d.ts.map +1 -1
  8. package/dist/hooks/useMcpOAuthCallback.js +21 -6
  9. package/dist/hooks/useMcpOAuthCallback.js.map +1 -1
  10. package/dist/primitives/server/McpServerRemoveButton.js +2 -1
  11. package/dist/primitives/server/McpServerRemoveButton.js.map +1 -1
  12. package/dist/resources/McpManagerResource.d.ts.map +1 -1
  13. package/dist/resources/McpManagerResource.js +155 -139
  14. package/dist/resources/McpManagerResource.js.map +1 -1
  15. package/dist/resources/McpServerResource.d.ts.map +1 -1
  16. package/dist/resources/McpServerResource.js +7 -2
  17. package/dist/resources/McpServerResource.js.map +1 -1
  18. package/package.json +16 -13
  19. package/src/context/McpConnectorByIndexProvider.tsx +8 -4
  20. package/src/context/McpCustomServerByIndexProvider.tsx +8 -4
  21. package/src/context/McpServerByIdProvider.tsx +8 -4
  22. package/src/hooks/useMcpOAuthCallback.test.ts +101 -2
  23. package/src/hooks/useMcpOAuthCallback.tsx +41 -5
  24. package/src/primitives/server/McpServerRemoveButton.test.tsx +71 -0
  25. package/src/primitives/server/McpServerRemoveButton.tsx +1 -1
  26. package/src/resources/McpManagerResource.test.ts +108 -1
  27. package/src/resources/McpManagerResource.ts +23 -13
  28. package/src/resources/McpServerResource.test.ts +37 -3
  29. package/src/resources/McpServerResource.ts +9 -2
  30. package/LICENSE +0 -21
@@ -1,5 +1,6 @@
1
- import { createTapRoot, useResource } from "@assistant-ui/tap";
1
+ import { createTapRoot, resource, useResource } from "@assistant-ui/tap";
2
2
  import { describe, expect, it, vi } from "vitest";
3
+ import { useState } from "react";
3
4
  import { defineConnector } from "../connector";
4
5
  import type { MCPConnector } from "../mcp-scope";
5
6
  import { assertUniqueServerIds } from "../utils/serverId";
@@ -208,3 +209,109 @@ describe("McpManagerResource storage failures", () => {
208
209
  }
209
210
  });
210
211
  });
212
+
213
+ describe("McpManagerResource storage ordering", () => {
214
+ it("persists custom server updates in invocation order", async () => {
215
+ let resolveFirstSave: (() => void) | undefined;
216
+ const firstSave = new Promise<void>((resolve) => {
217
+ resolveFirstSave = resolve;
218
+ });
219
+ let blockNextSave = false;
220
+ const persistedSnapshots: string[][] = [];
221
+ const saveCustomServers = vi.fn(async (records: { name: string }[]) => {
222
+ persistedSnapshots.push(records.map((record) => record.name));
223
+ if (blockNextSave) {
224
+ blockNextSave = false;
225
+ await firstSave;
226
+ }
227
+ });
228
+ const root = mount(
229
+ [],
230
+ McpCustomStorage({
231
+ loadCustomServers: vi.fn(async () => []),
232
+ saveCustomServers,
233
+ loadAuthState: vi.fn(async () => null),
234
+ saveAuthState: vi.fn(async () => {}),
235
+ clearAuthState: vi.fn(async () => {}),
236
+ }),
237
+ );
238
+
239
+ try {
240
+ await vi.waitFor(() =>
241
+ expect(root.getValue().getState().isHydrated).toBe(true),
242
+ );
243
+ await vi.waitFor(() => expect(saveCustomServers).toHaveBeenCalled());
244
+ saveCustomServers.mockClear();
245
+ persistedSnapshots.length = 0;
246
+ blockNextSave = true;
247
+
248
+ await root.getValue().addCustomServer({
249
+ name: "Docs",
250
+ url: "https://example.com/docs/mcp",
251
+ auth: { type: "none" },
252
+ });
253
+ await vi.waitFor(() =>
254
+ expect(saveCustomServers).toHaveBeenCalledTimes(1),
255
+ );
256
+
257
+ await root.getValue().addCustomServer({
258
+ name: "Linear",
259
+ url: "https://example.com/linear/mcp",
260
+ auth: { type: "none" },
261
+ });
262
+
263
+ await new Promise((resolve) => setTimeout(resolve, 0));
264
+ expect(saveCustomServers).toHaveBeenCalledTimes(1);
265
+
266
+ resolveFirstSave?.();
267
+ await vi.waitFor(() =>
268
+ expect(saveCustomServers).toHaveBeenCalledTimes(2),
269
+ );
270
+ expect(persistedSnapshots).toEqual([["Docs"], ["Docs", "Linear"]]);
271
+ } finally {
272
+ resolveFirstSave?.();
273
+ root.unmount();
274
+ }
275
+ });
276
+
277
+ it("does not persist unchanged servers when inline storage rerenders", async () => {
278
+ const saveCustomServers = vi.fn(async () => {});
279
+ let rerender = () => {};
280
+ const DynamicManager = resource(function useDynamicManager() {
281
+ const [, setVersion] = useState(0);
282
+ rerender = () => setVersion((version) => version + 1);
283
+
284
+ return useResource(
285
+ McpManagerResource({
286
+ connectors: [],
287
+ storage: McpCustomStorage({
288
+ loadCustomServers: vi.fn(async () => []),
289
+ saveCustomServers,
290
+ loadAuthState: vi.fn(async () => null),
291
+ saveAuthState: vi.fn(async () => {}),
292
+ clearAuthState: vi.fn(async () => {}),
293
+ }),
294
+ autoConnect: false,
295
+ }),
296
+ );
297
+ });
298
+ const root = createTapRoot(function Root() {
299
+ return useResource(DynamicManager());
300
+ });
301
+
302
+ try {
303
+ await vi.waitFor(() =>
304
+ expect(root.getValue().getState().isHydrated).toBe(true),
305
+ );
306
+ await vi.waitFor(() => expect(saveCustomServers).toHaveBeenCalled());
307
+ saveCustomServers.mockClear();
308
+
309
+ rerender();
310
+
311
+ await new Promise((resolve) => setTimeout(resolve, 0));
312
+ expect(saveCustomServers).not.toHaveBeenCalled();
313
+ } finally {
314
+ root.unmount();
315
+ }
316
+ });
317
+ });
@@ -10,7 +10,7 @@ import { ModelContext } from "@assistant-ui/core/store";
10
10
  import type { Tool } from "assistant-stream";
11
11
  import { McpServerResource } from "./McpServerResource";
12
12
  import { McpLocalStorage } from "./storage/McpLocalStorage";
13
- import type { MCPStorageElement } from "./storage/types";
13
+ import type { MCPStorage, MCPStorageElement } from "./storage/types";
14
14
  import { assertUniqueServerIds } from "../utils/serverId";
15
15
  import type {
16
16
  MCPAuthConfig,
@@ -49,6 +49,17 @@ const reportCustomStorageFailure = (
49
49
  );
50
50
  };
51
51
 
52
+ const persistCustomServers = async (
53
+ storage: MCPStorage,
54
+ records: MCPCustomServerRecord[],
55
+ ) => {
56
+ try {
57
+ await storage.saveCustomServers(records);
58
+ } catch (error) {
59
+ reportCustomStorageFailure("save", error);
60
+ }
61
+ };
62
+
52
63
  const useMcpManagerResource = (
53
64
  props: McpManagerResourceProps,
54
65
  ): ClientOutput<"mcp"> => {
@@ -66,6 +77,12 @@ const useMcpManagerResource = (
66
77
  const [isHydrated, setIsHydrated] = useState(false);
67
78
 
68
79
  const hydratedRef = useRef(false);
80
+ const storageRef = useRef(storage);
81
+ const persistenceQueueRef = useRef(Promise.resolve());
82
+
83
+ useEffect(() => {
84
+ storageRef.current = storage;
85
+ }, [storage]);
69
86
 
70
87
  const hydrate = useEffectEvent(async (signal: { cancelled: boolean }) => {
71
88
  const markHydrated = () => {
@@ -106,19 +123,12 @@ const useMcpManagerResource = (
106
123
  };
107
124
  }, []);
108
125
 
109
- const persistCustomServers = useEffectEvent(
110
- async (records: MCPCustomServerRecord[]) => {
111
- if (!hydratedRef.current) return;
112
- try {
113
- await storage.saveCustomServers(records);
114
- } catch (error) {
115
- reportCustomStorageFailure("save", error);
116
- }
117
- },
118
- );
119
-
120
126
  useEffect(() => {
121
- void persistCustomServers(customServers);
127
+ if (!hydratedRef.current) return;
128
+ const targetStorage = storageRef.current;
129
+ persistenceQueueRef.current = persistenceQueueRef.current.then(() =>
130
+ persistCustomServers(targetStorage, customServers),
131
+ );
122
132
  }, [customServers]);
123
133
 
124
134
  const serverElements = useMemo(() => {
@@ -150,6 +150,9 @@ const mount = (
150
150
  connectionTimeout?: number | undefined;
151
151
  cache?: { readonly defaultTtlMs?: number } | undefined;
152
152
  elicitation?: boolean | undefined;
153
+ kind?: "connector" | "custom" | undefined;
154
+ storage?: MCPStorage | undefined;
155
+ onRemove?: (() => Promise<void>) | undefined;
153
156
  },
154
157
  onMount?: (server: ClientOutput<"mcpServer">) => void,
155
158
  ) => {
@@ -160,11 +163,11 @@ const mount = (
160
163
  const server = useResource(
161
164
  McpServerResource({
162
165
  id: "docs",
163
- kind: "connector",
166
+ kind: props?.kind ?? "connector",
164
167
  name: "Docs",
165
168
  url: "https://example.com/mcp",
166
169
  auth: props?.auth ?? { type: "none" },
167
- storage: createStorage(),
170
+ storage: props?.storage ?? createStorage(),
168
171
  redirectUri: "https://example.com/callback",
169
172
  autoConnect: false,
170
173
  connectionTimeout,
@@ -172,7 +175,7 @@ const mount = (
172
175
  ...(props?.elicitation !== undefined
173
176
  ? { elicitation: props.elicitation }
174
177
  : {}),
175
- onRemove: vi.fn(async () => {}),
178
+ onRemove: props?.onRemove ?? vi.fn(async () => {}),
176
179
  }),
177
180
  );
178
181
  useEffect(() => {
@@ -1200,6 +1203,37 @@ describe("McpServerResource tools listChanged", () => {
1200
1203
  });
1201
1204
  });
1202
1205
 
1206
+ describe("McpServerResource removal", () => {
1207
+ beforeEach(() => {
1208
+ resetMocks();
1209
+ });
1210
+
1211
+ it("exposes auth storage failures and preserves the server", async () => {
1212
+ const error = new Error("storage unavailable");
1213
+ const storage = createStorage();
1214
+ vi.mocked(storage.clearAuthState).mockRejectedValueOnce(error);
1215
+ const onRemove = vi.fn(async () => {});
1216
+ const root = mount({ kind: "custom", storage, onRemove });
1217
+
1218
+ try {
1219
+ await expect(root.getValue().remove()).rejects.toBe(error);
1220
+ await waitForResourceUpdate(
1221
+ () =>
1222
+ root.getValue().getState().lastError?.message ===
1223
+ "storage unavailable",
1224
+ );
1225
+
1226
+ expect(root.getValue().getState()).toMatchObject({
1227
+ connectionState: "disconnected",
1228
+ lastError: { message: "storage unavailable" },
1229
+ });
1230
+ expect(onRemove).not.toHaveBeenCalled();
1231
+ } finally {
1232
+ root.unmount();
1233
+ }
1234
+ });
1235
+ });
1236
+
1203
1237
  describe("McpServerResource resource methods", () => {
1204
1238
  beforeEach(() => {
1205
1239
  mocks.clients.length = 0;
@@ -539,8 +539,15 @@ const useMcpServerResource = (
539
539
  disconnect: doDisconnect,
540
540
  remove: async () => {
541
541
  await doDisconnect();
542
- await props.storage.clearAuthState(props.id);
543
- await props.onRemove();
542
+ try {
543
+ await props.storage.clearAuthState(props.id);
544
+ await props.onRemove();
545
+ } catch (err) {
546
+ setLastError({
547
+ message: err instanceof Error ? err.message : String(err),
548
+ });
549
+ throw err;
550
+ }
544
551
  },
545
552
  callTool: async (name, args) => {
546
553
  const client = clientRef.current;
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 AgentbaseAI Inc.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.