@assistant-ui/react-mcp 0.1.2 → 0.1.4

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.
@@ -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;