@mcp-ts/sdk 1.5.2 → 1.6.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 +89 -27
- package/dist/adapters/agui-adapter.d.mts +1 -1
- package/dist/adapters/agui-adapter.d.ts +1 -1
- package/dist/adapters/agui-adapter.js +76 -19
- package/dist/adapters/agui-adapter.js.map +1 -1
- package/dist/adapters/agui-adapter.mjs +76 -19
- package/dist/adapters/agui-adapter.mjs.map +1 -1
- package/dist/adapters/agui-middleware.d.mts +5 -1
- package/dist/adapters/agui-middleware.d.ts +5 -1
- package/dist/adapters/agui-middleware.js +116 -49
- package/dist/adapters/agui-middleware.js.map +1 -1
- package/dist/adapters/agui-middleware.mjs +117 -50
- package/dist/adapters/agui-middleware.mjs.map +1 -1
- package/dist/adapters/ai-adapter.d.mts +1 -1
- package/dist/adapters/ai-adapter.d.ts +1 -1
- package/dist/adapters/ai-adapter.js +76 -19
- package/dist/adapters/ai-adapter.js.map +1 -1
- package/dist/adapters/ai-adapter.mjs +76 -19
- package/dist/adapters/ai-adapter.mjs.map +1 -1
- package/dist/adapters/langchain-adapter.d.mts +1 -1
- package/dist/adapters/langchain-adapter.d.ts +1 -1
- package/dist/adapters/langchain-adapter.js +76 -19
- package/dist/adapters/langchain-adapter.js.map +1 -1
- package/dist/adapters/langchain-adapter.mjs +76 -19
- package/dist/adapters/langchain-adapter.mjs.map +1 -1
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +207 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +207 -44
- package/dist/index.mjs.map +1 -1
- package/dist/server/index.js +1 -3
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +1 -3
- package/dist/server/index.mjs.map +1 -1
- package/dist/shared/index.d.mts +15 -8
- package/dist/shared/index.d.ts +15 -8
- package/dist/shared/index.js +206 -40
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs +206 -41
- package/dist/shared/index.mjs.map +1 -1
- package/dist/{tool-router-DsKhRmJm.d.ts → tool-router-Bn9R0KWr.d.ts} +56 -7
- package/dist/{tool-router-DK0RJblO.d.mts → tool-router-_O2tIwf7.d.mts} +56 -7
- package/package.json +5 -3
- package/src/adapters/agui-middleware.ts +163 -59
- package/src/server/mcp/oauth-client.ts +4 -4
- package/src/shared/index.ts +4 -0
- package/src/shared/meta-tools.ts +172 -37
- package/src/shared/tool-index.ts +123 -7
- package/src/shared/tool-router.ts +40 -7
|
@@ -80,6 +80,39 @@ interface ToolSummary {
|
|
|
80
80
|
/** Estimated token cost of the full inputSchema */
|
|
81
81
|
estimatedTokens: number;
|
|
82
82
|
}
|
|
83
|
+
/** Server-level summary derived from indexed tools. */
|
|
84
|
+
interface ToolServerSummary {
|
|
85
|
+
/** Human-readable server name */
|
|
86
|
+
serverName: string;
|
|
87
|
+
/** Stable server identifier */
|
|
88
|
+
serverId: string;
|
|
89
|
+
/** Session the server belongs to */
|
|
90
|
+
sessionId: string;
|
|
91
|
+
/** Number of indexed tools for this server */
|
|
92
|
+
toolCount: number;
|
|
93
|
+
}
|
|
94
|
+
/** Optional filters for search and listing. */
|
|
95
|
+
interface ToolSearchOptions {
|
|
96
|
+
/** Restrict results to this server ID. */
|
|
97
|
+
serverId?: string;
|
|
98
|
+
/** Restrict results to servers whose name or ID matches this value. */
|
|
99
|
+
serverName?: string;
|
|
100
|
+
}
|
|
101
|
+
/** Paginated tool listing result. */
|
|
102
|
+
interface ToolListResult {
|
|
103
|
+
tools: ToolSummary[];
|
|
104
|
+
totalCount: number;
|
|
105
|
+
returnedCount: number;
|
|
106
|
+
nextCursor?: string;
|
|
107
|
+
servers: ToolServerSummary[];
|
|
108
|
+
}
|
|
109
|
+
interface ToolLookupOptions {
|
|
110
|
+
/**
|
|
111
|
+
* Allow namespace to match a fragment of serverName after exact
|
|
112
|
+
* sessionId/serverId matching fails.
|
|
113
|
+
*/
|
|
114
|
+
allowServerNameFragment?: boolean;
|
|
115
|
+
}
|
|
83
116
|
/** A tool with routing metadata attached during indexing. */
|
|
84
117
|
interface IndexedTool extends Tool {
|
|
85
118
|
sessionId: string;
|
|
@@ -147,7 +180,7 @@ declare class ToolIndex {
|
|
|
147
180
|
*
|
|
148
181
|
* `score = keywordWeight × keyword_score + (1 - keywordWeight) × cosine_score`
|
|
149
182
|
*/
|
|
150
|
-
search(query: string, topK?: number): Promise<ToolSummary[]>;
|
|
183
|
+
search(query: string, topK?: number, options?: ToolSearchOptions): Promise<ToolSummary[]>;
|
|
151
184
|
/**
|
|
152
185
|
* Search tools using a regex pattern.
|
|
153
186
|
* Matches against name, description, and parameter metadata.
|
|
@@ -155,11 +188,19 @@ declare class ToolIndex {
|
|
|
155
188
|
searchRegex(pattern: string, topK?: number): ToolSummary[];
|
|
156
189
|
/**
|
|
157
190
|
* Get tool definition(s) by name.
|
|
158
|
-
* If namespace is provided,
|
|
191
|
+
* If namespace is provided, exact sessionId/serverId matches take precedence.
|
|
192
|
+
* Falls back to serverName fragment matching only when explicitly allowed.
|
|
159
193
|
*/
|
|
160
|
-
getTool(name: string, namespace?: string): IndexedTool[];
|
|
194
|
+
getTool(name: string, namespace?: string, options?: ToolLookupOptions): IndexedTool[];
|
|
161
195
|
/** All indexed tool names. */
|
|
162
196
|
getToolNames(): string[];
|
|
197
|
+
/** List indexed servers with tool counts. */
|
|
198
|
+
listServers(options?: ToolSearchOptions): ToolServerSummary[];
|
|
199
|
+
/** List tools deterministically, optionally scoped to a server. */
|
|
200
|
+
listTools(options?: ToolSearchOptions & {
|
|
201
|
+
limit?: number;
|
|
202
|
+
cursor?: string;
|
|
203
|
+
}): ToolListResult;
|
|
163
204
|
/** Number of indexed tools (including duplicates). */
|
|
164
205
|
get size(): number;
|
|
165
206
|
/** Total estimated token cost of all indexed tool schemas. */
|
|
@@ -174,6 +215,7 @@ declare class ToolIndex {
|
|
|
174
215
|
/** Build a single searchable string from tool metadata. */
|
|
175
216
|
private buildSearchableText;
|
|
176
217
|
private getDocumentKey;
|
|
218
|
+
private matchesServer;
|
|
177
219
|
/** Simple whitespace + camelCase + snake_case tokenizer. */
|
|
178
220
|
private tokenize;
|
|
179
221
|
/** Cosine similarity between two vectors. */
|
|
@@ -262,7 +304,7 @@ declare class ToolRouter {
|
|
|
262
304
|
* This is the main method adapters should call.
|
|
263
305
|
*
|
|
264
306
|
* - `all` → returns all tools (unchanged behavior)
|
|
265
|
-
* - `search` → returns only meta-tools (
|
|
307
|
+
* - `search` → returns only meta-tools (mcp_search_tools, mcp_get_tool_schema, mcp_execute_tool)
|
|
266
308
|
* - `groups` → returns tools from active groups only
|
|
267
309
|
*/
|
|
268
310
|
getFilteredTools(): Promise<Tool[]>;
|
|
@@ -270,17 +312,24 @@ declare class ToolRouter {
|
|
|
270
312
|
* Search tools by natural-language query.
|
|
271
313
|
* Works regardless of strategy.
|
|
272
314
|
*/
|
|
273
|
-
searchTools(query: string, topK?: number): Promise<ToolSummary[]>;
|
|
315
|
+
searchTools(query: string, topK?: number, options?: ToolSearchOptions): Promise<ToolSummary[]>;
|
|
274
316
|
/**
|
|
275
317
|
* Search tools by regex pattern.
|
|
276
318
|
* Matches against name, description, and parameter metadata.
|
|
277
319
|
*/
|
|
278
320
|
searchToolsRegex(pattern: string, topK?: number): Promise<ToolSummary[]>;
|
|
321
|
+
/** List connected MCP servers with indexed tool counts. */
|
|
322
|
+
listServers(options?: ToolSearchOptions): Promise<ToolServerSummary[]>;
|
|
323
|
+
/** List tools deterministically, optionally scoped to a server. */
|
|
324
|
+
listTools(options?: ToolSearchOptions & {
|
|
325
|
+
limit?: number;
|
|
326
|
+
cursor?: string;
|
|
327
|
+
}): Promise<ToolListResult>;
|
|
279
328
|
/**
|
|
280
329
|
* Get the full tool definition by name.
|
|
281
330
|
* If tool name is ambiguous, use namespace to specify the server.
|
|
282
331
|
*/
|
|
283
|
-
getToolSchema(toolName: string, namespace?: string): IndexedTool | undefined;
|
|
332
|
+
getToolSchema(toolName: string, namespace?: string, options?: ToolLookupOptions): IndexedTool | undefined;
|
|
284
333
|
/**
|
|
285
334
|
* Get compact (schema-less) summaries for all tools.
|
|
286
335
|
*/
|
|
@@ -325,4 +374,4 @@ declare class ToolRouter {
|
|
|
325
374
|
private getMetaToolDefinitions;
|
|
326
375
|
}
|
|
327
376
|
|
|
328
|
-
export { type CompactTool as C, type EmbedFn as E, type IndexedTool as I, SchemaCompressor as S, type ToolGroupInfo as T, type CompressionStats as a, ToolIndex as b, type ToolIndexOptions as c,
|
|
377
|
+
export { type CompactTool as C, type EmbedFn as E, type IndexedTool as I, SchemaCompressor as S, type ToolGroupInfo as T, type CompressionStats as a, ToolIndex as b, type ToolIndexOptions as c, type ToolListResult as d, ToolRouter as e, type ToolRouterClientInput as f, type ToolRouterOptions as g, type ToolRouterStrategy as h, type ToolSearchOptions as i, type ToolServerSummary as j, type ToolSummary as k };
|
|
@@ -80,6 +80,39 @@ interface ToolSummary {
|
|
|
80
80
|
/** Estimated token cost of the full inputSchema */
|
|
81
81
|
estimatedTokens: number;
|
|
82
82
|
}
|
|
83
|
+
/** Server-level summary derived from indexed tools. */
|
|
84
|
+
interface ToolServerSummary {
|
|
85
|
+
/** Human-readable server name */
|
|
86
|
+
serverName: string;
|
|
87
|
+
/** Stable server identifier */
|
|
88
|
+
serverId: string;
|
|
89
|
+
/** Session the server belongs to */
|
|
90
|
+
sessionId: string;
|
|
91
|
+
/** Number of indexed tools for this server */
|
|
92
|
+
toolCount: number;
|
|
93
|
+
}
|
|
94
|
+
/** Optional filters for search and listing. */
|
|
95
|
+
interface ToolSearchOptions {
|
|
96
|
+
/** Restrict results to this server ID. */
|
|
97
|
+
serverId?: string;
|
|
98
|
+
/** Restrict results to servers whose name or ID matches this value. */
|
|
99
|
+
serverName?: string;
|
|
100
|
+
}
|
|
101
|
+
/** Paginated tool listing result. */
|
|
102
|
+
interface ToolListResult {
|
|
103
|
+
tools: ToolSummary[];
|
|
104
|
+
totalCount: number;
|
|
105
|
+
returnedCount: number;
|
|
106
|
+
nextCursor?: string;
|
|
107
|
+
servers: ToolServerSummary[];
|
|
108
|
+
}
|
|
109
|
+
interface ToolLookupOptions {
|
|
110
|
+
/**
|
|
111
|
+
* Allow namespace to match a fragment of serverName after exact
|
|
112
|
+
* sessionId/serverId matching fails.
|
|
113
|
+
*/
|
|
114
|
+
allowServerNameFragment?: boolean;
|
|
115
|
+
}
|
|
83
116
|
/** A tool with routing metadata attached during indexing. */
|
|
84
117
|
interface IndexedTool extends Tool {
|
|
85
118
|
sessionId: string;
|
|
@@ -147,7 +180,7 @@ declare class ToolIndex {
|
|
|
147
180
|
*
|
|
148
181
|
* `score = keywordWeight × keyword_score + (1 - keywordWeight) × cosine_score`
|
|
149
182
|
*/
|
|
150
|
-
search(query: string, topK?: number): Promise<ToolSummary[]>;
|
|
183
|
+
search(query: string, topK?: number, options?: ToolSearchOptions): Promise<ToolSummary[]>;
|
|
151
184
|
/**
|
|
152
185
|
* Search tools using a regex pattern.
|
|
153
186
|
* Matches against name, description, and parameter metadata.
|
|
@@ -155,11 +188,19 @@ declare class ToolIndex {
|
|
|
155
188
|
searchRegex(pattern: string, topK?: number): ToolSummary[];
|
|
156
189
|
/**
|
|
157
190
|
* Get tool definition(s) by name.
|
|
158
|
-
* If namespace is provided,
|
|
191
|
+
* If namespace is provided, exact sessionId/serverId matches take precedence.
|
|
192
|
+
* Falls back to serverName fragment matching only when explicitly allowed.
|
|
159
193
|
*/
|
|
160
|
-
getTool(name: string, namespace?: string): IndexedTool[];
|
|
194
|
+
getTool(name: string, namespace?: string, options?: ToolLookupOptions): IndexedTool[];
|
|
161
195
|
/** All indexed tool names. */
|
|
162
196
|
getToolNames(): string[];
|
|
197
|
+
/** List indexed servers with tool counts. */
|
|
198
|
+
listServers(options?: ToolSearchOptions): ToolServerSummary[];
|
|
199
|
+
/** List tools deterministically, optionally scoped to a server. */
|
|
200
|
+
listTools(options?: ToolSearchOptions & {
|
|
201
|
+
limit?: number;
|
|
202
|
+
cursor?: string;
|
|
203
|
+
}): ToolListResult;
|
|
163
204
|
/** Number of indexed tools (including duplicates). */
|
|
164
205
|
get size(): number;
|
|
165
206
|
/** Total estimated token cost of all indexed tool schemas. */
|
|
@@ -174,6 +215,7 @@ declare class ToolIndex {
|
|
|
174
215
|
/** Build a single searchable string from tool metadata. */
|
|
175
216
|
private buildSearchableText;
|
|
176
217
|
private getDocumentKey;
|
|
218
|
+
private matchesServer;
|
|
177
219
|
/** Simple whitespace + camelCase + snake_case tokenizer. */
|
|
178
220
|
private tokenize;
|
|
179
221
|
/** Cosine similarity between two vectors. */
|
|
@@ -262,7 +304,7 @@ declare class ToolRouter {
|
|
|
262
304
|
* This is the main method adapters should call.
|
|
263
305
|
*
|
|
264
306
|
* - `all` → returns all tools (unchanged behavior)
|
|
265
|
-
* - `search` → returns only meta-tools (
|
|
307
|
+
* - `search` → returns only meta-tools (mcp_search_tools, mcp_get_tool_schema, mcp_execute_tool)
|
|
266
308
|
* - `groups` → returns tools from active groups only
|
|
267
309
|
*/
|
|
268
310
|
getFilteredTools(): Promise<Tool[]>;
|
|
@@ -270,17 +312,24 @@ declare class ToolRouter {
|
|
|
270
312
|
* Search tools by natural-language query.
|
|
271
313
|
* Works regardless of strategy.
|
|
272
314
|
*/
|
|
273
|
-
searchTools(query: string, topK?: number): Promise<ToolSummary[]>;
|
|
315
|
+
searchTools(query: string, topK?: number, options?: ToolSearchOptions): Promise<ToolSummary[]>;
|
|
274
316
|
/**
|
|
275
317
|
* Search tools by regex pattern.
|
|
276
318
|
* Matches against name, description, and parameter metadata.
|
|
277
319
|
*/
|
|
278
320
|
searchToolsRegex(pattern: string, topK?: number): Promise<ToolSummary[]>;
|
|
321
|
+
/** List connected MCP servers with indexed tool counts. */
|
|
322
|
+
listServers(options?: ToolSearchOptions): Promise<ToolServerSummary[]>;
|
|
323
|
+
/** List tools deterministically, optionally scoped to a server. */
|
|
324
|
+
listTools(options?: ToolSearchOptions & {
|
|
325
|
+
limit?: number;
|
|
326
|
+
cursor?: string;
|
|
327
|
+
}): Promise<ToolListResult>;
|
|
279
328
|
/**
|
|
280
329
|
* Get the full tool definition by name.
|
|
281
330
|
* If tool name is ambiguous, use namespace to specify the server.
|
|
282
331
|
*/
|
|
283
|
-
getToolSchema(toolName: string, namespace?: string): IndexedTool | undefined;
|
|
332
|
+
getToolSchema(toolName: string, namespace?: string, options?: ToolLookupOptions): IndexedTool | undefined;
|
|
284
333
|
/**
|
|
285
334
|
* Get compact (schema-less) summaries for all tools.
|
|
286
335
|
*/
|
|
@@ -325,4 +374,4 @@ declare class ToolRouter {
|
|
|
325
374
|
private getMetaToolDefinitions;
|
|
326
375
|
}
|
|
327
376
|
|
|
328
|
-
export { type CompactTool as C, type EmbedFn as E, type IndexedTool as I, SchemaCompressor as S, type ToolGroupInfo as T, type CompressionStats as a, ToolIndex as b, type ToolIndexOptions as c,
|
|
377
|
+
export { type CompactTool as C, type EmbedFn as E, type IndexedTool as I, SchemaCompressor as S, type ToolGroupInfo as T, type CompressionStats as a, ToolIndex as b, type ToolIndexOptions as c, type ToolListResult as d, ToolRouter as e, type ToolRouterClientInput as f, type ToolRouterOptions as g, type ToolRouterStrategy as h, type ToolSearchOptions as i, type ToolServerSummary as j, type ToolSummary as k };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-ts/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -84,6 +84,8 @@
|
|
|
84
84
|
"test": "npx playwright test",
|
|
85
85
|
"test:ui": "npx playwright test --ui",
|
|
86
86
|
"test:debug": "npx playwright test --debug",
|
|
87
|
+
"benchmark:toolrouter": "npm run build && node benchmarks/toolrouter-efficiency.mjs",
|
|
88
|
+
"benchmark:toolrouter:live": "npm run build && node benchmarks/toolrouter-live.mjs",
|
|
87
89
|
"supabase:push": "supabase db push",
|
|
88
90
|
"supabase:reset": "supabase db reset",
|
|
89
91
|
"supabase:status": "supabase status",
|
|
@@ -160,7 +162,7 @@
|
|
|
160
162
|
"nanoid": "^5.1.6"
|
|
161
163
|
},
|
|
162
164
|
"devDependencies": {
|
|
163
|
-
"@ag-ui/client": "^0.0.
|
|
165
|
+
"@ag-ui/client": "^0.0.52",
|
|
164
166
|
"@langchain/core": "^1.1.39",
|
|
165
167
|
"@playwright/test": "^1.58.0",
|
|
166
168
|
"@supabase/supabase-js": "^2.48.0",
|
|
@@ -185,4 +187,4 @@
|
|
|
185
187
|
"engines": {
|
|
186
188
|
"node": ">=18.0.0"
|
|
187
189
|
}
|
|
188
|
-
}
|
|
190
|
+
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @requires rxjs - Uses RxJS Observables for event streaming
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import { Observable, Subscriber } from 'rxjs';
|
|
11
|
+
import { Observable, Subscriber, Subscription } from 'rxjs';
|
|
12
12
|
import {
|
|
13
13
|
Middleware,
|
|
14
14
|
EventType,
|
|
@@ -28,11 +28,19 @@ interface ToolResult {
|
|
|
28
28
|
messageId: string;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
interface SnapshotToolCall {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
arguments: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
31
37
|
/** State for tracking tool calls during a run */
|
|
32
38
|
interface RunState {
|
|
33
39
|
toolCallArgsBuffer: Map<string, string>;
|
|
34
40
|
toolCallNames: Map<string, string>;
|
|
35
41
|
pendingMcpCalls: Set<string>;
|
|
42
|
+
completedMcpCalls: Set<string>;
|
|
43
|
+
assistantMessageId?: string;
|
|
36
44
|
textContent?: string;
|
|
37
45
|
error: boolean;
|
|
38
46
|
}
|
|
@@ -87,6 +95,10 @@ export class McpMiddleware extends Middleware {
|
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
|
|
98
|
+
private normalizeArgsString(argsString: string): string {
|
|
99
|
+
return JSON.stringify(this.parseArgs(argsString));
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
private async executeTool(toolName: string, args: Record<string, any>): Promise<string> {
|
|
91
103
|
const tool = this.tools.find(t => t.name === toolName);
|
|
92
104
|
if (!tool?.handler) {
|
|
@@ -116,13 +128,82 @@ export class McpMiddleware extends Middleware {
|
|
|
116
128
|
if (!anyInput.runId) anyInput.runId = this.generateId('mcp_run');
|
|
117
129
|
}
|
|
118
130
|
|
|
131
|
+
private getSnapshotToolCalls(message: any): SnapshotToolCall[] {
|
|
132
|
+
const rawToolCalls = Array.isArray(message?.toolCalls)
|
|
133
|
+
? message.toolCalls
|
|
134
|
+
: (Array.isArray(message?.tool_calls) ? message.tool_calls : []);
|
|
135
|
+
|
|
136
|
+
return rawToolCalls.flatMap((toolCall: any) => {
|
|
137
|
+
if (!toolCall || typeof toolCall !== 'object') return [];
|
|
138
|
+
|
|
139
|
+
const id = typeof toolCall.id === 'string' ? toolCall.id : undefined;
|
|
140
|
+
const fn = toolCall.function && typeof toolCall.function === 'object'
|
|
141
|
+
? toolCall.function
|
|
142
|
+
: undefined;
|
|
143
|
+
const name = typeof fn?.name === 'string'
|
|
144
|
+
? fn.name
|
|
145
|
+
: (typeof toolCall.name === 'string' ? toolCall.name : undefined);
|
|
146
|
+
|
|
147
|
+
if (!id || !name) return [];
|
|
148
|
+
|
|
149
|
+
const rawArgs = fn?.arguments ?? toolCall.arguments ?? toolCall.args;
|
|
150
|
+
const argsString = typeof rawArgs === 'string'
|
|
151
|
+
? rawArgs
|
|
152
|
+
: (rawArgs && typeof rawArgs === 'object' ? JSON.stringify(rawArgs) : '{}');
|
|
153
|
+
|
|
154
|
+
return [{ id, name, arguments: argsString }];
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private getResolvedToolCallIds(messages: any[]): Set<string> {
|
|
159
|
+
const resolved = new Set<string>();
|
|
160
|
+
for (const message of messages) {
|
|
161
|
+
if (message?.role !== 'tool') continue;
|
|
162
|
+
const toolCallId = message.toolCallId ?? message.tool_call_id;
|
|
163
|
+
if (typeof toolCallId === 'string') {
|
|
164
|
+
resolved.add(toolCallId);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return resolved;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private shouldFilterMessagesSnapshot(event: BaseEvent, state: RunState): boolean {
|
|
171
|
+
if (state.completedMcpCalls.size === 0 || state.pendingMcpCalls.size > 0) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Only suppress snapshots that replay an assistant answer we already streamed.
|
|
176
|
+
// Snapshot-only final answers still need to reach the UI.
|
|
177
|
+
if (!state.assistantMessageId && !state.textContent) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const messages = (event as any).messages;
|
|
182
|
+
if (!Array.isArray(messages)) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const lastAssistant = [...messages].reverse().find((message) => message?.role === 'assistant');
|
|
187
|
+
if (!lastAssistant) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
(typeof lastAssistant.id === 'string' && lastAssistant.id === state.assistantMessageId) ||
|
|
193
|
+
(typeof lastAssistant.content === 'string' && lastAssistant.content === state.textContent)
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
119
197
|
/** Process tool call events and update state */
|
|
120
198
|
private handleToolCallEvent(event: BaseEvent, state: RunState): void {
|
|
121
|
-
const { toolCallArgsBuffer, toolCallNames, pendingMcpCalls } = state;
|
|
199
|
+
const { toolCallArgsBuffer, toolCallNames, pendingMcpCalls, completedMcpCalls } = state;
|
|
122
200
|
|
|
123
201
|
// Accumulate text content for reconstruction
|
|
124
|
-
if (event.type === EventType.TEXT_MESSAGE_CHUNK) {
|
|
202
|
+
if (event.type === EventType.TEXT_MESSAGE_CHUNK || event.type === EventType.TEXT_MESSAGE_CONTENT) {
|
|
125
203
|
const e = event as any;
|
|
204
|
+
if (typeof e.messageId === 'string') {
|
|
205
|
+
state.assistantMessageId = e.messageId;
|
|
206
|
+
}
|
|
126
207
|
if (e.delta) {
|
|
127
208
|
state.textContent = (state.textContent || '') + e.delta;
|
|
128
209
|
}
|
|
@@ -132,7 +213,7 @@ export class McpMiddleware extends Middleware {
|
|
|
132
213
|
const e = event as any;
|
|
133
214
|
if (e.toolCallId && e.toolCallName) {
|
|
134
215
|
toolCallNames.set(e.toolCallId, e.toolCallName);
|
|
135
|
-
if (this.isMcpTool(e.toolCallName)) {
|
|
216
|
+
if (this.isMcpTool(e.toolCallName) && !completedMcpCalls.has(e.toolCallId)) {
|
|
136
217
|
pendingMcpCalls.add(e.toolCallId);
|
|
137
218
|
}
|
|
138
219
|
console.log(`[McpMiddleware] TOOL_CALL_START: ${e.toolCallName} (id: ${e.toolCallId}, isMCP: ${this.isMcpTool(e.toolCallName)})`);
|
|
@@ -152,33 +233,52 @@ export class McpMiddleware extends Middleware {
|
|
|
152
233
|
console.log(`[McpMiddleware] TOOL_CALL_END: ${toolCallNames.get(e.toolCallId) ?? 'unknown'} (id: ${e.toolCallId})`);
|
|
153
234
|
}
|
|
154
235
|
|
|
236
|
+
if (event.type === EventType.TOOL_CALL_RESULT) {
|
|
237
|
+
const e = event as any;
|
|
238
|
+
if (typeof e.toolCallId === 'string') {
|
|
239
|
+
pendingMcpCalls.delete(e.toolCallId);
|
|
240
|
+
completedMcpCalls.add(e.toolCallId);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
155
244
|
// Workaround: Extract parallel tool calls from MESSAGES_SNAPSHOT
|
|
156
245
|
if (event.type === EventType.MESSAGES_SNAPSHOT) {
|
|
157
246
|
const messages = (event as any).messages || [];
|
|
158
247
|
if (messages.length > 0) {
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
248
|
+
const resolvedToolCallIds = this.getResolvedToolCallIds(messages);
|
|
249
|
+
|
|
250
|
+
for (const toolCallId of [...pendingMcpCalls]) {
|
|
251
|
+
if (resolvedToolCallIds.has(toolCallId) || completedMcpCalls.has(toolCallId)) {
|
|
252
|
+
pendingMcpCalls.delete(toolCallId);
|
|
253
|
+
}
|
|
163
254
|
}
|
|
164
255
|
|
|
165
|
-
// Discover tools
|
|
256
|
+
// Discover unresolved tools. LangGraph snapshots include historical
|
|
257
|
+
// assistant tool calls, so only calls without a tool result are pending.
|
|
166
258
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
167
259
|
const msg = messages[i];
|
|
168
|
-
const tools =
|
|
169
|
-
|
|
260
|
+
const tools = this.getSnapshotToolCalls(msg)
|
|
261
|
+
.filter(tc =>
|
|
262
|
+
!resolvedToolCallIds.has(tc.id) &&
|
|
263
|
+
!completedMcpCalls.has(tc.id) &&
|
|
264
|
+
this.isMcpTool(tc.name)
|
|
265
|
+
);
|
|
170
266
|
|
|
171
267
|
if (msg.role === 'assistant' && tools.length > 0) {
|
|
172
268
|
for (const tc of tools) {
|
|
173
|
-
if (
|
|
174
|
-
toolCallNames.set(tc.id, tc.
|
|
175
|
-
toolCallArgsBuffer.set(tc.id, tc.
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
console.log(`[McpMiddleware] MESSAGES_SNAPSHOT: Discovered ${tc.function.name} (id: ${tc.id})`);
|
|
179
|
-
}
|
|
269
|
+
if (!toolCallNames.has(tc.id)) {
|
|
270
|
+
toolCallNames.set(tc.id, tc.name);
|
|
271
|
+
toolCallArgsBuffer.set(tc.id, tc.arguments || '{}');
|
|
272
|
+
pendingMcpCalls.add(tc.id);
|
|
273
|
+
console.log(`[McpMiddleware] MESSAGES_SNAPSHOT: Discovered ${tc.name} (id: ${tc.id})`);
|
|
180
274
|
}
|
|
181
275
|
}
|
|
276
|
+
if (typeof msg.id === 'string') {
|
|
277
|
+
state.assistantMessageId = msg.id;
|
|
278
|
+
}
|
|
279
|
+
if (typeof msg.content === 'string') {
|
|
280
|
+
state.textContent = msg.content;
|
|
281
|
+
}
|
|
182
282
|
break;
|
|
183
283
|
}
|
|
184
284
|
}
|
|
@@ -188,10 +288,15 @@ export class McpMiddleware extends Middleware {
|
|
|
188
288
|
|
|
189
289
|
/** Execute pending MCP tools and return results */
|
|
190
290
|
private async executeTools(state: RunState): Promise<ToolResult[]> {
|
|
191
|
-
const { toolCallArgsBuffer, toolCallNames, pendingMcpCalls } = state;
|
|
291
|
+
const { toolCallArgsBuffer, toolCallNames, pendingMcpCalls, completedMcpCalls } = state;
|
|
192
292
|
const results: ToolResult[] = [];
|
|
193
293
|
|
|
194
294
|
const promises = [...pendingMcpCalls].map(async (toolCallId) => {
|
|
295
|
+
if (completedMcpCalls.has(toolCallId)) {
|
|
296
|
+
pendingMcpCalls.delete(toolCallId);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
195
300
|
const toolName = toolCallNames.get(toolCallId);
|
|
196
301
|
if (!toolName) return;
|
|
197
302
|
|
|
@@ -206,6 +311,7 @@ export class McpMiddleware extends Middleware {
|
|
|
206
311
|
messageId: this.generateId('mcp_result'),
|
|
207
312
|
});
|
|
208
313
|
pendingMcpCalls.delete(toolCallId);
|
|
314
|
+
completedMcpCalls.add(toolCallId);
|
|
209
315
|
});
|
|
210
316
|
|
|
211
317
|
await Promise.all(promises);
|
|
@@ -229,10 +335,13 @@ export class McpMiddleware extends Middleware {
|
|
|
229
335
|
|
|
230
336
|
run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent> {
|
|
231
337
|
return new Observable<BaseEvent>((observer: Subscriber<BaseEvent>) => {
|
|
338
|
+
const subscriptions = new Set<Subscription>();
|
|
339
|
+
let continuationInProgress = false;
|
|
232
340
|
const state: RunState = {
|
|
233
341
|
toolCallArgsBuffer: new Map(),
|
|
234
342
|
toolCallNames: new Map(),
|
|
235
343
|
pendingMcpCalls: new Set(),
|
|
344
|
+
completedMcpCalls: new Set(),
|
|
236
345
|
textContent: '',
|
|
237
346
|
error: false,
|
|
238
347
|
};
|
|
@@ -265,12 +374,13 @@ export class McpMiddleware extends Middleware {
|
|
|
265
374
|
}
|
|
266
375
|
|
|
267
376
|
console.log(`[McpMiddleware] RUN_FINISHED with ${state.pendingMcpCalls.size} pending calls`);
|
|
377
|
+
continuationInProgress = true;
|
|
268
378
|
|
|
269
379
|
// Reconstruct the Assistant Message that triggered these tools
|
|
270
380
|
const toolCalls = [];
|
|
271
381
|
for (const toolCallId of state.pendingMcpCalls) {
|
|
272
382
|
const name = state.toolCallNames.get(toolCallId);
|
|
273
|
-
const args = state.toolCallArgsBuffer.get(toolCallId) || '{}';
|
|
383
|
+
const args = this.normalizeArgsString(state.toolCallArgsBuffer.get(toolCallId) || '{}');
|
|
274
384
|
if (name) {
|
|
275
385
|
toolCalls.push({
|
|
276
386
|
id: toolCallId,
|
|
@@ -283,10 +393,10 @@ export class McpMiddleware extends Middleware {
|
|
|
283
393
|
// Add the Assistant Message to history FIRST
|
|
284
394
|
if (toolCalls.length > 0 || state.textContent) {
|
|
285
395
|
const assistantMsg = {
|
|
286
|
-
id: this.generateId('msg_ast'),
|
|
396
|
+
id: state.assistantMessageId || this.generateId('msg_ast'),
|
|
287
397
|
role: 'assistant',
|
|
288
398
|
content: state.textContent || null, // Ensure null if empty string for strict LLMs
|
|
289
|
-
|
|
399
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined
|
|
290
400
|
};
|
|
291
401
|
input.messages.push(assistantMsg as any);
|
|
292
402
|
console.log(`[McpMiddleware] Added assistant message to history before tools: ${state.textContent?.slice(0, 50)}... [${toolCalls.length} tools]`);
|
|
@@ -304,7 +414,7 @@ export class McpMiddleware extends Middleware {
|
|
|
304
414
|
input.messages.push({
|
|
305
415
|
id: messageId,
|
|
306
416
|
role: 'tool',
|
|
307
|
-
|
|
417
|
+
toolCallId,
|
|
308
418
|
content: result,
|
|
309
419
|
} as any);
|
|
310
420
|
}
|
|
@@ -312,35 +422,45 @@ export class McpMiddleware extends Middleware {
|
|
|
312
422
|
// Reset state for next turn
|
|
313
423
|
state.toolCallArgsBuffer.clear();
|
|
314
424
|
state.toolCallNames.clear();
|
|
425
|
+
state.assistantMessageId = undefined;
|
|
315
426
|
state.textContent = ''; // Clear text content for next turn
|
|
316
427
|
|
|
317
|
-
anyInput.runId = this.generateId('mcp_run');
|
|
318
428
|
console.log(`[McpMiddleware] === CONTINUATION RUN === messages: ${input.messages.length}`);
|
|
319
429
|
|
|
320
|
-
|
|
321
|
-
|
|
430
|
+
subscribeToRun(true);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const subscribeToRun = (isContinuation: boolean): Subscription => {
|
|
434
|
+
let subscription = new Subscription();
|
|
435
|
+
subscription = next.run(input).subscribe({
|
|
322
436
|
next: (event) => {
|
|
323
437
|
if (state.error) return;
|
|
324
438
|
|
|
325
439
|
this.handleToolCallEvent(event, state);
|
|
326
440
|
|
|
327
441
|
if (event.type === EventType.RUN_ERROR) {
|
|
328
|
-
console.log(`[McpMiddleware] RUN_ERROR received in continuation`);
|
|
442
|
+
console.log(`[McpMiddleware] RUN_ERROR received${isContinuation ? ' in continuation' : ''}`);
|
|
329
443
|
state.error = true;
|
|
330
444
|
observer.next(event);
|
|
331
445
|
observer.complete();
|
|
332
446
|
return;
|
|
333
447
|
}
|
|
334
448
|
|
|
335
|
-
if (event.type === EventType.RUN_STARTED) {
|
|
449
|
+
if (isContinuation && event.type === EventType.RUN_STARTED) {
|
|
336
450
|
console.log(`[McpMiddleware] Filtering RUN_STARTED from continuation`);
|
|
337
451
|
return;
|
|
338
452
|
}
|
|
339
453
|
|
|
454
|
+
if (event.type === EventType.MESSAGES_SNAPSHOT && this.shouldFilterMessagesSnapshot(event, state)) {
|
|
455
|
+
console.log(`[McpMiddleware] Filtering completed MCP MESSAGES_SNAPSHOT to preserve streamed message order`);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
|
|
340
459
|
if (event.type === EventType.RUN_FINISHED) {
|
|
341
460
|
if (state.pendingMcpCalls.size > 0) {
|
|
342
461
|
handleRunFinished();
|
|
343
462
|
} else {
|
|
463
|
+
continuationInProgress = false;
|
|
344
464
|
observer.next(event);
|
|
345
465
|
observer.complete();
|
|
346
466
|
}
|
|
@@ -350,44 +470,28 @@ export class McpMiddleware extends Middleware {
|
|
|
350
470
|
},
|
|
351
471
|
error: (err) => {
|
|
352
472
|
state.error = true;
|
|
473
|
+
continuationInProgress = false;
|
|
353
474
|
observer.error(err);
|
|
354
475
|
},
|
|
355
476
|
complete: () => {
|
|
356
|
-
|
|
477
|
+
subscriptions.delete(subscription);
|
|
478
|
+
if (!state.error && state.pendingMcpCalls.size === 0 && !continuationInProgress) observer.complete();
|
|
357
479
|
},
|
|
358
480
|
});
|
|
481
|
+
if (!subscription.closed) {
|
|
482
|
+
subscriptions.add(subscription);
|
|
483
|
+
}
|
|
484
|
+
return subscription;
|
|
359
485
|
};
|
|
360
486
|
|
|
361
|
-
|
|
362
|
-
next: (event) => {
|
|
363
|
-
if (state.error) return;
|
|
364
|
-
|
|
365
|
-
this.handleToolCallEvent(event, state);
|
|
487
|
+
subscribeToRun(false);
|
|
366
488
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
if (event.type === EventType.RUN_FINISHED) {
|
|
376
|
-
handleRunFinished();
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
observer.next(event);
|
|
380
|
-
},
|
|
381
|
-
error: (err) => {
|
|
382
|
-
state.error = true;
|
|
383
|
-
observer.error(err);
|
|
384
|
-
},
|
|
385
|
-
complete: () => {
|
|
386
|
-
if (!state.error && state.pendingMcpCalls.size === 0) observer.complete();
|
|
387
|
-
},
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
return () => subscription.unsubscribe();
|
|
489
|
+
return () => {
|
|
490
|
+
for (const subscription of subscriptions) {
|
|
491
|
+
subscription.unsubscribe();
|
|
492
|
+
}
|
|
493
|
+
subscriptions.clear();
|
|
494
|
+
};
|
|
391
495
|
});
|
|
392
496
|
}
|
|
393
497
|
}
|
|
@@ -410,4 +514,4 @@ export { createMcpMiddleware as createMcpToolMiddleware };
|
|
|
410
514
|
|
|
411
515
|
// Re-exports
|
|
412
516
|
export { Middleware, EventType };
|
|
413
|
-
export type { RunAgentInput, BaseEvent, AbstractAgent, ToolCallEndEvent, Tool };
|
|
517
|
+
export type { RunAgentInput, BaseEvent, AbstractAgent, ToolCallEndEvent, Tool };
|