@pentoshi/clai 4.8.4 → 4.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/README.md +91 -4
  2. package/dist/agent/compaction-executor.d.ts +3 -5
  3. package/dist/agent/compaction-executor.js +165 -28
  4. package/dist/agent/compaction-executor.js.map +1 -1
  5. package/dist/agent/finalize-gate.js +0 -9
  6. package/dist/agent/finalize-gate.js.map +1 -1
  7. package/dist/agent/plan-mode-reminders.js +5 -5
  8. package/dist/agent/plan-mode-reminders.js.map +1 -1
  9. package/dist/agent/plan-tool.d.ts +1 -0
  10. package/dist/agent/plan-tool.js +50 -21
  11. package/dist/agent/plan-tool.js.map +1 -1
  12. package/dist/agent/reliability-policy.d.ts +9 -0
  13. package/dist/agent/reliability-policy.js +36 -1
  14. package/dist/agent/reliability-policy.js.map +1 -1
  15. package/dist/agent/runner.d.ts +4 -0
  16. package/dist/agent/runner.js +193 -116
  17. package/dist/agent/runner.js.map +1 -1
  18. package/dist/agent/task-analyzer.d.ts +8 -18
  19. package/dist/agent/task-analyzer.js +75 -182
  20. package/dist/agent/task-analyzer.js.map +1 -1
  21. package/dist/agent/task-evidence.js +10 -0
  22. package/dist/agent/task-evidence.js.map +1 -1
  23. package/dist/agent/tool-call-parser.js +14 -13
  24. package/dist/agent/tool-call-parser.js.map +1 -1
  25. package/dist/app/adapters/current-agent-adapter.d.ts +5 -1
  26. package/dist/app/adapters/current-agent-adapter.js +2 -1
  27. package/dist/app/adapters/current-agent-adapter.js.map +1 -1
  28. package/dist/app/commands/catalog.js +10 -0
  29. package/dist/app/commands/catalog.js.map +1 -1
  30. package/dist/app/commands/registry.js +1 -0
  31. package/dist/app/commands/registry.js.map +1 -1
  32. package/dist/app/controllers/session-compact-helper.js +3 -6
  33. package/dist/app/controllers/session-compact-helper.js.map +1 -1
  34. package/dist/app/ports/transcript-item.d.ts +4 -0
  35. package/dist/classic/app/ClassicApp.js +1 -1
  36. package/dist/classic/app/ClassicApp.js.map +1 -1
  37. package/dist/classic/app/action-handlers.js +4 -0
  38. package/dist/classic/app/action-handlers.js.map +1 -1
  39. package/dist/classic/app/app-wiring.js +1 -0
  40. package/dist/classic/app/app-wiring.js.map +1 -1
  41. package/dist/classic/app/wiring-lifecycle.js +1 -0
  42. package/dist/classic/app/wiring-lifecycle.js.map +1 -1
  43. package/dist/classic/blocks/compacted-lines.js +3 -0
  44. package/dist/classic/blocks/compacted-lines.js.map +1 -1
  45. package/dist/classic/blocks/thinking-lines.js +11 -2
  46. package/dist/classic/blocks/thinking-lines.js.map +1 -1
  47. package/dist/classic/bootstrap/start-classic.d.ts +1 -0
  48. package/dist/classic/bootstrap/start-classic.js +16 -0
  49. package/dist/classic/bootstrap/start-classic.js.map +1 -1
  50. package/dist/classic/chrome/Composer.d.ts +6 -0
  51. package/dist/classic/chrome/Composer.js +2 -3
  52. package/dist/classic/chrome/Composer.js.map +1 -1
  53. package/dist/classic/chrome/composer-controller.d.ts +6 -1
  54. package/dist/classic/chrome/composer-controller.js +18 -8
  55. package/dist/classic/chrome/composer-controller.js.map +1 -1
  56. package/dist/classic/chrome/editor-view.d.ts +2 -1
  57. package/dist/classic/chrome/editor-view.js +1 -1
  58. package/dist/classic/chrome/editor-view.js.map +1 -1
  59. package/dist/classic/chrome/status-rows.js +3 -2
  60. package/dist/classic/chrome/status-rows.js.map +1 -1
  61. package/dist/classic/panels/TextEditorPanel.d.ts +3 -0
  62. package/dist/classic/panels/TextEditorPanel.js +7 -0
  63. package/dist/classic/panels/TextEditorPanel.js.map +1 -0
  64. package/dist/classic/panels/panel-controller.js +28 -1
  65. package/dist/classic/panels/panel-controller.js.map +1 -1
  66. package/dist/classic/panels/panel-effect.d.ts +3 -0
  67. package/dist/classic/panels/panel-effect.js.map +1 -1
  68. package/dist/classic/panels/panel-effects.js +3 -0
  69. package/dist/classic/panels/panel-effects.js.map +1 -1
  70. package/dist/classic/panels/panel-host.js +3 -0
  71. package/dist/classic/panels/panel-host.js.map +1 -1
  72. package/dist/classic/panels/panel-types.d.ts +2 -0
  73. package/dist/classic/panels/secret-panel.d.ts +1 -1
  74. package/dist/classic/panels/secret-panel.js +4 -2
  75. package/dist/classic/panels/secret-panel.js.map +1 -1
  76. package/dist/classic/panels/text-editor-panel.d.ts +33 -0
  77. package/dist/classic/panels/text-editor-panel.js +101 -0
  78. package/dist/classic/panels/text-editor-panel.js.map +1 -0
  79. package/dist/index.js +60 -7
  80. package/dist/index.js.map +1 -1
  81. package/dist/interactive-session/transport-node-pty.d.ts +10 -1
  82. package/dist/interactive-session/transport-node-pty.js +36 -12
  83. package/dist/interactive-session/transport-node-pty.js.map +1 -1
  84. package/dist/llm/adapters/anthropic-tools.d.ts +2 -1
  85. package/dist/llm/adapters/anthropic-tools.js +55 -4
  86. package/dist/llm/adapters/anthropic-tools.js.map +1 -1
  87. package/dist/llm/anthropic.js +1 -1
  88. package/dist/llm/anthropic.js.map +1 -1
  89. package/dist/llm/aws-mantle.js +2 -0
  90. package/dist/llm/aws-mantle.js.map +1 -1
  91. package/dist/llm/meta.js +62 -68
  92. package/dist/llm/meta.js.map +1 -1
  93. package/dist/llm/operation-ledger.js +6 -2
  94. package/dist/llm/operation-ledger.js.map +1 -1
  95. package/dist/llm/provider.js +7 -7
  96. package/dist/llm/request-plan.js +7 -2
  97. package/dist/llm/request-plan.js.map +1 -1
  98. package/dist/llm/tool-protocol.d.ts +1 -0
  99. package/dist/llm/tool-protocol.js +7 -0
  100. package/dist/llm/tool-protocol.js.map +1 -1
  101. package/dist/mcp/client.d.ts +27 -0
  102. package/dist/mcp/client.js +119 -0
  103. package/dist/mcp/client.js.map +1 -0
  104. package/dist/mcp/config-file.d.ts +35 -0
  105. package/dist/mcp/config-file.js +217 -0
  106. package/dist/mcp/config-file.js.map +1 -0
  107. package/dist/mcp/discovery.d.ts +4 -0
  108. package/dist/mcp/discovery.js +409 -0
  109. package/dist/mcp/discovery.js.map +1 -0
  110. package/dist/mcp/format.d.ts +17 -0
  111. package/dist/mcp/format.js +122 -0
  112. package/dist/mcp/format.js.map +1 -0
  113. package/dist/mcp/index.d.ts +15 -0
  114. package/dist/mcp/index.js +16 -0
  115. package/dist/mcp/index.js.map +1 -0
  116. package/dist/mcp/jsonrpc.d.ts +40 -0
  117. package/dist/mcp/jsonrpc.js +175 -0
  118. package/dist/mcp/jsonrpc.js.map +1 -0
  119. package/dist/mcp/manager.d.ts +36 -0
  120. package/dist/mcp/manager.js +300 -0
  121. package/dist/mcp/manager.js.map +1 -0
  122. package/dist/mcp/mentions.d.ts +9 -0
  123. package/dist/mcp/mentions.js +44 -0
  124. package/dist/mcp/mentions.js.map +1 -0
  125. package/dist/mcp/names.d.ts +15 -0
  126. package/dist/mcp/names.js +59 -0
  127. package/dist/mcp/names.js.map +1 -0
  128. package/dist/mcp/results.d.ts +6 -0
  129. package/dist/mcp/results.js +182 -0
  130. package/dist/mcp/results.js.map +1 -0
  131. package/dist/mcp/runtime.d.ts +107 -0
  132. package/dist/mcp/runtime.js +505 -0
  133. package/dist/mcp/runtime.js.map +1 -0
  134. package/dist/mcp/substitution.d.ts +13 -0
  135. package/dist/mcp/substitution.js +74 -0
  136. package/dist/mcp/substitution.js.map +1 -0
  137. package/dist/mcp/transport-http.d.ts +53 -0
  138. package/dist/mcp/transport-http.js +433 -0
  139. package/dist/mcp/transport-http.js.map +1 -0
  140. package/dist/mcp/transport-stdio.d.ts +39 -0
  141. package/dist/mcp/transport-stdio.js +268 -0
  142. package/dist/mcp/transport-stdio.js.map +1 -0
  143. package/dist/mcp/transport.d.ts +20 -0
  144. package/dist/mcp/transport.js +44 -0
  145. package/dist/mcp/transport.js.map +1 -0
  146. package/dist/mcp/types.d.ts +179 -0
  147. package/dist/mcp/types.js +30 -0
  148. package/dist/mcp/types.js.map +1 -0
  149. package/dist/mcp/validation.d.ts +16 -0
  150. package/dist/mcp/validation.js +222 -0
  151. package/dist/mcp/validation.js.map +1 -0
  152. package/dist/modes/agent.d.ts +2 -0
  153. package/dist/modes/agent.js.map +1 -1
  154. package/dist/noninteractive/start-noninteractive.js +4 -0
  155. package/dist/noninteractive/start-noninteractive.js.map +1 -1
  156. package/dist/prompts/embedded.js +2 -2
  157. package/dist/prompts/embedded.js.map +1 -1
  158. package/dist/prompts/index.d.ts +3 -0
  159. package/dist/prompts/index.js +70 -55
  160. package/dist/prompts/index.js.map +1 -1
  161. package/dist/prompts/system.agent.md +34 -10
  162. package/dist/prompts/system.ask.md +8 -0
  163. package/dist/safety/classifier.js +4 -0
  164. package/dist/safety/classifier.js.map +1 -1
  165. package/dist/session-runtime/binding.d.ts +5 -0
  166. package/dist/session-runtime/binding.js +28 -0
  167. package/dist/session-runtime/binding.js.map +1 -0
  168. package/dist/session-runtime/child-bridge.d.ts +31 -0
  169. package/dist/session-runtime/child-bridge.js +169 -0
  170. package/dist/session-runtime/child-bridge.js.map +1 -0
  171. package/dist/session-runtime/client.d.ts +8 -0
  172. package/dist/session-runtime/client.js +448 -0
  173. package/dist/session-runtime/client.js.map +1 -0
  174. package/dist/session-runtime/discovery.d.ts +6 -0
  175. package/dist/session-runtime/discovery.js +100 -0
  176. package/dist/session-runtime/discovery.js.map +1 -0
  177. package/dist/session-runtime/host.d.ts +77 -0
  178. package/dist/session-runtime/host.js +758 -0
  179. package/dist/session-runtime/host.js.map +1 -0
  180. package/dist/session-runtime/launch.d.ts +11 -0
  181. package/dist/session-runtime/launch.js +68 -0
  182. package/dist/session-runtime/launch.js.map +1 -0
  183. package/dist/session-runtime/paths.d.ts +8 -0
  184. package/dist/session-runtime/paths.js +92 -0
  185. package/dist/session-runtime/paths.js.map +1 -0
  186. package/dist/session-runtime/protocol.d.ts +22 -0
  187. package/dist/session-runtime/protocol.js +149 -0
  188. package/dist/session-runtime/protocol.js.map +1 -0
  189. package/dist/session-runtime/replay-buffer.d.ts +12 -0
  190. package/dist/session-runtime/replay-buffer.js +50 -0
  191. package/dist/session-runtime/replay-buffer.js.map +1 -0
  192. package/dist/session-runtime/store.d.ts +14 -0
  193. package/dist/session-runtime/store.js +143 -0
  194. package/dist/session-runtime/store.js.map +1 -0
  195. package/dist/session-runtime/types.d.ts +102 -0
  196. package/dist/session-runtime/types.js +2 -0
  197. package/dist/session-runtime/types.js.map +1 -0
  198. package/dist/store/logs.js +17 -6
  199. package/dist/store/logs.js.map +1 -1
  200. package/dist/store/plan.d.ts +2 -0
  201. package/dist/store/plan.js.map +1 -1
  202. package/dist/store/session-workspace.js +2 -1
  203. package/dist/store/session-workspace.js.map +1 -1
  204. package/dist/tools/definitions.js +18 -0
  205. package/dist/tools/definitions.js.map +1 -1
  206. package/dist/tools/external-tools.d.ts +30 -0
  207. package/dist/tools/external-tools.js +52 -0
  208. package/dist/tools/external-tools.js.map +1 -0
  209. package/dist/tools/registry.js +36 -5
  210. package/dist/tools/registry.js.map +1 -1
  211. package/dist/tui-v2/bootstrap/start-tui-v2.d.ts +1 -0
  212. package/dist/tui-v2/bootstrap/start-tui-v2.js +16 -0
  213. package/dist/tui-v2/bootstrap/start-tui-v2.js.map +1 -1
  214. package/dist/tui-v2/components/completion/completion-menu.d.ts +4 -6
  215. package/dist/tui-v2/components/completion/completion-menu.js +34 -16
  216. package/dist/tui-v2/components/completion/completion-menu.js.map +1 -1
  217. package/dist/tui-v2/components/composer/composer-input-box.js +3 -1
  218. package/dist/tui-v2/components/composer/composer-input-box.js.map +1 -1
  219. package/dist/tui-v2/components/modal/secret-modal.js +6 -4
  220. package/dist/tui-v2/components/modal/secret-modal.js.map +1 -1
  221. package/dist/tui-v2/components/modal/text-editor-modal.d.ts +19 -0
  222. package/dist/tui-v2/components/modal/text-editor-modal.js +85 -0
  223. package/dist/tui-v2/components/modal/text-editor-modal.js.map +1 -0
  224. package/dist/tui-v2/components/overlay/overlay-host.js +2 -1
  225. package/dist/tui-v2/components/overlay/overlay-host.js.map +1 -1
  226. package/dist/tui-v2/components/transcript/compacted-row.js +12 -3
  227. package/dist/tui-v2/components/transcript/compacted-row.js.map +1 -1
  228. package/dist/tui-v2/components/transcript/thinking-block.d.ts +9 -1
  229. package/dist/tui-v2/components/transcript/thinking-block.js +253 -49
  230. package/dist/tui-v2/components/transcript/thinking-block.js.map +1 -1
  231. package/dist/tui-v2/components/transcript/thinking-presentation.d.ts +46 -0
  232. package/dist/tui-v2/components/transcript/thinking-presentation.js +84 -0
  233. package/dist/tui-v2/components/transcript/thinking-presentation.js.map +1 -0
  234. package/dist/tui-v2/components/transcript/transcript-row.d.ts +2 -0
  235. package/dist/tui-v2/components/transcript/transcript-row.js +7 -2
  236. package/dist/tui-v2/components/transcript/transcript-row.js.map +1 -1
  237. package/dist/tui-v2/components/transcript/transcript-view.js +195 -83
  238. package/dist/tui-v2/components/transcript/transcript-view.js.map +1 -1
  239. package/dist/tui-v2/composer/composer-editor.js +54 -39
  240. package/dist/tui-v2/composer/composer-editor.js.map +1 -1
  241. package/dist/tui-v2/composer/composer-highlight.d.ts +20 -0
  242. package/dist/tui-v2/composer/composer-highlight.js +97 -0
  243. package/dist/tui-v2/composer/composer-highlight.js.map +1 -0
  244. package/dist/tui-v2/composer/textarea-keybindings.d.ts +5 -0
  245. package/dist/tui-v2/composer/textarea-keybindings.js +12 -0
  246. package/dist/tui-v2/composer/textarea-keybindings.js.map +1 -1
  247. package/dist/ui/mentions.js +4 -1
  248. package/dist/ui/mentions.js.map +1 -1
  249. package/dist/ui-core/actions/action-id.d.ts +1 -1
  250. package/dist/ui-core/actions/action-id.js +1 -0
  251. package/dist/ui-core/actions/action-id.js.map +1 -1
  252. package/dist/ui-core/actions/format-shortcuts.js +5 -0
  253. package/dist/ui-core/actions/format-shortcuts.js.map +1 -1
  254. package/dist/ui-core/actions/keymap.js +3 -0
  255. package/dist/ui-core/actions/keymap.js.map +1 -1
  256. package/dist/ui-core/bootstrap/composition-root.d.ts +7 -0
  257. package/dist/ui-core/bootstrap/composition-root.js +7 -1
  258. package/dist/ui-core/bootstrap/composition-root.js.map +1 -1
  259. package/dist/ui-core/commands/command-handlers.js +5 -2
  260. package/dist/ui-core/commands/command-handlers.js.map +1 -1
  261. package/dist/ui-core/commands/config-commands.d.ts +1 -0
  262. package/dist/ui-core/commands/config-commands.js +6 -0
  263. package/dist/ui-core/commands/config-commands.js.map +1 -1
  264. package/dist/ui-core/commands/mcp-commands.d.ts +3 -0
  265. package/dist/ui-core/commands/mcp-commands.js +310 -0
  266. package/dist/ui-core/commands/mcp-commands.js.map +1 -0
  267. package/dist/ui-core/commands/picker-commands.d.ts +1 -1
  268. package/dist/ui-core/commands/picker-commands.js +70 -27
  269. package/dist/ui-core/commands/picker-commands.js.map +1 -1
  270. package/dist/ui-core/commands/session-commands.js +1 -0
  271. package/dist/ui-core/commands/session-commands.js.map +1 -1
  272. package/dist/ui-core/composer/completion-viewport.d.ts +40 -0
  273. package/dist/ui-core/composer/completion-viewport.js +105 -0
  274. package/dist/ui-core/composer/completion-viewport.js.map +1 -0
  275. package/dist/ui-core/controllers/overlay-controller.d.ts +24 -3
  276. package/dist/ui-core/controllers/overlay-controller.js +35 -4
  277. package/dist/ui-core/controllers/overlay-controller.js.map +1 -1
  278. package/dist/ui-core/rendering/artifact-pager-source.d.ts +1 -0
  279. package/dist/ui-core/rendering/artifact-pager-source.js +67 -1
  280. package/dist/ui-core/rendering/artifact-pager-source.js.map +1 -1
  281. package/dist/ui-core/rendering/duration.d.ts +3 -1
  282. package/dist/ui-core/rendering/duration.js +14 -0
  283. package/dist/ui-core/rendering/duration.js.map +1 -1
  284. package/dist/ui-core/rendering/exit-summary.js +4 -4
  285. package/dist/ui-core/rendering/format-help.js +4 -0
  286. package/dist/ui-core/rendering/format-help.js.map +1 -1
  287. package/dist/ui-core/rendering/intro-header.js +4 -4
  288. package/dist/ui-core/rendering/open-tool-output.js +90 -32
  289. package/dist/ui-core/rendering/open-tool-output.js.map +1 -1
  290. package/dist/ui-core/rendering/theme.d.ts +5 -3
  291. package/dist/ui-core/rendering/theme.js +8 -4
  292. package/dist/ui-core/rendering/theme.js.map +1 -1
  293. package/dist/ui-core/rendering/wordmark.d.ts +2 -2
  294. package/dist/ui-core/rendering/wordmark.js +62 -62
  295. package/dist/ui-core/rendering/wordmark.js.map +1 -1
  296. package/dist/ui-core/state/thinking-copy.d.ts +13 -0
  297. package/dist/ui-core/state/thinking-copy.js +33 -0
  298. package/dist/ui-core/state/thinking-copy.js.map +1 -0
  299. package/dist/ui-core/state/transcript-hydrate.js +20 -0
  300. package/dist/ui-core/state/transcript-hydrate.js.map +1 -1
  301. package/dist/ui-core/state/transcript-reducer.js +140 -62
  302. package/dist/ui-core/state/transcript-reducer.js.map +1 -1
  303. package/dist/ui-core/state/transcript-store.d.ts +10 -0
  304. package/dist/ui-core/state/transcript-store.js +235 -31
  305. package/dist/ui-core/state/transcript-store.js.map +1 -1
  306. package/dist/ui-core/state/transcript-types.d.ts +10 -0
  307. package/dist/ui-core/state/transcript-types.js +1 -0
  308. package/dist/ui-core/state/transcript-types.js.map +1 -1
  309. package/dist/ui-core/state/transcript-window.d.ts +32 -0
  310. package/dist/ui-core/state/transcript-window.js +63 -0
  311. package/dist/ui-core/state/transcript-window.js.map +1 -0
  312. package/dist/version.generated.d.ts +2 -2
  313. package/dist/version.generated.js +2 -2
  314. package/dist/version.generated.js.map +1 -1
  315. package/package.json +1 -1
  316. package/dist/tui-v2/composer/skill-highlight.d.ts +0 -3
  317. package/dist/tui-v2/composer/skill-highlight.js +0 -63
  318. package/dist/tui-v2/composer/skill-highlight.js.map +0 -1
