@ultimat3/mcp 31.49.0 → 31.51.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 +5 -5
- package/README.md +132 -14
- package/package.json +11 -11
- package/src/app-tool.ts +10 -3
- package/src/app-tools.ts +29 -7
- package/src/audit.ts +2 -0
- package/src/catalog-errors.ts +11 -5
- package/src/errors.ts +6 -1
- package/src/from-action.ts +33 -3
- package/src/index.ts +11 -1
- package/src/meta-call.ts +2 -2
- package/src/meta-errors.ts +27 -3
- package/src/meta-split-call.ts +45 -6
- package/src/meta-split-options.ts +180 -0
- package/src/meta-split.ts +3 -2
- package/src/meta-surface.ts +17 -1
- package/src/protocol-era.ts +2 -1
- package/src/recipe-errors.ts +19 -7
- package/src/recipe-markdown.ts +64 -12
- package/src/recipe-replay.ts +12 -5
- package/src/recipe-search.ts +14 -7
- package/src/recipes-surface.ts +19 -6
- package/src/recipes-tool.ts +67 -14
- package/src/recipes.ts +99 -24
- package/src/registry.ts +30 -0
- package/src/remote-client.ts +21 -2
- package/src/remote-tools.ts +4 -0
- package/src/runtime-tool.ts +10 -4
- package/src/server-catalog.ts +6 -0
- package/src/server-input.ts +61 -0
- package/src/server-voice.ts +17 -0
- package/src/server.ts +40 -78
- package/src/tenant-discover.ts +167 -0
- package/src/tenant.ts +94 -21
- package/src/url-elicitation.ts +153 -0
- package/src/wire.ts +2 -0
package/src/server-catalog.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { fixFor, isJsonObject, logger, stringField } from '@ultimat3/core';
|
|
|
9
9
|
import type { McpCatalogNameSource } from './catalog-errors';
|
|
10
10
|
import { McpCatalogToolDuplicateError, McpCatalogUnresolvedError } from './catalog-errors';
|
|
11
11
|
import { asFrameworkError, forAudience } from './framework-error';
|
|
12
|
+
import type { McpSplitOptions } from './meta-split-options';
|
|
12
13
|
import type { McpResourceGroups, McpSurfaceOption } from './meta-surface';
|
|
13
14
|
import { META_TOOL_NAMES, MetaSurface } from './meta-surface';
|
|
14
15
|
import type { AnyMcpTool, McpCaller } from './registry';
|
|
@@ -49,6 +50,7 @@ export interface PerCall {
|
|
|
49
50
|
readonly tools: readonly AnyMcpTool[];
|
|
50
51
|
readonly surface?: McpSurfaceOption | undefined;
|
|
51
52
|
readonly groups?: McpResourceGroups | undefined;
|
|
53
|
+
readonly split?: McpSplitOptions | undefined;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
export interface CatalogRefused {
|
|
@@ -67,6 +69,7 @@ export async function viewOf(
|
|
|
67
69
|
tools: perCall.tools,
|
|
68
70
|
surface: perCall.surface,
|
|
69
71
|
groups: perCall.groups,
|
|
72
|
+
split: perCall.split,
|
|
70
73
|
source: perCall.catalog,
|
|
71
74
|
tenantArg: perCall.tenant?.arg,
|
|
72
75
|
},
|
|
@@ -107,6 +110,8 @@ export interface CatalogBoot {
|
|
|
107
110
|
readonly tools: readonly AnyMcpTool[];
|
|
108
111
|
readonly surface: McpSurfaceOption | undefined;
|
|
109
112
|
readonly groups: McpResourceGroups | undefined;
|
|
113
|
+
/** The app's `split:`, so a resolved view's dispatchers are the boot ones. */
|
|
114
|
+
readonly split?: McpSplitOptions | undefined;
|
|
110
115
|
readonly source: McpCatalogSource;
|
|
111
116
|
/** `tenant.arg`, which no runtime tool may declare either. */
|
|
112
117
|
readonly tenantArg: string | undefined;
|
|
@@ -179,6 +184,7 @@ function metaOf(
|
|
|
179
184
|
surface: boot.surface,
|
|
180
185
|
groups: groups.size === 0 ? undefined : Object.fromEntries(groups),
|
|
181
186
|
tools,
|
|
187
|
+
split: boot.split,
|
|
182
188
|
}),
|
|
183
189
|
);
|
|
184
190
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// What `mcpServer(input)` takes. Split from `server.ts` for the size ceiling only: it declares,
|
|
2
|
+
// and decides nothing.
|
|
3
|
+
|
|
4
|
+
import type { Clock, ErrorAudience } from '@ultimat3/core';
|
|
5
|
+
import type { McpAuditHook } from './audit-hook';
|
|
6
|
+
import type { McpCatalogDetail } from './meta-catalog-render';
|
|
7
|
+
import type { McpSplitOptions } from './meta-split-options';
|
|
8
|
+
import type { McpResourceGroups, McpSurfaceOption } from './meta-surface';
|
|
9
|
+
import type { AnyMcpTool } from './registry';
|
|
10
|
+
import type { McpPrompt, McpResource, McpResourceSource } from './resources';
|
|
11
|
+
import type { McpCatalogSource } from './server-catalog';
|
|
12
|
+
import type { McpInstructions } from './server-voice';
|
|
13
|
+
import type { McpTenant } from './tenant';
|
|
14
|
+
import type { ServerInfo } from './wire';
|
|
15
|
+
|
|
16
|
+
export interface McpServerInput {
|
|
17
|
+
readonly tools?: readonly AnyMcpTool[];
|
|
18
|
+
readonly resources?: readonly McpResource[];
|
|
19
|
+
/** Resources listed per caller (a tenant's own docs), after the fixed ones. */
|
|
20
|
+
readonly resourceSources?: readonly McpResourceSource[];
|
|
21
|
+
readonly prompts?: readonly McpPrompt[];
|
|
22
|
+
readonly serverInfo?: ServerInfo;
|
|
23
|
+
/** `'flat'` (the default): one tool per primitive. See `McpSurfaceOption`. */
|
|
24
|
+
readonly surface?: McpSurfaceOption | undefined;
|
|
25
|
+
/** The meta catalog's resources. Required with a surface that can be `'meta'`. */
|
|
26
|
+
readonly groups?: McpResourceGroups | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* How much of each action `list_resources` says: `'full'` (the default) or `'compact'` — names
|
|
29
|
+
* and tags, the rest in `describe_resource`. Needs a surface that can be `'meta'`.
|
|
30
|
+
*/
|
|
31
|
+
readonly catalogDetail?: McpCatalogDetail | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* `initialize`'s `instructions`: how to use this server, for a client that injects it into the
|
|
34
|
+
* model's context (Claude Code does; many clients do not — never load-bear on it). A function
|
|
35
|
+
* answers per caller population — a staff surface and a customer one read different advice —
|
|
36
|
+
* and a function that throws or answers anything but a non-empty string sends none.
|
|
37
|
+
*/
|
|
38
|
+
readonly instructions?: McpInstructions | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Whose `fix:` line a refusal carries. `'developer'` (the default, and the dev server's): the
|
|
41
|
+
* fix the author wrote — `x policy explain …`, a declaration to edit. `'caller'`: the error's
|
|
42
|
+
* `callerFix` where it declares one — what a REMOTE agent can do about it. `defineAppMcp`
|
|
43
|
+
* serves `'caller'`.
|
|
44
|
+
*/
|
|
45
|
+
readonly errorAudience?: ErrorAudience | undefined;
|
|
46
|
+
/** Every gate decision as data, after its log line — see `audit-hook.ts`. The route reads it too. */
|
|
47
|
+
readonly onAudit?: McpAuditHook | undefined;
|
|
48
|
+
/** Stamps each `onAudit` event's `at`. */
|
|
49
|
+
readonly clock?: Clock | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* The per-caller catalog, resolved per request for `tools/list` and every `tools/call` and merged
|
|
52
|
+
* with `tools`/`groups` (`server-catalog.ts`). `defineAppMcp({ catalog })` builds it.
|
|
53
|
+
*/
|
|
54
|
+
readonly catalog?: McpCatalogSource | undefined;
|
|
55
|
+
/** One token across tenants, the tenant named per call — see `tenant.ts`. */
|
|
56
|
+
readonly tenant?: McpTenant | undefined /**
|
|
57
|
+
* What the app adds to the `'meta-split'` dispatchers: envelope fields of its own, and the
|
|
58
|
+
* `stricter` hook — see `meta-split-options.ts`. Needs a surface that can be `'meta-split'`.
|
|
59
|
+
*/;
|
|
60
|
+
readonly split?: McpSplitOptions | undefined;
|
|
61
|
+
}
|
package/src/server-voice.ts
CHANGED
|
@@ -6,11 +6,14 @@ import { InputInvalidError } from '@ultimat3/action';
|
|
|
6
6
|
import type { ErrorAudience } from '@ultimat3/core';
|
|
7
7
|
import { renderCauseValue } from '@ultimat3/core';
|
|
8
8
|
import { formatIssues } from '@ultimat3/schema';
|
|
9
|
+
import type { McpProtocolError } from './errors';
|
|
9
10
|
import type { McpCatalogDetail } from './meta-catalog-render';
|
|
10
11
|
import { McpSurfaceInvalidError } from './meta-errors';
|
|
11
12
|
import type { McpSurfaceOption } from './meta-surface';
|
|
12
13
|
import type { McpCaller, McpToolResult } from './registry';
|
|
13
14
|
import type { ArgIssue } from './validate-args';
|
|
15
|
+
import type { JsonRpcResponse } from './wire';
|
|
16
|
+
import { errorResponse, INVALID_REQUEST } from './wire';
|
|
14
17
|
|
|
15
18
|
/** See `McpServerInput.instructions`. */
|
|
16
19
|
export type McpInstructions = string | ((caller: McpCaller) => string | undefined);
|
|
@@ -96,3 +99,17 @@ export function screenCatalogDetail(
|
|
|
96
99
|
}
|
|
97
100
|
return detail;
|
|
98
101
|
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The envelope refusals — no id to answer on, so `null` — rendered the way the scope refusal
|
|
105
|
+
* already is: a `message` a client library surfaces verbatim, and `data: { code, fix, docs }`
|
|
106
|
+
* built by the error class that owns the wording. Every other refusal on this surface carried its
|
|
107
|
+
* instruction; these two answered a bare `-32600` and left the caller to guess.
|
|
108
|
+
*/
|
|
109
|
+
export function protocolRefusal(message: string, error: McpProtocolError): JsonRpcResponse {
|
|
110
|
+
return errorResponse(null, INVALID_REQUEST, message, {
|
|
111
|
+
code: error.code,
|
|
112
|
+
fix: error.fix,
|
|
113
|
+
docs: error.docs,
|
|
114
|
+
});
|
|
115
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -3,16 +3,14 @@
|
|
|
3
3
|
// and an already-resolved caller, and returns a response or `null` for a notification.
|
|
4
4
|
// Both transports (http, stdio) and every test drive this one function.
|
|
5
5
|
|
|
6
|
-
import type { Clock, ErrorAudience } from '@ultimat3/core';
|
|
7
6
|
import { fixFor } from '@ultimat3/core';
|
|
8
7
|
import { outcomeForResult } from './audit';
|
|
9
|
-
import type {
|
|
8
|
+
import type { McpAuditor } from './audit-hook';
|
|
10
9
|
import { LOG_ONLY_AUDITOR, mcpAuditor } from './audit-hook';
|
|
11
10
|
import { McpProtocolError, McpScopeDeniedError, TOOL_UNKNOWN_FIX } from './errors';
|
|
12
11
|
import { metaCall } from './meta-call';
|
|
13
|
-
import type { McpCatalogDetail } from './meta-catalog-render';
|
|
14
12
|
import { META_UNKNOWN_FIX } from './meta-errors';
|
|
15
|
-
import type {
|
|
13
|
+
import type { MetaResource } from './meta-surface';
|
|
16
14
|
import { MetaSurface } from './meta-surface';
|
|
17
15
|
import { promptListEntry, promptsGet, visiblePrompts } from './prompts-get';
|
|
18
16
|
import type { McpEra } from './protocol-era';
|
|
@@ -26,7 +24,6 @@ import {
|
|
|
26
24
|
unsupportedVersion,
|
|
27
25
|
} from './protocol-era';
|
|
28
26
|
import type {
|
|
29
|
-
AnyMcpTool,
|
|
30
27
|
McpCaller,
|
|
31
28
|
McpToolResult,
|
|
32
29
|
McpVerbClass,
|
|
@@ -34,16 +31,23 @@ import type {
|
|
|
34
31
|
ToolResolution,
|
|
35
32
|
} from './registry';
|
|
36
33
|
import { ToolRegistry } from './registry';
|
|
37
|
-
import type { McpPrompt
|
|
34
|
+
import type { McpPrompt } from './resources';
|
|
38
35
|
import { ResourceRegistry } from './resources';
|
|
39
|
-
import type { CatalogRefused, CatalogView,
|
|
36
|
+
import type { CatalogRefused, CatalogView, PerCall } from './server-catalog';
|
|
40
37
|
import { catalogRefusal, viewOf } from './server-catalog';
|
|
38
|
+
import type { McpServerInput } from './server-input';
|
|
41
39
|
import { resourcesList, resourcesRead } from './server-resources';
|
|
42
|
-
import type {
|
|
43
|
-
import {
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
import type { McpServerVoice } from './server-voice';
|
|
41
|
+
import {
|
|
42
|
+
instructionsFor,
|
|
43
|
+
invalidArgsResult,
|
|
44
|
+
protocolRefusal,
|
|
45
|
+
screenCatalogDetail,
|
|
46
|
+
} from './server-voice';
|
|
47
|
+
import { assertTenant, listTenantsTool, tenantStep, withTenantArg } from './tenant';
|
|
48
|
+
import { tenantDoor, tenantsCall } from './tenant-discover';
|
|
46
49
|
import { admitted, thrownResponse } from './tool-thrown';
|
|
50
|
+
import { inputRequiredFor } from './url-elicitation';
|
|
47
51
|
import type { JsonRpcId, JsonRpcRequest, JsonRpcResponse, McpWire, ServerInfo } from './wire';
|
|
48
52
|
import {
|
|
49
53
|
defaultServerInfo,
|
|
@@ -58,56 +62,16 @@ import {
|
|
|
58
62
|
resultResponse,
|
|
59
63
|
} from './wire';
|
|
60
64
|
|
|
65
|
+
export type { McpServerInput } from './server-input';
|
|
61
66
|
export type { McpWire } from './wire';
|
|
62
67
|
|
|
63
|
-
export interface McpServerInput {
|
|
64
|
-
readonly tools?: readonly AnyMcpTool[];
|
|
65
|
-
readonly resources?: readonly McpResource[];
|
|
66
|
-
/** Resources listed per caller (a tenant's own docs), after the fixed ones. */
|
|
67
|
-
readonly resourceSources?: readonly McpResourceSource[];
|
|
68
|
-
readonly prompts?: readonly McpPrompt[];
|
|
69
|
-
readonly serverInfo?: ServerInfo;
|
|
70
|
-
/** `'flat'` (the default): one tool per primitive. See `McpSurfaceOption`. */
|
|
71
|
-
readonly surface?: McpSurfaceOption | undefined;
|
|
72
|
-
/** The meta catalog's resources. Required with a surface that can be `'meta'`. */
|
|
73
|
-
readonly groups?: McpResourceGroups | undefined;
|
|
74
|
-
/**
|
|
75
|
-
* How much of each action `list_resources` says: `'full'` (the default) or `'compact'` — names
|
|
76
|
-
* and tags, the rest in `describe_resource`. Needs a surface that can be `'meta'`.
|
|
77
|
-
*/
|
|
78
|
-
readonly catalogDetail?: McpCatalogDetail | undefined;
|
|
79
|
-
/**
|
|
80
|
-
* `initialize`'s `instructions`: how to use this server, for a client that injects it into the
|
|
81
|
-
* model's context (Claude Code does; many clients do not — never load-bear on it). A function
|
|
82
|
-
* answers per caller population — a staff surface and a customer one read different advice —
|
|
83
|
-
* and a function that throws or answers anything but a non-empty string sends none.
|
|
84
|
-
*/
|
|
85
|
-
readonly instructions?: McpInstructions | undefined;
|
|
86
|
-
/**
|
|
87
|
-
* Whose `fix:` line a refusal carries. `'developer'` (the default, and the dev server's): the
|
|
88
|
-
* fix the author wrote — `x policy explain …`, a declaration to edit. `'caller'`: the error's
|
|
89
|
-
* `callerFix` where it declares one — what a REMOTE agent can do about it. `defineAppMcp`
|
|
90
|
-
* serves `'caller'`.
|
|
91
|
-
*/
|
|
92
|
-
readonly errorAudience?: ErrorAudience | undefined;
|
|
93
|
-
/** Every gate decision as data, after its log line — see `audit-hook.ts`. The route reads it too. */
|
|
94
|
-
readonly onAudit?: McpAuditHook | undefined;
|
|
95
|
-
/** Stamps each `onAudit` event's `at`. */
|
|
96
|
-
readonly clock?: Clock | undefined;
|
|
97
|
-
/**
|
|
98
|
-
* The per-caller catalog, resolved per request for `tools/list` and every `tools/call` and merged
|
|
99
|
-
* with `tools`/`groups` (`server-catalog.ts`). `defineAppMcp({ catalog })` builds it.
|
|
100
|
-
*/
|
|
101
|
-
readonly catalog?: McpCatalogSource | undefined;
|
|
102
|
-
/** One token across tenants, the tenant named per call — see `tenant.ts`. */
|
|
103
|
-
readonly tenant?: McpTenant | undefined;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
68
|
export function mcpServer(input: McpServerInput = {}): McpServer {
|
|
107
69
|
const tenant = input.tenant;
|
|
108
|
-
if (tenant !== undefined) assertTenant(tenant, input.tools ?? []);
|
|
70
|
+
if (tenant !== undefined) assertTenant(tenant, input.tools ?? [], input);
|
|
109
71
|
// `list_tenants` is a boot tool like any other, so every surface lists it and nothing shadows it.
|
|
110
|
-
|
|
72
|
+
// A `discover` tool takes its place, and it is then not registered.
|
|
73
|
+
const lists = tenant === undefined || tenant.discover !== undefined;
|
|
74
|
+
const all = [...(input.tools ?? []), ...(lists ? [] : [listTenantsTool(tenant)])];
|
|
111
75
|
const tools = new ToolRegistry(tenant?.arg).registerAll(all);
|
|
112
76
|
const resources = new ResourceRegistry().registerAll(input.resources ?? []);
|
|
113
77
|
for (const source of input.resourceSources ?? []) resources.addSource(source);
|
|
@@ -115,6 +79,7 @@ export function mcpServer(input: McpServerInput = {}): McpServer {
|
|
|
115
79
|
surface: input.surface,
|
|
116
80
|
groups: input.groups,
|
|
117
81
|
tools: all,
|
|
82
|
+
split: input.split,
|
|
118
83
|
});
|
|
119
84
|
const catalogDetail = screenCatalogDetail(input.catalogDetail, input.surface);
|
|
120
85
|
return new McpServer(
|
|
@@ -137,6 +102,7 @@ export function mcpServer(input: McpServerInput = {}): McpServer {
|
|
|
137
102
|
tools: all,
|
|
138
103
|
surface: input.surface,
|
|
139
104
|
groups: input.groups,
|
|
105
|
+
split: input.split,
|
|
140
106
|
},
|
|
141
107
|
);
|
|
142
108
|
}
|
|
@@ -287,14 +253,12 @@ export class McpServer {
|
|
|
287
253
|
private listIn(view: CatalogView, caller: McpCaller): readonly ToolListEntry[] {
|
|
288
254
|
const flat = view.tools.list(caller);
|
|
289
255
|
const meta = metaServed(view.meta, caller);
|
|
290
|
-
const
|
|
256
|
+
const tenant = this.perCall?.tenant;
|
|
291
257
|
const withArg = (entries: readonly ToolListEntry[]) =>
|
|
292
|
-
|
|
258
|
+
tenant === undefined ? entries : withTenantArg(entries, tenant);
|
|
293
259
|
if (meta === undefined) return withArg(flat);
|
|
294
260
|
// Constant: the meta tools plus whatever the app left ungrouped (`docs`, `whoami`).
|
|
295
|
-
const entries =
|
|
296
|
-
meta.surfaceFor(caller) === 'meta' ? 'meta' : 'meta-split',
|
|
297
|
-
);
|
|
261
|
+
const entries = meta.entries(meta.surfaceFor(caller) === 'meta' ? 'meta' : 'meta-split');
|
|
298
262
|
return withArg(
|
|
299
263
|
[...flat.filter((tool) => !meta.isGrouped(tool.name)), ...entries].sort((a, b) =>
|
|
300
264
|
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
|
|
@@ -342,8 +306,14 @@ export class McpServer {
|
|
|
342
306
|
// answers exactly as a hidden tool — `notFound`, the same call the dispatch below makes.
|
|
343
307
|
let who = caller;
|
|
344
308
|
let rawArgs: unknown = params['arguments'];
|
|
309
|
+
// A call naming none meets a door first (`tenant-discover.ts`): the tenant list, or the person.
|
|
345
310
|
const tenant = this.perCall?.tenant;
|
|
346
|
-
|
|
311
|
+
const door = tenant === undefined ? 'person' : tenantDoor(tenant, name, rawArgs);
|
|
312
|
+
if (tenant !== undefined && door === 'tenants') {
|
|
313
|
+
const resolved = tenantsCall(listTenantsTool(tenant), this.tools.get(name), name, caller);
|
|
314
|
+
return this.dispatch(id, name, resolved, caller, signal);
|
|
315
|
+
}
|
|
316
|
+
if (tenant !== undefined && door === 'step') {
|
|
347
317
|
const step = await tenantStep(tenant, rawArgs, caller);
|
|
348
318
|
if (step.kind === 'not-member') {
|
|
349
319
|
return this.notFound(
|
|
@@ -370,7 +340,8 @@ export class McpServer {
|
|
|
370
340
|
tools: view.tools,
|
|
371
341
|
audit: this.audit,
|
|
372
342
|
catalogDetail: this.voice.catalogDetail ?? 'full',
|
|
373
|
-
dispatch: (at, tool, resolved, who) =>
|
|
343
|
+
dispatch: (at, tool, resolved, who) =>
|
|
344
|
+
this.dispatch(at, tool, resolved, who, signal, req),
|
|
374
345
|
notFound: (at, tool, who, fix) => this.notFound(at, tool, who, fix),
|
|
375
346
|
refuse: (at, tool, error, who) =>
|
|
376
347
|
thrownResponse(at, tool, error, who, this.voice.errorAudience, this.audit),
|
|
@@ -380,7 +351,7 @@ export class McpServer {
|
|
|
380
351
|
);
|
|
381
352
|
if (answered !== undefined) return answered;
|
|
382
353
|
}
|
|
383
|
-
return this.dispatch(id, name, view.tools.resolve(name, rawArgs ?? {}, who), who, signal);
|
|
354
|
+
return this.dispatch(id, name, view.tools.resolve(name, rawArgs ?? {}, who), who, signal, req);
|
|
384
355
|
}
|
|
385
356
|
|
|
386
357
|
/** OUTCOME 1, the one answer for absent and hidden alike. See `dispatch`. */
|
|
@@ -399,6 +370,7 @@ export class McpServer {
|
|
|
399
370
|
resolved: ToolResolution,
|
|
400
371
|
caller: McpCaller,
|
|
401
372
|
signal?: AbortSignal,
|
|
373
|
+
request?: JsonRpcRequest,
|
|
402
374
|
): Promise<JsonRpcResponse> {
|
|
403
375
|
const audience = this.voice.errorAudience;
|
|
404
376
|
// Three outcomes, deliberately different — and every one of them audited, including the
|
|
@@ -450,8 +422,11 @@ export class McpServer {
|
|
|
450
422
|
}
|
|
451
423
|
|
|
452
424
|
let result: McpToolResult;
|
|
425
|
+
// A page the tool asked the person to open, for a client that can show one (`url-elicitation.ts`).
|
|
426
|
+
let asked: Record<string, unknown> | undefined;
|
|
453
427
|
try {
|
|
454
428
|
result = await resolved.tool.handle(resolved.args, caller, signal);
|
|
429
|
+
asked = request === undefined ? undefined : inputRequiredFor(request, result);
|
|
455
430
|
} catch (error) {
|
|
456
431
|
return thrownResponse(id, name, error, caller, audience, this.audit);
|
|
457
432
|
}
|
|
@@ -465,6 +440,7 @@ export class McpServer {
|
|
|
465
440
|
caller,
|
|
466
441
|
...(result.code === undefined ? {} : { code: result.code }),
|
|
467
442
|
});
|
|
443
|
+
if (asked !== undefined) return resultResponse(id, asked);
|
|
468
444
|
// `code` is audit-only and never reaches the wire: it is already inside the rendered body.
|
|
469
445
|
const payload: Record<string, unknown> = { content: result.content };
|
|
470
446
|
// Through `manage_resource` too, byte for byte: the meta door answers what the flat one does,
|
|
@@ -477,20 +453,6 @@ export class McpServer {
|
|
|
477
453
|
}
|
|
478
454
|
}
|
|
479
455
|
|
|
480
|
-
/**
|
|
481
|
-
* The envelope refusals — no id to answer on, so `null` — rendered the way the scope refusal
|
|
482
|
-
* already is: a `message` a client library surfaces verbatim, and `data: { code, fix, docs }`
|
|
483
|
-
* built by the error class that owns the wording. Every other refusal on this surface carried its
|
|
484
|
-
* instruction; these two answered a bare `-32600` and left the caller to guess.
|
|
485
|
-
*/
|
|
486
|
-
function protocolRefusal(message: string, error: McpProtocolError): JsonRpcResponse {
|
|
487
|
-
return errorResponse(null, INVALID_REQUEST, message, {
|
|
488
|
-
code: error.code,
|
|
489
|
-
fix: error.fix,
|
|
490
|
-
docs: error.docs,
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
|
|
494
456
|
/** `meta` when this caller is served it, else `undefined` — one rule for the boot and a resolved view. */
|
|
495
457
|
function metaServed(meta: MetaSurface | undefined, caller: McpCaller): MetaSurface | undefined {
|
|
496
458
|
return meta !== undefined && meta.surfaceFor(caller) !== 'flat' ? meta : undefined;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// `tenant.discover` and `tenant.optional`: the calls a `tenant` server answers with no tenant named.
|
|
2
|
+
//
|
|
3
|
+
// By default one tool does, `list_tenants`. `discover` names a tool the server already serves to
|
|
4
|
+
// take its place — called without `tenant.arg` it answers `tenant.list`, called with it it is the
|
|
5
|
+
// tool it always was — so the tool list keeps its size. `optional` names tools that run as the
|
|
6
|
+
// PERSON without the arg and as the tenant actor with it. Both are boot-checked: a name no caller
|
|
7
|
+
// could reach would leave `X_MCP_TENANT_REQUIRED` pointing at nothing.
|
|
8
|
+
|
|
9
|
+
import { isJsonObject, renderCauseValue } from '@ultimat3/core';
|
|
10
|
+
import { McpTenantInvalidError } from './catalog-errors';
|
|
11
|
+
import type { McpResourceGroups, McpSurfaceOption } from './meta-surface';
|
|
12
|
+
import { LIST_RESOURCES, META_TOOL_NAMES } from './meta-surface';
|
|
13
|
+
import type { AnyMcpTool, McpCaller, ToolResolution } from './registry';
|
|
14
|
+
|
|
15
|
+
/** The tool a `tenant` server registers to list tenants when `discover` names no other. */
|
|
16
|
+
export const LIST_TENANTS = 'list_tenants';
|
|
17
|
+
|
|
18
|
+
/** The half of `McpTenant` this file reads. */
|
|
19
|
+
export interface TenantDoors {
|
|
20
|
+
readonly arg: string;
|
|
21
|
+
readonly discover?: string | undefined;
|
|
22
|
+
readonly optional?: readonly string[] | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The meta declaration the discover and optional names are checked against. */
|
|
26
|
+
export interface TenantSurface {
|
|
27
|
+
readonly surface?: McpSurfaceOption | undefined;
|
|
28
|
+
readonly groups?: McpResourceGroups | undefined;
|
|
29
|
+
/** The dispatcher envelopes' own fields: the arg is stripped from those too. */
|
|
30
|
+
readonly split?: { readonly fields?: Readonly<Record<string, unknown>> | undefined } | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* How one call meets the tenant step. `'tenants'`: answer the tenant list. `'person'`: run the
|
|
35
|
+
* tool as the person, in no tenant. `'step'`: the call names its tenant, or is refused.
|
|
36
|
+
*/
|
|
37
|
+
export type TenantDoor = 'tenants' | 'person' | 'step';
|
|
38
|
+
|
|
39
|
+
/** The tool that lists this server's tenants: what `X_MCP_TENANT_REQUIRED` points at. */
|
|
40
|
+
export const tenantLister = (tenant: TenantDoors): string => tenant.discover ?? LIST_TENANTS;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Only an ABSENT arg opens a door. One that is present and unusable (`''`, `null`, a number) goes
|
|
44
|
+
* to the step and is refused there: a malformed tenant never quietly becomes "no tenant".
|
|
45
|
+
*/
|
|
46
|
+
export function tenantDoor(tenant: TenantDoors, name: string, rawArgs: unknown): TenantDoor {
|
|
47
|
+
// `list_tenants` takes no tenant whatever it is sent, as it always did.
|
|
48
|
+
if (tenant.discover === undefined && name === LIST_TENANTS) return 'person';
|
|
49
|
+
const named =
|
|
50
|
+
isJsonObject(rawArgs) &&
|
|
51
|
+
Object.hasOwn(rawArgs, tenant.arg) &&
|
|
52
|
+
rawArgs[tenant.arg] !== undefined;
|
|
53
|
+
if (named) return 'step';
|
|
54
|
+
if (name === tenant.discover) return 'tenants';
|
|
55
|
+
return tenant.optional?.includes(name) === true ? 'person' : 'step';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const FIX =
|
|
59
|
+
"defineAppMcp({ tenant: { arg, resolve, list, discover: 'list_resources', optional: ['docs'] } }) — discover names list_resources (surface: 'meta' or 'meta-split', never a function) or an ungrouped tool of tools: with no visibleTo; optional names ungrouped tools of tools: — or drop the option the cause names";
|
|
60
|
+
|
|
61
|
+
function refuse(cause: string): never {
|
|
62
|
+
throw new McpTenantInvalidError({ cause, fix: FIX });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Boot: `discover` and `optional` each name a tool every caller reaches by that name. */
|
|
66
|
+
export function assertTenantDoors(
|
|
67
|
+
tenant: TenantDoors,
|
|
68
|
+
tools: readonly AnyMcpTool[],
|
|
69
|
+
meta: TenantSurface,
|
|
70
|
+
): void {
|
|
71
|
+
if (Object.hasOwn(meta.split?.fields ?? {}, tenant.arg)) {
|
|
72
|
+
throw new McpTenantInvalidError({
|
|
73
|
+
cause: `split.fields declares "${tenant.arg}", which tenant.arg strips from every call`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const byName = new Map(tools.map((tool) => [tool.name, tool]));
|
|
77
|
+
const grouped = new Set(Object.values(meta.groups ?? {}).flatMap((group) => group.tools));
|
|
78
|
+
const discover: unknown = tenant.discover;
|
|
79
|
+
if (discover !== undefined) assertDiscover(discover, byName, grouped, meta.surface);
|
|
80
|
+
const optional: unknown = tenant.optional;
|
|
81
|
+
if (optional === undefined) return;
|
|
82
|
+
if (!Array.isArray(optional)) {
|
|
83
|
+
refuse(
|
|
84
|
+
`tenant.optional is ${renderCauseValue(optional)}, and it has to be a list of tool names`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const seen = new Set<unknown>();
|
|
88
|
+
for (const name of optional as readonly unknown[]) {
|
|
89
|
+
const shown = renderCauseValue(name);
|
|
90
|
+
if (seen.has(name)) refuse(`tenant.optional names ${shown} twice`);
|
|
91
|
+
seen.add(name);
|
|
92
|
+
if (name === discover) {
|
|
93
|
+
refuse(
|
|
94
|
+
`tenant.optional names ${shown}, which is tenant.discover: with no tenant it answers the tenant list`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
assertUngrouped('tenant.optional', name, byName, grouped);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function assertDiscover(
|
|
102
|
+
discover: unknown,
|
|
103
|
+
tools: ReadonlyMap<string, AnyMcpTool>,
|
|
104
|
+
grouped: ReadonlySet<string>,
|
|
105
|
+
surface: McpSurfaceOption | undefined,
|
|
106
|
+
): void {
|
|
107
|
+
const shown = renderCauseValue(discover);
|
|
108
|
+
if (typeof discover === 'string' && META_TOOL_NAMES.includes(discover)) {
|
|
109
|
+
if (discover !== LIST_RESOURCES) {
|
|
110
|
+
refuse(
|
|
111
|
+
`tenant.discover names ${shown}, and the one meta tool that can list tenants is "${LIST_RESOURCES}"`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
// A function serves some callers 'flat', and they would have no tool to list their tenants.
|
|
115
|
+
if (surface !== 'meta' && surface !== 'meta-split') {
|
|
116
|
+
refuse(
|
|
117
|
+
`tenant.discover names ${shown}, and surface is not 'meta' or 'meta-split' for every caller, so some caller is never served that tool`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const tool = assertUngrouped('tenant.discover', discover, tools, grouped);
|
|
123
|
+
// The tenant list is answered in the tool's place: its scope is still asked, but a caller it is
|
|
124
|
+
// hidden from would be pointed at a tool it cannot see.
|
|
125
|
+
if (tool.visibleTo !== undefined) {
|
|
126
|
+
refuse(
|
|
127
|
+
`tenant.discover names ${shown}, which declares visibleTo: a caller it is hidden from could not list its tenants`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A call of the discover tool that names no tenant, resolved: `lister` runs in its place, behind
|
|
134
|
+
* the scope the tool itself declares — the one gate of its own a boot check has not ruled out.
|
|
135
|
+
*/
|
|
136
|
+
export function tenantsCall(
|
|
137
|
+
lister: AnyMcpTool,
|
|
138
|
+
discover: AnyMcpTool | undefined,
|
|
139
|
+
name: string,
|
|
140
|
+
caller: McpCaller,
|
|
141
|
+
): ToolResolution {
|
|
142
|
+
const scope = discover?.scope;
|
|
143
|
+
return scope !== undefined && !caller.scopes.has(scope)
|
|
144
|
+
? { kind: 'scope-denied', name, scope }
|
|
145
|
+
: { kind: 'ok', tool: lister, args: {} };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function assertUngrouped(
|
|
149
|
+
option: string,
|
|
150
|
+
name: unknown,
|
|
151
|
+
tools: ReadonlyMap<string, AnyMcpTool>,
|
|
152
|
+
grouped: ReadonlySet<string>,
|
|
153
|
+
): AnyMcpTool {
|
|
154
|
+
const shown = renderCauseValue(name);
|
|
155
|
+
const tool = typeof name === 'string' ? tools.get(name) : undefined;
|
|
156
|
+
if (tool === undefined) {
|
|
157
|
+
return refuse(
|
|
158
|
+
`${option} names ${shown}, which is not a tool of this server's tools: (a per-caller catalog tool and a meta dispatcher cannot be named)`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
if (grouped.has(tool.name)) {
|
|
162
|
+
return refuse(
|
|
163
|
+
`${option} names ${shown}, which a group lists: a grouped tool is reached through a dispatcher, never by its name`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return tool;
|
|
167
|
+
}
|