@casys/mcp-server 0.24.0 → 0.24.1
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/README.md +53 -0
- package/mod.ts +3 -0
- package/package.json +1 -1
- package/src/mcp-app.ts +98 -0
- package/src/mrtr/mod.ts +7 -0
- package/src/mrtr/replay-store.ts +103 -0
- package/src/mrtr/types.ts +14 -0
package/README.md
CHANGED
|
@@ -239,6 +239,59 @@ MCP_AUTH_AUDIENCE=https://prod.example.com ./my-server --http --port 3000
|
|
|
239
239
|
|
|
240
240
|
Priority: `programmatic > env vars > YAML > no auth`
|
|
241
241
|
|
|
242
|
+
### MRTR requestState replay protection
|
|
243
|
+
|
|
244
|
+
When a tool returns `resultType: "input_required"`, configure a signing key so
|
|
245
|
+
the framework can bind the continuation to the principal, method, arguments,
|
|
246
|
+
expiry, and a random nonce:
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
const server = new McpApp({
|
|
250
|
+
name: "my-api",
|
|
251
|
+
version: "1.0.0",
|
|
252
|
+
mrtr: {
|
|
253
|
+
signingKey: Deno.env.get("MCP_MRTR_SIGNING_KEY"),
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Each verified nonce is consumed before the handler runs. With no explicit
|
|
259
|
+
`replayStore`, the built-in `MemoryMrtrReplayStore` rejects a second use within
|
|
260
|
+
one continuously running process.
|
|
261
|
+
|
|
262
|
+
Multi-instance or restart-safe deployments must inject one durable atomic store
|
|
263
|
+
shared by every instance:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import type { MrtrReplayStore } from "@casys/mcp-server";
|
|
267
|
+
|
|
268
|
+
const replayStore: MrtrReplayStore = {
|
|
269
|
+
async consume(nonce, expiresAt) {
|
|
270
|
+
// Atomically reserve the nonce until its signed expiry.
|
|
271
|
+
// Redis equivalent: SET mrtr:<nonce> 1 NX EXAT <expiresAt>
|
|
272
|
+
return await reserveNonce(nonce, expiresAt);
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const server = new McpApp({
|
|
277
|
+
name: "my-api",
|
|
278
|
+
version: "1.0.0",
|
|
279
|
+
mrtr: {
|
|
280
|
+
signingKey: Deno.env.get("MCP_MRTR_SIGNING_KEY"),
|
|
281
|
+
replayStore,
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
`consume()` must return `true` only for the caller that wins the atomic
|
|
287
|
+
reservation, `false` for a nonce already consumed, and throw when the store is
|
|
288
|
+
unavailable. Store failures are fail-closed; the handler is not executed.
|
|
289
|
+
|
|
290
|
+
This is at-most-once admission, not exactly-once completion. If business logic
|
|
291
|
+
commits and the response is lost, replaying the same token is rejected.
|
|
292
|
+
Returning the prior result safely requires a separate idempotency/result ledger,
|
|
293
|
+
ideally paired with idempotency support in the downstream system.
|
|
294
|
+
|
|
242
295
|
### RFC 9728
|
|
243
296
|
|
|
244
297
|
When auth is configured, the framework automatically exposes
|
package/mod.ts
CHANGED
|
@@ -275,11 +275,14 @@ export type {
|
|
|
275
275
|
InputRequestEntry,
|
|
276
276
|
InputRequestMethod,
|
|
277
277
|
InputRequiredSignal,
|
|
278
|
+
MemoryMrtrReplayStoreOptions,
|
|
278
279
|
MrtrOptions,
|
|
280
|
+
MrtrReplayStore,
|
|
279
281
|
} from "./src/mrtr/mod.js";
|
|
280
282
|
export {
|
|
281
283
|
exportStateKey,
|
|
282
284
|
generateStateKey,
|
|
285
|
+
MemoryMrtrReplayStore,
|
|
283
286
|
sealRequestState,
|
|
284
287
|
verifyRequestState,
|
|
285
288
|
} from "./src/mrtr/mod.js";
|
package/package.json
CHANGED
package/src/mcp-app.ts
CHANGED
|
@@ -88,6 +88,8 @@ import {
|
|
|
88
88
|
importStateKey,
|
|
89
89
|
type InputRequestEntry,
|
|
90
90
|
type InputRequiredSignal,
|
|
91
|
+
MemoryMrtrReplayStore,
|
|
92
|
+
type MrtrReplayStore,
|
|
91
93
|
paramsDigest,
|
|
92
94
|
sealRequestState,
|
|
93
95
|
verifyRequestState,
|
|
@@ -440,6 +442,14 @@ export class McpApp {
|
|
|
440
442
|
*/
|
|
441
443
|
private mrtrKeyPromise: Promise<CryptoKey | null> | null = null;
|
|
442
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Consumes a verified requestState nonce before the handler runs.
|
|
447
|
+
*
|
|
448
|
+
* A signed deployment always has a store: process-local by default, or the
|
|
449
|
+
* shared atomic implementation supplied by the consumer.
|
|
450
|
+
*/
|
|
451
|
+
private readonly mrtrReplayStore: MrtrReplayStore | null;
|
|
452
|
+
|
|
443
453
|
/**
|
|
444
454
|
* Validated `ttlMs`. The spec requires servers to provide a value `>= 0`, and
|
|
445
455
|
* an integer — clients are told to ignore a negative one, which would make a
|
|
@@ -512,6 +522,23 @@ export class McpApp {
|
|
|
512
522
|
"or the exported generateStateKey() + exportStateKey() helpers.",
|
|
513
523
|
);
|
|
514
524
|
}
|
|
525
|
+
if (
|
|
526
|
+
options.mrtr?.replayStore !== undefined && mrtrKey === undefined
|
|
527
|
+
) {
|
|
528
|
+
throw new Error(
|
|
529
|
+
"[McpApp] mrtr.replayStore requires mrtr.signingKey. " +
|
|
530
|
+
"Only a verified token exposes a trusted nonce to consume.",
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
this.mrtrReplayStore = mrtrKey === undefined
|
|
534
|
+
? null
|
|
535
|
+
: options.mrtr?.replayStore ?? new MemoryMrtrReplayStore();
|
|
536
|
+
if (mrtrKey !== undefined && options.mrtr?.replayStore === undefined) {
|
|
537
|
+
(options.logger ?? console.error)(
|
|
538
|
+
"[McpApp] WARNING: MRTR replay protection is process-local. " +
|
|
539
|
+
"Load-balanced deployments must configure one shared atomic mrtr.replayStore.",
|
|
540
|
+
);
|
|
541
|
+
}
|
|
515
542
|
|
|
516
543
|
// Create SDK MCP server
|
|
517
544
|
this.mcpServer = new McpServer(
|
|
@@ -2121,6 +2148,77 @@ export class McpApp {
|
|
|
2121
2148
|
400,
|
|
2122
2149
|
);
|
|
2123
2150
|
}
|
|
2151
|
+
|
|
2152
|
+
const replayStore = this.mrtrReplayStore;
|
|
2153
|
+
if (replayStore === null) {
|
|
2154
|
+
return jsonRpcResponse(
|
|
2155
|
+
{
|
|
2156
|
+
jsonrpc: "2.0",
|
|
2157
|
+
id,
|
|
2158
|
+
error: {
|
|
2159
|
+
code: ErrorCode.InternalError,
|
|
2160
|
+
message: "MRTR replay protection is unavailable",
|
|
2161
|
+
data: {
|
|
2162
|
+
problem: "mrtr_replay_store_unavailable",
|
|
2163
|
+
recovery:
|
|
2164
|
+
"Configure mrtr.signingKey with a working replay store and retry the full interaction.",
|
|
2165
|
+
},
|
|
2166
|
+
},
|
|
2167
|
+
},
|
|
2168
|
+
500,
|
|
2169
|
+
{ "MCP-Protocol-Version": statelessVersion },
|
|
2170
|
+
);
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
let consumed: boolean;
|
|
2174
|
+
try {
|
|
2175
|
+
const storeResult = await replayStore.consume(
|
|
2176
|
+
verdict.payload.nonce,
|
|
2177
|
+
verdict.payload.exp,
|
|
2178
|
+
);
|
|
2179
|
+
if (storeResult !== true && storeResult !== false) {
|
|
2180
|
+
throw new Error(
|
|
2181
|
+
"mrtr.replayStore.consume() must return a boolean",
|
|
2182
|
+
);
|
|
2183
|
+
}
|
|
2184
|
+
consumed = storeResult;
|
|
2185
|
+
} catch {
|
|
2186
|
+
this.log(
|
|
2187
|
+
"[WARN] MRTR replay store unavailable; retry rejected before handler",
|
|
2188
|
+
);
|
|
2189
|
+
return jsonRpcResponse(
|
|
2190
|
+
{
|
|
2191
|
+
jsonrpc: "2.0",
|
|
2192
|
+
id,
|
|
2193
|
+
error: {
|
|
2194
|
+
code: ErrorCode.InternalError,
|
|
2195
|
+
message: "MRTR replay protection is unavailable",
|
|
2196
|
+
data: {
|
|
2197
|
+
problem: "mrtr_replay_store_unavailable",
|
|
2198
|
+
recovery:
|
|
2199
|
+
"Restore the shared replay store, then restart the interaction to obtain a fresh requestState.",
|
|
2200
|
+
},
|
|
2201
|
+
},
|
|
2202
|
+
},
|
|
2203
|
+
500,
|
|
2204
|
+
{ "MCP-Protocol-Version": statelessVersion },
|
|
2205
|
+
);
|
|
2206
|
+
}
|
|
2207
|
+
if (!consumed) {
|
|
2208
|
+
return jsonRpcResponse(
|
|
2209
|
+
{
|
|
2210
|
+
jsonrpc: "2.0",
|
|
2211
|
+
id,
|
|
2212
|
+
error: {
|
|
2213
|
+
code: JSONRPC_INVALID_PARAMS,
|
|
2214
|
+
message: "Invalid requestState: replayed",
|
|
2215
|
+
data: { reason: "replayed" },
|
|
2216
|
+
},
|
|
2217
|
+
},
|
|
2218
|
+
400,
|
|
2219
|
+
{ "MCP-Protocol-Version": statelessVersion },
|
|
2220
|
+
);
|
|
2221
|
+
}
|
|
2124
2222
|
retryVerified = true;
|
|
2125
2223
|
}
|
|
2126
2224
|
}
|
package/src/mrtr/mod.ts
CHANGED
|
@@ -15,6 +15,13 @@ export type {
|
|
|
15
15
|
MrtrOptions,
|
|
16
16
|
} from "./types.js";
|
|
17
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
|
+
|
|
18
25
|
// requestState seal/verify (HMAC-SHA256 integrity protection)
|
|
19
26
|
export type {
|
|
20
27
|
RequestStatePayload,
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
}
|
package/src/mrtr/types.ts
CHANGED
|
@@ -90,4 +90,18 @@ export interface MrtrOptions {
|
|
|
90
90
|
* shared store on the first use).
|
|
91
91
|
*/
|
|
92
92
|
defaultTtlSecs?: number;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Atomic store used to reject a second use of the same signed requestState.
|
|
96
|
+
*
|
|
97
|
+
* With a signing key and no explicit store, McpApp uses a process-local
|
|
98
|
+
* MemoryMrtrReplayStore. That protects one continuously running process only:
|
|
99
|
+
* a restart loses its reservations. Every instance in a load-balanced
|
|
100
|
+
* deployment MUST receive the same durable shared store implementation.
|
|
101
|
+
*
|
|
102
|
+
* The nonce is consumed before the handler runs. This provides at-most-once
|
|
103
|
+
* admission, not exactly-once completion: if the handler commits a mutation
|
|
104
|
+
* and its response is lost, the same token is still rejected on retry.
|
|
105
|
+
*/
|
|
106
|
+
replayStore?: import("./replay-store.js").MrtrReplayStore;
|
|
93
107
|
}
|