@mcp-ts/sdk 1.5.2 β†’ 1.5.3

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 CHANGED
@@ -1,32 +1,67 @@
1
- <div align="center">
2
- <img src="docs/images/mcp-ts-banner.svg" alt="MCP-TS Banner" width="100%" style="max-width: 1200px;" />
3
- </div>
1
+ <p align="center">
2
+ <a href="https://github.com/zonlabs/mcp-ts">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="docs/images/logo-dark.png">
5
+ <img src="docs/images/logo-light.png" alt="mcp toolkit" width="400">
6
+ </picture>
7
+ </a>
8
+ </p>
4
9
 
5
10
  <div align="center">
6
- <a href="https://zonlabs.github.io/mcp-ts/#ag-ui-demo">
7
- <em>Watch AG-UI + LangChain demo</em>
8
- </a>
11
+ <p>Every resource is context for your AI</p>
12
+
13
+ <p>
14
+ <a href="https://mcp-assistant.in/">🌐 Website</a>
15
+ &nbsp;&nbsp;|&nbsp;&nbsp;
16
+ <a href="https://docs.mcp-assistant.in/">πŸ“š Documentation</a>
17
+ </p>
18
+
19
+ <p>
20
+ <a href="https://www.npmjs.com/package/@mcp-ts/sdk">
21
+ <img src="https://img.shields.io/npm/v/@mcp-ts/sdk?color=dc2626&label=npm&logo=npm&style=flat-square" alt="npm version" />
22
+ </a>
23
+ <a href="https://pypi.org/project/mcpassistant-gateway/">
24
+ <img src="https://img.shields.io/pypi/v/mcpassistant-gateway?color=3776ab&label=pypi&logo=pypi&style=flat-square" alt="pypi version" />
25
+ </a>
26
+ <a href="https://opensource.org/licenses/MIT">
27
+ <img src="https://img.shields.io/badge/license-MIT-84cc16?style=flat-square" alt="License: MIT" />
28
+ </a>
29
+ </p>
9
30
  </div>
31
+
10
32
  <br />
11
33
 
34
+ ## πŸ“– Table of Contents
12
35
 
