@mcp-ts/sdk 1.3.7 → 1.3.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.
Files changed (66) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +397 -404
  3. package/dist/adapters/agui-middleware.js.map +1 -1
  4. package/dist/adapters/agui-middleware.mjs.map +1 -1
  5. package/dist/bin/mcp-ts.js +0 -0
  6. package/dist/bin/mcp-ts.js.map +1 -1
  7. package/dist/bin/mcp-ts.mjs +0 -0
  8. package/dist/bin/mcp-ts.mjs.map +1 -1
  9. package/dist/client/index.js.map +1 -1
  10. package/dist/client/index.mjs.map +1 -1
  11. package/dist/client/react.d.mts +10 -28
  12. package/dist/client/react.d.ts +10 -28
  13. package/dist/client/react.js +101 -52
  14. package/dist/client/react.js.map +1 -1
  15. package/dist/client/react.mjs +102 -53
  16. package/dist/client/react.mjs.map +1 -1
  17. package/dist/client/vue.js.map +1 -1
  18. package/dist/client/vue.mjs.map +1 -1
  19. package/dist/index.js +78 -6
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +73 -6
  22. package/dist/index.mjs.map +1 -1
  23. package/dist/server/index.js +78 -6
  24. package/dist/server/index.js.map +1 -1
  25. package/dist/server/index.mjs +73 -6
  26. package/dist/server/index.mjs.map +1 -1
  27. package/dist/shared/index.js.map +1 -1
  28. package/dist/shared/index.mjs.map +1 -1
  29. package/package.json +185 -185
  30. package/src/adapters/agui-middleware.ts +382 -382
  31. package/src/bin/mcp-ts.ts +102 -102
  32. package/src/client/core/app-host.ts +417 -417
  33. package/src/client/core/sse-client.ts +371 -371
  34. package/src/client/core/types.ts +31 -31
  35. package/src/client/index.ts +27 -27
  36. package/src/client/react/index.ts +20 -16
  37. package/src/client/react/use-app-host.ts +74 -73
  38. package/src/client/react/use-mcp-apps.tsx +224 -214
  39. package/src/client/react/use-mcp.ts +669 -641
  40. package/src/client/vue/index.ts +10 -10
  41. package/src/client/vue/use-mcp.ts +617 -617
  42. package/src/index.ts +11 -11
  43. package/src/server/handlers/nextjs-handler.ts +204 -204
  44. package/src/server/handlers/sse-handler.ts +631 -631
  45. package/src/server/index.ts +57 -57
  46. package/src/server/mcp/multi-session-client.ts +228 -228
  47. package/src/server/mcp/oauth-client.ts +1188 -1188
  48. package/src/server/mcp/storage-oauth-provider.ts +272 -272
  49. package/src/server/storage/crypto.ts +92 -0
  50. package/src/server/storage/file-backend.ts +157 -157
  51. package/src/server/storage/index.ts +176 -176
  52. package/src/server/storage/memory-backend.ts +123 -123
  53. package/src/server/storage/redis-backend.ts +276 -276
  54. package/src/server/storage/redis.ts +160 -160
  55. package/src/server/storage/sqlite-backend.ts +182 -182
  56. package/src/server/storage/supabase-backend.ts +229 -228
  57. package/src/server/storage/types.ts +116 -116
  58. package/src/shared/constants.ts +29 -29
  59. package/src/shared/errors.ts +133 -133
  60. package/src/shared/event-routing.ts +28 -28
  61. package/src/shared/events.ts +180 -180
  62. package/src/shared/index.ts +75 -75
  63. package/src/shared/tool-utils.ts +61 -61
  64. package/src/shared/types.ts +282 -282
  65. package/src/shared/utils.ts +38 -38
  66. package/supabase/migrations/20260330195700_install_mcp_sessions.sql +84 -84
