@dizzlkheinz/ynab-mcpb 0.18.4 → 0.19.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 (343) hide show
  1. package/CLAUDE.md +87 -8
  2. package/bin/ynab-mcp-server.cjs +2 -2
  3. package/bin/ynab-mcp-server.js +3 -3
  4. package/biome.json +39 -0
  5. package/dist/bundle/index.cjs +67 -67
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +27 -27
  8. package/dist/server/YNABMCPServer.d.ts +3 -4
  9. package/dist/server/YNABMCPServer.js +111 -116
  10. package/dist/server/budgetResolver.d.ts +6 -5
  11. package/dist/server/budgetResolver.js +46 -36
  12. package/dist/server/cacheKeys.js +6 -6
  13. package/dist/server/cacheManager.js +14 -11
  14. package/dist/server/completions.d.ts +2 -2
  15. package/dist/server/completions.js +20 -15
  16. package/dist/server/config.d.ts +10 -5
  17. package/dist/server/config.js +24 -7
  18. package/dist/server/deltaCache.d.ts +2 -2
  19. package/dist/server/deltaCache.js +22 -16
  20. package/dist/server/deltaCache.merge.d.ts +2 -2
  21. package/dist/server/diagnostics.d.ts +4 -4
  22. package/dist/server/diagnostics.js +38 -32
  23. package/dist/server/errorHandler.d.ts +5 -12
  24. package/dist/server/errorHandler.js +219 -217
  25. package/dist/server/prompts.d.ts +2 -2
  26. package/dist/server/prompts.js +45 -45
  27. package/dist/server/rateLimiter.js +4 -4
  28. package/dist/server/requestLogger.d.ts +1 -1
  29. package/dist/server/requestLogger.js +40 -35
  30. package/dist/server/resources.d.ts +3 -3
  31. package/dist/server/resources.js +55 -52
  32. package/dist/server/responseFormatter.js +6 -6
  33. package/dist/server/securityMiddleware.d.ts +2 -2
  34. package/dist/server/securityMiddleware.js +22 -20
  35. package/dist/server/serverKnowledgeStore.js +1 -1
  36. package/dist/server/toolRegistry.d.ts +3 -3
  37. package/dist/server/toolRegistry.js +47 -40
  38. package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
  39. package/dist/tools/__tests__/deltaTestUtils.js +2 -2
  40. package/dist/tools/accountTools.d.ts +9 -8
  41. package/dist/tools/accountTools.js +47 -47
  42. package/dist/tools/adapters.d.ts +13 -8
  43. package/dist/tools/adapters.js +21 -11
  44. package/dist/tools/budgetTools.d.ts +8 -7
  45. package/dist/tools/budgetTools.js +22 -22
  46. package/dist/tools/categoryTools.d.ts +9 -8
  47. package/dist/tools/categoryTools.js +68 -59
  48. package/dist/tools/compareTransactions/formatter.d.ts +3 -3
  49. package/dist/tools/compareTransactions/formatter.js +9 -9
  50. package/dist/tools/compareTransactions/index.d.ts +6 -6
  51. package/dist/tools/compareTransactions/index.js +58 -43
  52. package/dist/tools/compareTransactions/matcher.d.ts +1 -1
  53. package/dist/tools/compareTransactions/matcher.js +28 -15
  54. package/dist/tools/compareTransactions/parser.d.ts +2 -2
  55. package/dist/tools/compareTransactions/parser.js +144 -138
  56. package/dist/tools/compareTransactions/types.d.ts +4 -4
  57. package/dist/tools/compareTransactions.d.ts +1 -1
  58. package/dist/tools/compareTransactions.js +1 -1
  59. package/dist/tools/deltaFetcher.d.ts +2 -2
  60. package/dist/tools/deltaFetcher.js +16 -15
  61. package/dist/tools/deltaSupport.d.ts +4 -4
  62. package/dist/tools/deltaSupport.js +35 -41
  63. package/dist/tools/exportTransactions.d.ts +5 -4
  64. package/dist/tools/exportTransactions.js +61 -59
  65. package/dist/tools/monthTools.d.ts +7 -6
  66. package/dist/tools/monthTools.js +31 -29
  67. package/dist/tools/payeeTools.d.ts +7 -6
  68. package/dist/tools/payeeTools.js +28 -28
  69. package/dist/tools/reconcileAdapter.d.ts +2 -2
  70. package/dist/tools/reconcileAdapter.js +19 -12
  71. package/dist/tools/reconciliation/analyzer.d.ts +4 -4
  72. package/dist/tools/reconciliation/analyzer.js +73 -59
  73. package/dist/tools/reconciliation/csvParser.d.ts +3 -3
  74. package/dist/tools/reconciliation/csvParser.js +128 -104
  75. package/dist/tools/reconciliation/executor.d.ts +4 -4
  76. package/dist/tools/reconciliation/executor.js +148 -109
  77. package/dist/tools/reconciliation/index.d.ts +10 -10
  78. package/dist/tools/reconciliation/index.js +96 -83
  79. package/dist/tools/reconciliation/matcher.d.ts +3 -3
  80. package/dist/tools/reconciliation/matcher.js +17 -16
  81. package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
  82. package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
  83. package/dist/tools/reconciliation/recommendationEngine.js +40 -40
  84. package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
  85. package/dist/tools/reconciliation/reportFormatter.js +59 -58
  86. package/dist/tools/reconciliation/signDetector.d.ts +1 -1
  87. package/dist/tools/reconciliation/types.d.ts +16 -16
  88. package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
  89. package/dist/tools/schemas/common.d.ts +1 -1
  90. package/dist/tools/schemas/common.js +1 -1
  91. package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
  92. package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
  93. package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
  94. package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
  95. package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
  96. package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
  97. package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
  98. package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
  99. package/dist/tools/schemas/outputs/index.d.ts +14 -14
  100. package/dist/tools/schemas/outputs/index.js +14 -14
  101. package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
  102. package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
  103. package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
  104. package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
  105. package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
  106. package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
  107. package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
  108. package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
  109. package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
  110. package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
  111. package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
  112. package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
  113. package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
  114. package/dist/tools/schemas/shared/commonOutputs.js +15 -9
  115. package/dist/tools/transactionReadTools.d.ts +11 -0
  116. package/dist/tools/transactionReadTools.js +202 -0
  117. package/dist/tools/transactionSchemas.d.ts +7 -7
  118. package/dist/tools/transactionSchemas.js +77 -57
  119. package/dist/tools/transactionTools.d.ts +6 -24
  120. package/dist/tools/transactionTools.js +7 -1499
  121. package/dist/tools/transactionUtils.d.ts +6 -6
  122. package/dist/tools/transactionUtils.js +78 -63
  123. package/dist/tools/transactionWriteTools.d.ts +20 -0
  124. package/dist/tools/transactionWriteTools.js +1342 -0
  125. package/dist/tools/utilityTools.d.ts +5 -4
  126. package/dist/tools/utilityTools.js +11 -11
  127. package/dist/types/index.d.ts +7 -7
  128. package/dist/types/index.js +6 -6
  129. package/dist/types/reconciliation.d.ts +1 -1
  130. package/dist/types/toolRegistration.d.ts +14 -12
  131. package/dist/utils/amountUtils.js +1 -1
  132. package/dist/utils/dateUtils.js +4 -4
  133. package/dist/utils/errors.d.ts +3 -3
  134. package/dist/utils/errors.js +4 -4
  135. package/dist/utils/money.d.ts +2 -2
  136. package/dist/utils/money.js +8 -8
  137. package/dist/utils/validationError.d.ts +1 -1
  138. package/dist/utils/validationError.js +1 -1
  139. package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
  140. package/docs/assets/schemas/reconciliation-v2.json +360 -336
  141. package/esbuild.config.mjs +53 -50
  142. package/meta.json +12548 -12548
  143. package/package.json +98 -111
  144. package/scripts/analyze-bundle.mjs +33 -30
  145. package/scripts/create-pr-description.js +169 -120
  146. package/scripts/run-all-tests.js +178 -169
  147. package/scripts/run-domain-integration-tests.js +28 -18
  148. package/scripts/run-generate-mcpb.js +19 -17
  149. package/scripts/run-throttled-integration-tests.js +92 -83
  150. package/scripts/test-delta-params.mjs +149 -120
  151. package/scripts/test-recommendations.ts +36 -32
  152. package/scripts/tmpTransaction.ts +80 -43
  153. package/scripts/validate-env.js +98 -91
  154. package/scripts/verify-build.js +78 -76
  155. package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
  156. package/src/__tests__/performance.test.ts +723 -671
  157. package/src/__tests__/setup.ts +442 -395
  158. package/src/__tests__/smoke.e2e.test.ts +41 -39
  159. package/src/__tests__/testRunner.ts +314 -295
  160. package/src/__tests__/testUtils.ts +456 -364
  161. package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
  162. package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
  163. package/src/index.ts +68 -59
  164. package/src/server/CLAUDE.md +480 -0
  165. package/src/server/YNABMCPServer.ts +821 -794
  166. package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
  167. package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
  168. package/src/server/__tests__/budgetResolver.test.ts +466 -423
  169. package/src/server/__tests__/cacheManager.test.ts +891 -874
  170. package/src/server/__tests__/completions.integration.test.ts +115 -106
  171. package/src/server/__tests__/completions.test.ts +334 -313
  172. package/src/server/__tests__/config.test.ts +98 -86
  173. package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
  174. package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
  175. package/src/server/__tests__/deltaCache.test.ts +946 -759
  176. package/src/server/__tests__/diagnostics.test.ts +825 -792
  177. package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
  178. package/src/server/__tests__/errorHandler.test.ts +402 -397
  179. package/src/server/__tests__/prompts.test.ts +424 -347
  180. package/src/server/__tests__/rateLimiter.test.ts +313 -309
  181. package/src/server/__tests__/requestLogger.test.ts +443 -403
  182. package/src/server/__tests__/resources.template.test.ts +196 -185
  183. package/src/server/__tests__/resources.test.ts +294 -288
  184. package/src/server/__tests__/security.integration.test.ts +487 -421
  185. package/src/server/__tests__/securityMiddleware.test.ts +519 -444
  186. package/src/server/__tests__/server-startup.integration.test.ts +509 -490
  187. package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
  188. package/src/server/__tests__/toolRegistration.test.ts +239 -210
  189. package/src/server/__tests__/toolRegistry.test.ts +907 -845
  190. package/src/server/budgetResolver.ts +221 -181
  191. package/src/server/cacheKeys.ts +6 -6
  192. package/src/server/cacheManager.ts +498 -484
  193. package/src/server/completions.ts +267 -243
  194. package/src/server/config.ts +35 -14
  195. package/src/server/deltaCache.merge.ts +146 -128
  196. package/src/server/deltaCache.ts +352 -309
  197. package/src/server/diagnostics.ts +257 -242
  198. package/src/server/errorHandler.ts +747 -744
  199. package/src/server/prompts.ts +181 -176
  200. package/src/server/rateLimiter.ts +131 -129
  201. package/src/server/requestLogger.ts +350 -322
  202. package/src/server/resources.ts +442 -374
  203. package/src/server/responseFormatter.ts +41 -37
  204. package/src/server/securityMiddleware.ts +223 -205
  205. package/src/server/serverKnowledgeStore.ts +67 -67
  206. package/src/server/toolRegistry.ts +508 -474
  207. package/src/tools/CLAUDE.md +604 -0
  208. package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
  209. package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
  210. package/src/tools/__tests__/accountTools.test.ts +685 -638
  211. package/src/tools/__tests__/adapters.test.ts +142 -108
  212. package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
  213. package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
  214. package/src/tools/__tests__/budgetTools.test.ts +442 -413
  215. package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
  216. package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
  217. package/src/tools/__tests__/categoryTools.test.ts +656 -625
  218. package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
  219. package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
  220. package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
  221. package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
  222. package/src/tools/__tests__/compareTransactions.test.ts +352 -332
  223. package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
  224. package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
  225. package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
  226. package/src/tools/__tests__/deltaSupport.test.ts +211 -184
  227. package/src/tools/__tests__/deltaTestUtils.ts +37 -33
  228. package/src/tools/__tests__/exportTransactions.test.ts +205 -200
  229. package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
  230. package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
  231. package/src/tools/__tests__/monthTools.test.ts +561 -512
  232. package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
  233. package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
  234. package/src/tools/__tests__/payeeTools.test.ts +486 -434
  235. package/src/tools/__tests__/transactionSchemas.test.ts +1202 -1186
  236. package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
  237. package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
  238. package/src/tools/__tests__/transactionUtils.test.ts +1004 -977
  239. package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
  240. package/src/tools/__tests__/utilityTools.test.ts +68 -58
  241. package/src/tools/accountTools.ts +293 -271
  242. package/src/tools/adapters.ts +120 -63
  243. package/src/tools/budgetTools.ts +121 -116
  244. package/src/tools/categoryTools.ts +379 -339
  245. package/src/tools/compareTransactions/formatter.ts +131 -119
  246. package/src/tools/compareTransactions/index.ts +249 -214
  247. package/src/tools/compareTransactions/matcher.ts +259 -209
  248. package/src/tools/compareTransactions/parser.ts +517 -487
  249. package/src/tools/compareTransactions/types.ts +38 -38
  250. package/src/tools/compareTransactions.ts +1 -1
  251. package/src/tools/deltaFetcher.ts +281 -260
  252. package/src/tools/deltaSupport.ts +264 -259
  253. package/src/tools/exportTransactions.ts +230 -218
  254. package/src/tools/monthTools.ts +180 -165
  255. package/src/tools/payeeTools.ts +152 -140
  256. package/src/tools/reconcileAdapter.ts +297 -252
  257. package/src/tools/reconciliation/CLAUDE.md +506 -0
  258. package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +133 -124
  259. package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -230
  260. package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -400
  261. package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
  262. package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
  263. package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
  264. package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
  265. package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
  266. package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
  267. package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
  268. package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -989
  269. package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
  270. package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -533
  271. package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -74
  272. package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -62
  273. package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
  274. package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +56 -55
  275. package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
  276. package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
  277. package/src/tools/reconciliation/analyzer.ts +564 -504
  278. package/src/tools/reconciliation/csvParser.ts +656 -609
  279. package/src/tools/reconciliation/executor.ts +1290 -1128
  280. package/src/tools/reconciliation/index.ts +580 -528
  281. package/src/tools/reconciliation/matcher.ts +256 -240
  282. package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
  283. package/src/tools/reconciliation/recommendationEngine.ts +357 -345
  284. package/src/tools/reconciliation/reportFormatter.ts +343 -307
  285. package/src/tools/reconciliation/signDetector.ts +89 -83
  286. package/src/tools/reconciliation/types.ts +164 -159
  287. package/src/tools/reconciliation/ynabAdapter.ts +17 -15
  288. package/src/tools/schemas/CLAUDE.md +546 -0
  289. package/src/tools/schemas/common.ts +1 -1
  290. package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
  291. package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
  292. package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
  293. package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
  294. package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
  295. package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
  296. package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
  297. package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
  298. package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
  299. package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
  300. package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
  301. package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
  302. package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
  303. package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
  304. package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
  305. package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
  306. package/src/tools/schemas/outputs/index.ts +163 -163
  307. package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
  308. package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
  309. package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
  310. package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
  311. package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
  312. package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
  313. package/src/tools/schemas/shared/commonOutputs.ts +27 -19
  314. package/src/tools/toolCategories.ts +114 -114
  315. package/src/tools/transactionReadTools.ts +327 -0
  316. package/src/tools/transactionSchemas.ts +322 -291
  317. package/src/tools/transactionTools.ts +84 -2246
  318. package/src/tools/transactionUtils.ts +507 -422
  319. package/src/tools/transactionWriteTools.ts +2110 -0
  320. package/src/tools/utilityTools.ts +46 -41
  321. package/src/types/CLAUDE.md +477 -0
  322. package/src/types/__tests__/index.test.ts +51 -51
  323. package/src/types/index.ts +43 -39
  324. package/src/types/integration-tests.d.ts +26 -26
  325. package/src/types/reconciliation.ts +29 -29
  326. package/src/types/toolAnnotations.ts +30 -30
  327. package/src/types/toolRegistration.ts +43 -32
  328. package/src/utils/CLAUDE.md +508 -0
  329. package/src/utils/__tests__/dateUtils.test.ts +174 -168
  330. package/src/utils/__tests__/money.test.ts +193 -187
  331. package/src/utils/amountUtils.ts +5 -5
  332. package/src/utils/baseError.ts +5 -5
  333. package/src/utils/dateUtils.ts +29 -26
  334. package/src/utils/errors.ts +14 -14
  335. package/src/utils/money.ts +66 -52
  336. package/src/utils/validationError.ts +1 -1
  337. package/tsconfig.json +29 -29
  338. package/tsconfig.prod.json +16 -16
  339. package/vitest-reporters/split-json-reporter.ts +247 -204
  340. package/vitest.config.ts +99 -95
  341. package/.prettierignore +0 -10
  342. package/.prettierrc.json +0 -10
  343. package/eslint.config.js +0 -49
