@ultimat3/mcp 29.4.0 → 30.1.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
package/src/server.ts
CHANGED
|
@@ -9,13 +9,21 @@ import { outcomeForResult } from './audit';
|
|
|
9
9
|
import type { McpAuditHook, McpAuditor } from './audit-hook';
|
|
10
10
|
import { LOG_ONLY_AUDITOR, mcpAuditor } from './audit-hook';
|
|
11
11
|
import { McpProtocolError, McpScopeDeniedError, TOOL_UNKNOWN_FIX } from './errors';
|
|
12
|
-
import { asFrameworkError, forAudience } from './framework-error';
|
|
13
12
|
import { metaCall } from './meta-call';
|
|
14
13
|
import { META_UNKNOWN_FIX } from './meta-errors';
|
|
15
|
-
import { SPLIT_DISPATCHERS } from './meta-split';
|
|
16
14
|
import type { McpResourceGroups, McpSurfaceOption, MetaResource } from './meta-surface';
|
|
17
|
-
import {
|
|
15
|
+
import { MetaSurface } from './meta-surface';
|
|
18
16
|
import { promptListEntry, promptsGet, visiblePrompts } from './prompts-get';
|
|
17
|
+
import type { McpEra } from './protocol-era';
|
|
18
|
+
import {
|
|
19
|
+
classifyBody,
|
|
20
|
+
discoverResult,
|
|
21
|
+
eraOf,
|
|
22
|
+
initializeResult,
|
|
23
|
+
METHODS,
|
|
24
|
+
stamped,
|
|
25
|
+
unsupportedVersion,
|
|
26
|
+
} from './protocol-era';
|
|
19
27
|
import type {
|
|
20
28
|
AnyMcpTool,
|
|
21
29
|
McpCaller,
|
|
@@ -25,21 +33,24 @@ import type {
|
|
|
25
33
|
ToolResolution,
|
|
26
34
|
} from './registry';
|
|
27
35
|
import { ToolRegistry } from './registry';
|
|
28
|
-
import type { McpPrompt, McpResource } from './resources';
|
|
36
|
+
import type { McpPrompt, McpResource, McpResourceSource } from './resources';
|
|
29
37
|
import { ResourceRegistry } from './resources';
|
|
38
|
+
import type { CatalogRefused, CatalogView, McpCatalogSource, PerCall } from './server-catalog';
|
|
39
|
+
import { catalogRefusal, viewOf } from './server-catalog';
|
|
40
|
+
import { resourcesRead } from './server-resources';
|
|
30
41
|
import type { McpInstructions, McpServerVoice } from './server-voice';
|
|
31
42
|
import { instructionsFor, invalidArgsResult } from './server-voice';
|
|
43
|
+
import type { McpTenant } from './tenant';
|
|
44
|
+
import { assertTenant, LIST_TENANTS, listTenantsTool, tenantStep, withTenantArg } from './tenant';
|
|
32
45
|
import { admitted, thrownResponse } from './tool-thrown';
|
|
33
46
|
import type { JsonRpcId, JsonRpcRequest, JsonRpcResponse, McpWire, ServerInfo } from './wire';
|
|
34
47
|
import {
|
|
35
48
|
defaultServerInfo,
|
|
36
49
|
errorResponse,
|
|
37
|
-
INTERNAL_ERROR,
|
|
38
50
|
INVALID_PARAMS,
|
|
39
51
|
INVALID_REQUEST,
|
|
40
52
|
isJsonRpcRequest,
|
|
41
53
|
isNotification,
|
|
42
|
-
MCP_PROTOCOL_VERSION,
|
|
43
54
|
METHOD_NOT_FOUND,
|
|
44
55
|
messageUnitOf,
|
|
45
56
|
paramsOf,
|
|
@@ -51,6 +62,8 @@ export type { McpWire } from './wire';
|
|
|
51
62
|
export interface McpServerInput {
|
|
52
63
|
readonly tools?: readonly AnyMcpTool[];
|
|
53
64
|
readonly resources?: readonly McpResource[];
|
|
65
|
+
/** Resources listed per caller (a tenant's own docs), after the fixed ones. */
|
|
66
|
+
readonly resourceSources?: readonly McpResourceSource[];
|
|
54
67
|
readonly prompts?: readonly McpPrompt[];
|
|
55
68
|
readonly serverInfo?: ServerInfo;
|
|
56
69
|
/** `'flat'` (the default): one tool per primitive. See `McpSurfaceOption`. */
|
|
@@ -75,27 +88,27 @@ export interface McpServerInput {
|
|
|
75
88
|
readonly onAudit?: McpAuditHook | undefined;
|
|
76
89
|
/** Stamps each `onAudit` event's `at`. */
|
|
77
90
|
readonly clock?: Clock | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* The per-caller catalog, resolved per request for `tools/list` and every `tools/call` and merged
|
|
93
|
+
* with `tools`/`groups` (`server-catalog.ts`). `defineAppMcp({ catalog })` builds it.
|
|
94
|
+
*/
|
|
95
|
+
readonly catalog?: McpCatalogSource | undefined;
|
|
96
|
+
/** One token across tenants, the tenant named per call — see `tenant.ts`. */
|
|
97
|
+
readonly tenant?: McpTenant | undefined;
|
|
78
98
|
}
|
|
79
99
|
|
|
80
|
-
/** The set of JSON-RPC methods this server answers. Kept in sync with `classify`. */
|
|
81
|
-
const METHODS = [
|
|
82
|
-
'initialize',
|
|
83
|
-
'ping',
|
|
84
|
-
'tools/list',
|
|
85
|
-
'tools/call',
|
|
86
|
-
'resources/list',
|
|
87
|
-
'resources/read',
|
|
88
|
-
'prompts/list',
|
|
89
|
-
'prompts/get',
|
|
90
|
-
] as const;
|
|
91
|
-
|
|
92
100
|
export function mcpServer(input: McpServerInput = {}): McpServer {
|
|
93
|
-
const
|
|
101
|
+
const tenant = input.tenant;
|
|
102
|
+
if (tenant !== undefined) assertTenant(tenant, input.tools ?? []);
|
|
103
|
+
// `list_tenants` is a boot tool like any other, so every surface lists it and nothing shadows it.
|
|
104
|
+
const all = [...(input.tools ?? []), ...(tenant === undefined ? [] : [listTenantsTool(tenant)])];
|
|
105
|
+
const tools = new ToolRegistry().registerAll(all);
|
|
94
106
|
const resources = new ResourceRegistry().registerAll(input.resources ?? []);
|
|
107
|
+
for (const source of input.resourceSources ?? []) resources.addSource(source);
|
|
95
108
|
const meta = MetaSurface.build({
|
|
96
109
|
surface: input.surface,
|
|
97
110
|
groups: input.groups,
|
|
98
|
-
tools:
|
|
111
|
+
tools: all,
|
|
99
112
|
});
|
|
100
113
|
return new McpServer(
|
|
101
114
|
tools,
|
|
@@ -107,6 +120,13 @@ export function mcpServer(input: McpServerInput = {}): McpServer {
|
|
|
107
120
|
input.onAudit === undefined && input.clock === undefined
|
|
108
121
|
? LOG_ONLY_AUDITOR
|
|
109
122
|
: mcpAuditor({ onAudit: input.onAudit, clock: input.clock }),
|
|
123
|
+
{
|
|
124
|
+
catalog: input.catalog,
|
|
125
|
+
tenant,
|
|
126
|
+
tools: all,
|
|
127
|
+
surface: input.surface,
|
|
128
|
+
groups: input.groups,
|
|
129
|
+
},
|
|
110
130
|
);
|
|
111
131
|
}
|
|
112
132
|
|
|
@@ -120,6 +140,7 @@ export class McpServer {
|
|
|
120
140
|
private readonly voice: McpServerVoice;
|
|
121
141
|
/** Where every decision is audited — the log line, then `onAudit`. Public for the HTTP route. */
|
|
122
142
|
readonly audit: McpAuditor;
|
|
143
|
+
private readonly perCall: PerCall | undefined;
|
|
123
144
|
|
|
124
145
|
constructor(
|
|
125
146
|
tools: ToolRegistry,
|
|
@@ -129,7 +150,9 @@ export class McpServer {
|
|
|
129
150
|
meta?: MetaSurface,
|
|
130
151
|
voice: McpServerVoice = { errorAudience: 'developer' },
|
|
131
152
|
audit: McpAuditor = LOG_ONLY_AUDITOR,
|
|
153
|
+
perCall?: PerCall,
|
|
132
154
|
) {
|
|
155
|
+
this.perCall = perCall;
|
|
133
156
|
this.tools = tools;
|
|
134
157
|
this.resources = resources;
|
|
135
158
|
this.prompts = prompts;
|
|
@@ -148,7 +171,7 @@ export class McpServer {
|
|
|
148
171
|
// rate-limit class. Refused BY NAME rather than falling through to the envelope check below —
|
|
149
172
|
// an array is not an envelope, so it did, and a client sending a batch got the same bare
|
|
150
173
|
// `-32600` as `{ not: 'jsonrpc' }` with no word that batching was the problem. Measured
|
|
151
|
-
// through
|
|
174
|
+
// through one adopter's `POST /mcp` on 2026-09-07. The `message` is the same on every wire;
|
|
152
175
|
// only the fix names the unit — `POST <path>` as mounted, or a line — and only when the
|
|
153
176
|
// transport said which it is.
|
|
154
177
|
if (Array.isArray(body)) {
|
|
@@ -171,29 +194,58 @@ export class McpServer {
|
|
|
171
194
|
// Notifications get no answer at all; the transport replies 202 with an empty body.
|
|
172
195
|
if (isNotification(body)) return null;
|
|
173
196
|
const id = body.id ?? null;
|
|
197
|
+
// The era is per request (`protocol-era.ts`): a modern one is answered statelessly and stamped.
|
|
198
|
+
const era = eraOf(body);
|
|
199
|
+
if (typeof era !== 'string') return unsupportedVersion(id, era.unsupported);
|
|
200
|
+
const answer = METHODS[era].includes(body.method)
|
|
201
|
+
? await this.answer(id, body, caller, era)
|
|
202
|
+
: undefined;
|
|
203
|
+
if (answer === undefined) {
|
|
204
|
+
return errorResponse(id, METHOD_NOT_FOUND, `method not found: ${body.method}`, {
|
|
205
|
+
supported: METHODS[era],
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return era === 'modern' ? stamped(answer, body.method, this.serverInfo) : answer;
|
|
209
|
+
}
|
|
174
210
|
|
|
211
|
+
/** One method of `METHODS[era]`, answered. */
|
|
212
|
+
private async answer(
|
|
213
|
+
id: JsonRpcId,
|
|
214
|
+
body: JsonRpcRequest,
|
|
215
|
+
caller: McpCaller,
|
|
216
|
+
era: McpEra,
|
|
217
|
+
): Promise<JsonRpcResponse | undefined> {
|
|
175
218
|
switch (body.method) {
|
|
176
|
-
case 'initialize':
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
219
|
+
case 'initialize':
|
|
220
|
+
return resultResponse(
|
|
221
|
+
id,
|
|
222
|
+
initializeResult(
|
|
223
|
+
paramsOf(body),
|
|
224
|
+
this.serverInfo,
|
|
225
|
+
instructionsFor(this.voice.instructions, caller),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
case 'server/discover':
|
|
229
|
+
return resultResponse(id, discoverResult(instructionsFor(this.voice.instructions, caller)));
|
|
230
|
+
// Empty on both eras until an adopter declares a template; `resources` is advertised.
|
|
231
|
+
case 'resources/templates/list':
|
|
232
|
+
return resultResponse(id, { resourceTemplates: [] });
|
|
233
|
+
case 'tools/list': {
|
|
234
|
+
const view = await this.viewFor(caller);
|
|
235
|
+
if ('error' in view) return catalogRefusal(id, view.error, this.voice.errorAudience);
|
|
236
|
+
return resultResponse(id, { tools: this.listIn(view, caller) });
|
|
188
237
|
}
|
|
189
|
-
case 'tools/list':
|
|
190
|
-
return resultResponse(id, { tools: this.list(caller) });
|
|
191
238
|
case 'tools/call':
|
|
192
239
|
return this.toolsCall(body, caller);
|
|
193
240
|
case 'resources/list':
|
|
194
|
-
return resultResponse(id, { resources: this.resources.
|
|
241
|
+
return resultResponse(id, { resources: await this.resources.listFor(caller) });
|
|
195
242
|
case 'resources/read':
|
|
196
|
-
return
|
|
243
|
+
return resourcesRead(
|
|
244
|
+
{ resources: this.resources, audit: this.audit, audience: this.voice.errorAudience },
|
|
245
|
+
body,
|
|
246
|
+
caller,
|
|
247
|
+
era,
|
|
248
|
+
);
|
|
197
249
|
// MCP's keep-alive: an empty result, which clients treat as "still here".
|
|
198
250
|
case 'ping':
|
|
199
251
|
return resultResponse(id, {});
|
|
@@ -204,26 +256,41 @@ export class McpServer {
|
|
|
204
256
|
case 'prompts/get':
|
|
205
257
|
return promptsGet(this.prompts, id, paramsOf(body), caller);
|
|
206
258
|
default:
|
|
207
|
-
return
|
|
208
|
-
supported: METHODS,
|
|
209
|
-
});
|
|
259
|
+
return undefined;
|
|
210
260
|
}
|
|
211
261
|
}
|
|
212
262
|
|
|
213
|
-
/**
|
|
263
|
+
/**
|
|
264
|
+
* Role-filtered BOOT catalog, synchronously — a capability probe, the dev server. A server with a
|
|
265
|
+
* `catalog` answers `tools/list` per request on top of it (`viewFor`).
|
|
266
|
+
*/
|
|
214
267
|
list(caller: McpCaller): readonly ToolListEntry[] {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
268
|
+
return this.listIn({ tools: this.tools, meta: this.meta }, caller);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private listIn(view: CatalogView, caller: McpCaller): readonly ToolListEntry[] {
|
|
272
|
+
const flat = view.tools.list(caller);
|
|
273
|
+
const meta = metaServed(view.meta, caller);
|
|
274
|
+
const arg = this.perCall?.tenant?.arg;
|
|
275
|
+
const withArg = (entries: readonly ToolListEntry[]) =>
|
|
276
|
+
arg === undefined ? entries : withTenantArg(entries, arg);
|
|
277
|
+
if (meta === undefined) return withArg(flat);
|
|
218
278
|
// Constant: the meta tools plus whatever the app left ungrouped (`docs`, `whoami`).
|
|
219
279
|
const entries = MetaSurface.entriesFor(
|
|
220
280
|
meta.surfaceFor(caller) === 'meta' ? 'meta' : 'meta-split',
|
|
221
281
|
);
|
|
222
|
-
return
|
|
223
|
-
|
|
282
|
+
return withArg(
|
|
283
|
+
[...flat.filter((tool) => !meta.isGrouped(tool.name)), ...entries].sort((a, b) =>
|
|
284
|
+
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
|
|
285
|
+
),
|
|
224
286
|
);
|
|
225
287
|
}
|
|
226
288
|
|
|
289
|
+
/** The boot view, or this caller's resolved catalog merged over it — one per request. */
|
|
290
|
+
private viewFor(caller: McpCaller): Promise<CatalogView | CatalogRefused> {
|
|
291
|
+
return viewOf(this.perCall, { tools: this.tools, meta: this.meta }, caller);
|
|
292
|
+
}
|
|
293
|
+
|
|
227
294
|
/**
|
|
228
295
|
* The meta catalog THIS caller would read from `list_resources`, as data — `undefined` for a caller
|
|
229
296
|
* served the flat surface. For a test or a tool asserting on the catalog: the wire form is text.
|
|
@@ -234,34 +301,12 @@ export class McpServer {
|
|
|
234
301
|
|
|
235
302
|
/** The meta surface when THIS caller is served it, else `undefined`. */
|
|
236
303
|
private metaFor(caller: McpCaller): MetaSurface | undefined {
|
|
237
|
-
|
|
238
|
-
return meta !== undefined && meta.surfaceFor(caller) !== 'flat' ? meta : undefined;
|
|
304
|
+
return metaServed(this.meta, caller);
|
|
239
305
|
}
|
|
240
306
|
|
|
241
|
-
/**
|
|
242
|
-
* Rate-limit class of a body WITHOUT executing it, and WITHOUT a caller — bucket
|
|
243
|
-
* selection is metering, not authorization. All MCP traffic is one `POST /mcp`, so a
|
|
244
|
-
* coarse per-route rule would charge `initialize` and every read to the write bucket and
|
|
245
|
-
* throttle an agent on its handshake.
|
|
246
|
-
*
|
|
247
|
-
* KEEP IN SYNC with `handle`: only `tools/call` can reach a tool, so every other method
|
|
248
|
-
* is protocol chatter that structurally cannot mutate.
|
|
249
|
-
*/
|
|
307
|
+
/** Rate-limit class of a body without executing it — see `classifyBody`. */
|
|
250
308
|
classify(body: unknown): McpVerbClass {
|
|
251
|
-
|
|
252
|
-
const params = paramsOf(body);
|
|
253
|
-
const name = params?.['name'];
|
|
254
|
-
// An unresolvable call is refused before it runs, so charging it the strict bucket
|
|
255
|
-
// only costs a broken client — it never hands an unproven verb the cheap one.
|
|
256
|
-
if (typeof name !== 'string') return 'write';
|
|
257
|
-
if (this.meta !== undefined && (name === MANAGE_RESOURCE || SPLIT_DISPATCHERS.includes(name))) {
|
|
258
|
-
// Billed as the tool it reaches, so the dispatcher is never a cheap door to a write.
|
|
259
|
-
const args = params?.['arguments'];
|
|
260
|
-
const action = isRecord(args) ? args['action'] : undefined;
|
|
261
|
-
return typeof action === 'string' ? this.tools.verbClass(action) : 'write';
|
|
262
|
-
}
|
|
263
|
-
if (this.meta !== undefined && META_TOOL_NAMES.includes(name)) return 'read';
|
|
264
|
-
return this.tools.verbClass(name);
|
|
309
|
+
return classifyBody(body, this.meta, this.tools);
|
|
265
310
|
}
|
|
266
311
|
|
|
267
312
|
private async toolsCall(req: JsonRpcRequest, caller: McpCaller): Promise<JsonRpcResponse> {
|
|
@@ -273,11 +318,36 @@ export class McpServer {
|
|
|
273
318
|
return errorResponse(id, INVALID_PARAMS, 'tools/call params.name must be a string');
|
|
274
319
|
}
|
|
275
320
|
|
|
276
|
-
|
|
321
|
+
// The tenant first: every gate after it, and the policy, sees the tenant's actor. Not a member
|
|
322
|
+
// answers exactly as a hidden tool — `notFound`, the same call the dispatch below makes.
|
|
323
|
+
let who = caller;
|
|
324
|
+
let rawArgs: unknown = params['arguments'];
|
|
325
|
+
const tenant = this.perCall?.tenant;
|
|
326
|
+
if (tenant !== undefined && name !== LIST_TENANTS) {
|
|
327
|
+
const step = await tenantStep(tenant, rawArgs, caller);
|
|
328
|
+
if (step.kind === 'not-member') {
|
|
329
|
+
return this.notFound(
|
|
330
|
+
id,
|
|
331
|
+
name,
|
|
332
|
+
caller,
|
|
333
|
+
this.metaFor(caller) ? META_UNKNOWN_FIX : TOOL_UNKNOWN_FIX,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
if (step.kind === 'refused') {
|
|
337
|
+
return thrownResponse(id, name, step.error, caller, this.voice.errorAudience, this.audit);
|
|
338
|
+
}
|
|
339
|
+
who = step.caller;
|
|
340
|
+
rawArgs = step.args;
|
|
341
|
+
}
|
|
342
|
+
const view = await this.viewFor(who);
|
|
343
|
+
if ('error' in view) {
|
|
344
|
+
return thrownResponse(id, name, view.error, who, this.voice.errorAudience, this.audit);
|
|
345
|
+
}
|
|
346
|
+
const meta = metaServed(view.meta, who);
|
|
277
347
|
if (meta !== undefined) {
|
|
278
348
|
const answered = await metaCall(
|
|
279
349
|
{
|
|
280
|
-
tools:
|
|
350
|
+
tools: view.tools,
|
|
281
351
|
audit: this.audit,
|
|
282
352
|
dispatch: (at, tool, resolved, who) => this.dispatch(at, tool, resolved, who),
|
|
283
353
|
notFound: (at, tool, who, fix) => this.notFound(at, tool, who, fix),
|
|
@@ -285,16 +355,11 @@ export class McpServer {
|
|
|
285
355
|
thrownResponse(at, tool, error, who, this.voice.errorAudience, this.audit),
|
|
286
356
|
},
|
|
287
357
|
meta,
|
|
288
|
-
{ id, name, rawArgs
|
|
358
|
+
{ id, name, rawArgs, caller: who },
|
|
289
359
|
);
|
|
290
360
|
if (answered !== undefined) return answered;
|
|
291
361
|
}
|
|
292
|
-
return this.dispatch(
|
|
293
|
-
id,
|
|
294
|
-
name,
|
|
295
|
-
this.tools.resolve(name, params['arguments'] ?? {}, caller),
|
|
296
|
-
caller,
|
|
297
|
-
);
|
|
362
|
+
return this.dispatch(id, name, view.tools.resolve(name, rawArgs ?? {}, who), who);
|
|
298
363
|
}
|
|
299
364
|
|
|
300
365
|
/** OUTCOME 1, the one answer for absent and hidden alike. See `dispatch`. */
|
|
@@ -388,92 +453,6 @@ export class McpServer {
|
|
|
388
453
|
if (result.isError === true) payload['isError'] = true;
|
|
389
454
|
return resultResponse(id, payload);
|
|
390
455
|
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* The same three-outcome shape `toolsCall` above applies, on the document surface. It took no
|
|
394
|
-
* caller at all until 2026-08: every accepted token could list every URI and read every one of
|
|
395
|
-
* them — the manifest, the OpenAPI document, the route table and the entity schema.
|
|
396
|
-
*
|
|
397
|
-
* And every outcome is AUDITED, hidden included, exactly as `toolsCall`'s are: this method
|
|
398
|
-
* emitted nothing at all, so a URI walk over those four documents was invisible while the same
|
|
399
|
-
* walk over tool names was one `warn` per attempt. `resources/list` stays silent, as
|
|
400
|
-
* `tools/list` does — it is answered pre-filtered.
|
|
401
|
-
*/
|
|
402
|
-
private async resourcesRead(req: JsonRpcRequest, caller: McpCaller): Promise<JsonRpcResponse> {
|
|
403
|
-
const id = req.id ?? null;
|
|
404
|
-
const uri = paramsOf(req)?.['uri'];
|
|
405
|
-
if (typeof uri !== 'string') {
|
|
406
|
-
return errorResponse(id, INVALID_PARAMS, 'resources/read params.uri must be a string');
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const resolved = this.resources.resolve(uri, caller);
|
|
410
|
-
switch (resolved.kind) {
|
|
411
|
-
// OUTCOME 1. Absent AND hidden collapse to one answer with no `data`: this branch used to
|
|
412
|
-
// return `available: [...every uri]`, so one wrong guess enumerated the whole catalog. No
|
|
413
|
-
// `code` on the audit line either, because the wire carries none — the tool surface's
|
|
414
|
-
// `X_MCP_TOOL_UNKNOWN` is an error class this branch has no twin for.
|
|
415
|
-
case 'not-found':
|
|
416
|
-
this.audit.resourceRead({ uri, outcome: 'hidden', caller });
|
|
417
|
-
return errorResponse(id, METHOD_NOT_FOUND, `resource not found: ${uri}`);
|
|
418
|
-
// OUTCOME 2. The caller can already see this resource, so naming the missing scope leaks
|
|
419
|
-
// nothing — and the fix travels with it, built by the error that owns the wording.
|
|
420
|
-
case 'scope-denied': {
|
|
421
|
-
const denial = new McpScopeDeniedError({
|
|
422
|
-
name: uri,
|
|
423
|
-
scope: resolved.scope,
|
|
424
|
-
subject: 'resource',
|
|
425
|
-
});
|
|
426
|
-
this.audit.resourceRead({
|
|
427
|
-
uri,
|
|
428
|
-
outcome: 'scope-denied',
|
|
429
|
-
caller,
|
|
430
|
-
scope: resolved.scope,
|
|
431
|
-
code: denial.code,
|
|
432
|
-
});
|
|
433
|
-
return errorResponse(id, INVALID_REQUEST, `missing scope: ${resolved.scope}`, {
|
|
434
|
-
code: denial.code,
|
|
435
|
-
scope: resolved.scope,
|
|
436
|
-
fix: fixFor(denial, this.voice.errorAudience),
|
|
437
|
-
docs: denial.docs,
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
case 'ok':
|
|
441
|
-
break;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// The provider is an INJECTED THUNK — `frameworkResources` wires these to file reads, and
|
|
445
|
-
// `Bun.file(...).text()` on a missing `x.manifest.json` throws ENOENT. Outside a try it escaped
|
|
446
|
-
// `handle()` entirely: `serveStdio` rejected with the raw error, zero frames written, the
|
|
447
|
-
// request unanswered and every later request on that buffer never processed. Same shape
|
|
448
|
-
// `toolsCall` uses above, for the same reason.
|
|
449
|
-
try {
|
|
450
|
-
const contents = await this.resources.read(uri);
|
|
451
|
-
if (contents === undefined) {
|
|
452
|
-
// The resolver said `ok` and the registry then had nothing: a bug here, not a walk, so it
|
|
453
|
-
// is `failed` in the log while the caller still gets the same not-found it would have.
|
|
454
|
-
this.audit.resourceRead({ uri, outcome: 'failed', caller });
|
|
455
|
-
return errorResponse(id, METHOD_NOT_FOUND, `resource not found: ${uri}`);
|
|
456
|
-
}
|
|
457
|
-
this.audit.resourceRead({ uri, outcome: 'ok', caller });
|
|
458
|
-
return resultResponse(id, { contents: [contents] });
|
|
459
|
-
} catch (error) {
|
|
460
|
-
const thrown = asFrameworkError(error);
|
|
461
|
-
if (thrown !== undefined) {
|
|
462
|
-
const framework = forAudience(thrown, this.voice.errorAudience);
|
|
463
|
-
this.audit.resourceRead({ uri, outcome: 'failed', caller, code: framework.code });
|
|
464
|
-
return errorResponse(id, INTERNAL_ERROR, `resource "${uri}" could not be read`, {
|
|
465
|
-
code: framework.code,
|
|
466
|
-
cause: framework.cause,
|
|
467
|
-
fix: fixFor(framework, this.voice.errorAudience),
|
|
468
|
-
...(framework.docs === undefined ? {} : { docs: framework.docs }),
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
// No internals: a provider's own message names a path, a query or a host the caller has no
|
|
472
|
-
// business seeing, exactly as a failing tool's does.
|
|
473
|
-
this.audit.resourceRead({ uri, outcome: 'failed', caller });
|
|
474
|
-
return errorResponse(id, INTERNAL_ERROR, `resource "${uri}" could not be read`);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
456
|
}
|
|
478
457
|
|
|
479
458
|
/**
|
|
@@ -490,5 +469,7 @@ function protocolRefusal(message: string, error: McpProtocolError): JsonRpcRespo
|
|
|
490
469
|
});
|
|
491
470
|
}
|
|
492
471
|
|
|
493
|
-
|
|
494
|
-
|
|
472
|
+
/** `meta` when this caller is served it, else `undefined` — one rule for the boot and a resolved view. */
|
|
473
|
+
function metaServed(meta: MetaSurface | undefined, caller: McpCaller): MetaSurface | undefined {
|
|
474
|
+
return meta !== undefined && meta.surfaceFor(caller) !== 'flat' ? meta : undefined;
|
|
475
|
+
}
|
package/src/tenant.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// `defineAppMcp({ tenant })` — one token across tenants, the tenant named per call.
|
|
2
|
+
//
|
|
3
|
+
// The token resolves to a PERSON (`resolveToken`, unchanged); every `tools/call` names the tenant it
|
|
4
|
+
// acts in under `tenant.arg`. The arg is stripped before anything reads the arguments, membership is
|
|
5
|
+
// the app's `resolve`, and the actor it answers IS the caller from there on — visibility, scope,
|
|
6
|
+
// args, admit, confirmation, policy, `ctx.actor.orgId` — so the tenancy guards see one org per call.
|
|
7
|
+
// A tenant the person is not in answers exactly as a hidden tool; a lookup that throws never does.
|
|
8
|
+
|
|
9
|
+
import type { Actor } from '@ultimat3/core';
|
|
10
|
+
import { logger, renderCauseValue, renderThrowable } from '@ultimat3/core';
|
|
11
|
+
import {
|
|
12
|
+
McpTenantInvalidError,
|
|
13
|
+
McpTenantRequiredError,
|
|
14
|
+
McpTenantUnresolvedError,
|
|
15
|
+
} from './catalog-errors';
|
|
16
|
+
import { McpToolDuplicateError } from './errors';
|
|
17
|
+
import type { AnyMcpTool, McpCaller, ToolListEntry } from './registry';
|
|
18
|
+
import { jsonResult } from './registry';
|
|
19
|
+
import type { JsonSchema } from './wire';
|
|
20
|
+
import { NO_ARGS } from './wire';
|
|
21
|
+
|
|
22
|
+
/** One tenant the person may act in, as `list_tenants` shows it. */
|
|
23
|
+
export interface McpTenantEntry {
|
|
24
|
+
/** What a call sends as `tenant.arg`. */
|
|
25
|
+
readonly value: string;
|
|
26
|
+
/** A label for the model choosing between them. */
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface McpTenant {
|
|
31
|
+
/** The argument every `tools/call` names its tenant under — `'project'`, `'org'`. */
|
|
32
|
+
readonly arg: string;
|
|
33
|
+
/**
|
|
34
|
+
* The actor this person acts as in tenant `value`, or `null` when they are not a member — `null`
|
|
35
|
+
* answers as a hidden tool. Must answer a `kind: 'agent'` actor; a throw is
|
|
36
|
+
* `X_MCP_TENANT_UNRESOLVED`, never read as "not a member".
|
|
37
|
+
*/
|
|
38
|
+
resolve(input: {
|
|
39
|
+
readonly person: Actor;
|
|
40
|
+
readonly value: string;
|
|
41
|
+
}): Promise<Actor | null> | Actor | null;
|
|
42
|
+
/** The tenants this person may name, served by `list_tenants` — what `X_MCP_TENANT_REQUIRED` points at. */
|
|
43
|
+
list(input: {
|
|
44
|
+
readonly person: Actor;
|
|
45
|
+
}): Promise<readonly McpTenantEntry[]> | readonly McpTenantEntry[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The one tool a `tenant` server answers without a tenant: how a caller learns its tenants. */
|
|
49
|
+
export const LIST_TENANTS = 'list_tenants';
|
|
50
|
+
|
|
51
|
+
const ARG_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
52
|
+
|
|
53
|
+
/** A value longer than this names no tenant anyone issues; refused without asking `resolve`. */
|
|
54
|
+
const MAX_TENANT_VALUE = 256;
|
|
55
|
+
|
|
56
|
+
/** Boot: the arg is a plain name no tool's input declares, and `list_tenants` is not taken. */
|
|
57
|
+
export function assertTenant(tenant: McpTenant, tools: readonly AnyMcpTool[]): void {
|
|
58
|
+
const arg: unknown = tenant.arg;
|
|
59
|
+
if (typeof arg !== 'string' || !ARG_NAME.test(arg)) {
|
|
60
|
+
throw new McpTenantInvalidError({
|
|
61
|
+
cause: `tenant.arg is ${renderCauseValue(arg)}, and it has to be a plain argument name`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
for (const tool of tools) {
|
|
65
|
+
if (tool.name === LIST_TENANTS) throw new McpToolDuplicateError({ name: LIST_TENANTS });
|
|
66
|
+
assertArgFree(arg, tool);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The arg is stripped from every call, so a tool declaring it would never receive it. */
|
|
71
|
+
export function assertArgFree(arg: string, tool: AnyMcpTool): void {
|
|
72
|
+
if (Object.hasOwn(tool.inputSchema.properties ?? {}, arg)) {
|
|
73
|
+
throw new McpTenantInvalidError({
|
|
74
|
+
cause: `tool ${renderCauseValue(tool.name)} declares an input property "${arg}", which tenant.arg strips from every call`,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** `list_tenants`: the person's own tenants, nothing else — no scope, no tenant. */
|
|
80
|
+
export function listTenantsTool(tenant: McpTenant): AnyMcpTool {
|
|
81
|
+
return {
|
|
82
|
+
name: LIST_TENANTS,
|
|
83
|
+
title: 'List tenants',
|
|
84
|
+
description: `The tenants this caller may act in. Every other tool takes one of their values as "${tenant.arg}". Read-only.`,
|
|
85
|
+
inputSchema: NO_ARGS,
|
|
86
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
87
|
+
destructive: false,
|
|
88
|
+
async handle(_args, caller) {
|
|
89
|
+
const listed = await tenant.list({ person: caller.actor });
|
|
90
|
+
return jsonResult({
|
|
91
|
+
tenants: listed.map((entry) => ({
|
|
92
|
+
value: entry.value,
|
|
93
|
+
...(entry.name === undefined ? {} : { name: entry.name }),
|
|
94
|
+
})),
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `tools/list` with the tenant arg published on every tool but `list_tenants` — never in a tool's own schema. */
|
|
101
|
+
export function withTenantArg(
|
|
102
|
+
entries: readonly ToolListEntry[],
|
|
103
|
+
arg: string,
|
|
104
|
+
): readonly ToolListEntry[] {
|
|
105
|
+
return entries.map((entry) =>
|
|
106
|
+
entry.name === LIST_TENANTS
|
|
107
|
+
? entry
|
|
108
|
+
: { ...entry, inputSchema: published(entry.inputSchema, arg) },
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function published(schema: JsonSchema, arg: string): JsonSchema {
|
|
113
|
+
const properties: Record<string, JsonSchema> = { ...schema.properties };
|
|
114
|
+
Object.defineProperty(properties, arg, {
|
|
115
|
+
value: {
|
|
116
|
+
type: 'string',
|
|
117
|
+
description: `The tenant this call acts in: a value from ${LIST_TENANTS}.`,
|
|
118
|
+
},
|
|
119
|
+
enumerable: true,
|
|
120
|
+
writable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
});
|
|
123
|
+
return { ...schema, properties, required: [...(schema.required ?? []), arg] };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** One call's tenant step. `not-member` is answered exactly as a hidden tool. */
|
|
127
|
+
export type TenantStep =
|
|
128
|
+
| { readonly kind: 'ok'; readonly caller: McpCaller; readonly args: unknown }
|
|
129
|
+
| { readonly kind: 'refused'; readonly error: McpTenantRequiredError | McpTenantUnresolvedError }
|
|
130
|
+
| { readonly kind: 'not-member' };
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Strip the arg, ask membership, build the call's caller. Runs before visibility, so every gate after
|
|
134
|
+
* it — and the policy — sees the tenant's actor. A fresh caller per call: nothing is cached.
|
|
135
|
+
*/
|
|
136
|
+
export async function tenantStep(
|
|
137
|
+
tenant: McpTenant,
|
|
138
|
+
rawArgs: unknown,
|
|
139
|
+
caller: McpCaller,
|
|
140
|
+
): Promise<TenantStep> {
|
|
141
|
+
const args = isRecord(rawArgs) ? rawArgs : {};
|
|
142
|
+
const value = Object.hasOwn(args, tenant.arg) ? args[tenant.arg] : undefined;
|
|
143
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
144
|
+
return { kind: 'refused', error: new McpTenantRequiredError({ arg: tenant.arg }) };
|
|
145
|
+
}
|
|
146
|
+
if (value.length > MAX_TENANT_VALUE) return { kind: 'not-member' };
|
|
147
|
+
let actor: Actor | null;
|
|
148
|
+
try {
|
|
149
|
+
actor = await tenant.resolve({ person: caller.actor, value });
|
|
150
|
+
} catch (thrown) {
|
|
151
|
+
return unresolved(`tenant.resolve threw: ${renderThrowable(thrown)}`, thrown);
|
|
152
|
+
}
|
|
153
|
+
if (actor === null) return { kind: 'not-member' };
|
|
154
|
+
if ((actor as { kind?: unknown }).kind !== 'agent') {
|
|
155
|
+
return unresolved('tenant.resolve answered an actor that is not kind "agent"');
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
kind: 'ok',
|
|
159
|
+
caller: { ...caller, actor, tenant: value },
|
|
160
|
+
args: without(args, tenant.arg),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function unresolved(cause: string, thrown?: unknown): TenantStep {
|
|
165
|
+
const error = new McpTenantUnresolvedError({
|
|
166
|
+
cause,
|
|
167
|
+
...(thrown === undefined ? {} : { sourceError: thrown }),
|
|
168
|
+
});
|
|
169
|
+
logger.error('mcp.tenant.unresolved', { surface: 'mcp', code: error.code, cause: error.cause });
|
|
170
|
+
return { kind: 'refused', error };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A copy without `key`, every other key landed through `defineProperty` (`__proto__` included). */
|
|
174
|
+
function without(args: Readonly<Record<string, unknown>>, key: string): Record<string, unknown> {
|
|
175
|
+
const out: Record<string, unknown> = {};
|
|
176
|
+
for (const [name, value] of Object.entries(args)) {
|
|
177
|
+
if (name === key) continue;
|
|
178
|
+
Object.defineProperty(out, name, {
|
|
179
|
+
value,
|
|
180
|
+
enumerable: true,
|
|
181
|
+
writable: true,
|
|
182
|
+
configurable: true,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
189
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|