@casys/mcp-server 0.25.0 → 0.26.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 +54 -2
- package/mod.ts +2 -0
- package/package.json +2 -1
- package/src/mcp-app.ts +788 -317
- package/src/mrtr/mod.ts +8 -0
- package/src/mrtr/retry-guard.ts +110 -0
- package/src/mrtr/types.ts +1 -1
- package/src/types.ts +71 -10
- package/src/testdata/stdio-e2e-server.ts +0 -30
package/src/mrtr/mod.ts
CHANGED
|
@@ -22,6 +22,14 @@ export type {
|
|
|
22
22
|
} from "./replay-store.js";
|
|
23
23
|
export { MemoryMrtrReplayStore } from "./replay-store.js";
|
|
24
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
|
+
|
|
25
33
|
// requestState seal/verify (HMAC-SHA256 integrity protection)
|
|
26
34
|
export type {
|
|
27
35
|
RequestStatePayload,
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MRTR retry admission policy.
|
|
3
|
+
*
|
|
4
|
+
* This module owns the transport-independent decision whether a retried
|
|
5
|
+
* request may reach application code. HTTP and stdio adapt its outcomes to
|
|
6
|
+
* their respective error envelopes; neither transport reimplements HMAC
|
|
7
|
+
* verification or nonce consumption.
|
|
8
|
+
*
|
|
9
|
+
* @module lib/server/mrtr/retry-guard
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { MrtrReplayStore } from "./replay-store.js";
|
|
13
|
+
import {
|
|
14
|
+
type RequestStateRejectionReason,
|
|
15
|
+
verifyRequestState,
|
|
16
|
+
} from "./request-state.js";
|
|
17
|
+
|
|
18
|
+
/** Bind a requestState to the retry that is attempting to spend it. */
|
|
19
|
+
export interface MrtrRetryBindings {
|
|
20
|
+
readonly principal: string;
|
|
21
|
+
readonly method: string;
|
|
22
|
+
readonly paramsDigest: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Inputs required to decide whether one MRTR retry is admissible. */
|
|
26
|
+
export interface GuardMrtrRetryInput {
|
|
27
|
+
/** Presence matters; response contents remain untrusted handler input. */
|
|
28
|
+
readonly inputResponses?: Record<string, unknown>;
|
|
29
|
+
/** Attacker-controlled state echoed by the client, when any. */
|
|
30
|
+
readonly requestState?: string;
|
|
31
|
+
/** Null deliberately represents the explicitly unprotected deployment mode. */
|
|
32
|
+
readonly key: CryptoKey | null;
|
|
33
|
+
/** Required only when a signed requestState is presented. */
|
|
34
|
+
readonly replayStore: MrtrReplayStore | null;
|
|
35
|
+
readonly bindings: MrtrRetryBindings;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Transport-neutral admission outcome.
|
|
40
|
+
*
|
|
41
|
+
* `accepted.retryVerified` is deliberately tri-state: absent for a first leg,
|
|
42
|
+
* false for the configured unprotected mode, and true only after signature,
|
|
43
|
+
* binding and atomic nonce reservation all succeeded.
|
|
44
|
+
*/
|
|
45
|
+
export type MrtrRetryGuardOutcome =
|
|
46
|
+
| { readonly kind: "accepted"; readonly retryVerified?: boolean }
|
|
47
|
+
| { readonly kind: "missing_state" }
|
|
48
|
+
| {
|
|
49
|
+
readonly kind: "invalid_state";
|
|
50
|
+
readonly reason: RequestStateRejectionReason;
|
|
51
|
+
}
|
|
52
|
+
| {
|
|
53
|
+
readonly kind: "replay_store_unavailable";
|
|
54
|
+
readonly reason: "missing" | "consume_failed";
|
|
55
|
+
}
|
|
56
|
+
| { readonly kind: "replayed" };
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Decide whether a retried request may enter application code.
|
|
60
|
+
*
|
|
61
|
+
* A signed state is verified and its nonce is atomically reserved before the
|
|
62
|
+
* handler runs. This intentionally gives at-most-once admission, not exactly
|
|
63
|
+
* once completion: a transport adapter must never move consumption after a
|
|
64
|
+
* business side effect.
|
|
65
|
+
*/
|
|
66
|
+
export async function guardMrtrRetry(
|
|
67
|
+
input: GuardMrtrRetryInput,
|
|
68
|
+
): Promise<MrtrRetryGuardOutcome> {
|
|
69
|
+
const hasInputResponses = input.inputResponses !== undefined;
|
|
70
|
+
const { requestState, key } = input;
|
|
71
|
+
|
|
72
|
+
if (hasInputResponses && requestState === undefined && key !== null) {
|
|
73
|
+
return { kind: "missing_state" };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (requestState === undefined) {
|
|
77
|
+
return { kind: "accepted" };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (key === null) {
|
|
81
|
+
return { kind: "accepted", retryVerified: false };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const verdict = await verifyRequestState(requestState, key, input.bindings);
|
|
85
|
+
if (!verdict.ok) {
|
|
86
|
+
return { kind: "invalid_state", reason: verdict.reason };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (input.replayStore === null) {
|
|
90
|
+
return { kind: "replay_store_unavailable", reason: "missing" };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let consumed: boolean;
|
|
94
|
+
try {
|
|
95
|
+
const result = await input.replayStore.consume(
|
|
96
|
+
verdict.payload.nonce,
|
|
97
|
+
verdict.payload.exp,
|
|
98
|
+
);
|
|
99
|
+
if (result !== true && result !== false) {
|
|
100
|
+
throw new Error("mrtr.replayStore.consume() must return a boolean");
|
|
101
|
+
}
|
|
102
|
+
consumed = result;
|
|
103
|
+
} catch {
|
|
104
|
+
return { kind: "replay_store_unavailable", reason: "consume_failed" };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return consumed
|
|
108
|
+
? { kind: "accepted", retryVerified: true }
|
|
109
|
+
: { kind: "replayed" };
|
|
110
|
+
}
|
package/src/mrtr/types.ts
CHANGED
|
@@ -62,7 +62,7 @@ export interface InputRequiredSignal {
|
|
|
62
62
|
/**
|
|
63
63
|
* Configuration for the MRTR subsystem in McpAppOptions.
|
|
64
64
|
*
|
|
65
|
-
* Wire this in to McpAppOptions.mrtr before calling startHttp().
|
|
65
|
+
* Wire this in to McpAppOptions.mrtr before calling start() or startHttp().
|
|
66
66
|
*/
|
|
67
67
|
export interface MrtrOptions {
|
|
68
68
|
/**
|
package/src/types.ts
CHANGED
|
@@ -223,9 +223,10 @@ export interface McpAppOptions {
|
|
|
223
223
|
* Multi Round-Trip Requests (spec 2026-07-28, SEP-2322).
|
|
224
224
|
*
|
|
225
225
|
* Configures how `requestState` is protected when a tool handler asks the
|
|
226
|
-
* client for input mid-call.
|
|
227
|
-
*
|
|
228
|
-
*
|
|
226
|
+
* client for input mid-call. Consulted by stateless HTTP and by the v2 stdio
|
|
227
|
+
* serving entry. On a legacy stdio connection the SDK fulfils the embedded
|
|
228
|
+
* requests through its compatibility shim, then re-enters the same verified
|
|
229
|
+
* handler; application code keeps one MRTR contract across both eras.
|
|
229
230
|
*
|
|
230
231
|
* Without `signingKey`, `requestState` travels unprotected and the server logs
|
|
231
232
|
* a warning at startup. That is only acceptable when tampering with it can
|
|
@@ -325,27 +326,84 @@ export interface MCPResource {
|
|
|
325
326
|
/** Description of the resource */
|
|
326
327
|
description?: string;
|
|
327
328
|
|
|
328
|
-
/**
|
|
329
|
+
/** Optional display title, distinct from the protocol-required `name`. */
|
|
330
|
+
title?: string;
|
|
331
|
+
|
|
332
|
+
/** Optional visual hints advertised in `resources/list`. */
|
|
333
|
+
icons?: Array<{
|
|
334
|
+
src: string;
|
|
335
|
+
mimeType?: string;
|
|
336
|
+
sizes?: string[];
|
|
337
|
+
theme?: "light" | "dark";
|
|
338
|
+
}>;
|
|
339
|
+
|
|
340
|
+
/** Optional client-facing resource annotations. */
|
|
341
|
+
annotations?: {
|
|
342
|
+
audience?: Array<"user" | "assistant">;
|
|
343
|
+
priority?: number;
|
|
344
|
+
lastModified?: string;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/** Optional protocol/extension metadata advertised in `resources/list`. */
|
|
348
|
+
_meta?: Record<string, unknown>;
|
|
349
|
+
|
|
350
|
+
/** MIME type, when known before the resource is read. */
|
|
329
351
|
mimeType?: string;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Byte size of the resource when it is known.
|
|
355
|
+
*
|
|
356
|
+
* This is an exact byte-size attestation: when supplied, every read must
|
|
357
|
+
* return exactly this many UTF-8 bytes for `text`, or decoded bytes for
|
|
358
|
+
* `blob`. It is surfaced in `resources/list` so clients can make loading
|
|
359
|
+
* decisions before reading. Must be a non-negative safe integer.
|
|
360
|
+
*/
|
|
361
|
+
size?: number;
|
|
330
362
|
}
|
|
331
363
|
|
|
332
364
|
/**
|
|
333
|
-
*
|
|
365
|
+
* Common fields of every resource response.
|
|
334
366
|
*/
|
|
335
|
-
|
|
336
|
-
/** URI of the resource
|
|
367
|
+
interface ResourceContentBase {
|
|
368
|
+
/** URI of the resource. It must exactly match the URI that was requested. */
|
|
337
369
|
uri: string;
|
|
338
|
-
/** MIME type of the content */
|
|
370
|
+
/** Non-empty MIME type of the content. */
|
|
339
371
|
mimeType: string;
|
|
340
|
-
/**
|
|
372
|
+
/** Optional MCP metadata passed through to the protocol response. */
|
|
373
|
+
_meta?: Record<string, unknown>;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** A UTF-8/text resource response. */
|
|
377
|
+
export interface TextResourceContent extends ResourceContentBase {
|
|
378
|
+
/** Text payload, including HTML for MCP Apps. */
|
|
341
379
|
text: string;
|
|
380
|
+
/** Text and blob payloads are mutually exclusive. */
|
|
381
|
+
blob?: never;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** A binary resource response encoded as canonical standard base64. */
|
|
385
|
+
export interface BlobResourceContent extends ResourceContentBase {
|
|
386
|
+
/** Standard, padded base64 payload. */
|
|
387
|
+
blob: string;
|
|
388
|
+
/** Text and blob payloads are mutually exclusive. */
|
|
389
|
+
text?: never;
|
|
342
390
|
}
|
|
343
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Content returned by a resource handler.
|
|
394
|
+
*
|
|
395
|
+
* Exactly one payload form is allowed: `text` for text/HTML, or `blob` for
|
|
396
|
+
* binary content encoded as canonical standard base64. The framework validates
|
|
397
|
+
* the same XOR contract at runtime before writing the response, which keeps
|
|
398
|
+
* JavaScript consumers and unchecked handler results on the protocol-safe path.
|
|
399
|
+
*/
|
|
400
|
+
export type ResourceContent = TextResourceContent | BlobResourceContent;
|
|
401
|
+
|
|
344
402
|
/**
|
|
345
403
|
* Resource handler callback
|
|
346
404
|
*
|
|
347
405
|
* @param uri - The requested resource URI as URL object
|
|
348
|
-
* @returns ResourceContent
|
|
406
|
+
* @returns A text or base64-blob ResourceContent whose URI matches `uri`
|
|
349
407
|
*
|
|
350
408
|
* @example
|
|
351
409
|
* ```typescript
|
|
@@ -354,6 +412,9 @@ export interface ResourceContent {
|
|
|
354
412
|
* mimeType: MCP_APP_MIME_TYPE,
|
|
355
413
|
* text: "<html>...</html>"
|
|
356
414
|
* });
|
|
415
|
+
*
|
|
416
|
+
* // For binary content, return `blob` instead of `text`:
|
|
417
|
+
* // { uri: uri.toString(), mimeType: "image/png", blob: "iVBORw0KGgo..." }
|
|
357
418
|
* ```
|
|
358
419
|
*/
|
|
359
420
|
export type ResourceHandler = (
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A real stdio server, started the way a consumer starts one.
|
|
3
|
-
*
|
|
4
|
-
* Spawned as a subprocess by `stdio-e2e_test.ts`. Kept as a fixture rather than
|
|
5
|
-
* written to a temp file at run time so the thing under test is readable and
|
|
6
|
-
* versioned: this is exactly the five lines from the README.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { McpApp } from "../mcp-app.js";
|
|
10
|
-
|
|
11
|
-
const app = new McpApp({
|
|
12
|
-
name: "e2e-stdio",
|
|
13
|
-
version: "9.9.9",
|
|
14
|
-
logger: () => {},
|
|
15
|
-
instructions: "fixture server",
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
app.registerTool(
|
|
19
|
-
{
|
|
20
|
-
name: "echo",
|
|
21
|
-
description: "Echo the input",
|
|
22
|
-
inputSchema: {
|
|
23
|
-
type: "object",
|
|
24
|
-
properties: { value: { type: "string" } },
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
(args) => args.value ?? "empty",
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
await app.start();
|