@mcp-ts/sdk 1.0.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.
Files changed (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +297 -0
  3. package/dist/adapters/agui-adapter.d.mts +119 -0
  4. package/dist/adapters/agui-adapter.d.ts +119 -0
  5. package/dist/adapters/agui-adapter.js +109 -0
  6. package/dist/adapters/agui-adapter.js.map +1 -0
  7. package/dist/adapters/agui-adapter.mjs +107 -0
  8. package/dist/adapters/agui-adapter.mjs.map +1 -0
  9. package/dist/adapters/agui-middleware.d.mts +171 -0
  10. package/dist/adapters/agui-middleware.d.ts +171 -0
  11. package/dist/adapters/agui-middleware.js +429 -0
  12. package/dist/adapters/agui-middleware.js.map +1 -0
  13. package/dist/adapters/agui-middleware.mjs +417 -0
  14. package/dist/adapters/agui-middleware.mjs.map +1 -0
  15. package/dist/adapters/ai-adapter.d.mts +38 -0
  16. package/dist/adapters/ai-adapter.d.ts +38 -0
  17. package/dist/adapters/ai-adapter.js +82 -0
  18. package/dist/adapters/ai-adapter.js.map +1 -0
  19. package/dist/adapters/ai-adapter.mjs +80 -0
  20. package/dist/adapters/ai-adapter.mjs.map +1 -0
  21. package/dist/adapters/langchain-adapter.d.mts +46 -0
  22. package/dist/adapters/langchain-adapter.d.ts +46 -0
  23. package/dist/adapters/langchain-adapter.js +102 -0
  24. package/dist/adapters/langchain-adapter.js.map +1 -0
  25. package/dist/adapters/langchain-adapter.mjs +100 -0
  26. package/dist/adapters/langchain-adapter.mjs.map +1 -0
  27. package/dist/adapters/mastra-adapter.d.mts +49 -0
  28. package/dist/adapters/mastra-adapter.d.ts +49 -0
  29. package/dist/adapters/mastra-adapter.js +95 -0
  30. package/dist/adapters/mastra-adapter.js.map +1 -0
  31. package/dist/adapters/mastra-adapter.mjs +93 -0
  32. package/dist/adapters/mastra-adapter.mjs.map +1 -0
  33. package/dist/client/index.d.mts +119 -0
  34. package/dist/client/index.d.ts +119 -0
  35. package/dist/client/index.js +225 -0
  36. package/dist/client/index.js.map +1 -0
  37. package/dist/client/index.mjs +223 -0
  38. package/dist/client/index.mjs.map +1 -0
  39. package/dist/client/react.d.mts +151 -0
  40. package/dist/client/react.d.ts +151 -0
  41. package/dist/client/react.js +492 -0
  42. package/dist/client/react.js.map +1 -0
  43. package/dist/client/react.mjs +489 -0
  44. package/dist/client/react.mjs.map +1 -0
  45. package/dist/client/vue.d.mts +157 -0
  46. package/dist/client/vue.d.ts +157 -0
  47. package/dist/client/vue.js +474 -0
  48. package/dist/client/vue.js.map +1 -0
  49. package/dist/client/vue.mjs +471 -0
  50. package/dist/client/vue.mjs.map +1 -0
  51. package/dist/events-BP6WyRNh.d.mts +110 -0
  52. package/dist/events-BP6WyRNh.d.ts +110 -0
  53. package/dist/index.d.mts +10 -0
  54. package/dist/index.d.ts +10 -0
  55. package/dist/index.js +2784 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/index.mjs +2723 -0
  58. package/dist/index.mjs.map +1 -0
  59. package/dist/multi-session-client-BOFgPypS.d.ts +389 -0
  60. package/dist/multi-session-client-DMF3ED2O.d.mts +389 -0
  61. package/dist/server/index.d.mts +269 -0
  62. package/dist/server/index.d.ts +269 -0
  63. package/dist/server/index.js +2444 -0
  64. package/dist/server/index.js.map +1 -0
  65. package/dist/server/index.mjs +2414 -0
  66. package/dist/server/index.mjs.map +1 -0
  67. package/dist/shared/index.d.mts +24 -0
  68. package/dist/shared/index.d.ts +24 -0
  69. package/dist/shared/index.js +223 -0
  70. package/dist/shared/index.js.map +1 -0
  71. package/dist/shared/index.mjs +190 -0
  72. package/dist/shared/index.mjs.map +1 -0
  73. package/dist/types-SbDlA2VX.d.mts +153 -0
  74. package/dist/types-SbDlA2VX.d.ts +153 -0
  75. package/dist/utils-0qmYrqoa.d.mts +92 -0
  76. package/dist/utils-0qmYrqoa.d.ts +92 -0
  77. package/package.json +165 -0
  78. package/src/adapters/agui-adapter.ts +210 -0
  79. package/src/adapters/agui-middleware.ts +512 -0
  80. package/src/adapters/ai-adapter.ts +115 -0
  81. package/src/adapters/langchain-adapter.ts +127 -0
  82. package/src/adapters/mastra-adapter.ts +126 -0
  83. package/src/client/core/sse-client.ts +340 -0
  84. package/src/client/index.ts +26 -0
  85. package/src/client/react/index.ts +10 -0
  86. package/src/client/react/useMcp.ts +558 -0
  87. package/src/client/vue/index.ts +10 -0
  88. package/src/client/vue/useMcp.ts +542 -0
  89. package/src/index.ts +11 -0
  90. package/src/server/handlers/nextjs-handler.ts +216 -0
  91. package/src/server/handlers/sse-handler.ts +699 -0
  92. package/src/server/index.ts +57 -0
  93. package/src/server/mcp/multi-session-client.ts +132 -0
  94. package/src/server/mcp/oauth-client.ts +1168 -0
  95. package/src/server/mcp/storage-oauth-provider.ts +239 -0
  96. package/src/server/storage/file-backend.ts +169 -0
  97. package/src/server/storage/index.ts +115 -0
  98. package/src/server/storage/memory-backend.ts +132 -0
  99. package/src/server/storage/redis-backend.ts +210 -0
  100. package/src/server/storage/redis.ts +160 -0
  101. package/src/server/storage/types.ts +109 -0
  102. package/src/shared/constants.ts +29 -0
  103. package/src/shared/errors.ts +133 -0
  104. package/src/shared/events.ts +166 -0
  105. package/src/shared/index.ts +70 -0
  106. package/src/shared/types.ts +274 -0
  107. package/src/shared/utils.ts +16 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/agui-adapter.ts"],"names":["result"],"mappings":";;;AAkFO,IAAM,cAAN,MAAkB;AAAA,EACrB,WAAA,CACY,MAAA,EACA,OAAA,GAA8B,EAAC,EACzC;AAFU,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYJ,MAAM,QAAA,GAAgC;AAClC,IAAA,MAAM,cAAA,GAAiB,OAAQ,IAAA,CAAK,MAAA,CAAe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AAA,CAAK,wBAAA,CAAyB,IAAA,CAAK,MAAmB,CAAA;AAAA,EACjE;AAAA,EAEA,MAAc,yBAAyB,MAAA,EAAkD;AACrF,IAAA,IAAI,CAAC,MAAA,CAAO,WAAA,EAAY,EAAG;AACvB,MAAA,OAAO,EAAC;AAAA,IACZ;AAEA,IAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,SAAA,EAAU;AACtC,IAAA,MAAM,SAAS,IAAA,CAAK,OAAA,CAAQ,MAAA,IAAU,MAAA,CAAO,aAAY,IAAK,KAAA;AAC9D,IAAA,MAAM,QAA8B,EAAC;AAErC,IAAA,KAAA,MAAW,IAAA,IAAQ,OAAO,KAAA,EAAO;AAC7B,MAAA,KAAA,CAAM,IAAA,CAAK;AAAA,QACP,IAAA,EAAM,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,KAAK,IAAI,CAAA,CAAA;AAAA,QAC5B,WAAA,EAAa,IAAA,CAAK,WAAA,IAAe,CAAA,QAAA,EAAW,KAAK,IAAI,CAAA,CAAA;AAAA,QACrD,UAAA,EAAY,KAAK,WAAA,IAAe,EAAE,MAAM,QAAA,EAAU,UAAA,EAAY,EAAC;AAAE,OACpE,CAAA;AAAA,IACL;AAEA,IAAA,OAAO,KAAA;AAAA,EACX;AACJ","file":"agui-adapter.js","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"]}
@@ -0,0 +1,107 @@
1
+ // src/adapters/agui-adapter.ts
2
+ var AguiAdapter = class {
3
+ constructor(client, options = {}) {
4
+ this.client = client;
5
+ this.options = options;
6
+ }
7
+ /**
8
+ * Get tools with handlers for MCP tool execution.
9
+ *
10
+ * Each tool includes a handler function that:
11
+ * 1. Calls the MCP tool via the client
12
+ * 2. Extracts text content from the result
13
+ * 3. Returns the result as a string or JSON
14
+ *
15
+ * @returns Array of AguiTool objects
16
+ */
17
+ async getTools() {
18
+ const isMultiSession = typeof this.client.getClients === "function";
19
+ if (isMultiSession) {
20
+ const clients = this.client.getClients();
21
+ const allTools = [];
22
+ for (const client of clients) {
23
+ const tools = await this.transformTools(client);
24
+ allTools.push(...tools);
25
+ }
26
+ return allTools;
27
+ }
28
+ return this.transformTools(this.client);
29
+ }
30
+ async transformTools(client) {
31
+ if (!client.isConnected()) {
32
+ return [];
33
+ }
34
+ const result = await client.listTools();
35
+ const prefix = this.options.prefix ?? client.getServerId() ?? "mcp";
36
+ const tools = [];
37
+ for (const tool of result.tools) {
38
+ const toolName = `${prefix}_${tool.name}`;
39
+ tools.push({
40
+ name: toolName,
41
+ description: tool.description || `Execute ${tool.name}`,
42
+ parameters: tool.inputSchema || { type: "object", properties: {} },
43
+ handler: async (args) => {
44
+ console.log(`[AguiAdapter] Executing MCP tool: ${tool.name}`, args);
45
+ const result2 = await client.callTool(tool.name, args);
46
+ if (result2.content && Array.isArray(result2.content)) {
47
+ const textContent = result2.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
48
+ return textContent || result2;
49
+ }
50
+ return result2;
51
+ }
52
+ });
53
+ }
54
+ return tools;
55
+ }
56
+ /**
57
+ * Get tools as a function (for dynamic loading).
58
+ *
59
+ * @returns Function that returns a Promise of tools
60
+ */
61
+ getToolsFunction() {
62
+ return async () => this.getTools();
63
+ }
64
+ /**
65
+ * Get tool definitions in JSON Schema format for passing to remote agents.
66
+ *
67
+ * This format is compatible with:
68
+ * - OpenAI's function calling API
69
+ * - AG-UI input.tools format
70
+ * - Most LLM tool/function calling implementations
71
+ *
72
+ * @returns Array of AguiToolDefinition objects
73
+ */
74
+ async getToolDefinitions() {
75
+ const isMultiSession = typeof this.client.getClients === "function";
76
+ if (isMultiSession) {
77
+ const clients = this.client.getClients();
78
+ const allTools = [];
79
+ for (const client of clients) {
80
+ const tools = await this.transformToolDefinitions(client);
81
+ allTools.push(...tools);
82
+ }
83
+ return allTools;
84
+ }
85
+ return this.transformToolDefinitions(this.client);
86
+ }
87
+ async transformToolDefinitions(client) {
88
+ if (!client.isConnected()) {
89
+ return [];
90
+ }
91
+ const result = await client.listTools();
92
+ const prefix = this.options.prefix ?? client.getServerId() ?? "mcp";
93
+ const tools = [];
94
+ for (const tool of result.tools) {
95
+ tools.push({
96
+ name: `${prefix}_${tool.name}`,
97
+ description: tool.description || `Execute ${tool.name}`,
98
+ parameters: tool.inputSchema || { type: "object", properties: {} }
99
+ });
100
+ }
101
+ return tools;
102
+ }
103
+ };
104
+
105
+ export { AguiAdapter };
106
+ //# sourceMappingURL=agui-adapter.mjs.map
107
+ //# sourceMappingURL=agui-adapter.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/adapters/agui-adapter.ts"],"names":["result"],"mappings":";AAkFO,IAAM,cAAN,MAAkB;AAAA,EACrB,WAAA,CACY,MAAA,EACA,OAAA,GAA8B,EAAC,EACzC;AAFU,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYJ,MAAM,QAAA,GAAgC;AAClC,IAAA,MAAM,cAAA,GAAiB,OAAQ,IAAA,CAAK,MAAA,CAAe,UAAA,KAAe,UAAA;AAElE,IAAA,IAAI,cAAA,EAAgB;AAChB,MAAA,MAAM,OAAA,GAAW,IAAA,CAAK,MAAA,CAA8B,UAAA,EAAW;AAC/D,MAAA,MAAM,WAAuB,EAAC;AAE9B,MAAA,KAAA,MAAW,UAAU,OAAA,EAAS;AAC1B,QAAA,MAAM,KAAA,GAAQ,MAAM,IAAA,CAAK,cAAA,CAAe,MAAM,CAAA;AAC9C,QAAA,QAAA,CAAS,IAAA,CAAK,GAAG,KAAK,CAAA;AAAA,MAC1B;AAEA,MAAA,OAAO,QAAA;AAAA,IACX;AAEA,IAAA,OAAO,IAAA,CAAK,cAAA,CAAe,IAAA,CAAK,MAAmB,CAAA;AAAA,EACvD;AAAA,EAEA,MAAc,eAAe,MAAA,EAAwC;AACjE,IAAA,IAAI,CAAC,MAAA,CAAO,WAAA,EAAY,EAAG;AACvB,MAAA,OAAO,EAAC;AAAA,IACZ;AAEA,IAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO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IAAI,CAAA,CAAA;AAAA,QACrD,UAAA,EAAY,KAAK,WAAA,IAAe,EAAE,MAAM,QAAA,EAAU,UAAA,EAAY,EAAC;AAAE,OACpE,CAAA;AAAA,IACL;AAEA,IAAA,OAAO,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"]}
@@ -0,0 +1,171 @@
1
+ import { Observable } from 'rxjs';
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';
5
+ import { AguiTool } from './agui-adapter.mjs';
6
+ import '../events-BP6WyRNh.mjs';
7
+ import '@modelcontextprotocol/sdk/types.js';
8
+ import '@modelcontextprotocol/sdk/shared/auth.js';
9
+ import '@modelcontextprotocol/sdk/client/auth.js';
10
+
11
+ /**
12
+ * AG-UI Middleware for MCP Tool Execution
13
+ *
14
+ * This middleware intercepts tool calls from remote agents (e.g., LangGraph, AutoGen)
15
+ * and executes MCP tools server-side, returning results back to the agent.
16
+ *
17
+ * ## How It Works
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
46
+ * @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
+ */
73
+
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
+ /**
83
+ * Configuration for McpMiddleware
84
+ */
85
+ interface McpMiddlewareConfig {
86
+ /**
87
+ * MCP client or MultiSessionClient for executing tools
88
+ */
89
+ client: MCPClient | MultiSessionClient;
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[];
101
+ }
102
+ /**
103
+ * 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
+ */
110
+ declare class McpMiddleware extends Middleware {
111
+ private client;
112
+ private toolPrefix;
113
+ private actions;
114
+ private tools;
115
+ private actionsLoaded;
116
+ 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
+ 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
+ */
132
+ private executeTool;
133
+ /**
134
+ * Generate a unique message ID for tool results
135
+ */
136
+ private generateMessageId;
137
+ /**
138
+ * Run the middleware, intercepting and executing MCP tool calls
139
+ */
140
+ run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent>;
141
+ private triggerNewRun;
142
+ private handlePendingCalls;
143
+ }
144
+ /**
145
+ * 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
+ */
166
+ declare function createMcpMiddleware(client: MCPClient | MultiSessionClient, options?: {
167
+ toolPrefix?: string;
168
+ tools?: AguiTool[];
169
+ }): (input: RunAgentInput, next: AbstractAgent) => Observable<BaseEvent>;
170
+
171
+ export { type AgUiTool, McpMiddleware, type McpMiddlewareConfig, McpMiddleware as McpToolExecutorMiddleware, createMcpMiddleware, createMcpMiddleware as createMcpToolMiddleware };
@@ -0,0 +1,171 @@
1
+ import { Observable } from 'rxjs';
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';
5
+ import { AguiTool } from './agui-adapter.js';
6
+ import '../events-BP6WyRNh.js';
7
+ import '@modelcontextprotocol/sdk/types.js';
8
+ import '@modelcontextprotocol/sdk/shared/auth.js';
9
+ import '@modelcontextprotocol/sdk/client/auth.js';
10
+
11
+ /**
12
+ * AG-UI Middleware for MCP Tool Execution
13
+ *
14
+ * This middleware intercepts tool calls from remote agents (e.g., LangGraph, AutoGen)
15
+ * and executes MCP tools server-side, returning results back to the agent.
16
+ *
17
+ * ## How It Works
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
46
+ * @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
+ */
73
+
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
+ /**
83
+ * Configuration for McpMiddleware
84
+ */
85
+ interface McpMiddlewareConfig {
86
+ /**
87
+ * MCP client or MultiSessionClient for executing tools
88
+ */
89
+ client: MCPClient | MultiSessionClient;
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[];
101
+ }
102
+ /**
103
+ * 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
+ */
110
+ declare class McpMiddleware extends Middleware {
111
+ private client;
112
+ private toolPrefix;
113
+ private actions;
114
+ private tools;
115
+ private actionsLoaded;
116
+ 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
+ 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
+ */
132
+ private executeTool;
133
+ /**
134
+ * Generate a unique message ID for tool results
135
+ */
136
+ private generateMessageId;
137
+ /**
138
+ * Run the middleware, intercepting and executing MCP tool calls
139
+ */
140
+ run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent>;
141
+ private triggerNewRun;
142
+ private handlePendingCalls;
143
+ }
144
+ /**
145
+ * 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
+ */
166
+ declare function createMcpMiddleware(client: MCPClient | MultiSessionClient, options?: {
167
+ toolPrefix?: string;
168
+ tools?: AguiTool[];
169
+ }): (input: RunAgentInput, next: AbstractAgent) => Observable<BaseEvent>;
170
+
171
+ export { type AgUiTool, McpMiddleware, type McpMiddlewareConfig, McpMiddleware as McpToolExecutorMiddleware, createMcpMiddleware, createMcpMiddleware as createMcpToolMiddleware };