@@ -1,180 +1,180 @@
1
- /**
2
- * Simple event emitter pattern for MCP connection events
3
- * Inspired by Cloudflare's agents pattern but adapted for serverless
4
- */
5
-
6
- export type Disposable = {
7
- dispose(): void;
8
- };
9
-
10
- export type Event<T> = (listener: (event: T) => void) => Disposable;
11
-
12
- /**
13
- * Event emitter class for type-safe event handling
14
- * Similar to Cloudflare's Emitter but simplified for our use case
15
- */
16
- export class Emitter<T> {
17
- private listeners: Set<(event: T) => void> = new Set();
18
-
19
- /**
20
- * Subscribe to events
21
- * @param listener - Callback function to handle events
22
- * @returns Disposable to unsubscribe
23
- */
24
- get event(): Event<T> {
25
- return (listener: (event: T) => void) => {
26
- this.listeners.add(listener);
27
- return {
28
- dispose: () => {
29
- this.listeners.delete(listener);
30
- },
31
- };
32
- };
33
- }
34
-
35
- /**
36
- * Fire an event to all listeners
37
- * @param event - Event data to emit
38
- */
39
- fire(event: T): void {
40
- for (const listener of this.listeners) {
41
- try {
42
- listener(event);
43
- } catch (error) {
44
- console.error('[Emitter] Error in event listener:', error);
45
- }
46
- }
47
- }
48
-
49
- /**
50
- * Clear all listeners
51
- */
52
- dispose(): void {
53
- this.listeners.clear();
54
- }
55
-
56
- /**
57
- * Get number of active listeners
58
- */
59
- get listenerCount(): number {
60
- return this.listeners.size;
61
- }
62
- }
63
-
64
- /**
65
- * Connection state types matching your existing ConnectionStatus
66
- * Extended with more granular states for better observability
67
- */
68
- export type McpConnectionState =
69
- | 'DISCONNECTED' // Not connected
70
- | 'CONNECTING' // Establishing transport connection to MCP server
71
- | 'AUTHENTICATING' // OAuth flow in progress
72
- | 'AUTHENTICATED' // OAuth complete, pre-connect
73
- | 'DISCOVERING' // Discovering server capabilities (tools, resources, prompts)
74
- | 'CONNECTED' // Transport connection established
75
- | 'READY' // Fully connected and ready to use
76
- | 'VALIDATING' // Validating existing session
77
- | 'RECONNECTING' // Attempting to reconnect
78
- | 'INITIALIZING' // Initializing session or connection
79
- | 'FAILED'; // Connection error at some point
80
-
81
- /**
82
- * MCP Connection Event Types
83
- * Discriminated union for type-safe event handling
84
- */
85
- export type McpConnectionEvent =
86
- | {
87
- type: 'state_changed';
88
- sessionId: string;
89
- serverId: string;
90
- serverName: string;
91
- serverUrl: string;
92
- createdAt?: number;
93
- state: McpConnectionState;
94
- previousState: McpConnectionState;
95
- timestamp: number;
96
- }
97
- | {
98
- type: 'tools_discovered';
99
- sessionId: string;
100
- serverId: string;
101
- toolCount: number;
102
- tools: any[];
103
- timestamp: number;
104
- }
105
- | {
106
- type: 'auth_required';
107
- sessionId: string;
108
- serverId: string;
109
- authUrl: string;
110
- timestamp: number;
111
- }
112
- | {
113
- type: 'error';
114
- sessionId: string;
115
- serverId: string;
116
- error: string;
117
- errorType: 'connection' | 'auth' | 'validation' | 'unknown';
118
- timestamp: number;
119
- }
120
- | {
121
- type: 'disconnected';
122
- sessionId: string;
123
- serverId: string;
124
- reason?: string;
125
- timestamp: number;
126
- }
127
- | {
128
- type: 'progress';
129
- sessionId: string;
130
- serverId: string;
131
- message: string;
132
- timestamp: number;
133
- };
134
-
135
- /**
136
- * Event fired when a tool execution returns a UI resource URI
137
- */
138
- export interface McpAppsUIEvent {
139
- type: 'mcp-apps-ui';
140
- sessionId: string;
141
- resourceUri: string;
142
- toolName: string;
143
- result: unknown;
144
- timestamp: number;
145
- }
146
-
147
- /**
148
- * Observability event for debugging and monitoring
149
- */
150
- export interface McpObservabilityEvent {
151
- type?: string;
152
- level?: 'debug' | 'info' | 'warn' | 'error';
153
- message?: string;
154
- displayMessage?: string;
155
- sessionId?: string;
156
- serverId?: string;
157
- payload?: Record<string, any>;
158
- metadata?: Record<string, any>; // Kept for backward compatibility
159
- timestamp: number;
160
- id?: string;
161
- }
162
-
163
- /**
164
- * DisposableStore for managing multiple disposables
165
- * Useful for cleanup in React hooks
166
- */
167
- export class DisposableStore {
168
- private disposables: Set<Disposable> = new Set();
169
-
170
- add(disposable: Disposable): void {
171
- this.disposables.add(disposable);
172
- }
173
-
174
- dispose(): void {
175
- for (const disposable of this.disposables) {
176
- disposable.dispose();
177
- }
178
- this.disposables.clear();
179
- }
180
- }
1
+ /**
2
+ * Simple event emitter pattern for MCP connection events
3
+ * Inspired by Cloudflare's agents pattern but adapted for serverless
4
+ */
5
+
6
+ export type Disposable = {
7
+ dispose(): void;
8
+ };
9
+
10
+ export type Event<T> = (listener: (event: T) => void) => Disposable;
11
+
12
+ /**
13
+ * Event emitter class for type-safe event handling
14
+ * Similar to Cloudflare's Emitter but simplified for our use case
15
+ */
16
+ export class Emitter<T> {
17
+ private listeners: Set<(event: T) => void> = new Set();
18
+
19
+ /**
20
+ * Subscribe to events
21
+ * @param listener - Callback function to handle events
22
+ * @returns Disposable to unsubscribe
23
+ */
24
+ get event(): Event<T> {
25
+ return (listener: (event: T) => void) => {
26
+ this.listeners.add(listener);
27
+ return {
28
+ dispose: () => {
29
+ this.listeners.delete(listener);
30
+ },
31
+ };
32
+ };
33
+ }
34
+
35
+ /**
36
+ * Fire an event to all listeners
37
+ * @param event - Event data to emit
38
+ */
39
+ fire(event: T): void {
40
+ for (const listener of this.listeners) {
41
+ try {
42
+ listener(event);
43
+ } catch (error) {
44
+ console.error('[Emitter] Error in event listener:', error);
45
+ }
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Clear all listeners
51
+ */
52
+ dispose(): void {
53
+ this.listeners.clear();
54
+ }
55
+
56
+ /**
57
+ * Get number of active listeners
58
+ */
59
+ get listenerCount(): number {
60
+ return this.listeners.size;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Connection state types matching your existing ConnectionStatus
66
+ * Extended with more granular states for better observability
67
+ */
68
+ export type McpConnectionState =
69
+ | 'DISCONNECTED' // Not connected
70
+ | 'CONNECTING' // Establishing transport connection to MCP server
71
+ | 'AUTHENTICATING' // OAuth flow in progress
72
+ | 'AUTHENTICATED' // OAuth complete, pre-connect
73
+ | 'DISCOVERING' // Discovering server capabilities (tools, resources, prompts)
74
+ | 'CONNECTED' // Transport connection established
75
+ | 'READY' // Fully connected and ready to use
76
+ | 'VALIDATING' // Validating existing session
77
+ | 'RECONNECTING' // Attempting to reconnect
78
+ | 'INITIALIZING' // Initializing session or connection
79
+ | 'FAILED'; // Connection error at some point
80
+
81
+ /**
82
+ * MCP Connection Event Types
83
+ * Discriminated union for type-safe event handling
84
+ */
85
+ export type McpConnectionEvent =
86
+ | {
87
+ type: 'state_changed';
88
+ sessionId: string;
89
+ serverId: string;
90
+ serverName: string;
91
+ serverUrl: string;
92
+ createdAt?: number;
93
+ state: McpConnectionState;
94
+ previousState: McpConnectionState;
95
+ timestamp: number;
96
+ }
97
+ | {
98
+ type: 'tools_discovered';
99
+ sessionId: string;
100
+ serverId: string;
101
+ toolCount: number;
102
+ tools: any[];
103
+ timestamp: number;
104
+ }
105
+ | {
106
+ type: 'auth_required';
107
+ sessionId: string;
108
+ serverId: string;
109
+ authUrl: string;
110
+ timestamp: number;
111
+ }
112
+ | {
113
+ type: 'error';
114
+ sessionId: string;
115
+ serverId: string;
116
+ error: string;
117
+ errorType: 'connection' | 'auth' | 'validation' | 'unknown';
118
+ timestamp: number;
119
+ }
120
+ | {
121
+ type: 'disconnected';
122
+ sessionId: string;
123
+ serverId: string;
124
+ reason?: string;
125
+ timestamp: number;
126
+ }
127
+ | {
128
+ type: 'progress';
129
+ sessionId: string;
130
+ serverId: string;
131
+ message: string;
132
+ timestamp: number;
133
+ };
134
+
135
+ /**
136
+ * Event fired when a tool execution returns a UI resource URI
137
+ */
138
+ export interface McpAppsUIEvent {
139
+ type: 'mcp-apps-ui';
140
+ sessionId: string;
141
+ resourceUri: string;
142
+ toolName: string;
143
+ result: unknown;
144
+ timestamp: number;
145
+ }
146
+
147
+ /**
148
+ * Observability event for debugging and monitoring
149
+ */
150
+ export interface McpObservabilityEvent {
151
+ type?: string;
152
+ level?: 'debug' | 'info' | 'warn' | 'error';
153
+ message?: string;
154
+ displayMessage?: string;
155
+ sessionId?: string;
156
+ serverId?: string;
157
+ payload?: Record<string, any>;
158
+ metadata?: Record<string, any>; // Kept for backward compatibility
159
+ timestamp: number;
160
+ id?: string;
161
+ }
162
+
163
+ /**
164
+ * DisposableStore for managing multiple disposables
165
+ * Useful for cleanup in React hooks
166
+ */
167
+ export class DisposableStore {
168
+ private disposables: Set<Disposable> = new Set();
169
+
170
+ add(disposable: Disposable): void {
171
+ this.disposables.add(disposable);
172
+ }
173
+
174
+ dispose(): void {
175
+ for (const disposable of this.disposables) {
176
+ disposable.dispose();
177
+ }
178
+ this.disposables.clear();
179
+ }
180
+ }
@@ -1,75 +1,75 @@
1
- /**
2
- * MCP Redis Shared Package
3
- * Shared types and utilities for both server and client
4
- */
5
-
6
- // Events
7
- export {
8
- Emitter,
9
- DisposableStore,
10
- type Disposable,
11
- type Event,
12
- type McpConnectionState,
13
- type McpConnectionEvent,
14
- type McpObservabilityEvent,
15
- } from './events';
16
-
17
- // Constants
18
- export * from './constants';
19
-
20
- // Errors
21
- export * from './errors';
22
-
23
- // Types
24
- export type {
25
- ToolInfo,
26
- McpRpcRequest,
27
- McpRpcResponse,
28
- McpRpcMethod,
29
- McpRpcParams,
30
- TransportType,
31
- // API types
32
- ConnectRequest,
33
- ConnectResponse,
34
- ConnectSuccessResponse,
35
- ConnectAuthRequiredResponse,
36
- ConnectErrorResponse,
37
- ListToolsResponse,
38
- CallToolRequest,
39
- CallToolResponse,
40
- // RPC param types
41
- ConnectParams,
42
- DisconnectParams,
43
- SessionParams,
44
- CallToolParams,
45
- GetPromptParams,
46
- ReadResourceParams,
47
- FinishAuthParams,
48
- // RPC result types
49
- SessionInfo,
50
- SessionListResult,
51
- ConnectResult,
52
- DisconnectResult,
53
- RestoreSessionResult,
54
- FinishAuthResult,
55
- ListToolsRpcResult,
56
- ListPromptsResult,
57
- ListResourcesResult,
58
- } from './types';
59
-
60
- export {
61
- isConnectSuccess,
62
- isConnectAuthRequired,
63
- isConnectError,
64
- isListToolsSuccess,
65
- isCallToolSuccess,
66
- } from './types';
67
-
68
- // Utilities
69
- export { sanitizeServerLabel } from './utils.js';
70
- export {
71
- getToolUiResourceUri,
72
- findToolByName,
73
- type ToolUiConfig,
74
- } from './tool-utils.js';
75
-
1
+ /**
2
+ * MCP Redis Shared Package
3
+ * Shared types and utilities for both server and client
4
+ */
5
+
6
+ // Events
7
+ export {
8
+ Emitter,
9
+ DisposableStore,
10
+ type Disposable,
11
+ type Event,
12
+ type McpConnectionState,
13
+ type McpConnectionEvent,
14
+ type McpObservabilityEvent,
15
+ } from './events';
16
+
17
+ // Constants
18
+ export * from './constants';
19
+
20
+ // Errors
21
+ export * from './errors';
22
+
23
+ // Types
24
+ export type {
25
+ ToolInfo,
26
+ McpRpcRequest,
27
+ McpRpcResponse,
28
+ McpRpcMethod,
29
+ McpRpcParams,
30
+ TransportType,
31
+ // API types
32
+ ConnectRequest,
33
+ ConnectResponse,
34
+ ConnectSuccessResponse,
35
+ ConnectAuthRequiredResponse,
36
+ ConnectErrorResponse,
37
+ ListToolsResponse,
38
+ CallToolRequest,
39
+ CallToolResponse,
40
+ // RPC param types
41
+ ConnectParams,
42
+ DisconnectParams,
43
+ SessionParams,
44
+ CallToolParams,
45
+ GetPromptParams,
46
+ ReadResourceParams,
47
+ FinishAuthParams,
48
+ // RPC result types
49
+ SessionInfo,
50
+ SessionListResult,
51
+ ConnectResult,
52
+ DisconnectResult,
53
+ RestoreSessionResult,
54
+ FinishAuthResult,
55
+ ListToolsRpcResult,
56
+ ListPromptsResult,
57
+ ListResourcesResult,
58
+ } from './types';
59
+
60
+ export {
61
+ isConnectSuccess,
62
+ isConnectAuthRequired,
63
+ isConnectError,
64
+ isListToolsSuccess,
65
+ isCallToolSuccess,
66
+ } from './types';
67
+
68
+ // Utilities
69
+ export { sanitizeServerLabel } from './utils.js';
70
+ export {
71
+ getToolUiResourceUri,
72
+ findToolByName,
73
+ type ToolUiConfig,
74
+ } from './tool-utils.js';
75
+
@@ -1,61 +1,61 @@
1
- /**
2
- * Utility functions for working with MCP tool metadata
3
- */
4
-
5
- import type { ToolInfo } from './types.js';
6
-
7
- export interface ToolUiConfig {
8
- resourceUri: string;
9
- sessionId: string;
10
- }
11
-
12
- /**
13
- * Extract UI resource URI from tool metadata
14
- *
15
- * @param tool - The tool to extract UI config from
16
- * @returns The resource URI if available, undefined otherwise
17
- *
18
- * @example
19
- * const uri = getToolUiResourceUri(tool);
20
- * if (uri) {
21
- * // Tool has UI configuration
22
- * }
23
- */
24
- export function getToolUiResourceUri(tool: ToolInfo): string | undefined {
25
- const meta = (tool as any)._meta;
26
- if (!meta?.ui) return undefined;
27
-
28
- const ui = meta.ui;
29
- if (typeof ui !== "object" || !ui) return undefined;
30
-
31
- // Check visibility filter - skip if explicitly hidden from app
32
- if (ui.visibility && !ui.visibility.includes("app")) return undefined;
33
-
34
- // Support both 'uri' and 'resourceUri' field names for flexibility
35
- return typeof ui.resourceUri === "string"
36
- ? ui.resourceUri
37
- : typeof ui.uri === "string"
38
- ? ui.uri
39
- : undefined;
40
- }
41
-
42
- /**
43
- * Find a tool by name within connections
44
- *
45
- * @param connections - Array of MCP connections
46
- * @param toolName - Name of the tool to find
47
- * @returns The tool if found, undefined otherwise
48
- *
49
- * @example
50
- * const tool = findToolByName(connections, "get_weather");
51
- */
52
- export function findToolByName(
53
- connections: Array<{ tools: ToolInfo[] }>,
54
- toolName: string
55
- ): ToolInfo | undefined {
56
- for (const conn of connections) {
57
- const tool = conn.tools.find((t) => t.name === toolName);
58
- if (tool) return tool;
59
- }
60
- return undefined;
61
- }
1
+ /**
2
+ * Utility functions for working with MCP tool metadata
3
+ */
4
+
5
+ import type { ToolInfo } from './types.js';
6
+
7
+ export interface ToolUiConfig {
8
+ resourceUri: string;
9
+ sessionId: string;
10
+ }
11
+
12
+ /**
13
+ * Extract UI resource URI from tool metadata
14
+ *
15
+ * @param tool - The tool to extract UI config from
16
+ * @returns The resource URI if available, undefined otherwise
17
+ *
18
+ * @example
19
+ * const uri = getToolUiResourceUri(tool);
20
+ * if (uri) {
21
+ * // Tool has UI configuration
22
+ * }
23
+ */
24
+ export function getToolUiResourceUri(tool: ToolInfo): string | undefined {
25
+ const meta = (tool as any)._meta;
26
+ if (!meta?.ui) return undefined;
27
+
28
+ const ui = meta.ui;
29
+ if (typeof ui !== "object" || !ui) return undefined;
30
+
31
+ // Check visibility filter - skip if explicitly hidden from app
32
+ if (ui.visibility && !ui.visibility.includes("app")) return undefined;
33
+
34
+ // Support both 'uri' and 'resourceUri' field names for flexibility
35
+ return typeof ui.resourceUri === "string"
36
+ ? ui.resourceUri
37
+ : typeof ui.uri === "string"
38
+ ? ui.uri
39
+ : undefined;
40
+ }
41
+
42
+ /**
43
+ * Find a tool by name within connections
44
+ *
45
+ * @param connections - Array of MCP connections
46
+ * @param toolName - Name of the tool to find
47
+ * @returns The tool if found, undefined otherwise
48
+ *
49
+ * @example
50
+ * const tool = findToolByName(connections, "get_weather");
51
+ */
52
+ export function findToolByName(
53
+ connections: Array<{ tools: ToolInfo[] }>,
54
+ toolName: string
55
+ ): ToolInfo | undefined {
56
+ for (const conn of connections) {
57
+ const tool = conn.tools.find((t) => t.name === toolName);
58
+ if (tool) return tool;
59
+ }
60
+ return undefined;
61
+ }