@@ -0,0 +1,300 @@
1
+ import { discoverMcpServers } from "./discovery.js";
2
+ import { McpClient } from "./client.js";
3
+ import { redactSecrets } from "./format.js";
4
+ import { toToolMetadata } from "./results.js";
5
+ import { allocateWireNames, canonicalToolName, toolIdentity, } from "./names.js";
6
+ import { McpTransportError } from "./transport.js";
7
+ import { StdioTransport } from "./transport-stdio.js";
8
+ import { LegacySseTransport, StreamableHttpTransport, } from "./transport-http.js";
9
+ const DEFAULT_CONNECT_CONCURRENCY = 4;
10
+ const DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
11
+ const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
12
+ async function runBounded(items, limit, worker) {
13
+ if (items.length === 0)
14
+ return;
15
+ let index = 0;
16
+ const size = Math.max(1, Math.min(limit, items.length));
17
+ const runners = Array.from({ length: size }, async () => {
18
+ while (index < items.length) {
19
+ const current = items[index++];
20
+ await worker(current);
21
+ }
22
+ });
23
+ await Promise.all(runners);
24
+ }
25
+ export class McpManager {
26
+ options;
27
+ connections = new Map();
28
+ discovery = {
29
+ servers: [],
30
+ shadowed: [],
31
+ invalid: [],
32
+ sources: [],
33
+ warnings: [],
34
+ };
35
+ constructor(options = {}) {
36
+ this.options = options;
37
+ }
38
+ get connectTimeoutMs() {
39
+ return this.options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
40
+ }
41
+ get requestTimeoutMs() {
42
+ return this.options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
43
+ }
44
+ createTransport(definition) {
45
+ if (this.options.transportFactory)
46
+ return this.options.transportFactory(definition);
47
+ const config = definition.config;
48
+ if (config.transport === "stdio") {
49
+ return new StdioTransport(config, { requestTimeoutMs: this.requestTimeoutMs });
50
+ }
51
+ const httpOptions = { requestTimeoutMs: this.requestTimeoutMs };
52
+ if (config.transport === "sse")
53
+ return new LegacySseTransport(config, httpOptions);
54
+ return new StreamableHttpTransport(config, httpOptions);
55
+ }
56
+ getDiscovery() {
57
+ return this.discovery;
58
+ }
59
+ async refresh(options = {}) {
60
+ this.discovery = discoverMcpServers(this.options.discovery ?? {});
61
+ const next = new Map();
62
+ for (const definition of this.discovery.servers)
63
+ next.set(definition.name, definition);
64
+ for (const [name, state] of [...this.connections]) {
65
+ if (!next.has(name)) {
66
+ await this.disposeConnection(state);
67
+ this.connections.delete(name);
68
+ }
69
+ }
70
+ const toConnect = [];
71
+ for (const definition of this.discovery.servers) {
72
+ if (definition.disabled) {
73
+ const existing = this.connections.get(definition.name);
74
+ if (existing)
75
+ await this.disposeConnection(existing);
76
+ this.connections.set(definition.name, {
77
+ definition,
78
+ client: undefined,
79
+ status: "disabled",
80
+ tools: [],
81
+ detail: "disabled by configuration",
82
+ serverInfo: undefined,
83
+ protocolVersion: undefined,
84
+ });
85
+ continue;
86
+ }
87
+ const existing = this.connections.get(definition.name);
88
+ const reusable = existing !== undefined &&
89
+ existing.status === "ready" &&
90
+ existing.definition.signature === definition.signature &&
91
+ options.force !== true;
92
+ if (reusable) {
93
+ existing.definition = definition;
94
+ continue;
95
+ }
96
+ if (existing)
97
+ await this.disposeConnection(existing);
98
+ this.connections.set(definition.name, {
99
+ definition,
100
+ client: undefined,
101
+ status: "connecting",
102
+ tools: [],
103
+ detail: undefined,
104
+ serverInfo: undefined,
105
+ protocolVersion: undefined,
106
+ });
107
+ toConnect.push(definition);
108
+ }
109
+ await runBounded(toConnect, this.options.connectConcurrency ?? DEFAULT_CONNECT_CONCURRENCY, (definition) => this.connect(definition));
110
+ return this.snapshot();
111
+ }
112
+ forceRefresh() {
113
+ return this.refresh({ force: true });
114
+ }
115
+ async reconnect(name) {
116
+ const definition = this.discovery.servers.find((server) => server.name === name) ??
117
+ this.connections.get(name)?.definition;
118
+ if (!definition)
119
+ return this.snapshot();
120
+ const existing = this.connections.get(name);
121
+ if (existing)
122
+ await this.disposeConnection(existing);
123
+ if (definition.disabled) {
124
+ this.connections.set(name, {
125
+ definition,
126
+ client: undefined,
127
+ status: "disabled",
128
+ tools: [],
129
+ detail: "disabled by configuration",
130
+ serverInfo: undefined,
131
+ protocolVersion: undefined,
132
+ });
133
+ return this.snapshot();
134
+ }
135
+ this.connections.set(name, {
136
+ definition,
137
+ client: undefined,
138
+ status: "connecting",
139
+ tools: [],
140
+ detail: undefined,
141
+ serverInfo: undefined,
142
+ protocolVersion: undefined,
143
+ });
144
+ await this.connect(definition);
145
+ return this.snapshot();
146
+ }
147
+ async connect(definition) {
148
+ const state = this.connections.get(definition.name);
149
+ if (!state)
150
+ return;
151
+ let client;
152
+ try {
153
+ const transport = this.createTransport(definition);
154
+ client = new McpClient(transport, this.options.clientOptions ?? {});
155
+ const init = await client.initialize({ timeoutMs: this.connectTimeoutMs });
156
+ state.client = client;
157
+ state.serverInfo = init.serverInfo;
158
+ state.protocolVersion = init.protocolVersion;
159
+ try {
160
+ const descriptors = await client.listTools({ timeoutMs: this.connectTimeoutMs });
161
+ const selected = descriptors
162
+ .filter((descriptor) => definition.toolSelection === "all" ||
163
+ definition.toolSelection.includes(descriptor.name))
164
+ .filter((descriptor, index, list) => list.findIndex((candidate) => candidate.name === descriptor.name) === index)
165
+ .sort((a, b) => a.name.localeCompare(b.name));
166
+ const allocated = allocateWireNames(selected.map((descriptor) => ({
167
+ serverName: definition.name,
168
+ toolName: descriptor.name,
169
+ })));
170
+ state.tools = selected.map((descriptor) => toToolMetadata(definition.name, descriptor, allocated.get(toolIdentity(definition.name, descriptor.name))));
171
+ state.status = "ready";
172
+ state.detail = undefined;
173
+ }
174
+ catch (error) {
175
+ state.status = "degraded";
176
+ state.tools = [];
177
+ state.detail = describeError(error, definition.secretValues);
178
+ }
179
+ }
180
+ catch (error) {
181
+ state.status = "error";
182
+ state.tools = [];
183
+ state.detail = describeError(error, definition.secretValues);
184
+ if (client)
185
+ await client.close().catch(() => undefined);
186
+ state.client = undefined;
187
+ }
188
+ }
189
+ reconcileToolNames() {
190
+ const all = [...this.connections.values()].flatMap((state) => state.status === "ready"
191
+ ? state.tools.map((tool) => ({
192
+ serverName: tool.serverName,
193
+ toolName: tool.toolName,
194
+ }))
195
+ : []);
196
+ const allocated = allocateWireNames(all);
197
+ for (const state of this.connections.values()) {
198
+ state.tools = state.tools.map((tool) => ({
199
+ ...tool,
200
+ canonicalName: canonicalToolName(tool.serverName, tool.toolName),
201
+ wireName: allocated.get(toolIdentity(tool.serverName, tool.toolName)) ??
202
+ tool.wireName,
203
+ }));
204
+ }
205
+ }
206
+ async disposeConnection(state) {
207
+ const client = state.client;
208
+ state.client = undefined;
209
+ if (client)
210
+ await client.close().catch(() => undefined);
211
+ }
212
+ async closeAll() {
213
+ const states = [...this.connections.values()];
214
+ this.connections.clear();
215
+ await Promise.all(states.map((state) => this.disposeConnection(state)));
216
+ }
217
+ snapshot() {
218
+ this.reconcileToolNames();
219
+ const statuses = [];
220
+ const tools = [];
221
+ const byCanonical = new Map();
222
+ const byWire = new Map();
223
+ const ordered = [...this.connections.values()].sort((a, b) => a.definition.name.localeCompare(b.definition.name));
224
+ for (const state of ordered) {
225
+ statuses.push(this.toStatus(state));
226
+ if (state.status === "ready") {
227
+ for (const tool of state.tools) {
228
+ if (byCanonical.has(tool.canonicalName))
229
+ continue;
230
+ byCanonical.set(tool.canonicalName, tool);
231
+ byWire.set(tool.wireName, tool);
232
+ tools.push(tool);
233
+ }
234
+ }
235
+ }
236
+ const snapshot = {
237
+ createdAt: Date.now(),
238
+ statuses: Object.freeze(statuses),
239
+ tools: Object.freeze(tools),
240
+ toolsByCanonicalName: byCanonical,
241
+ toolsByWireName: byWire,
242
+ shadowed: this.discovery.shadowed,
243
+ invalid: this.discovery.invalid,
244
+ };
245
+ return Object.freeze(snapshot);
246
+ }
247
+ toStatus(state) {
248
+ const base = {
249
+ name: state.definition.name,
250
+ status: state.status,
251
+ transport: state.definition.config.transport,
252
+ source: state.definition.source,
253
+ toolCount: state.tools.length,
254
+ signature: state.definition.signature,
255
+ };
256
+ return {
257
+ ...base,
258
+ ...(state.detail !== undefined ? { detail: state.detail } : {}),
259
+ ...(state.serverInfo !== undefined ? { serverInfo: state.serverInfo } : {}),
260
+ ...(state.protocolVersion !== undefined ? { protocolVersion: state.protocolVersion } : {}),
261
+ };
262
+ }
263
+ getTool(name) {
264
+ this.reconcileToolNames();
265
+ for (const state of this.connections.values()) {
266
+ if (state.status !== "ready")
267
+ continue;
268
+ const found = state.tools.find((tool) => tool.canonicalName === name || tool.wireName === name);
269
+ if (found)
270
+ return found;
271
+ }
272
+ return undefined;
273
+ }
274
+ listTools() {
275
+ return this.snapshot().tools.slice();
276
+ }
277
+ async callTool(name, args, options = {}) {
278
+ const tool = this.getTool(name);
279
+ if (!tool) {
280
+ throw new McpTransportError("protocol", `Unknown MCP tool "${name}".`);
281
+ }
282
+ const state = this.connections.get(tool.serverName);
283
+ if (!state || !state.client || state.status !== "ready") {
284
+ throw new McpTransportError("closed", `MCP server "${tool.serverName}" is not ready.`);
285
+ }
286
+ return await state.client.callTool(tool.toolName, args, {
287
+ timeoutMs: this.requestTimeoutMs,
288
+ ...options,
289
+ });
290
+ }
291
+ }
292
+ function describeError(error, secretValues = []) {
293
+ const text = error instanceof McpTransportError
294
+ ? `${error.kind}: ${error.message}`
295
+ : error instanceof Error
296
+ ? error.message
297
+ : String(error);
298
+ return redactSecrets(text, secretValues);
299
+ }
300
+ //# sourceMappingURL=manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../src/mcp/manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAyB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,iBAAiB,EAAqB,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EACL,kBAAkB,EAClB,uBAAuB,GAExB,MAAM,qBAAqB,CAAC;AAgB7B,MAAM,2BAA2B,GAAG,CAAC,CAAC;AACtC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAC1C,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAuB1C,KAAK,UAAU,UAAU,CACvB,KAAmB,EACnB,KAAa,EACb,MAAkC;IAElC,IAAI,KAAK,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CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;YACxD,MAAM,IAAI,iBAAiB,CAAC,QAAQ,EAAE,eAAe,IAAI,CAAC,UAAU,iBAAiB,CAAC,CAAC;QACzF,CAAC;QACD,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE;YACtD,SAAS,EAAE,IAAI,CAAC,gBAAgB;YAChC,GAAG,OAAO;SACX,CAAC,CAAC;IACL,CAAC;CACF;AAED,SAAS,aAAa,CAAC,KAAc,EAAE,eAAkC,EAAE;IACzE,MAAM,IAAI,GACR,KAAK,YAAY,iBAAiB;QAChC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE;QACnC,CAAC,CAAC,KAAK,YAAY,KAAK;YACtB,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtB,OAAO,aAAa,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,9 @@
1
+ export interface McpMentionRange {
2
+ readonly start: number;
3
+ readonly end: number;
4
+ readonly name: string;
5
+ }
6
+ export declare function formatMcpToken(name: string): string;
7
+ export declare function hasMcpMentionSyntax(text: string): boolean;
8
+ export declare function findMcpMentions(text: string, known: ReadonlySet<string>): McpMentionRange[];
9
+ export declare function mcpMentionNames(text: string, known: ReadonlySet<string>): string[];
@@ -0,0 +1,44 @@
1
+ const TOKEN_RE = /(^|[^\w:/\\.-])@mcp:([A-Za-z0-9][A-Za-z0-9_.-]*)/g;
2
+ const TRAILING_PUNCTUATION = /[._-]+$/;
3
+ export function formatMcpToken(name) {
4
+ return `@mcp:${name}`;
5
+ }
6
+ export function hasMcpMentionSyntax(text) {
7
+ return text.includes("@mcp:");
8
+ }
9
+ function resolveName(raw, known) {
10
+ let candidate = raw;
11
+ while (candidate.length > 0) {
12
+ if (known.has(candidate))
13
+ return candidate;
14
+ const trimmed = candidate.replace(TRAILING_PUNCTUATION, "");
15
+ if (trimmed === candidate)
16
+ return undefined;
17
+ candidate = trimmed;
18
+ }
19
+ return undefined;
20
+ }
21
+ export function findMcpMentions(text, known) {
22
+ if (!text || known.size === 0 || !hasMcpMentionSyntax(text))
23
+ return [];
24
+ const out = [];
25
+ const pattern = new RegExp(TOKEN_RE.source, TOKEN_RE.flags);
26
+ let match;
27
+ while ((match = pattern.exec(text)) !== null) {
28
+ const lead = match[1] ?? "";
29
+ const raw = match[2] ?? "";
30
+ const name = resolveName(raw, known);
31
+ if (!name)
32
+ continue;
33
+ const start = match.index + lead.length;
34
+ out.push({ start, end: start + formatMcpToken(name).length, name });
35
+ }
36
+ return out;
37
+ }
38
+ export function mcpMentionNames(text, known) {
39
+ const seen = new Set();
40
+ for (const mention of findMcpMentions(text, known))
41
+ seen.add(mention.name);
42
+ return [...seen];
43
+ }
44
+ //# sourceMappingURL=mentions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mentions.js","sourceRoot":"","sources":["../../src/mcp/mentions.ts"],"names":[],"mappings":"AAMA,MAAM,QAAQ,GAAG,mDAAmD,CAAC;AACrE,MAAM,oBAAoB,GAAG,SAAS,CAAC;AAEvC,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,QAAQ,IAAI,EAAE,CAAC;AACxB,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,WAAW,CAAC,GAAW,EAAE,KAA0B;IAC1D,IAAI,SAAS,GAAG,GAAG,CAAC;IACpB,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5B,IAAI,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,OAAO,SAAS,CAAC;QAC3C,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC,oBAAoB,EA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AE,KAAK,CAAC;QAAE,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;AACnB,CAAC"}
@@ -0,0 +1,15 @@
1
+ export declare const MCP_CANONICAL_PREFIX: "mcp.";
2
+ export declare const MCP_WIRE_PREFIX: "mcp_";
3
+ export declare const MCP_WIRE_MAX_LENGTH = 64;
4
+ export declare function canonicalToolName(serverName: string, toolName: string): string;
5
+ export declare function isCanonicalToolName(value: string): boolean;
6
+ export declare function toolIdentity(serverName: string, toolName: string): string;
7
+ export interface WireNameInput {
8
+ readonly serverName: string;
9
+ readonly toolName: string;
10
+ }
11
+ export declare class WireNameAllocator {
12
+ private readonly used;
13
+ assign(input: WireNameInput): string;
14
+ }
15
+ export declare function allocateWireNames(inputs: readonly WireNameInput[]): Map<string, string>;
@@ -0,0 +1,59 @@
1
+ import { createHash } from "node:crypto";
2
+ export const MCP_CANONICAL_PREFIX = "mcp.";
3
+ export const MCP_WIRE_PREFIX = "mcp_";
4
+ export const MCP_WIRE_MAX_LENGTH = 64;
5
+ const MIN_HASH_LENGTH = 8;
6
+ const NON_WIRE_CHAR = /[^A-Za-z0-9]+/g;
7
+ const IDENTITY_SEPARATOR = "\u0000";
8
+ export function canonicalToolName(serverName, toolName) {
9
+ return `${MCP_CANONICAL_PREFIX}${serverName}.${toolName}`;
10
+ }
11
+ export function isCanonicalToolName(value) {
12
+ return value.startsWith(MCP_CANONICAL_PREFIX) && value.length > MCP_CANONICAL_PREFIX.length;
13
+ }
14
+ export function toolIdentity(serverName, toolName) {
15
+ return `${serverName}${IDENTITY_SEPARATOR}${toolName}`;
16
+ }
17
+ function sanitizeSegment(raw) {
18
+ const replaced = raw.replace(NON_WIRE_CHAR, "_").replace(/^_+|_+$/g, "");
19
+ const value = replaced.length > 0 ? replaced : "x";
20
+ return { value, normalized: value !== raw };
21
+ }
22
+ function hashFor(identity, length) {
23
+ return createHash("sha256").update(identity).digest("hex").slice(0, length);
24
+ }
25
+ function withHash(base, identity, length) {
26
+ const suffix = `_${hashFor(identity, length)}`;
27
+ const room = Math.max(MCP_WIRE_PREFIX.length, MCP_WIRE_MAX_LENGTH - suffix.length);
28
+ const head = base.length > room ? base.slice(0, room) : base;
29
+ return `${head}${suffix}`;
30
+ }
31
+ export class WireNameAllocator {
32
+ used = new Map();
33
+ assign(input) {
34
+ const identity = toolIdentity(input.serverName, input.toolName);
35
+ const server = sanitizeSegment(input.serverName);
36
+ const tool = sanitizeSegment(input.toolName);
37
+ const base = `${MCP_WIRE_PREFIX}${server.value}_${tool.value}`;
38
+ const collides = this.used.has(base) && this.used.get(base) !== identity;
39
+ const mustHash = server.normalized || tool.normalized || base.length > MCP_WIRE_MAX_LENGTH || collides;
40
+ let hashLength = MIN_HASH_LENGTH;
41
+ let candidate = mustHash ? withHash(base, identity, hashLength) : base;
42
+ while (this.used.has(candidate) && this.used.get(candidate) !== identity) {
43
+ hashLength += 4;
44
+ candidate = withHash(base, identity, hashLength);
45
+ }
46
+ this.used.set(candidate, identity);
47
+ return candidate;
48
+ }
49
+ }
50
+ export function allocateWireNames(inputs) {
51
+ const sorted = [...inputs].sort((a, b) => toolIdentity(a.serverName, a.toolName).localeCompare(toolIdentity(b.serverName, b.toolName)));
52
+ const allocator = new WireNameAllocator();
53
+ const byIdentity = new Map();
54
+ for (const input of sorted) {
55
+ byIdentity.set(toolIdentity(input.serverName, input.toolName), allocator.assign(input));
56
+ }
57
+ return byIdentity;
58
+ }
59
+ //# sourceMappingURL=names.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"names.js","sourceRoot":"","sources":["../../src/mcp/names.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAe,CAAC;AACpD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAe,CAAC;AAC/C,MAAM,CAAC,MAAM,mBAAmB,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G,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,QAAQ,EAAE,CAAC;YACzE,UAAU,IAAI,CAAC,CAAC;YAChB,SAAS,GAAG,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;QACnD,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACnC,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAED,MAAM,UAAU,iBAAiB,CAAC,MAAgC;IAChE,MAAM,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CACvC,YAAY,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAC7F,CAAC;IACF,MAAM,SAAS,GAAG,IAAI,iBAAiB,EAAE,CAAC;IAC1C,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC7C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1F,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { RiskLevel } from "../types.js";
2
+ import type { McpNormalizedResult, McpToolAnnotations, McpToolDescriptor, McpToolMetadata } from "./types.js";
3
+ export declare function deriveRisk(annotations: McpToolAnnotations): RiskLevel;
4
+ export declare function parseToolDescriptor(value: unknown): McpToolDescriptor | undefined;
5
+ export declare function toToolMetadata(serverName: string, descriptor: McpToolDescriptor, wireName: string): McpToolMetadata;
6
+ export declare function normalizeToolResult(raw: unknown): McpNormalizedResult;
@@ -0,0 +1,182 @@
1
+ import { canonicalToolName } from "./names.js";
2
+ function coerceSchema(schema) {
3
+ if (typeof schema === "object" &&
4
+ schema !== null &&
5
+ schema.type === "object") {
6
+ const record = schema;
7
+ const properties = typeof record.properties === "object" && record.properties !== null
8
+ ? record.properties
9
+ : {};
10
+ const required = Array.isArray(record.required)
11
+ ? record.required.filter((entry) => typeof entry === "string")
12
+ : undefined;
13
+ const additionalProperties = typeof record.additionalProperties === "boolean"
14
+ ? record.additionalProperties
15
+ : undefined;
16
+ return {
17
+ type: "object",
18
+ properties,
19
+ ...(required !== undefined ? { required } : {}),
20
+ ...(additionalProperties !== undefined ? { additionalProperties } : {}),
21
+ };
22
+ }
23
+ return { type: "object", properties: {} };
24
+ }
25
+ function readAnnotations(value) {
26
+ if (typeof value !== "object" || value === null)
27
+ return {};
28
+ const record = value;
29
+ const result = {};
30
+ if (typeof record.title === "string")
31
+ result.title = record.title;
32
+ if (typeof record.readOnlyHint === "boolean")
33
+ result.readOnlyHint = record.readOnlyHint;
34
+ if (typeof record.destructiveHint === "boolean")
35
+ result.destructiveHint = record.destructiveHint;
36
+ if (typeof record.idempotentHint === "boolean")
37
+ result.idempotentHint = record.idempotentHint;
38
+ if (typeof record.openWorldHint === "boolean")
39
+ result.openWorldHint = record.openWorldHint;
40
+ return result;
41
+ }
42
+ export function deriveRisk(annotations) {
43
+ if (annotations.readOnlyHint === true)
44
+ return "safe";
45
+ return "confirm";
46
+ }
47
+ export function parseToolDescriptor(value) {
48
+ if (typeof value !== "object" || value === null)
49
+ return undefined;
50
+ const record = value;
51
+ if (typeof record.name !== "string" || record.name.length === 0)
52
+ return undefined;
53
+ const descriptor = { name: record.name };
54
+ if (typeof record.title === "string")
55
+ descriptor.title = record.title;
56
+ if (typeof record.description === "string")
57
+ descriptor.description = record.description;
58
+ if (typeof record.inputSchema === "object" && record.inputSchema !== null) {
59
+ descriptor.inputSchema = record.inputSchema;
60
+ }
61
+ if ("annotations" in record)
62
+ descriptor.annotations = readAnnotations(record.annotations);
63
+ return descriptor;
64
+ }
65
+ export function toToolMetadata(serverName, descriptor, wireName) {
66
+ const annotations = descriptor.annotations ?? {};
67
+ const readOnly = annotations.readOnlyHint === true;
68
+ const destructive = readOnly ? false : annotations.destructiveHint ?? true;
69
+ const idempotent = annotations.idempotentHint ?? false;
70
+ const openWorld = annotations.openWorldHint ?? false;
71
+ const title = descriptor.title ?? annotations.title;
72
+ const base = {
73
+ canonicalName: canonicalToolName(serverName, descriptor.name),
74
+ wireName,
75
+ serverName,
76
+ toolName: descriptor.name,
77
+ description: descriptor.description ?? "",
78
+ inputSchema: coerceSchema(descriptor.inputSchema),
79
+ risk: deriveRisk(annotations),
80
+ readOnly,
81
+ destructive,
82
+ idempotent,
83
+ openWorld,
84
+ annotations,
85
+ };
86
+ return title !== undefined ? { ...base, title } : base;
87
+ }
88
+ function readContentImages(content) {
89
+ const images = [];
90
+ for (const entry of content) {
91
+ if (typeof entry !== "object" || entry === null)
92
+ continue;
93
+ const record = entry;
94
+ if (record.type !== "image")
95
+ continue;
96
+ const data = record.data;
97
+ const mimeType = record.mimeType;
98
+ if (typeof data === "string" && typeof mimeType === "string") {
99
+ images.push({ mediaType: mimeType, dataBase64: data });
100
+ }
101
+ }
102
+ return images;
103
+ }
104
+ function readContentResources(content) {
105
+ const resources = [];
106
+ for (const entry of content) {
107
+ if (typeof entry !== "object" || entry === null)
108
+ continue;
109
+ const record = entry;
110
+ if (record.type !== "resource")
111
+ continue;
112
+ const resource = record.resource;
113
+ if (typeof resource !== "object" || resource === null)
114
+ continue;
115
+ const res = resource;
116
+ const uri = typeof res.uri === "string" ? res.uri : "";
117
+ const normalized = { uri };
118
+ if (typeof res.mimeType === "string")
119
+ normalized.mimeType = res.mimeType;
120
+ if (typeof res.text === "string")
121
+ normalized.text = res.text;
122
+ if (typeof res.blob === "string")
123
+ normalized.blobBase64 = res.blob;
124
+ resources.push(normalized);
125
+ }
126
+ return resources;
127
+ }
128
+ function readContentText(content) {
129
+ const parts = [];
130
+ for (const entry of content) {
131
+ if (typeof entry !== "object" || entry === null)
132
+ continue;
133
+ const record = entry;
134
+ if (record.type === "text" && typeof record.text === "string") {
135
+ parts.push(record.text);
136
+ }
137
+ else if (record.type === "resource") {
138
+ const resource = record.resource;
139
+ if (resource && typeof resource.text === "string") {
140
+ const uri = typeof resource.uri === "string" ? resource.uri : "resource";
141
+ parts.push(`[${uri}]\n${resource.text}`);
142
+ }
143
+ else if (resource && typeof resource.uri === "string") {
144
+ parts.push(`[resource ${resource.uri}]`);
145
+ }
146
+ }
147
+ else if (record.type === "image") {
148
+ const mime = typeof record.mimeType === "string" ? record.mimeType : "image";
149
+ parts.push(`[image ${mime}]`);
150
+ }
151
+ else if (record.type === "audio") {
152
+ const mime = typeof record.mimeType === "string" ? record.mimeType : "audio";
153
+ parts.push(`[audio ${mime}]`);
154
+ }
155
+ }
156
+ return parts.join("\n");
157
+ }
158
+ export function normalizeToolResult(raw) {
159
+ const record = typeof raw === "object" && raw !== null ? raw : {};
160
+ const isError = record.isError === true;
161
+ const content = Array.isArray(record.content) ? record.content : [];
162
+ const images = readContentImages(content);
163
+ const resources = readContentResources(content);
164
+ const chatImages = images.map((image) => ({
165
+ mediaType: image.mediaType,
166
+ dataBase64: image.dataBase64,
167
+ }));
168
+ let text = readContentText(content);
169
+ if (text.length === 0 && typeof record.structuredContent === "object" && record.structuredContent !== null) {
170
+ text = JSON.stringify(record.structuredContent);
171
+ }
172
+ return {
173
+ ok: !isError,
174
+ isError,
175
+ text,
176
+ images,
177
+ resources,
178
+ chatImages,
179
+ raw,
180
+ };
181
+ }
182
+ //# sourceMappingURL=results.js.map