@ultimat3/mcp 29.3.2 → 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,200 @@
|
|
|
1
|
+
// The per-caller catalog: what `tools/list` and `tools/call` read for ONE request, resolved per
|
|
2
|
+
// request (never cached) and merged with the boot catalog into a fresh `ToolRegistry` and meta
|
|
3
|
+
// surface, so every gate runs unchanged. A failure fails the request (`X_MCP_CATALOG_*`).
|
|
4
|
+
//
|
|
5
|
+
// Never a smaller catalog, never one name twice.
|
|
6
|
+
|
|
7
|
+
import type { ErrorAudience } from '@ultimat3/core';
|
|
8
|
+
import { fixFor, logger, stringField } from '@ultimat3/core';
|
|
9
|
+
import type { McpCatalogNameSource } from './catalog-errors';
|
|
10
|
+
import { McpCatalogToolDuplicateError, McpCatalogUnresolvedError } from './catalog-errors';
|
|
11
|
+
import { asFrameworkError, forAudience } from './framework-error';
|
|
12
|
+
import type { McpResourceGroups, McpSurfaceOption } from './meta-surface';
|
|
13
|
+
import { META_TOOL_NAMES, MetaSurface } from './meta-surface';
|
|
14
|
+
import type { AnyMcpTool, McpCaller } from './registry';
|
|
15
|
+
import { ToolRegistry } from './registry';
|
|
16
|
+
import type { McpTenant } from './tenant';
|
|
17
|
+
import { assertArgFree, LIST_TENANTS } from './tenant';
|
|
18
|
+
import type { JsonRpcId, JsonRpcResponse } from './wire';
|
|
19
|
+
import { errorResponse, INTERNAL_ERROR } from './wire';
|
|
20
|
+
|
|
21
|
+
/** What a catalog source answers for one caller: tools, and — on a meta server — their resources. */
|
|
22
|
+
export interface McpResolvedCatalog {
|
|
23
|
+
readonly tools: readonly AnyMcpTool[];
|
|
24
|
+
/** Resources for `list_resources`, beside the boot `groups:` — never one of the same name. */
|
|
25
|
+
readonly groups?: McpResourceGroups | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `defineAppMcp({ catalog })`, already checked there (`runtimeTool()` only, scopes, confirmations). */
|
|
29
|
+
export type McpCatalogSource = (caller: McpCaller) => Promise<McpResolvedCatalog>;
|
|
30
|
+
|
|
31
|
+
/** The registry and meta surface one request reads. */
|
|
32
|
+
export interface CatalogView {
|
|
33
|
+
readonly tools: ToolRegistry;
|
|
34
|
+
readonly meta: MetaSurface | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type CatalogAnswer =
|
|
38
|
+
| { readonly ok: true; readonly view: CatalogView }
|
|
39
|
+
| {
|
|
40
|
+
readonly ok: false;
|
|
41
|
+
readonly error: McpCatalogToolDuplicateError | McpCatalogUnresolvedError;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** What `McpServer` reads per request beyond its boot catalog. */
|
|
45
|
+
export interface PerCall {
|
|
46
|
+
readonly catalog?: McpCatalogSource | undefined;
|
|
47
|
+
readonly tenant?: McpTenant | undefined;
|
|
48
|
+
/** The boot tools (with `list_tenants`) and meta declaration a resolved catalog merges with. */
|
|
49
|
+
readonly tools: readonly AnyMcpTool[];
|
|
50
|
+
readonly surface?: McpSurfaceOption | undefined;
|
|
51
|
+
readonly groups?: McpResourceGroups | undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface CatalogRefused {
|
|
55
|
+
readonly error: McpCatalogToolDuplicateError | McpCatalogUnresolvedError;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The boot view when there is no catalog source, else this caller's merged one, or its refusal. */
|
|
59
|
+
export async function viewOf(
|
|
60
|
+
perCall: PerCall | undefined,
|
|
61
|
+
boot: CatalogView,
|
|
62
|
+
caller: McpCaller,
|
|
63
|
+
): Promise<CatalogView | CatalogRefused> {
|
|
64
|
+
if (perCall?.catalog === undefined) return boot;
|
|
65
|
+
const answer = await catalogFor(
|
|
66
|
+
{
|
|
67
|
+
tools: perCall.tools,
|
|
68
|
+
surface: perCall.surface,
|
|
69
|
+
groups: perCall.groups,
|
|
70
|
+
source: perCall.catalog,
|
|
71
|
+
tenantArg: perCall.tenant?.arg,
|
|
72
|
+
},
|
|
73
|
+
caller,
|
|
74
|
+
);
|
|
75
|
+
return answer.ok ? answer.view : { error: answer.error };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `tools/list` when the catalog cannot be served: one `-32603` with `{ code, cause, fix }` attached
|
|
80
|
+
* — a 5xx, so a `'caller'` audience reads the hidden cause. `tools/call` renders it as a result.
|
|
81
|
+
*/
|
|
82
|
+
export function catalogRefusal(
|
|
83
|
+
id: JsonRpcId,
|
|
84
|
+
error: McpCatalogToolDuplicateError | McpCatalogUnresolvedError,
|
|
85
|
+
audience: ErrorAudience,
|
|
86
|
+
): JsonRpcResponse {
|
|
87
|
+
// Always framework-shaped — these are our own classes; the fallback only satisfies the type.
|
|
88
|
+
const shown = forAudience(
|
|
89
|
+
asFrameworkError(error) ?? {
|
|
90
|
+
code: error.code,
|
|
91
|
+
title: error.title,
|
|
92
|
+
cause: error.cause,
|
|
93
|
+
fix: error.fix,
|
|
94
|
+
},
|
|
95
|
+
audience,
|
|
96
|
+
);
|
|
97
|
+
const fix = fixFor(shown, audience);
|
|
98
|
+
return errorResponse(id, INTERNAL_ERROR, `tool catalog unavailable — ${fix}`, {
|
|
99
|
+
code: shown.code,
|
|
100
|
+
cause: shown.cause,
|
|
101
|
+
fix,
|
|
102
|
+
...(shown.docs === undefined ? {} : { docs: shown.docs }),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface CatalogBoot {
|
|
107
|
+
readonly tools: readonly AnyMcpTool[];
|
|
108
|
+
readonly surface: McpSurfaceOption | undefined;
|
|
109
|
+
readonly groups: McpResourceGroups | undefined;
|
|
110
|
+
readonly source: McpCatalogSource;
|
|
111
|
+
/** `tenant.arg`, which no runtime tool may declare either. */
|
|
112
|
+
readonly tenantArg: string | undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Resolve `boot.source` for this caller and merge it, or the refusal — logged once, here. */
|
|
116
|
+
export async function catalogFor(boot: CatalogBoot, caller: McpCaller): Promise<CatalogAnswer> {
|
|
117
|
+
try {
|
|
118
|
+
return { ok: true, view: merge(boot, await boot.source(caller)) };
|
|
119
|
+
} catch (thrown) {
|
|
120
|
+
const error =
|
|
121
|
+
thrown instanceof McpCatalogToolDuplicateError || thrown instanceof McpCatalogUnresolvedError
|
|
122
|
+
? thrown
|
|
123
|
+
: McpCatalogUnresolvedError.threw(thrown);
|
|
124
|
+
logger.error('mcp.catalog.unresolved', {
|
|
125
|
+
surface: 'mcp',
|
|
126
|
+
code: error.code,
|
|
127
|
+
cause: error.cause,
|
|
128
|
+
actor: caller.actor.id,
|
|
129
|
+
...(caller.tenant === undefined ? {} : { tenant: caller.tenant }),
|
|
130
|
+
});
|
|
131
|
+
return { ok: false, error };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function merge(boot: CatalogBoot, resolved: McpResolvedCatalog): CatalogView {
|
|
136
|
+
if (!isRecord(resolved) || !Array.isArray(resolved.tools)) {
|
|
137
|
+
throw new McpCatalogUnresolvedError({
|
|
138
|
+
cause: 'the catalog resolver answered something other than { tools: [...] }',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
// Every name already answered: static tools (and `list_tenants`), and the meta tools, reserved
|
|
142
|
+
// whatever the surface — a surface function may serve them to the next caller.
|
|
143
|
+
const taken = new Map<string, McpCatalogNameSource>();
|
|
144
|
+
for (const tool of boot.tools) {
|
|
145
|
+
taken.set(tool.name, tool.name === LIST_TENANTS ? 'list_tenants' : 'tools:');
|
|
146
|
+
}
|
|
147
|
+
for (const name of META_TOOL_NAMES) taken.set(name, 'a meta tool');
|
|
148
|
+
for (const tool of resolved.tools) {
|
|
149
|
+
const first = taken.get(tool.name);
|
|
150
|
+
if (first !== undefined) {
|
|
151
|
+
throw new McpCatalogToolDuplicateError({ name: tool.name, kind: 'tool', first });
|
|
152
|
+
}
|
|
153
|
+
taken.set(tool.name, 'catalog');
|
|
154
|
+
if (boot.tenantArg !== undefined) refused(() => assertArgFree(boot.tenantArg ?? '', tool));
|
|
155
|
+
}
|
|
156
|
+
const all = [...boot.tools, ...resolved.tools];
|
|
157
|
+
return { tools: new ToolRegistry().registerAll(all), meta: metaOf(boot, resolved, all) };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The boot groups plus the resolved ones, held to every boot check `MetaSurface.build` makes. */
|
|
161
|
+
function metaOf(
|
|
162
|
+
boot: CatalogBoot,
|
|
163
|
+
resolved: McpResolvedCatalog,
|
|
164
|
+
tools: readonly AnyMcpTool[],
|
|
165
|
+
): MetaSurface | undefined {
|
|
166
|
+
const extra = resolved.groups === undefined ? [] : Object.entries(resolved.groups);
|
|
167
|
+
const groups = new Map(Object.entries(boot.groups ?? {}));
|
|
168
|
+
for (const [name, group] of extra) {
|
|
169
|
+
if (groups.has(name)) {
|
|
170
|
+
throw new McpCatalogToolDuplicateError({ name, kind: 'resource', first: 'groups:' });
|
|
171
|
+
}
|
|
172
|
+
groups.set(name, group);
|
|
173
|
+
}
|
|
174
|
+
return refused(() =>
|
|
175
|
+
MetaSurface.build({
|
|
176
|
+
surface: boot.surface,
|
|
177
|
+
groups: groups.size === 0 ? undefined : Object.fromEntries(groups),
|
|
178
|
+
tools,
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A boot refusal (`X_MCP_GROUP_UNKNOWN`, `X_MCP_EFFECT_UNDECLARED`, `X_MCP_TENANT_INVALID`…) met at
|
|
185
|
+
* request time is this catalog's refusal, naming the boot code it failed.
|
|
186
|
+
*/
|
|
187
|
+
function refused<T>(check: () => T): T {
|
|
188
|
+
try {
|
|
189
|
+
return check();
|
|
190
|
+
} catch (thrown) {
|
|
191
|
+
const code = stringField(thrown, 'code') ?? 'a check';
|
|
192
|
+
throw new McpCatalogUnresolvedError({
|
|
193
|
+
cause: `the resolved catalog fails ${code}: ${stringField(thrown, 'cause') ?? ''}`,
|
|
194
|
+
sourceError: thrown,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
200
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// `resources/read` — the document surface's three outcomes (hidden = absent, scope, read), every
|
|
2
|
+
// one audited, and every provider call inside a `try`. Split from `server.ts` for the size ceiling;
|
|
3
|
+
// the server passes what it owns, so this stays a function and never a second dispatcher.
|
|
4
|
+
|
|
5
|
+
import type { ErrorAudience } from '@ultimat3/core';
|
|
6
|
+
import { fixFor } from '@ultimat3/core';
|
|
7
|
+
import type { McpAuditor } from './audit-hook';
|
|
8
|
+
import { McpScopeDeniedError } from './errors';
|
|
9
|
+
import { asFrameworkError, forAudience } from './framework-error';
|
|
10
|
+
import type { McpEra } from './protocol-era';
|
|
11
|
+
import { resourceNotFoundCode } from './protocol-era';
|
|
12
|
+
import type { McpCaller } from './registry';
|
|
13
|
+
import type { ResourceRegistry } from './resources';
|
|
14
|
+
import { readResource } from './resources';
|
|
15
|
+
import type { JsonRpcRequest, JsonRpcResponse } from './wire';
|
|
16
|
+
import {
|
|
17
|
+
errorResponse,
|
|
18
|
+
INTERNAL_ERROR,
|
|
19
|
+
INVALID_PARAMS,
|
|
20
|
+
INVALID_REQUEST,
|
|
21
|
+
paramsOf,
|
|
22
|
+
resultResponse,
|
|
23
|
+
} from './wire';
|
|
24
|
+
|
|
25
|
+
/** What the read needs from the server that owns it. */
|
|
26
|
+
export interface ResourceReadDeps {
|
|
27
|
+
readonly resources: ResourceRegistry;
|
|
28
|
+
readonly audit: McpAuditor;
|
|
29
|
+
readonly audience: ErrorAudience;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The same three-outcome shape `toolsCall` above applies, on the document surface. It took no
|
|
34
|
+
* caller at all until 2026-08: every accepted token could list every URI and read every one of
|
|
35
|
+
* them — the manifest, the OpenAPI document, the route table and the entity schema.
|
|
36
|
+
*
|
|
37
|
+
* And every outcome is AUDITED, hidden included, exactly as `toolsCall`'s are: this method
|
|
38
|
+
* emitted nothing at all, so a URI walk over those four documents was invisible while the same
|
|
39
|
+
* walk over tool names was one `warn` per attempt. `resources/list` stays silent, as
|
|
40
|
+
* `tools/list` does — it is answered pre-filtered.
|
|
41
|
+
*/
|
|
42
|
+
export async function resourcesRead(
|
|
43
|
+
on: ResourceReadDeps,
|
|
44
|
+
req: JsonRpcRequest,
|
|
45
|
+
caller: McpCaller,
|
|
46
|
+
era: McpEra,
|
|
47
|
+
): Promise<JsonRpcResponse> {
|
|
48
|
+
const id = req.id ?? null;
|
|
49
|
+
const uri = paramsOf(req)?.['uri'];
|
|
50
|
+
if (typeof uri !== 'string') {
|
|
51
|
+
return errorResponse(id, INVALID_PARAMS, 'resources/read params.uri must be a string');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const resolved = await on.resources.resolveFor(uri, caller);
|
|
55
|
+
switch (resolved.kind) {
|
|
56
|
+
// OUTCOME 1. Absent AND hidden collapse to one answer with no `data`: this branch used to
|
|
57
|
+
// return `available: [...every uri]`, so one wrong guess enumerated the whole catalog. No
|
|
58
|
+
// `code` on the audit line either, because the wire carries none — the tool surface's
|
|
59
|
+
// `X_MCP_TOOL_UNKNOWN` is an error class this branch has no twin for.
|
|
60
|
+
case 'not-found':
|
|
61
|
+
on.audit.resourceRead({ uri, outcome: 'hidden', caller });
|
|
62
|
+
return errorResponse(id, resourceNotFoundCode(era), `resource not found: ${uri}`);
|
|
63
|
+
// OUTCOME 2. The caller can already see this resource, so naming the missing scope leaks
|
|
64
|
+
// nothing — and the fix travels with it, built by the error that owns the wording.
|
|
65
|
+
case 'scope-denied': {
|
|
66
|
+
const denial = new McpScopeDeniedError({
|
|
67
|
+
name: uri,
|
|
68
|
+
scope: resolved.scope,
|
|
69
|
+
subject: 'resource',
|
|
70
|
+
});
|
|
71
|
+
on.audit.resourceRead({
|
|
72
|
+
uri,
|
|
73
|
+
outcome: 'scope-denied',
|
|
74
|
+
caller,
|
|
75
|
+
scope: resolved.scope,
|
|
76
|
+
code: denial.code,
|
|
77
|
+
});
|
|
78
|
+
return errorResponse(id, INVALID_REQUEST, `missing scope: ${resolved.scope}`, {
|
|
79
|
+
code: denial.code,
|
|
80
|
+
scope: resolved.scope,
|
|
81
|
+
fix: fixFor(denial, on.audience),
|
|
82
|
+
docs: denial.docs,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
case 'ok':
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The provider is an INJECTED THUNK — `frameworkResources` wires these to file reads, and
|
|
90
|
+
// `Bun.file(...).text()` on a missing `x.manifest.json` throws ENOENT. Outside a try it escaped
|
|
91
|
+
// `handle()` entirely: `serveStdio` rejected with the raw error, zero frames written, the
|
|
92
|
+
// request unanswered and every later request on that buffer never processed. Same shape
|
|
93
|
+
// `toolsCall` uses above, for the same reason.
|
|
94
|
+
try {
|
|
95
|
+
// The resource the resolver admitted, read AS that caller: a per-caller source's resource is
|
|
96
|
+
// in no fixed map, and a body may vary by who asked.
|
|
97
|
+
const contents = await readResource(resolved.resource, caller);
|
|
98
|
+
on.audit.resourceRead({ uri, outcome: 'ok', caller });
|
|
99
|
+
return resultResponse(id, { contents: [contents] });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
const thrown = asFrameworkError(error);
|
|
102
|
+
if (thrown !== undefined) {
|
|
103
|
+
const framework = forAudience(thrown, on.audience);
|
|
104
|
+
on.audit.resourceRead({ uri, outcome: 'failed', caller, code: framework.code });
|
|
105
|
+
return errorResponse(id, INTERNAL_ERROR, `resource "${uri}" could not be read`, {
|
|
106
|
+
code: framework.code,
|
|
107
|
+
cause: framework.cause,
|
|
108
|
+
fix: fixFor(framework, on.audience),
|
|
109
|
+
...(framework.docs === undefined ? {} : { docs: framework.docs }),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// No internals: a provider's own message names a path, a query or a host the caller has no
|
|
113
|
+
// business seeing, exactly as a failing tool's does.
|
|
114
|
+
on.audit.resourceRead({ uri, outcome: 'failed', caller });
|
|
115
|
+
return errorResponse(id, INTERNAL_ERROR, `resource "${uri}" could not be read`);
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/server-voice.ts
CHANGED
|
@@ -46,7 +46,16 @@ export function invalidArgsResult(
|
|
|
46
46
|
issues: readonly ArgIssue[],
|
|
47
47
|
audience: ErrorAudience,
|
|
48
48
|
): { readonly code: string; readonly result: McpToolResult } {
|
|
49
|
-
const invalid =
|
|
49
|
+
const invalid = invalidArgsError(name, issues);
|
|
50
|
+
return {
|
|
51
|
+
code: invalid.code,
|
|
52
|
+
result: { content: [{ type: 'text', text: invalid.format({ audience }) }], isError: true },
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A tool's arguments, refused against its published schema: the one `X_INPUT_INVALID` for that. */
|
|
57
|
+
export function invalidArgsError(name: string, issues: readonly ArgIssue[]): InputInvalidError {
|
|
58
|
+
return new InputInvalidError(
|
|
50
59
|
name,
|
|
51
60
|
formatIssues(issues).join('; '),
|
|
52
61
|
issues.map((issue) => ({
|
|
@@ -57,8 +66,4 @@ export function invalidArgsResult(
|
|
|
57
66
|
})),
|
|
58
67
|
'tool',
|
|
59
68
|
);
|
|
60
|
-
return {
|
|
61
|
-
code: invalid.code,
|
|
62
|
-
result: { content: [{ type: 'text', text: invalid.format({ audience }) }], isError: true },
|
|
63
|
-
};
|
|
64
69
|
}
|