@mcp-ts/sdk 1.0.0 → 1.0.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 +3 -3
- package/dist/adapters/agui-adapter.d.mts +19 -42
- package/dist/adapters/agui-adapter.d.ts +19 -42
- package/dist/adapters/agui-adapter.js +69 -69
- package/dist/adapters/agui-adapter.js.map +1 -1
- package/dist/adapters/agui-adapter.mjs +69 -70
- package/dist/adapters/agui-adapter.mjs.map +1 -1
- package/dist/adapters/agui-middleware.d.mts +24 -136
- package/dist/adapters/agui-middleware.d.ts +24 -136
- package/dist/adapters/agui-middleware.js +275 -350
- package/dist/adapters/agui-middleware.js.map +1 -1
- package/dist/adapters/agui-middleware.mjs +275 -350
- package/dist/adapters/agui-middleware.mjs.map +1 -1
- package/dist/client/index.d.mts +2 -2
- package/dist/client/index.d.ts +2 -2
- package/dist/client/react.d.mts +2 -2
- package/dist/client/react.d.ts +2 -2
- package/dist/client/vue.d.mts +2 -2
- package/dist/client/vue.d.ts +2 -2
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/server/index.d.mts +3 -3
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.js +2 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +2 -1
- package/dist/server/index.mjs.map +1 -1
- package/dist/shared/index.d.mts +1 -1
- package/dist/shared/index.d.ts +1 -1
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs.map +1 -1
- package/dist/{types-SbDlA2VX.d.mts → types-CLccx9wW.d.mts} +1 -1
- package/dist/{types-SbDlA2VX.d.ts → types-CLccx9wW.d.ts} +1 -1
- package/package.json +2 -2
- package/src/adapters/agui-adapter.ts +98 -109
- package/src/adapters/agui-middleware.ts +424 -512
- package/src/server/handlers/sse-handler.ts +4 -1
- package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O,KAAA;AAAA,EACX;AACJ","file":"agui-adapter.mjs","sourcesContent":["/**\r\n * MCP Adapter for AG-UI Integration\r\n *\r\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\r\n * It provides tools with handlers for server-side execution and tool definitions\r\n * in JSON Schema format for passing to remote agents.\r\n *\r\n * @example\r\n * ```typescript\r\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\r\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/mcp-adapter';\r\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\r\n * import { HttpAgent } from '@ag-ui/client';\r\n *\r\n * // Create MCP client\r\n * const mcpClient = new MultiSessionClient('user_123');\r\n * await mcpClient.connect();\r\n *\r\n * // Create adapter and get tools\r\n * const adapter = new AguiAdapter(mcpClient);\r\n * const tools = await adapter.getTools();\r\n *\r\n * // Use with AG-UI middleware\r\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\r\n * agent.use(createMcpMiddleware(mcpClient, { tools }));\r\n * ```\r\n */\r\n\r\nimport { MCPClient } from '../server/mcp/oauth-client.js';\r\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\r\n\r\n/**\r\n * Configuration options for AguiAdapter\r\n */\r\nexport interface AguiAdapterOptions {\r\n /**\r\n * Prefix for tool names to avoid collision with other tools.\r\n * @default serverId or 'mcp'\r\n */\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * AG-UI Tool with handler for server-side execution.\r\n *\r\n * Tools contain:\r\n * - `name`: Unique identifier (prefixed with server ID)\r\n * - `description`: Human-readable description for the LLM\r\n * - `parameters`: JSON Schema defining the input format\r\n * - `handler`: Function that executes the tool via MCP client\r\n */\r\nexport interface AguiTool {\r\n /** Unique tool name (e.g., \"server-abc_get_weather\") */\r\n name: string;\r\n /** Human-readable description for the LLM */\r\n description: string;\r\n /** JSON Schema format parameters */\r\n parameters?: Record<string, any>;\r\n /** Handler function that executes the MCP tool */\r\n handler?: (args: any) => any | Promise<any>;\r\n}\r\n\r\n/**\r\n * Tool definition format for passing to remote agents (without handler).\r\n * Compatible with OpenAI's function calling API.\r\n */\r\nexport interface AguiToolDefinition {\r\n /** Tool name (e.g., \"server-abc_get_weather\") */\r\n name: string;\r\n /** Human-readable description */\r\n description: string;\r\n /** JSON Schema format parameters */\r\n parameters: Record<string, any>;\r\n}\r\n\r\n/**\r\n * Adapter that transforms MCP tools into AG-UI compatible formats.\r\n *\r\n * This adapter provides two main outputs:\r\n * - `getTools()`: Returns tools with handlers for server-side execution\r\n * - `getToolDefinitions()`: Returns tool definitions in JSON Schema format for remote agents\r\n */\r\nexport class AguiAdapter {\r\n constructor(\r\n private client: MCPClient | MultiSessionClient,\r\n private options: AguiAdapterOptions = {}\r\n ) { }\r\n\r\n /**\r\n * Get tools with handlers for MCP tool execution.\r\n *\r\n * Each tool includes a handler function that:\r\n * 1. Calls the MCP tool via the client\r\n * 2. Extracts text content from the result\r\n * 3. Returns the result as a string or JSON\r\n *\r\n * @returns Array of AguiTool objects\r\n */\r\n async getTools(): Promise<AguiTool[]> {\r\n const isMultiSession = typeof (this.client as any).getClients === 'function';\r\n\r\n if (isMultiSession) {\r\n const clients = (this.client as MultiSessionClient).getClients();\r\n const allTools: AguiTool[] = [];\r\n\r\n for (const client of clients) {\r\n const tools = await this.transformTools(client);\r\n allTools.push(...tools);\r\n }\r\n\r\n return allTools;\r\n }\r\n\r\n return this.transformTools(this.client as MCPClient);\r\n }\r\n\r\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\r\n if (!client.isConnected()) {\r\n return [];\r\n }\r\n\r\n const result = await client.listTools();\r\n const prefix = this.options.prefix ?? client.getServerId() ?? 'mcp';\r\n const tools: AguiTool[] = [];\r\n\r\n for (const tool of result.tools) {\r\n const toolName = `${prefix}_${tool.name}`;\r\n\r\n tools.push({\r\n name: toolName,\r\n description: tool.description || `Execute ${tool.name}`,\r\n parameters: tool.inputSchema || { type: 'object', properties: {} },\r\n handler: async (args: any) => {\r\n console.log(`[AguiAdapter] Executing MCP tool: ${tool.name}`, args);\r\n const result = await client.callTool(tool.name, args);\r\n\r\n // Extract text content from result\r\n if (result.content && Array.isArray(result.content)) {\r\n const textContent = result.content\r\n .filter((c: any) => c.type === 'text')\r\n .map((c: any) => c.text)\r\n .join('\\n');\r\n return textContent || result;\r\n }\r\n\r\n return result;\r\n }\r\n });\r\n }\r\n\r\n return tools;\r\n }\r\n\r\n /**\r\n * Get tools as a function (for dynamic loading).\r\n *\r\n * @returns Function that returns a Promise of tools\r\n */\r\n getToolsFunction(): () => Promise<AguiTool[]> {\r\n return async () => this.getTools();\r\n }\r\n\r\n /**\r\n * Get tool definitions in JSON Schema format for passing to remote agents.\r\n *\r\n * This format is compatible with:\r\n * - OpenAI's function calling API\r\n * - AG-UI input.tools format\r\n * - Most LLM tool/function calling implementations\r\n *\r\n * @returns Array of AguiToolDefinition objects\r\n */\r\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\r\n const isMultiSession = typeof (this.client as any).getClients === 'function';\r\n\r\n if (isMultiSession) {\r\n const clients = (this.client as MultiSessionClient).getClients();\r\n const allTools: AguiToolDefinition[] = [];\r\n\r\n for (const client of clients) {\r\n const tools = await this.transformToolDefinitions(client);\r\n allTools.push(...tools);\r\n }\r\n\r\n return allTools;\r\n }\r\n\r\n return this.transformToolDefinitions(this.client as MCPClient);\r\n }\r\n\r\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\r\n if (!client.isConnected()) {\r\n return [];\r\n }\r\n\r\n const result = await client.listTools();\r\n const prefix = this.options.prefix ?? client.getServerId() ?? 'mcp';\r\n const tools: AguiToolDefinition[] = [];\r\n\r\n for (const tool of result.tools) {\r\n tools.push({\r\n name: `${prefix}_${tool.name}`,\r\n description: tool.description || `Execute ${tool.name}`,\r\n parameters: tool.inputSchema || { type: 'object', properties: {} },\r\n });\r\n }\r\n\r\n return tools;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/agui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eA,OAAAA;AAAA,QAC1B;AACA,QAAA,OAAOA,OAAAA;AAAA,MACX;AAAA,KACJ,CAAE,CAAA;AAAA,EACN;AAAA,EAEA,MAAc,yBAAyB,MAAA,EAAkD;AACrF,IAAA,IAAI,CAAC,MAAA,CAAO,WAAA,EAAY,SAAU,EAAC;AAEnC,IAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,SAAA,EAAU;AACtC,IAAA,MAAM,MAAA,GAAS,KAAK,OAAA,CAAQ,MAAA,IAAU,QAAQ,MAAA,CAAO,WAAA,MAAiB,KAAK,CAAA,CAAA;AAE3E,IAAA,OAAO,MAAA,CAAO,KAAA,CAAM,GAAA,CAAI,CAAA,IAAA,MAAS;AAAA,MAC7B,IAAA,EAAM,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,KAAK,IAAI,CAAA,CAAA;AAAA,MAC5B,WAAA,EAAa,IAAA,CAAK,WAAA,IAAe,CAAA,QAAA,EAAW,KAAK,IAAI,CAAA,CAAA;AAAA,MACrD,UAAA,EAAY,WAAA,CAAY,IAAA,CAAK,WAAW;AAAA,KAC5C,CAAE,CAAA;AAAA,EACN;AACJ","file":"agui-adapter.mjs","sourcesContent":["/**\r\n * MCP Adapter for AG-UI Integration\r\n *\r\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\r\n * It provides tools with handlers for server-side execution and tool definitions\r\n * in JSON Schema format for passing to remote agents.\r\n *\r\n * @example\r\n * ```typescript\r\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\r\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';\r\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\r\n * import { HttpAgent } from '@ag-ui/client';\r\n *\r\n * // Create MCP client\r\n * const mcpClient = new MultiSessionClient('user_123');\r\n * await mcpClient.connect();\r\n *\r\n * // Create adapter and get tools\r\n * const adapter = new AguiAdapter(mcpClient);\r\n * const tools = await adapter.getTools();\r\n *\r\n * // Use with AG-UI middleware\r\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\r\n * agent.use(createMcpMiddleware({ tools }));\r\n * ```\r\n */\r\n\r\nimport { MCPClient } from '../server/mcp/oauth-client.js';\r\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\r\n\r\n/**\r\n * Cleans a JSON Schema by removing meta-properties that cause issues with\r\n * strict Pydantic validation (e.g., Google ADK).\r\n *\r\n * Removes: $schema, $id, $comment, $defs, definitions\r\n *\r\n * @param schema - The JSON Schema to clean\r\n * @returns Cleaned schema without meta-properties\r\n */\r\nexport function cleanSchema(schema: Record<string, any> | undefined): Record<string, any> {\r\n if (!schema) {\r\n return { type: 'object', properties: {} };\r\n }\r\n\r\n const cleaned = { ...schema };\r\n\r\n // Remove JSON Schema meta-properties that cause Pydantic validation errors\r\n delete cleaned.$schema;\r\n delete cleaned.$id;\r\n delete cleaned.$comment;\r\n delete cleaned.$defs;\r\n delete cleaned.definitions;\r\n\r\n // Recursively clean nested properties\r\n if (cleaned.properties && typeof cleaned.properties === 'object') {\r\n const cleanedProps: Record<string, any> = {};\r\n for (const [key, value] of Object.entries(cleaned.properties)) {\r\n if (typeof value === 'object' && value !== null) {\r\n cleanedProps[key] = cleanSchema(value as Record<string, any>);\r\n } else {\r\n cleanedProps[key] = value;\r\n }\r\n }\r\n cleaned.properties = cleanedProps;\r\n }\r\n\r\n // Clean items if it's an array schema\r\n if (cleaned.items && typeof cleaned.items === 'object') {\r\n cleaned.items = cleanSchema(cleaned.items);\r\n }\r\n\r\n // Clean additionalProperties if it's an object schema\r\n if (cleaned.additionalProperties && typeof cleaned.additionalProperties === 'object') {\r\n cleaned.additionalProperties = cleanSchema(cleaned.additionalProperties);\r\n }\r\n\r\n return cleaned;\r\n}\r\n\r\n/**\r\n * Configuration options for AguiAdapter\r\n */\r\nexport interface AguiAdapterOptions {\r\n /**\r\n * Prefix for tool names to avoid collision with other tools.\r\n * @default serverId or 'mcp'\r\n */\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * AG-UI Tool with handler for server-side execution.\r\n */\r\nexport interface AguiTool {\r\n name: string;\r\n description: string;\r\n parameters?: Record<string, any>;\r\n handler?: (args: any) => any | Promise<any>;\r\n}\r\n\r\n/**\r\n * Tool definition format for passing to remote agents (without handler).\r\n */\r\nexport interface AguiToolDefinition {\r\n name: string;\r\n description: string;\r\n parameters: Record<string, any>;\r\n}\r\n\r\n/**\r\n * Adapter that transforms MCP tools into AG-UI compatible formats.\r\n */\r\nexport class AguiAdapter {\r\n constructor(\r\n private client: MCPClient | MultiSessionClient,\r\n private options: AguiAdapterOptions = {}\r\n ) { }\r\n\r\n /**\r\n * Get tools with handlers for MCP tool execution.\r\n */\r\n async getTools(): Promise<AguiTool[]> {\r\n if (this.isMultiSession()) {\r\n const clients = (this.client as MultiSessionClient).getClients();\r\n const allTools: AguiTool[] = [];\r\n for (const client of clients) {\r\n allTools.push(...await this.transformTools(client));\r\n }\r\n return allTools;\r\n }\r\n return this.transformTools(this.client as MCPClient);\r\n }\r\n\r\n /**\r\n * Get tool definitions in JSON Schema format for passing to remote agents.\r\n */\r\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\r\n if (this.isMultiSession()) {\r\n const clients = (this.client as MultiSessionClient).getClients();\r\n const allTools: AguiToolDefinition[] = [];\r\n for (const client of clients) {\r\n allTools.push(...await this.transformToolDefinitions(client));\r\n }\r\n return allTools;\r\n }\r\n return this.transformToolDefinitions(this.client as MCPClient);\r\n }\r\n\r\n /**\r\n * Get tools as a function (for dynamic loading).\r\n */\r\n getToolsFunction(): () => Promise<AguiTool[]> {\r\n return () => this.getTools();\r\n }\r\n\r\n private isMultiSession(): boolean {\r\n return typeof (this.client as any).getClients === 'function';\r\n }\r\n\r\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\r\n if (!client.isConnected()) return [];\r\n\r\n const result = await client.listTools();\r\n const prefix = this.options.prefix ?? `tool_${client.getServerId() ?? 'mcp'}`;\r\n\r\n return result.tools.map(tool => ({\r\n name: `${prefix}_${tool.name}`,\r\n description: tool.description || `Execute ${tool.name}`,\r\n parameters: cleanSchema(tool.inputSchema),\r\n handler: async (args: any) => {\r\n console.log(`[AguiAdapter] Executing MCP tool: ${tool.name}`, args);\r\n const result = await client.callTool(tool.name, args);\r\n\r\n if (result.content && Array.isArray(result.content)) {\r\n const textContent = result.content\r\n .filter((c: any) => c.type === 'text')\r\n .map((c: any) => c.text)\r\n .join('\\n');\r\n return textContent || result;\r\n }\r\n return result;\r\n }\r\n }));\r\n }\r\n\r\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\r\n if (!client.isConnected()) return [];\r\n\r\n const result = await client.listTools();\r\n const prefix = this.options.prefix ?? `tool_${client.getServerId() ?? 'mcp'}`;\r\n\r\n return result.tools.map(tool => ({\r\n name: `${prefix}_${tool.name}`,\r\n description: tool.description || `Execute ${tool.name}`,\r\n parameters: cleanSchema(tool.inputSchema),\r\n }));\r\n }\r\n}\r\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { Middleware, RunAgentInput, AbstractAgent, BaseEvent } from '@ag-ui/client';
|
|
3
|
-
export { AbstractAgent, BaseEvent, EventType, Middleware, RunAgentInput, ToolCallEndEvent } from '@ag-ui/client';
|
|
4
|
-
import { M as MCPClient, a as MultiSessionClient } from '../multi-session-client-DMF3ED2O.mjs';
|
|
3
|
+
export { AbstractAgent, BaseEvent, EventType, Middleware, RunAgentInput, Tool, ToolCallEndEvent } from '@ag-ui/client';
|
|
5
4
|
import { AguiTool } from './agui-adapter.mjs';
|
|
5
|
+
import '../multi-session-client-DMF3ED2O.mjs';
|
|
6
6
|
import '../events-BP6WyRNh.mjs';
|
|
7
7
|
import '@modelcontextprotocol/sdk/types.js';
|
|
8
8
|
import '@modelcontextprotocol/sdk/shared/auth.js';
|
|
@@ -11,161 +11,49 @@ import '@modelcontextprotocol/sdk/client/auth.js';
|
|
|
11
11
|
/**
|
|
12
12
|
* AG-UI Middleware for MCP Tool Execution
|
|
13
13
|
*
|
|
14
|
-
* This middleware intercepts tool calls from remote agents
|
|
15
|
-
*
|
|
14
|
+
* This middleware intercepts tool calls from remote agents and executes
|
|
15
|
+
* MCP tools server-side, returning results back to the agent.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* 1. **Tool Injection**: When a run starts, the middleware injects MCP tool definitions
|
|
20
|
-
* into `input.tools` so the remote agent knows about available MCP tools.
|
|
21
|
-
*
|
|
22
|
-
* 2. **Event Interception**: The middleware subscribes to the agent's event stream and
|
|
23
|
-
* tracks tool calls using AG-UI events:
|
|
24
|
-
* - `TOOL_CALL_START`: Records tool name and ID
|
|
25
|
-
* - `TOOL_CALL_ARGS`: Accumulates streamed arguments
|
|
26
|
-
* - `TOOL_CALL_END`: Marks tool call as complete
|
|
27
|
-
* - `RUN_FINISHED`: Triggers execution of pending MCP tools
|
|
28
|
-
*
|
|
29
|
-
* 3. **Server-Side Execution**: When `RUN_FINISHED` arrives with pending MCP tool calls,
|
|
30
|
-
* the middleware:
|
|
31
|
-
* - Executes each MCP tool via the MCP client
|
|
32
|
-
* - Emits `TOOL_CALL_RESULT` events with the results
|
|
33
|
-
* - Adds results to `input.messages` for context
|
|
34
|
-
* - Emits `RUN_FINISHED` to close the current run
|
|
35
|
-
* - Triggers a new run so the agent can process tool results
|
|
36
|
-
*
|
|
37
|
-
* 4. **Recursive Processing**: If the new run makes more MCP tool calls, the cycle
|
|
38
|
-
* repeats until the agent completes without pending MCP calls.
|
|
39
|
-
*
|
|
40
|
-
* ## Tool Identification
|
|
41
|
-
*
|
|
42
|
-
* MCP tools are identified by a configurable prefix (default: `server-`).
|
|
43
|
-
* Tools not matching this prefix are passed through without interception.
|
|
44
|
-
*
|
|
45
|
-
* @requires @ag-ui/client - This middleware requires @ag-ui/client as a peer dependency
|
|
17
|
+
* @requires @ag-ui/client - Peer dependency for AG-UI types
|
|
46
18
|
* @requires rxjs - Uses RxJS Observables for event streaming
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
51
|
-
* import { McpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';
|
|
52
|
-
* import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';
|
|
53
|
-
*
|
|
54
|
-
* // Create MCP client and adapter
|
|
55
|
-
* const mcpClient = new MultiSessionClient('user_123');
|
|
56
|
-
* await mcpClient.connect();
|
|
57
|
-
*
|
|
58
|
-
* const adapter = new AguiAdapter(mcpClient);
|
|
59
|
-
* const actions = await adapter.getActions();
|
|
60
|
-
*
|
|
61
|
-
* // Create middleware with pre-loaded actions
|
|
62
|
-
* const middleware = new McpMiddleware({
|
|
63
|
-
* client: mcpClient,
|
|
64
|
-
* actions,
|
|
65
|
-
* toolPrefix: 'server-',
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* // Use with HttpAgent
|
|
69
|
-
* const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });
|
|
70
|
-
* agent.use(middleware);
|
|
71
|
-
* ```
|
|
72
19
|
*/
|
|
73
20
|
|
|
74
|
-
/**
|
|
75
|
-
* Tool definition format for AG-UI input.tools
|
|
76
|
-
*/
|
|
77
|
-
interface AgUiTool {
|
|
78
|
-
name: string;
|
|
79
|
-
description: string;
|
|
80
|
-
parameters?: Record<string, any>;
|
|
81
|
-
}
|
|
82
21
|
/**
|
|
83
22
|
* Configuration for McpMiddleware
|
|
84
23
|
*/
|
|
85
24
|
interface McpMiddlewareConfig {
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Prefix used to identify MCP tool names.
|
|
92
|
-
* Tools starting with this prefix will be executed server-side.
|
|
93
|
-
* @default 'server-'
|
|
94
|
-
*/
|
|
95
|
-
toolPrefix?: string;
|
|
96
|
-
/**
|
|
97
|
-
* Pre-loaded tools with handlers for execution.
|
|
98
|
-
* If not provided, tools will be loaded from the MCP client on first use.
|
|
99
|
-
*/
|
|
100
|
-
tools?: AguiTool[];
|
|
25
|
+
/** Pre-loaded tools with handlers (required) */
|
|
26
|
+
tools: AguiTool[];
|
|
27
|
+
/** Max result length in chars (default: 50000) */
|
|
28
|
+
maxResultLength?: number;
|
|
101
29
|
}
|
|
102
30
|
/**
|
|
103
31
|
* AG-UI Middleware that executes MCP tools server-side.
|
|
104
|
-
*
|
|
105
|
-
* This middleware intercepts tool calls for MCP tools (identified by prefix),
|
|
106
|
-
* executes them via the MCP client, and returns results to the agent.
|
|
107
|
-
*
|
|
108
|
-
* @see {@link createMcpMiddleware} for a simpler factory function
|
|
109
32
|
*/
|
|
110
33
|
declare class McpMiddleware extends Middleware {
|
|
111
|
-
private client;
|
|
112
|
-
private toolPrefix;
|
|
113
|
-
private actions;
|
|
114
34
|
private tools;
|
|
115
|
-
private
|
|
35
|
+
private toolSchemas;
|
|
36
|
+
private maxResultLength;
|
|
116
37
|
constructor(config: McpMiddlewareConfig);
|
|
117
|
-
/**
|
|
118
|
-
* Convert actions to AG-UI tool format
|
|
119
|
-
*/
|
|
120
|
-
private actionsToTools;
|
|
121
|
-
/**
|
|
122
|
-
* Check if a tool name is an MCP tool (matches the configured prefix)
|
|
123
|
-
*/
|
|
124
38
|
private isMcpTool;
|
|
125
|
-
|
|
126
|
-
* Load actions from the MCP client if not already loaded
|
|
127
|
-
*/
|
|
128
|
-
private ensureActionsLoaded;
|
|
129
|
-
/**
|
|
130
|
-
* Execute an MCP tool and return the result as a string
|
|
131
|
-
*/
|
|
39
|
+
private parseArgs;
|
|
132
40
|
private executeTool;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
private
|
|
137
|
-
/**
|
|
138
|
-
|
|
139
|
-
|
|
41
|
+
private generateId;
|
|
42
|
+
private ensureIds;
|
|
43
|
+
/** Process tool call events and update state */
|
|
44
|
+
private handleToolCallEvent;
|
|
45
|
+
/** Execute pending MCP tools and return results */
|
|
46
|
+
private executeTools;
|
|
47
|
+
/** Emit tool results (without RUN_FINISHED - that's emitted when truly done) */
|
|
48
|
+
private emitToolResults;
|
|
140
49
|
run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent>;
|
|
141
|
-
private triggerNewRun;
|
|
142
|
-
private handlePendingCalls;
|
|
143
50
|
}
|
|
144
51
|
/**
|
|
145
52
|
* Factory function to create MCP middleware.
|
|
146
|
-
*
|
|
147
|
-
* This is a convenience wrapper around McpMiddleware that returns a function
|
|
148
|
-
* compatible with the AG-UI middleware pattern.
|
|
149
|
-
*
|
|
150
|
-
* @param client - MCP client or MultiSessionClient
|
|
151
|
-
* @param options - Configuration options
|
|
152
|
-
* @returns Middleware function
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
157
|
-
* import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';
|
|
158
|
-
*
|
|
159
|
-
* const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });
|
|
160
|
-
* agent.use(createMcpMiddleware(multiSessionClient, {
|
|
161
|
-
* toolPrefix: 'server-',
|
|
162
|
-
* actions: mcpActions,
|
|
163
|
-
* }));
|
|
164
|
-
* ```
|
|
165
53
|
*/
|
|
166
|
-
declare function createMcpMiddleware(
|
|
167
|
-
|
|
168
|
-
|
|
54
|
+
declare function createMcpMiddleware(options: {
|
|
55
|
+
tools: AguiTool[];
|
|
56
|
+
maxResultLength?: number;
|
|
169
57
|
}): (input: RunAgentInput, next: AbstractAgent) => Observable<BaseEvent>;
|
|
170
58
|
|
|
171
|
-
export {
|
|
59
|
+
export { McpMiddleware, type McpMiddlewareConfig, McpMiddleware as McpToolExecutorMiddleware, createMcpMiddleware, createMcpMiddleware as createMcpToolMiddleware };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { Middleware, RunAgentInput, AbstractAgent, BaseEvent } from '@ag-ui/client';
|
|
3
|
-
export { AbstractAgent, BaseEvent, EventType, Middleware, RunAgentInput, ToolCallEndEvent } from '@ag-ui/client';
|
|
4
|
-
import { M as MCPClient, a as MultiSessionClient } from '../multi-session-client-BOFgPypS.js';
|
|
3
|
+
export { AbstractAgent, BaseEvent, EventType, Middleware, RunAgentInput, Tool, ToolCallEndEvent } from '@ag-ui/client';
|
|
5
4
|
import { AguiTool } from './agui-adapter.js';
|
|
5
|
+
import '../multi-session-client-BOFgPypS.js';
|
|
6
6
|
import '../events-BP6WyRNh.js';
|
|
7
7
|
import '@modelcontextprotocol/sdk/types.js';
|
|
8
8
|
import '@modelcontextprotocol/sdk/shared/auth.js';
|
|
@@ -11,161 +11,49 @@ import '@modelcontextprotocol/sdk/client/auth.js';
|
|
|
11
11
|
/**
|
|
12
12
|
* AG-UI Middleware for MCP Tool Execution
|
|
13
13
|
*
|
|
14
|
-
* This middleware intercepts tool calls from remote agents
|
|
15
|
-
*
|
|
14
|
+
* This middleware intercepts tool calls from remote agents and executes
|
|
15
|
+
* MCP tools server-side, returning results back to the agent.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* 1. **Tool Injection**: When a run starts, the middleware injects MCP tool definitions
|
|
20
|
-
* into `input.tools` so the remote agent knows about available MCP tools.
|
|
21
|
-
*
|
|
22
|
-
* 2. **Event Interception**: The middleware subscribes to the agent's event stream and
|
|
23
|
-
* tracks tool calls using AG-UI events:
|
|
24
|
-
* - `TOOL_CALL_START`: Records tool name and ID
|
|
25
|
-
* - `TOOL_CALL_ARGS`: Accumulates streamed arguments
|
|
26
|
-
* - `TOOL_CALL_END`: Marks tool call as complete
|
|
27
|
-
* - `RUN_FINISHED`: Triggers execution of pending MCP tools
|
|
28
|
-
*
|
|
29
|
-
* 3. **Server-Side Execution**: When `RUN_FINISHED` arrives with pending MCP tool calls,
|
|
30
|
-
* the middleware:
|
|
31
|
-
* - Executes each MCP tool via the MCP client
|
|
32
|
-
* - Emits `TOOL_CALL_RESULT` events with the results
|
|
33
|
-
* - Adds results to `input.messages` for context
|
|
34
|
-
* - Emits `RUN_FINISHED` to close the current run
|
|
35
|
-
* - Triggers a new run so the agent can process tool results
|
|
36
|
-
*
|
|
37
|
-
* 4. **Recursive Processing**: If the new run makes more MCP tool calls, the cycle
|
|
38
|
-
* repeats until the agent completes without pending MCP calls.
|
|
39
|
-
*
|
|
40
|
-
* ## Tool Identification
|
|
41
|
-
*
|
|
42
|
-
* MCP tools are identified by a configurable prefix (default: `server-`).
|
|
43
|
-
* Tools not matching this prefix are passed through without interception.
|
|
44
|
-
*
|
|
45
|
-
* @requires @ag-ui/client - This middleware requires @ag-ui/client as a peer dependency
|
|
17
|
+
* @requires @ag-ui/client - Peer dependency for AG-UI types
|
|
46
18
|
* @requires rxjs - Uses RxJS Observables for event streaming
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
51
|
-
* import { McpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';
|
|
52
|
-
* import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';
|
|
53
|
-
*
|
|
54
|
-
* // Create MCP client and adapter
|
|
55
|
-
* const mcpClient = new MultiSessionClient('user_123');
|
|
56
|
-
* await mcpClient.connect();
|
|
57
|
-
*
|
|
58
|
-
* const adapter = new AguiAdapter(mcpClient);
|
|
59
|
-
* const actions = await adapter.getActions();
|
|
60
|
-
*
|
|
61
|
-
* // Create middleware with pre-loaded actions
|
|
62
|
-
* const middleware = new McpMiddleware({
|
|
63
|
-
* client: mcpClient,
|
|
64
|
-
* actions,
|
|
65
|
-
* toolPrefix: 'server-',
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* // Use with HttpAgent
|
|
69
|
-
* const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });
|
|
70
|
-
* agent.use(middleware);
|
|
71
|
-
* ```
|
|
72
19
|
*/
|
|
73
20
|
|
|
74
|
-
/**
|
|
75
|
-
* Tool definition format for AG-UI input.tools
|
|
76
|
-
*/
|
|
77
|
-
interface AgUiTool {
|
|
78
|
-
name: string;
|
|
79
|
-
description: string;
|
|
80
|
-
parameters?: Record<string, any>;
|
|
81
|
-
}
|
|
82
21
|
/**
|
|
83
22
|
* Configuration for McpMiddleware
|
|
84
23
|
*/
|
|
85
24
|
interface McpMiddlewareConfig {
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Prefix used to identify MCP tool names.
|
|
92
|
-
* Tools starting with this prefix will be executed server-side.
|
|
93
|
-
* @default 'server-'
|
|
94
|
-
*/
|
|
95
|
-
toolPrefix?: string;
|
|
96
|
-
/**
|
|
97
|
-
* Pre-loaded tools with handlers for execution.
|
|
98
|
-
* If not provided, tools will be loaded from the MCP client on first use.
|
|
99
|
-
*/
|
|
100
|
-
tools?: AguiTool[];
|
|
25
|
+
/** Pre-loaded tools with handlers (required) */
|
|
26
|
+
tools: AguiTool[];
|
|
27
|
+
/** Max result length in chars (default: 50000) */
|
|
28
|
+
maxResultLength?: number;
|
|
101
29
|
}
|
|
102
30
|
/**
|
|
103
31
|
* AG-UI Middleware that executes MCP tools server-side.
|
|
104
|
-
*
|
|
105
|
-
* This middleware intercepts tool calls for MCP tools (identified by prefix),
|
|
106
|
-
* executes them via the MCP client, and returns results to the agent.
|
|
107
|
-
*
|
|
108
|
-
* @see {@link createMcpMiddleware} for a simpler factory function
|
|
109
32
|
*/
|
|
110
33
|
declare class McpMiddleware extends Middleware {
|
|
111
|
-
private client;
|
|
112
|
-
private toolPrefix;
|
|
113
|
-
private actions;
|
|
114
34
|
private tools;
|
|
115
|
-
private
|
|
35
|
+
private toolSchemas;
|
|
36
|
+
private maxResultLength;
|
|
116
37
|
constructor(config: McpMiddlewareConfig);
|
|
117
|
-
/**
|
|
118
|
-
* Convert actions to AG-UI tool format
|
|
119
|
-
*/
|
|
120
|
-
private actionsToTools;
|
|
121
|
-
/**
|
|
122
|
-
* Check if a tool name is an MCP tool (matches the configured prefix)
|
|
123
|
-
*/
|
|
124
38
|
private isMcpTool;
|
|
125
|
-
|
|
126
|
-
* Load actions from the MCP client if not already loaded
|
|
127
|
-
*/
|
|
128
|
-
private ensureActionsLoaded;
|
|
129
|
-
/**
|
|
130
|
-
* Execute an MCP tool and return the result as a string
|
|
131
|
-
*/
|
|
39
|
+
private parseArgs;
|
|
132
40
|
private executeTool;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
private
|
|
137
|
-
/**
|
|
138
|
-
|
|
139
|
-
|
|
41
|
+
private generateId;
|
|
42
|
+
private ensureIds;
|
|
43
|
+
/** Process tool call events and update state */
|
|
44
|
+
private handleToolCallEvent;
|
|
45
|
+
/** Execute pending MCP tools and return results */
|
|
46
|
+
private executeTools;
|
|
47
|
+
/** Emit tool results (without RUN_FINISHED - that's emitted when truly done) */
|
|
48
|
+
private emitToolResults;
|
|
140
49
|
run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent>;
|
|
141
|
-
private triggerNewRun;
|
|
142
|
-
private handlePendingCalls;
|
|
143
50
|
}
|
|
144
51
|
/**
|
|
145
52
|
* Factory function to create MCP middleware.
|
|
146
|
-
*
|
|
147
|
-
* This is a convenience wrapper around McpMiddleware that returns a function
|
|
148
|
-
* compatible with the AG-UI middleware pattern.
|
|
149
|
-
*
|
|
150
|
-
* @param client - MCP client or MultiSessionClient
|
|
151
|
-
* @param options - Configuration options
|
|
152
|
-
* @returns Middleware function
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* import { HttpAgent } from '@ag-ui/client';
|
|
157
|
-
* import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';
|
|
158
|
-
*
|
|
159
|
-
* const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });
|
|
160
|
-
* agent.use(createMcpMiddleware(multiSessionClient, {
|
|
161
|
-
* toolPrefix: 'server-',
|
|
162
|
-
* actions: mcpActions,
|
|
163
|
-
* }));
|
|
164
|
-
* ```
|
|
165
53
|
*/
|
|
166
|
-
declare function createMcpMiddleware(
|
|
167
|
-
|
|
168
|
-
|
|
54
|
+
declare function createMcpMiddleware(options: {
|
|
55
|
+
tools: AguiTool[];
|
|
56
|
+
maxResultLength?: number;
|
|
169
57
|
}): (input: RunAgentInput, next: AbstractAgent) => Observable<BaseEvent>;
|
|
170
58
|
|
|
171
|
-
export {
|
|
59
|
+
export { McpMiddleware, type McpMiddlewareConfig, McpMiddleware as McpToolExecutorMiddleware, createMcpMiddleware, createMcpMiddleware as createMcpToolMiddleware };
|