@@ -1,855 +1,917 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import { z } from 'zod/v4';
3
- import type * as ynab from 'ynab';
4
- import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
1
+ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
+ import type * as ynab from "ynab";
4
+ import { z } from "zod/v4";
5
5
  import {
6
- ToolRegistry,
7
- ToolDefinition,
8
- ToolRegistryDependencies,
9
- ToolExecutionPayload,
10
- } from '../toolRegistry.js';
6
+ ReconcileAccountSchema,
7
+ handleReconcileAccount,
8
+ } from "../../tools/reconciliation/index.js";
11
9
  import {
12
- handleReconcileAccount,
13
- ReconcileAccountSchema,
14
- } from '../../tools/reconciliation/index.js';
10
+ type ToolDefinition,
11
+ type ToolExecutionPayload,
12
+ ToolRegistry,
13
+ type ToolRegistryDependencies,
14
+ } from "../toolRegistry.js";
15
15
 
16
16
  function createResult(label: string): CallToolResult {
17
- return {
18
- content: [
19
- {
20
- type: 'text',
21
- text: label,
22
- },
23
- ],
24
- };
17
+ return {
18
+ content: [
19
+ {
20
+ type: "text",
21
+ text: label,
22
+ },
23
+ ],
24
+ };
25
25
  }
26
26
 
