@casys/mcp-server 0.26.1 → 0.27.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 +1779 -0
- package/LICENSE +21 -0
- package/README.md +15 -4
- package/esm/_dnt.polyfills.d.ts +12 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts +33 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.js +48 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts +106 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.js +636 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts +67 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.js +1393 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts +44 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.js +117 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.js +103 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.js +32 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.js +96 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.js +159 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.js +14 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.js +10 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.js +22 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.js +29 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.js +19 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.js +30 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.js +10 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.js +14 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.js +9 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.js +81 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.js +20 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts +32 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.js +6 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts +4 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.js +13 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts +51 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.js +53 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts +79 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.js +91 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts +100 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.js +33 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts +111 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.js +104 -0
- package/esm/mod.d.ts +102 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +103 -0
- package/esm/package.json +3 -0
- package/esm/src/auth/as-metadata-proxy.d.ts +112 -0
- package/esm/src/auth/as-metadata-proxy.d.ts.map +1 -0
- package/esm/src/auth/as-metadata-proxy.js +171 -0
- package/esm/src/auth/config.d.ts +117 -0
- package/esm/src/auth/config.d.ts.map +1 -0
- package/esm/src/auth/config.js +219 -0
- package/esm/src/auth/jwt-provider.d.ts +157 -0
- package/esm/src/auth/jwt-provider.d.ts.map +1 -0
- package/esm/src/auth/jwt-provider.js +187 -0
- package/esm/src/auth/middleware.d.ts +61 -0
- package/esm/src/auth/middleware.d.ts.map +1 -0
- package/esm/src/auth/middleware.js +209 -0
- package/esm/src/auth/mod.d.ts +23 -0
- package/esm/src/auth/mod.d.ts.map +1 -0
- package/esm/src/auth/mod.js +27 -0
- package/esm/src/auth/multi-tenant-middleware.d.ts +131 -0
- package/esm/src/auth/multi-tenant-middleware.d.ts.map +1 -0
- package/esm/src/auth/multi-tenant-middleware.js +128 -0
- package/esm/src/auth/presets.d.ts +231 -0
- package/esm/src/auth/presets.d.ts.map +1 -0
- package/esm/src/auth/presets.js +223 -0
- package/{src/auth/provider.ts → esm/src/auth/provider.d.ts} +13 -15
- package/esm/src/auth/provider.d.ts.map +1 -0
- package/esm/src/auth/provider.js +34 -0
- package/esm/src/auth/scope-middleware.d.ts +30 -0
- package/esm/src/auth/scope-middleware.d.ts.map +1 -0
- package/esm/src/auth/scope-middleware.js +48 -0
- package/esm/src/auth/static-token-provider.d.ts +155 -0
- package/esm/src/auth/static-token-provider.d.ts.map +1 -0
- package/esm/src/auth/static-token-provider.js +256 -0
- package/esm/src/auth/types.d.ts +151 -0
- package/esm/src/auth/types.d.ts.map +1 -0
- package/esm/src/auth/types.js +64 -0
- package/esm/src/client-auth/callback-server.d.ts +34 -0
- package/esm/src/client-auth/callback-server.d.ts.map +1 -0
- package/esm/src/client-auth/callback-server.js +141 -0
- package/esm/src/client-auth/client-id-metadata.d.ts +29 -0
- package/esm/src/client-auth/client-id-metadata.d.ts.map +1 -0
- package/esm/src/client-auth/client-id-metadata.js +149 -0
- package/esm/src/client-auth/connect.d.ts +32 -0
- package/esm/src/client-auth/connect.d.ts.map +1 -0
- package/esm/src/client-auth/connect.js +47 -0
- package/esm/src/client-auth/mod.d.ts +19 -0
- package/esm/src/client-auth/mod.d.ts.map +1 -0
- package/{src/client-auth/mod.ts → esm/src/client-auth/mod.js} +1 -26
- package/esm/src/client-auth/provider.d.ts +78 -0
- package/esm/src/client-auth/provider.d.ts.map +1 -0
- package/esm/src/client-auth/provider.js +246 -0
- package/esm/src/client-auth/token-store/file-store.d.ts +20 -0
- package/esm/src/client-auth/token-store/file-store.d.ts.map +1 -0
- package/esm/src/client-auth/token-store/file-store.js +62 -0
- package/esm/src/client-auth/token-store/memory-store.d.ts +14 -0
- package/esm/src/client-auth/token-store/memory-store.d.ts.map +1 -0
- package/esm/src/client-auth/token-store/memory-store.js +25 -0
- package/esm/src/client-auth/types.d.ts +89 -0
- package/esm/src/client-auth/types.d.ts.map +1 -0
- package/esm/src/client-auth/types.js +10 -0
- package/esm/src/concurrency/rate-limiter.d.ts +87 -0
- package/esm/src/concurrency/rate-limiter.d.ts.map +1 -0
- package/esm/src/concurrency/rate-limiter.js +165 -0
- package/esm/src/concurrency/request-queue.d.ts +71 -0
- package/esm/src/concurrency/request-queue.d.ts.map +1 -0
- package/esm/src/concurrency/request-queue.js +128 -0
- package/esm/src/http/body.d.ts +20 -0
- package/esm/src/http/body.d.ts.map +1 -0
- package/esm/src/http/body.js +63 -0
- package/esm/src/http/request-guards.d.ts +54 -0
- package/esm/src/http/request-guards.d.ts.map +1 -0
- package/esm/src/http/request-guards.js +114 -0
- package/esm/src/http/wire.d.ts +26 -0
- package/esm/src/http/wire.d.ts.map +1 -0
- package/{src/http/wire.ts → esm/src/http/wire.js} +13 -19
- package/esm/src/inspector/launcher.d.ts +48 -0
- package/esm/src/inspector/launcher.d.ts.map +1 -0
- package/esm/src/inspector/launcher.js +100 -0
- package/esm/src/mcp-app.d.ts +728 -0
- package/esm/src/mcp-app.d.ts.map +1 -0
- package/esm/src/mcp-app.js +3530 -0
- package/{src/middleware/backpressure.ts → esm/src/middleware/backpressure.d.ts} +2 -14
- package/esm/src/middleware/backpressure.d.ts.map +1 -0
- package/esm/src/middleware/backpressure.js +31 -0
- package/esm/src/middleware/mod.d.ts +11 -0
- package/esm/src/middleware/mod.d.ts.map +1 -0
- package/{src/middleware/mod.ts → esm/src/middleware/mod.js} +0 -10
- package/{src/middleware/rate-limit.ts → esm/src/middleware/rate-limit.d.ts} +2 -25
- package/esm/src/middleware/rate-limit.d.ts.map +1 -0
- package/esm/src/middleware/rate-limit.js +33 -0
- package/esm/src/middleware/runner.d.ts +32 -0
- package/esm/src/middleware/runner.d.ts.map +1 -0
- package/esm/src/middleware/runner.js +49 -0
- package/esm/src/middleware/types.d.ts +76 -0
- package/esm/src/middleware/types.d.ts.map +1 -0
- package/esm/src/middleware/types.js +9 -0
- package/esm/src/middleware/validation.d.ts +24 -0
- package/esm/src/middleware/validation.d.ts.map +1 -0
- package/esm/src/middleware/validation.js +30 -0
- package/esm/src/mrtr/capability-check.d.ts +75 -0
- package/esm/src/mrtr/capability-check.d.ts.map +1 -0
- package/esm/src/mrtr/capability-check.js +233 -0
- package/esm/src/mrtr/mod.d.ts +18 -0
- package/esm/src/mrtr/mod.d.ts.map +1 -0
- package/esm/src/mrtr/mod.js +12 -0
- package/esm/src/mrtr/replay-store.d.ts +50 -0
- package/esm/src/mrtr/replay-store.d.ts.map +1 -0
- package/esm/src/mrtr/replay-store.js +61 -0
- package/esm/src/mrtr/request-state.d.ts +181 -0
- package/esm/src/mrtr/request-state.d.ts.map +1 -0
- package/esm/src/mrtr/request-state.js +313 -0
- package/esm/src/mrtr/retry-guard.d.ts +61 -0
- package/esm/src/mrtr/retry-guard.d.ts.map +1 -0
- package/esm/src/mrtr/retry-guard.js +53 -0
- package/esm/src/mrtr/types.d.ts +99 -0
- package/esm/src/mrtr/types.d.ts.map +1 -0
- package/esm/src/mrtr/types.js +14 -0
- package/esm/src/observability/metrics.d.ts +104 -0
- package/esm/src/observability/metrics.d.ts.map +1 -0
- package/esm/src/observability/metrics.js +207 -0
- package/{src/observability/mod.ts → esm/src/observability/mod.d.ts} +2 -11
- package/esm/src/observability/mod.d.ts.map +1 -0
- package/esm/src/observability/mod.js +11 -0
- package/esm/src/observability/otel.d.ts +73 -0
- package/esm/src/observability/otel.d.ts.map +1 -0
- package/esm/src/observability/otel.js +260 -0
- package/esm/src/runtime/runtime.d.ts +10 -0
- package/esm/src/runtime/runtime.d.ts.map +1 -0
- package/esm/src/runtime/runtime.deno.d.ts +50 -0
- package/esm/src/runtime/runtime.deno.d.ts.map +1 -0
- package/esm/src/runtime/runtime.deno.js +120 -0
- package/{src/runtime/runtime.ts → esm/src/runtime/runtime.js} +5 -14
- package/esm/src/runtime/runtime.node.d.ts +51 -0
- package/esm/src/runtime/runtime.node.d.ts.map +1 -0
- package/esm/src/runtime/runtime.node.js +268 -0
- package/{src/runtime/types.ts → esm/src/runtime/types.d.ts} +28 -51
- package/esm/src/runtime/types.d.ts.map +1 -0
- package/esm/src/runtime/types.js +12 -0
- package/esm/src/security/channel-hmac.d.ts +43 -0
- package/esm/src/security/channel-hmac.d.ts.map +1 -0
- package/{src/security/channel-hmac.ts → esm/src/security/channel-hmac.js} +12 -17
- package/esm/src/security/csp.d.ts +50 -0
- package/esm/src/security/csp.d.ts.map +1 -0
- package/esm/src/security/csp.js +63 -0
- package/esm/src/security/message-signer.d.ts +88 -0
- package/esm/src/security/message-signer.d.ts.map +1 -0
- package/esm/src/security/message-signer.js +166 -0
- package/esm/src/subscriptions/subscription-registry.d.ts +256 -0
- package/esm/src/subscriptions/subscription-registry.d.ts.map +1 -0
- package/esm/src/subscriptions/subscription-registry.js +406 -0
- package/{src/tasks/mod.ts → esm/src/tasks/mod.d.ts} +3 -23
- package/esm/src/tasks/mod.d.ts.map +1 -0
- package/esm/src/tasks/mod.js +11 -0
- package/esm/src/tasks/task-store.d.ts +342 -0
- package/esm/src/tasks/task-store.d.ts.map +1 -0
- package/esm/src/tasks/task-store.js +649 -0
- package/esm/src/transport/request-headers.d.ts +145 -0
- package/esm/src/transport/request-headers.d.ts.map +1 -0
- package/esm/src/transport/request-headers.js +525 -0
- package/esm/src/types.d.ts +776 -0
- package/esm/src/types.d.ts.map +1 -0
- package/esm/src/types.js +92 -0
- package/esm/src/ui/viewer-utils.d.ts +45 -0
- package/esm/src/ui/viewer-utils.d.ts.map +1 -0
- package/esm/src/ui/viewer-utils.js +97 -0
- package/esm/src/validation/schema-validator.d.ts +115 -0
- package/esm/src/validation/schema-validator.d.ts.map +1 -0
- package/esm/src/validation/schema-validator.js +249 -0
- package/package.json +35 -24
- package/mod.ts +0 -302
- package/src/auth/as-metadata-proxy.ts +0 -288
- package/src/auth/config.ts +0 -368
- package/src/auth/jwt-provider.ts +0 -327
- package/src/auth/middleware.ts +0 -257
- package/src/auth/mod.ts +0 -89
- package/src/auth/multi-tenant-middleware.ts +0 -236
- package/src/auth/presets.ts +0 -351
- package/src/auth/scope-middleware.ts +0 -59
- package/src/auth/static-token-provider.ts +0 -197
- package/src/auth/types.ts +0 -207
- package/src/client-auth/callback-server.ts +0 -166
- package/src/client-auth/client-id-metadata.ts +0 -260
- package/src/client-auth/connect.ts +0 -72
- package/src/client-auth/provider.ts +0 -300
- package/src/client-auth/token-store/file-store.ts +0 -73
- package/src/client-auth/token-store/memory-store.ts +0 -32
- package/src/client-auth/types.ts +0 -95
- package/src/concurrency/rate-limiter.ts +0 -190
- package/src/concurrency/request-queue.ts +0 -140
- package/src/http/body.ts +0 -72
- package/src/http/request-guards.ts +0 -137
- package/src/inspector/launcher.ts +0 -131
- package/src/mcp-app.ts +0 -4595
- package/src/middleware/runner.ts +0 -63
- package/src/middleware/types.ts +0 -104
- package/src/middleware/validation.ts +0 -28
- package/src/mrtr/capability-check.ts +0 -303
- package/src/mrtr/mod.ts +0 -54
- package/src/mrtr/replay-store.ts +0 -103
- package/src/mrtr/request-state.ts +0 -435
- package/src/mrtr/retry-guard.ts +0 -110
- package/src/mrtr/types.ts +0 -107
- package/src/observability/metrics.ts +0 -336
- package/src/observability/otel.ts +0 -330
- package/src/runtime/runtime.deno.ts +0 -149
- package/src/runtime/runtime.node.ts +0 -324
- package/src/security/csp.ts +0 -89
- package/src/security/message-signer.ts +0 -223
- package/src/subscriptions/subscription-registry.ts +0 -558
- package/src/tasks/task-store.ts +0 -1014
- package/src/transport/request-headers.ts +0 -661
- package/src/types.ts +0 -908
- package/src/ui/viewer-utils.ts +0 -117
- package/src/validation/schema-validator.ts +0 -307
package/src/middleware/runner.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Middleware pipeline runner.
|
|
3
|
-
*
|
|
4
|
-
* Composes an array of middlewares into a single callable function
|
|
5
|
-
* using the onion model: each middleware wraps the next.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/middleware/runner
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
Middleware,
|
|
12
|
-
MiddlewareContext,
|
|
13
|
-
MiddlewareResult,
|
|
14
|
-
} from "./types.js";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Create a middleware runner that composes middlewares + a final handler.
|
|
18
|
-
*
|
|
19
|
-
* Execution order (onion model):
|
|
20
|
-
* ```
|
|
21
|
-
* m1-before → m2-before → handler → m2-after → m1-after
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @param middlewares - Array of middleware functions to execute in order
|
|
25
|
-
* @param handler - Final handler (the tool execution)
|
|
26
|
-
* @returns A function that runs the full pipeline for a given context
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const run = createMiddlewareRunner(
|
|
31
|
-
* [rateLimitMiddleware, validationMiddleware],
|
|
32
|
-
* async (ctx) => toolHandler(ctx.args),
|
|
33
|
-
* );
|
|
34
|
-
* const result = await run({ toolName: "my_tool", args: { x: 1 } });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export function createMiddlewareRunner(
|
|
38
|
-
middlewares: Middleware[],
|
|
39
|
-
handler: (ctx: MiddlewareContext) => Promise<MiddlewareResult>,
|
|
40
|
-
): (ctx: MiddlewareContext) => Promise<MiddlewareResult> {
|
|
41
|
-
return (ctx: MiddlewareContext) => {
|
|
42
|
-
let index = 0;
|
|
43
|
-
let handlerCalled = false;
|
|
44
|
-
|
|
45
|
-
// deno-lint-ignore require-await
|
|
46
|
-
const next = async (): Promise<MiddlewareResult> => {
|
|
47
|
-
if (index < middlewares.length) {
|
|
48
|
-
const middleware = middlewares[index++];
|
|
49
|
-
return middleware(ctx, next);
|
|
50
|
-
}
|
|
51
|
-
if (handlerCalled) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
"[MiddlewareRunner] next() called after pipeline already completed. " +
|
|
54
|
-
"A middleware may be calling next() multiple times.",
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
handlerCalled = true;
|
|
58
|
-
return handler(ctx);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
return next();
|
|
62
|
-
};
|
|
63
|
-
}
|
package/src/middleware/types.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Middleware pipeline types for McpApp.
|
|
3
|
-
*
|
|
4
|
-
* Provides an onion-model middleware system (similar to Koa/Hono)
|
|
5
|
-
* where each middleware wraps the next, enabling before/after logic.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/middleware/types
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
ClientCapabilities,
|
|
12
|
-
Implementation,
|
|
13
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* MCP logging severities (RFC 5424 syslog levels), ordered least to most severe.
|
|
17
|
-
*
|
|
18
|
-
* Declared locally rather than imported: the SDK's `LoggingLevel` is tied to the
|
|
19
|
-
* `logging/setLevel` RPC that spec 2026-07-28 removed, and this type now serves a
|
|
20
|
-
* per-request field instead.
|
|
21
|
-
*/
|
|
22
|
-
export type McpLogLevel =
|
|
23
|
-
| "debug"
|
|
24
|
-
| "info"
|
|
25
|
-
| "notice"
|
|
26
|
-
| "warning"
|
|
27
|
-
| "error"
|
|
28
|
-
| "critical"
|
|
29
|
-
| "alert"
|
|
30
|
-
| "emergency";
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Context passed through the middleware pipeline.
|
|
34
|
-
* Each middleware can read and enrich the context.
|
|
35
|
-
*/
|
|
36
|
-
export interface MiddlewareContext {
|
|
37
|
-
/** Name of the tool being called */
|
|
38
|
-
toolName: string;
|
|
39
|
-
|
|
40
|
-
/** Tool arguments */
|
|
41
|
-
args: Record<string, unknown>;
|
|
42
|
-
|
|
43
|
-
/** HTTP request (only present for HTTP transport, undefined for STDIO) */
|
|
44
|
-
request?: Request;
|
|
45
|
-
|
|
46
|
-
/** Session ID (only present for HTTP transport) */
|
|
47
|
-
sessionId?: string;
|
|
48
|
-
|
|
49
|
-
/** Client implementation metadata (stateless HTTP transport, when provided) */
|
|
50
|
-
clientInfo?: Implementation;
|
|
51
|
-
|
|
52
|
-
/** Client capabilities metadata (stateless HTTP transport, when provided) */
|
|
53
|
-
clientCapabilities?: ClientCapabilities;
|
|
54
|
-
|
|
55
|
-
/** MRTR: answers the client echoed back on a retry (spec 2026-07-28). */
|
|
56
|
-
inputResponses?: Record<string, unknown>;
|
|
57
|
-
|
|
58
|
-
/** MRTR: whether the echoed `requestState` passed integrity verification. */
|
|
59
|
-
retryVerified?: boolean;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Log level the client requested **for this request** (spec 2026-07-28).
|
|
63
|
-
*
|
|
64
|
-
* Replaces the removed `logging/setLevel` RPC: there is no server-wide level
|
|
65
|
-
* to mutate any more, so the level travels with the call that wants it. Absent
|
|
66
|
-
* means the client asked for no logging — and the spec is explicit that a
|
|
67
|
-
* server **MUST NOT** emit `notifications/message` in that case.
|
|
68
|
-
*/
|
|
69
|
-
logLevel?: McpLogLevel;
|
|
70
|
-
|
|
71
|
-
/** Extensible by middlewares (e.g., authInfo added by auth middleware) */
|
|
72
|
-
[key: string]: unknown;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Result returned by a middleware or the final handler.
|
|
77
|
-
*/
|
|
78
|
-
export type MiddlewareResult = unknown;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Function to invoke the next middleware in the chain.
|
|
82
|
-
*/
|
|
83
|
-
export type NextFunction = () => Promise<MiddlewareResult>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* A middleware function.
|
|
87
|
-
*
|
|
88
|
-
* Receives the context and a `next()` function to call the next middleware.
|
|
89
|
-
* Can short-circuit the pipeline by not calling `next()`.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* const loggingMiddleware: Middleware = async (ctx, next) => {
|
|
94
|
-
* console.log(`Before: ${ctx.toolName}`);
|
|
95
|
-
* const result = await next();
|
|
96
|
-
* console.log(`After: ${ctx.toolName}`);
|
|
97
|
-
* return result;
|
|
98
|
-
* };
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
export type Middleware = (
|
|
102
|
-
ctx: MiddlewareContext,
|
|
103
|
-
next: NextFunction,
|
|
104
|
-
) => Promise<MiddlewareResult>;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Schema validation middleware.
|
|
3
|
-
*
|
|
4
|
-
* Validates tool arguments against JSON Schema before execution.
|
|
5
|
-
*
|
|
6
|
-
* @module lib/server/middleware/validation
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { SchemaValidator } from "../validation/schema-validator.js";
|
|
10
|
-
import type { Middleware } from "./types.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Create a schema validation middleware.
|
|
14
|
-
*
|
|
15
|
-
* Validates `ctx.args` against the registered schema for `ctx.toolName`.
|
|
16
|
-
* Throws with a descriptive error if validation fails.
|
|
17
|
-
*
|
|
18
|
-
* @param validator - SchemaValidator instance with pre-registered schemas
|
|
19
|
-
*/
|
|
20
|
-
export function createValidationMiddleware(
|
|
21
|
-
validator: SchemaValidator,
|
|
22
|
-
): Middleware {
|
|
23
|
-
// deno-lint-ignore require-await
|
|
24
|
-
return async (ctx, next) => {
|
|
25
|
-
validator.validateOrThrow(ctx.toolName, ctx.args);
|
|
26
|
-
return next();
|
|
27
|
-
};
|
|
28
|
-
}
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Capability check for MRTR inputRequests (spec 2026-07-28, SEP-2322).
|
|
3
|
-
*
|
|
4
|
-
* Rule 7 (MUST NOT): "Servers MUST NOT emit an inputRequest for a capability
|
|
5
|
-
* the client has not declared in io.modelcontextprotocol/clientCapabilities."
|
|
6
|
-
*
|
|
7
|
-
* In the stateless transport model, `clientCapabilities` is per-request: it
|
|
8
|
-
* arrives in `params._meta["io.modelcontextprotocol/clientCapabilities"]` and
|
|
9
|
-
* is therefore already parsed by the time this check runs. There is no session
|
|
10
|
-
* to cache it from — each call carries its own capability declaration.
|
|
11
|
-
*
|
|
12
|
-
* The function is pure: it takes the data it needs and returns a typed result.
|
|
13
|
-
* No network, no server state, no side effects.
|
|
14
|
-
*
|
|
15
|
-
* @module lib/server/mrtr/capability-check
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import type { InputRequestEntry } from "./types.js";
|
|
19
|
-
|
|
20
|
-
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
21
|
-
|
|
22
|
-
/** Successful capability check — all methods map to declared capabilities. */
|
|
23
|
-
export interface CapabilityCheckOk {
|
|
24
|
-
/**
|
|
25
|
-
* The canonical form of `inputRequests` — the value the caller MUST emit.
|
|
26
|
-
*
|
|
27
|
-
* Checking a JSON clone and then serialising the original is two serialisations,
|
|
28
|
-
* and a stateful `toJSON()` can return different values for each. That is not a
|
|
29
|
-
* theoretical gap: it reopens exactly the bypass canonicalisation was added to
|
|
30
|
-
* close. Emit this, not the input.
|
|
31
|
-
*/
|
|
32
|
-
readonly canonicalRequests?: Record<string, unknown>;
|
|
33
|
-
readonly ok: true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Failed capability check — one or more capabilities are missing. */
|
|
37
|
-
export interface CapabilityCheckFail {
|
|
38
|
-
/**
|
|
39
|
-
* Which fault this is.
|
|
40
|
-
*
|
|
41
|
-
* `missing_capability` — the client did not declare something the request needs.
|
|
42
|
-
* That is the client's to fix, and `-32021` says exactly that.
|
|
43
|
-
*
|
|
44
|
-
* `malformed` — the SERVER produced an input request that is structurally
|
|
45
|
-
* invalid: an unknown method, a missing or non-object `params`, a map that will
|
|
46
|
-
* not serialise. Reporting those as a missing capability tells the client to
|
|
47
|
-
* declare something it already has, and hides a server bug behind a client-facing
|
|
48
|
-
* error.
|
|
49
|
-
*/
|
|
50
|
-
readonly kind: "missing_capability" | "malformed";
|
|
51
|
-
/**
|
|
52
|
-
* `ClientCapabilities`-shaped payload for the error's `data`, when the missing
|
|
53
|
-
* capability is nested. Absent for top-level ones, where the caller derives it
|
|
54
|
-
* from `missingCapabilities`.
|
|
55
|
-
*/
|
|
56
|
-
readonly requiredCapabilities?: Record<string, unknown>;
|
|
57
|
-
readonly ok: false;
|
|
58
|
-
/**
|
|
59
|
-
* Top-level `ClientCapabilities` keys the client did not declare but the
|
|
60
|
-
* `inputRequests` map requires.
|
|
61
|
-
*
|
|
62
|
-
* Forward these directly into `-32021 MissingRequiredClientCapabilityError`
|
|
63
|
-
* as `data.requiredCapabilities` (spec 2026-07-28 §per-request protocol fields).
|
|
64
|
-
*
|
|
65
|
-
* Example: `["sampling", "elicitation"]` if both are absent.
|
|
66
|
-
*/
|
|
67
|
-
readonly missingCapabilities: string[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type CapabilityCheckResult = CapabilityCheckOk | CapabilityCheckFail;
|
|
71
|
-
|
|
72
|
-
// ── Method → capability mapping ───────────────────────────────────────────────
|
|
73
|
-
//
|
|
74
|
-
// Spec 2026-07-28: only these three methods are legal in `inputRequests`.
|
|
75
|
-
// Any unrecognised method is treated as a missing capability (fail-safe): an
|
|
76
|
-
// unknown method is a server-authoring error, and the spec forbids emitting it
|
|
77
|
-
// on the wire. Rejecting it here prevents that wire violation.
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The only methods an input request may carry, mapped to the capability each needs.
|
|
81
|
-
*
|
|
82
|
-
* Exported so the Tasks boundary enforces the same grammar. A task that requests
|
|
83
|
-
* input uses MRTR request shapes, and a second, looser list there is how a task
|
|
84
|
-
* ends up asking for a method no client implements — which strands it rather than
|
|
85
|
-
* failing it.
|
|
86
|
-
*/
|
|
87
|
-
export const METHOD_TO_CAPABILITY: Readonly<Record<string, string>> = {
|
|
88
|
-
"elicitation/create": "elicitation",
|
|
89
|
-
"sampling/createMessage": "sampling",
|
|
90
|
-
"roots/list": "roots",
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// ── checkInputRequestCapabilities ─────────────────────────────────────────────
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Verify that every `inputRequest` entry maps to a capability the client declared.
|
|
97
|
-
*
|
|
98
|
-
* Mapping (spec 2026-07-28):
|
|
99
|
-
* elicitation/create → clientCapabilities.elicitation
|
|
100
|
-
* sampling/createMessage → clientCapabilities.sampling
|
|
101
|
-
* roots/list → clientCapabilities.roots
|
|
102
|
-
*
|
|
103
|
-
* Unknown methods fail-safe: the spec only permits the three methods above.
|
|
104
|
-
* An unknown method in `inputRequests` is a server-authoring bug and is
|
|
105
|
-
* rejected here before it reaches the wire.
|
|
106
|
-
*
|
|
107
|
-
* @param inputRequests - The map from the handler's `InputRequiredSignal`.
|
|
108
|
-
* @param clientCapabilities - Parsed from `params._meta[STATELESS_CLIENT_CAPABILITIES_KEY]`.
|
|
109
|
-
* `undefined` when the client sent no `_meta` at all —
|
|
110
|
-
* treated as "no capabilities declared".
|
|
111
|
-
* @returns `{ ok: true }` or `{ ok: false, missingCapabilities: string[] }`.
|
|
112
|
-
*/
|
|
113
|
-
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
114
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Reduce a value to what it will actually be on the wire.
|
|
119
|
-
*
|
|
120
|
-
* The gate must inspect the JSON representation, not the live object: the two can
|
|
121
|
-
* differ. `new String("url")` is not `=== "url"` but serialises to `"url"`, and a
|
|
122
|
-
* `toJSON()` can introduce fields — `tools`, say — that exist only after
|
|
123
|
-
* serialisation. Checking the live object then shipping the serialised one means
|
|
124
|
-
* the check and the payload disagree, which is precisely the gap a caller would
|
|
125
|
-
* use to obtain an input request the client cannot service.
|
|
126
|
-
*
|
|
127
|
-
* Returns `undefined` when the value cannot be serialised at all, which callers
|
|
128
|
-
* treat as malformed.
|
|
129
|
-
*/
|
|
130
|
-
function canonicalise(value: unknown): unknown {
|
|
131
|
-
try {
|
|
132
|
-
return JSON.parse(JSON.stringify(value ?? null));
|
|
133
|
-
} catch {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function checkInputRequestCapabilities(
|
|
139
|
-
inputRequests: Record<string, InputRequestEntry>,
|
|
140
|
-
clientCapabilities: Record<string, unknown> | undefined,
|
|
141
|
-
): CapabilityCheckResult {
|
|
142
|
-
// Collect every capability key that is required by the inputRequests.
|
|
143
|
-
// Use a Set to deduplicate: multiple slots may require the same capability.
|
|
144
|
-
// Canonicalise the WHOLE map once. Everything below inspects this clone, and the
|
|
145
|
-
// caller emits it — so the value checked and the value sent are the same object,
|
|
146
|
-
// serialised once.
|
|
147
|
-
const canonicalRequests = canonicalise(inputRequests);
|
|
148
|
-
if (!isRecord(canonicalRequests)) {
|
|
149
|
-
return {
|
|
150
|
-
ok: false,
|
|
151
|
-
kind: "malformed",
|
|
152
|
-
missingCapabilities: ["<inputRequests could not be serialised>"],
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const required = new Set<string>();
|
|
157
|
-
for (const entry of Object.values(canonicalRequests) as InputRequestEntry[]) {
|
|
158
|
-
// Sub-capabilities: declaring `elicitation` does not grant URL mode, and
|
|
159
|
-
// declaring `sampling` does not grant tool-using sampling. Checking only the
|
|
160
|
-
// top-level key let a server ask for a mode the client cannot service, which
|
|
161
|
-
// strands the request exactly as an undeclared capability would.
|
|
162
|
-
// The map VALUE must itself be an object. `{ k: null }` serialises fine, so it
|
|
163
|
-
// survived canonicalisation and then threw on `entry.params` — landing in the
|
|
164
|
-
// generic tool-error path as a 200, not the 500 a malformed server output
|
|
165
|
-
// requires. Checked before anything dereferences it.
|
|
166
|
-
if (!isRecord(entry)) {
|
|
167
|
-
return {
|
|
168
|
-
ok: false,
|
|
169
|
-
kind: "malformed",
|
|
170
|
-
missingCapabilities: [
|
|
171
|
-
"<inputRequests values must be request objects>",
|
|
172
|
-
],
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// `method` must be a string, and looked up as an OWN key.
|
|
177
|
-
//
|
|
178
|
-
// Two distinct faults hid here. A non-string method — `{ toString: null }`
|
|
179
|
-
// survives JSON — throws a TypeError when used as a property key, landing in
|
|
180
|
-
// the generic tool-error path as a 200. And `"toString"` or `"constructor"`
|
|
181
|
-
// resolve INHERITED properties of the mapping object, so `capKey` came back
|
|
182
|
-
// as a function rather than undefined and produced a nonsense
|
|
183
|
-
// missing-capability error naming a method that does not exist.
|
|
184
|
-
if (typeof entry.method !== "string") {
|
|
185
|
-
return {
|
|
186
|
-
ok: false,
|
|
187
|
-
kind: "malformed",
|
|
188
|
-
missingCapabilities: ["<inputRequest method must be a string>"],
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Already canonical: `entry` came out of the clone above. A `params` that is
|
|
193
|
-
// present but not an object is malformed — reading it as `{}` would skip the
|
|
194
|
-
// sub-capability checks entirely, a server-authoring bug resolving to "no
|
|
195
|
-
// extra capability needed".
|
|
196
|
-
//
|
|
197
|
-
// `null` is rejected along with the rest, deliberately. I had intended to let
|
|
198
|
-
// it through as equivalent to "absent", but the two are not the same: omitting
|
|
199
|
-
// `params` says the request needs none, while `params: null` asserts a value
|
|
200
|
-
// that the schema types as an object. Accepting it would mean the sub-capability
|
|
201
|
-
// checks silently do not run for a request that looks like it declared
|
|
202
|
-
// something.
|
|
203
|
-
//
|
|
204
|
-
// Absence is method-specific: the schema requires `params` on
|
|
205
|
-
// `elicitation/create` (message + requestedSchema) and `sampling/createMessage`
|
|
206
|
-
// (messages + maxTokens). Only `ListRootsRequest` may omit it. Accepting an
|
|
207
|
-
// omission everywhere let a request ship that no client could service — it has
|
|
208
|
-
// nothing to render or sample.
|
|
209
|
-
if (entry.params === undefined && entry.method !== "roots/list") {
|
|
210
|
-
return {
|
|
211
|
-
ok: false,
|
|
212
|
-
kind: "malformed",
|
|
213
|
-
missingCapabilities: [
|
|
214
|
-
`<${entry.method} requires params>`,
|
|
215
|
-
],
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
if (entry.params !== undefined && !isRecord(entry.params)) {
|
|
219
|
-
return {
|
|
220
|
-
ok: false,
|
|
221
|
-
kind: "malformed",
|
|
222
|
-
missingCapabilities: [
|
|
223
|
-
`<malformed inputRequest params for ${entry.method}>`,
|
|
224
|
-
],
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
const params = isRecord(entry.params) ? entry.params : {};
|
|
228
|
-
if (entry.method === "elicitation/create" && params["mode"] === "url") {
|
|
229
|
-
const elicitation = isRecord(clientCapabilities?.["elicitation"])
|
|
230
|
-
? clientCapabilities["elicitation"] as Record<string, unknown>
|
|
231
|
-
: undefined;
|
|
232
|
-
if (elicitation?.["url"] === undefined) {
|
|
233
|
-
return {
|
|
234
|
-
ok: false,
|
|
235
|
-
kind: "missing_capability",
|
|
236
|
-
missingCapabilities: ["elicitation.url"],
|
|
237
|
-
// Nested, matching ClientCapabilities. A flat "elicitation.url" key
|
|
238
|
-
// deserialises to `{}` on a typed client — it reads as "nothing
|
|
239
|
-
// missing", the opposite of the message.
|
|
240
|
-
requiredCapabilities: { elicitation: { url: {} } },
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
if (
|
|
245
|
-
entry.method === "sampling/createMessage" &&
|
|
246
|
-
(params["tools"] !== undefined || params["toolChoice"] !== undefined)
|
|
247
|
-
) {
|
|
248
|
-
const sampling = isRecord(clientCapabilities?.["sampling"])
|
|
249
|
-
? clientCapabilities["sampling"] as Record<string, unknown>
|
|
250
|
-
: undefined;
|
|
251
|
-
if (sampling?.["tools"] === undefined) {
|
|
252
|
-
return {
|
|
253
|
-
ok: false,
|
|
254
|
-
kind: "missing_capability",
|
|
255
|
-
missingCapabilities: ["sampling.tools"],
|
|
256
|
-
requiredCapabilities: { sampling: { tools: {} } },
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const capKey = Object.hasOwn(METHOD_TO_CAPABILITY, entry.method)
|
|
262
|
-
? METHOD_TO_CAPABILITY[entry.method]
|
|
263
|
-
: undefined;
|
|
264
|
-
if (capKey === undefined) {
|
|
265
|
-
// MRTR permits exactly three methods. An unknown one is invalid outright,
|
|
266
|
-
// so it must fail regardless of what the client declared — mapping it to
|
|
267
|
-
// its own name let a client "satisfy" it by declaring a capability of the
|
|
268
|
-
// same name, which is not a capability at all.
|
|
269
|
-
return {
|
|
270
|
-
ok: false,
|
|
271
|
-
kind: "malformed",
|
|
272
|
-
missingCapabilities: [
|
|
273
|
-
`<unsupported inputRequest method: ${entry.method}>`,
|
|
274
|
-
],
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
required.add(capKey);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
if (required.size === 0) {
|
|
281
|
-
// Empty inputRequests map — nothing to check
|
|
282
|
-
return { ok: true, canonicalRequests };
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const missing: string[] = [];
|
|
286
|
-
for (const capKey of required) {
|
|
287
|
-
// The client "declared" the capability when its key is present in
|
|
288
|
-
// `clientCapabilities` with any value (including `{}`). An absent key or
|
|
289
|
-
// an undefined `clientCapabilities` object means "not declared".
|
|
290
|
-
const declared = clientCapabilities !== undefined &&
|
|
291
|
-
Object.hasOwn(clientCapabilities, capKey);
|
|
292
|
-
if (!declared) missing.push(capKey);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (missing.length > 0) {
|
|
296
|
-
return {
|
|
297
|
-
ok: false,
|
|
298
|
-
kind: "missing_capability",
|
|
299
|
-
missingCapabilities: missing,
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
return { ok: true, canonicalRequests };
|
|
303
|
-
}
|
package/src/mrtr/mod.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MRTR — Multi Round-Trip Requests (Track B, spec 2026-07-28, SEP-2322).
|
|
3
|
-
*
|
|
4
|
-
* Public re-export barrel. Import everything the transport layer needs from
|
|
5
|
-
* this single path rather than reaching into sub-modules directly.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/mrtr
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
// Types shared across all MRTR code
|
|
11
|
-
export type {
|
|
12
|
-
InputRequestEntry,
|
|
13
|
-
InputRequestMethod,
|
|
14
|
-
InputRequiredSignal,
|
|
15
|
-
MrtrOptions,
|
|
16
|
-
} from "./types.js";
|
|
17
|
-
|
|
18
|
-
// Atomic single-use enforcement for verified requestState nonces.
|
|
19
|
-
export type {
|
|
20
|
-
MemoryMrtrReplayStoreOptions,
|
|
21
|
-
MrtrReplayStore,
|
|
22
|
-
} from "./replay-store.js";
|
|
23
|
-
export { MemoryMrtrReplayStore } from "./replay-store.js";
|
|
24
|
-
|
|
25
|
-
// Transport-neutral retry admission: integrity bindings + single-use replay gate.
|
|
26
|
-
export type {
|
|
27
|
-
GuardMrtrRetryInput,
|
|
28
|
-
MrtrRetryBindings,
|
|
29
|
-
MrtrRetryGuardOutcome,
|
|
30
|
-
} from "./retry-guard.js";
|
|
31
|
-
export { guardMrtrRetry } from "./retry-guard.js";
|
|
32
|
-
|
|
33
|
-
// requestState seal/verify (HMAC-SHA256 integrity protection)
|
|
34
|
-
export type {
|
|
35
|
-
RequestStatePayload,
|
|
36
|
-
RequestStateRejectionReason,
|
|
37
|
-
VerifyResult,
|
|
38
|
-
} from "./request-state.js";
|
|
39
|
-
export {
|
|
40
|
-
exportStateKey,
|
|
41
|
-
generateStateKey,
|
|
42
|
-
importStateKey,
|
|
43
|
-
paramsDigest,
|
|
44
|
-
sealRequestState,
|
|
45
|
-
verifyRequestState,
|
|
46
|
-
} from "./request-state.js";
|
|
47
|
-
|
|
48
|
-
// Capability check (spec rule 7 — MUST NOT emit for undeclared capabilities)
|
|
49
|
-
export type {
|
|
50
|
-
CapabilityCheckFail,
|
|
51
|
-
CapabilityCheckOk,
|
|
52
|
-
CapabilityCheckResult,
|
|
53
|
-
} from "./capability-check.js";
|
|
54
|
-
export { checkInputRequestCapabilities } from "./capability-check.js";
|
package/src/mrtr/replay-store.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Atomic nonce consumption for MRTR requestState tokens.
|
|
3
|
-
*
|
|
4
|
-
* Integrity verification proves that a token was minted by the server and is
|
|
5
|
-
* bound to the current request. A replay store adds the separate single-use
|
|
6
|
-
* property by reserving the signed nonce before business logic runs.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Store used to consume a verified MRTR nonce exactly once.
|
|
11
|
-
*
|
|
12
|
-
* Implementations MUST make the check-and-reserve operation atomic. Return
|
|
13
|
-
* `true` only for the caller that reserved the nonce, `false` when it was
|
|
14
|
-
* already consumed, and throw when availability cannot be established.
|
|
15
|
-
*
|
|
16
|
-
* `expiresAt` is the signed token expiry as Unix epoch seconds. Stores should
|
|
17
|
-
* remove the reservation after that time; retaining it longer is safe but
|
|
18
|
-
* wastes capacity.
|
|
19
|
-
*
|
|
20
|
-
* A load-balanced deployment MUST provide one shared implementation to every
|
|
21
|
-
* McpApp instance. Redis can implement the contract with `SET key 1 NX EXAT`.
|
|
22
|
-
*/
|
|
23
|
-
export interface MrtrReplayStore {
|
|
24
|
-
consume(nonce: string, expiresAt: number): boolean | Promise<boolean>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MemoryMrtrReplayStoreOptions {
|
|
28
|
-
/**
|
|
29
|
-
* Maximum live reservations before the store fails closed.
|
|
30
|
-
*
|
|
31
|
-
* Default: 100,000.
|
|
32
|
-
*/
|
|
33
|
-
maxEntries?: number;
|
|
34
|
-
|
|
35
|
-
/** Test seam for the current Unix epoch time in seconds. */
|
|
36
|
-
nowSecs?: () => number;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Process-local replay protection.
|
|
41
|
-
*
|
|
42
|
-
* This is the safe default for one continuously running McpApp process. It
|
|
43
|
-
* cannot coordinate separate processes or survive a restart; horizontally
|
|
44
|
-
* scaled or restart-safe deployments must inject a durable shared
|
|
45
|
-
* `MrtrReplayStore`.
|
|
46
|
-
*/
|
|
47
|
-
export class MemoryMrtrReplayStore implements MrtrReplayStore {
|
|
48
|
-
private readonly entries = new Map<string, number>();
|
|
49
|
-
private readonly maxEntries: number;
|
|
50
|
-
private readonly nowSecs: () => number;
|
|
51
|
-
|
|
52
|
-
constructor(options: MemoryMrtrReplayStoreOptions = {}) {
|
|
53
|
-
const maxEntries = options.maxEntries ?? 100_000;
|
|
54
|
-
if (!Number.isSafeInteger(maxEntries) || maxEntries < 1) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
`MemoryMrtrReplayStore maxEntries must be a positive integer (got ${maxEntries})`,
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
this.maxEntries = maxEntries;
|
|
60
|
-
this.nowSecs = options.nowSecs ?? (() => Math.floor(Date.now() / 1000));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
consume(nonce: string, expiresAt: number): boolean {
|
|
64
|
-
if (!/^[0-9a-f]{32}$/.test(nonce)) {
|
|
65
|
-
throw new Error("MRTR replay nonce must be 32 lowercase hex characters");
|
|
66
|
-
}
|
|
67
|
-
if (!Number.isSafeInteger(expiresAt) || expiresAt < 0) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
"MRTR replay expiry must be a non-negative safe-integer Unix timestamp",
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const now = this.nowSecs();
|
|
74
|
-
if (!Number.isSafeInteger(now) || now < 0) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
"MRTR replay clock must return a non-negative safe-integer Unix timestamp",
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
const existingExpiry = this.entries.get(nonce);
|
|
80
|
-
if (existingExpiry !== undefined) {
|
|
81
|
-
if (existingExpiry > now) return false;
|
|
82
|
-
this.entries.delete(nonce);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (expiresAt <= now) return false;
|
|
86
|
-
|
|
87
|
-
if (this.entries.size >= this.maxEntries) {
|
|
88
|
-
for (const [storedNonce, storedExpiry] of this.entries) {
|
|
89
|
-
if (storedExpiry <= now) this.entries.delete(storedNonce);
|
|
90
|
-
}
|
|
91
|
-
if (this.entries.size >= this.maxEntries) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
`MRTR replay store capacity exceeded (${this.maxEntries} live nonces)`,
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// No await may occur between the check and this write. In one JS process,
|
|
99
|
-
// that makes the reservation atomic across concurrent request continuations.
|
|
100
|
-
this.entries.set(nonce, expiresAt);
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
}
|