@assistant-ui/mcp-docs-server 0.1.9 → 0.1.10

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.
@@ -0,0 +1,324 @@
1
+ ---
2
+ title: Migrating to react-langgraph v0.7
3
+ ---
4
+
5
+ import { Callout } from "fumadocs-ui/components/callout";
6
+ import { Steps, Step } from "fumadocs-ui/components/steps";
7
+
8
+ ## Overview
9
+
10
+ This guide helps you migrate from the previous LangGraph integration pattern to the new simplified API introduced in `@assistant-ui/react-langgraph` v0.7. The new API consolidates thread management directly into `useLangGraphRuntime`, eliminating the need for separate runtime hooks and manual thread state management.
11
+
12
+ ## Key Changes
13
+
14
+ ### 1. Simplified Thread Management
15
+
16
+ The `useLangGraphRuntime` hook now directly handles thread lifecycle:
17
+ - No more `useRemoteThreadListRuntime` wrapper
18
+ - No more separate runtime hook functions
19
+ - Thread management is built into the core runtime
20
+
21
+ ### 2. New `initialize` Parameter
22
+
23
+ The `stream` function now receives an `initialize` parameter that handles thread creation and loading automatically.
24
+
25
+ ### 3. Direct Cloud Integration
26
+
27
+ Cloud persistence can now be configured directly in `useLangGraphRuntime` with the `cloud` parameter.
28
+
29
+ ## Migration Steps
30
+
31
+ <Steps>
32
+
33
+ <Step>
34
+
35
+ ### Update Your Runtime Implementation
36
+
37
+ #### Before (Old Pattern)
38
+
39
+ ```tsx
40
+ import {
41
+ useCloudThreadListRuntime,
42
+ useThreadListItemRuntime,
43
+ } from "@assistant-ui/react";
44
+ import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
45
+
46
+ const useMyLangGraphRuntime = () => {
47
+ const threadListItemRuntime = useThreadListItemRuntime();
48
+
49
+ const runtime = useLangGraphRuntime({
50
+ stream: async function* (messages) {
51
+ const { externalId } = await threadListItemRuntime.initialize();
52
+ if (!externalId) throw new Error("Thread not found");
53
+
54
+ return sendMessage({
55
+ threadId: externalId,
56
+ messages,
57
+ });
58
+ },
59
+ onSwitchToThread: async (externalId) => {
60
+ const state = await getThreadState(externalId);
61
+ return {
62
+ messages: state.values.messages,
63
+ };
64
+ },
65
+ });
66
+
67
+ return runtime;
68
+ };
69
+
70
+ // In your component:
71
+ const runtime = useCloudThreadListRuntime({
72
+ cloud,
73
+ runtimeHook: useMyLangGraphRuntime,
74
+ create: async () => {
75
+ const { thread_id } = await createThread();
76
+ return { externalId: thread_id };
77
+ },
78
+ });
79
+ ```
80
+
81
+ #### After (New Pattern)
82
+
83
+ ```tsx
84
+ import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
85
+
86
+ // Directly in your component:
87
+ const runtime = useLangGraphRuntime({
88
+ cloud, // Optional: for cloud persistence
89
+ stream: async function* (messages, { initialize }) {
90
+ const { externalId } = await initialize();
91
+ if (!externalId) throw new Error("Thread not found");
92
+
93
+ return sendMessage({
94
+ threadId: externalId,
95
+ messages,
96
+ });
97
+ },
98
+ create: async () => {
99
+ const { thread_id } = await createThread();
100
+ return { externalId: thread_id };
101
+ },
102
+ load: async (externalId) => {
103
+ const state = await getThreadState(externalId);
104
+ return {
105
+ messages: state.values.messages,
106
+ };
107
+ },
108
+ });
109
+ ```
110
+
111
+ </Step>
112
+
113
+ <Step>
114
+
115
+ ### Update Import Statements
116
+
117
+ Remove unused imports:
118
+
119
+ ```diff
120
+ - import {
121
+ - useCloudThreadListRuntime,
122
+ - useThreadListItemRuntime,
123
+ - } from "@assistant-ui/react";
124
+ + import { AssistantCloud } from "@assistant-ui/react";
125
+ ```
126
+
127
+ </Step>
128
+
129
+ <Step>
130
+
131
+ ### Simplify Component Structure
132
+
133
+ You no longer need a separate runtime hook function. Everything can be defined directly in your component or provider:
134
+
135
+ ```tsx
136
+ export function MyRuntimeProvider({ children }) {
137
+ const cloud = useMemo(
138
+ () => new AssistantCloud({
139
+ baseUrl: process.env.NEXT_PUBLIC_ASSISTANT_BASE_URL,
140
+ }),
141
+ []
142
+ );
143
+
144
+ const runtime = useLangGraphRuntime({
145
+ cloud,
146
+ // All configuration inline
147
+ stream: async function* (messages, { initialize }) {
148
+ // Your stream implementation
149
+ },
150
+ create: async () => {
151
+ // Your create implementation
152
+ },
153
+ load: async (externalId) => {
154
+ // Your load implementation
155
+ },
156
+ });
157
+
158
+ return (
159
+ <AssistantRuntimeProvider runtime={runtime}>
160
+ {children}
161
+ </AssistantRuntimeProvider>
162
+ );
163
+ }
164
+ ```
165
+
166
+ </Step>
167
+
168
+ <Step>
169
+
170
+ ### Update Method Names
171
+
172
+ Rename methods to match the new API:
173
+
174
+ - `onSwitchToThread` → `load`
175
+ - `onSwitchToNewThread` → handled automatically via `create`
176
+ - Thread ID management is now automatic
177
+
178
+ </Step>
179
+
180
+ </Steps>
181
+
182
+ ## API Reference Changes
183
+
184
+ ### Old API
185
+
186
+ ```typescript
187
+ type OldLangGraphRuntimeOptions = {
188
+ threadId?: string;
189
+ stream: (messages: Message[]) => AsyncGenerator;
190
+ onSwitchToNewThread?: () => Promise<void>;
191
+ onSwitchToThread?: (threadId: string) => Promise<ThreadState>;
192
+ };
193
+ ```
194
+
195
+ ### New API
196
+
197
+ ```typescript
198
+ type NewLangGraphRuntimeOptions = {
199
+ cloud?: AssistantCloud;
200
+ stream: (
201
+ messages: Message[],
202
+ context: {
203
+ initialize: () => Promise<{
204
+ remoteId: string;
205
+ externalId: string | undefined;
206
+ }>
207
+ }
208
+ ) => AsyncGenerator;
209
+ create?: () => Promise<{ externalId: string }>;
210
+ load?: (externalId: string) => Promise<ThreadState>;
211
+ delete?: (externalId: string) => Promise<void>;
212
+ };
213
+ ```
214
+
215
+ ## Benefits of the New API
216
+
217
+ 1. **Simpler Setup**: No need for multiple runtime hooks and wrappers
218
+ 2. **Cleaner Code**: All configuration in one place
219
+ 3. **Better Type Safety**: More explicit types for thread management
220
+ 4. **Automatic Thread Handling**: The runtime manages thread lifecycle internally
221
+ 5. **Optional Cloud Integration**: Add cloud persistence with a single parameter
222
+
223
+ ## Common Migration Issues
224
+
225
+ <Callout type="warning">
226
+ **Breaking Change**: The `threadId` and `onSwitchToNewThread` parameters are no longer supported. Use the new `create` and `load` methods instead.
227
+ </Callout>
228
+
229
+ ### Issue: `threadListItemRuntime` is not defined
230
+
231
+ **Solution**: Remove references to `useThreadListItemRuntime()`. Use the `initialize` parameter in the stream function instead.
232
+
233
+ ### Issue: Thread switching doesn't work
234
+
235
+ **Solution**: Ensure you've implemented both `create` and `load` functions. The runtime needs both to manage thread lifecycle.
236
+
237
+ ### Issue: Cloud persistence not working
238
+
239
+ **Solution**: Pass the `AssistantCloud` instance directly to `useLangGraphRuntime` via the `cloud` parameter.
240
+
241
+ ## Example: Complete Migration
242
+
243
+ Here's a complete before and after example for a typical LangGraph integration:
244
+
245
+ ### Before
246
+
247
+ ```tsx title="runtime-provider.tsx"
248
+ import {
249
+ AssistantCloud,
250
+ AssistantRuntimeProvider,
251
+ useCloudThreadListRuntime,
252
+ useThreadListItemRuntime,
253
+ } from "@assistant-ui/react";
254
+ import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
255
+
256
+ const useMyRuntime = () => {
257
+ const threadListItemRuntime = useThreadListItemRuntime();
258
+
259
+ return useLangGraphRuntime({
260
+ stream: async function* (messages) {
261
+ const { externalId } = await threadListItemRuntime.initialize();
262
+ // ... implementation
263
+ },
264
+ onSwitchToThread: async (externalId) => {
265
+ // ... implementation
266
+ },
267
+ });
268
+ };
269
+
270
+ export function Provider({ children }) {
271
+ const cloud = new AssistantCloud({ /* config */ });
272
+
273
+ const runtime = useCloudThreadListRuntime({
274
+ cloud,
275
+ runtimeHook: useMyRuntime,
276
+ create: async () => { /* ... */ },
277
+ });
278
+
279
+ return (
280
+ <AssistantRuntimeProvider runtime={runtime}>
281
+ {children}
282
+ </AssistantRuntimeProvider>
283
+ );
284
+ }
285
+ ```
286
+
287
+ ### After
288
+
289
+ ```tsx title="runtime-provider.tsx"
290
+ import {
291
+ AssistantCloud,
292
+ AssistantRuntimeProvider,
293
+ } from "@assistant-ui/react";
294
+ import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
295
+
296
+ export function Provider({ children }) {
297
+ const cloud = new AssistantCloud({ /* config */ });
298
+
299
+ const runtime = useLangGraphRuntime({
300
+ cloud,
301
+ stream: async function* (messages, { initialize }) {
302
+ const { externalId } = await initialize();
303
+ // ... implementation
304
+ },
305
+ create: async () => { /* ... */ },
306
+ load: async (externalId) => {
307
+ // ... implementation (formerly onSwitchToThread)
308
+ },
309
+ });
310
+
311
+ return (
312
+ <AssistantRuntimeProvider runtime={runtime}>
313
+ {children}
314
+ </AssistantRuntimeProvider>
315
+ );
316
+ }
317
+ ```
318
+
319
+ ## Need Help?
320
+
321
+ If you encounter issues during migration:
322
+ 1. Check the updated [LangGraph documentation](/docs/runtimes/langgraph)
323
+ 2. Review the [example implementation](https://github.com/assistant-ui/assistant-ui/tree/main/examples/with-langgraph)
324
+ 3. Report issues on [GitHub](https://github.com/assistant-ui/assistant-ui/issues)
@@ -61,7 +61,6 @@ npm install @assistant-ui/react @assistant-ui/react-ui @assistant-ui/react-langg
61
61
  ```tsx twoslash title="@/api/api/[...path]/route.ts"
62
62
  import { NextRequest, NextResponse } from "next/server";
63
63
 
64
-
65
64
  function getCorsHeaders() {
66
65
  return {
67
66
  "Access-Control-Allow-Origin": "*",
@@ -188,7 +187,6 @@ export const sendMessage = async (params: {
188
187
  // ---cut---
189
188
  "use client";
190
189
 
191
- import { useRef } from "react";
192
190
  import { Thread } from "@/components/assistant-ui";
193
191
  import { AssistantRuntimeProvider } from "@assistant-ui/react";
194
192
  import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
@@ -196,27 +194,21 @@ import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
196
194
  import { createThread, getThreadState, sendMessage } from "@/lib/chatApi";
197
195
 
198
196
  export function MyAssistant() {
199
- const threadIdRef = useRef<string | undefined>();
200
197
  const runtime = useLangGraphRuntime({
201
- threadId: threadIdRef.current,
202
- stream: async (messages) => {
203
- if (!threadIdRef.current) {
204
- const { thread_id } = await createThread();
205
- threadIdRef.current = thread_id;
206
- }
207
- const threadId = threadIdRef.current;
198
+ stream: async (messages, { initialize }) => {
199
+ const { externalId } = await initialize();
200
+ if (!externalId) throw new Error("Thread not found");
208
201
  return sendMessage({
209
- threadId,
202
+ threadId: externalId,
210
203
  messages,
211
204
  });
212
205
  },
213
- onSwitchToNewThread: async () => {
206
+ create: async () => {
214
207
  const { thread_id } = await createThread();
215
- threadIdRef.current = thread_id;
208
+ return { externalId: thread_id };
216
209
  },
217
- onSwitchToThread: async (threadId) => {
218
- const state = await getThreadState(threadId);
219
- threadIdRef.current = threadId;
210
+ load: async (externalId) => {
211
+ const state = await getThreadState(externalId);
220
212
  return {
221
213
  messages: state.values.messages,
222
214
  interrupts: state.tasks[0]?.interrupts,
@@ -306,14 +298,57 @@ import { convertLangChainMessages } from "@assistant-ui/react-langgraph";
306
298
  const threadMessage = convertLangChainMessages(langChainMessage);
307
299
  ```
308
300
 
309
- ## Interrupt Persistence
301
+ ## Thread Management
302
+
303
+ ### Basic Thread Support
304
+
305
+ The `useLangGraphRuntime` hook now includes built-in thread management capabilities:
306
+
307
+ ```typescript
308
+ const runtime = useLangGraphRuntime({
309
+ stream: async (messages, { initialize }) => {
310
+ // initialize() creates or loads a thread and returns its IDs
311
+ const { remoteId, externalId } = await initialize();
312
+ // Use externalId (your backend's thread ID) for API calls
313
+ return sendMessage({ threadId: externalId, messages });
314
+ },
315
+ create: async () => {
316
+ // Called when creating a new thread
317
+ const { thread_id } = await createThread();
318
+ return { externalId: thread_id };
319
+ },
320
+ load: async (externalId) => {
321
+ // Called when loading an existing thread
322
+ const state = await getThreadState(externalId);
323
+ return {
324
+ messages: state.values.messages,
325
+ interrupts: state.tasks[0]?.interrupts,
326
+ };
327
+ },
328
+ });
329
+ ```
310
330
 
311
- LangGraph supports interrupting the execution flow to request user input or handle specific interactions. These interrupts can be persisted and restored when switching between threads. This means that if a user switches away from a thread during an interaction (like waiting for user approval), the interaction state will be preserved when they return to that thread.
331
+ ### Cloud Persistence
332
+
333
+ For persistent thread history across sessions, integrate with assistant-cloud:
334
+
335
+ ```typescript
336
+ const runtime = useLangGraphRuntime({
337
+ cloud: new AssistantCloud({
338
+ baseUrl: process.env.NEXT_PUBLIC_ASSISTANT_BASE_URL,
339
+ }),
340
+ // ... stream, create, load functions
341
+ });
342
+ ```
343
+
344
+ See the [Cloud Persistence guide](/docs/cloud/persistence/langgraph) for detailed setup instructions.
345
+
346
+ ## Interrupt Persistence
312
347
 
313
- To handle interrupts in your application:
348
+ LangGraph supports interrupting the execution flow to request user input or handle specific interactions. These interrupts can be persisted and restored when switching between threads:
314
349
 
315
350
  1. Make sure your thread state type includes the `interrupts` field
316
- 2. Return the interrupts from `onSwitchToThread` along with the messages
351
+ 2. Return the interrupts from the `load` function along with the messages
317
352
  3. The runtime will automatically restore the interrupt state when switching threads
318
353
 
319
354
  This feature is particularly useful for applications that require user approval flows, multi-step forms, or any other interactive elements that might span multiple thread switches.