36
+ - [✨ Features](#-features)
37
+ - [πŸ“¦ Packages](#-packages)
38
+ - [πŸ› οΈ SDK Setup (@mcp-ts/sdk)](#️-sdk-setup-mcp-tssdk)
39
+ - [πŸ“¦ Installation](#-installation)
40
+ - [πŸš€ Quick Start](#-quick-start)
41
+ - [🐍 Gateway Setup (mcpassistant-gateway)](#-gateway-setup-mcpassistant-gateway)
42
+ - [πŸ“¦ Installation](#-installation-1)
43
+ - [πŸš€ Usage](#-usage)
44
+ - [πŸ—οΈ Architecture](#️-architecture)
45
+ - [πŸ“š Documentation](#-documentation)
46
+ - [βš™οΈ Environment Setup](#️-environment-setup)
47
+ - [πŸ§ͺ Examples](#-examples)
48
+ - [πŸ’‘ Inspiration](#-inspiration)
13
49
 
14
- <p align="center">
15
- <a href="https://www.npmjs.com/package/@mcp-ts/sdk">
16
- <img src="https://img.shields.io/npm/v/@mcp-ts/sdk?style=flat-square&logo=npm&logoColor=white&label=%40mcp-ts%2Fsdk&color=dc2626" alt="npm version" />
17
- </a>
18
- <a href="https://docs.mcp-assistant.in/">
19
- <img src="https://img.shields.io/badge/docs-website-2563eb?style=flat-square&logo=readthedocs&logoColor=white" alt="Documentation" />
20
- </a>
21
- <a href="https://opensource.org/licenses/MIT">
22
- <img src="https://img.shields.io/badge/license-MIT-84cc16?style=flat-square" alt="License: MIT" />
23
- </a>
24
- </p>
50
+ ---
25
51
 
52
+ ## πŸ“¦ Packages
26
53
 
54
+ | Package | Description | Install |
55
+ | :--- | :--- | :--- |
56
+ | **[@mcp-ts/sdk](src)** | TypeScript/JavaScript SDK for clients & servers. | `npm i @mcp-ts/sdk` |
57
+ | **[mcpassistant-gateway](packages/mcp-local-agent)** | Python bridge for local MCP support in remote apps. | `pip install mcpassistant-gateway` |
58
+
59
+ ---
27
60
 
28
61
  ## ✨ Features
29
62
 
63
+ Most features are available out-of-the-box in the **TypeScript SDK**:
64
+
30
65
  - **SSE** - Server-Sent Events for connection state and observability updates
31
66
  - **Flexible Storage** - Redis, SQLite, File System, or In-Memory backends
32
67
  - **Serverless** - Works in serverless environments (Vercel, AWS Lambda, etc.)
@@ -71,21 +106,23 @@ That’s how `@mcp-ts` started.
71
106
 
72
107
  <br/>
73
108
 
74
- ## πŸ“¦ Installation
109
+ ## πŸ› οΈ SDK Setup (@mcp-ts/sdk)
110
+
111
+ ### πŸ“¦ Installation
75
112
 
76
113
  ```bash
77
114
  npm install @mcp-ts/sdk
78
115
  ```
79
116
 
80
- The package supports multiple storage backends out of the box:
117
+ The SDK supports multiple storage backends out of the box:
81
118
  - **Memory** (default, no setup required)
82
119
  - **File** (local persistence)
83
120
  - **SQLite** (fast local persistence, requires `npm install better-sqlite3`)
84
121
  - **Redis** (production-ready, requires `npm install ioredis`)
85
122
 
86
- ## πŸš€ Quick Start
123
+ ### πŸš€ Quick Start
87
124
 
88
- ### πŸ–₯️ Server-Side (Next.js)
125
+ #### πŸ–₯️ Server-Side (Next.js)
89
126
 
90
127
  ```typescript
91
128
  // app/api/mcp/route.ts
@@ -322,14 +359,39 @@ The library supports multiple storage backends. You can explicitly select one us
322
359
  MCP_TS_STORAGE_TYPE=memory
323
360
  ```
324
361
 
325
- ## πŸ—οΈ Architecture
362
+ ---
363
+
364
+ ## 🐍 Gateway Setup (mcpassistant-gateway)
365
+
366
+ The **MCP Gateway** is a Python-based bridge that allows local MCP servers to be accessed by remote applications via an outbound connection. This is useful for providing local context (like your filesystem) to a hosted AI agent.
367
+
368
+ ### πŸ“¦ Installation
369
+
370
+ ```bash
371
+ pip install mcpassistant-gateway
372
+ ```
373
+
374
+ ### πŸš€ Usage
326
375
 
327
- `@mcp-ts/sdk` supports two common runtime topologies: direct SSE from browser clients, and outbound bridge connectivity for local agents.
376
+ You can run the gateway using `uvx` or `pip`:
377
+
378
+ ```bash
379
+ # Run the interactive menu
380
+ uvx mcpassistant-gateway menu
381
+
382
+ # Run the bridge directly
383
+ uvx mcpassistant-gateway run --name "local-files"
384
+ ```
385
+
386
+ ---
387
+
388
+ ## πŸ—οΈ Architecture
328
389
 
390
+ The MCP Toolkit supports two common runtime topologies:
329
391
 
330
392
  ```mermaid
331
393
  graph LR
332
- subgraph Direct["Direct SDK Flow (SSE)"]
394
+ subgraph Direct["Direct SDK Flow (TypeScript)"]
333
395
  UI[Browser UI]
334
396
  Hook[useMcp Hook]
335
397
  API[Next.js /api/mcp]
@@ -345,10 +407,10 @@ graph LR
345
407
  Mgr <--> MCP
346
408
  end
347
409
 
348
- subgraph Bridge["Remote Bridge Flow (mcp-local-agent)"]
410
+ subgraph Bridge["Remote Bridge Flow (Python)"]
349
411
  direction TB
350
412
  Spacer[" "]
351
- Agent[Local Agent Runtime]
413
+ Agent[mcpassistant-gateway]
352
414
  Remote[Remote Bridge Server]
353
415
  LocalMcp[Local MCP Servers]
354
416
 
@@ -360,7 +422,7 @@ graph LR
360
422
  ```
361
423
 
362
424
  - **Direct SDK flow**: Browser clients use `useMcp` over HTTP + SSE to a server route backed by `MultiSessionClient`.
363
- - **Bridge flow**: `mcp-local-agent` keeps an outbound authenticated WebSocket to a remote bridge and forwards tool calls to local MCP servers.
425
+ - **Bridge flow**: `mcpassistant-gateway` keeps an outbound authenticated WebSocket to a remote bridge and forwards tool calls to local MCP servers.
364
426
  - **Storage**: Session state and connection metadata persist in Redis, File, SQLite, or Memory backends.
365
427
 
366
428
  > [!NOTE]
@@ -99,7 +99,13 @@ async function executeMetaTool(toolName, args, router, callToolFn) {
99
99
  const schema = {
100
100
  name: tool.name,
101
101
  description: tool.description,
102
- inputSchema: tool.inputSchema
102
+ inputSchema: tool.inputSchema,
103
+ executionInstructions: {
104
+ nextTool: "mcp_execute_tool",
105
+ toolName: tool.name,
106
+ serverId: tool.serverId,
107
+ note: "Do not call this discovered tool directly unless it was explicitly registered as a runtime tool. Execute it via mcp_execute_tool and pass these parameters inside args."
108
+ }
103
109
  };
104
110
  return {
105
111
  content: [{ type: "text", text: JSON.stringify(schema, null, 2) }],
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/shared/meta-tools.ts","../../src/adapters/agui-adapter.ts"],"names":["text","namespace"],"mappings":";;;AAyLA,eAAsB,eAAA,CACpB,QAAA,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A,MAAc,4BAA4B,MAAA,EAAmD;AACzF,IAAA,MAAM,aAAA,GAAgB,MAAM,MAAA,CAAO,gBAAA,EAAiB;AACpD,IAAA,OAAO,aAAA,CAAc,IAAI,CAAA,IAAA,KAAQ;AAC7B,MAAA,MAAM,UAAA,GAAa,IAAA;AACnB,MAAA,OAAO;AAAA,QACH,IAAA,EAAM,UAAA,CAAW,IAAA,CAAK,IAAI,IACpB,IAAA,CAAK,IAAA,GACL,IAAA,CAAK,gBAAA,CAAiB,IAAA,CAAK,IAAA,EAAM,UAAA,CAAW,SAAA,EAAW,WAAW,QAAQ,CAAA;AAAA,QAChF,WAAA,EAAa,IAAA,CAAK,WAAA,IAAe,CAAA,QAAA,EAAW,KAAK,IAAI,CAAA,CAAA;AAAA,QACrD,UAAA,EAAY,WAAA,CAAY,IAAA,CAAK,WAAW;AAAA,OAC5C;AAAA,IACJ,CAAC,CAAA;AAAA,EACL;AAAA,EAEQ,gBAAA,CAAiB,QAAA,EAAkB,SAAA,EAAoB,QAAA,EAA2B;AACtF,IAAA,MAAM,SAAA,GAAY,aAAa,QAAA,IAAY,KAAA;AAC3C,IAAA,MAAM,UAAA,GAAa,SAAA,CACd,WAAA,EAAY,CACZ,OAAA,CAAQ,aAAA,EAAe,GAAG,CAAA,CAC1B,OAAA,CAAQ,UAAA,EAAY,EAAE,CAAA,IAAK,KAAA;AAChC,IAAA,OAAO,CAAA,KAAA,EAAQ,UAAU,CAAA,CAAA,EAAI,QAAQ,CAAA,CAAA;AAAA,EACzC;AACJ","file":"agui-adapter.js","sourcesContent":["/**\n * Meta-tools β€” Injectable tool definitions that let the LLM discover and\n * load MCP tools on-demand, following Anthropic's Tool Search pattern.\n *\n * Instead of injecting 50+ full tool schemas into the context window, you\n * inject just these 4 meta-tools. The LLM calls them to find and load\n * only the tools it actually needs.\n *\n * Meta-tools:\n * β€’ `mcp_search_tool_bm25` β€” BM25 natural language search\n * β€’ `mcp_search_tool_regex` β€” Regex pattern search\n * β€’ `mcp_get_tool_schema` β€” Get full inputSchema for a discovered tool\n * β€’ `mcp_execute_tool` β€” Execute a discovered tool\n *\n * @packageDocumentation\n */\n\nimport type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';\nimport type { ToolRouter } from './tool-router.js';\n\n// ---------------------------------------------------------------------------\n// Tool Definitions\n// ---------------------------------------------------------------------------\n\n/**\n * Creates the `mcp_search_tool_bm25` tool definition.\n *\n * This tool lets the LLM search the full catalog of available MCP tools\n * using a BM25 natural-language query. Returns tool names and descriptions\n * without the full inputSchema to save context space.\n */\nexport function createSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_bm25',\n description:\n 'Search the catalog of available tools. Returns tool names, descriptions, and server info. ' +\n 'Use this FIRST to find relevant tools before calling them.\\n\\n' +\n 'Query forms:\\n' +\n '- \"select:Read,Edit,Grep\" β€” fetch these exact tools by name\\n' +\n '- \"notebook jupyter\" β€” keyword search, up to limit best matches\\n' +\n '- \"+slack send\" β€” require \"slack\" in the name, rank by remaining terms',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Query to find tools. Use \"select:<tool_name>\" for direct selection, or keywords to search. Prefix keywords with + to require them.',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_search_tool_regex` tool definition.\n * \n * Matches Anthropic's tool_search_tool_regex exactly (takes a 'query' regex pattern).\n */\nexport function createRegexSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_regex',\n description:\n 'Search the catalog of available tools using a Python-style regex pattern. ' +\n 'Matches against tool names, descriptions, and parameter descriptions. ' +\n 'Example patterns: \"^github_\", \"weather\", \"(?i)slack\".',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Regex pattern to search for (e.g., \"^get_.*_data\", \"database\").',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_get_tool_schema` tool definition.\n *\n * After discovering tools via `mcp_search_tool_bm25` or\n * `mcp_search_tool_regex`, the LLM calls this to load the full\n * inputSchema for a specific tool so it can construct the correct\n * arguments.\n */\nexport function createGetSchemaToolDefinition(): Tool {\n return {\n name: 'mcp_get_tool_schema',\n description:\n 'Get the full input schema (parameters) for a specific tool. ' +\n 'Call this after mcp_search_tool_bm25 to get the parameter details ' +\n 'needed to call a tool correctly.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name returned by mcp_search_tool_bm25.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n/**\n * Creates the `mcp_execute_tool` tool definition.\n *\n * This is the execution meta-tool β€” the LLM calls this to execute any\n * tool discovered via `mcp_search_tool_bm25` or `mcp_search_tool_regex`.\n * The LLM should first call `mcp_get_tool_schema` to know the correct\n * arguments.\n *\n * Instead of registering every real tool with the framework, we proxy\n * all execution through a single meta-tool.\n */\nexport function createExecuteToolDefinition(): Tool {\n return {\n name: 'mcp_execute_tool',\n description:\n 'Execute a tool that was discovered via mcp_search_tool_bm25. ' +\n 'You MUST call mcp_get_tool_schema first to know the correct parameters. ' +\n 'Pass the exact tool name and its arguments.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name from mcp_search_tool_bm25 results.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n args: {\n type: 'object',\n description:\n \"Arguments matching the tool's inputSchema. Omit or pass {} if the tool takes no parameters.\",\n additionalProperties: true,\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n// ---------------------------------------------------------------------------\n// Meta-tool Executors\n// ---------------------------------------------------------------------------\n\n/**\n * Callback for executing a real MCP tool via the correct client.\n * Provided by adapters that wire up client routing.\n */\nexport type CallToolFn = (\n toolName: string,\n args: Record<string, unknown>,\n namespace?: string\n) => Promise<any>;\n\n/**\n * Execute a meta-tool call and return the result in MCP CallToolResult format.\n *\n * @param toolName - One of the meta-tool names (mcp_search_tool_bm25, mcp_search_tool_regex, etc.)\n * @param args - The arguments from the LLM's tool call\n * @param router - The ToolRouter to query\n * @param callToolFn - Optional callback for executing real tools (required for mcp_execute_tool)\n * @returns MCP-compatible CallToolResult, or null if this isn't a meta-tool\n */\nexport async function executeMetaTool(\n toolName: string,\n args: Record<string, unknown>,\n router: ToolRouter,\n callToolFn?: CallToolFn\n): Promise<CallToolResult | null> {\n const resolveToolSchema = (name: string, namespace?: string): { tool?: Tool; error?: CallToolResult } => {\n try {\n return { tool: router.getToolSchema(name, namespace) };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n error: {\n content: [{ type: 'text', text: errorMessage }],\n isError: true,\n },\n };\n }\n };\n\n switch (toolName) {\n case 'mcp_search_tool_bm25': {\n const query = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n // Fast path: Check for select: prefix\n const selectMatch = query.match(/^select:(.+)$/i);\n if (selectMatch) {\n const requested = selectMatch[1]!\n .split(',')\n .map((s) => s.trim())\n .filter(Boolean);\n\n const found: any[] = [];\n const errors: string[] = [];\n \n for (const requestedToolName of requested) {\n const { tool, error } = resolveToolSchema(requestedToolName);\n if (error) {\n const errorMsg = error.content[0]?.type === 'text' ? error.content[0].text : 'Unknown error';\n errors.push(`- **${requestedToolName}**: ${errorMsg}`);\n } else if (tool) {\n found.push(tool);\n } else {\n errors.push(`- **${requestedToolName}**: Tool not found. Try searching with mcp_search_tool_bm25.`);\n }\n }\n\n const lines: string[] = [];\n\n if (found.length > 0) {\n lines.push(...found.map((t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n ${t.description}`\n ));\n }\n \n if (errors.length > 0) {\n if (lines.length > 0) lines.push(\"\"); // Add empty line spacing\n lines.push(\"Errors resolving some tools:\");\n lines.push(...errors);\n }\n\n const text = lines.length > 0 \n ? lines.join('\\n') \n : `No tools found matching select query: ${requested.join(', ')}`;\n\n return {\n content: [{ type: 'text', text }],\n isError: found.length === 0,\n };\n }\n\n const results = await router.searchTools(query, limit);\n\n const text = results.length === 0\n ? 'No tools found matching your query. Try different keywords.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_search_tool_regex': {\n const pattern = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n const results = await router.searchToolsRegex(pattern, limit);\n\n const text = results.length === 0\n ? 'No tools matched your regex pattern. Try a broader pattern.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_get_tool_schema': {\n const name = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const { tool, error } = resolveToolSchema(name, namespace);\n\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${name}\" not found. Use mcp_search_tool_bm25 to find available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n const schema = {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n };\n\n return {\n content: [{ type: 'text', text: JSON.stringify(schema, null, 2) }],\n isError: false,\n };\n }\n\n case 'mcp_execute_tool': {\n const targetToolName = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const toolArgs = (args.args as Record<string, unknown>) ?? {};\n\n if (!targetToolName) {\n return {\n content: [{ type: 'text', text: 'Missing required parameter \"toolName\". Specify which tool to execute.' }],\n isError: true,\n };\n }\n\n // Verify the tool exists in our index\n const { tool, error } = resolveToolSchema(targetToolName, namespace);\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${targetToolName}\" not found. Use mcp_search_tool_bm25 to discover available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n if (!callToolFn) {\n return {\n content: [{ type: 'text', text: 'Tool execution is not available. No callToolFn was configured.' }],\n isError: true,\n };\n }\n\n try {\n const result = await callToolFn(targetToolName, toolArgs, namespace);\n\n // Normalize result to text\n if (result && typeof result === 'object' && 'content' in result) {\n // Already MCP CallToolResult format\n return result as CallToolResult;\n }\n\n const text = typeof result === 'string' ? result : JSON.stringify(result, null, 2);\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n content: [{ type: 'text', text: `Tool execution failed: ${errorMessage}` }],\n isError: true,\n };\n }\n }\n\n default:\n return null;\n }\n}\n\n/** Check if a tool name is one of the meta-tools. */\nexport function isMetaTool(toolName: string): boolean {\n return (\n toolName === 'mcp_search_tool_bm25' ||\n toolName === 'mcp_search_tool_regex' ||\n toolName === 'mcp_get_tool_schema' ||\n toolName === 'mcp_execute_tool'\n );\n}\n\n/**\n * Unwraps a meta-tool proxy call (like mcp_execute_tool) to find the real target tool name and arguments.\n * Also automatically strips routing prefixes like tool_{serverId}_.\n * \n * Useful for frontend components that need to determine the actual tool being executed by an AI agent.\n */\nexport function resolveMetaToolProxy(\n toolName: string,\n args: Record<string, unknown> | null | undefined\n): { toolName: string; args: Record<string, unknown> } {\n // Unwrap mcp_execute_tool proxy arguments\n if (toolName === 'mcp_execute_tool') {\n const innerName = args?.toolName;\n const innerArgs = args?.args;\n return {\n toolName: typeof innerName === 'string' && innerName ? innerName : toolName,\n args: innerArgs && typeof innerArgs === 'object' && !Array.isArray(innerArgs)\n ? (innerArgs as Record<string, unknown>)\n : {},\n };\n }\n\n // Strip tool_<serverId>_ prefix used by AIAdapter\n const match = toolName.match(/(?:tool_[^_]+_)?(.+)$/);\n const resolvedName = match?.[1] ?? toolName;\n\n return { toolName: resolvedName, args: args ?? {} };\n}\n\n","/**\n * MCP Adapter for AG-UI Integration\n *\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\n * It provides tools with handlers for server-side execution and tool definitions\n * in JSON Schema format for passing to remote agents.\n *\n * @example\n * ```typescript\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\n * import { HttpAgent } from '@ag-ui/client';\n *\n * // Create MCP client\n * const mcpClient = new MultiSessionClient('user_123');\n * await mcpClient.connect();\n *\n * // Create adapter and get tools\n * const adapter = new AguiAdapter(mcpClient);\n * const tools = await adapter.getTools();\n *\n * // Use with AG-UI middleware\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\n * agent.use(createMcpMiddleware({ tools }));\n * ```\n */\n\nimport { MCPClient } from '../server/mcp/oauth-client.js';\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\nimport { ToolRouter } from '../shared/tool-router.js';\nimport { executeMetaTool, isMetaTool } from '../shared/meta-tools.js';\n\n/**\n * Extended JSON Schema properties that Pydantic's strict validation rejects.\n * These are valid JSON Schema extensions but not part of the core spec.\n */\nconst PYDANTIC_FORBIDDEN_PROPS = [\n // JSON Schema meta-properties\n '$schema', '$id', '$comment', '$defs', 'definitions',\n // Extended properties used by some MCP servers (e.g., Apify)\n 'prefill', 'examples', 'enumTitles', 'enumDescriptions',\n // Other common extensions\n 'deprecated', 'readOnly', 'writeOnly', 'contentMediaType', 'contentEncoding',\n];\n\n/**\n * Cleans a JSON Schema by removing meta-properties that cause issues with\n * strict Pydantic validation (e.g., Google ADK, LangGraph).\n *\n * @param schema - The JSON Schema to clean\n * @returns Cleaned schema without forbidden properties\n */\nexport function cleanSchema(schema: Record<string, any> | undefined): Record<string, any> {\n if (!schema) {\n return { type: 'object', properties: {} };\n }\n\n const cleaned = { ...schema };\n\n // Remove all forbidden properties\n for (const prop of PYDANTIC_FORBIDDEN_PROPS) {\n delete cleaned[prop];\n }\n\n // Recursively clean nested properties\n if (cleaned.properties && typeof cleaned.properties === 'object') {\n const cleanedProps: Record<string, any> = {};\n for (const [key, value] of Object.entries(cleaned.properties)) {\n if (typeof value === 'object' && value !== null) {\n cleanedProps[key] = cleanSchema(value as Record<string, any>);\n } else {\n cleanedProps[key] = value;\n }\n }\n cleaned.properties = cleanedProps;\n }\n\n // Clean items if it's an array schema\n if (cleaned.items && typeof cleaned.items === 'object') {\n cleaned.items = cleanSchema(cleaned.items);\n }\n\n // Clean additionalProperties if it's an object schema\n if (cleaned.additionalProperties && typeof cleaned.additionalProperties === 'object') {\n cleaned.additionalProperties = cleanSchema(cleaned.additionalProperties);\n }\n\n return cleaned;\n}\n\n/**\n * Configuration options for AguiAdapter\n */\nexport interface AguiAdapterOptions {\n /**\n * Prefix for tool names to avoid collision with other tools.\n * @default serverId or 'mcp'\n */\n prefix?: string;\n\n /**\n * Optional ToolRouter for intelligent tool selection.\n */\n toolRouter?: ToolRouter;\n}\n\n/**\n * AG-UI Tool with handler for server-side execution.\n */\nexport interface AguiTool {\n name: string;\n description: string;\n parameters?: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiTool\n handler?: (args: any) => any | Promise<any>;\n}\n\n/**\n * Tool definition format for passing to remote agents (without handler).\n */\nexport interface AguiToolDefinition {\n name: string;\n description: string;\n parameters: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiToolDefinition\n}\n\n/**\n * Adapter that transforms MCP tools into AG-UI compatible formats.\n */\nexport class AguiAdapter {\n constructor(\n private client: MCPClient | MultiSessionClient,\n private options: AguiAdapterOptions = {}\n ) { }\n\n /**\n * Get tools with handlers for MCP tool execution.\n */\n async getTools(): Promise<AguiTool[]> {\n if (this.options.toolRouter) {\n return this.getToolsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiTool[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformTools(client));\n }\n return allTools;\n }\n return this.transformTools(this.client as MCPClient);\n }\n\n /**\n * Get tool definitions in JSON Schema format for passing to remote agents.\n */\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\n if (this.options.toolRouter) {\n return this.getToolDefinitionsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiToolDefinition[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformToolDefinitions(client));\n }\n return allTools;\n }\n return this.transformToolDefinitions(this.client as MCPClient);\n }\n\n /**\n * Get tools as a function (for dynamic loading).\n */\n getToolsFunction(): () => Promise<AguiTool[]> {\n return () => this.getTools();\n }\n\n private isMultiSession(): boolean {\n return typeof (this.client as any).getClients === 'function';\n }\n\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n // Type assertion to access _meta if it exists on the tool object (it comes from MCP SDK)\n const mcpTool = tool as any;\n const mcpToolName = tool.name;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n handler: async (args: any) => {\n // Call the actual MCP tool\n const callResult = await (client as any).callTool(mcpToolName, args ?? {});\n\n // Return the raw result object so middleware can inspect `_meta` (e.g. for UI triggers)\n return callResult;\n }\n }\n });\n }\n\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n const mcpTool = tool as any;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n };\n });\n }\n\n /**\n * Build AG-UI tools from a ToolRouter's filtered output.\n *\n * In `search` strategy, only meta-tools are registered with the framework.\n * Real tool execution is proxied through `mcp_execute_tool` which uses\n * `router.callTool()` to route to the correct MCP client.\n */\n private async getToolsViaRouter(router: ToolRouter): Promise<AguiTool[]> {\n const filteredTools = await router.getFilteredTools();\n\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n const namespace = routedTool.serverId ?? routedTool.sessionId;\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n handler: async (args: any) => {\n if (isMetaTool(tool.name)) {\n const result = await executeMetaTool(\n tool.name,\n args,\n router,\n (name, toolArgs, namespace) => router.callTool(name, toolArgs, namespace)\n );\n if (result) {\n return result.content.map((c: any) => c.text ?? '').join('\\n');\n }\n return \"Failed to execute meta-tool\";\n }\n\n // For non-meta tools in 'all' or 'groups' strategy,\n // route directly to the correct MCP client\n return await router.callTool(tool.name, args, namespace);\n }\n };\n });\n }\n\n private async getToolDefinitionsViaRouter(router: ToolRouter): Promise<AguiToolDefinition[]> {\n const filteredTools = await router.getFilteredTools();\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema)\n };\n });\n }\n\n private getRouterToolKey(toolName: string, sessionId?: string, serverId?: string): string {\n const namespace = sessionId ?? serverId ?? 'mcp';\n const normalized = namespace\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '_')\n .replace(/^_+|_+$/g, '') || 'mcp';\n return `tool_${normalized}_${toolName}`;\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/shared/meta-tools.ts","../../src/adapters/agui-adapter.ts"],"names":["text","namespace"],"mappings":";;;AA2LA,eAAsB,eAAA,CACpB,QAAA,EACA,IAAA,EACA,MAAA,EACA,UAAA,EACgC;AAChC,EAAA,MAAM,iBAAA,GAAoB,CAAC,IAAA,EAAc,SAAA,KAAuE;AAC9G,IAAA,IAAI;AACF,MAAA,OAAO,EAAE,IAAA,EAAM,MAAA,CAAO,aAAA,CAAc,IAAA,EAAM,SAAS,CAAA,EAAE;AAAA,IACvD,SAAS,GAAA,EAAK;AACZ,MAAA,MAAM,eAAe,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,OAAO,GAAG,CAAA;AACpE,MAAA,OAAO;AAAA,QACL,KAAA,EAAO;AAAA,UACL,SAAS,CAAC,EAAE,MAAM,MAAA,EAAQ,IAAA,EAAM,cAAc,CAAA;AAAA,UAC9C,OAAA,EAAS;AAAA;AACX,OACF;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,QAAQ,QAAA;AAAU,IAChB,KAAK,sBAAA,EAAwB;AAC3B,MAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,IAAA,CAAK,KAAA,IAAS,EAAE,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AAA,EAAY,WAAA,CAAY,IAAA,CAAK,WAAW;AAAA,OAC5C;AAAA,IACJ,CAAC,CAAA;AAAA,EACL;AAAA,EAEQ,gBAAA,CAAiB,QAAA,EAAkB,SAAA,EAAoB,QAAA,EAA2B;AACtF,IAAA,MAAM,SAAA,GAAY,aAAa,QAAA,IAAY,KAAA;AAC3C,IAAA,MAAM,UAAA,GAAa,SAAA,CACd,WAAA,EAAY,CACZ,OAAA,CAAQ,aAAA,EAAe,GAAG,CAAA,CAC1B,OAAA,CAAQ,UAAA,EAAY,EAAE,CAAA,IAAK,KAAA;AAChC,IAAA,OAAO,CAAA,KAAA,EAAQ,UAAU,CAAA,CAAA,EAAI,QAAQ,CAAA,CAAA;AAAA,EACzC;AACJ","file":"agui-adapter.js","sourcesContent":["/**\n * Meta-tools β€” Injectable tool definitions that let the LLM discover and\n * load MCP tools on-demand, following Anthropic's Tool Search pattern.\n *\n * Instead of injecting 50+ full tool schemas into the context window, you\n * inject just these 4 meta-tools. The LLM calls them to find and load\n * only the tools it actually needs.\n *\n * Meta-tools:\n * β€’ `mcp_search_tool_bm25` β€” BM25 natural language search\n * β€’ `mcp_search_tool_regex` β€” Regex pattern search\n * β€’ `mcp_get_tool_schema` β€” Get full inputSchema for a discovered tool\n * β€’ `mcp_execute_tool` β€” Execute a discovered tool\n *\n * @packageDocumentation\n */\n\nimport type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';\nimport type { ToolRouter } from './tool-router.js';\nimport type { IndexedTool } from './tool-index.js';\n\n// ---------------------------------------------------------------------------\n// Tool Definitions\n// ---------------------------------------------------------------------------\n\n/**\n * Creates the `mcp_search_tool_bm25` tool definition.\n *\n * This tool lets the LLM search the full catalog of available MCP tools\n * using a BM25 natural-language query. Returns tool names and descriptions\n * without the full inputSchema to save context space.\n */\nexport function createSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_bm25',\n description:\n 'Search the catalog of available tools. Returns tool names, descriptions, and server info. ' +\n 'Use this FIRST to find relevant tools before calling them.\\n\\n' +\n 'Query forms:\\n' +\n '- \"select:Read,Edit,Grep\" β€” fetch these exact tools by name\\n' +\n '- \"notebook jupyter\" β€” keyword search, up to limit best matches\\n' +\n '- \"+slack send\" β€” require \"slack\" in the name, rank by remaining terms',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Query to find tools. Use \"select:<tool_name>\" for direct selection, or keywords to search. Prefix keywords with + to require them.',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_search_tool_regex` tool definition.\n * \n * Matches Anthropic's tool_search_tool_regex exactly (takes a 'query' regex pattern).\n */\nexport function createRegexSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_regex',\n description:\n 'Search the catalog of available tools using a Python-style regex pattern. ' +\n 'Matches against tool names, descriptions, and parameter descriptions. ' +\n 'Example patterns: \"^github_\", \"weather\", \"(?i)slack\".',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Regex pattern to search for (e.g., \"^get_.*_data\", \"database\").',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_get_tool_schema` tool definition.\n *\n * After discovering tools via `mcp_search_tool_bm25` or\n * `mcp_search_tool_regex`, the LLM calls this to load the full\n * inputSchema for a specific tool so it can construct the correct\n * arguments.\n */\nexport function createGetSchemaToolDefinition(): Tool {\n return {\n name: 'mcp_get_tool_schema',\n description:\n 'Get the full input schema (parameters) for a specific tool. ' +\n 'Call this after mcp_search_tool_bm25 to get the parameter details ' +\n 'needed to call a tool correctly. ' +\n 'Do NOT call the discovered tool directly; after reading the schema, call mcp_execute_tool.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name returned by mcp_search_tool_bm25.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n/**\n * Creates the `mcp_execute_tool` tool definition.\n *\n * This is the execution meta-tool β€” the LLM calls this to execute any\n * tool discovered via `mcp_search_tool_bm25` or `mcp_search_tool_regex`.\n * The LLM should first call `mcp_get_tool_schema` to know the correct\n * arguments.\n *\n * Instead of registering every real tool with the framework, we proxy\n * all execution through a single meta-tool.\n */\nexport function createExecuteToolDefinition(): Tool {\n return {\n name: 'mcp_execute_tool',\n description:\n 'Execute a tool that was discovered via mcp_search_tool_bm25. ' +\n 'You MUST call mcp_get_tool_schema first to know the correct parameters. ' +\n 'Pass the exact tool name and its arguments.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name from mcp_search_tool_bm25 results.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n args: {\n type: 'object',\n description:\n \"Arguments matching the tool's inputSchema. Omit or pass {} if the tool takes no parameters.\",\n additionalProperties: true,\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n// ---------------------------------------------------------------------------\n// Meta-tool Executors\n// ---------------------------------------------------------------------------\n\n/**\n * Callback for executing a real MCP tool via the correct client.\n * Provided by adapters that wire up client routing.\n */\nexport type CallToolFn = (\n toolName: string,\n args: Record<string, unknown>,\n namespace?: string\n) => Promise<any>;\n\n/**\n * Execute a meta-tool call and return the result in MCP CallToolResult format.\n *\n * @param toolName - One of the meta-tool names (mcp_search_tool_bm25, mcp_search_tool_regex, etc.)\n * @param args - The arguments from the LLM's tool call\n * @param router - The ToolRouter to query\n * @param callToolFn - Optional callback for executing real tools (required for mcp_execute_tool)\n * @returns MCP-compatible CallToolResult, or null if this isn't a meta-tool\n */\nexport async function executeMetaTool(\n toolName: string,\n args: Record<string, unknown>,\n router: ToolRouter,\n callToolFn?: CallToolFn\n): Promise<CallToolResult | null> {\n const resolveToolSchema = (name: string, namespace?: string): { tool?: IndexedTool; error?: CallToolResult } => {\n try {\n return { tool: router.getToolSchema(name, namespace) };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n error: {\n content: [{ type: 'text', text: errorMessage }],\n isError: true,\n },\n };\n }\n };\n\n switch (toolName) {\n case 'mcp_search_tool_bm25': {\n const query = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n // Fast path: Check for select: prefix\n const selectMatch = query.match(/^select:(.+)$/i);\n if (selectMatch) {\n const requested = selectMatch[1]!\n .split(',')\n .map((s) => s.trim())\n .filter(Boolean);\n\n const found: any[] = [];\n const errors: string[] = [];\n \n for (const requestedToolName of requested) {\n const { tool, error } = resolveToolSchema(requestedToolName);\n if (error) {\n const errorMsg = error.content[0]?.type === 'text' ? error.content[0].text : 'Unknown error';\n errors.push(`- **${requestedToolName}**: ${errorMsg}`);\n } else if (tool) {\n found.push(tool);\n } else {\n errors.push(`- **${requestedToolName}**: Tool not found. Try searching with mcp_search_tool_bm25.`);\n }\n }\n\n const lines: string[] = [];\n\n if (found.length > 0) {\n lines.push(...found.map((t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n ${t.description}`\n ));\n }\n \n if (errors.length > 0) {\n if (lines.length > 0) lines.push(\"\"); // Add empty line spacing\n lines.push(\"Errors resolving some tools:\");\n lines.push(...errors);\n }\n\n const text = lines.length > 0 \n ? lines.join('\\n') \n : `No tools found matching select query: ${requested.join(', ')}`;\n\n return {\n content: [{ type: 'text', text }],\n isError: found.length === 0,\n };\n }\n\n const results = await router.searchTools(query, limit);\n\n const text = results.length === 0\n ? 'No tools found matching your query. Try different keywords.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_search_tool_regex': {\n const pattern = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n const results = await router.searchToolsRegex(pattern, limit);\n\n const text = results.length === 0\n ? 'No tools matched your regex pattern. Try a broader pattern.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_get_tool_schema': {\n const name = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const { tool, error } = resolveToolSchema(name, namespace);\n\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${name}\" not found. Use mcp_search_tool_bm25 to find available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n const schema = {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n executionInstructions: {\n nextTool: 'mcp_execute_tool',\n toolName: tool.name,\n serverId: tool.serverId,\n note:\n 'Do not call this discovered tool directly unless it was explicitly registered as a runtime tool. Execute it via mcp_execute_tool and pass these parameters inside args.',\n },\n };\n\n return {\n content: [{ type: 'text', text: JSON.stringify(schema, null, 2) }],\n isError: false,\n };\n }\n\n case 'mcp_execute_tool': {\n const targetToolName = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const toolArgs = (args.args as Record<string, unknown>) ?? {};\n\n if (!targetToolName) {\n return {\n content: [{ type: 'text', text: 'Missing required parameter \"toolName\". Specify which tool to execute.' }],\n isError: true,\n };\n }\n\n // Verify the tool exists in our index\n const { tool, error } = resolveToolSchema(targetToolName, namespace);\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${targetToolName}\" not found. Use mcp_search_tool_bm25 to discover available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n if (!callToolFn) {\n return {\n content: [{ type: 'text', text: 'Tool execution is not available. No callToolFn was configured.' }],\n isError: true,\n };\n }\n\n try {\n const result = await callToolFn(targetToolName, toolArgs, namespace);\n\n // Normalize result to text\n if (result && typeof result === 'object' && 'content' in result) {\n // Already MCP CallToolResult format\n return result as CallToolResult;\n }\n\n const text = typeof result === 'string' ? result : JSON.stringify(result, null, 2);\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n content: [{ type: 'text', text: `Tool execution failed: ${errorMessage}` }],\n isError: true,\n };\n }\n }\n\n default:\n return null;\n }\n}\n\n/** Check if a tool name is one of the meta-tools. */\nexport function isMetaTool(toolName: string): boolean {\n return (\n toolName === 'mcp_search_tool_bm25' ||\n toolName === 'mcp_search_tool_regex' ||\n toolName === 'mcp_get_tool_schema' ||\n toolName === 'mcp_execute_tool'\n );\n}\n\n/**\n * Unwraps a meta-tool proxy call (like mcp_execute_tool) to find the real target tool name and arguments.\n * Also automatically strips routing prefixes like tool_{serverId}_.\n * \n * Useful for frontend components that need to determine the actual tool being executed by an AI agent.\n */\nexport function resolveMetaToolProxy(\n toolName: string,\n args: Record<string, unknown> | null | undefined\n): { toolName: string; args: Record<string, unknown> } {\n // Unwrap mcp_execute_tool proxy arguments\n if (toolName === 'mcp_execute_tool') {\n const innerName = args?.toolName;\n const innerArgs = args?.args;\n return {\n toolName: typeof innerName === 'string' && innerName ? innerName : toolName,\n args: innerArgs && typeof innerArgs === 'object' && !Array.isArray(innerArgs)\n ? (innerArgs as Record<string, unknown>)\n : {},\n };\n }\n\n // Strip tool_<serverId>_ prefix used by AIAdapter\n const match = toolName.match(/(?:tool_[^_]+_)?(.+)$/);\n const resolvedName = match?.[1] ?? toolName;\n\n return { toolName: resolvedName, args: args ?? {} };\n}\n\n","/**\n * MCP Adapter for AG-UI Integration\n *\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\n * It provides tools with handlers for server-side execution and tool definitions\n * in JSON Schema format for passing to remote agents.\n *\n * @example\n * ```typescript\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\n * import { HttpAgent } from '@ag-ui/client';\n *\n * // Create MCP client\n * const mcpClient = new MultiSessionClient('user_123');\n * await mcpClient.connect();\n *\n * // Create adapter and get tools\n * const adapter = new AguiAdapter(mcpClient);\n * const tools = await adapter.getTools();\n *\n * // Use with AG-UI middleware\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\n * agent.use(createMcpMiddleware({ tools }));\n * ```\n */\n\nimport { MCPClient } from '../server/mcp/oauth-client.js';\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\nimport { ToolRouter } from '../shared/tool-router.js';\nimport { executeMetaTool, isMetaTool } from '../shared/meta-tools.js';\n\n/**\n * Extended JSON Schema properties that Pydantic's strict validation rejects.\n * These are valid JSON Schema extensions but not part of the core spec.\n */\nconst PYDANTIC_FORBIDDEN_PROPS = [\n // JSON Schema meta-properties\n '$schema', '$id', '$comment', '$defs', 'definitions',\n // Extended properties used by some MCP servers (e.g., Apify)\n 'prefill', 'examples', 'enumTitles', 'enumDescriptions',\n // Other common extensions\n 'deprecated', 'readOnly', 'writeOnly', 'contentMediaType', 'contentEncoding',\n];\n\n/**\n * Cleans a JSON Schema by removing meta-properties that cause issues with\n * strict Pydantic validation (e.g., Google ADK, LangGraph).\n *\n * @param schema - The JSON Schema to clean\n * @returns Cleaned schema without forbidden properties\n */\nexport function cleanSchema(schema: Record<string, any> | undefined): Record<string, any> {\n if (!schema) {\n return { type: 'object', properties: {} };\n }\n\n const cleaned = { ...schema };\n\n // Remove all forbidden properties\n for (const prop of PYDANTIC_FORBIDDEN_PROPS) {\n delete cleaned[prop];\n }\n\n // Recursively clean nested properties\n if (cleaned.properties && typeof cleaned.properties === 'object') {\n const cleanedProps: Record<string, any> = {};\n for (const [key, value] of Object.entries(cleaned.properties)) {\n if (typeof value === 'object' && value !== null) {\n cleanedProps[key] = cleanSchema(value as Record<string, any>);\n } else {\n cleanedProps[key] = value;\n }\n }\n cleaned.properties = cleanedProps;\n }\n\n // Clean items if it's an array schema\n if (cleaned.items && typeof cleaned.items === 'object') {\n cleaned.items = cleanSchema(cleaned.items);\n }\n\n // Clean additionalProperties if it's an object schema\n if (cleaned.additionalProperties && typeof cleaned.additionalProperties === 'object') {\n cleaned.additionalProperties = cleanSchema(cleaned.additionalProperties);\n }\n\n return cleaned;\n}\n\n/**\n * Configuration options for AguiAdapter\n */\nexport interface AguiAdapterOptions {\n /**\n * Prefix for tool names to avoid collision with other tools.\n * @default serverId or 'mcp'\n */\n prefix?: string;\n\n /**\n * Optional ToolRouter for intelligent tool selection.\n */\n toolRouter?: ToolRouter;\n}\n\n/**\n * AG-UI Tool with handler for server-side execution.\n */\nexport interface AguiTool {\n name: string;\n description: string;\n parameters?: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiTool\n handler?: (args: any) => any | Promise<any>;\n}\n\n/**\n * Tool definition format for passing to remote agents (without handler).\n */\nexport interface AguiToolDefinition {\n name: string;\n description: string;\n parameters: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiToolDefinition\n}\n\n/**\n * Adapter that transforms MCP tools into AG-UI compatible formats.\n */\nexport class AguiAdapter {\n constructor(\n private client: MCPClient | MultiSessionClient,\n private options: AguiAdapterOptions = {}\n ) { }\n\n /**\n * Get tools with handlers for MCP tool execution.\n */\n async getTools(): Promise<AguiTool[]> {\n if (this.options.toolRouter) {\n return this.getToolsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiTool[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformTools(client));\n }\n return allTools;\n }\n return this.transformTools(this.client as MCPClient);\n }\n\n /**\n * Get tool definitions in JSON Schema format for passing to remote agents.\n */\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\n if (this.options.toolRouter) {\n return this.getToolDefinitionsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiToolDefinition[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformToolDefinitions(client));\n }\n return allTools;\n }\n return this.transformToolDefinitions(this.client as MCPClient);\n }\n\n /**\n * Get tools as a function (for dynamic loading).\n */\n getToolsFunction(): () => Promise<AguiTool[]> {\n return () => this.getTools();\n }\n\n private isMultiSession(): boolean {\n return typeof (this.client as any).getClients === 'function';\n }\n\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n // Type assertion to access _meta if it exists on the tool object (it comes from MCP SDK)\n const mcpTool = tool as any;\n const mcpToolName = tool.name;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n handler: async (args: any) => {\n // Call the actual MCP tool\n const callResult = await (client as any).callTool(mcpToolName, args ?? {});\n\n // Return the raw result object so middleware can inspect `_meta` (e.g. for UI triggers)\n return callResult;\n }\n }\n });\n }\n\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n const mcpTool = tool as any;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n };\n });\n }\n\n /**\n * Build AG-UI tools from a ToolRouter's filtered output.\n *\n * In `search` strategy, only meta-tools are registered with the framework.\n * Real tool execution is proxied through `mcp_execute_tool` which uses\n * `router.callTool()` to route to the correct MCP client.\n */\n private async getToolsViaRouter(router: ToolRouter): Promise<AguiTool[]> {\n const filteredTools = await router.getFilteredTools();\n\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n const namespace = routedTool.serverId ?? routedTool.sessionId;\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n handler: async (args: any) => {\n if (isMetaTool(tool.name)) {\n const result = await executeMetaTool(\n tool.name,\n args,\n router,\n (name, toolArgs, namespace) => router.callTool(name, toolArgs, namespace)\n );\n if (result) {\n return result.content.map((c: any) => c.text ?? '').join('\\n');\n }\n return \"Failed to execute meta-tool\";\n }\n\n // For non-meta tools in 'all' or 'groups' strategy,\n // route directly to the correct MCP client\n return await router.callTool(tool.name, args, namespace);\n }\n };\n });\n }\n\n private async getToolDefinitionsViaRouter(router: ToolRouter): Promise<AguiToolDefinition[]> {\n const filteredTools = await router.getFilteredTools();\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema)\n };\n });\n }\n\n private getRouterToolKey(toolName: string, sessionId?: string, serverId?: string): string {\n const namespace = sessionId ?? serverId ?? 'mcp';\n const normalized = namespace\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '_')\n .replace(/^_+|_+$/g, '') || 'mcp';\n return `tool_${normalized}_${toolName}`;\n }\n}\n"]}
@@ -97,7 +97,13 @@ async function executeMetaTool(toolName, args, router, callToolFn) {
97
97
  const schema = {
98
98
  name: tool.name,
99
99
  description: tool.description,
100
- inputSchema: tool.inputSchema
100
+ inputSchema: tool.inputSchema,
101
+ executionInstructions: {
102
+ nextTool: "mcp_execute_tool",
103
+ toolName: tool.name,
104
+ serverId: tool.serverId,
105
+ note: "Do not call this discovered tool directly unless it was explicitly registered as a runtime tool. Execute it via mcp_execute_tool and pass these parameters inside args."
106
+ }
101
107
  };
102
108
  return {
103
109
  content: [{ type: "text", text: JSON.stringify(schema, null, 2) }],
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/shared/meta-tools.ts","../../src/adapters/agui-adapter.ts"],"names":["text","namespace"],"mappings":";AAyLA,eAAsB,eAAA,CACpB,QAAA,EACA,IAAA,EACA,MAAA,EACA,UAAA,EACgC;AAChC,EAAA,MAAM,iBAAA,GAAoB,CAAC,IAAA,EAAc,SAAA,KAAgE;AACvG,IAAA,IAAI;AACF,MAAA,OAAO,EAAE,IAAA,EAAM,MAAA,CAAO,aAAA,CAAc,IAAA,EAAM,SAAS,CAAA,EAAE;AAAA,IACvD,SAAS,GAAA,EAAK;AACZ,MAAA,MAAM,eAAe,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,OAAO,GAAG,CAAA;AACpE,MAAA,OAAO;AAAA,QACL,KAAA,EAAO;AAAA,UACL,SAAS,CAAC,EAAE,MAAM,MAAA,EAAQ,IAAA,EAAM,cAAc,CAAA;AAAA,UAC9C,OAAA,EAAS;AAAA;AACX,OACF;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,QAAQ,QA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5C;AAAA,IACJ,CAAC,CAAA;AAAA,EACL;AAAA,EAEQ,gBAAA,CAAiB,QAAA,EAAkB,SAAA,EAAoB,QAAA,EAA2B;AACtF,IAAA,MAAM,SAAA,GAAY,aAAa,QAAA,IAAY,KAAA;AAC3C,IAAA,MAAM,UAAA,GAAa,SAAA,CACd,WAAA,EAAY,CACZ,OAAA,CAAQ,aAAA,EAAe,GAAG,CAAA,CAC1B,OAAA,CAAQ,UAAA,EAAY,EAAE,CAAA,IAAK,KAAA;AAChC,IAAA,OAAO,CAAA,KAAA,EAAQ,UAAU,CAAA,CAAA,EAAI,QAAQ,CAAA,CAAA;AAAA,EACzC;AACJ","file":"agui-adapter.mjs","sourcesContent":["/**\n * Meta-tools β€” Injectable tool definitions that let the LLM discover and\n * load MCP tools on-demand, following Anthropic's Tool Search pattern.\n *\n * Instead of injecting 50+ full tool schemas into the context window, you\n * inject just these 4 meta-tools. The LLM calls them to find and load\n * only the tools it actually needs.\n *\n * Meta-tools:\n * β€’ `mcp_search_tool_bm25` β€” BM25 natural language search\n * β€’ `mcp_search_tool_regex` β€” Regex pattern search\n * β€’ `mcp_get_tool_schema` β€” Get full inputSchema for a discovered tool\n * β€’ `mcp_execute_tool` β€” Execute a discovered tool\n *\n * @packageDocumentation\n */\n\nimport type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';\nimport type { ToolRouter } from './tool-router.js';\n\n// ---------------------------------------------------------------------------\n// Tool Definitions\n// ---------------------------------------------------------------------------\n\n/**\n * Creates the `mcp_search_tool_bm25` tool definition.\n *\n * This tool lets the LLM search the full catalog of available MCP tools\n * using a BM25 natural-language query. Returns tool names and descriptions\n * without the full inputSchema to save context space.\n */\nexport function createSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_bm25',\n description:\n 'Search the catalog of available tools. Returns tool names, descriptions, and server info. ' +\n 'Use this FIRST to find relevant tools before calling them.\\n\\n' +\n 'Query forms:\\n' +\n '- \"select:Read,Edit,Grep\" β€” fetch these exact tools by name\\n' +\n '- \"notebook jupyter\" β€” keyword search, up to limit best matches\\n' +\n '- \"+slack send\" β€” require \"slack\" in the name, rank by remaining terms',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Query to find tools. Use \"select:<tool_name>\" for direct selection, or keywords to search. Prefix keywords with + to require them.',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_search_tool_regex` tool definition.\n * \n * Matches Anthropic's tool_search_tool_regex exactly (takes a 'query' regex pattern).\n */\nexport function createRegexSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_regex',\n description:\n 'Search the catalog of available tools using a Python-style regex pattern. ' +\n 'Matches against tool names, descriptions, and parameter descriptions. ' +\n 'Example patterns: \"^github_\", \"weather\", \"(?i)slack\".',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Regex pattern to search for (e.g., \"^get_.*_data\", \"database\").',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_get_tool_schema` tool definition.\n *\n * After discovering tools via `mcp_search_tool_bm25` or\n * `mcp_search_tool_regex`, the LLM calls this to load the full\n * inputSchema for a specific tool so it can construct the correct\n * arguments.\n */\nexport function createGetSchemaToolDefinition(): Tool {\n return {\n name: 'mcp_get_tool_schema',\n description:\n 'Get the full input schema (parameters) for a specific tool. ' +\n 'Call this after mcp_search_tool_bm25 to get the parameter details ' +\n 'needed to call a tool correctly.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name returned by mcp_search_tool_bm25.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n/**\n * Creates the `mcp_execute_tool` tool definition.\n *\n * This is the execution meta-tool β€” the LLM calls this to execute any\n * tool discovered via `mcp_search_tool_bm25` or `mcp_search_tool_regex`.\n * The LLM should first call `mcp_get_tool_schema` to know the correct\n * arguments.\n *\n * Instead of registering every real tool with the framework, we proxy\n * all execution through a single meta-tool.\n */\nexport function createExecuteToolDefinition(): Tool {\n return {\n name: 'mcp_execute_tool',\n description:\n 'Execute a tool that was discovered via mcp_search_tool_bm25. ' +\n 'You MUST call mcp_get_tool_schema first to know the correct parameters. ' +\n 'Pass the exact tool name and its arguments.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name from mcp_search_tool_bm25 results.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n args: {\n type: 'object',\n description:\n \"Arguments matching the tool's inputSchema. Omit or pass {} if the tool takes no parameters.\",\n additionalProperties: true,\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n// ---------------------------------------------------------------------------\n// Meta-tool Executors\n// ---------------------------------------------------------------------------\n\n/**\n * Callback for executing a real MCP tool via the correct client.\n * Provided by adapters that wire up client routing.\n */\nexport type CallToolFn = (\n toolName: string,\n args: Record<string, unknown>,\n namespace?: string\n) => Promise<any>;\n\n/**\n * Execute a meta-tool call and return the result in MCP CallToolResult format.\n *\n * @param toolName - One of the meta-tool names (mcp_search_tool_bm25, mcp_search_tool_regex, etc.)\n * @param args - The arguments from the LLM's tool call\n * @param router - The ToolRouter to query\n * @param callToolFn - Optional callback for executing real tools (required for mcp_execute_tool)\n * @returns MCP-compatible CallToolResult, or null if this isn't a meta-tool\n */\nexport async function executeMetaTool(\n toolName: string,\n args: Record<string, unknown>,\n router: ToolRouter,\n callToolFn?: CallToolFn\n): Promise<CallToolResult | null> {\n const resolveToolSchema = (name: string, namespace?: string): { tool?: Tool; error?: CallToolResult } => {\n try {\n return { tool: router.getToolSchema(name, namespace) };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n error: {\n content: [{ type: 'text', text: errorMessage }],\n isError: true,\n },\n };\n }\n };\n\n switch (toolName) {\n case 'mcp_search_tool_bm25': {\n const query = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n // Fast path: Check for select: prefix\n const selectMatch = query.match(/^select:(.+)$/i);\n if (selectMatch) {\n const requested = selectMatch[1]!\n .split(',')\n .map((s) => s.trim())\n .filter(Boolean);\n\n const found: any[] = [];\n const errors: string[] = [];\n \n for (const requestedToolName of requested) {\n const { tool, error } = resolveToolSchema(requestedToolName);\n if (error) {\n const errorMsg = error.content[0]?.type === 'text' ? error.content[0].text : 'Unknown error';\n errors.push(`- **${requestedToolName}**: ${errorMsg}`);\n } else if (tool) {\n found.push(tool);\n } else {\n errors.push(`- **${requestedToolName}**: Tool not found. Try searching with mcp_search_tool_bm25.`);\n }\n }\n\n const lines: string[] = [];\n\n if (found.length > 0) {\n lines.push(...found.map((t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n ${t.description}`\n ));\n }\n \n if (errors.length > 0) {\n if (lines.length > 0) lines.push(\"\"); // Add empty line spacing\n lines.push(\"Errors resolving some tools:\");\n lines.push(...errors);\n }\n\n const text = lines.length > 0 \n ? lines.join('\\n') \n : `No tools found matching select query: ${requested.join(', ')}`;\n\n return {\n content: [{ type: 'text', text }],\n isError: found.length === 0,\n };\n }\n\n const results = await router.searchTools(query, limit);\n\n const text = results.length === 0\n ? 'No tools found matching your query. Try different keywords.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_search_tool_regex': {\n const pattern = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n const results = await router.searchToolsRegex(pattern, limit);\n\n const text = results.length === 0\n ? 'No tools matched your regex pattern. Try a broader pattern.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_get_tool_schema': {\n const name = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const { tool, error } = resolveToolSchema(name, namespace);\n\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${name}\" not found. Use mcp_search_tool_bm25 to find available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n const schema = {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n };\n\n return {\n content: [{ type: 'text', text: JSON.stringify(schema, null, 2) }],\n isError: false,\n };\n }\n\n case 'mcp_execute_tool': {\n const targetToolName = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const toolArgs = (args.args as Record<string, unknown>) ?? {};\n\n if (!targetToolName) {\n return {\n content: [{ type: 'text', text: 'Missing required parameter \"toolName\". Specify which tool to execute.' }],\n isError: true,\n };\n }\n\n // Verify the tool exists in our index\n const { tool, error } = resolveToolSchema(targetToolName, namespace);\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${targetToolName}\" not found. Use mcp_search_tool_bm25 to discover available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n if (!callToolFn) {\n return {\n content: [{ type: 'text', text: 'Tool execution is not available. No callToolFn was configured.' }],\n isError: true,\n };\n }\n\n try {\n const result = await callToolFn(targetToolName, toolArgs, namespace);\n\n // Normalize result to text\n if (result && typeof result === 'object' && 'content' in result) {\n // Already MCP CallToolResult format\n return result as CallToolResult;\n }\n\n const text = typeof result === 'string' ? result : JSON.stringify(result, null, 2);\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n content: [{ type: 'text', text: `Tool execution failed: ${errorMessage}` }],\n isError: true,\n };\n }\n }\n\n default:\n return null;\n }\n}\n\n/** Check if a tool name is one of the meta-tools. */\nexport function isMetaTool(toolName: string): boolean {\n return (\n toolName === 'mcp_search_tool_bm25' ||\n toolName === 'mcp_search_tool_regex' ||\n toolName === 'mcp_get_tool_schema' ||\n toolName === 'mcp_execute_tool'\n );\n}\n\n/**\n * Unwraps a meta-tool proxy call (like mcp_execute_tool) to find the real target tool name and arguments.\n * Also automatically strips routing prefixes like tool_{serverId}_.\n * \n * Useful for frontend components that need to determine the actual tool being executed by an AI agent.\n */\nexport function resolveMetaToolProxy(\n toolName: string,\n args: Record<string, unknown> | null | undefined\n): { toolName: string; args: Record<string, unknown> } {\n // Unwrap mcp_execute_tool proxy arguments\n if (toolName === 'mcp_execute_tool') {\n const innerName = args?.toolName;\n const innerArgs = args?.args;\n return {\n toolName: typeof innerName === 'string' && innerName ? innerName : toolName,\n args: innerArgs && typeof innerArgs === 'object' && !Array.isArray(innerArgs)\n ? (innerArgs as Record<string, unknown>)\n : {},\n };\n }\n\n // Strip tool_<serverId>_ prefix used by AIAdapter\n const match = toolName.match(/(?:tool_[^_]+_)?(.+)$/);\n const resolvedName = match?.[1] ?? toolName;\n\n return { toolName: resolvedName, args: args ?? {} };\n}\n\n","/**\n * MCP Adapter for AG-UI Integration\n *\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\n * It provides tools with handlers for server-side execution and tool definitions\n * in JSON Schema format for passing to remote agents.\n *\n * @example\n * ```typescript\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\n * import { HttpAgent } from '@ag-ui/client';\n *\n * // Create MCP client\n * const mcpClient = new MultiSessionClient('user_123');\n * await mcpClient.connect();\n *\n * // Create adapter and get tools\n * const adapter = new AguiAdapter(mcpClient);\n * const tools = await adapter.getTools();\n *\n * // Use with AG-UI middleware\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\n * agent.use(createMcpMiddleware({ tools }));\n * ```\n */\n\nimport { MCPClient } from '../server/mcp/oauth-client.js';\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\nimport { ToolRouter } from '../shared/tool-router.js';\nimport { executeMetaTool, isMetaTool } from '../shared/meta-tools.js';\n\n/**\n * Extended JSON Schema properties that Pydantic's strict validation rejects.\n * These are valid JSON Schema extensions but not part of the core spec.\n */\nconst PYDANTIC_FORBIDDEN_PROPS = [\n // JSON Schema meta-properties\n '$schema', '$id', '$comment', '$defs', 'definitions',\n // Extended properties used by some MCP servers (e.g., Apify)\n 'prefill', 'examples', 'enumTitles', 'enumDescriptions',\n // Other common extensions\n 'deprecated', 'readOnly', 'writeOnly', 'contentMediaType', 'contentEncoding',\n];\n\n/**\n * Cleans a JSON Schema by removing meta-properties that cause issues with\n * strict Pydantic validation (e.g., Google ADK, LangGraph).\n *\n * @param schema - The JSON Schema to clean\n * @returns Cleaned schema without forbidden properties\n */\nexport function cleanSchema(schema: Record<string, any> | undefined): Record<string, any> {\n if (!schema) {\n return { type: 'object', properties: {} };\n }\n\n const cleaned = { ...schema };\n\n // Remove all forbidden properties\n for (const prop of PYDANTIC_FORBIDDEN_PROPS) {\n delete cleaned[prop];\n }\n\n // Recursively clean nested properties\n if (cleaned.properties && typeof cleaned.properties === 'object') {\n const cleanedProps: Record<string, any> = {};\n for (const [key, value] of Object.entries(cleaned.properties)) {\n if (typeof value === 'object' && value !== null) {\n cleanedProps[key] = cleanSchema(value as Record<string, any>);\n } else {\n cleanedProps[key] = value;\n }\n }\n cleaned.properties = cleanedProps;\n }\n\n // Clean items if it's an array schema\n if (cleaned.items && typeof cleaned.items === 'object') {\n cleaned.items = cleanSchema(cleaned.items);\n }\n\n // Clean additionalProperties if it's an object schema\n if (cleaned.additionalProperties && typeof cleaned.additionalProperties === 'object') {\n cleaned.additionalProperties = cleanSchema(cleaned.additionalProperties);\n }\n\n return cleaned;\n}\n\n/**\n * Configuration options for AguiAdapter\n */\nexport interface AguiAdapterOptions {\n /**\n * Prefix for tool names to avoid collision with other tools.\n * @default serverId or 'mcp'\n */\n prefix?: string;\n\n /**\n * Optional ToolRouter for intelligent tool selection.\n */\n toolRouter?: ToolRouter;\n}\n\n/**\n * AG-UI Tool with handler for server-side execution.\n */\nexport interface AguiTool {\n name: string;\n description: string;\n parameters?: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiTool\n handler?: (args: any) => any | Promise<any>;\n}\n\n/**\n * Tool definition format for passing to remote agents (without handler).\n */\nexport interface AguiToolDefinition {\n name: string;\n description: string;\n parameters: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiToolDefinition\n}\n\n/**\n * Adapter that transforms MCP tools into AG-UI compatible formats.\n */\nexport class AguiAdapter {\n constructor(\n private client: MCPClient | MultiSessionClient,\n private options: AguiAdapterOptions = {}\n ) { }\n\n /**\n * Get tools with handlers for MCP tool execution.\n */\n async getTools(): Promise<AguiTool[]> {\n if (this.options.toolRouter) {\n return this.getToolsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiTool[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformTools(client));\n }\n return allTools;\n }\n return this.transformTools(this.client as MCPClient);\n }\n\n /**\n * Get tool definitions in JSON Schema format for passing to remote agents.\n */\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\n if (this.options.toolRouter) {\n return this.getToolDefinitionsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiToolDefinition[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformToolDefinitions(client));\n }\n return allTools;\n }\n return this.transformToolDefinitions(this.client as MCPClient);\n }\n\n /**\n * Get tools as a function (for dynamic loading).\n */\n getToolsFunction(): () => Promise<AguiTool[]> {\n return () => this.getTools();\n }\n\n private isMultiSession(): boolean {\n return typeof (this.client as any).getClients === 'function';\n }\n\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n // Type assertion to access _meta if it exists on the tool object (it comes from MCP SDK)\n const mcpTool = tool as any;\n const mcpToolName = tool.name;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n handler: async (args: any) => {\n // Call the actual MCP tool\n const callResult = await (client as any).callTool(mcpToolName, args ?? {});\n\n // Return the raw result object so middleware can inspect `_meta` (e.g. for UI triggers)\n return callResult;\n }\n }\n });\n }\n\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n const mcpTool = tool as any;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n };\n });\n }\n\n /**\n * Build AG-UI tools from a ToolRouter's filtered output.\n *\n * In `search` strategy, only meta-tools are registered with the framework.\n * Real tool execution is proxied through `mcp_execute_tool` which uses\n * `router.callTool()` to route to the correct MCP client.\n */\n private async getToolsViaRouter(router: ToolRouter): Promise<AguiTool[]> {\n const filteredTools = await router.getFilteredTools();\n\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n const namespace = routedTool.serverId ?? routedTool.sessionId;\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n handler: async (args: any) => {\n if (isMetaTool(tool.name)) {\n const result = await executeMetaTool(\n tool.name,\n args,\n router,\n (name, toolArgs, namespace) => router.callTool(name, toolArgs, namespace)\n );\n if (result) {\n return result.content.map((c: any) => c.text ?? '').join('\\n');\n }\n return \"Failed to execute meta-tool\";\n }\n\n // For non-meta tools in 'all' or 'groups' strategy,\n // route directly to the correct MCP client\n return await router.callTool(tool.name, args, namespace);\n }\n };\n });\n }\n\n private async getToolDefinitionsViaRouter(router: ToolRouter): Promise<AguiToolDefinition[]> {\n const filteredTools = await router.getFilteredTools();\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema)\n };\n });\n }\n\n private getRouterToolKey(toolName: string, sessionId?: string, serverId?: string): string {\n const namespace = sessionId ?? serverId ?? 'mcp';\n const normalized = namespace\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '_')\n .replace(/^_+|_+$/g, '') || 'mcp';\n return `tool_${normalized}_${toolName}`;\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/shared/meta-tools.ts","../../src/adapters/agui-adapter.ts"],"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CP tools on-demand, following Anthropic's Tool Search pattern.\n *\n * Instead of injecting 50+ full tool schemas into the context window, you\n * inject just these 4 meta-tools. The LLM calls them to find and load\n * only the tools it actually needs.\n *\n * Meta-tools:\n * β€’ `mcp_search_tool_bm25` β€” BM25 natural language search\n * β€’ `mcp_search_tool_regex` β€” Regex pattern search\n * β€’ `mcp_get_tool_schema` β€” Get full inputSchema for a discovered tool\n * β€’ `mcp_execute_tool` β€” Execute a discovered tool\n *\n * @packageDocumentation\n */\n\nimport type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';\nimport type { ToolRouter } from './tool-router.js';\nimport type { IndexedTool } from './tool-index.js';\n\n// ---------------------------------------------------------------------------\n// Tool Definitions\n// ---------------------------------------------------------------------------\n\n/**\n * Creates the `mcp_search_tool_bm25` tool definition.\n *\n * This tool lets the LLM search the full catalog of available MCP tools\n * using a BM25 natural-language query. Returns tool names and descriptions\n * without the full inputSchema to save context space.\n */\nexport function createSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_bm25',\n description:\n 'Search the catalog of available tools. Returns tool names, descriptions, and server info. ' +\n 'Use this FIRST to find relevant tools before calling them.\\n\\n' +\n 'Query forms:\\n' +\n '- \"select:Read,Edit,Grep\" β€” fetch these exact tools by name\\n' +\n '- \"notebook jupyter\" β€” keyword search, up to limit best matches\\n' +\n '- \"+slack send\" β€” require \"slack\" in the name, rank by remaining terms',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Query to find tools. Use \"select:<tool_name>\" for direct selection, or keywords to search. Prefix keywords with + to require them.',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_search_tool_regex` tool definition.\n * \n * Matches Anthropic's tool_search_tool_regex exactly (takes a 'query' regex pattern).\n */\nexport function createRegexSearchToolDefinition(): Tool {\n return {\n name: 'mcp_search_tool_regex',\n description:\n 'Search the catalog of available tools using a Python-style regex pattern. ' +\n 'Matches against tool names, descriptions, and parameter descriptions. ' +\n 'Example patterns: \"^github_\", \"weather\", \"(?i)slack\".',\n inputSchema: {\n type: 'object' as const,\n properties: {\n query: {\n type: 'string',\n description: 'Regex pattern to search for (e.g., \"^get_.*_data\", \"database\").',\n },\n limit: {\n type: 'number',\n description: 'Maximum number of results to return (default: 5, max: 20).',\n },\n },\n required: ['query'],\n },\n };\n}\n\n/**\n * Creates the `mcp_get_tool_schema` tool definition.\n *\n * After discovering tools via `mcp_search_tool_bm25` or\n * `mcp_search_tool_regex`, the LLM calls this to load the full\n * inputSchema for a specific tool so it can construct the correct\n * arguments.\n */\nexport function createGetSchemaToolDefinition(): Tool {\n return {\n name: 'mcp_get_tool_schema',\n description:\n 'Get the full input schema (parameters) for a specific tool. ' +\n 'Call this after mcp_search_tool_bm25 to get the parameter details ' +\n 'needed to call a tool correctly. ' +\n 'Do NOT call the discovered tool directly; after reading the schema, call mcp_execute_tool.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name returned by mcp_search_tool_bm25.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n/**\n * Creates the `mcp_execute_tool` tool definition.\n *\n * This is the execution meta-tool β€” the LLM calls this to execute any\n * tool discovered via `mcp_search_tool_bm25` or `mcp_search_tool_regex`.\n * The LLM should first call `mcp_get_tool_schema` to know the correct\n * arguments.\n *\n * Instead of registering every real tool with the framework, we proxy\n * all execution through a single meta-tool.\n */\nexport function createExecuteToolDefinition(): Tool {\n return {\n name: 'mcp_execute_tool',\n description:\n 'Execute a tool that was discovered via mcp_search_tool_bm25. ' +\n 'You MUST call mcp_get_tool_schema first to know the correct parameters. ' +\n 'Pass the exact tool name and its arguments.',\n inputSchema: {\n type: 'object' as const,\n properties: {\n toolName: {\n type: 'string',\n description: 'The exact tool name from mcp_search_tool_bm25 results.',\n },\n serverId: {\n type: 'string',\n description:\n 'Optional: The server ID provided in mcp_search_tool_bm25. Required if multiple tools have the same name.',\n },\n args: {\n type: 'object',\n description:\n \"Arguments matching the tool's inputSchema. Omit or pass {} if the tool takes no parameters.\",\n additionalProperties: true,\n },\n },\n required: ['toolName'],\n },\n };\n}\n\n// ---------------------------------------------------------------------------\n// Meta-tool Executors\n// ---------------------------------------------------------------------------\n\n/**\n * Callback for executing a real MCP tool via the correct client.\n * Provided by adapters that wire up client routing.\n */\nexport type CallToolFn = (\n toolName: string,\n args: Record<string, unknown>,\n namespace?: string\n) => Promise<any>;\n\n/**\n * Execute a meta-tool call and return the result in MCP CallToolResult format.\n *\n * @param toolName - One of the meta-tool names (mcp_search_tool_bm25, mcp_search_tool_regex, etc.)\n * @param args - The arguments from the LLM's tool call\n * @param router - The ToolRouter to query\n * @param callToolFn - Optional callback for executing real tools (required for mcp_execute_tool)\n * @returns MCP-compatible CallToolResult, or null if this isn't a meta-tool\n */\nexport async function executeMetaTool(\n toolName: string,\n args: Record<string, unknown>,\n router: ToolRouter,\n callToolFn?: CallToolFn\n): Promise<CallToolResult | null> {\n const resolveToolSchema = (name: string, namespace?: string): { tool?: IndexedTool; error?: CallToolResult } => {\n try {\n return { tool: router.getToolSchema(name, namespace) };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n error: {\n content: [{ type: 'text', text: errorMessage }],\n isError: true,\n },\n };\n }\n };\n\n switch (toolName) {\n case 'mcp_search_tool_bm25': {\n const query = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n // Fast path: Check for select: prefix\n const selectMatch = query.match(/^select:(.+)$/i);\n if (selectMatch) {\n const requested = selectMatch[1]!\n .split(',')\n .map((s) => s.trim())\n .filter(Boolean);\n\n const found: any[] = [];\n const errors: string[] = [];\n \n for (const requestedToolName of requested) {\n const { tool, error } = resolveToolSchema(requestedToolName);\n if (error) {\n const errorMsg = error.content[0]?.type === 'text' ? error.content[0].text : 'Unknown error';\n errors.push(`- **${requestedToolName}**: ${errorMsg}`);\n } else if (tool) {\n found.push(tool);\n } else {\n errors.push(`- **${requestedToolName}**: Tool not found. Try searching with mcp_search_tool_bm25.`);\n }\n }\n\n const lines: string[] = [];\n\n if (found.length > 0) {\n lines.push(...found.map((t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n ${t.description}`\n ));\n }\n \n if (errors.length > 0) {\n if (lines.length > 0) lines.push(\"\"); // Add empty line spacing\n lines.push(\"Errors resolving some tools:\");\n lines.push(...errors);\n }\n\n const text = lines.length > 0 \n ? lines.join('\\n') \n : `No tools found matching select query: ${requested.join(', ')}`;\n\n return {\n content: [{ type: 'text', text }],\n isError: found.length === 0,\n };\n }\n\n const results = await router.searchTools(query, limit);\n\n const text = results.length === 0\n ? 'No tools found matching your query. Try different keywords.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_search_tool_regex': {\n const pattern = String(args.query ?? '');\n const limit = Math.min(Number(args.limit) || 5, 20);\n\n const results = await router.searchToolsRegex(pattern, limit);\n\n const text = results.length === 0\n ? 'No tools matched your regex pattern. Try a broader pattern.'\n : results\n .map(\n (t, i) =>\n `${i + 1}. **${t.name}** (server: ${t.serverName}, serverId: ${t.serverId})\\n` +\n ` ${t.description}\\n` +\n ` Estimated tokens: ${t.estimatedTokens}`\n )\n .join('\\n');\n\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n }\n\n case 'mcp_get_tool_schema': {\n const name = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const { tool, error } = resolveToolSchema(name, namespace);\n\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${name}\" not found. Use mcp_search_tool_bm25 to find available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n const schema = {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n executionInstructions: {\n nextTool: 'mcp_execute_tool',\n toolName: tool.name,\n serverId: tool.serverId,\n note:\n 'Do not call this discovered tool directly unless it was explicitly registered as a runtime tool. Execute it via mcp_execute_tool and pass these parameters inside args.',\n },\n };\n\n return {\n content: [{ type: 'text', text: JSON.stringify(schema, null, 2) }],\n isError: false,\n };\n }\n\n case 'mcp_execute_tool': {\n const targetToolName = String(args.toolName ?? '');\n const namespace = String(args.serverId ?? '') || undefined;\n const toolArgs = (args.args as Record<string, unknown>) ?? {};\n\n if (!targetToolName) {\n return {\n content: [{ type: 'text', text: 'Missing required parameter \"toolName\". Specify which tool to execute.' }],\n isError: true,\n };\n }\n\n // Verify the tool exists in our index\n const { tool, error } = resolveToolSchema(targetToolName, namespace);\n if (error) {\n return error;\n }\n\n if (!tool) {\n return {\n content: [\n {\n type: 'text',\n text: `Tool \"${targetToolName}\" not found. Use mcp_search_tool_bm25 to discover available tools first.`,\n },\n ],\n isError: true,\n };\n }\n\n if (!callToolFn) {\n return {\n content: [{ type: 'text', text: 'Tool execution is not available. No callToolFn was configured.' }],\n isError: true,\n };\n }\n\n try {\n const result = await callToolFn(targetToolName, toolArgs, namespace);\n\n // Normalize result to text\n if (result && typeof result === 'object' && 'content' in result) {\n // Already MCP CallToolResult format\n return result as CallToolResult;\n }\n\n const text = typeof result === 'string' ? result : JSON.stringify(result, null, 2);\n return {\n content: [{ type: 'text', text }],\n isError: false,\n };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : String(err);\n return {\n content: [{ type: 'text', text: `Tool execution failed: ${errorMessage}` }],\n isError: true,\n };\n }\n }\n\n default:\n return null;\n }\n}\n\n/** Check if a tool name is one of the meta-tools. */\nexport function isMetaTool(toolName: string): boolean {\n return (\n toolName === 'mcp_search_tool_bm25' ||\n toolName === 'mcp_search_tool_regex' ||\n toolName === 'mcp_get_tool_schema' ||\n toolName === 'mcp_execute_tool'\n );\n}\n\n/**\n * Unwraps a meta-tool proxy call (like mcp_execute_tool) to find the real target tool name and arguments.\n * Also automatically strips routing prefixes like tool_{serverId}_.\n * \n * Useful for frontend components that need to determine the actual tool being executed by an AI agent.\n */\nexport function resolveMetaToolProxy(\n toolName: string,\n args: Record<string, unknown> | null | undefined\n): { toolName: string; args: Record<string, unknown> } {\n // Unwrap mcp_execute_tool proxy arguments\n if (toolName === 'mcp_execute_tool') {\n const innerName = args?.toolName;\n const innerArgs = args?.args;\n return {\n toolName: typeof innerName === 'string' && innerName ? innerName : toolName,\n args: innerArgs && typeof innerArgs === 'object' && !Array.isArray(innerArgs)\n ? (innerArgs as Record<string, unknown>)\n : {},\n };\n }\n\n // Strip tool_<serverId>_ prefix used by AIAdapter\n const match = toolName.match(/(?:tool_[^_]+_)?(.+)$/);\n const resolvedName = match?.[1] ?? toolName;\n\n return { toolName: resolvedName, args: args ?? {} };\n}\n\n","/**\n * MCP Adapter for AG-UI Integration\n *\n * This adapter transforms MCP tools into formats compatible with AG-UI agents.\n * It provides tools with handlers for server-side execution and tool definitions\n * in JSON Schema format for passing to remote agents.\n *\n * @example\n * ```typescript\n * import { MultiSessionClient } from '@mcp-ts/sdk/server';\n * import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';\n * import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';\n * import { HttpAgent } from '@ag-ui/client';\n *\n * // Create MCP client\n * const mcpClient = new MultiSessionClient('user_123');\n * await mcpClient.connect();\n *\n * // Create adapter and get tools\n * const adapter = new AguiAdapter(mcpClient);\n * const tools = await adapter.getTools();\n *\n * // Use with AG-UI middleware\n * const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });\n * agent.use(createMcpMiddleware({ tools }));\n * ```\n */\n\nimport { MCPClient } from '../server/mcp/oauth-client.js';\nimport { MultiSessionClient } from '../server/mcp/multi-session-client.js';\nimport { ToolRouter } from '../shared/tool-router.js';\nimport { executeMetaTool, isMetaTool } from '../shared/meta-tools.js';\n\n/**\n * Extended JSON Schema properties that Pydantic's strict validation rejects.\n * These are valid JSON Schema extensions but not part of the core spec.\n */\nconst PYDANTIC_FORBIDDEN_PROPS = [\n // JSON Schema meta-properties\n '$schema', '$id', '$comment', '$defs', 'definitions',\n // Extended properties used by some MCP servers (e.g., Apify)\n 'prefill', 'examples', 'enumTitles', 'enumDescriptions',\n // Other common extensions\n 'deprecated', 'readOnly', 'writeOnly', 'contentMediaType', 'contentEncoding',\n];\n\n/**\n * Cleans a JSON Schema by removing meta-properties that cause issues with\n * strict Pydantic validation (e.g., Google ADK, LangGraph).\n *\n * @param schema - The JSON Schema to clean\n * @returns Cleaned schema without forbidden properties\n */\nexport function cleanSchema(schema: Record<string, any> | undefined): Record<string, any> {\n if (!schema) {\n return { type: 'object', properties: {} };\n }\n\n const cleaned = { ...schema };\n\n // Remove all forbidden properties\n for (const prop of PYDANTIC_FORBIDDEN_PROPS) {\n delete cleaned[prop];\n }\n\n // Recursively clean nested properties\n if (cleaned.properties && typeof cleaned.properties === 'object') {\n const cleanedProps: Record<string, any> = {};\n for (const [key, value] of Object.entries(cleaned.properties)) {\n if (typeof value === 'object' && value !== null) {\n cleanedProps[key] = cleanSchema(value as Record<string, any>);\n } else {\n cleanedProps[key] = value;\n }\n }\n cleaned.properties = cleanedProps;\n }\n\n // Clean items if it's an array schema\n if (cleaned.items && typeof cleaned.items === 'object') {\n cleaned.items = cleanSchema(cleaned.items);\n }\n\n // Clean additionalProperties if it's an object schema\n if (cleaned.additionalProperties && typeof cleaned.additionalProperties === 'object') {\n cleaned.additionalProperties = cleanSchema(cleaned.additionalProperties);\n }\n\n return cleaned;\n}\n\n/**\n * Configuration options for AguiAdapter\n */\nexport interface AguiAdapterOptions {\n /**\n * Prefix for tool names to avoid collision with other tools.\n * @default serverId or 'mcp'\n */\n prefix?: string;\n\n /**\n * Optional ToolRouter for intelligent tool selection.\n */\n toolRouter?: ToolRouter;\n}\n\n/**\n * AG-UI Tool with handler for server-side execution.\n */\nexport interface AguiTool {\n name: string;\n description: string;\n parameters?: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiTool\n handler?: (args: any) => any | Promise<any>;\n}\n\n/**\n * Tool definition format for passing to remote agents (without handler).\n */\nexport interface AguiToolDefinition {\n name: string;\n description: string;\n parameters: Record<string, any>;\n _meta?: Record<string, any>; // Add _meta to AguiToolDefinition\n}\n\n/**\n * Adapter that transforms MCP tools into AG-UI compatible formats.\n */\nexport class AguiAdapter {\n constructor(\n private client: MCPClient | MultiSessionClient,\n private options: AguiAdapterOptions = {}\n ) { }\n\n /**\n * Get tools with handlers for MCP tool execution.\n */\n async getTools(): Promise<AguiTool[]> {\n if (this.options.toolRouter) {\n return this.getToolsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiTool[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformTools(client));\n }\n return allTools;\n }\n return this.transformTools(this.client as MCPClient);\n }\n\n /**\n * Get tool definitions in JSON Schema format for passing to remote agents.\n */\n async getToolDefinitions(): Promise<AguiToolDefinition[]> {\n if (this.options.toolRouter) {\n return this.getToolDefinitionsViaRouter(this.options.toolRouter);\n }\n\n if (this.isMultiSession()) {\n const clients = (this.client as MultiSessionClient).getClients();\n const allTools: AguiToolDefinition[] = [];\n for (const client of clients) {\n allTools.push(...await this.transformToolDefinitions(client));\n }\n return allTools;\n }\n return this.transformToolDefinitions(this.client as MCPClient);\n }\n\n /**\n * Get tools as a function (for dynamic loading).\n */\n getToolsFunction(): () => Promise<AguiTool[]> {\n return () => this.getTools();\n }\n\n private isMultiSession(): boolean {\n return typeof (this.client as any).getClients === 'function';\n }\n\n private async transformTools(client: MCPClient): Promise<AguiTool[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n // Type assertion to access _meta if it exists on the tool object (it comes from MCP SDK)\n const mcpTool = tool as any;\n const mcpToolName = tool.name;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n handler: async (args: any) => {\n // Call the actual MCP tool\n const callResult = await (client as any).callTool(mcpToolName, args ?? {});\n\n // Return the raw result object so middleware can inspect `_meta` (e.g. for UI triggers)\n return callResult;\n }\n }\n });\n }\n\n private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {\n if (!client.isConnected()) return [];\n\n const result = await client.listTools();\n const serverId = (typeof (client as any).getServerId === 'function'\n ? (client as any).getServerId()\n : undefined) as string | undefined;\n const normalizedPrefix = this.options.prefix?.replace(/-/g, '') ?? serverId?.replace(/-/g, '').substring(0, 8) ?? 'mcp';\n const prefix = `tool_${normalizedPrefix}`;\n\n return result.tools.map(tool => {\n const mcpTool = tool as any;\n return {\n name: `${prefix}_${tool.name}`,\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n _meta: { ...mcpTool._meta, sessionId: (client as any).getSessionId?.() },\n };\n });\n }\n\n /**\n * Build AG-UI tools from a ToolRouter's filtered output.\n *\n * In `search` strategy, only meta-tools are registered with the framework.\n * Real tool execution is proxied through `mcp_execute_tool` which uses\n * `router.callTool()` to route to the correct MCP client.\n */\n private async getToolsViaRouter(router: ToolRouter): Promise<AguiTool[]> {\n const filteredTools = await router.getFilteredTools();\n\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n const namespace = routedTool.serverId ?? routedTool.sessionId;\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema),\n handler: async (args: any) => {\n if (isMetaTool(tool.name)) {\n const result = await executeMetaTool(\n tool.name,\n args,\n router,\n (name, toolArgs, namespace) => router.callTool(name, toolArgs, namespace)\n );\n if (result) {\n return result.content.map((c: any) => c.text ?? '').join('\\n');\n }\n return \"Failed to execute meta-tool\";\n }\n\n // For non-meta tools in 'all' or 'groups' strategy,\n // route directly to the correct MCP client\n return await router.callTool(tool.name, args, namespace);\n }\n };\n });\n }\n\n private async getToolDefinitionsViaRouter(router: ToolRouter): Promise<AguiToolDefinition[]> {\n const filteredTools = await router.getFilteredTools();\n return filteredTools.map(tool => {\n const routedTool = tool as typeof tool & { sessionId?: string; serverId?: string; serverName?: string };\n return {\n name: isMetaTool(tool.name)\n ? tool.name\n : this.getRouterToolKey(tool.name, routedTool.sessionId, routedTool.serverId),\n description: tool.description || `Execute ${tool.name}`,\n parameters: cleanSchema(tool.inputSchema)\n };\n });\n }\n\n private getRouterToolKey(toolName: string, sessionId?: string, serverId?: string): string {\n const namespace = sessionId ?? serverId ?? 'mcp';\n const normalized = namespace\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '_')\n .replace(/^_+|_+$/g, '') || 'mcp';\n return `tool_${normalized}_${toolName}`;\n }\n}\n"]}
@@ -36,9 +36,13 @@ declare class McpMiddleware extends Middleware {
36
36
  constructor(config: McpMiddlewareConfig);
37
37
  private isMcpTool;
38
38
  private parseArgs;
39
+ private normalizeArgsString;
39
40
  private executeTool;
40
41
  private generateId;
41
42
  private ensureIds;
43
+ private getSnapshotToolCalls;
44
+ private getResolvedToolCallIds;
45
+ private shouldFilterMessagesSnapshot;
42
46
  /** Process tool call events and update state */
43
47
  private handleToolCallEvent;
44
48
  /** Execute pending MCP tools and return results */