@dizzlkheinz/ynab-mcpb 0.18.3 → 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.
- package/CHANGELOG.md +17 -0
- package/CLAUDE.md +87 -8
- package/bin/ynab-mcp-server.cjs +2 -2
- package/bin/ynab-mcp-server.js +3 -3
- package/biome.json +39 -0
- package/dist/bundle/index.cjs +67 -67
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -27
- package/dist/server/YNABMCPServer.d.ts +3 -4
- package/dist/server/YNABMCPServer.js +111 -116
- package/dist/server/budgetResolver.d.ts +6 -5
- package/dist/server/budgetResolver.js +46 -36
- package/dist/server/cacheKeys.js +6 -6
- package/dist/server/cacheManager.js +14 -11
- package/dist/server/completions.d.ts +2 -2
- package/dist/server/completions.js +20 -15
- package/dist/server/config.d.ts +10 -5
- package/dist/server/config.js +24 -7
- package/dist/server/deltaCache.d.ts +2 -2
- package/dist/server/deltaCache.js +22 -16
- package/dist/server/deltaCache.merge.d.ts +2 -2
- package/dist/server/diagnostics.d.ts +4 -4
- package/dist/server/diagnostics.js +38 -32
- package/dist/server/errorHandler.d.ts +5 -12
- package/dist/server/errorHandler.js +219 -217
- package/dist/server/prompts.d.ts +2 -2
- package/dist/server/prompts.js +45 -45
- package/dist/server/rateLimiter.js +4 -4
- package/dist/server/requestLogger.d.ts +1 -1
- package/dist/server/requestLogger.js +40 -35
- package/dist/server/resources.d.ts +3 -3
- package/dist/server/resources.js +55 -52
- package/dist/server/responseFormatter.js +6 -6
- package/dist/server/securityMiddleware.d.ts +2 -2
- package/dist/server/securityMiddleware.js +22 -20
- package/dist/server/serverKnowledgeStore.js +1 -1
- package/dist/server/toolRegistry.d.ts +3 -3
- package/dist/server/toolRegistry.js +47 -40
- package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
- package/dist/tools/__tests__/deltaTestUtils.js +2 -2
- package/dist/tools/accountTools.d.ts +9 -8
- package/dist/tools/accountTools.js +47 -47
- package/dist/tools/adapters.d.ts +13 -8
- package/dist/tools/adapters.js +21 -11
- package/dist/tools/budgetTools.d.ts +8 -7
- package/dist/tools/budgetTools.js +22 -22
- package/dist/tools/categoryTools.d.ts +9 -8
- package/dist/tools/categoryTools.js +68 -59
- package/dist/tools/compareTransactions/formatter.d.ts +3 -3
- package/dist/tools/compareTransactions/formatter.js +9 -9
- package/dist/tools/compareTransactions/index.d.ts +6 -6
- package/dist/tools/compareTransactions/index.js +58 -43
- package/dist/tools/compareTransactions/matcher.d.ts +1 -1
- package/dist/tools/compareTransactions/matcher.js +28 -15
- package/dist/tools/compareTransactions/parser.d.ts +2 -2
- package/dist/tools/compareTransactions/parser.js +144 -138
- package/dist/tools/compareTransactions/types.d.ts +4 -4
- package/dist/tools/compareTransactions.d.ts +1 -1
- package/dist/tools/compareTransactions.js +1 -1
- package/dist/tools/deltaFetcher.d.ts +2 -2
- package/dist/tools/deltaFetcher.js +16 -15
- package/dist/tools/deltaSupport.d.ts +4 -4
- package/dist/tools/deltaSupport.js +35 -41
- package/dist/tools/exportTransactions.d.ts +5 -4
- package/dist/tools/exportTransactions.js +61 -59
- package/dist/tools/monthTools.d.ts +7 -6
- package/dist/tools/monthTools.js +31 -29
- package/dist/tools/payeeTools.d.ts +7 -6
- package/dist/tools/payeeTools.js +28 -28
- package/dist/tools/reconcileAdapter.d.ts +2 -2
- package/dist/tools/reconcileAdapter.js +21 -11
- package/dist/tools/reconciliation/analyzer.d.ts +4 -4
- package/dist/tools/reconciliation/analyzer.js +136 -57
- package/dist/tools/reconciliation/csvParser.d.ts +3 -3
- package/dist/tools/reconciliation/csvParser.js +128 -104
- package/dist/tools/reconciliation/executor.d.ts +4 -4
- package/dist/tools/reconciliation/executor.js +148 -109
- package/dist/tools/reconciliation/index.d.ts +10 -10
- package/dist/tools/reconciliation/index.js +96 -83
- package/dist/tools/reconciliation/matcher.d.ts +3 -3
- package/dist/tools/reconciliation/matcher.js +17 -16
- package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
- package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
- package/dist/tools/reconciliation/recommendationEngine.js +40 -40
- package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
- package/dist/tools/reconciliation/reportFormatter.js +79 -54
- package/dist/tools/reconciliation/signDetector.d.ts +1 -1
- package/dist/tools/reconciliation/types.d.ts +19 -16
- package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
- package/dist/tools/schemas/common.d.ts +1 -1
- package/dist/tools/schemas/common.js +1 -1
- package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
- package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
- package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
- package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
- package/dist/tools/schemas/outputs/index.d.ts +14 -14
- package/dist/tools/schemas/outputs/index.js +14 -14
- package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
- package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
- package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
- package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
- package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
- package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
- package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
- package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
- package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
- package/dist/tools/schemas/shared/commonOutputs.js +15 -9
- package/dist/tools/transactionReadTools.d.ts +11 -0
- package/dist/tools/transactionReadTools.js +202 -0
- package/dist/tools/transactionSchemas.d.ts +309 -0
- package/dist/tools/transactionSchemas.js +235 -0
- package/dist/tools/transactionTools.d.ts +6 -302
- package/dist/tools/transactionTools.js +7 -2054
- package/dist/tools/transactionUtils.d.ts +31 -0
- package/dist/tools/transactionUtils.js +364 -0
- package/dist/tools/transactionWriteTools.d.ts +20 -0
- package/dist/tools/transactionWriteTools.js +1342 -0
- package/dist/tools/utilityTools.d.ts +5 -4
- package/dist/tools/utilityTools.js +11 -11
- package/dist/types/index.d.ts +7 -7
- package/dist/types/index.js +6 -6
- package/dist/types/reconciliation.d.ts +1 -1
- package/dist/types/toolRegistration.d.ts +14 -12
- package/dist/utils/amountUtils.js +1 -1
- package/dist/utils/dateUtils.js +4 -4
- package/dist/utils/errors.d.ts +3 -3
- package/dist/utils/errors.js +4 -4
- package/dist/utils/money.d.ts +2 -2
- package/dist/utils/money.js +8 -8
- package/dist/utils/validationError.d.ts +1 -1
- package/dist/utils/validationError.js +1 -1
- package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
- package/docs/assets/schemas/reconciliation-v2.json +360 -336
- package/docs/plans/2025-12-25-transaction-tools-refactor-design.md +211 -0
- package/docs/plans/2025-12-25-transaction-tools-refactor.md +905 -0
- package/esbuild.config.mjs +53 -50
- package/meta.json +12548 -12548
- package/package.json +98 -109
- package/scripts/analyze-bundle.mjs +33 -30
- package/scripts/create-pr-description.js +169 -120
- package/scripts/run-all-tests.js +205 -0
- package/scripts/run-domain-integration-tests.js +28 -18
- package/scripts/run-generate-mcpb.js +19 -17
- package/scripts/run-throttled-integration-tests.js +92 -83
- package/scripts/test-delta-params.mjs +149 -120
- package/scripts/test-recommendations.ts +36 -32
- package/scripts/tmpTransaction.ts +80 -43
- package/scripts/validate-env.js +98 -91
- package/scripts/verify-build.js +78 -76
- package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
- package/src/__tests__/performance.test.ts +723 -671
- package/src/__tests__/setup.ts +442 -395
- package/src/__tests__/smoke.e2e.test.ts +41 -39
- package/src/__tests__/testRunner.ts +314 -295
- package/src/__tests__/testUtils.ts +456 -364
- package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
- package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
- package/src/index.ts +68 -59
- package/src/server/CLAUDE.md +480 -0
- package/src/server/YNABMCPServer.ts +821 -794
- package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
- package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
- package/src/server/__tests__/budgetResolver.test.ts +466 -423
- package/src/server/__tests__/cacheManager.test.ts +891 -874
- package/src/server/__tests__/completions.integration.test.ts +115 -106
- package/src/server/__tests__/completions.test.ts +334 -313
- package/src/server/__tests__/config.test.ts +98 -86
- package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
- package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
- package/src/server/__tests__/deltaCache.test.ts +946 -759
- package/src/server/__tests__/diagnostics.test.ts +825 -792
- package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
- package/src/server/__tests__/errorHandler.test.ts +402 -397
- package/src/server/__tests__/prompts.test.ts +424 -347
- package/src/server/__tests__/rateLimiter.test.ts +313 -309
- package/src/server/__tests__/requestLogger.test.ts +443 -403
- package/src/server/__tests__/resources.template.test.ts +196 -185
- package/src/server/__tests__/resources.test.ts +294 -288
- package/src/server/__tests__/security.integration.test.ts +487 -421
- package/src/server/__tests__/securityMiddleware.test.ts +519 -444
- package/src/server/__tests__/server-startup.integration.test.ts +509 -490
- package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
- package/src/server/__tests__/toolRegistration.test.ts +239 -210
- package/src/server/__tests__/toolRegistry.test.ts +907 -845
- package/src/server/budgetResolver.ts +221 -181
- package/src/server/cacheKeys.ts +6 -6
- package/src/server/cacheManager.ts +498 -484
- package/src/server/completions.ts +267 -243
- package/src/server/config.ts +35 -14
- package/src/server/deltaCache.merge.ts +146 -128
- package/src/server/deltaCache.ts +352 -309
- package/src/server/diagnostics.ts +257 -242
- package/src/server/errorHandler.ts +747 -744
- package/src/server/prompts.ts +181 -176
- package/src/server/rateLimiter.ts +131 -129
- package/src/server/requestLogger.ts +350 -322
- package/src/server/resources.ts +442 -374
- package/src/server/responseFormatter.ts +41 -37
- package/src/server/securityMiddleware.ts +223 -205
- package/src/server/serverKnowledgeStore.ts +67 -67
- package/src/server/toolRegistry.ts +508 -474
- package/src/tools/CLAUDE.md +604 -0
- package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
- package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
- package/src/tools/__tests__/accountTools.test.ts +685 -638
- package/src/tools/__tests__/adapters.test.ts +142 -108
- package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
- package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
- package/src/tools/__tests__/budgetTools.test.ts +442 -413
- package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
- package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
- package/src/tools/__tests__/categoryTools.test.ts +656 -625
- package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
- package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
- package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
- package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
- package/src/tools/__tests__/compareTransactions.test.ts +352 -332
- package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
- package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
- package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
- package/src/tools/__tests__/deltaSupport.test.ts +211 -184
- package/src/tools/__tests__/deltaTestUtils.ts +37 -33
- package/src/tools/__tests__/exportTransactions.test.ts +205 -200
- package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
- package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
- package/src/tools/__tests__/monthTools.test.ts +561 -512
- package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
- package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
- package/src/tools/__tests__/payeeTools.test.ts +486 -434
- package/src/tools/__tests__/transactionSchemas.test.ts +1204 -0
- package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
- package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
- package/src/tools/__tests__/transactionUtils.test.ts +1016 -0
- package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
- package/src/tools/__tests__/utilityTools.test.ts +68 -58
- package/src/tools/accountTools.ts +293 -271
- package/src/tools/adapters.ts +120 -63
- package/src/tools/budgetTools.ts +121 -116
- package/src/tools/categoryTools.ts +379 -339
- package/src/tools/compareTransactions/formatter.ts +131 -119
- package/src/tools/compareTransactions/index.ts +249 -214
- package/src/tools/compareTransactions/matcher.ts +259 -209
- package/src/tools/compareTransactions/parser.ts +517 -487
- package/src/tools/compareTransactions/types.ts +38 -38
- package/src/tools/compareTransactions.ts +1 -1
- package/src/tools/deltaFetcher.ts +281 -260
- package/src/tools/deltaSupport.ts +264 -259
- package/src/tools/exportTransactions.ts +230 -218
- package/src/tools/monthTools.ts +180 -165
- package/src/tools/payeeTools.ts +152 -140
- package/src/tools/reconcileAdapter.ts +297 -246
- package/src/tools/reconciliation/CLAUDE.md +506 -0
- package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +135 -112
- package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -227
- package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -335
- package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
- package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
- package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
- package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
- package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
- package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
- package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
- package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -986
- package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
- package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -530
- package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -71
- package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -58
- package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
- package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +58 -43
- package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
- package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
- package/src/tools/reconciliation/analyzer.ts +582 -406
- package/src/tools/reconciliation/csvParser.ts +656 -609
- package/src/tools/reconciliation/executor.ts +1290 -1128
- package/src/tools/reconciliation/index.ts +580 -528
- package/src/tools/reconciliation/matcher.ts +256 -240
- package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
- package/src/tools/reconciliation/recommendationEngine.ts +357 -345
- package/src/tools/reconciliation/reportFormatter.ts +349 -276
- package/src/tools/reconciliation/signDetector.ts +89 -83
- package/src/tools/reconciliation/types.ts +164 -153
- package/src/tools/reconciliation/ynabAdapter.ts +17 -15
- package/src/tools/schemas/CLAUDE.md +546 -0
- package/src/tools/schemas/common.ts +1 -1
- package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
- package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
- package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
- package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
- package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
- package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
- package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
- package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
- package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
- package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
- package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
- package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
- package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
- package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
- package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
- package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
- package/src/tools/schemas/outputs/index.ts +163 -163
- package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
- package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
- package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
- package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
- package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
- package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
- package/src/tools/schemas/shared/commonOutputs.ts +27 -19
- package/src/tools/toolCategories.ts +114 -114
- package/src/tools/transactionReadTools.ts +327 -0
- package/src/tools/transactionSchemas.ts +484 -0
- package/src/tools/transactionTools.ts +107 -2990
- package/src/tools/transactionUtils.ts +621 -0
- package/src/tools/transactionWriteTools.ts +2110 -0
- package/src/tools/utilityTools.ts +46 -41
- package/src/types/CLAUDE.md +477 -0
- package/src/types/__tests__/index.test.ts +51 -51
- package/src/types/index.ts +43 -39
- package/src/types/integration-tests.d.ts +26 -26
- package/src/types/reconciliation.ts +29 -29
- package/src/types/toolAnnotations.ts +30 -30
- package/src/types/toolRegistration.ts +43 -32
- package/src/utils/CLAUDE.md +508 -0
- package/src/utils/__tests__/dateUtils.test.ts +174 -168
- package/src/utils/__tests__/money.test.ts +193 -187
- package/src/utils/amountUtils.ts +5 -5
- package/src/utils/baseError.ts +5 -5
- package/src/utils/dateUtils.ts +29 -26
- package/src/utils/errors.ts +14 -14
- package/src/utils/money.ts +66 -52
- package/src/utils/validationError.ts +1 -1
- package/tsconfig.json +29 -29
- package/tsconfig.prod.json +16 -16
- package/vitest-reporters/split-json-reporter.ts +247 -204
- package/vitest.config.ts +99 -95
- package/.prettierignore +0 -10
- package/.prettierrc.json +0 -10
- package/eslint.config.js +0 -49
|
@@ -1,855 +1,917 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type * as ynab from
|
|
4
|
-
import
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
ToolExecutionPayload,
|
|
10
|
-
} from '../toolRegistry.js';
|
|
6
|
+
ReconcileAccountSchema,
|
|
7
|
+
handleReconcileAccount,
|
|
8
|
+
} from "../../tools/reconciliation/index.js";
|
|
11
9
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
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
|
});
|