@ultimat3/mcp 29.4.0 → 30.0.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/CLAUDE.md +54 -59
- package/README.md +259 -2
- package/package.json +21 -10
- package/src/app-catalog.ts +84 -0
- package/src/app-tool.ts +6 -2
- package/src/app-tools.ts +80 -5
- package/src/audit-hook.ts +7 -1
- package/src/audit.ts +12 -1
- package/src/catalog-errors.ts +97 -0
- package/src/confirmation-approvers.ts +85 -0
- package/src/confirmation-decide.ts +78 -8
- package/src/confirmation-evidence.ts +118 -0
- package/src/confirmation-gate.ts +147 -44
- package/src/confirmation-postgres.ts +88 -7
- package/src/confirmation-quorum-errors.ts +102 -0
- package/src/confirmation-schema.ts +13 -2
- package/src/confirmation-store.ts +54 -19
- package/src/confirmations.ts +77 -8
- package/src/dev-graph-tool.ts +62 -0
- package/src/dev-server.ts +10 -2
- package/src/errors-transport.ts +84 -3
- package/src/errors.ts +46 -0
- package/src/exposed.ts +13 -3
- package/src/framework-error.ts +8 -1
- package/src/from-action.ts +2 -3
- package/src/index.ts +102 -1
- package/src/job-status-tool.ts +66 -0
- package/src/job-tool-errors.ts +15 -0
- package/src/job-tool.ts +167 -0
- package/src/oauth-metadata.ts +2 -2
- package/src/oauth-server.ts +80 -0
- package/src/projectable.ts +15 -10
- package/src/protocol-era.ts +270 -0
- package/src/recipe-errors.ts +75 -0
- package/src/recipe-links.ts +49 -0
- package/src/recipe-markdown.ts +143 -0
- package/src/recipe-replay.ts +101 -0
- package/src/recipe-search.ts +82 -0
- package/src/recipes-surface.ts +55 -0
- package/src/recipes-tool.ts +123 -0
- package/src/recipes.ts +167 -0
- package/src/registry.ts +5 -0
- package/src/remote-client.ts +378 -0
- package/src/remote-errors.ts +76 -0
- package/src/remote-listen.ts +108 -0
- package/src/remote-lock.ts +139 -0
- package/src/remote-oauth.ts +258 -0
- package/src/remote-schema.ts +51 -0
- package/src/remote-tools.ts +157 -0
- package/src/remote-wire.ts +121 -0
- package/src/resources.ts +84 -13
- package/src/runtime-schema.ts +132 -0
- package/src/runtime-tool.ts +115 -0
- package/src/scopes.ts +8 -1
- package/src/server-catalog.ts +200 -0
- package/src/server-resources.ts +117 -0
- package/src/server-voice.ts +10 -5
- package/src/server.ts +149 -168
- package/src/tenant.ts +189 -0
- package/src/transport-http-answers.ts +124 -0
- package/src/transport-http.ts +50 -94
- package/src/wire.ts +18 -4
- package/src/caller-context.ts +0 -22
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// What `POST /mcp` answers before it dispatches: the 401, the 403s, the 421, the 429 and the
|
|
2
|
+
// per-address failure meter that decides one of them. `{ code, cause, fix }` and a status, never a
|
|
3
|
+
// JSON-RPC envelope — nothing was called. Split from `transport-http.ts`, which keeps the order.
|
|
4
|
+
|
|
5
|
+
import type { Clock, UltimateError } from '@ultimat3/core';
|
|
6
|
+
import { proveSameOrigin } from '@ultimat3/core';
|
|
7
|
+
import type { Bucket, RateLimitStore } from '@ultimat3/http';
|
|
8
|
+
import {
|
|
9
|
+
McpHostRefusedError,
|
|
10
|
+
McpOriginRefusedError,
|
|
11
|
+
McpRateLimitedError,
|
|
12
|
+
} from './errors-transport';
|
|
13
|
+
import type { JsonRpcResponse } from './wire';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Every answer on this route is one caller's: a 401 challenge, a refusal, a JSON-RPC result for
|
|
17
|
+
* the token that asked. `no-store` on all of them (RFC 6750 §3 / RFC 6749 §5.1 for the auth
|
|
18
|
+
* answers), stated HERE because the descriptor is also served bare by `x mcp serve`, where no
|
|
19
|
+
* pipeline stage decides a cache header for it.
|
|
20
|
+
*/
|
|
21
|
+
export const NO_STORE_HEADERS = { 'cache-control': 'no-store' } as const;
|
|
22
|
+
const JSON_HEADERS = { 'content-type': 'application/json', ...NO_STORE_HEADERS } as const;
|
|
23
|
+
|
|
24
|
+
const refusal = (error: UltimateError, status: number, headers: Record<string, string> = {}) =>
|
|
25
|
+
new Response(JSON.stringify({ code: error.code, cause: error.cause, fix: error.fix }), {
|
|
26
|
+
status,
|
|
27
|
+
headers: { ...JSON_HEADERS, ...headers },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export function json(response: JsonRpcResponse, status: number): Response {
|
|
31
|
+
return new Response(JSON.stringify(response), { status, headers: JSON_HEADERS });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The refusal, rendered the way this file's 401 and 403 already are. `retry-after` carries the same
|
|
36
|
+
* number the `fix:` line names, because an agent reads one of the two and must not get different
|
|
37
|
+
* answers from them.
|
|
38
|
+
*/
|
|
39
|
+
export function throttled(verbClass: string, limit: number, retryAfterSeconds: number): Response {
|
|
40
|
+
const error = new McpRateLimitedError({ verbClass, limit, retryAfterSeconds });
|
|
41
|
+
return refusal(error, 429, { 'retry-after': String(retryAfterSeconds) });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The per-address failure allowance. `refusing` reads the bucket (`RateLimitStore.peek`, which
|
|
46
|
+
* writes nothing) and answers the 429 when not one more failure fits; `spend` charges one
|
|
47
|
+
* failure and answers the 429 when that one did not fit. `undefined` from either: carry on.
|
|
48
|
+
*/
|
|
49
|
+
export function failureMeter(store: RateLimitStore, bucket: Bucket, clock: Clock) {
|
|
50
|
+
const keyOf = (address: string): string => `mcp:unauthenticated|ip:${address}`;
|
|
51
|
+
const refused = (retryAfterSeconds: number): Response =>
|
|
52
|
+
throttled('unauthenticated', bucket.capacity, retryAfterSeconds);
|
|
53
|
+
return {
|
|
54
|
+
async refusing(address: string | undefined): Promise<Response | undefined> {
|
|
55
|
+
if (address === undefined) return undefined;
|
|
56
|
+
// READ ONLY: this runs on every request, an agent's included, and a write per call would be
|
|
57
|
+
// a Postgres upsert on the hot path of every authenticated one.
|
|
58
|
+
const peek = await store.peek(keyOf(address), bucket, clock.now().getTime());
|
|
59
|
+
return peek.remaining >= 1 ? undefined : refused(peek.retryAfterSeconds);
|
|
60
|
+
},
|
|
61
|
+
async spend(address: string | undefined): Promise<Response | undefined> {
|
|
62
|
+
if (address === undefined) return undefined;
|
|
63
|
+
const spent = await store.take(keyOf(address), bucket, 1, clock.now().getTime());
|
|
64
|
+
return spent.allowed ? undefined : refused(spent.retryAfterSeconds);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 403 for a present `Origin` that core's `proveSameOrigin` cannot show is this app's, else
|
|
71
|
+
* `undefined`. A missing `Origin` passes: every browser sends one on a POST, a client process none.
|
|
72
|
+
*/
|
|
73
|
+
export function originRefusal(input: {
|
|
74
|
+
readonly origin: string | null;
|
|
75
|
+
readonly secFetchSite: string | null;
|
|
76
|
+
readonly self: string;
|
|
77
|
+
readonly listed: (origin: string) => boolean;
|
|
78
|
+
readonly path: string;
|
|
79
|
+
}): Response | undefined {
|
|
80
|
+
if (input.origin === null) return undefined;
|
|
81
|
+
const verdict = proveSameOrigin({
|
|
82
|
+
selfOrigins: [input.self],
|
|
83
|
+
origin: input.origin,
|
|
84
|
+
secFetchSite: input.secFetchSite,
|
|
85
|
+
listed: input.listed,
|
|
86
|
+
listName: 'http.cors.origins',
|
|
87
|
+
});
|
|
88
|
+
if (verdict.ok) return undefined;
|
|
89
|
+
const error = new McpOriginRefusedError({
|
|
90
|
+
reason: verdict.reason,
|
|
91
|
+
endpoint: `${input.self}${input.path}`,
|
|
92
|
+
});
|
|
93
|
+
return refusal(error, 403);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `421 Misdirected Request` for a Host a loopback-only server does not serve — "not this server",
|
|
98
|
+
* the truth for a rebound name. The host decides what is loopback; `endpoint` is where it IS.
|
|
99
|
+
*/
|
|
100
|
+
export function mcpHostRefused(endpoint: string): Response {
|
|
101
|
+
return refusal(new McpHostRefusedError({ endpoint }), 421);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function notAnAgent(): Response {
|
|
105
|
+
return new Response(
|
|
106
|
+
JSON.stringify({
|
|
107
|
+
code: 'X_MCP_PROTOCOL',
|
|
108
|
+
cause: 'resolveToken returned an actor whose kind is not "agent"',
|
|
109
|
+
fix: 'return { kind: "agent", ... } from resolveToken; MCP callers are agents, not users',
|
|
110
|
+
}),
|
|
111
|
+
{ status: 403, headers: JSON_HEADERS },
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function unauthorized(challenge: string): Response {
|
|
116
|
+
return new Response(
|
|
117
|
+
JSON.stringify({
|
|
118
|
+
code: 'X_MCP_PROTOCOL',
|
|
119
|
+
cause: 'missing or unrecognised bearer token',
|
|
120
|
+
fix: "send Authorization: Bearer <token> with a token the app's resolveToken(token) resolves to { actor, scopes } — it is the only issuer this route has",
|
|
121
|
+
}),
|
|
122
|
+
{ status: 401, headers: { ...JSON_HEADERS, 'www-authenticate': challenge } },
|
|
123
|
+
);
|
|
124
|
+
}
|
package/src/transport-http.ts
CHANGED
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
|
|
22
22
|
import type { Actor, Clock } from '@ultimat3/core';
|
|
23
23
|
import { finiteCount, readWithinLimit, systemClock } from '@ultimat3/core';
|
|
24
|
-
import type {
|
|
24
|
+
import type { RequestFacts as McpRequestFacts, RateLimitStore } from '@ultimat3/http';
|
|
25
25
|
import { memoryRateLimitStore, requestFacts, toBucket } from '@ultimat3/http';
|
|
26
|
-
import { McpBodyTooLargeError
|
|
26
|
+
import { McpBodyTooLargeError } from './errors-transport';
|
|
27
27
|
import type { McpOAuth } from './oauth-metadata';
|
|
28
28
|
import {
|
|
29
29
|
assertMcpOAuth,
|
|
@@ -35,9 +35,18 @@ import {
|
|
|
35
35
|
resourceUrl,
|
|
36
36
|
supportedScopes,
|
|
37
37
|
} from './oauth-metadata';
|
|
38
|
+
import { headerRefusal, httpStatusOf } from './protocol-era';
|
|
38
39
|
import type { McpCaller, McpRole, McpVerbClass } from './registry';
|
|
39
40
|
import type { McpServer } from './server';
|
|
40
|
-
import
|
|
41
|
+
import {
|
|
42
|
+
failureMeter,
|
|
43
|
+
json,
|
|
44
|
+
NO_STORE_HEADERS,
|
|
45
|
+
notAnAgent,
|
|
46
|
+
originRefusal,
|
|
47
|
+
throttled,
|
|
48
|
+
unauthorized,
|
|
49
|
+
} from './transport-http-answers';
|
|
41
50
|
import { errorResponse, INVALID_REQUEST, PARSE_ERROR, refusalMessage } from './wire';
|
|
42
51
|
|
|
43
52
|
/**
|
|
@@ -119,8 +128,18 @@ export interface McpHttpTransportInput {
|
|
|
119
128
|
|
|
120
129
|
/** How the host saw the request — what a bare `Request` behind a TLS-terminating proxy cannot say. */
|
|
121
130
|
export interface McpRequestOrigin {
|
|
122
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* The PUBLIC origin, `https://www.example.com`: the URLs this route publishes, and the self origin
|
|
133
|
+
* a browser's `Origin` is proven against. Defaults to the request URL's own. Both are read off
|
|
134
|
+
* the Host, so a DNS-rebound name proves same-origin: a server that serves only this machine
|
|
135
|
+
* refuses a non-loopback Host before calling `handle` (`x mcp serve`, `mcpHostRefused`).
|
|
136
|
+
*/
|
|
123
137
|
readonly origin?: string | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* An EXACT allowance for a sibling origin — the app's `http.cors.origins`, through
|
|
140
|
+
* `@ultimat3/http`'s `originListed`. Absent: the request's own origin only.
|
|
141
|
+
*/
|
|
142
|
+
readonly listed?: ((origin: string) => boolean) | undefined;
|
|
124
143
|
/**
|
|
125
144
|
* The caller's address as the host resolved it (a trusted proxy's `x-forwarded-for` honoured) —
|
|
126
145
|
* what failed authentications are counted against. Absent, they are not counted here: one
|
|
@@ -149,15 +168,6 @@ export interface McpRouteDescriptor {
|
|
|
149
168
|
handle(request: Request, seen?: McpRequestOrigin): Promise<Response>;
|
|
150
169
|
}
|
|
151
170
|
|
|
152
|
-
/**
|
|
153
|
-
* Every answer on this route is one caller's: a 401 challenge, a refusal, a JSON-RPC result for
|
|
154
|
-
* the token that asked. `no-store` on all of them (RFC 6750 §3 / RFC 6749 §5.1 for the auth
|
|
155
|
-
* answers), stated HERE because the descriptor is also served bare by `x mcp serve`, where no
|
|
156
|
-
* pipeline stage decides a cache header for it.
|
|
157
|
-
*/
|
|
158
|
-
const NO_STORE_HEADERS = { 'cache-control': 'no-store' } as const;
|
|
159
|
-
const JSON_HEADERS = { 'content-type': 'application/json', ...NO_STORE_HEADERS } as const;
|
|
160
|
-
|
|
161
171
|
export function mcpHttpRoute(input: McpHttpTransportInput): McpRouteDescriptor {
|
|
162
172
|
const { server } = input;
|
|
163
173
|
// At CONSTRUCTION, beside the two buckets and for the same reason: `readWithinLimit` refuses a
|
|
@@ -228,6 +238,25 @@ export function mcpHttpRoute(input: McpHttpTransportInput): McpRouteDescriptor {
|
|
|
228
238
|
}),
|
|
229
239
|
|
|
230
240
|
async handle(request: Request, seen?: McpRequestOrigin): Promise<Response> {
|
|
241
|
+
const address = seen?.address;
|
|
242
|
+
// Built once per request: the resolver's second argument and every refusal's audit facts.
|
|
243
|
+
// The address inside is the host's answer (`seen.address`), never a header read here.
|
|
244
|
+
const facts = requestFacts({ headers: request.headers, url: request.url, address });
|
|
245
|
+
const audit = server.audit;
|
|
246
|
+
// A browser page that cannot be shown to be this app's is refused before the token is read,
|
|
247
|
+
// so a token in its hands decides nothing — core's `proveSameOrigin`, the rule CSRF, the sync
|
|
248
|
+
// upgrade and `/_x` answer to. A client process sends no `Origin` and is never asked.
|
|
249
|
+
const foreign = originRefusal({
|
|
250
|
+
origin: facts.origin,
|
|
251
|
+
secFetchSite: request.headers.get('sec-fetch-site'),
|
|
252
|
+
self: seen?.origin ?? new URL(request.url).origin,
|
|
253
|
+
listed: seen?.listed ?? (() => false),
|
|
254
|
+
path,
|
|
255
|
+
});
|
|
256
|
+
if (foreign !== undefined) {
|
|
257
|
+
audit.authRefused('origin-refused', foreign.status, facts);
|
|
258
|
+
return foreign;
|
|
259
|
+
}
|
|
231
260
|
// Every authentication answer lands BEFORE the body is read. Parsing first meant a caller
|
|
232
261
|
// holding a rejected token still learned whether its JSON was well formed — `400 parse
|
|
233
262
|
// error` for one payload and `401` for the next is exactly the oracle the 401 exists to
|
|
@@ -236,11 +265,6 @@ export function mcpHttpRoute(input: McpHttpTransportInput): McpRouteDescriptor {
|
|
|
236
265
|
const metadataUrl = resource === undefined ? undefined : metadataUrlFor(resource);
|
|
237
266
|
// An address that has spent its failures is refused BEFORE the credential store is asked:
|
|
238
267
|
// counting only after the fact turned a 401 into a 429 and still answered a valid guess.
|
|
239
|
-
const address = seen?.address;
|
|
240
|
-
// Built once per request: the resolver's second argument and every refusal's audit facts.
|
|
241
|
-
// The address inside is the host's answer (`seen.address`), never a header read here.
|
|
242
|
-
const facts = requestFacts({ headers: request.headers, url: request.url, address });
|
|
243
|
-
const audit = server.audit;
|
|
244
268
|
const exhausted = await failures.refusing(address);
|
|
245
269
|
if (exhausted !== undefined) {
|
|
246
270
|
audit.authRefused('throttled', exhausted.status, facts);
|
|
@@ -308,6 +332,11 @@ export function mcpHttpRoute(input: McpHttpTransportInput): McpRouteDescriptor {
|
|
|
308
332
|
return json(errorResponse(null, PARSE_ERROR, 'request body is not valid JSON'), 400);
|
|
309
333
|
}
|
|
310
334
|
|
|
335
|
+
// A versioned request mirrors its body into headers, and an intermediary may have routed on
|
|
336
|
+
// them: one that disagrees is refused before it is metered or run.
|
|
337
|
+
const refusedHeaders = headerRefusal(body, request.headers);
|
|
338
|
+
if (refusedHeaders !== undefined) return json(refusedHeaders, 400);
|
|
339
|
+
|
|
311
340
|
// Metered AFTER the class is known and BEFORE the tool runs. It cannot move above the
|
|
312
341
|
// parse — the class comes out of the body — and an unauthenticated caller never reaches it,
|
|
313
342
|
// so a token nobody issued cannot spend an actor's allowance.
|
|
@@ -340,35 +369,9 @@ export function mcpHttpRoute(input: McpHttpTransportInput): McpRouteDescriptor {
|
|
|
340
369
|
const response = await server.handle(body, caller, { transport: 'http', path });
|
|
341
370
|
// A notification has no response. 202 with an empty body is the MCP-correct answer.
|
|
342
371
|
if (response === null) return new Response(null, { status: 202, headers: NO_STORE_HEADERS });
|
|
343
|
-
// JSON-RPC errors are 200s: the transport succeeded, the call did not.
|
|
344
|
-
//
|
|
345
|
-
|
|
346
|
-
return json(response, status);
|
|
347
|
-
},
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* The per-address failure allowance. `refusing` reads the bucket (`RateLimitStore.peek`, which
|
|
353
|
-
* writes nothing) and answers the 429 when not one more failure fits; `spend` charges one
|
|
354
|
-
* failure and answers the 429 when that one did not fit. `undefined` from either: carry on.
|
|
355
|
-
*/
|
|
356
|
-
function failureMeter(store: RateLimitStore, bucket: Bucket, clock: Clock) {
|
|
357
|
-
const keyOf = (address: string): string => `mcp:unauthenticated|ip:${address}`;
|
|
358
|
-
const refusal = (retryAfterSeconds: number): Response =>
|
|
359
|
-
throttled('unauthenticated', bucket.capacity, retryAfterSeconds);
|
|
360
|
-
return {
|
|
361
|
-
async refusing(address: string | undefined): Promise<Response | undefined> {
|
|
362
|
-
if (address === undefined) return undefined;
|
|
363
|
-
// READ ONLY: this runs on every request, an agent's included, and a write per call would be
|
|
364
|
-
// a Postgres upsert on the hot path of every authenticated one.
|
|
365
|
-
const peek = await store.peek(keyOf(address), bucket, clock.now().getTime());
|
|
366
|
-
return peek.remaining >= 1 ? undefined : refusal(peek.retryAfterSeconds);
|
|
367
|
-
},
|
|
368
|
-
async spend(address: string | undefined): Promise<Response | undefined> {
|
|
369
|
-
if (address === undefined) return undefined;
|
|
370
|
-
const spent = await store.take(keyOf(address), bucket, 1, clock.now().getTime());
|
|
371
|
-
return spent.allowed ? undefined : refusal(spent.retryAfterSeconds);
|
|
372
|
+
// JSON-RPC errors are 200s: the transport succeeded, the call did not. A malformed envelope,
|
|
373
|
+
// an unserved version and a modern unknown method are the HTTP-level ones.
|
|
374
|
+
return json(response, httpStatusOf(body, response));
|
|
372
375
|
},
|
|
373
376
|
};
|
|
374
377
|
}
|
|
@@ -389,50 +392,3 @@ export function bearerToken(request: Request): string | null {
|
|
|
389
392
|
export function isAgentActor(actor: Actor): boolean {
|
|
390
393
|
return (actor as { kind?: unknown }).kind === 'agent';
|
|
391
394
|
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* The refusal, rendered the way this file's 401 and 403 already are: `{ code, cause, fix }` and an
|
|
395
|
-
* HTTP status, never a JSON-RPC envelope — the transport refused before dispatch, so there is no
|
|
396
|
-
* call to answer. `retry-after` carries the same number the `fix:` line names, because an agent
|
|
397
|
-
* reads one of the two and must not get different answers from them.
|
|
398
|
-
*/
|
|
399
|
-
function throttled(
|
|
400
|
-
verbClass: keyof McpRateLimits,
|
|
401
|
-
limit: number,
|
|
402
|
-
retryAfterSeconds: number,
|
|
403
|
-
): Response {
|
|
404
|
-
const error = new McpRateLimitedError({ verbClass, limit, retryAfterSeconds });
|
|
405
|
-
return new Response(JSON.stringify({ code: error.code, cause: error.cause, fix: error.fix }), {
|
|
406
|
-
status: 429,
|
|
407
|
-
headers: { ...JSON_HEADERS, 'retry-after': String(retryAfterSeconds) },
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function notAnAgent(): Response {
|
|
412
|
-
return new Response(
|
|
413
|
-
JSON.stringify({
|
|
414
|
-
code: 'X_MCP_PROTOCOL',
|
|
415
|
-
cause: 'resolveToken returned an actor whose kind is not "agent"',
|
|
416
|
-
fix: 'return { kind: "agent", ... } from resolveToken; MCP callers are agents, not users',
|
|
417
|
-
}),
|
|
418
|
-
{ status: 403, headers: JSON_HEADERS },
|
|
419
|
-
);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
function unauthorized(challenge: string): Response {
|
|
423
|
-
return new Response(
|
|
424
|
-
JSON.stringify({
|
|
425
|
-
code: 'X_MCP_PROTOCOL',
|
|
426
|
-
cause: 'missing or unrecognised bearer token',
|
|
427
|
-
fix: "send Authorization: Bearer <token> with a token the app's resolveToken(token) resolves to { actor, scopes } — it is the only issuer this route has",
|
|
428
|
-
}),
|
|
429
|
-
{
|
|
430
|
-
status: 401,
|
|
431
|
-
headers: { ...JSON_HEADERS, 'www-authenticate': challenge },
|
|
432
|
-
},
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
function json(response: JsonRpcResponse, status: number): Response {
|
|
437
|
-
return new Response(JSON.stringify(response), { status, headers: JSON_HEADERS });
|
|
438
|
-
}
|
package/src/wire.ts
CHANGED
|
@@ -3,13 +3,23 @@
|
|
|
3
3
|
// Pure data + pure functions so every transport (http, stdio, a test) agrees on the wire
|
|
4
4
|
// without importing the server.
|
|
5
5
|
//
|
|
6
|
-
// Reference: https://modelcontextprotocol.io/specification (
|
|
6
|
+
// Reference: https://modelcontextprotocol.io/specification (2026-07-28, read 2026-11-02).
|
|
7
7
|
|
|
8
8
|
import { frameworkVersion } from '@ultimat3/core';
|
|
9
9
|
import type { WireJsonSchema } from '@ultimat3/schema';
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The modern revisions served: version, identity and capabilities ride in each request's `_meta`,
|
|
13
|
+
* no `initialize`. Newest first.
|
|
14
|
+
*/
|
|
15
|
+
export const MCP_MODERN_VERSIONS = ['2026-07-28'] as const;
|
|
16
|
+
|
|
17
|
+
/** The handshake revisions `initialize` negotiates — echoed when offered, else the first. */
|
|
18
|
+
export const MCP_LEGACY_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26'] as const;
|
|
19
|
+
|
|
20
|
+
/** The `_meta` keys a modern request and result carry. */
|
|
21
|
+
export const META_PROTOCOL_VERSION = 'io.modelcontextprotocol/protocolVersion';
|
|
22
|
+
export const META_SERVER_INFO = 'io.modelcontextprotocol/serverInfo';
|
|
13
23
|
|
|
14
24
|
/**
|
|
15
25
|
* Identity advertised on `initialize` unless the host overrides it. A call and not a constant:
|
|
@@ -50,7 +60,7 @@ export interface JsonRpcResponse {
|
|
|
50
60
|
|
|
51
61
|
// ── standard error codes (jsonrpc.org/spec) ──────────────────────────────────
|
|
52
62
|
//
|
|
53
|
-
// The
|
|
63
|
+
// The two security answers map onto these:
|
|
54
64
|
// INVALID_REQUEST (-32600) → Forbidden: the tool exists and you may see it, but your
|
|
55
65
|
// token lacks its scope.
|
|
56
66
|
// METHOD_NOT_FOUND (-32601) → ToolNotFound: the tool is absent OR hidden from your role.
|
|
@@ -62,6 +72,10 @@ export const METHOD_NOT_FOUND = -32601;
|
|
|
62
72
|
export const INVALID_PARAMS = -32602;
|
|
63
73
|
export const INTERNAL_ERROR = -32603;
|
|
64
74
|
|
|
75
|
+
// ── MCP's own (2026-07-28), from the sub-range the spec reserves ─────────────
|
|
76
|
+
export const HEADER_MISMATCH = -32020;
|
|
77
|
+
export const UNSUPPORTED_PROTOCOL_VERSION = -32022;
|
|
78
|
+
|
|
65
79
|
/**
|
|
66
80
|
* The JSON Schema subset a tool publishes and `validate-args.ts` enforces — `@ultimat3/schema`'s
|
|
67
81
|
* `WireJsonSchema`, declared there so `@ultimat3/action`'s `.tool()` speaks the same one.
|
package/src/caller-context.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// The one place this package installs an ambient identity for a tool call.
|
|
2
|
-
//
|
|
3
|
-
// Its own file because both callers — a hand-written app tool and a projected action — must answer
|
|
4
|
-
// the same way on both transports, and two call sites each choosing `withChildContext` is how they
|
|
5
|
-
// came to answer differently by transport.
|
|
6
|
-
|
|
7
|
-
import type { Actor } from '@ultimat3/core';
|
|
8
|
-
import { ctxOf, hasContext, runWithContext, withChildContext } from '@ultimat3/core';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Run `fn` with `actor` as the ambient identity, whether or not a request is already in flight.
|
|
12
|
-
*
|
|
13
|
-
* `withChildContext` alone is wrong here, and was: it calls `useContext()`, which throws
|
|
14
|
-
* `X_NO_CONTEXT` when nothing is in flight — and NOTHING in this package installs a root context.
|
|
15
|
-
* Over `x mcp serve` (stdio) there is no surrounding request, so every app tool call and every
|
|
16
|
-
* projected action answered `X_NO_CONTEXT` instead of running. Over HTTP the transport's request
|
|
17
|
-
* supplies the parent, and the child is what keeps the policy subject and whatever the handler
|
|
18
|
-
* reads off `ctx.actor` the same identity by construction rather than by two call sites agreeing.
|
|
19
|
-
*/
|
|
20
|
-
export function asCallerContext<T>(actor: Actor, fn: () => T): T {
|
|
21
|
-
return hasContext() ? withChildContext({ actor }, fn) : runWithContext(ctxOf({ actor }), fn);
|
|
22
|
-
}
|