@mcp-ts/sdk 1.1.0 → 1.2.0
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 +21 -10
- package/dist/adapters/agui-adapter.js +0 -1
- package/dist/adapters/agui-adapter.js.map +1 -1
- package/dist/adapters/agui-adapter.mjs +0 -1
- package/dist/adapters/agui-adapter.mjs.map +1 -1
- package/dist/adapters/agui-middleware.d.mts +5 -0
- package/dist/adapters/agui-middleware.d.ts +5 -0
- package/dist/adapters/agui-middleware.js +12 -23
- package/dist/adapters/agui-middleware.js.map +1 -1
- package/dist/adapters/agui-middleware.mjs +12 -23
- package/dist/adapters/agui-middleware.mjs.map +1 -1
- package/dist/client/react.d.mts +351 -3
- package/dist/client/react.d.ts +351 -3
- package/dist/client/react.js +308 -6
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs +302 -7
- package/dist/client/react.mjs.map +1 -1
- package/dist/client/vue.js +1 -1
- package/dist/client/vue.js.map +1 -1
- package/dist/client/vue.mjs +1 -1
- package/dist/client/vue.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +18 -1
- package/dist/index.mjs.map +1 -1
- package/dist/shared/index.d.mts +38 -2
- package/dist/shared/index.d.ts +38 -2
- package/dist/shared/index.js +19 -0
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs +18 -1
- package/dist/shared/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/adapters/agui-adapter.ts +2 -4
- package/src/adapters/agui-middleware.ts +15 -27
- package/src/client/react/agui-subscriber.ts +275 -0
- package/src/client/react/index.ts +23 -4
- package/src/client/react/use-agui-subscriber.ts +270 -0
- package/src/client/react/{use-mcp-app.ts → use-app-host.ts} +2 -2
- package/src/client/react/use-mcp-app-iframe.ts +164 -0
- package/src/client/react/{useMcp.ts → use-mcp.ts} +2 -2
- package/src/client/vue/index.ts +1 -1
- package/src/shared/index.ts +6 -1
- package/src/shared/tool-utils.ts +61 -0
- /package/src/client/vue/{useMcp.ts → use-mcp.ts} +0 -0
package/dist/client/react.d.mts
CHANGED
|
@@ -4,6 +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.mjs';
|
|
5
5
|
import { T as ToolInfo, k as FinishAuthResult, n as ListToolsRpcResult, L as ListPromptsResult, l as ListResourcesResult } from '../types-CLccx9wW.mjs';
|
|
6
6
|
export { p as McpRpcRequest, q as McpRpcResponse } from '../types-CLccx9wW.mjs';
|
|
7
|
+
import { AbstractAgent, AgentSubscriber } from '@ag-ui/client';
|
|
7
8
|
import '@modelcontextprotocol/sdk/types.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -151,7 +152,7 @@ interface McpClient {
|
|
|
151
152
|
/**
|
|
152
153
|
* Access the underlying SSEClient instance (for advanced usage like AppHost)
|
|
153
154
|
*/
|
|
154
|
-
|
|
155
|
+
sseClient: SSEClient | null;
|
|
155
156
|
}
|
|
156
157
|
/**
|
|
157
158
|
* React hook for MCP connection management with SSE
|
|
@@ -171,7 +172,7 @@ declare function useMcp(options: UseMcpOptions): McpClient;
|
|
|
171
172
|
* @param options - Optional configuration
|
|
172
173
|
* @returns Object containing the AppHost instance (or null) and error state
|
|
173
174
|
*/
|
|
174
|
-
declare function
|
|
175
|
+
declare function useAppHost(client: SSEClient, iframeRef: React.RefObject<HTMLIFrameElement>, options?: {
|
|
175
176
|
/** Callback when the App sends a message (e.g. to chat) */
|
|
176
177
|
onMessage?: (params: {
|
|
177
178
|
role: string;
|
|
@@ -182,4 +183,351 @@ declare function useMcpApp(client: SSEClient, iframeRef: React.RefObject<HTMLIFr
|
|
|
182
183
|
error: Error | null;
|
|
183
184
|
};
|
|
184
185
|
|
|
185
|
-
|
|
186
|
+
/**
|
|
187
|
+
* useMcpAppIframe Hook
|
|
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
|
+
* });
|
|
254
|
+
*
|
|
255
|
+
* return (
|
|
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
|
+
* );
|
|
262
|
+
*/
|
|
263
|
+
declare function useMcpAppIframe({ resourceUri, sessionId, toolInput, toolResult, toolStatus, sseClient, }: McpAppIframeProps): McpAppIframeResult;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* AG-UI Subscriber for MCP Apps
|
|
267
|
+
*
|
|
268
|
+
* Provides a standalone subscriber pattern for listening to AG-UI agent events
|
|
269
|
+
* without depending on CopilotKit components. This allows MCP apps to be rendered
|
|
270
|
+
* based on tool call events from any AG-UI agent.
|
|
271
|
+
*
|
|
272
|
+
* @requires @ag-ui/client - Peer dependency for AG-UI types
|
|
273
|
+
*/
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* MCP App UI event payload emitted from middleware
|
|
277
|
+
*/
|
|
278
|
+
interface McpAppEvent {
|
|
279
|
+
/** Tool call ID that triggered this UI */
|
|
280
|
+
toolCallId: string;
|
|
281
|
+
/** Resource URI for the MCP app */
|
|
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';
|
|
293
|
+
}
|
|
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;
|
|
303
|
+
toolName: string;
|
|
304
|
+
args?: Record<string, unknown>;
|
|
305
|
+
result?: any;
|
|
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;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Creates an AG-UI AgentSubscriber that listens for MCP app events
|
|
322
|
+
*
|
|
323
|
+
* This subscriber can be attached to any AG-UI agent instance using agent.subscribe()
|
|
324
|
+
* and will call the provided handlers when MCP app events are detected.
|
|
325
|
+
*
|
|
326
|
+
* @param config - Configuration with event handlers
|
|
327
|
+
* @returns AgentSubscriber that can be passed to agent.subscribe()
|
|
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;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* React Hook for AG-UI Subscriber Pattern
|
|
418
|
+
*
|
|
419
|
+
* Provides React hooks for subscribing to AG-UI agent events without
|
|
420
|
+
* depending on CopilotKit components. Works with any AG-UI agent instance.
|
|
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
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* ```typescript
|
|
469
|
+
* import { useMcpApps } from '@mcp-ts/sdk/client/react';
|
|
470
|
+
*
|
|
471
|
+
* function ToolRenderer() {
|
|
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);
|
|
515
|
+
*
|
|
516
|
+
* return (
|
|
517
|
+
* <div>
|
|
518
|
+
* {Object.entries(toolCalls).map(([id, event]) => (
|
|
519
|
+
* <div key={id}>
|
|
520
|
+
* {event.toolName} - {event.status}
|
|
521
|
+
* </div>
|
|
522
|
+
* ))}
|
|
523
|
+
* </div>
|
|
524
|
+
* );
|
|
525
|
+
* }
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
528
|
+
declare function useToolCallEvents(agent: AbstractAgent | null | undefined): {
|
|
529
|
+
/** All tool call events indexed by tool call ID */
|
|
530
|
+
toolCalls: Record<string, ToolCallEventData>;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
export { AppHost, type McpAppEvent, type McpAppEventHandler, McpAppEventManager, type McpAppIframeProps, type McpAppSubscriberConfig, type McpClient, type McpConnection, McpConnectionEvent, McpConnectionState, SSEClient, type ToolCallEventData, type ToolCallEventHandler, ToolInfo, type UseMcpOptions, createMcpAppSubscriber, subscribeMcpAppEvents, useAguiSubscriber, useAppHost, useMcp, useMcpAppIframe, useMcpApps, useToolCallEvents };
|