@mcp-ts/sdk 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -40
- package/dist/adapters/agui-middleware.d.mts +2 -21
- package/dist/adapters/agui-middleware.d.ts +2 -21
- package/dist/adapters/agui-middleware.js +30 -50
- package/dist/adapters/agui-middleware.js.map +1 -1
- package/dist/adapters/agui-middleware.mjs +31 -50
- package/dist/adapters/agui-middleware.mjs.map +1 -1
- package/dist/client/react.d.mts +51 -331
- package/dist/client/react.d.ts +51 -331
- package/dist/client/react.js +76 -281
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs +78 -277
- package/dist/client/react.mjs.map +1 -1
- package/package.json +1 -1
- package/src/adapters/agui-middleware.ts +60 -109
- package/src/client/react/index.ts +16 -30
- package/src/client/react/use-mcp-apps.tsx +214 -0
- package/src/client/react/agui-subscriber.ts +0 -275
- package/src/client/react/use-agui-subscriber.ts +0 -270
- package/src/client/react/use-mcp-app-iframe.ts +0 -164
package/dist/client/react.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { c as McpConnectionState, M as McpConnectionEvent } from '../events-Bgez
|
|
|
4
4
|
export { D as Disposable, a as DisposableStore, E as Emitter, b as Event, d as McpObservabilityEvent } from '../events-BgeztGYZ.js';
|
|
5
5
|
import { T as ToolInfo, k as FinishAuthResult, n as ListToolsRpcResult, L as ListPromptsResult, l as ListResourcesResult } from '../types-CLccx9wW.js';
|
|
6
6
|
export { p as McpRpcRequest, q as McpRpcResponse } from '../types-CLccx9wW.js';
|
|
7
|
-
import
|
|
7
|
+
import React$1 from 'react';
|
|
8
8
|
import '@modelcontextprotocol/sdk/types.js';
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -66,7 +66,7 @@ interface McpConnection {
|
|
|
66
66
|
error?: string;
|
|
67
67
|
connectedAt?: Date;
|
|
68
68
|
}
|
|
69
|
-
interface McpClient {
|
|
69
|
+
interface McpClient$1 {
|
|
70
70
|
/**
|
|
71
71
|
* All connections
|
|
72
72
|
*/
|
|
@@ -157,7 +157,7 @@ interface McpClient {
|
|
|
157
157
|
/**
|
|
158
158
|
* React hook for MCP connection management with SSE
|
|
159
159
|
*/
|
|
160
|
-
declare function useMcp(options: UseMcpOptions): McpClient;
|
|
160
|
+
declare function useMcp(options: UseMcpOptions): McpClient$1;
|
|
161
161
|
|
|
162
162
|
/**
|
|
163
163
|
* Hook to host an MCP App in a React component
|
|
@@ -184,350 +184,70 @@ declare function useAppHost(client: SSEClient, iframeRef: React.RefObject<HTMLIF
|
|
|
184
184
|
};
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
|
-
*
|
|
188
|
-
* Manages iframe lifecycle, app host communication, and tool data flow
|
|
189
|
-
*/
|
|
190
|
-
|
|
191
|
-
interface McpAppIframeProps {
|
|
192
|
-
/**
|
|
193
|
-
* The resource URI of the MCP app to load
|
|
194
|
-
*/
|
|
195
|
-
resourceUri: string;
|
|
196
|
-
/**
|
|
197
|
-
* The session ID for the MCP connection
|
|
198
|
-
*/
|
|
199
|
-
sessionId: string;
|
|
200
|
-
/**
|
|
201
|
-
* Tool input arguments to send to the app
|
|
202
|
-
*/
|
|
203
|
-
toolInput?: Record<string, unknown>;
|
|
204
|
-
/**
|
|
205
|
-
* Tool execution result to send to the app
|
|
206
|
-
*/
|
|
207
|
-
toolResult?: unknown;
|
|
208
|
-
/**
|
|
209
|
-
* Current status of the tool execution
|
|
210
|
-
*/
|
|
211
|
-
toolStatus?: 'executing' | 'inProgress' | 'complete';
|
|
212
|
-
/**
|
|
213
|
-
* SSE client instance for MCP operations
|
|
214
|
-
*/
|
|
215
|
-
sseClient: SSEClient;
|
|
216
|
-
}
|
|
217
|
-
interface McpAppIframeResult {
|
|
218
|
-
/**
|
|
219
|
-
* Ref to attach to the iframe element
|
|
220
|
-
*/
|
|
221
|
-
iframeRef: React.RefObject<HTMLIFrameElement>;
|
|
222
|
-
/**
|
|
223
|
-
* Whether the app has been successfully launched
|
|
224
|
-
*/
|
|
225
|
-
isLaunched: boolean;
|
|
226
|
-
/**
|
|
227
|
-
* Error that occurred during initialization or execution
|
|
228
|
-
*/
|
|
229
|
-
error: Error | null;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Hook to manage MCP app iframe lifecycle and communication
|
|
233
|
-
*
|
|
234
|
-
* Handles:
|
|
235
|
-
* - Iframe setup and host initialization
|
|
236
|
-
* - App launching with resource preloading
|
|
237
|
-
* - Tool input and result communication
|
|
238
|
-
* - Error tracking
|
|
239
|
-
*
|
|
240
|
-
* Returns refs and state for UI rendering - styling is left to the user.
|
|
241
|
-
*
|
|
242
|
-
* @param props - Configuration for the iframe
|
|
243
|
-
* @returns Iframe ref, launch state, and error state
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* const { iframeRef, isLaunched, error } = useMcpAppIframe({
|
|
247
|
-
* resourceUri: "https://example.com/app",
|
|
248
|
-
* sessionId: "session-123",
|
|
249
|
-
* toolInput: myInput,
|
|
250
|
-
* toolResult: myResult,
|
|
251
|
-
* toolStatus: "complete",
|
|
252
|
-
* sseClient: sseClient,
|
|
253
|
-
* });
|
|
187
|
+
* MCP Apps Hook
|
|
254
188
|
*
|
|
255
|
-
*
|
|
256
|
-
* <div className="my-custom-container">
|
|
257
|
-
* <iframe ref={iframeRef} className="my-iframe-style" />
|
|
258
|
-
* {!isLaunched && <p>Loading...</p>}
|
|
259
|
-
* {error && <p>Error: {error.message}</p>}
|
|
260
|
-
* </div>
|
|
261
|
-
* );
|
|
189
|
+
* Provides utilities for rendering interactive UI components from MCP servers.
|
|
262
190
|
*/
|
|
263
|
-
declare function useMcpAppIframe({ resourceUri, sessionId, toolInput, toolResult, toolStatus, sseClient, }: McpAppIframeProps): McpAppIframeResult;
|
|
264
191
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
resourceUri: string;
|
|
283
|
-
/** Session ID for the MCP connection */
|
|
284
|
-
sessionId?: string;
|
|
285
|
-
/** Name of the tool that was called */
|
|
286
|
-
toolName: string;
|
|
287
|
-
/** Tool execution result (if available) */
|
|
288
|
-
result?: any;
|
|
289
|
-
/** Tool input arguments */
|
|
290
|
-
input?: Record<string, unknown>;
|
|
291
|
-
/** Tool execution status */
|
|
292
|
-
status?: 'executing' | 'inProgress' | 'complete';
|
|
192
|
+
interface McpClient {
|
|
193
|
+
connections: Array<{
|
|
194
|
+
sessionId: string;
|
|
195
|
+
tools: Array<{
|
|
196
|
+
name: string;
|
|
197
|
+
mcpApp?: {
|
|
198
|
+
resourceUri: string;
|
|
199
|
+
};
|
|
200
|
+
_meta?: {
|
|
201
|
+
ui?: {
|
|
202
|
+
resourceUri?: string;
|
|
203
|
+
};
|
|
204
|
+
'ui/resourceUri'?: string;
|
|
205
|
+
};
|
|
206
|
+
}>;
|
|
207
|
+
}>;
|
|
208
|
+
sseClient?: SSEClient | null;
|
|
293
209
|
}
|
|
294
|
-
|
|
295
|
-
* Event handler for MCP app events
|
|
296
|
-
*/
|
|
297
|
-
type McpAppEventHandler = (event: McpAppEvent) => void;
|
|
298
|
-
/**
|
|
299
|
-
* Event handler for tool call events
|
|
300
|
-
*/
|
|
301
|
-
interface ToolCallEventData {
|
|
302
|
-
toolCallId: string;
|
|
210
|
+
interface McpAppMetadata {
|
|
303
211
|
toolName: string;
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
status: 'start' | 'args' | 'end' | 'result';
|
|
307
|
-
}
|
|
308
|
-
type ToolCallEventHandler = (event: ToolCallEventData) => void;
|
|
309
|
-
/**
|
|
310
|
-
* Configuration for MCP app subscriber
|
|
311
|
-
*/
|
|
312
|
-
interface McpAppSubscriberConfig {
|
|
313
|
-
/** Handler for MCP app UI events */
|
|
314
|
-
onMcpApp?: McpAppEventHandler;
|
|
315
|
-
/** Handler for general tool call events */
|
|
316
|
-
onToolCall?: ToolCallEventHandler;
|
|
317
|
-
/** Custom event name to listen for (default: 'mcp-apps-ui') */
|
|
318
|
-
eventName?: string;
|
|
212
|
+
resourceUri: string;
|
|
213
|
+
sessionId: string;
|
|
319
214
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
*
|
|
329
|
-
* @example
|
|
330
|
-
* ```typescript
|
|
331
|
-
* import { createMcpAppSubscriber } from '@mcp-ts/sdk/client/react';
|
|
332
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
333
|
-
*
|
|
334
|
-
* const agent = new HttpAgent({ url: '/api/agent' });
|
|
335
|
-
*
|
|
336
|
-
* const subscriber = createMcpAppSubscriber({
|
|
337
|
-
* onMcpApp: (event) => {
|
|
338
|
-
* console.log('MCP App:', event.resourceUri);
|
|
339
|
-
* // Render MCP app UI
|
|
340
|
-
* },
|
|
341
|
-
* onToolCall: (event) => {
|
|
342
|
-
* console.log('Tool Call:', event.toolName, event.status);
|
|
343
|
-
* }
|
|
344
|
-
* });
|
|
345
|
-
*
|
|
346
|
-
* const { unsubscribe } = agent.subscribe(subscriber);
|
|
347
|
-
* ```
|
|
348
|
-
*/
|
|
349
|
-
declare function createMcpAppSubscriber(config: McpAppSubscriberConfig): AgentSubscriber;
|
|
350
|
-
/**
|
|
351
|
-
* Subscribes to MCP app events from an AG-UI agent
|
|
352
|
-
*
|
|
353
|
-
* Convenience function that creates a subscriber and attaches it to an agent.
|
|
354
|
-
* Returns an unsubscribe function to clean up the subscription.
|
|
355
|
-
*
|
|
356
|
-
* @param agent - AG-UI agent instance
|
|
357
|
-
* @param config - Configuration with event handlers
|
|
358
|
-
* @returns Unsubscribe function
|
|
359
|
-
*
|
|
360
|
-
* @example
|
|
361
|
-
* ```typescript
|
|
362
|
-
* const unsubscribe = subscribeMcpAppEvents(agent, {
|
|
363
|
-
* onMcpApp: (event) => {
|
|
364
|
-
* renderMcpApp(event.resourceUri, event.sessionId);
|
|
365
|
-
* }
|
|
366
|
-
* });
|
|
367
|
-
*
|
|
368
|
-
* // Later, to cleanup:
|
|
369
|
-
* unsubscribe();
|
|
370
|
-
* ```
|
|
371
|
-
*/
|
|
372
|
-
declare function subscribeMcpAppEvents(agent: AbstractAgent, config: McpAppSubscriberConfig): () => void;
|
|
373
|
-
/**
|
|
374
|
-
* Manager for MCP app events with built-in state management
|
|
375
|
-
*
|
|
376
|
-
* Provides a higher-level API for managing MCP app events with automatic
|
|
377
|
-
* state tracking. Useful for React contexts or state management systems.
|
|
378
|
-
*/
|
|
379
|
-
declare class McpAppEventManager {
|
|
380
|
-
private events;
|
|
381
|
-
private toolCalls;
|
|
382
|
-
private listeners;
|
|
383
|
-
private unsubscribe?;
|
|
384
|
-
private cachedSnapshot;
|
|
385
|
-
/**
|
|
386
|
-
* Attach to an AG-UI agent
|
|
387
|
-
*/
|
|
388
|
-
attach(agent: AbstractAgent): void;
|
|
389
|
-
/**
|
|
390
|
-
* Detach from the current agent
|
|
391
|
-
*/
|
|
392
|
-
detach(): void;
|
|
393
|
-
/**
|
|
394
|
-
* Get MCP app event for a specific tool
|
|
395
|
-
*/
|
|
396
|
-
getEvent(toolName: string): McpAppEvent | undefined;
|
|
397
|
-
/**
|
|
398
|
-
* Get all MCP app events (cached for useSyncExternalStore)
|
|
399
|
-
*/
|
|
400
|
-
getAllEvents(): Record<string, McpAppEvent>;
|
|
401
|
-
/**
|
|
402
|
-
* Get tool call event by ID
|
|
403
|
-
*/
|
|
404
|
-
getToolCall(toolCallId: string): ToolCallEventData | undefined;
|
|
405
|
-
/**
|
|
406
|
-
* Subscribe to event changes
|
|
407
|
-
*/
|
|
408
|
-
subscribe(listener: () => void): () => void;
|
|
409
|
-
/**
|
|
410
|
-
* Clear all events
|
|
411
|
-
*/
|
|
412
|
-
clear(): void;
|
|
413
|
-
private notify;
|
|
215
|
+
interface McpAppRendererProps {
|
|
216
|
+
metadata: McpAppMetadata;
|
|
217
|
+
input?: Record<string, unknown>;
|
|
218
|
+
result?: unknown;
|
|
219
|
+
status: 'executing' | 'inProgress' | 'complete' | 'idle';
|
|
220
|
+
sseClient?: SSEClient | null;
|
|
221
|
+
/** Custom CSS class for the container */
|
|
222
|
+
className?: string;
|
|
414
223
|
}
|
|
415
|
-
|
|
416
224
|
/**
|
|
417
|
-
*
|
|
225
|
+
* Simple hook to get MCP app metadata
|
|
418
226
|
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*/
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* React hook to subscribe to MCP app events from an AG-UI agent
|
|
425
|
-
*
|
|
426
|
-
* This hook manages the subscription lifecycle and provides event state.
|
|
427
|
-
* It's completely independent of CopilotKit and works with any AG-UI agent.
|
|
428
|
-
*
|
|
429
|
-
* @param agent - AG-UI agent instance (can be from HttpAgent, LangGraphAgent, etc.)
|
|
430
|
-
* @param config - Configuration with event handlers
|
|
431
|
-
*
|
|
432
|
-
* @example
|
|
433
|
-
* ```typescript
|
|
434
|
-
* import { useAguiSubscriber } from '@mcp-ts/sdk/client/react';
|
|
435
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
436
|
-
*
|
|
437
|
-
* function MyComponent() {
|
|
438
|
-
* const [agent] = useState(() => new HttpAgent({ url: '/api/agent' }));
|
|
439
|
-
*
|
|
440
|
-
* useAguiSubscriber(agent, {
|
|
441
|
-
* onMcpApp: (event) => {
|
|
442
|
-
* console.log('MCP App:', event.resourceUri);
|
|
443
|
-
* },
|
|
444
|
-
* onToolCall: (event) => {
|
|
445
|
-
* console.log('Tool:', event.toolName, event.status);
|
|
446
|
-
* }
|
|
447
|
-
* });
|
|
448
|
-
*
|
|
449
|
-
* // Your UI code...
|
|
450
|
-
* }
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
declare function useAguiSubscriber(agent: AbstractAgent | null | undefined, config: McpAppSubscriberConfig): void;
|
|
454
|
-
/**
|
|
455
|
-
* React hook for managing MCP apps
|
|
456
|
-
*
|
|
457
|
-
* Returns MCP apps that need to be rendered. Automatically combines:
|
|
458
|
-
* 1. Tool metadata (instant, synchronous)
|
|
459
|
-
* 2. AG-UI agent events (async, after tool execution)
|
|
460
|
-
*
|
|
461
|
-
* Prioritizes tool metadata for instant loading.
|
|
462
|
-
*
|
|
463
|
-
* @param agent - AG-UI agent instance (optional)
|
|
464
|
-
* @param mcpClient - MCP client with tool metadata (optional)
|
|
465
|
-
* @returns Object with MCP apps and helper functions
|
|
227
|
+
* @param mcpClient - The MCP client from useMcp() or context
|
|
228
|
+
* @returns Object with getAppMetadata function and McpAppRenderer component
|
|
466
229
|
*
|
|
467
230
|
* @example
|
|
468
|
-
* ```
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
* const { agent } = useAgent({ agentId: "myAgent" });
|
|
473
|
-
* const { mcpClient } = useMcpContext();
|
|
474
|
-
* const { apps } = useMcpApps(agent, mcpClient);
|
|
475
|
-
*
|
|
476
|
-
* return (
|
|
477
|
-
* <>
|
|
478
|
-
* {Object.entries(apps).map(([toolName, app]) => (
|
|
479
|
-
* <McpAppUI key={toolName} {...app} />
|
|
480
|
-
* ))}
|
|
481
|
-
* </>
|
|
482
|
-
* );
|
|
483
|
-
* }
|
|
484
|
-
* ```
|
|
485
|
-
*/
|
|
486
|
-
declare function useMcpApps(agent?: AbstractAgent | null, mcpClient?: {
|
|
487
|
-
connections: Array<{
|
|
488
|
-
tools: any[];
|
|
489
|
-
sessionId: string;
|
|
490
|
-
}>;
|
|
491
|
-
} | null): {
|
|
492
|
-
/** All MCP apps indexed by tool name */
|
|
493
|
-
apps: Record<string, McpAppEvent>;
|
|
494
|
-
/** Get app for a specific tool */
|
|
495
|
-
getApp: (toolName: string) => McpAppEvent | undefined;
|
|
496
|
-
/** Clear all apps */
|
|
497
|
-
clear: () => void;
|
|
498
|
-
};
|
|
499
|
-
/**
|
|
500
|
-
* React hook for tracking tool call lifecycle events
|
|
501
|
-
*
|
|
502
|
-
* Provides access to detailed tool call events (start, args, end, result)
|
|
503
|
-
* for debugging or custom UI rendering.
|
|
504
|
-
*
|
|
505
|
-
* @param agent - AG-UI agent instance
|
|
506
|
-
* @returns Object with tool call events and helper functions
|
|
507
|
-
*
|
|
508
|
-
* @example
|
|
509
|
-
* ```typescript
|
|
510
|
-
* import { useToolCallEvents } from '@mcp-ts/sdk/client/react';
|
|
511
|
-
*
|
|
512
|
-
* function ToolCallDebugger() {
|
|
513
|
-
* const [agent] = useState(() => new HttpAgent({ url: '/api/agent' }));
|
|
514
|
-
* const { toolCalls } = useToolCallEvents(agent);
|
|
231
|
+
* ```tsx
|
|
232
|
+
* function ToolRenderer(props) {
|
|
233
|
+
* const { getAppMetadata, McpAppRenderer } = useMcpApps(mcpClient);
|
|
234
|
+
* const metadata = getAppMetadata(props.name);
|
|
515
235
|
*
|
|
236
|
+
* if (!metadata) return null;
|
|
516
237
|
* return (
|
|
517
|
-
* <
|
|
518
|
-
* {
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
* </div>
|
|
238
|
+
* <McpAppRenderer
|
|
239
|
+
* metadata={metadata}
|
|
240
|
+
* input={props.args}
|
|
241
|
+
* result={props.result}
|
|
242
|
+
* status={props.status}
|
|
243
|
+
* />
|
|
524
244
|
* );
|
|
525
245
|
* }
|
|
526
246
|
* ```
|
|
527
247
|
*/
|
|
528
|
-
declare function
|
|
529
|
-
|
|
530
|
-
|
|
248
|
+
declare function useMcpApps(mcpClient: McpClient | null): {
|
|
249
|
+
getAppMetadata: (toolName: string) => McpAppMetadata | undefined;
|
|
250
|
+
McpAppRenderer: React$1.NamedExoticComponent<McpAppRendererProps>;
|
|
531
251
|
};
|
|
532
252
|
|
|
533
|
-
export { AppHost, type
|
|
253
|
+
export { AppHost, type McpClient$1 as McpClient, type McpConnection, McpConnectionEvent, McpConnectionState, SSEClient, ToolInfo, type UseMcpOptions, useAppHost, useMcp, useMcpApps };
|