27
27
  function makeTestDeps() {
28
- const securityInvocations: {
29
- namespace: string;
30
- operation: string;
31
- accessToken: string;
32
- params: Record<string, unknown>;
33
- }[] = [];
34
-
35
- const responseFormatter = {
36
- runWithMinifyOverride: vi.fn(<T>(minifyOverride: boolean | undefined, fn: () => T): T => fn()),
37
- format: vi.fn((value) => JSON.stringify(value)),
38
- };
39
-
40
- const errorHandler = {
41
- createValidationError: vi.fn((message: string, details?: string) => ({
42
- content: [
43
- {
44
- type: 'text',
45
- text: `validation:${message}${details ? `:${details}` : ''}`,
46
- },
47
- ],
48
- })),
49
- handleError: vi.fn((error: unknown, context: string) => ({
50
- content: [
51
- {
52
- type: 'text',
53
- text: `handled:${context}:${error instanceof Error ? error.message : String(error)}`,
54
- },
55
- ],
56
- })),
57
- };
58
-
59
- const withSecurityWrapper = vi.fn(
60
- <T extends Record<string, unknown>>(
61
- namespace: string,
62
- operation: string,
63
- schema: z.ZodSchema<T>,
64
- ) =>
65
- (accessToken: string) =>
66
- (params: Record<string, unknown>) =>
67
- async (handler: (validated: T) => Promise<CallToolResult>) => {
68
- securityInvocations.push({ namespace, operation, accessToken, params });
69
- try {
70
- const validated = schema.parse(params ?? {});
71
- return await handler(validated);
72
- } catch (error) {
73
- return errorHandler.createValidationError(
74
- `Invalid parameters for ${operation}`,
75
- error instanceof Error ? error.message : undefined,
76
- );
77
- }
78
- },
79
- );
80
-
81
- const dependencies: ToolRegistryDependencies = {
82
- errorHandler,
83
- responseFormatter,
84
- withSecurityWrapper,
85
- };
86
-
87
- return {
88
- dependencies,
89
- securityInvocations,
90
- errorHandler,
91
- responseFormatter,
92
- withSecurityWrapper,
93
- };
28
+ const securityInvocations: {
29
+ namespace: string;
30
+ operation: string;
31
+ accessToken: string;
32
+ params: Record<string, unknown>;
33
+ }[] = [];
34
+
35
+ const responseFormatter = {
36
+ runWithMinifyOverride: vi.fn(
37
+ <T>(minifyOverride: boolean | undefined, fn: () => T): T => fn(),
38
+ ),
39
+ format: vi.fn((value) => JSON.stringify(value)),
40
+ };
41
+
42
+ const errorHandler = {
43
+ createValidationError: vi.fn((message: string, details?: string) => ({
44
+ content: [
45
+ {
46
+ type: "text",
47
+ text: `validation:${message}${details ? `:${details}` : ""}`,
48
+ },
49
+ ],
50
+ })),
51
+ handleError: vi.fn((error: unknown, context: string) => ({
52
+ content: [
53
+ {
54
+ type: "text",
55
+ text: `handled:${context}:${error instanceof Error ? error.message : String(error)}`,
56
+ },
57
+ ],
58
+ })),
59
+ };
60
+
61
+ const withSecurityWrapper = vi.fn(
62
+ <T extends Record<string, unknown>>(
63
+ namespace: string,
64
+ operation: string,
65
+ schema: z.ZodSchema<T>,
66
+ ) =>
67
+ (accessToken: string) =>
68
+ (params: Record<string, unknown>) =>
69
+ async (handler: (validated: T) => Promise<CallToolResult>) => {
70
+ securityInvocations.push({ namespace, operation, accessToken, params });
71
+ try {
72
+ const validated = schema.parse(params ?? {});
73
+ return await handler(validated);
74
+ } catch (error) {
75
+ return errorHandler.createValidationError(
76
+ `Invalid parameters for ${operation}`,
77
+ error instanceof Error ? error.message : undefined,
78
+ );
79
+ }
80
+ },
81
+ );
82
+
83
+ const dependencies: ToolRegistryDependencies = {
84
+ errorHandler,
85
+ responseFormatter,
86
+ withSecurityWrapper,
87
+ };
88
+
89
+ return {
90
+ dependencies,
91
+ securityInvocations,
92
+ errorHandler,
93
+ responseFormatter,
94
+ withSecurityWrapper,
95
+ };
94
96
  }
95
97
 
