@casys/mcp-server 0.27.0 → 0.27.2
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 +18 -1764
- package/README.md +21 -631
- package/esm/mod.d.ts +1 -102
- package/esm/mod.js +1 -103
- package/package.json +17 -25
- package/esm/_dnt.polyfills.d.ts +0 -12
- package/esm/_dnt.polyfills.d.ts.map +0 -1
- package/esm/_dnt.polyfills.js +0 -15
- package/esm/_dnt.shims.d.ts +0 -2
- package/esm/_dnt.shims.d.ts.map +0 -1
- package/esm/_dnt.shims.js +0 -57
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts +0 -33
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.js +0 -48
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts +0 -106
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.js +0 -636
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts +0 -67
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.js +0 -1393
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts +0 -44
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.js +0 -117
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.js +0 -103
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.js +0 -32
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.js +0 -96
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.js +0 -159
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.js +0 -14
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.js +0 -10
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.js +0 -22
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.js +0 -29
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.js +0 -19
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.js +0 -30
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.js +0 -10
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.js +0 -14
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.js +0 -9
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.js +0 -81
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts +0 -3
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.js +0 -20
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts +0 -32
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.js +0 -6
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts +0 -4
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.js +0 -13
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts +0 -51
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.js +0 -53
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts +0 -79
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.js +0 -91
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts +0 -100
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.js +0 -33
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts +0 -111
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.js +0 -104
- package/esm/mod.d.ts.map +0 -1
- package/esm/package.json +0 -3
- package/esm/src/auth/as-metadata-proxy.d.ts +0 -112
- package/esm/src/auth/as-metadata-proxy.d.ts.map +0 -1
- package/esm/src/auth/as-metadata-proxy.js +0 -171
- package/esm/src/auth/config.d.ts +0 -117
- package/esm/src/auth/config.d.ts.map +0 -1
- package/esm/src/auth/config.js +0 -219
- package/esm/src/auth/jwt-provider.d.ts +0 -157
- package/esm/src/auth/jwt-provider.d.ts.map +0 -1
- package/esm/src/auth/jwt-provider.js +0 -187
- package/esm/src/auth/middleware.d.ts +0 -61
- package/esm/src/auth/middleware.d.ts.map +0 -1
- package/esm/src/auth/middleware.js +0 -209
- package/esm/src/auth/mod.d.ts +0 -23
- package/esm/src/auth/mod.d.ts.map +0 -1
- package/esm/src/auth/mod.js +0 -27
- package/esm/src/auth/multi-tenant-middleware.d.ts +0 -131
- package/esm/src/auth/multi-tenant-middleware.d.ts.map +0 -1
- package/esm/src/auth/multi-tenant-middleware.js +0 -128
- package/esm/src/auth/presets.d.ts +0 -231
- package/esm/src/auth/presets.d.ts.map +0 -1
- package/esm/src/auth/presets.js +0 -223
- package/esm/src/auth/provider.d.ts +0 -47
- package/esm/src/auth/provider.d.ts.map +0 -1
- package/esm/src/auth/provider.js +0 -34
- package/esm/src/auth/scope-middleware.d.ts +0 -30
- package/esm/src/auth/scope-middleware.d.ts.map +0 -1
- package/esm/src/auth/scope-middleware.js +0 -48
- package/esm/src/auth/static-token-provider.d.ts +0 -155
- package/esm/src/auth/static-token-provider.d.ts.map +0 -1
- package/esm/src/auth/static-token-provider.js +0 -256
- package/esm/src/auth/types.d.ts +0 -151
- package/esm/src/auth/types.d.ts.map +0 -1
- package/esm/src/auth/types.js +0 -64
- package/esm/src/client-auth/callback-server.d.ts +0 -34
- package/esm/src/client-auth/callback-server.d.ts.map +0 -1
- package/esm/src/client-auth/callback-server.js +0 -141
- package/esm/src/client-auth/client-id-metadata.d.ts +0 -29
- package/esm/src/client-auth/client-id-metadata.d.ts.map +0 -1
- package/esm/src/client-auth/client-id-metadata.js +0 -149
- package/esm/src/client-auth/connect.d.ts +0 -32
- package/esm/src/client-auth/connect.d.ts.map +0 -1
- package/esm/src/client-auth/connect.js +0 -47
- package/esm/src/client-auth/mod.d.ts +0 -19
- package/esm/src/client-auth/mod.d.ts.map +0 -1
- package/esm/src/client-auth/mod.js +0 -19
- package/esm/src/client-auth/provider.d.ts +0 -78
- package/esm/src/client-auth/provider.d.ts.map +0 -1
- package/esm/src/client-auth/provider.js +0 -246
- package/esm/src/client-auth/token-store/file-store.d.ts +0 -20
- package/esm/src/client-auth/token-store/file-store.d.ts.map +0 -1
- package/esm/src/client-auth/token-store/file-store.js +0 -62
- package/esm/src/client-auth/token-store/memory-store.d.ts +0 -14
- package/esm/src/client-auth/token-store/memory-store.d.ts.map +0 -1
- package/esm/src/client-auth/token-store/memory-store.js +0 -25
- package/esm/src/client-auth/types.d.ts +0 -89
- package/esm/src/client-auth/types.d.ts.map +0 -1
- package/esm/src/client-auth/types.js +0 -10
- package/esm/src/concurrency/rate-limiter.d.ts +0 -87
- package/esm/src/concurrency/rate-limiter.d.ts.map +0 -1
- package/esm/src/concurrency/rate-limiter.js +0 -165
- package/esm/src/concurrency/request-queue.d.ts +0 -71
- package/esm/src/concurrency/request-queue.d.ts.map +0 -1
- package/esm/src/concurrency/request-queue.js +0 -128
- package/esm/src/http/body.d.ts +0 -20
- package/esm/src/http/body.d.ts.map +0 -1
- package/esm/src/http/body.js +0 -63
- package/esm/src/http/request-guards.d.ts +0 -54
- package/esm/src/http/request-guards.d.ts.map +0 -1
- package/esm/src/http/request-guards.js +0 -114
- package/esm/src/http/wire.d.ts +0 -26
- package/esm/src/http/wire.d.ts.map +0 -1
- package/esm/src/http/wire.js +0 -35
- package/esm/src/inspector/launcher.d.ts +0 -48
- package/esm/src/inspector/launcher.d.ts.map +0 -1
- package/esm/src/inspector/launcher.js +0 -100
- package/esm/src/mcp-app.d.ts +0 -728
- package/esm/src/mcp-app.d.ts.map +0 -1
- package/esm/src/mcp-app.js +0 -3530
- package/esm/src/middleware/backpressure.d.ts +0 -24
- package/esm/src/middleware/backpressure.d.ts.map +0 -1
- package/esm/src/middleware/backpressure.js +0 -31
- package/esm/src/middleware/mod.d.ts +0 -11
- package/esm/src/middleware/mod.d.ts.map +0 -1
- package/esm/src/middleware/mod.js +0 -11
- package/esm/src/middleware/rate-limit.d.ts +0 -22
- package/esm/src/middleware/rate-limit.d.ts.map +0 -1
- package/esm/src/middleware/rate-limit.js +0 -33
- package/esm/src/middleware/runner.d.ts +0 -32
- package/esm/src/middleware/runner.d.ts.map +0 -1
- package/esm/src/middleware/runner.js +0 -49
- package/esm/src/middleware/types.d.ts +0 -76
- package/esm/src/middleware/types.d.ts.map +0 -1
- package/esm/src/middleware/types.js +0 -9
- package/esm/src/middleware/validation.d.ts +0 -24
- package/esm/src/middleware/validation.d.ts.map +0 -1
- package/esm/src/middleware/validation.js +0 -30
- package/esm/src/mrtr/capability-check.d.ts +0 -75
- package/esm/src/mrtr/capability-check.d.ts.map +0 -1
- package/esm/src/mrtr/capability-check.js +0 -233
- package/esm/src/mrtr/mod.d.ts +0 -18
- package/esm/src/mrtr/mod.d.ts.map +0 -1
- package/esm/src/mrtr/mod.js +0 -12
- package/esm/src/mrtr/replay-store.d.ts +0 -50
- package/esm/src/mrtr/replay-store.d.ts.map +0 -1
- package/esm/src/mrtr/replay-store.js +0 -61
- package/esm/src/mrtr/request-state.d.ts +0 -181
- package/esm/src/mrtr/request-state.d.ts.map +0 -1
- package/esm/src/mrtr/request-state.js +0 -313
- package/esm/src/mrtr/retry-guard.d.ts +0 -61
- package/esm/src/mrtr/retry-guard.d.ts.map +0 -1
- package/esm/src/mrtr/retry-guard.js +0 -53
- package/esm/src/mrtr/types.d.ts +0 -99
- package/esm/src/mrtr/types.d.ts.map +0 -1
- package/esm/src/mrtr/types.js +0 -14
- package/esm/src/observability/metrics.d.ts +0 -104
- package/esm/src/observability/metrics.d.ts.map +0 -1
- package/esm/src/observability/metrics.js +0 -207
- package/esm/src/observability/mod.d.ts +0 -12
- package/esm/src/observability/mod.d.ts.map +0 -1
- package/esm/src/observability/mod.js +0 -11
- package/esm/src/observability/otel.d.ts +0 -73
- package/esm/src/observability/otel.d.ts.map +0 -1
- package/esm/src/observability/otel.js +0 -260
- package/esm/src/runtime/runtime.d.ts +0 -10
- package/esm/src/runtime/runtime.d.ts.map +0 -1
- package/esm/src/runtime/runtime.deno.d.ts +0 -50
- package/esm/src/runtime/runtime.deno.d.ts.map +0 -1
- package/esm/src/runtime/runtime.deno.js +0 -120
- package/esm/src/runtime/runtime.js +0 -41
- package/esm/src/runtime/runtime.node.d.ts +0 -51
- package/esm/src/runtime/runtime.node.d.ts.map +0 -1
- package/esm/src/runtime/runtime.node.js +0 -268
- package/esm/src/runtime/types.d.ts +0 -107
- package/esm/src/runtime/types.d.ts.map +0 -1
- package/esm/src/runtime/types.js +0 -12
- package/esm/src/security/channel-hmac.d.ts +0 -43
- package/esm/src/security/channel-hmac.d.ts.map +0 -1
- package/esm/src/security/channel-hmac.js +0 -135
- package/esm/src/security/csp.d.ts +0 -50
- package/esm/src/security/csp.d.ts.map +0 -1
- package/esm/src/security/csp.js +0 -63
- package/esm/src/security/message-signer.d.ts +0 -88
- package/esm/src/security/message-signer.d.ts.map +0 -1
- package/esm/src/security/message-signer.js +0 -166
- package/esm/src/subscriptions/subscription-registry.d.ts +0 -256
- package/esm/src/subscriptions/subscription-registry.d.ts.map +0 -1
- package/esm/src/subscriptions/subscription-registry.js +0 -406
- package/esm/src/tasks/mod.d.ts +0 -13
- package/esm/src/tasks/mod.d.ts.map +0 -1
- package/esm/src/tasks/mod.js +0 -11
- package/esm/src/tasks/task-store.d.ts +0 -342
- package/esm/src/tasks/task-store.d.ts.map +0 -1
- package/esm/src/tasks/task-store.js +0 -649
- package/esm/src/transport/request-headers.d.ts +0 -145
- package/esm/src/transport/request-headers.d.ts.map +0 -1
- package/esm/src/transport/request-headers.js +0 -525
- package/esm/src/types.d.ts +0 -776
- package/esm/src/types.d.ts.map +0 -1
- package/esm/src/types.js +0 -92
- package/esm/src/ui/viewer-utils.d.ts +0 -45
- package/esm/src/ui/viewer-utils.d.ts.map +0 -1
- package/esm/src/ui/viewer-utils.js +0 -97
- package/esm/src/validation/schema-validator.d.ts +0 -115
- package/esm/src/validation/schema-validator.d.ts.map +0 -1
- package/esm/src/validation/schema-validator.js +0 -249
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rate Limiter
|
|
3
|
-
*
|
|
4
|
-
* Sliding window rate limiter for per-client request throttling.
|
|
5
|
-
* Prevents server overload by limiting requests per time window.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/rate-limiter
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Sliding window rate limiter
|
|
11
|
-
*
|
|
12
|
-
* Features:
|
|
13
|
-
* - Per-key rate limiting (client ID, IP, etc.)
|
|
14
|
-
* - Sliding window for smooth rate enforcement
|
|
15
|
-
* - Automatic cleanup of old timestamps
|
|
16
|
-
* - Backoff waiting when limit exceeded
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const limiter = new RateLimiter({ maxRequests: 100, windowMs: 60000 });
|
|
21
|
-
*
|
|
22
|
-
* if (await limiter.checkLimit("client-123")) {
|
|
23
|
-
* // Execute request
|
|
24
|
-
* } else {
|
|
25
|
-
* // Rate limited
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export class RateLimiter {
|
|
30
|
-
requestCounts = new Map();
|
|
31
|
-
windowMs;
|
|
32
|
-
maxRequests;
|
|
33
|
-
operationsSinceLastPurge = 0;
|
|
34
|
-
static PURGE_EVERY_N_OPS = 1000;
|
|
35
|
-
constructor(options) {
|
|
36
|
-
this.maxRequests = options.maxRequests;
|
|
37
|
-
this.windowMs = options.windowMs;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Check if request is allowed for the given key
|
|
41
|
-
*
|
|
42
|
-
* @param key - Identifier (client ID, IP, etc.)
|
|
43
|
-
* @returns true if allowed, false if rate limited
|
|
44
|
-
*/
|
|
45
|
-
checkLimit(key) {
|
|
46
|
-
const now = Date.now();
|
|
47
|
-
const requests = this.requestCounts.get(key) || [];
|
|
48
|
-
// Remove old requests outside the sliding window
|
|
49
|
-
const validRequests = requests.filter((time) => now - time < this.windowMs);
|
|
50
|
-
if (validRequests.length === 0) {
|
|
51
|
-
// No active requests for this key — remove from map to prevent unbounded growth
|
|
52
|
-
this.requestCounts.delete(key);
|
|
53
|
-
}
|
|
54
|
-
if (validRequests.length >= this.maxRequests) {
|
|
55
|
-
// Update with cleaned list
|
|
56
|
-
this.requestCounts.set(key, validRequests);
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
// Add current request timestamp
|
|
60
|
-
validRequests.push(now);
|
|
61
|
-
this.requestCounts.set(key, validRequests);
|
|
62
|
-
// Periodic full purge to catch keys that haven't been checked recently
|
|
63
|
-
this.operationsSinceLastPurge++;
|
|
64
|
-
if (this.operationsSinceLastPurge >= RateLimiter.PURGE_EVERY_N_OPS) {
|
|
65
|
-
this.purgeExpiredKeys();
|
|
66
|
-
}
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Remove all keys with no active requests in the current window.
|
|
71
|
-
* Called automatically every PURGE_EVERY_N_OPS operations.
|
|
72
|
-
*/
|
|
73
|
-
purgeExpiredKeys() {
|
|
74
|
-
const now = Date.now();
|
|
75
|
-
let purged = 0;
|
|
76
|
-
for (const [key, requests] of this.requestCounts) {
|
|
77
|
-
const active = requests.filter((time) => now - time < this.windowMs);
|
|
78
|
-
if (active.length === 0) {
|
|
79
|
-
this.requestCounts.delete(key);
|
|
80
|
-
purged++;
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
this.requestCounts.set(key, active);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
this.operationsSinceLastPurge = 0;
|
|
87
|
-
return purged;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Wait until request slot is available (with exponential backoff)
|
|
91
|
-
*
|
|
92
|
-
* @param key - Identifier (client ID, IP, etc.)
|
|
93
|
-
* @throws Error if max wait time exceeded (defaults to windowMs)
|
|
94
|
-
*/
|
|
95
|
-
async waitForSlot(key) {
|
|
96
|
-
let retries = 0;
|
|
97
|
-
const baseDelay = 100;
|
|
98
|
-
const maxWaitMs = this.windowMs;
|
|
99
|
-
const startTime = Date.now();
|
|
100
|
-
while (!this.checkLimit(key)) {
|
|
101
|
-
if (Date.now() - startTime >= maxWaitMs) {
|
|
102
|
-
throw new Error(`Rate limit wait timeout: waited ${maxWaitMs}ms for key "${key}"`);
|
|
103
|
-
}
|
|
104
|
-
// Exponential backoff: 100ms, 200ms, 400ms, 800ms, cap at 1000ms
|
|
105
|
-
const delay = Math.min(baseDelay * Math.pow(2, retries), 1000);
|
|
106
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
107
|
-
retries++;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Get current request count for a key
|
|
112
|
-
*/
|
|
113
|
-
getCurrentCount(key) {
|
|
114
|
-
const now = Date.now();
|
|
115
|
-
const requests = this.requestCounts.get(key) || [];
|
|
116
|
-
return requests.filter((time) => now - time < this.windowMs).length;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Get remaining requests for a key
|
|
120
|
-
*/
|
|
121
|
-
getRemainingRequests(key) {
|
|
122
|
-
return Math.max(0, this.maxRequests - this.getCurrentCount(key));
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Get time until next slot is available (in ms)
|
|
126
|
-
* Returns 0 if a slot is available now
|
|
127
|
-
*/
|
|
128
|
-
getTimeUntilSlot(key) {
|
|
129
|
-
const now = Date.now();
|
|
130
|
-
const requests = this.requestCounts.get(key) || [];
|
|
131
|
-
const validRequests = requests.filter((time) => now - time < this.windowMs);
|
|
132
|
-
if (validRequests.length < this.maxRequests) {
|
|
133
|
-
return 0;
|
|
134
|
-
}
|
|
135
|
-
// Find oldest request in window - when it expires, a slot opens
|
|
136
|
-
const oldest = Math.min(...validRequests);
|
|
137
|
-
return Math.max(0, oldest + this.windowMs - now);
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Clear rate limit history for a key
|
|
141
|
-
*/
|
|
142
|
-
clear(key) {
|
|
143
|
-
this.requestCounts.delete(key);
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Clear all rate limit history
|
|
147
|
-
*/
|
|
148
|
-
clearAll() {
|
|
149
|
-
this.requestCounts.clear();
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Get metrics for monitoring
|
|
153
|
-
*/
|
|
154
|
-
getMetrics() {
|
|
155
|
-
let totalRequests = 0;
|
|
156
|
-
const now = Date.now();
|
|
157
|
-
for (const requests of this.requestCounts.values()) {
|
|
158
|
-
totalRequests += requests.filter((time) => now - time < this.windowMs).length;
|
|
159
|
-
}
|
|
160
|
-
return {
|
|
161
|
-
keys: this.requestCounts.size,
|
|
162
|
-
totalRequests,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request Queue with Concurrency Control and Backpressure
|
|
3
|
-
*
|
|
4
|
-
* Limits the number of concurrent requests to prevent resource exhaustion
|
|
5
|
-
* and implements backpressure strategies when at capacity.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/request-queue
|
|
8
|
-
*/
|
|
9
|
-
import type { QueueMetrics, QueueOptions } from "../types.js";
|
|
10
|
-
/**
|
|
11
|
-
* RequestQueue manages concurrent request execution with backpressure
|
|
12
|
-
*
|
|
13
|
-
* Features:
|
|
14
|
-
* - Limits max concurrent requests (default: 10)
|
|
15
|
-
* - Multiple backpressure strategies: sleep, queue, reject
|
|
16
|
-
* - Metrics for monitoring (inFlight, queued)
|
|
17
|
-
* - Graceful degradation under load
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* const queue = new RequestQueue({
|
|
22
|
-
* maxConcurrent: 5,
|
|
23
|
-
* strategy: 'queue',
|
|
24
|
-
* sleepMs: 10
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* await queue.acquire();
|
|
28
|
-
* try {
|
|
29
|
-
* // Execute request
|
|
30
|
-
* } finally {
|
|
31
|
-
* queue.release();
|
|
32
|
-
* }
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare class RequestQueue {
|
|
36
|
-
private inFlight;
|
|
37
|
-
private maxConcurrent;
|
|
38
|
-
private strategy;
|
|
39
|
-
private sleepMs;
|
|
40
|
-
private waitQueue;
|
|
41
|
-
constructor(options: QueueOptions);
|
|
42
|
-
/**
|
|
43
|
-
* Acquire a slot for request execution
|
|
44
|
-
* Blocks until a slot is available based on backpressure strategy
|
|
45
|
-
*
|
|
46
|
-
* @throws {Error} If strategy is 'reject' and queue is at capacity
|
|
47
|
-
*/
|
|
48
|
-
acquire(): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Release a slot after request completion
|
|
51
|
-
* Notifies next waiting request if using queue strategy
|
|
52
|
-
*/
|
|
53
|
-
release(): void;
|
|
54
|
-
/**
|
|
55
|
-
* Get current queue metrics for monitoring
|
|
56
|
-
*/
|
|
57
|
-
getMetrics(): QueueMetrics;
|
|
58
|
-
/**
|
|
59
|
-
* Check if queue is at capacity
|
|
60
|
-
*/
|
|
61
|
-
isAtCapacity(): boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Get current in-flight request count
|
|
64
|
-
*/
|
|
65
|
-
getInFlight(): number;
|
|
66
|
-
/**
|
|
67
|
-
* Get current queued request count
|
|
68
|
-
*/
|
|
69
|
-
getQueued(): number;
|
|
70
|
-
}
|
|
71
|
-
//# sourceMappingURL=request-queue.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request-queue.d.ts","sourceRoot":"","sources":["../../../src/src/concurrency/request-queue.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAA+B;IAC/C,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAyB;gBAE9B,OAAO,EAAE,YAAY;IAMjC;;;;;OAKG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAsC9B;;;OAGG;IACH,OAAO,IAAI,IAAI;IAYf;;OAEG;IACH,UAAU,IAAI,YAAY;IAO1B;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,WAAW,IAAI,MAAM;IAIrB;;OAEG;IACH,SAAS,IAAI,MAAM;CAGpB"}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request Queue with Concurrency Control and Backpressure
|
|
3
|
-
*
|
|
4
|
-
* Limits the number of concurrent requests to prevent resource exhaustion
|
|
5
|
-
* and implements backpressure strategies when at capacity.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/request-queue
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* RequestQueue manages concurrent request execution with backpressure
|
|
11
|
-
*
|
|
12
|
-
* Features:
|
|
13
|
-
* - Limits max concurrent requests (default: 10)
|
|
14
|
-
* - Multiple backpressure strategies: sleep, queue, reject
|
|
15
|
-
* - Metrics for monitoring (inFlight, queued)
|
|
16
|
-
* - Graceful degradation under load
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const queue = new RequestQueue({
|
|
21
|
-
* maxConcurrent: 5,
|
|
22
|
-
* strategy: 'queue',
|
|
23
|
-
* sleepMs: 10
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* await queue.acquire();
|
|
27
|
-
* try {
|
|
28
|
-
* // Execute request
|
|
29
|
-
* } finally {
|
|
30
|
-
* queue.release();
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export class RequestQueue {
|
|
35
|
-
inFlight = 0;
|
|
36
|
-
maxConcurrent;
|
|
37
|
-
strategy;
|
|
38
|
-
sleepMs;
|
|
39
|
-
waitQueue = [];
|
|
40
|
-
constructor(options) {
|
|
41
|
-
this.maxConcurrent = options.maxConcurrent;
|
|
42
|
-
this.strategy = options.strategy;
|
|
43
|
-
this.sleepMs = options.sleepMs;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Acquire a slot for request execution
|
|
47
|
-
* Blocks until a slot is available based on backpressure strategy
|
|
48
|
-
*
|
|
49
|
-
* @throws {Error} If strategy is 'reject' and queue is at capacity
|
|
50
|
-
*/
|
|
51
|
-
async acquire() {
|
|
52
|
-
if (this.strategy === "reject") {
|
|
53
|
-
// Fail fast - reject immediately if at capacity
|
|
54
|
-
if (this.inFlight >= this.maxConcurrent) {
|
|
55
|
-
throw new Error(`Server at capacity (${this.maxConcurrent} concurrent requests)`);
|
|
56
|
-
}
|
|
57
|
-
this.inFlight++;
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (this.strategy === "queue") {
|
|
61
|
-
// Queue-based backpressure - wait in FIFO queue.
|
|
62
|
-
// Claim the slot BEFORE awaiting to prevent TOCTOU race:
|
|
63
|
-
// without this, two waiters woken back-to-back could both see
|
|
64
|
-
// inFlight < maxConcurrent and both increment past the limit.
|
|
65
|
-
if (this.inFlight >= this.maxConcurrent) {
|
|
66
|
-
await new Promise((resolve) => {
|
|
67
|
-
this.waitQueue.push(resolve);
|
|
68
|
-
});
|
|
69
|
-
// Slot was pre-claimed by release() before waking us
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
this.inFlight++;
|
|
73
|
-
}
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
// Sleep-based backpressure (default) - busy-wait with sleep.
|
|
77
|
-
// The check + increment is safe here because JS is single-threaded
|
|
78
|
-
// for synchronous code: after the while loop exits, no other code
|
|
79
|
-
// runs before inFlight++ (no await between check and increment).
|
|
80
|
-
while (this.inFlight >= this.maxConcurrent) {
|
|
81
|
-
await new Promise((resolve) => setTimeout(resolve, this.sleepMs));
|
|
82
|
-
}
|
|
83
|
-
this.inFlight++;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Release a slot after request completion
|
|
87
|
-
* Notifies next waiting request if using queue strategy
|
|
88
|
-
*/
|
|
89
|
-
release() {
|
|
90
|
-
if (this.strategy === "queue" && this.waitQueue.length > 0) {
|
|
91
|
-
// Transfer the slot directly to the next waiter without decrementing.
|
|
92
|
-
// This prevents the TOCTOU race: the waiter wakes up with
|
|
93
|
-
// the slot already claimed (inFlight unchanged).
|
|
94
|
-
const next = this.waitQueue.shift();
|
|
95
|
-
next();
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
this.inFlight--;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Get current queue metrics for monitoring
|
|
103
|
-
*/
|
|
104
|
-
getMetrics() {
|
|
105
|
-
return {
|
|
106
|
-
inFlight: this.inFlight,
|
|
107
|
-
queued: this.waitQueue.length,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Check if queue is at capacity
|
|
112
|
-
*/
|
|
113
|
-
isAtCapacity() {
|
|
114
|
-
return this.inFlight >= this.maxConcurrent;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Get current in-flight request count
|
|
118
|
-
*/
|
|
119
|
-
getInFlight() {
|
|
120
|
-
return this.inFlight;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Get current queued request count
|
|
124
|
-
*/
|
|
125
|
-
getQueued() {
|
|
126
|
-
return this.waitQueue.length;
|
|
127
|
-
}
|
|
128
|
-
}
|
package/esm/src/http/body.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP body reading, with a size ceiling.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp-app.ts`: these are pure functions over Web APIs with no
|
|
5
|
-
* dependency on `McpApp` state, which is exactly what made them separable when
|
|
6
|
-
* the dispatch code was not. Keeping them here makes them unit-testable without
|
|
7
|
-
* standing up a server.
|
|
8
|
-
*
|
|
9
|
-
* Not re-exported from `mod.ts` — this is internal.
|
|
10
|
-
*
|
|
11
|
-
* @module lib/server/http/body
|
|
12
|
-
*/
|
|
13
|
-
/** Default cap on a request body, in bytes. */
|
|
14
|
-
export declare const DEFAULT_MAX_BODY_BYTES = 1000000;
|
|
15
|
-
export declare class BodyTooLargeError extends Error {
|
|
16
|
-
constructor(maxBytes: number);
|
|
17
|
-
}
|
|
18
|
-
export declare function getClientIpFromHeaders(headers: Headers): string;
|
|
19
|
-
export declare function readBodyWithLimit(request: Request, maxBytes: number | null): Promise<Uint8Array>;
|
|
20
|
-
//# sourceMappingURL=body.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../../../src/src/http/body.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,+CAA+C;AAC/C,eAAO,MAAM,sBAAsB,UAAY,CAAC;AAEhD,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,QAAQ,EAAE,MAAM;CAI7B;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQ/D;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAAC,UAAU,CAAC,CAmCrB"}
|
package/esm/src/http/body.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP body reading, with a size ceiling.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp-app.ts`: these are pure functions over Web APIs with no
|
|
5
|
-
* dependency on `McpApp` state, which is exactly what made them separable when
|
|
6
|
-
* the dispatch code was not. Keeping them here makes them unit-testable without
|
|
7
|
-
* standing up a server.
|
|
8
|
-
*
|
|
9
|
-
* Not re-exported from `mod.ts` — this is internal.
|
|
10
|
-
*
|
|
11
|
-
* @module lib/server/http/body
|
|
12
|
-
*/
|
|
13
|
-
/** Default cap on a request body, in bytes. */
|
|
14
|
-
export const DEFAULT_MAX_BODY_BYTES = 1_000_000;
|
|
15
|
-
export class BodyTooLargeError extends Error {
|
|
16
|
-
constructor(maxBytes) {
|
|
17
|
-
super(`Payload too large. Max ${maxBytes} bytes.`);
|
|
18
|
-
this.name = "BodyTooLargeError";
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
export function getClientIpFromHeaders(headers) {
|
|
22
|
-
const forwarded = headers.get("x-forwarded-for");
|
|
23
|
-
if (forwarded) {
|
|
24
|
-
return forwarded.split(",")[0]?.trim() || "unknown";
|
|
25
|
-
}
|
|
26
|
-
return headers.get("x-real-ip") ??
|
|
27
|
-
headers.get("cf-connecting-ip") ??
|
|
28
|
-
"unknown";
|
|
29
|
-
}
|
|
30
|
-
export async function readBodyWithLimit(request, maxBytes) {
|
|
31
|
-
const contentLength = request.headers.get("content-length");
|
|
32
|
-
if (maxBytes !== null && contentLength) {
|
|
33
|
-
const length = Number(contentLength);
|
|
34
|
-
if (!Number.isNaN(length) && length > maxBytes) {
|
|
35
|
-
throw new BodyTooLargeError(maxBytes);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (!request.body) {
|
|
39
|
-
return new Uint8Array();
|
|
40
|
-
}
|
|
41
|
-
const reader = request.body.getReader();
|
|
42
|
-
const chunks = [];
|
|
43
|
-
let total = 0;
|
|
44
|
-
while (true) {
|
|
45
|
-
const { done, value } = await reader.read();
|
|
46
|
-
if (done)
|
|
47
|
-
break;
|
|
48
|
-
if (!value)
|
|
49
|
-
continue;
|
|
50
|
-
total += value.length;
|
|
51
|
-
if (maxBytes !== null && total > maxBytes) {
|
|
52
|
-
throw new BodyTooLargeError(maxBytes);
|
|
53
|
-
}
|
|
54
|
-
chunks.push(value);
|
|
55
|
-
}
|
|
56
|
-
const body = new Uint8Array(total);
|
|
57
|
-
let offset = 0;
|
|
58
|
-
for (const chunk of chunks) {
|
|
59
|
-
body.set(chunk, offset);
|
|
60
|
-
offset += chunk.length;
|
|
61
|
-
}
|
|
62
|
-
return body;
|
|
63
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request-scoped guards: who is calling, and may they touch this task.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp-app.ts`. Like `body.ts`, these are pure over their
|
|
5
|
-
* arguments — they read no `McpApp` state — which is what let them move at all.
|
|
6
|
-
* `jsonRpcResponse` stays in `mcp-app.ts` and is passed in rather than imported,
|
|
7
|
-
* so this module has no edge back to the class.
|
|
8
|
-
*
|
|
9
|
-
* Not re-exported from `mod.ts` — internal.
|
|
10
|
-
*
|
|
11
|
-
* @module lib/server/http/request-guards
|
|
12
|
-
*/
|
|
13
|
-
import type { AuthInfo } from "../auth/types.js";
|
|
14
|
-
import type { McpAppOptions } from "../types.js";
|
|
15
|
-
/**
|
|
16
|
-
* Principal to bind a `requestState` to.
|
|
17
|
-
*
|
|
18
|
-
* Three cases, only two of which are acceptable:
|
|
19
|
-
*
|
|
20
|
-
* 1. **No auth configured** — every caller shares one authority by definition, so
|
|
21
|
-
* there is no boundary to enforce and the sentinel above is honest about it.
|
|
22
|
-
* The method and argument bindings still apply.
|
|
23
|
-
* 2. **Authenticated with a subject** — the binding separates callers, which is
|
|
24
|
-
* the case it exists for.
|
|
25
|
-
* 3. **Authenticated without a usable subject** — `AuthInfo.subject` falls back to
|
|
26
|
-
* `"unknown"` for a valid token carrying no `sub` claim. The reserved no-auth
|
|
27
|
-
* sentinel is also invalid for authenticated callers. Sealing against either
|
|
28
|
-
* would hide an authority collision. Returns `null` so the caller fails closed.
|
|
29
|
-
*/
|
|
30
|
-
export declare function callerPrincipal(authInfo: AuthInfo | undefined): string | null;
|
|
31
|
-
/**
|
|
32
|
-
* Authorization key for a task-related request.
|
|
33
|
-
*
|
|
34
|
-
* Defaults to the authenticated principal. A consumer whose authorization depends
|
|
35
|
-
* on more than identity — a tenant, an organisation, a workspace — supplies
|
|
36
|
-
* `taskOwnerKey` so that dimension becomes part of the key. Without it, binding to
|
|
37
|
-
* the subject alone leaves a task created under one tenant reachable from another
|
|
38
|
-
* by the same user.
|
|
39
|
-
*/
|
|
40
|
-
export declare function taskOwnerKeyFor(options: McpAppOptions, authInfo: AuthInfo | undefined, request: Request | undefined, principal: string): Promise<string | null>;
|
|
41
|
-
/** The error to return when `taskOwnerKey` produced an unusable value. */
|
|
42
|
-
export declare function noOwnerKeyError(id: unknown, version: string): Response;
|
|
43
|
-
/** The error to return when no caller boundary can be established. */
|
|
44
|
-
export declare function noPrincipalError(id: unknown, version: string): Response;
|
|
45
|
-
/** Lowercase hex encoding, for the requestState nonce. */
|
|
46
|
-
export declare function bytesToHexLocal(bytes: Uint8Array): string;
|
|
47
|
-
/**
|
|
48
|
-
* Build a JSON-RPC response with an explicit HTTP status.
|
|
49
|
-
*
|
|
50
|
-
* Module-scoped rather than nested in `startHttp`: request-validation helpers on
|
|
51
|
-
* the class need it too, and duplicating the header defaults is how a response
|
|
52
|
-
* ends up missing its Content-Type.
|
|
53
|
-
*/
|
|
54
|
-
//# sourceMappingURL=request-guards.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request-guards.d.ts","sourceRoot":"","sources":["../../../src/src/http/request-guards.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAGjD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAc7E;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,aAAa,EACtB,QAAQ,EAAE,QAAQ,GAAG,SAAS,EAC9B,OAAO,EAAE,OAAO,GAAG,SAAS,EAC5B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAkBxB;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,CAmBtE;AAED,sEAAsE;AACtE,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,CAmBvE;AAED,0DAA0D;AAC1D,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEzD;AAED;;;;;;GAMG"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request-scoped guards: who is calling, and may they touch this task.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp-app.ts`. Like `body.ts`, these are pure over their
|
|
5
|
-
* arguments — they read no `McpApp` state — which is what let them move at all.
|
|
6
|
-
* `jsonRpcResponse` stays in `mcp-app.ts` and is passed in rather than imported,
|
|
7
|
-
* so this module has no edge back to the class.
|
|
8
|
-
*
|
|
9
|
-
* Not re-exported from `mod.ts` — internal.
|
|
10
|
-
*
|
|
11
|
-
* @module lib/server/http/request-guards
|
|
12
|
-
*/
|
|
13
|
-
import { ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
14
|
-
import { jsonRpcResponse, MRTR_NO_AUTH_PRINCIPAL } from "./wire.js";
|
|
15
|
-
/**
|
|
16
|
-
* Principal to bind a `requestState` to.
|
|
17
|
-
*
|
|
18
|
-
* Three cases, only two of which are acceptable:
|
|
19
|
-
*
|
|
20
|
-
* 1. **No auth configured** — every caller shares one authority by definition, so
|
|
21
|
-
* there is no boundary to enforce and the sentinel above is honest about it.
|
|
22
|
-
* The method and argument bindings still apply.
|
|
23
|
-
* 2. **Authenticated with a subject** — the binding separates callers, which is
|
|
24
|
-
* the case it exists for.
|
|
25
|
-
* 3. **Authenticated without a usable subject** — `AuthInfo.subject` falls back to
|
|
26
|
-
* `"unknown"` for a valid token carrying no `sub` claim. The reserved no-auth
|
|
27
|
-
* sentinel is also invalid for authenticated callers. Sealing against either
|
|
28
|
-
* would hide an authority collision. Returns `null` so the caller fails closed.
|
|
29
|
-
*/
|
|
30
|
-
export function callerPrincipal(authInfo) {
|
|
31
|
-
if (authInfo === undefined)
|
|
32
|
-
return MRTR_NO_AUTH_PRINCIPAL;
|
|
33
|
-
const subject = authInfo.subject;
|
|
34
|
-
if (subject === undefined || subject === "" || subject === "unknown" ||
|
|
35
|
-
subject === MRTR_NO_AUTH_PRINCIPAL) {
|
|
36
|
-
// `null`, not a throw: the task handlers call this outside any try/catch, so
|
|
37
|
-
// a throw would surface through the POST-level catch as -32700 "Parse error"
|
|
38
|
-
// — a diagnosis pointing nowhere near a misconfigured auth provider. The
|
|
39
|
-
// caller turns this into an explicit error instead.
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
return subject;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Authorization key for a task-related request.
|
|
46
|
-
*
|
|
47
|
-
* Defaults to the authenticated principal. A consumer whose authorization depends
|
|
48
|
-
* on more than identity — a tenant, an organisation, a workspace — supplies
|
|
49
|
-
* `taskOwnerKey` so that dimension becomes part of the key. Without it, binding to
|
|
50
|
-
* the subject alone leaves a task created under one tenant reachable from another
|
|
51
|
-
* by the same user.
|
|
52
|
-
*/
|
|
53
|
-
export async function taskOwnerKeyFor(options, authInfo, request, principal) {
|
|
54
|
-
if (options.taskOwnerKey === undefined || request === undefined) {
|
|
55
|
-
return principal;
|
|
56
|
-
}
|
|
57
|
-
let key;
|
|
58
|
-
try {
|
|
59
|
-
key = await options.taskOwnerKey(authInfo, request);
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
// A throwing hook is a consumer bug, but it must fail closed and legibly —
|
|
63
|
-
// not escape to the POST-level catch and be reported as -32700 Parse error.
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
// An empty or non-string key would put every caller under one owner, undoing
|
|
67
|
-
// the isolation the hook exists to provide — and `""` is exactly what the
|
|
68
|
-
// obvious implementation yields when a header is missing. Refuse it rather
|
|
69
|
-
// than silently sharing tasks between callers.
|
|
70
|
-
if (typeof key !== "string" || key.length === 0)
|
|
71
|
-
return null;
|
|
72
|
-
return key;
|
|
73
|
-
}
|
|
74
|
-
/** The error to return when `taskOwnerKey` produced an unusable value. */
|
|
75
|
-
export function noOwnerKeyError(id, version) {
|
|
76
|
-
return jsonRpcResponse({
|
|
77
|
-
jsonrpc: "2.0",
|
|
78
|
-
id,
|
|
79
|
-
error: {
|
|
80
|
-
code: ErrorCode.InternalError,
|
|
81
|
-
message: "taskOwnerKey returned an empty or non-string value, which would place every caller under one owner.",
|
|
82
|
-
data: {
|
|
83
|
-
problem: "invalid_task_owner_key",
|
|
84
|
-
recovery: 'Return a non-empty string. If the value it derives from can be absent, decide explicitly what an anonymous caller\'s key is rather than letting it fall to "".',
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
}, 500, { "MCP-Protocol-Version": version });
|
|
88
|
-
}
|
|
89
|
-
/** The error to return when no caller boundary can be established. */
|
|
90
|
-
export function noPrincipalError(id, version) {
|
|
91
|
-
return jsonRpcResponse({
|
|
92
|
-
jsonrpc: "2.0",
|
|
93
|
-
id,
|
|
94
|
-
error: {
|
|
95
|
-
code: ErrorCode.InternalError,
|
|
96
|
-
message: "Server cannot establish a caller identity: the auth provider returned no usable subject for an authenticated request.",
|
|
97
|
-
data: {
|
|
98
|
-
problem: "no_caller_principal",
|
|
99
|
-
recovery: "Configure the auth provider to supply a subject claim, or disable auth if all callers genuinely share one authority.",
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
}, 500, { "MCP-Protocol-Version": version });
|
|
103
|
-
}
|
|
104
|
-
/** Lowercase hex encoding, for the requestState nonce. */
|
|
105
|
-
export function bytesToHexLocal(bytes) {
|
|
106
|
-
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Build a JSON-RPC response with an explicit HTTP status.
|
|
110
|
-
*
|
|
111
|
-
* Module-scoped rather than nested in `startHttp`: request-validation helpers on
|
|
112
|
-
* the class need it too, and duplicating the header defaults is how a response
|
|
113
|
-
* ends up missing its Content-Type.
|
|
114
|
-
*/
|
package/esm/src/http/wire.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wire-level primitives shared by the dispatch code and its extracted guards.
|
|
3
|
-
*
|
|
4
|
-
* This module exists to keep `http/request-guards.ts` from importing
|
|
5
|
-
* `mcp-app.ts`, which would be a cycle. It holds only what both need: no state,
|
|
6
|
-
* no `McpApp` reference.
|
|
7
|
-
*
|
|
8
|
-
* Not re-exported from `mod.ts` — internal.
|
|
9
|
-
*
|
|
10
|
-
* @module lib/server/http/wire
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Sentinel used when there is no authentication at all.
|
|
14
|
-
*
|
|
15
|
-
* Deliberately not a plausible subject value. Authenticated callers are also
|
|
16
|
-
* checked against this exact value before MRTR or task state is accepted, so a
|
|
17
|
-
* custom provider cannot create an authority collision by returning it.
|
|
18
|
-
*/
|
|
19
|
-
export declare const MRTR_NO_AUTH_PRINCIPAL = "\0unauthenticated";
|
|
20
|
-
export declare function jsonRpcResponse(payload: Record<string, unknown>, status: number, headers?: Record<string, string>): Response;
|
|
21
|
-
/**
|
|
22
|
-
* Narrow type-guard: returns true iff `v` is a plain object (not array, not null).
|
|
23
|
-
* Used to safely extract fields from JSON-RPC `params` without unsafe casts.
|
|
24
|
-
*/
|
|
25
|
-
export declare function isRecord(v: unknown): v is Record<string, unknown>;
|
|
26
|
-
//# sourceMappingURL=wire.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wire.d.ts","sourceRoot":"","sources":["../../../src/src/http/wire.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,sBAA0B,CAAC;AAE9D,wBAAgB,eAAe,CAC7B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC/B,QAAQ,CAQV;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEjE"}
|