96
- describe('ToolRegistry', () => {
97
- let dependencies: ToolRegistryDependencies;
98
- let registry: ToolRegistry;
99
- let securityInvocations: {
100
- namespace: string;
101
- operation: string;
102
- accessToken: string;
103
- params: Record<string, unknown>;
104
- }[];
105
- let responseFormatter: ReturnType<typeof makeTestDeps>['responseFormatter'];
106
-
107
- const handlerResult = createResult('handler-success');
108
-
109
- beforeEach(() => {
110
- const setup = makeTestDeps();
111
- ({ dependencies, securityInvocations, responseFormatter } = setup);
112
- registry = new ToolRegistry(dependencies);
113
-
114
- // Spy on error handler methods for testing
115
- vi.spyOn(dependencies.errorHandler, 'createValidationError');
116
- vi.spyOn(dependencies.errorHandler, 'handleError');
117
- });
118
-
119
- const registerSampleTool = (definition?: Partial<ToolDefinition>) => {
120
- const base: ToolDefinition = {
121
- name: 'sample_tool',
122
- description: 'Test tool for registry',
123
- inputSchema: z.object({
124
- id: z.string().min(1, 'id required'),
125
- minify: z.boolean().optional(),
126
- }),
127
- handler: vi.fn(async ({ input }: ToolExecutionPayload<{ id: string }>) => {
128
- return createResult(`handled:${input.id}`);
129
- }),
130
- ...definition,
131
- };
132
-
133
- registry.register(base);
134
- return base;
135
- };
136
-
137
- it('registers a tool and exposes it through getToolDefinitions', () => {
138
- registerSampleTool();
139
-
140
- const definitions = registry.getToolDefinitions();
141
- expect(definitions).toHaveLength(1);
142
- expect(definitions[0]?.name).toBe('sample_tool');
143
- expect(definitions[0]?.description).toBe('Test tool for registry');
144
- });
145
-
146
- it('throws when registering duplicate tool names', () => {
147
- registerSampleTool();
148
-
149
- expect(() => registerSampleTool()).toThrowError("Tool 'sample_tool' is already registered");
150
- });
151
-
152
- it('rejects invalid tool definitions', () => {
153
- expect(() =>
154
- registry.register({
155
- // @ts-expect-error intentionally malformed
156
- name: '',
157
- description: 'invalid',
158
- inputSchema: z.object({}),
159
- handler: null,
160
- }),
161
- ).toThrowError('Tool definition requires a non-empty name');
162
- });
163
-
164
- it('lists tools with generated JSON schema when metadata missing', () => {
165
- registerSampleTool();
166
-
167
- const tools = registry.listTools();
168
- expect(tools).toHaveLength(1);
169
- expect(tools[0]?.name).toBe('sample_tool');
170
- const schema = tools[0]?.inputSchema as Record<string, unknown> | undefined;
171
- expect(schema).toBeDefined();
172
- // Input schemas use io:'input' mode which doesn't set additionalProperties
173
- expect(schema).toMatchObject({
174
- type: 'object',
175
- properties: expect.objectContaining({
176
- id: expect.objectContaining({ type: 'string' }),
177
- minify: expect.objectContaining({ type: 'boolean' }),
178
- }),
179
- required: ['id'],
180
- });
181
- expect(typeof schema?.['$schema']).toBe('string');
182
- });
183
-
184
- it('prefers custom metadata JSON schema when provided', () => {
185
- const customSchema = { type: 'object', properties: { foo: { type: 'string' } } };
186
- registry.register({
187
- name: 'meta_tool',
188
- description: 'Has metadata schema',
189
- inputSchema: z.object({ foo: z.string() }),
190
- handler: async () => handlerResult,
191
- metadata: { inputJsonSchema: customSchema },
192
- });
193
-
194
- const tools = registry.listTools();
195
- const found = tools.find((tool) => tool.name === 'meta_tool');
196
- expect(found?.inputSchema).toEqual(customSchema);
197
- });
198
-
199
- it('executes a registered tool via security wrapper and handler', async () => {
200
- const handler = vi.fn(async () => handlerResult);
201
- registry.register({
202
- name: 'exec_tool',
203
- description: 'Execute tool',
204
- inputSchema: z.object({ id: z.string().min(1) }),
205
- handler,
206
- });
207
-
208
- const result = await registry.executeTool({
209
- name: 'exec_tool',
210
- accessToken: 'token-123',
211
- arguments: { id: 'abc' },
212
- });
213
-
214
- expect(result).toEqual(handlerResult);
215
- expect(handler).toHaveBeenCalledTimes(1);
216
- expect(securityInvocations).toHaveLength(1);
217
- expect(securityInvocations[0]).toMatchObject({
218
- namespace: 'ynab',
219
- operation: 'exec_tool',
220
- accessToken: 'token-123',
221
- params: { id: 'abc' },
222
- });
223
- });
224
-
225
- // NOTE: Reconcile dual-channel output is properly tested in reconciliation integration tests
226
- // This test requires complex DeltaFetcher mocking that's covered elsewhere
227
- it.skip('routes reconcile_account tool to handler emitting dual-channel output', async () => {
228
- const mockYnabAPI = {
229
- accounts: {
230
- getAccount: vi.fn().mockResolvedValue({
231
- data: {
232
- account: {
233
- name: 'Checking',
234
- balance: 0,
235
- cleared_balance: 0,
236
- uncleared_balance: 0,
237
- },
238
- },
239
- }),
240
- },
241
- budgets: {
242
- getBudgetById: vi.fn().mockResolvedValue({
243
- data: { budget: { currency_format: { currency_code: 'USD' } } },
244
- }),
245
- },
246
- transactions: {
247
- getTransactionsByAccount: vi.fn().mockResolvedValue({ data: { transactions: [] } }),
248
- },
249
- } as unknown as ynab.API;
250
-
251
- const adapt =
252
- <TInput extends Record<string, unknown>>(
253
- handler: (api: ynab.API, params: TInput) => Promise<CallToolResult>,
254
- ) =>
255
- async ({ input }: ToolExecutionPayload<TInput>) =>
256
- handler(mockYnabAPI, input);
257
-
258
- registry.register({
259
- name: 'reconcile_account',
260
- description: 'Guided reconciliation workflow with dual-channel output',
261
- inputSchema: ReconcileAccountSchema,
262
- handler: adapt(handleReconcileAccount),
263
- });
264
-
265
- const result = await registry.executeTool({
266
- name: 'reconcile_account',
267
- accessToken: 'token-xyz',
268
- arguments: {
269
- budget_id: 'budget-1',
270
- account_id: 'account-1',
271
- csv_data: 'Date,Description,Amount\n2025-10-01,Sample,-1.23',
272
- statement_balance: -1.23,
273
- include_structured_data: true, // Request both human + structured output
274
- },
275
- });
276
-
277
- expect(result.content).toHaveLength(2);
278
- expect(result.content.every((entry) => entry.type === 'text')).toBe(true);
279
- expect(mockYnabAPI.accounts.getAccount).toHaveBeenCalled();
280
-
281
- const toolNames = registry.listTools().map((tool) => tool.name);
282
- expect(toolNames).toEqual(expect.arrayContaining(['reconcile_account']));
283
- });
284
-
285
- it('merges default arguments before validation', async () => {
286
- registry.register({
287
- name: 'defaulted_tool',
288
- description: 'Has defaults',
289
- inputSchema: z.object({ id: z.string() }),
290
- defaultArgumentResolver: vi.fn(async () => ({ id: 'resolved-id' })),
291
- handler: vi.fn(async () => handlerResult),
292
- });
293
-
294
- await registry.executeTool({ name: 'defaulted_tool', accessToken: 'token-1' });
295
-
296
- expect(securityInvocations[0]?.params).toEqual({ id: 'resolved-id' });
297
- });
298
-
299
- it('passes cache helpers to the handler context when injected', async () => {
300
- const cacheHelpers = {
301
- generateKey: vi.fn((...segments: unknown[]) => segments.join(':')),
302
- };
303
-
304
- dependencies.cacheHelpers = cacheHelpers;
305
- registry = new ToolRegistry(dependencies);
306
-
307
- const handler = vi.fn(async (payload: ToolExecutionPayload<{ id: string }>) => {
308
- expect(payload.context.cache).toBe(cacheHelpers);
309
- expect(payload.context.rawArguments).toEqual({ id: '42' });
310
- return handlerResult;
311
- });
312
-
313
- registry.register({
314
- name: 'cache_tool',
315
- description: 'Needs cache',
316
- inputSchema: z.object({ id: z.string() }),
317
- handler,
318
- });
319
-
320
- await registry.executeTool({
321
- name: 'cache_tool',
322
- accessToken: 'token',
323
- arguments: { id: '42' },
324
- });
325
-
326
- expect(handler).toHaveBeenCalledTimes(1);
327
- });
328
-
329
- it('extracts minify override from arguments when not explicitly provided', async () => {
330
- const handler = vi.fn(async () => handlerResult);
331
- registry.register({
332
- name: 'minify_hint_tool',
333
- description: 'Uses argument minify hint',
334
- inputSchema: z.object({ id: z.string(), _minify: z.boolean().optional() }),
335
- handler,
336
- });
337
-
338
- let capturedFn: (() => Promise<CallToolResult>) | undefined;
339
- let release: ((value: CallToolResult) => void) | undefined;
340
- const formatterResolution = new Promise<CallToolResult>((resolve) => {
341
- release = resolve;
342
- });
343
-
344
- responseFormatter.runWithMinifyOverride.mockImplementationOnce((minify, fn) => {
345
- capturedFn = fn;
346
- return formatterResolution;
347
- });
348
-
349
- const execution = registry.executeTool({
350
- name: 'minify_hint_tool',
351
- accessToken: 'token',
352
- arguments: { id: 'abc', _minify: false },
353
- });
354
-
355
- expect(responseFormatter.runWithMinifyOverride).toHaveBeenCalledWith(
356
- false,
357
- expect.any(Function),
358
- );
359
- expect(capturedFn).toBeDefined();
360
-
361
- const manualResult = await capturedFn!();
362
- expect(manualResult).toEqual(handlerResult);
363
- expect(handler).toHaveBeenCalledTimes(1);
364
-
365
- release?.(manualResult);
366
-
367
- const finalResult = await execution;
368
- expect(finalResult).toEqual(handlerResult);
369
- });
370
-
371
- it('prefers explicit minify override option over argument hints', async () => {
372
- const handler = vi.fn(async () => handlerResult);
373
- registry.register({
374
- name: 'minify_option_tool',
375
- description: 'Uses option minify',
376
- inputSchema: z.object({ id: z.string(), minify: z.boolean().optional() }),
377
- handler,
378
- });
379
-
380
- let capturedFn: (() => Promise<CallToolResult>) | undefined;
381
- let release: ((value: CallToolResult) => void) | undefined;
382
- const formatterResolution = new Promise<CallToolResult>((resolve) => {
383
- release = resolve;
384
- });
385
-
386
- responseFormatter.runWithMinifyOverride.mockImplementationOnce((minify, fn) => {
387
- capturedFn = fn;
388
- return formatterResolution;
389
- });
390
-
391
- const execution = registry.executeTool({
392
- name: 'minify_option_tool',
393
- accessToken: 'token',
394
- arguments: { id: 'abc', minify: false },
395
- minifyOverride: true,
396
- });
397
-
398
- expect(responseFormatter.runWithMinifyOverride).toHaveBeenCalledWith(
399
- true,
400
- expect.any(Function),
401
- );
402
- expect(capturedFn).toBeDefined();
403
-
404
- const manualResult = await capturedFn!();
405
- expect(manualResult).toEqual(handlerResult);
406
- expect(handler).toHaveBeenCalledTimes(1);
407
-
408
- release?.(manualResult);
409
-
410
- const finalResult = await execution;
411
- expect(finalResult).toEqual(handlerResult);
412
- });
413
-
414
- it('returns validation error result for unknown tools', async () => {
415
- const result = await registry.executeTool({ name: 'missing_tool', accessToken: 'token' });
416
-
417
- expect(result).toEqual(
418
- createResult(
419
- 'validation:Unknown tool: missing_tool:The requested tool is not registered with the server',
420
- ),
421
- );
422
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalledTimes(1);
423
- });
424
-
425
- it('surfaces validation failures from security wrapper', async () => {
426
- registerSampleTool();
427
-
428
- const result = await registry.executeTool({
429
- name: 'sample_tool',
430
- accessToken: 'token',
431
- arguments: {},
432
- });
433
-
434
- expect(
435
- result.content[0]?.text?.startsWith('validation:Invalid parameters for sample_tool:'),
436
- ).toBe(true);
437
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
438
- });
439
-
440
- it('routes handler exceptions to error handler', async () => {
441
- const handlerError = new Error('boom');
442
- const handler = vi.fn(async () => {
443
- throw handlerError;
444
- });
445
-
446
- registry.register({
447
- name: 'error_tool',
448
- description: 'Throws',
449
- inputSchema: z.object({ id: z.string() }),
450
- handler,
451
- });
452
-
453
- const result = await registry.executeTool({
454
- name: 'error_tool',
455
- accessToken: 'token',
456
- arguments: { id: 'abc' },
457
- });
458
-
459
- expect(result).toEqual(createResult('handled:executing error_tool - error_tool:boom'));
460
- expect(dependencies.errorHandler.handleError).toHaveBeenCalledWith(
461
- handlerError,
462
- 'executing error_tool - error_tool',
463
- );
464
- });
465
-
466
- it('normalizes unexpected security errors', async () => {
467
- const error = new Error('rate limit');
468
- const customDeps: ToolRegistryDependencies = {
469
- ...dependencies,
470
- withSecurityWrapper: vi.fn(() => () => () => {
471
- throw error;
472
- }),
473
- };
474
-
475
- const customRegistry = new ToolRegistry(customDeps);
476
-
477
- // Spy on custom error handler methods for testing
478
- vi.spyOn(customDeps.errorHandler, 'handleError');
479
-
480
- customRegistry.register({
481
- name: 'security_tool',
482
- description: 'Security throws',
483
- inputSchema: z.object({}),
484
- handler: vi.fn(async () => handlerResult),
485
- });
486
-
487
- const result = await customRegistry.executeTool({
488
- name: 'security_tool',
489
- accessToken: 'token',
490
- });
491
-
492
- expect(result).toEqual(createResult('handled:executing security_tool:rate limit'));
493
- expect(customDeps.errorHandler.handleError).toHaveBeenCalledWith(
494
- error,
495
- 'executing security_tool',
496
- );
497
- });
498
-
499
- it('returns permissive schema when conversion fails', () => {
500
- registry.register({
501
- name: 'any_tool',
502
- description: 'Any schema',
503
- // z.any is not supported by converter and should fallback
504
- inputSchema: z.any(),
505
- handler: vi.fn(async () => handlerResult),
506
- });
507
-
508
- const tool = registry.listTools().find((item) => item.name === 'any_tool');
509
- const schema = tool?.inputSchema as Record<string, unknown> | undefined;
510
- expect(schema).toBeDefined();
511
- expect(typeof schema?.['$schema']).toBe('string');
512
- });
513
-
514
- it('supports empty registry listings', () => {
515
- const emptyRegistry = new ToolRegistry(dependencies);
516
- expect(emptyRegistry.listTools()).toEqual([]);
517
- expect(emptyRegistry.getToolDefinitions()).toEqual([]);
518
- });
519
-
520
- describe('ErrorHandler integration', () => {
521
- it('should use injected ErrorHandler instance', async () => {
522
- const mockErrorHandler = {
523
- handleError: vi.fn(() => ({ content: [{ type: 'text', text: 'Mock error' }] })),
524
- createValidationError: vi.fn(() => ({
525
- content: [{ type: 'text', text: 'Mock validation error' }],
526
- })),
527
- };
528
-
529
- const customDeps = {
530
- ...dependencies,
531
- errorHandler: mockErrorHandler,
532
- };
533
-
534
- const customRegistry = new ToolRegistry(customDeps);
535
-
536
- // Test that the registry uses the injected error handler
537
- const result = await customRegistry.executeTool({
538
- name: 'nonexistent_tool',
539
- accessToken: 'test-token',
540
- });
541
-
542
- expect(mockErrorHandler.createValidationError).toHaveBeenCalled();
543
- expect(result.content[0]?.text).toBe('Mock validation error');
544
- });
545
-
546
- it('should use the same formatter instance for both ErrorHandler and ToolRegistry', () => {
547
- const { dependencies: deps, responseFormatter: formatter, errorHandler } = makeTestDeps();
548
-
549
- // Both should use the same formatter
550
- expect(deps.errorHandler).toBe(errorHandler);
551
- expect(deps.responseFormatter).toBe(formatter);
552
- });
553
-
554
- it('should implement ErrorHandler contract interface', () => {
555
- const { errorHandler } = makeTestDeps();
556
-
557
- expect(typeof errorHandler.handleError).toBe('function');
558
- expect(typeof errorHandler.createValidationError).toBe('function');
559
- });
560
- });
561
-
562
- describe('Output Schema Validation', () => {
563
- it('validates handler output against declared output schema', async () => {
564
- const outputSchema = z.object({
565
- success: z.boolean(),
566
- data: z.object({
567
- id: z.string(),
568
- value: z.number(),
569
- }),
570
- });
571
-
572
- const handler = vi.fn(async () =>
573
- createResult(
574
- JSON.stringify({
575
- success: true,
576
- data: { id: 'test-id', value: 42 },
577
- }),
578
- ),
579
- );
580
-
581
- registry.register({
582
- name: 'validated_output_tool',
583
- description: 'Has output schema',
584
- inputSchema: z.object({ id: z.string() }),
585
- outputSchema,
586
- handler,
587
- });
588
-
589
- const result = await registry.executeTool({
590
- name: 'validated_output_tool',
591
- accessToken: 'token',
592
- arguments: { id: 'test' },
593
- });
594
-
595
- expect(handler).toHaveBeenCalledTimes(1);
596
- expect(result.content[0]?.text).toContain('success');
597
- expect(result.content[0]?.text).toContain('test-id');
598
- });
599
-
600
- it('rejects handler output that does not match output schema', async () => {
601
- const outputSchema = z.object({
602
- success: z.boolean(),
603
- data: z.object({
604
- id: z.string(),
605
- value: z.number(),
606
- }),
607
- });
608
-
609
- const handler = vi.fn(async () =>
610
- createResult(
611
- JSON.stringify({
612
- success: true,
613
- data: { id: 'test-id', value: 'not-a-number' }, // Invalid: value should be number
614
- }),
615
- ),
616
- );
617
-
618
- registry.register({
619
- name: 'invalid_output_tool',
620
- description: 'Returns invalid output',
621
- inputSchema: z.object({ id: z.string() }),
622
- outputSchema,
623
- handler,
624
- });
625
-
626
- const result = await registry.executeTool({
627
- name: 'invalid_output_tool',
628
- accessToken: 'token',
629
- arguments: { id: 'test' },
630
- });
631
-
632
- expect(handler).toHaveBeenCalledTimes(1);
633
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
634
- expect(result.content[0]?.text).toContain('Output validation failed');
635
- expect(result.content[0]?.text).toContain('invalid_output_tool');
636
- });
637
-
638
- it('rejects handler output with missing required fields', async () => {
639
- const outputSchema = z.object({
640
- success: z.boolean(),
641
- data: z.object({
642
- id: z.string(),
643
- value: z.number(),
644
- }),
645
- });
646
-
647
- const handler = vi.fn(async () =>
648
- createResult(
649
- JSON.stringify({
650
- success: true,
651
- // Missing 'data' field
652
- }),
653
- ),
654
- );
655
-
656
- registry.register({
657
- name: 'missing_field_tool',
658
- description: 'Returns output missing required field',
659
- inputSchema: z.object({ id: z.string() }),
660
- outputSchema,
661
- handler,
662
- });
663
-
664
- const result = await registry.executeTool({
665
- name: 'missing_field_tool',
666
- accessToken: 'token',
667
- arguments: { id: 'test' },
668
- });
669
-
670
- expect(handler).toHaveBeenCalledTimes(1);
671
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
672
- expect(result.content[0]?.text).toContain('Output validation failed');
673
- expect(result.content[0]?.text).toContain('missing_field_tool');
674
- });
675
-
676
- it('rejects handler output with invalid JSON', async () => {
677
- const outputSchema = z.object({
678
- success: z.boolean(),
679
- });
680
-
681
- const handler = vi.fn(async () => createResult('not valid json {'));
682
-
683
- registry.register({
684
- name: 'invalid_json_tool',
685
- description: 'Returns invalid JSON',
686
- inputSchema: z.object({ id: z.string() }),
687
- outputSchema,
688
- handler,
689
- });
690
-
691
- const result = await registry.executeTool({
692
- name: 'invalid_json_tool',
693
- accessToken: 'token',
694
- arguments: { id: 'test' },
695
- });
696
-
697
- expect(handler).toHaveBeenCalledTimes(1);
698
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
699
- expect(result.content[0]?.text).toContain('Output validation failed');
700
- expect(result.content[0]?.text).toContain('Invalid JSON');
701
- });
702
-
703
- it('rejects handler output with empty content', async () => {
704
- const outputSchema = z.object({
705
- success: z.boolean(),
706
- });
707
-
708
- const handler = vi.fn(async () => ({ content: [] }));
709
-
710
- registry.register({
711
- name: 'empty_content_tool',
712
- description: 'Returns empty content',
713
- inputSchema: z.object({ id: z.string() }),
714
- outputSchema,
715
- handler,
716
- });
717
-
718
- const result = await registry.executeTool({
719
- name: 'empty_content_tool',
720
- accessToken: 'token',
721
- arguments: { id: 'test' },
722
- });
723
-
724
- expect(handler).toHaveBeenCalledTimes(1);
725
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
726
- expect(result.content[0]?.text).toContain('Output validation failed');
727
- expect(result.content[0]?.text).toContain('empty content');
728
- });
729
-
730
- it('rejects handler output with non-text content', async () => {
731
- const outputSchema = z.object({
732
- success: z.boolean(),
733
- });
734
-
735
- const handler = vi.fn(async () => ({
736
- content: [{ type: 'image', data: 'base64...' }],
737
- }));
738
-
739
- registry.register({
740
- name: 'non_text_tool',
741
- description: 'Returns non-text content',
742
- inputSchema: z.object({ id: z.string() }),
743
- outputSchema,
744
- handler,
745
- });
746
-
747
- const result = await registry.executeTool({
748
- name: 'non_text_tool',
749
- accessToken: 'token',
750
- arguments: { id: 'test' },
751
- });
752
-
753
- expect(handler).toHaveBeenCalledTimes(1);
754
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
755
- expect(result.content[0]?.text).toContain('Output validation failed');
756
- expect(result.content[0]?.text).toContain('Handler returned invalid content items');
757
- expect(result.content[0]?.text).toContain('Item 0: type is "image" instead of "text"');
758
- });
759
-
760
- it('rejects handler output with multiple invalid content items', async () => {
761
- const outputSchema = z.object({
762
- success: z.boolean(),
763
- });
764
-
765
- const handler = vi.fn(async () => ({
766
- content: [
767
- { type: 'text', text: '{"success": true}' }, // Valid
768
- { type: 'image', data: 'base64...' }, // Invalid: wrong type
769
- { type: 'text', text: 123 }, // Invalid: text is not string
770
- { type: 'text' }, // Invalid: missing text property
771
- ],
772
- }));
773
-
774
- registry.register({
775
- name: 'multi_invalid_tool',
776
- description: 'Returns multiple content items with some invalid',
777
- inputSchema: z.object({ id: z.string() }),
778
- outputSchema,
779
- handler,
780
- });
781
-
782
- const result = await registry.executeTool({
783
- name: 'multi_invalid_tool',
784
- accessToken: 'token',
785
- arguments: { id: 'test' },
786
- });
787
-
788
- expect(handler).toHaveBeenCalledTimes(1);
789
- expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
790
- expect(result.content[0]?.text).toContain('Output validation failed');
791
- expect(result.content[0]?.text).toContain('Handler returned invalid content items');
792
- expect(result.content[0]?.text).toContain('3 of 4 failed');
793
- expect(result.content[0]?.text).toContain('Item 1: type is "image" instead of "text"');
794
- expect(result.content[0]?.text).toContain(
795
- 'Item 2: text property is number instead of string',
796
- );
797
- expect(result.content[0]?.text).toContain(
798
- 'Item 3: text property is undefined instead of string',
799
- );
800
- });
801
-
802
- it('skips validation when no output schema is defined', async () => {
803
- const handler = vi.fn(async () => createResult('any output format'));
804
-
805
- registry.register({
806
- name: 'no_schema_tool',
807
- description: 'Has no output schema',
808
- inputSchema: z.object({ id: z.string() }),
809
- // No outputSchema defined
810
- handler,
811
- });
812
-
813
- const result = await registry.executeTool({
814
- name: 'no_schema_tool',
815
- accessToken: 'token',
816
- arguments: { id: 'test' },
817
- });
818
-
819
- expect(handler).toHaveBeenCalledTimes(1);
820
- expect(result.content[0]?.text).toBe('any output format');
821
- // Error handler should not be called for validation
822
- expect(dependencies.errorHandler.createValidationError).not.toHaveBeenCalled();
823
- });
824
-
825
- it('includes output schema in tool listing when defined', () => {
826
- const outputSchema = z.object({
827
- success: z.boolean(),
828
- message: z.string(),
829
- });
830
-
831
- registry.register({
832
- name: 'schema_listing_tool',
833
- description: 'Has output schema',
834
- inputSchema: z.object({ id: z.string() }),
835
- outputSchema,
836
- handler: vi.fn(async () => handlerResult),
837
- });
838
-
839
- const tools = registry.listTools();
840
- const tool = tools.find((t) => t.name === 'schema_listing_tool');
841
-
842
- expect(tool).toBeDefined();
843
- expect(tool?.outputSchema).toBeDefined();
844
- const schema = tool?.outputSchema as Record<string, unknown> | undefined;
845
- expect(schema).toMatchObject({
846
- type: 'object',
847
- properties: expect.objectContaining({
848
- success: expect.objectContaining({ type: 'boolean' }),
849
- message: expect.objectContaining({ type: 'string' }),
850
- }),
851
- required: ['success', 'message'],
852
- });
853
- });
854
- });
98
+ describe("ToolRegistry", () => {
99
+ let dependencies: ToolRegistryDependencies;
100
+ let registry: ToolRegistry;
101
+ let securityInvocations: {
102
+ namespace: string;
103
+ operation: string;
104
+ accessToken: string;
105
+ params: Record<string, unknown>;
106
+ }[];
107
+ let responseFormatter: ReturnType<typeof makeTestDeps>["responseFormatter"];
108
+
109
+ const handlerResult = createResult("handler-success");
110
+
111
+ beforeEach(() => {
112
+ const setup = makeTestDeps();
113
+ ({ dependencies, securityInvocations, responseFormatter } = setup);
114
+ registry = new ToolRegistry(dependencies);
115
+
116
+ // Spy on error handler methods for testing
117
+ vi.spyOn(dependencies.errorHandler, "createValidationError");
118
+ vi.spyOn(dependencies.errorHandler, "handleError");
119
+ });
120
+
121
+ const registerSampleTool = (definition?: Partial<ToolDefinition>) => {
122
+ const base: ToolDefinition = {
123
+ name: "sample_tool",
124
+ description: "Test tool for registry",
125
+ inputSchema: z.object({
126
+ id: z.string().min(1, "id required"),
127
+ minify: z.boolean().optional(),
128
+ }),
129
+ handler: vi.fn(
130
+ async ({ input }: ToolExecutionPayload<{ id: string }>) => {
131
+ return createResult(`handled:${input.id}`);
132
+ },
133
+ ),
134
+ ...definition,
135
+ };
136
+
137
+ registry.register(base);
138
+ return base;
139
+ };
140
+
141
+ it("registers a tool and exposes it through getToolDefinitions", () => {
142
+ registerSampleTool();
143
+
144
+ const definitions = registry.getToolDefinitions();
145
+ expect(definitions).toHaveLength(1);
146
+ expect(definitions[0]?.name).toBe("sample_tool");
147
+ expect(definitions[0]?.description).toBe("Test tool for registry");
148
+ });
149
+
150
+ it("throws when registering duplicate tool names", () => {
151
+ registerSampleTool();
152
+
153
+ expect(() => registerSampleTool()).toThrowError(
154
+ "Tool 'sample_tool' is already registered",
155
+ );
156
+ });
157
+
158
+ it("rejects invalid tool definitions", () => {
159
+ expect(() =>
160
+ registry.register({
161
+ // @ts-expect-error intentionally malformed
162
+ name: "",
163
+ description: "invalid",
164
+ inputSchema: z.object({}),
165
+ handler: null,
166
+ }),
167
+ ).toThrowError("Tool definition requires a non-empty name");
168
+ });
169
+
170
+ it("lists tools with generated JSON schema when metadata missing", () => {
171
+ registerSampleTool();
172
+
173
+ const tools = registry.listTools();
174
+ expect(tools).toHaveLength(1);
175
+ expect(tools[0]?.name).toBe("sample_tool");
176
+ const schema = tools[0]?.inputSchema as Record<string, unknown> | undefined;
177
+ expect(schema).toBeDefined();
178
+ // Input schemas use io:'input' mode which doesn't set additionalProperties
179
+ expect(schema).toMatchObject({
180
+ type: "object",
181
+ properties: expect.objectContaining({
182
+ id: expect.objectContaining({ type: "string" }),
183
+ minify: expect.objectContaining({ type: "boolean" }),
184
+ }),
185
+ required: ["id"],
186
+ });
187
+ expect(typeof schema?.$schema).toBe("string");
188
+ });
189
+
190
+ it("prefers custom metadata JSON schema when provided", () => {
191
+ const customSchema = {
192
+ type: "object",
193
+ properties: { foo: { type: "string" } },
194
+ };
195
+ registry.register({
196
+ name: "meta_tool",
197
+ description: "Has metadata schema",
198
+ inputSchema: z.object({ foo: z.string() }),
199
+ handler: async () => handlerResult,
200
+ metadata: { inputJsonSchema: customSchema },
201
+ });
202
+
203
+ const tools = registry.listTools();
204
+ const found = tools.find((tool) => tool.name === "meta_tool");
205
+ expect(found?.inputSchema).toEqual(customSchema);
206
+ });
207
+
208
+ it("executes a registered tool via security wrapper and handler", async () => {
209
+ const handler = vi.fn(async () => handlerResult);
210
+ registry.register({
211
+ name: "exec_tool",
212
+ description: "Execute tool",
213
+ inputSchema: z.object({ id: z.string().min(1) }),
214
+ handler,
215
+ });
216
+
217
+ const result = await registry.executeTool({
218
+ name: "exec_tool",
219
+ accessToken: "token-123",
220
+ arguments: { id: "abc" },
221
+ });
222
+
223
+ expect(result).toEqual(handlerResult);
224
+ expect(handler).toHaveBeenCalledTimes(1);
225
+ expect(securityInvocations).toHaveLength(1);
226
+ expect(securityInvocations[0]).toMatchObject({
227
+ namespace: "ynab",
228
+ operation: "exec_tool",
229
+ accessToken: "token-123",
230
+ params: { id: "abc" },
231
+ });
232
+ });
233
+
234
+ // NOTE: Reconcile dual-channel output is properly tested in reconciliation integration tests
235
+ // This test requires complex DeltaFetcher mocking that's covered elsewhere
236
+ it.skip("routes reconcile_account tool to handler emitting dual-channel output", async () => {
237
+ const mockYnabAPI = {
238
+ accounts: {
239
+ getAccount: vi.fn().mockResolvedValue({
240
+ data: {
241
+ account: {
242
+ name: "Checking",
243
+ balance: 0,
244
+ cleared_balance: 0,
245
+ uncleared_balance: 0,
246
+ },
247
+ },
248
+ }),
249
+ },
250
+ budgets: {
251
+ getBudgetById: vi.fn().mockResolvedValue({
252
+ data: { budget: { currency_format: { currency_code: "USD" } } },
253
+ }),
254
+ },
255
+ transactions: {
256
+ getTransactionsByAccount: vi
257
+ .fn()
258
+ .mockResolvedValue({ data: { transactions: [] } }),
259
+ },
260
+ } as unknown as ynab.API;
261
+
262
+ const adapt =
263
+ <TInput extends Record<string, unknown>>(
264
+ handler: (api: ynab.API, params: TInput) => Promise<CallToolResult>,
265
+ ) =>
266
+ async ({ input }: ToolExecutionPayload<TInput>) =>
267
+ handler(mockYnabAPI, input);
268
+
269
+ registry.register({
270
+ name: "reconcile_account",
271
+ description: "Guided reconciliation workflow with dual-channel output",
272
+ inputSchema: ReconcileAccountSchema,
273
+ handler: adapt(handleReconcileAccount),
274
+ });
275
+
276
+ const result = await registry.executeTool({
277
+ name: "reconcile_account",
278
+ accessToken: "token-xyz",
279
+ arguments: {
280
+ budget_id: "budget-1",
281
+ account_id: "account-1",
282
+ csv_data: "Date,Description,Amount\n2025-10-01,Sample,-1.23",
283
+ statement_balance: -1.23,
284
+ include_structured_data: true, // Request both human + structured output
285
+ },
286
+ });
287
+
288
+ expect(result.content).toHaveLength(2);
289
+ expect(result.content.every((entry) => entry.type === "text")).toBe(true);
290
+ expect(mockYnabAPI.accounts.getAccount).toHaveBeenCalled();
291
+
292
+ const toolNames = registry.listTools().map((tool) => tool.name);
293
+ expect(toolNames).toEqual(expect.arrayContaining(["reconcile_account"]));
294
+ });
295
+
296
+ it("merges default arguments before validation", async () => {
297
+ registry.register({
298
+ name: "defaulted_tool",
299
+ description: "Has defaults",
300
+ inputSchema: z.object({ id: z.string() }),
301
+ defaultArgumentResolver: vi.fn(async () => ({ id: "resolved-id" })),
302
+ handler: vi.fn(async () => handlerResult),
303
+ });
304
+
305
+ await registry.executeTool({
306
+ name: "defaulted_tool",
307
+ accessToken: "token-1",
308
+ });
309
+
310
+ expect(securityInvocations[0]?.params).toEqual({ id: "resolved-id" });
311
+ });
312
+
313
+ it("passes cache helpers to the handler context when injected", async () => {
314
+ const cacheHelpers = {
315
+ generateKey: vi.fn((...segments: unknown[]) => segments.join(":")),
316
+ };
317
+
318
+ dependencies.cacheHelpers = cacheHelpers;
319
+ registry = new ToolRegistry(dependencies);
320
+
321
+ const handler = vi.fn(
322
+ async (payload: ToolExecutionPayload<{ id: string }>) => {
323
+ expect(payload.context.cache).toBe(cacheHelpers);
324
+ expect(payload.context.rawArguments).toEqual({ id: "42" });
325
+ return handlerResult;
326
+ },
327
+ );
328
+
329
+ registry.register({
330
+ name: "cache_tool",
331
+ description: "Needs cache",
332
+ inputSchema: z.object({ id: z.string() }),
333
+ handler,
334
+ });
335
+
336
+ await registry.executeTool({
337
+ name: "cache_tool",
338
+ accessToken: "token",
339
+ arguments: { id: "42" },
340
+ });
341
+
342
+ expect(handler).toHaveBeenCalledTimes(1);
343
+ });
344
+
345
+ it("extracts minify override from arguments when not explicitly provided", async () => {
346
+ const handler = vi.fn(async () => handlerResult);
347
+ registry.register({
348
+ name: "minify_hint_tool",
349
+ description: "Uses argument minify hint",
350
+ inputSchema: z.object({
351
+ id: z.string(),
352
+ _minify: z.boolean().optional(),
353
+ }),
354
+ handler,
355
+ });
356
+
357
+ let capturedFn: (() => Promise<CallToolResult>) | undefined;
358
+ let release: ((value: CallToolResult) => void) | undefined;
359
+ const formatterResolution = new Promise<CallToolResult>((resolve) => {
360
+ release = resolve;
361
+ });
362
+
363
+ responseFormatter.runWithMinifyOverride.mockImplementationOnce(
364
+ (minify, fn) => {
365
+ capturedFn = fn;
366
+ return formatterResolution;
367
+ },
368
+ );
369
+
370
+ const execution = registry.executeTool({
371
+ name: "minify_hint_tool",
372
+ accessToken: "token",
373
+ arguments: { id: "abc", _minify: false },
374
+ });
375
+
376
+ expect(responseFormatter.runWithMinifyOverride).toHaveBeenCalledWith(
377
+ false,
378
+ expect.any(Function),
379
+ );
380
+ expect(capturedFn).toBeDefined();
381
+
382
+ const manualResult = await capturedFn?.();
383
+ expect(manualResult).toEqual(handlerResult);
384
+ expect(handler).toHaveBeenCalledTimes(1);
385
+
386
+ release?.(manualResult);
387
+
388
+ const finalResult = await execution;
389
+ expect(finalResult).toEqual(handlerResult);
390
+ });
391
+
392
+ it("prefers explicit minify override option over argument hints", async () => {
393
+ const handler = vi.fn(async () => handlerResult);
394
+ registry.register({
395
+ name: "minify_option_tool",
396
+ description: "Uses option minify",
397
+ inputSchema: z.object({ id: z.string(), minify: z.boolean().optional() }),
398
+ handler,
399
+ });
400
+
401
+ let capturedFn: (() => Promise<CallToolResult>) | undefined;
402
+ let release: ((value: CallToolResult) => void) | undefined;
403
+ const formatterResolution = new Promise<CallToolResult>((resolve) => {
404
+ release = resolve;
405
+ });
406
+
407
+ responseFormatter.runWithMinifyOverride.mockImplementationOnce(
408
+ (minify, fn) => {
409
+ capturedFn = fn;
410
+ return formatterResolution;
411
+ },
412
+ );
413
+
414
+ const execution = registry.executeTool({
415
+ name: "minify_option_tool",
416
+ accessToken: "token",
417
+ arguments: { id: "abc", minify: false },
418
+ minifyOverride: true,
419
+ });
420
+
421
+ expect(responseFormatter.runWithMinifyOverride).toHaveBeenCalledWith(
422
+ true,
423
+ expect.any(Function),
424
+ );
425
+ expect(capturedFn).toBeDefined();
426
+
427
+ const manualResult = await capturedFn?.();
428
+ expect(manualResult).toEqual(handlerResult);
429
+ expect(handler).toHaveBeenCalledTimes(1);
430
+
431
+ release?.(manualResult);
432
+
433
+ const finalResult = await execution;
434
+ expect(finalResult).toEqual(handlerResult);
435
+ });
436
+
437
+ it("returns validation error result for unknown tools", async () => {
438
+ const result = await registry.executeTool({
439
+ name: "missing_tool",
440
+ accessToken: "token",
441
+ });
442
+
443
+ expect(result).toEqual(
444
+ createResult(
445
+ "validation:Unknown tool: missing_tool:The requested tool is not registered with the server",
446
+ ),
447
+ );
448
+ expect(
449
+ dependencies.errorHandler.createValidationError,
450
+ ).toHaveBeenCalledTimes(1);
451
+ });
452
+
453
+ it("surfaces validation failures from security wrapper", async () => {
454
+ registerSampleTool();
455
+
456
+ const result = await registry.executeTool({
457
+ name: "sample_tool",
458
+ accessToken: "token",
459
+ arguments: {},
460
+ });
461
+
462
+ expect(
463
+ result.content[0]?.text?.startsWith(
464
+ "validation:Invalid parameters for sample_tool:",
465
+ ),
466
+ ).toBe(true);
467
+ expect(dependencies.errorHandler.createValidationError).toHaveBeenCalled();
468
+ });
469
+
470
+ it("routes handler exceptions to error handler", async () => {
471
+ const handlerError = new Error("boom");
472
+ const handler = vi.fn(async () => {
473
+ throw handlerError;
474
+ });
475
+
476
+ registry.register({
477
+ name: "error_tool",
478
+ description: "Throws",
479
+ inputSchema: z.object({ id: z.string() }),
480
+ handler,
481
+ });
482
+
483
+ const result = await registry.executeTool({
484
+ name: "error_tool",
485
+ accessToken: "token",
486
+ arguments: { id: "abc" },
487
+ });
488
+
489
+ expect(result).toEqual(
490
+ createResult("handled:executing error_tool - error_tool:boom"),
491
+ );
492
+ expect(dependencies.errorHandler.handleError).toHaveBeenCalledWith(
493
+ handlerError,
494
+ "executing error_tool - error_tool",
495
+ );
496
+ });
497
+
498
+ it("normalizes unexpected security errors", async () => {
499
+ const error = new Error("rate limit");
500
+ const customDeps: ToolRegistryDependencies = {
501
+ ...dependencies,
502
+ withSecurityWrapper: vi.fn(() => () => () => {
503
+ throw error;
504
+ }),
505
+ };
506
+
507
+ const customRegistry = new ToolRegistry(customDeps);
508
+
509
+ // Spy on custom error handler methods for testing
510
+ vi.spyOn(customDeps.errorHandler, "handleError");
511
+
512
+ customRegistry.register({
513
+ name: "security_tool",
514
+ description: "Security throws",
515
+ inputSchema: z.object({}),
516
+ handler: vi.fn(async () => handlerResult),
517
+ });
518
+
519
+ const result = await customRegistry.executeTool({
520
+ name: "security_tool",
521
+ accessToken: "token",
522
+ });
523
+
524
+ expect(result).toEqual(
525
+ createResult("handled:executing security_tool:rate limit"),
526
+ );
527
+ expect(customDeps.errorHandler.handleError).toHaveBeenCalledWith(
528
+ error,
529
+ "executing security_tool",
530
+ );
531
+ });
532
+
533
+ it("returns permissive schema when conversion fails", () => {
534
+ registry.register({
535
+ name: "any_tool",
536
+ description: "Any schema",
537
+ // z.any is not supported by converter and should fallback
538
+ inputSchema: z.any(),
539
+ handler: vi.fn(async () => handlerResult),
540
+ });
541
+
542
+ const tool = registry.listTools().find((item) => item.name === "any_tool");
543
+ const schema = tool?.inputSchema as Record<string, unknown> | undefined;
544
+ expect(schema).toBeDefined();
545
+ expect(typeof schema?.$schema).toBe("string");
546
+ });
547
+
548
+ it("supports empty registry listings", () => {
549
+ const emptyRegistry = new ToolRegistry(dependencies);
550
+ expect(emptyRegistry.listTools()).toEqual([]);
551
+ expect(emptyRegistry.getToolDefinitions()).toEqual([]);
552
+ });
553
+
554
+ describe("ErrorHandler integration", () => {
555
+ it("should use injected ErrorHandler instance", async () => {
556
+ const mockErrorHandler = {
557
+ handleError: vi.fn(() => ({
558
+ content: [{ type: "text", text: "Mock error" }],
559
+ })),
560
+ createValidationError: vi.fn(() => ({
561
+ content: [{ type: "text", text: "Mock validation error" }],
562
+ })),
563
+ };
564
+
565
+ const customDeps = {
566
+ ...dependencies,
567
+ errorHandler: mockErrorHandler,
568
+ };
569
+
570
+ const customRegistry = new ToolRegistry(customDeps);
571
+
572
+ // Test that the registry uses the injected error handler
573
+ const result = await customRegistry.executeTool({
574
+ name: "nonexistent_tool",
575
+ accessToken: "test-token",
576
+ });
577
+
578
+ expect(mockErrorHandler.createValidationError).toHaveBeenCalled();
579
+ expect(result.content[0]?.text).toBe("Mock validation error");
580
+ });
581
+
582
+ it("should use the same formatter instance for both ErrorHandler and ToolRegistry", () => {
583
+ const {
584
+ dependencies: deps,
585
+ responseFormatter: formatter,
586
+ errorHandler,
587
+ } = makeTestDeps();
588
+
589
+ // Both should use the same formatter
590
+ expect(deps.errorHandler).toBe(errorHandler);
591
+ expect(deps.responseFormatter).toBe(formatter);
592
+ });
593
+
594
+ it("should implement ErrorHandler contract interface", () => {
595
+ const { errorHandler } = makeTestDeps();
596
+
597
+ expect(typeof errorHandler.handleError).toBe("function");
598
+ expect(typeof errorHandler.createValidationError).toBe("function");
599
+ });
600
+ });
601
+
602
+ describe("Output Schema Validation", () => {
603
+ it("validates handler output against declared output schema", async () => {
604
+ const outputSchema = z.object({
605
+ success: z.boolean(),
606
+ data: z.object({
607
+ id: z.string(),
608
+ value: z.number(),
609
+ }),
610
+ });
611
+
612
+ const handler = vi.fn(async () =>
613
+ createResult(
614
+ JSON.stringify({
615
+ success: true,
616
+ data: { id: "test-id", value: 42 },
617
+ }),
618
+ ),
619
+ );
620
+
621
+ registry.register({
622
+ name: "validated_output_tool",
623
+ description: "Has output schema",
624
+ inputSchema: z.object({ id: z.string() }),
625
+ outputSchema,
626
+ handler,
627
+ });
628
+
629
+ const result = await registry.executeTool({
630
+ name: "validated_output_tool",
631
+ accessToken: "token",
632
+ arguments: { id: "test" },
633
+ });
634
+
635
+ expect(handler).toHaveBeenCalledTimes(1);
636
+ expect(result.content[0]?.text).toContain("success");
637
+ expect(result.content[0]?.text).toContain("test-id");
638
+ });
639
+
640
+ it("rejects handler output that does not match output schema", async () => {
641
+ const outputSchema = z.object({
642
+ success: z.boolean(),
643
+ data: z.object({
644
+ id: z.string(),
645
+ value: z.number(),
646
+ }),
647
+ });
648
+
649
+ const handler = vi.fn(async () =>
650
+ createResult(
651
+ JSON.stringify({
652
+ success: true,
653
+ data: { id: "test-id", value: "not-a-number" }, // Invalid: value should be number
654
+ }),
655
+ ),
656
+ );
657
+
658
+ registry.register({
659
+ name: "invalid_output_tool",
660
+ description: "Returns invalid output",
661
+ inputSchema: z.object({ id: z.string() }),
662
+ outputSchema,
663
+ handler,
664
+ });
665
+
666
+ const result = await registry.executeTool({
667
+ name: "invalid_output_tool",
668
+ accessToken: "token",
669
+ arguments: { id: "test" },
670
+ });
671
+
672
+ expect(handler).toHaveBeenCalledTimes(1);
673
+ expect(
674
+ dependencies.errorHandler.createValidationError,
675
+ ).toHaveBeenCalled();
676
+ expect(result.content[0]?.text).toContain("Output validation failed");
677
+ expect(result.content[0]?.text).toContain("invalid_output_tool");
678
+ });
679
+
680
+ it("rejects handler output with missing required fields", async () => {
681
+ const outputSchema = z.object({
682
+ success: z.boolean(),
683
+ data: z.object({
684
+ id: z.string(),
685
+ value: z.number(),
686
+ }),
687
+ });
688
+
689
+ const handler = vi.fn(async () =>
690
+ createResult(
691
+ JSON.stringify({
692
+ success: true,
693
+ // Missing 'data' field
694
+ }),
695
+ ),
696
+ );
697
+
698
+ registry.register({
699
+ name: "missing_field_tool",
700
+ description: "Returns output missing required field",
701
+ inputSchema: z.object({ id: z.string() }),
702
+ outputSchema,
703
+ handler,
704
+ });
705
+
706
+ const result = await registry.executeTool({
707
+ name: "missing_field_tool",
708
+ accessToken: "token",
709
+ arguments: { id: "test" },
710
+ });
711
+
712
+ expect(handler).toHaveBeenCalledTimes(1);
713
+ expect(
714
+ dependencies.errorHandler.createValidationError,
715
+ ).toHaveBeenCalled();
716
+ expect(result.content[0]?.text).toContain("Output validation failed");
717
+ expect(result.content[0]?.text).toContain("missing_field_tool");
718
+ });
719
+
720
+ it("rejects handler output with invalid JSON", async () => {
721
+ const outputSchema = z.object({
722
+ success: z.boolean(),
723
+ });
724
+
725
+ const handler = vi.fn(async () => createResult("not valid json {"));
726
+
727
+ registry.register({
728
+ name: "invalid_json_tool",
729
+ description: "Returns invalid JSON",
730
+ inputSchema: z.object({ id: z.string() }),
731
+ outputSchema,
732
+ handler,
733
+ });
734
+
735
+ const result = await registry.executeTool({
736
+ name: "invalid_json_tool",
737
+ accessToken: "token",
738
+ arguments: { id: "test" },
739
+ });
740
+
741
+ expect(handler).toHaveBeenCalledTimes(1);
742
+ expect(
743
+ dependencies.errorHandler.createValidationError,
744
+ ).toHaveBeenCalled();
745
+ expect(result.content[0]?.text).toContain("Output validation failed");
746
+ expect(result.content[0]?.text).toContain("Invalid JSON");
747
+ });
748
+
749
+ it("rejects handler output with empty content", async () => {
750
+ const outputSchema = z.object({
751
+ success: z.boolean(),
752
+ });
753
+
754
+ const handler = vi.fn(async () => ({ content: [] }));
755
+
756
+ registry.register({
757
+ name: "empty_content_tool",
758
+ description: "Returns empty content",
759
+ inputSchema: z.object({ id: z.string() }),
760
+ outputSchema,
761
+ handler,
762
+ });
763
+
764
+ const result = await registry.executeTool({
765
+ name: "empty_content_tool",
766
+ accessToken: "token",
767
+ arguments: { id: "test" },
768
+ });
769
+
770
+ expect(handler).toHaveBeenCalledTimes(1);
771
+ expect(
772
+ dependencies.errorHandler.createValidationError,
773
+ ).toHaveBeenCalled();
774
+ expect(result.content[0]?.text).toContain("Output validation failed");
775
+ expect(result.content[0]?.text).toContain("empty content");
776
+ });
777
+
778
+ it("rejects handler output with non-text content", async () => {
779
+ const outputSchema = z.object({
780
+ success: z.boolean(),
781
+ });
782
+
783
+ const handler = vi.fn(async () => ({
784
+ content: [{ type: "image", data: "base64..." }],
785
+ }));
786
+
787
+ registry.register({
788
+ name: "non_text_tool",
789
+ description: "Returns non-text content",
790
+ inputSchema: z.object({ id: z.string() }),
791
+ outputSchema,
792
+ handler,
793
+ });
794
+
795
+ const result = await registry.executeTool({
796
+ name: "non_text_tool",
797
+ accessToken: "token",
798
+ arguments: { id: "test" },
799
+ });
800
+
801
+ expect(handler).toHaveBeenCalledTimes(1);
802
+ expect(
803
+ dependencies.errorHandler.createValidationError,
804
+ ).toHaveBeenCalled();
805
+ expect(result.content[0]?.text).toContain("Output validation failed");
806
+ expect(result.content[0]?.text).toContain(
807
+ "Handler returned invalid content items",
808
+ );
809
+ expect(result.content[0]?.text).toContain(
810
+ 'Item 0: type is "image" instead of "text"',
811
+ );
812
+ });
813
+
814
+ it("rejects handler output with multiple invalid content items", async () => {
815
+ const outputSchema = z.object({
816
+ success: z.boolean(),
817
+ });
818
+
819
+ const handler = vi.fn(async () => ({
820
+ content: [
821
+ { type: "text", text: '{"success": true}' }, // Valid
822
+ { type: "image", data: "base64..." }, // Invalid: wrong type
823
+ { type: "text", text: 123 }, // Invalid: text is not string
824
+ { type: "text" }, // Invalid: missing text property
825
+ ],
826
+ }));
827
+
828
+ registry.register({
829
+ name: "multi_invalid_tool",
830
+ description: "Returns multiple content items with some invalid",
831
+ inputSchema: z.object({ id: z.string() }),
832
+ outputSchema,
833
+ handler,
834
+ });
835
+
836
+ const result = await registry.executeTool({
837
+ name: "multi_invalid_tool",
838
+ accessToken: "token",
839
+ arguments: { id: "test" },
840
+ });
841
+
842
+ expect(handler).toHaveBeenCalledTimes(1);
843
+ expect(
844
+ dependencies.errorHandler.createValidationError,
845
+ ).toHaveBeenCalled();
846
+ expect(result.content[0]?.text).toContain("Output validation failed");
847
+ expect(result.content[0]?.text).toContain(
848
+ "Handler returned invalid content items",
849
+ );
850
+ expect(result.content[0]?.text).toContain("3 of 4 failed");
851
+ expect(result.content[0]?.text).toContain(
852
+ 'Item 1: type is "image" instead of "text"',
853
+ );
854
+ expect(result.content[0]?.text).toContain(
855
+ "Item 2: text property is number instead of string",
856
+ );
857
+ expect(result.content[0]?.text).toContain(
858
+ "Item 3: text property is undefined instead of string",
859
+ );
860
+ });
861
+
862
+ it("skips validation when no output schema is defined", async () => {
863
+ const handler = vi.fn(async () => createResult("any output format"));
864
+
865
+ registry.register({
866
+ name: "no_schema_tool",
867
+ description: "Has no output schema",
868
+ inputSchema: z.object({ id: z.string() }),
869
+ // No outputSchema defined
870
+ handler,
871
+ });
872
+
873
+ const result = await registry.executeTool({
874
+ name: "no_schema_tool",
875
+ accessToken: "token",
876
+ arguments: { id: "test" },
877
+ });
878
+
879
+ expect(handler).toHaveBeenCalledTimes(1);
880
+ expect(result.content[0]?.text).toBe("any output format");
881
+ // Error handler should not be called for validation
882
+ expect(
883
+ dependencies.errorHandler.createValidationError,
884
+ ).not.toHaveBeenCalled();
885
+ });
886
+
887
+ it("includes output schema in tool listing when defined", () => {
888
+ const outputSchema = z.object({
889
+ success: z.boolean(),
890
+ message: z.string(),
891
+ });
892
+
893
+ registry.register({
894
+ name: "schema_listing_tool",
895
+ description: "Has output schema",
896
+ inputSchema: z.object({ id: z.string() }),
897
+ outputSchema,
898
+ handler: vi.fn(async () => handlerResult),
899
+ });
900
+
901
+ const tools = registry.listTools();
902
+ const tool = tools.find((t) => t.name === "schema_listing_tool");
903
+
904
+ expect(tool).toBeDefined();
905
+ expect(tool?.outputSchema).toBeDefined();
906
+ const schema = tool?.outputSchema as Record<string, unknown> | undefined;
907
+ expect(schema).toMatchObject({
908
+ type: "object",
909
+ properties: expect.objectContaining({
910
+ success: expect.objectContaining({ type: "boolean" }),
911
+ message: expect.objectContaining({ type: "string" }),
912
+ }),
913
+ required: ["success", "message"],
914
+ });
915
+ });
916
+ });
855
917
  });