@ultimat3/mcp 30.9.0 → 31.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 +6 -10
- package/README.md +50 -14
- package/package.json +12 -11
- package/src/adapter.ts +18 -0
- package/src/app-tools.ts +9 -1
- package/src/audit.ts +20 -6
- package/src/catalog-errors.ts +1 -1
- package/src/confirmation-approvers.ts +18 -0
- package/src/confirmation-decide.ts +6 -0
- package/src/confirmation-gate.ts +6 -1
- package/src/confirmation-installed.ts +34 -0
- package/src/confirmation-quorum-errors.ts +8 -3
- package/src/confirmations.ts +18 -8
- package/src/index.ts +19 -120
- package/src/meta-call.ts +6 -2
- package/src/meta-catalog-render.ts +195 -0
- package/src/meta-host.ts +3 -0
- package/src/meta-surface.ts +1 -140
- package/src/oauth-metadata.ts +12 -5
- package/src/projectable.ts +2 -2
- package/src/registry.ts +62 -5
- package/src/remote-lock.ts +7 -19
- package/src/remote-tools.ts +72 -9
- package/src/server-resources.ts +7 -2
- package/src/server-voice.ts +29 -0
- package/src/server.ts +14 -2
- package/src/tenant.ts +12 -20
- package/src/transport-http.ts +1 -0
package/CLAUDE.md
CHANGED
|
@@ -27,7 +27,7 @@ import. The CLI wires it.
|
|
|
27
27
|
| `transport-http.ts` | `POST /mcp` route descriptor, bearer → agent actor, and the per-caller rate limit it enforces itself; it keeps the order |
|
|
28
28
|
| `transport-http-answers.ts` | the pre-dispatch answers (401, 403 not-an-agent / origin, 421, 429) and the per-address failure meter |
|
|
29
29
|
| `audit-hook.ts` | `onAudit`; `resolveToken`'s 2nd arg is http's `RequestFacts` (`seen.address` only, never `x-forwarded-for`), re-exported as `McpRequestFacts` |
|
|
30
|
-
| `confirmations.ts` · `confirmation-*.ts` | `mcpConfirmations`: a factory over `action` (view/decide) · the gate on `handle` · stores · DDL · codes |
|
|
30
|
+
| `confirmations.ts` · `confirmation-*.ts` | `mcpConfirmations`: a factory over `action` (view/decide) · the gate on `handle` · stores · DDL · codes. `confirmation-installed.ts` is the `mcpConfirmationStore` slot a declaration with no `store` reads; `adapter.ts` (`./adapter`) its interface, error and installer |
|
|
31
31
|
| `oauth-metadata.ts` | RFC 9728 protected-resource metadata + the 401 `resource_metadata` challenge (`defineAppMcp({ oauth })`) |
|
|
32
32
|
| `oauth-server.ts` | `mcpAuthorizationServer`: `@ultimat3/auth`'s `authorizationServer` bound to one endpoint's `resource` (default and required by `resolveToken`), its `oauth` block, `mcp.auth.<event>` lines. No protocol rule lives here |
|
|
33
33
|
| `transport-stdio.ts` | NDJSON on stdin/stdout for `x mcp serve` |
|
|
@@ -40,7 +40,7 @@ import. The CLI wires it.
|
|
|
40
40
|
| `scopes.ts` | the `scopes:` map — outcome 2's declaration surface and its three boot refusals |
|
|
41
41
|
| `readonly-sql.ts` · `readonly-sql-calls.ts` | layer 3 of `db.query` — the single-read parse — and `db.migrate`'s branch check · the banned call families |
|
|
42
42
|
| `query-limits.ts` | layer 4 of `db.query` — the row, byte and timeout ceilings, and what truncation reports |
|
|
43
|
-
| `meta-surface.ts` | `surface: 'meta'` catalog
|
|
43
|
+
| `meta-surface.ts` · `meta-catalog-render.ts` | `surface: 'meta'` catalog, `groups:` boot checks (dispatch: `server.ts`) · its text, `catalogDetail` |
|
|
44
44
|
| `meta-split.ts` · `meta-split-call.ts` · `meta-host.ts` | `surface: 'meta-split'`: the three dispatchers, the effect boot check · their dispatch (wrong tool, preview, `confirmed_by_user`) · the server half both meta doors call |
|
|
45
45
|
| `list-params.ts` | `listParams` whitelist → the schema `manage_resource` enforces (flat keys: `status_eq`) |
|
|
46
46
|
| `meta-errors.ts` · `framework-error.ts` | meta boot refusals · a thrown value read and rendered (split from `server.ts`) |
|
|
@@ -162,13 +162,9 @@ import. The CLI wires it.
|
|
|
162
162
|
wrapper since 25.0.0) — the one answer shared with `action`, `query`, `ai`, `manifest`; the pin
|
|
163
163
|
lives in `@ultimat3/cli`. Never spell `=== true` inline here again.
|
|
164
164
|
- Exposure is declared at the primitive, never in `defineAppMcp`. A primitive NAMED in
|
|
165
|
-
`actions:`/`queries:` without `mcp: { expose: true }` is `X_MCP_TOOL_UNDECLARED` at boot
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
because that list is every primitive the app registered, not one anyone wrote out.
|
|
169
|
-
`actions:` and `queries:` go through **one** `toolsListed` call over the concatenation: it
|
|
170
|
-
collects every offender before throwing, so one boot names all of them and one edit closes
|
|
171
|
-
all of them. Two calls would throw on the first array and never examine the second.
|
|
165
|
+
`actions:`/`queries:` without `mcp: { expose: true }` is `X_MCP_TOOL_UNDECLARED` at boot;
|
|
166
|
+
`include: 'exposed'` sweeps the registries and skips. Both lists go through **one**
|
|
167
|
+
`toolsListed` call, so one boot names every offender. Why: `docs/history/mcp.md`.
|
|
172
168
|
- `actions:`/`queries:` take the **real primitives** (`actions: [publishPost]`), adapted by
|
|
173
169
|
`projectable.ts` into the same `ProjectablePrimitive` the sweep builds. `ProjectablePrimitive`
|
|
174
170
|
stays in the union for programmatic catalogs (`@ultimat3/admin`); `isAction`/`isQuery` read each
|
|
@@ -269,7 +265,7 @@ import. The CLI wires it.
|
|
|
269
265
|
## Commands
|
|
270
266
|
|
|
271
267
|
```
|
|
272
|
-
bun test packages/mcp
|
|
268
|
+
bun test ./packages/mcp/
|
|
273
269
|
bun run --filter @ultimat3/mcp typecheck
|
|
274
270
|
```
|
|
275
271
|
|
package/README.md
CHANGED
|
@@ -93,7 +93,7 @@ strand a well-behaved client.
|
|
|
93
93
|
| A predicate audience sees the caller and nothing else | it is handed `McpCaller` — never the call arguments, so two calls with different inputs cannot answer differently. Must return the literal `true`; if it throws, the tool is hidden |
|
|
94
94
|
| `tools/list` is answered per caller | filtered on every call against the caller the transport resolved — one per HTTP request, one per stdio connection — never a static catalog |
|
|
95
95
|
| Gate order | visibility → scope → arguments → policy; the scope gate never waits on a policy run against attacker-supplied input |
|
|
96
|
-
| Every outcome is audited | one line per `tools/call`; hidden/scope/policy at `warn`, ok, invalid-args and
|
|
96
|
+
| Every outcome is audited | one line per `tools/call`; hidden/scope/policy at `warn`, and so is `refused` — a tenant-scoped server's call that named no tenant, refused BEFORE dispatch — while ok, invalid-args, unconfirmed and previewed log at `info` — see `audit.ts`; the same decision reaches `onAudit` after the line. A tool that renders its OWN `isError` result may name the code it refused with (`McpToolResult.code`, audit-only, never on the wire) and is then classified by the same `outcomeForCode` a thrown error is — otherwise every self-rendered refusal lands in the `policy-denied` bucket a prober's name walk is alerted from |
|
|
97
97
|
| Audit lines carry no payload | tool, outcome, actor, code. Never arguments, never rows |
|
|
98
98
|
| No trusted-tool mode | there is no flag that skips policy evaluation |
|
|
99
99
|
|
|
@@ -240,7 +240,7 @@ defineAppMcp({
|
|
|
240
240
|
|
|
241
241
|
| Tool | Answers |
|
|
242
242
|
|---|---|
|
|
243
|
-
| `list_resources` | PLAIN TEXT (`As of 22.10`; JSON until then): one line per resource, and under it one line per action — ` previewPayoutBatch (action; confirms) {batchId: string, note?: string} — <description>`. Compact by rule, never by a character cut (`As of 2026-10`, #590): a scope every action of a resource shares — or every action of one kind, `query scope payouts:read; action scope payouts:write` — is said once on the resource line, and stays on each action otherwise; only `(action)` is tagged, untagged is a read-only query (from `destructive`, never the name); `oneLineParams` names every required field, then optional ones up to 4 fields / 100 characters, then `…`, and cuts an enum after 8 literals with `\|…`. The same catalog as data: `server.catalog(caller)` (`MetaResource[]`, per-action `scope` intact, `undefined` for a flat caller); the renderer is `renderCatalog` |
|
|
243
|
+
| `list_resources` | PLAIN TEXT (`As of 22.10`; JSON until then): one line per resource, and under it one line per action — ` previewPayoutBatch (action; confirms) {batchId: string, note?: string} — <description>`. Compact by rule, never by a character cut (`As of 2026-10`, #590): a scope every action of a resource shares — or every action of one kind, `query scope payouts:read; action scope payouts:write` — is said once on the resource line, and stays on each action otherwise; only `(action)` is tagged, untagged is a read-only query (from `destructive`, never the name); `oneLineParams` names every required field, then optional ones up to 4 fields / 100 characters, then `…`, and cuts an enum after 8 literals with `\|…`. The same catalog as data: `server.catalog(caller)` (`MetaResource[]`, per-action `scope` intact, `undefined` for a flat caller); the renderer is `renderCatalog` (`meta-catalog-render.ts`). `defineAppMcp({ catalogDetail: 'compact' })` — the app's one choice — keeps the resource lines and puts each resource's actions on ONE line of names grouped by kind tag (`listPosts; action: publishPost (confirms)`), descriptions and hints left to `describe_resource`; refused on a flat-only server (`X_MCP_SURFACE_INVALID`) |
|
|
244
244
|
| `describe_resource` | `{ resources: string[] }` (batched) → each action's full `inputSchema` |
|
|
245
245
|
| `manage_resource` | `{ resource, action, params }` → the flat tool's answer, byte for byte |
|
|
246
246
|
|
|
@@ -388,6 +388,22 @@ them as the tenant actor, and the call is authoritative:
|
|
|
388
388
|
A role or a runtime tool an agent must discover per tenant belongs on the person's actor, or in a
|
|
389
389
|
tool the agent reads after `list_tenants` (`docs`, `list_resources`).
|
|
390
390
|
|
|
391
|
+
**What carries into a tenant, and what `resolve` decides** (ruling, 2026-11-09):
|
|
392
|
+
|
|
393
|
+
| on the call's caller | where it comes from | narrowed per tenant by |
|
|
394
|
+
|---|---|---|
|
|
395
|
+
| `scopes`, MCP `role` | the TOKEN, unchanged in every tenant — one token, one grant | nothing: a scope the token lacks is `X_MCP_SCOPE_DENIED` in every tenant |
|
|
396
|
+
| `actor` — `roles`, `orgId`, everything the policy reads | `resolve`'s answer, which REPLACES the person's: nothing of the person's actor is merged in | `resolve`: answer fewer roles and the policy refuses (`X_FORBIDDEN`) |
|
|
397
|
+
|
|
398
|
+
So the app's `resolve` may narrow, and is the one place that does. Held by
|
|
399
|
+
`tenant-semantics.test.ts`.
|
|
400
|
+
|
|
401
|
+
**Only `tools/call` names a tenant.** `resources/read`, `resources/list`, `prompts/*` and
|
|
402
|
+
`tools/list` run as the PERSON: a `tenant.arg` sent in their params is ignored, `resolve` is never
|
|
403
|
+
asked, and a reader sees `caller.tenant === undefined` and no `orgId`. A resource that reads tenant
|
|
404
|
+
data therefore fails closed at the entity tenancy guard (`X_TENANCY_ACTOR_ORG_REQUIRED`). Tenant
|
|
405
|
+
data an agent must read belongs in a tool, which names its tenant.
|
|
406
|
+
|
|
391
407
|
```ts
|
|
392
408
|
import { agentActor } from '@ultimat3/core';
|
|
393
409
|
import { defineAppMcp } from '@ultimat3/mcp';
|
|
@@ -525,12 +541,12 @@ export const mcp = defineAppMcp({
|
|
|
525
541
|
|
|
526
542
|
`mcpServer({ onAudit })` · `defineAppMcp({ onAudit })` — the route reads its server's. One
|
|
527
543
|
audit PATH with a second destination: the `mcp.*` log line is written, then the same decision is
|
|
528
|
-
handed on. What an action or query DID still reaches core's `
|
|
544
|
+
handed on. What an action or query DID still reaches core's `auditSink` port (`surface: 'mcp'`);
|
|
529
545
|
`onAudit` carries what no `AuditRecord` can — the refusals before anything ran.
|
|
530
546
|
|
|
531
547
|
| `event.kind` | When | Carries |
|
|
532
548
|
|---|---|---|
|
|
533
|
-
| `tool-call` | every `tools/call` outcome (`ok`, `hidden`, `scope-denied`, `policy-denied`, `invalid-args`, `unconfirmed`, `failed`) | `tool`, `outcome`, `caller`, `scope?`, `code?` |
|
|
549
|
+
| `tool-call` | every `tools/call` outcome (`ok`, `hidden`, `scope-denied`, `refused`, `policy-denied`, `invalid-args`, `unconfirmed`, `previewed`, `failed`) — `refused` (at `warn`) is the pre-dispatch one: a `tenant:` server's call that named no tenant (`X_MCP_TENANT_REQUIRED`), answered before any tool ran. A `tenant.resolve` that throws or answers a non-agent is `X_MCP_TENANT_UNRESOLVED`, and lands in `failed` | `tool`, `outcome`, `caller`, `scope?`, `code?` |
|
|
534
550
|
| `resource-read` | every `resources/read` outcome | `uri`, `outcome`, `caller`, `scope?`, `code?` |
|
|
535
551
|
| `auth-refused` | a missing or rejected token, a non-agent actor, an address past its failure allowance, a cross-origin `Origin` (`origin-refused`, before the token is read) — also logged `mcp.auth.<reason>` at `warn` | `reason`, `status` (401/403/429), `facts` |
|
|
536
552
|
|
|
@@ -542,7 +558,12 @@ not awaited and cannot change an answer: one that throws or rejects is logged on
|
|
|
542
558
|
### OAuth discovery (RFC 9728)
|
|
543
559
|
|
|
544
560
|
`defineAppMcp({ oauth: { authorizationServers: ['https://www.example.com'] } })` (`As of 22.6.0`;
|
|
545
|
-
MCP authorization spec
|
|
561
|
+
MCP authorization spec, `As of 2026-11` served at modern `2026-07-28` and the legacy handshake
|
|
562
|
+
revisions `2025-11-25` / `2025-06-18` / `2025-03-26`, discovery not the same in each — RFC 9728
|
|
563
|
+
protected-resource metadata is required from `2025-06-18` on; `2025-11-25` made the 401's
|
|
564
|
+
`WWW-Authenticate` optional with a `.well-known` fallback (SEP-985) where `2025-06-18` required it;
|
|
565
|
+
`2025-03-26` defines none, its discovery being RFC 8414 authorization-server metadata at the
|
|
566
|
+
origin): every 401 carries
|
|
546
567
|
`WWW-Authenticate: Bearer realm="ultimate-mcp", resource_metadata="<origin>/.well-known/oauth-protected-resource<path>"`
|
|
547
568
|
(`error="invalid_token"` when a token was sent), and `route.protectedResource` carries the document
|
|
548
569
|
and the two paths it is served at — path-inserted per RFC 9728 §3.1 and the root — which
|
|
@@ -567,15 +588,11 @@ A factory over `action`, never a ninth primitive: what it returns IS the decisio
|
|
|
567
588
|
`audit: true` if asked — and `defineAppMcp({ confirmations })` gates the named tools with it.
|
|
568
589
|
|
|
569
590
|
```ts
|
|
570
|
-
import
|
|
571
|
-
import { defineAppMcp, mcpConfirmations, postgresConfirmationStore } from '@ultimat3/mcp';
|
|
572
|
-
|
|
573
|
-
declare const executor: PgExecutor;
|
|
591
|
+
import { defineAppMcp, mcpConfirmations } from '@ultimat3/mcp';
|
|
574
592
|
|
|
575
593
|
// apps/web/app/orders/actions/confirm-refunds.ts — register it like any action
|
|
576
594
|
export const confirmRefunds = mcpConfirmations({
|
|
577
595
|
tools: ['refundOrder'],
|
|
578
|
-
store: postgresConfirmationStore({ executor }), // memoryConfirmationStore() in a test
|
|
579
596
|
permission: 'order:refund',
|
|
580
597
|
ttlMs: 600_000, // DEFAULT_MCP_CONFIRMATION_TTL_MS
|
|
581
598
|
});
|
|
@@ -655,7 +672,7 @@ export const confirmRefunds = mcpConfirmations({
|
|
|
655
672
|
| `from` | `'role:<name>'` (an application role on the decider) or `'actor:<id>'`, a closed template type. Matched by the decision action's policy — the one authz path — for `approve` and `reject`; `view` stays `permission` + `check` |
|
|
656
673
|
| the requester never counts | a decider whose id is the row's `actorId` is `X_FORBIDDEN` (`mcp-confirmation.requester`), and so is an impersonated one (`onBehalfOf`). Only when `approvers` is declared: without it, the original single decision by anyone the policy admits holds |
|
|
657
674
|
| one approver, one vote | the store's conditional write refuses a second vote by the same id; the decision answers `X_MCP_CONFIRMATION_QUORUM_PENDING` naming it |
|
|
658
|
-
| partial | the row stays `pending` until `quorum` distinct votes; the agent's repeat answers `X_MCP_CONFIRMATION_QUORUM_PENDING` (`n of m`), nothing runs. One rejection rejects. `ttlMs` expires a partial row like any other |
|
|
675
|
+
| partial | the row stays `pending` until `quorum` distinct votes; the agent's repeat answers `X_MCP_CONFIRMATION_QUORUM_PENDING` (`n of m`; `meta.outstanding` names the stored selectors still able to vote), nothing runs. One rejection rejects. `ttlMs` expires a partial row like any other |
|
|
659
676
|
| refused at the call | a quorum that is not a whole number ≥ 1, an empty or malformed `from`, or more named `actor:` approvers than exist without the requester: `X_MCP_CONFIRMATION_QUORUM_INVALID`, no row. `evidence` with no `verify`: the same code at declaration |
|
|
660
677
|
| what is signed | `signingPayload` — `confirmationSigningPayload(row)`, canonical JSON of purpose, confirmation id, tool and the keyed argument digest — returned by `view`. The id makes a signature good for one row (a replay onto the next identical call binds another id); the digest for one set of arguments |
|
|
661
678
|
| at the decision | `approve` carries `signature`; missing or not `true` from `verify` is `X_MCP_CONFIRMATION_EVIDENCE_INVALID` and stores no vote. A `verify` that throws propagates: an outage, never a verdict |
|
|
@@ -668,6 +685,16 @@ answers `quorum`, `approvedBy` and `signingPayload` too. `McpConfirmationStore.d
|
|
|
668
685
|
signature as a fifth argument. Three columns are added by the boot (`alter table … add column if
|
|
669
686
|
not exists`): a row opened before them reads as one approval, no selectors.
|
|
670
687
|
|
|
688
|
+
### Where the rows live: the `mcpConfirmationStore` port
|
|
689
|
+
|
|
690
|
+
A declaration naming no `store` keeps its rows in the `mcpConfirmationStore` port, read at each
|
|
691
|
+
use (never when it is built — `runtime.ts` may import it before the boot installs the port): `runtime.ts`'s `mcpConfirmationStore` (installed by the boot before the app's modules
|
|
692
|
+
import), else `postgresConfirmationStore` over the app's pool (31.0 — `store` was required before).
|
|
693
|
+
`store: memoryConfirmationStore()` still wins, for a test. The interface, `McpConfirmationContestedError`
|
|
694
|
+
and the installer (`installConfirmationStore`, boot and `@ultimat3/testing` only) are
|
|
695
|
+
`@ultimat3/mcp/adapter`'s; the suite is `@ultimat3/testing/conformance`'s
|
|
696
|
+
`mcpConfirmationStoreConformance`.
|
|
697
|
+
|
|
671
698
|
## Resources
|
|
672
699
|
|
|
673
700
|
| URI | Contents |
|
|
@@ -755,7 +782,9 @@ lists resources per caller — what `extraRecipes` is built on.
|
|
|
755
782
|
| `mcpTools({ name, url, auth, allow, policy, lock })` | one `action` per allowed remote tool, named `<name>_<tool>`, under the APP's `resource:verb` permission (`policy: 'docs:search'` — a predicate over the remote's input would read nothing), rate limit and confirmation gate. `allow` is required; nothing is discovered at run time |
|
|
756
783
|
| `tools.lock.json` | what a person reviewed: per tool, the description, input schema and `readOnly` (a person sets it; `false` keeps the gate binding). `x mcp lock <name>` writes it; `--check` compares. An allowed tool the lock lacks refuses at declaration; a live drift is `X_MCP_REMOTE_TOOL_CHANGED` before the first call, and the `manifest` step of `x verify` runs the check for every declared server (an unreachable server is a warning) |
|
|
757
784
|
| `notifications/tools/list_changed` | heard on any response stream, and — for a legacy session — on the `GET` stream the client holds open for the server's own messages (one per session, detached from the request, `listenMs` long, default 5 min, re-opened by the next call; a `405` is a server without one, never asked again; `client.close()` ends it). The next call re-lists and is held to the lock again — a changed tool is refused, never trusted silently |
|
|
758
|
-
| output | `{ untrusted, isError }`: the remote's text inside `<untrusted source="mcp:<name>/<tool>">`, middle cut past `maxResultChars` |
|
|
785
|
+
| output | `{ untrusted, isError }`: the remote's text inside `<untrusted source="mcp:<name>/<tool>">`, middle cut past `maxResultChars`; invisible format characters (`\p{Cf}`) stripped, then every `<` (and full-width `<`, small `﹤`) written `<` — no tag-shaped pattern left to bypass |
|
|
786
|
+
| `expose` | default `false`: the actions are this app's, not its `/mcp`'s — `include: 'exposed'` leaves them out, while an in-app `agent()` is still offered them (`agents: true`). `expose: true` opts one remote server in, making this app's `/mcp` answer for a third party's tools |
|
|
787
|
+
| `reverifyMs` | default 3,600,000: one verdict against the live server stands this long (read on `clock`, default `systemClock`), then the next call re-lists and holds it to the lock again — a remote that changes and announces nothing is caught within the hour |
|
|
759
788
|
| `mcpClient({ name, url, dial, deadlineMs, auth })` | over core's `outbound()`; modern era first, the legacy handshake when the server refuses it for its era. A text-only result is a success. `X_MCP_REMOTE_REFUSED`'s `meta.failure` tells `transport` / `status` / `rpc` / `malformed` apart; a tool's own `isError` is a result. A 401 is `X_MCP_REMOTE_AUTH_REQUIRED` with `meta.resourceMetadata` |
|
|
760
789
|
| `mcpOAuthClient({ clientId, clientName, redirectUri, scopes? })` | the upstream's OAuth: `begin(serverUrl)` reads RFC 9728 then RFC 8414 metadata and answers `{ authorizeUrl, handshake }` (PKCE S256 from `@ultimat3/auth`, `resource` per RFC 8707); `exchange(handshake, { code, state })` and `refresh(handshake, token)` answer `{ accessToken, refreshToken, expiresIn }`. `clientId` is the HTTPS URL this app serves `metadata()` at (a client ID metadata document). Keep the handshake server-side; return the token from `auth` |
|
|
761
790
|
|
|
@@ -771,6 +800,9 @@ lists resources per caller — what `extraRecipes` is built on.
|
|
|
771
800
|
| `X_INPUT_INVALID` | arguments failed the published `inputSchema` — an `isError` result since 22.10 |
|
|
772
801
|
| `X_MCP_ARGS_INVALID` | no longer raised on the wire (22.10); `McpArgsInvalidError` stays exported |
|
|
773
802
|
| `X_MCP_SURFACE_OVER_BUDGET` | `assertMcpSurfaceBudget` measured a surface over its declared ceiling |
|
|
803
|
+
| `X_MCP_TENANT_REQUIRED` | a call on a `tenant:` server names no tenant (missing or empty `tenant.arg`); refused before dispatch, `list_tenants({})` names the values — `refused`, at `warn` |
|
|
804
|
+
| `X_MCP_TENANT_UNRESOLVED` | `tenant.resolve` threw or answered a non-agent actor — never "not a member", which is the hidden-tool answer |
|
|
805
|
+
| `X_MCP_TENANT_INVALID` | `tenant.arg` is not a plain name, or a tool's own input already declares it — at boot |
|
|
774
806
|
| `X_MCP_IDEMPOTENCY_KEY_SHADOWED` | an idempotent action's input declares `idempotencyKey`, the reserved retry-key argument |
|
|
775
807
|
| `X_MCP_PROTOCOL` | malformed envelope, unknown method, bad auth header |
|
|
776
808
|
| `X_MCP_PROTOCOL_UNSUPPORTED` | the request names a protocol version this server does not serve (`-32022`, 400) |
|
|
@@ -791,7 +823,7 @@ lists resources per caller — what `extraRecipes` is built on.
|
|
|
791
823
|
| `X_MCP_CONFIRMATION_TOOL_UNKNOWN` | `mcpConfirmations({ tools })` names a tool this server does not project, or none |
|
|
792
824
|
| `X_MCP_CONFIRMATION_CONTESTED` | identical concurrent calls kept racing one confirmation; retry |
|
|
793
825
|
| `X_MCP_CONFIRMATION_ARGUMENTS_MISMATCH` | an approval did not carry the arguments the agent sent (missing, different, or keyed before a rotation) |
|
|
794
|
-
| `X_MCP_CONFIRMATION_QUORUM_PENDING` | some distinct approvers approved and the quorum needs more; also a repeat vote by one who already counted |
|
|
826
|
+
| `X_MCP_CONFIRMATION_QUORUM_PENDING` | some distinct approvers approved and the quorum needs more; also a repeat vote by one who already counted. `meta.outstanding`: `string[]` of the stored `actor:<id>` / `role:<name>` selectors still able to vote — never the requester or an approver who already voted; `[]` when `from` is open |
|
|
795
827
|
| `X_MCP_CONFIRMATION_EVIDENCE_INVALID` | under `evidence`, a vote's signature is missing or does not verify, or the stored row no longer proves its approval |
|
|
796
828
|
| `X_MCP_CONFIRMATION_QUORUM_INVALID` | `approvers` answered a bar nobody can meet, or `evidence` declares no `verify` |
|
|
797
829
|
| `X_MCP_RATE_LIMITED` | the caller spent its per-minute allowance for this request's class. Its own code rather than `@ultimat3/http`'s `X_RATE_LIMITED` because the KNOB differs — `rateLimits` on the route, never `rateLimit.buckets` in `app.config.ts` |
|
|
@@ -808,7 +840,7 @@ a job boundary the class is gone and the `code` is what survives — match on th
|
|
|
808
840
|
| `McpBodyTooLargeError` | `X_MCP_BODY_TOO_LARGE` | `src/errors.ts` |
|
|
809
841
|
| `McpIdempotencyKeyShadowedError` | `X_MCP_IDEMPOTENCY_KEY_SHADOWED` | `src/errors.ts` |
|
|
810
842
|
| `McpConfirmationArgumentsMismatchError` | `X_MCP_CONFIRMATION_ARGUMENTS_MISMATCH` | `src/confirmation-errors.ts` |
|
|
811
|
-
| `McpConfirmationContestedError` | `X_MCP_CONFIRMATION_CONTESTED` | `src/confirmation-errors.ts` |
|
|
843
|
+
| `McpConfirmationContestedError` | `X_MCP_CONFIRMATION_CONTESTED` | `src/confirmation-errors.ts` — on `@ultimat3/mcp/adapter` only, a store author throws it |
|
|
812
844
|
| `McpConfirmationDecidedError` | `X_MCP_CONFIRMATION_DECIDED` | `src/confirmation-errors.ts` |
|
|
813
845
|
| `McpConfirmationEvidenceInvalidError` | `X_MCP_CONFIRMATION_EVIDENCE_INVALID` | `src/confirmation-quorum-errors.ts` |
|
|
814
846
|
| `McpConfirmationExpiredError` | `X_MCP_CONFIRMATION_EXPIRED` | `src/confirmation-errors.ts` |
|
|
@@ -830,12 +862,16 @@ a job boundary the class is gone and the `code` is what survives — match on th
|
|
|
830
862
|
| `McpSurfaceInvalidError` | `X_MCP_SURFACE_INVALID` | `src/meta-errors.ts` |
|
|
831
863
|
| `McpQueryRejectedError` | `X_MCP_QUERY_REJECTED` | `src/errors.ts` |
|
|
832
864
|
| `McpRateLimitedError` | `X_MCP_RATE_LIMITED` | `src/errors.ts` |
|
|
865
|
+
| `McpRemoteAuthRequiredError` | `X_MCP_REMOTE_AUTH_REQUIRED` | `src/remote-errors.ts` — catch it to start `mcpOAuthClient().begin(url)` |
|
|
833
866
|
| `McpResourceDuplicateError` | `X_MCP_RESOURCE_DUPLICATE` | `src/errors.ts` |
|
|
834
867
|
| `McpScopeConflictError` | `X_MCP_SCOPE_CONFLICT` | `src/errors.ts` |
|
|
835
868
|
| `McpScopeDeniedError` | `X_MCP_SCOPE_DENIED` | `src/errors.ts` |
|
|
836
869
|
| `McpScopeUncoveredError` | `X_MCP_SCOPE_UNCOVERED` | `src/errors.ts` |
|
|
837
870
|
| `McpScopeUnknownError` | `X_MCP_SCOPE_UNKNOWN` | `src/errors.ts` |
|
|
838
871
|
| `McpSurfaceOverBudgetError` | `X_MCP_SURFACE_OVER_BUDGET` | `src/errors.ts` |
|
|
872
|
+
| `McpTenantInvalidError` | `X_MCP_TENANT_INVALID` | `src/catalog-errors.ts` |
|
|
873
|
+
| `McpTenantRequiredError` | `X_MCP_TENANT_REQUIRED` | `src/catalog-errors.ts` |
|
|
874
|
+
| `McpTenantUnresolvedError` | `X_MCP_TENANT_UNRESOLVED` | `src/catalog-errors.ts` |
|
|
839
875
|
| `McpToolDuplicateError` | `X_MCP_TOOL_DUPLICATE` | `src/errors.ts` |
|
|
840
876
|
| `McpToolUndeclaredError` | `X_MCP_TOOL_UNDECLARED` | `src/errors.ts` |
|
|
841
877
|
| `McpToolUnknownError` | `X_MCP_TOOL_UNKNOWN` | `src/errors.ts` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/mcp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "31.1.0",
|
|
4
4
|
"description": "MCP server, dev tools, and the action-to-tool projection \u2014 one authz system, two surfaces",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"exports": {
|
|
27
27
|
".": "./src/index.ts",
|
|
28
|
+
"./adapter": "./src/adapter.ts",
|
|
28
29
|
"./schema": "./src/confirmation-schema.ts"
|
|
29
30
|
},
|
|
30
31
|
"files": [
|
|
@@ -43,15 +44,15 @@
|
|
|
43
44
|
"test": "bun test"
|
|
44
45
|
},
|
|
45
46
|
"dependencies": {
|
|
46
|
-
"@ultimat3/action": "
|
|
47
|
-
"@ultimat3/auth": "
|
|
48
|
-
"@ultimat3/core": "
|
|
49
|
-
"@ultimat3/db": "
|
|
50
|
-
"@ultimat3/entity": "
|
|
51
|
-
"@ultimat3/http": "
|
|
52
|
-
"@ultimat3/jobs": "
|
|
53
|
-
"@ultimat3/policy": "
|
|
54
|
-
"@ultimat3/query": "
|
|
55
|
-
"@ultimat3/schema": "
|
|
47
|
+
"@ultimat3/action": "31.1.0",
|
|
48
|
+
"@ultimat3/auth": "31.1.0",
|
|
49
|
+
"@ultimat3/core": "31.1.0",
|
|
50
|
+
"@ultimat3/db": "31.1.0",
|
|
51
|
+
"@ultimat3/entity": "31.1.0",
|
|
52
|
+
"@ultimat3/http": "31.1.0",
|
|
53
|
+
"@ultimat3/jobs": "31.1.0",
|
|
54
|
+
"@ultimat3/policy": "31.1.0",
|
|
55
|
+
"@ultimat3/query": "31.1.0",
|
|
56
|
+
"@ultimat3/schema": "31.1.0"
|
|
56
57
|
}
|
|
57
58
|
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// `@ultimat3/mcp/adapter`: the `mcpConfirmationStore` port (`ADAPTER_PORTS`) — the interface a store
|
|
2
|
+
// implements, the error it raises on a lost race, and the installer with its reset. Never on the `.`
|
|
3
|
+
// barrel: the boot (`apps/<app>/runtime.ts`) and `@ultimat3/testing` are the callers. The shipped
|
|
4
|
+
// stores (`memoryConfirmationStore`, `postgresConfirmationStore`) stay on `.`.
|
|
5
|
+
|
|
6
|
+
export { McpConfirmationContestedError } from './confirmation-errors';
|
|
7
|
+
export {
|
|
8
|
+
installConfirmationStore,
|
|
9
|
+
installedConfirmationStore,
|
|
10
|
+
resetConfirmationStore,
|
|
11
|
+
} from './confirmation-installed';
|
|
12
|
+
export type {
|
|
13
|
+
McpConfirmation,
|
|
14
|
+
McpConfirmationApproval,
|
|
15
|
+
McpConfirmationDraft,
|
|
16
|
+
McpConfirmationStatus,
|
|
17
|
+
McpConfirmationStore,
|
|
18
|
+
} from './confirmation-store';
|
package/src/app-tools.ts
CHANGED
|
@@ -25,6 +25,7 @@ import { toolsFrom, toolsListed } from './from-action';
|
|
|
25
25
|
import { noteIncluded } from './include-early';
|
|
26
26
|
import { jobStatusPrimitive } from './job-status-tool';
|
|
27
27
|
import { jobToolBrand } from './job-tool';
|
|
28
|
+
import type { McpCatalogDetail } from './meta-catalog-render';
|
|
28
29
|
import type { McpResourceGroups, McpSurfaceOption } from './meta-surface';
|
|
29
30
|
import type { McpOAuth } from './oauth-metadata';
|
|
30
31
|
import type { ListedPrimitive } from './projectable';
|
|
@@ -147,6 +148,12 @@ export interface DefineAppMcpInput<TSchemas extends AppToolSchemas = AppToolSche
|
|
|
147
148
|
* answers to is `X_MCP_GROUP_UNKNOWN`, a tool in two groups `X_MCP_GROUP_CONFLICT`.
|
|
148
149
|
*/
|
|
149
150
|
readonly groups?: McpResourceGroups;
|
|
151
|
+
/**
|
|
152
|
+
* How much of each action `list_resources` says — chosen once, here. `'full'` (the default):
|
|
153
|
+
* description and parameter hint per action. `'compact'`: names and tags only, the rest in
|
|
154
|
+
* `describe_resource` — for a catalog whose full listing outgrows a model's turn.
|
|
155
|
+
*/
|
|
156
|
+
readonly catalogDetail?: McpCatalogDetail;
|
|
150
157
|
/**
|
|
151
158
|
* `initialize`'s `instructions` — the advice a client that honours it puts in the model's context:
|
|
152
159
|
* the entry-point call, the three or four goals most sessions are, the traps. A string, or a
|
|
@@ -181,7 +188,7 @@ export interface DefineAppMcpInput<TSchemas extends AppToolSchemas = AppToolSche
|
|
|
181
188
|
/**
|
|
182
189
|
* Every gate decision as data — each `tools/call` and `resources/read` outcome and every refused
|
|
183
190
|
* token — after its `mcp.*` log line is written. What an action or query DID still goes to
|
|
184
|
-
* core's `
|
|
191
|
+
* core's `auditSink` port; this is what the gate decided, mostly before anything ran. A hook that
|
|
185
192
|
* throws changes no answer (`audit-hook.ts`).
|
|
186
193
|
*/
|
|
187
194
|
readonly onAudit?: McpAuditHook | undefined;
|
|
@@ -310,6 +317,7 @@ export function defineAppMcp<TSchemas extends AppToolSchemas>(
|
|
|
310
317
|
serverInfo: { name: input.name ?? 'ultimate-app', version: input.version ?? '0.0.0' },
|
|
311
318
|
...(input.surface === undefined ? {} : { surface: input.surface }),
|
|
312
319
|
...(input.groups === undefined ? {} : { groups: input.groups }),
|
|
320
|
+
...(input.catalogDetail === undefined ? {} : { catalogDetail: input.catalogDetail }),
|
|
313
321
|
...(input.instructions === undefined ? {} : { instructions: input.instructions }),
|
|
314
322
|
errorAudience: input.errorAudience ?? 'caller',
|
|
315
323
|
...(input.onAudit === undefined ? {} : { onAudit: input.onAudit }),
|
package/src/audit.ts
CHANGED
|
@@ -19,6 +19,12 @@ export type McpOutcome =
|
|
|
19
19
|
| 'hidden'
|
|
20
20
|
/** OUTCOME 2: visible, but the connection's token lacks the tool's scope. */
|
|
21
21
|
| 'scope-denied'
|
|
22
|
+
/**
|
|
23
|
+
* Refused before dispatch by a gate that is not the tool's policy: a tenant-scoped tool called
|
|
24
|
+
* without its tenant arg (`X_MCP_TENANT_REQUIRED`). The answer names the miss, so it is not a
|
|
25
|
+
* probe signal — but a string of them is still an agent misusing the surface: `warn`.
|
|
26
|
+
*/
|
|
27
|
+
| 'refused'
|
|
22
28
|
/** OUTCOME 3: ran, and the tool's own policy refused this input. */
|
|
23
29
|
| 'policy-denied'
|
|
24
30
|
/** Arguments failed the schema the agent was handed. Not an authz outcome. */
|
|
@@ -66,12 +72,14 @@ const DENIAL_CODES: ReadonlySet<string> = new Set([
|
|
|
66
72
|
* input this server already validated failing inside the action means the two have drifted, and
|
|
67
73
|
* that wants a human.
|
|
68
74
|
*/
|
|
69
|
-
const ARGUMENT_CODES: ReadonlySet<string> = new Set([
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
const ARGUMENT_CODES: ReadonlySet<string> = new Set(['X_ADMIN_INVALID', 'X_MCP_WRONG_TOOL']);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Refused by a gate that is not the tool's policy, before dispatch: a call on a `tenant` server that
|
|
79
|
+
* named no tenant. The answer names the miss (so it leaks nothing), but a run of them is an agent
|
|
80
|
+
* misusing the surface, not a client misreading a schema — `refused`, at `warn`.
|
|
81
|
+
*/
|
|
82
|
+
const GATE_CODES: ReadonlySet<string> = new Set(['X_MCP_TENANT_REQUIRED']);
|
|
75
83
|
|
|
76
84
|
/**
|
|
77
85
|
* `mcpConfirmations`' answers for a call no live approval covers. A rejection is NOT here: a human
|
|
@@ -89,6 +97,7 @@ const UNCONFIRMED_CODES: ReadonlySet<string> = new Set([
|
|
|
89
97
|
export function outcomeForCode(code: string): McpOutcome {
|
|
90
98
|
if (DENIAL_CODES.has(code)) return 'policy-denied';
|
|
91
99
|
if (UNCONFIRMED_CODES.has(code)) return 'unconfirmed';
|
|
100
|
+
if (GATE_CODES.has(code)) return 'refused';
|
|
92
101
|
return ARGUMENT_CODES.has(code) ? 'invalid-args' : 'failed';
|
|
93
102
|
}
|
|
94
103
|
|
|
@@ -138,6 +147,7 @@ const LEVEL: ReadonlyMap<McpOutcome, 'info' | 'warn' | 'error'> = new Map([
|
|
|
138
147
|
['ok', 'info'],
|
|
139
148
|
['hidden', 'warn'],
|
|
140
149
|
['scope-denied', 'warn'],
|
|
150
|
+
['refused', 'warn'],
|
|
141
151
|
['policy-denied', 'warn'],
|
|
142
152
|
['invalid-args', 'info'],
|
|
143
153
|
['unconfirmed', 'info'],
|
|
@@ -195,6 +205,10 @@ function callerFields(entry: McpResourceAuditEntry | McpAuditEntry): LogFields {
|
|
|
195
205
|
outcome: entry.outcome,
|
|
196
206
|
actor: entry.caller.actor.id,
|
|
197
207
|
actorKind: entry.caller.actor.kind,
|
|
208
|
+
// Which org the call ran for — the scoped identity, whatever rebuilt it. On a per-call
|
|
209
|
+
// tenant option this is the one field that makes a mixed-tenant log sortable by renter.
|
|
210
|
+
// Spread-gated like `role`: an actor with no org carries no field, not a null one.
|
|
211
|
+
...(entry.caller.actor.orgId === undefined ? {} : { orgId: entry.caller.actor.orgId }),
|
|
198
212
|
...(entry.caller.role === undefined ? {} : { role: entry.caller.role }),
|
|
199
213
|
// The tenant a `tenant` server resolved for this call — the decision's subject, not an argument.
|
|
200
214
|
...(entry.caller.tenant === undefined ? {} : { tenant: entry.caller.tenant }),
|
package/src/catalog-errors.ts
CHANGED
|
@@ -65,7 +65,7 @@ export class McpTenantRequiredError extends UltimateError {
|
|
|
65
65
|
super({
|
|
66
66
|
code: 'X_MCP_TENANT_REQUIRED',
|
|
67
67
|
cause: `this server acts every call in the tenant the call names, and this call's arguments carry no "${input.arg}"`,
|
|
68
|
-
fix:
|
|
68
|
+
fix: 'list_tenants({}) — then send one of its values as the tenant argument of every tools/call',
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -74,6 +74,24 @@ export function resolveApprovers(
|
|
|
74
74
|
return Object.freeze({ quorum, from: Object.freeze([...new Set(from)]) });
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* The stored selectors that can still add a vote: an `actor:<id>` who has not voted and is not the
|
|
79
|
+
* requester, and every `role:<name>` as stored (who holds a role is only known at decision time, so
|
|
80
|
+
* it is named, never expanded). `from: null` names nobody — "any decider the policy admits" has no
|
|
81
|
+
* list to owe. The pending answer carries this so the agent knows whom to ask.
|
|
82
|
+
*/
|
|
83
|
+
export function outstandingApprovers(
|
|
84
|
+
from: readonly string[] | null,
|
|
85
|
+
voted: readonly string[],
|
|
86
|
+
requester: string,
|
|
87
|
+
): string[] {
|
|
88
|
+
if (from === null) return [];
|
|
89
|
+
const counted = new Set([...voted, requester]);
|
|
90
|
+
return from.filter(
|
|
91
|
+
(selector) => !(selector.startsWith('actor:') && counted.has(selector.slice(6))),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
77
95
|
/** Does `actor` match one of a row's stored selectors? `null` selectors admit every decider. */
|
|
78
96
|
export function matchesApprover(actor: Actor, from: readonly string[] | null): boolean {
|
|
79
97
|
if (from === null) return true;
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import type { Clock, SealKeySource, UltimateError } from '@ultimat3/core';
|
|
7
7
|
import { compareFingerprint, openText } from '@ultimat3/core';
|
|
8
8
|
import type { JsonValue } from '@ultimat3/schema';
|
|
9
|
+
import { outstandingApprovers } from './confirmation-approvers';
|
|
9
10
|
import {
|
|
10
11
|
McpConfirmationArgumentsMismatchError,
|
|
11
12
|
McpConfirmationDecidedError,
|
|
@@ -97,6 +98,11 @@ function repeatRefusal(row: McpConfirmation, deciderId: string): UltimateError |
|
|
|
97
98
|
approvals: row.approvals.length,
|
|
98
99
|
quorum: row.quorum,
|
|
99
100
|
repeated: deciderId,
|
|
101
|
+
outstanding: outstandingApprovers(
|
|
102
|
+
row.approvers,
|
|
103
|
+
row.approvals.map((vote) => vote.approver),
|
|
104
|
+
row.actorId,
|
|
105
|
+
),
|
|
100
106
|
});
|
|
101
107
|
}
|
|
102
108
|
|
package/src/confirmation-gate.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { ToolGate } from '@ultimat3/action';
|
|
|
7
7
|
import { toolGateFor } from '@ultimat3/action';
|
|
8
8
|
import type { Actor, UltimateError } from '@ultimat3/core';
|
|
9
9
|
import { keyedFingerprint, seal, uuidV7 } from '@ultimat3/core';
|
|
10
|
-
import { resolveApprovers } from './confirmation-approvers';
|
|
10
|
+
import { outstandingApprovers, resolveApprovers } from './confirmation-approvers';
|
|
11
11
|
import {
|
|
12
12
|
McpConfirmationContestedError,
|
|
13
13
|
McpConfirmationExpiredError,
|
|
@@ -200,5 +200,10 @@ function waiting(row: McpConfirmation, decider: string): UltimateError {
|
|
|
200
200
|
...subject,
|
|
201
201
|
approvals: row.approvals.length,
|
|
202
202
|
quorum: row.quorum,
|
|
203
|
+
outstanding: outstandingApprovers(
|
|
204
|
+
row.approvers,
|
|
205
|
+
row.approvals.map((vote) => vote.approver),
|
|
206
|
+
row.actorId,
|
|
207
|
+
),
|
|
203
208
|
});
|
|
204
209
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Single responsibility: the `mcpConfirmationStore` port's slot — where `mcpConfirmations()` keeps
|
|
2
|
+
// its pending rows when the app names no store. The boot installs `runtime.ts`'s; with none
|
|
3
|
+
// installed it is the env default, `postgresConfirmationStore` over the app's pool.
|
|
4
|
+
|
|
5
|
+
import { adapterSlot } from '@ultimat3/core/adapter';
|
|
6
|
+
import { dbExecutor } from '@ultimat3/db';
|
|
7
|
+
import { postgresConfirmationStore } from './confirmation-postgres';
|
|
8
|
+
import type { McpConfirmationStore } from './confirmation-store';
|
|
9
|
+
|
|
10
|
+
/** Core's one slot shape (`adapter-slot.ts`): installs stack, each release removes its own frame. */
|
|
11
|
+
const stores = /* @__PURE__ */ adapterSlot<McpConfirmationStore | undefined>(() => undefined);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The `mcpConfirmationStore` port's installer — on `@ultimat3/mcp/adapter` only: the boot calls it
|
|
15
|
+
* with `runtime.ts`'s `mcpConfirmationStore`. Answers the release that removes THIS install alone.
|
|
16
|
+
*/
|
|
17
|
+
export function installConfirmationStore(store: McpConfirmationStore): () => void {
|
|
18
|
+
return stores.install(store);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What a declaration naming no `store` reads at EACH use — never captured, because `runtime.ts`
|
|
23
|
+
* may import the declaration before the boot installs this port. Nothing installed: Postgres over the ambient pool — the store
|
|
24
|
+
* every app wrote by hand before 31.0.0 (`postgresConfirmationStore({ executor: dbExecutor() })`);
|
|
25
|
+
* building it dials nothing.
|
|
26
|
+
*/
|
|
27
|
+
export function installedConfirmationStore(): McpConfirmationStore {
|
|
28
|
+
return stores.current() ?? postgresConfirmationStore({ executor: dbExecutor() });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Test seam: every install dropped. On `./adapter`, beside the installer. */
|
|
32
|
+
export function resetConfirmationStore(): void {
|
|
33
|
+
stores.reset();
|
|
34
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// an `approvers` declaration no approver set could ever meet. Split from `confirmation-errors.ts`
|
|
4
4
|
// for its ceiling; each `cause` names the confirmation id, the one fact every reader needs.
|
|
5
5
|
|
|
6
|
-
import { UltimateError } from '@ultimat3/core';
|
|
6
|
+
import { singleLine, UltimateError } from '@ultimat3/core';
|
|
7
7
|
|
|
8
8
|
interface Subject {
|
|
9
9
|
readonly id: string;
|
|
@@ -21,23 +21,28 @@ export class McpConfirmationQuorumPendingError extends UltimateError {
|
|
|
21
21
|
readonly quorum: number;
|
|
22
22
|
/** Set when THIS approver had already counted: a second vote is never a second approver. */
|
|
23
23
|
readonly repeated?: string | undefined;
|
|
24
|
+
/** The stored selectors still able to vote (`outstandingApprovers`); empty names nobody. */
|
|
25
|
+
readonly outstanding?: readonly string[] | undefined;
|
|
24
26
|
},
|
|
25
27
|
) {
|
|
28
|
+
const outstanding = (subject.outstanding ?? []).map(singleLine);
|
|
29
|
+
const owed = outstanding.length === 0 ? '' : ` (${outstanding.join(', ')})`;
|
|
26
30
|
const already =
|
|
27
31
|
subject.repeated === undefined
|
|
28
32
|
? ''
|
|
29
33
|
: `; ${subject.repeated} has already approved it, and one approver counts once`;
|
|
30
34
|
super({
|
|
31
35
|
code: 'X_MCP_CONFIRMATION_QUORUM_PENDING',
|
|
32
|
-
cause: `confirmation ${subject.id} for ${subject.tool} has ${subject.approvals} of the ${subject.quorum} distinct approvals it needs${already}; it stays open until ${until(subject)}`,
|
|
36
|
+
cause: `confirmation ${subject.id} for ${subject.tool} has ${subject.approvals} of the ${subject.quorum} distinct approvals it needs${already}${outstanding.length === 0 ? '' : `; still able to approve: ${outstanding.join(', ')}`}; it stays open until ${until(subject)}`,
|
|
33
37
|
fix: `confirmations({ id, decision: 'view' }) # as ANOTHER approver the quorum names, with id ${subject.id}; then confirmations({ id, decision: 'approve', arguments }) with exactly what view returned, and the agent repeats the same tools/call before ${until(subject)}`,
|
|
34
|
-
callerFix: `ask ${subject.quorum - subject.approvals} more person(s) who may approve it to view confirmation ${subject.id} and approve it, then call ${subject.tool} again with exactly the same arguments before ${until(subject)}`,
|
|
38
|
+
callerFix: `ask ${subject.quorum - subject.approvals} more person(s) who may approve it${owed} to view confirmation ${subject.id} and approve it, then call ${subject.tool} again with exactly the same arguments before ${until(subject)}`,
|
|
35
39
|
meta: {
|
|
36
40
|
confirmation: subject.id,
|
|
37
41
|
tool: subject.tool,
|
|
38
42
|
expiresAt: until(subject),
|
|
39
43
|
approvals: subject.approvals,
|
|
40
44
|
quorum: subject.quorum,
|
|
45
|
+
outstanding,
|
|
41
46
|
},
|
|
42
47
|
});
|
|
43
48
|
}
|
package/src/confirmations.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { decideConfirmation } from './confirmation-decide';
|
|
|
19
19
|
import { McpConfirmationToolUnknownError } from './confirmation-errors';
|
|
20
20
|
import type { McpConfirmationEvidence } from './confirmation-evidence';
|
|
21
21
|
import { confirmationToolGate } from './confirmation-gate';
|
|
22
|
+
import { installedConfirmationStore } from './confirmation-installed';
|
|
22
23
|
import { McpConfirmationQuorumInvalidError } from './confirmation-quorum-errors';
|
|
23
24
|
import type { McpConfirmation, McpConfirmationStore } from './confirmation-store';
|
|
24
25
|
|
|
@@ -72,8 +73,11 @@ export interface McpConfirmationsInput<TTools extends string = string> {
|
|
|
72
73
|
* `agent({ tools })`. Each must be one the server projects.
|
|
73
74
|
*/
|
|
74
75
|
readonly tools: readonly TTools[];
|
|
75
|
-
/**
|
|
76
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Where pending rows live. Omitted: the `mcpConfirmationStore` port — what `runtime.ts`
|
|
78
|
+
* installed, else Postgres over the app's pool. `memoryConfirmationStore()` for a test.
|
|
79
|
+
*/
|
|
80
|
+
readonly store?: McpConfirmationStore | undefined;
|
|
77
81
|
/** What the deciding actor must hold — the action's permission, read by `can()`. */
|
|
78
82
|
readonly permission: KnownPermission;
|
|
79
83
|
/**
|
|
@@ -124,7 +128,6 @@ export interface McpConfirmations<TTools extends string = string>
|
|
|
124
128
|
* // apps/web/app/orders/actions/confirm-refunds.ts
|
|
125
129
|
* export const confirmRefunds = mcpConfirmations({
|
|
126
130
|
* tools: ['refundOrder'],
|
|
127
|
-
* store: postgresConfirmationStore({ executor }),
|
|
128
131
|
* permission: 'order:refund',
|
|
129
132
|
* });
|
|
130
133
|
* // apps/web/app/mcp.ts
|
|
@@ -143,7 +146,10 @@ export function mcpConfirmations<const TTools extends string>(
|
|
|
143
146
|
1,
|
|
144
147
|
);
|
|
145
148
|
const clock = input.clock ?? systemClock;
|
|
146
|
-
const {
|
|
149
|
+
const { check, approvers, evidence } = input;
|
|
150
|
+
// Read per USE, never captured: `runtime.ts` may import this declaration before the boot installs
|
|
151
|
+
// `runtime.mcpConfirmationStore`, and a captured default would keep rows in the wrong store.
|
|
152
|
+
const storeOf = (): McpConfirmationStore => input.store ?? installedConfirmationStore();
|
|
147
153
|
const sealKeys: SealKeySource = input.sealKeys ?? {};
|
|
148
154
|
if (approvers !== undefined && typeof approvers !== 'function') {
|
|
149
155
|
throw new McpConfirmationQuorumInvalidError({
|
|
@@ -170,20 +176,24 @@ export function mcpConfirmations<const TTools extends string>(
|
|
|
170
176
|
return standing ?? (check ?? sameTenant)(args);
|
|
171
177
|
}),
|
|
172
178
|
...(input.audit === true ? { audit: true } : {}),
|
|
173
|
-
row: async ({ input: asked }) => (await
|
|
179
|
+
row: async ({ input: asked }) => (await storeOf().get(asked.id)) ?? null,
|
|
174
180
|
// The view and the decision: state only — the policy above has already decided who.
|
|
175
181
|
handle: ({ input: asked, ctx }) =>
|
|
176
|
-
decideConfirmation({ store, clock, sealKeys, evidence }, asked, ctx.actor.id),
|
|
182
|
+
decideConfirmation({ store: storeOf(), clock, sealKeys, evidence }, asked, ctx.actor.id),
|
|
177
183
|
});
|
|
178
|
-
const
|
|
184
|
+
const declared = Object.assign(decide, {
|
|
179
185
|
tools: Object.freeze([...input.tools]),
|
|
180
|
-
store,
|
|
181
186
|
ttlMs,
|
|
182
187
|
clock,
|
|
183
188
|
sealKeys,
|
|
184
189
|
approvers,
|
|
185
190
|
evidence,
|
|
186
191
|
});
|
|
192
|
+
// A getter, so the gate (`confirmation-gate.ts`) asks the port at each call too.
|
|
193
|
+
const confirmations: McpConfirmations<TTools> = Object.defineProperty(declared, 'store', {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
get: storeOf,
|
|
196
|
+
}) as typeof declared & { readonly store: McpConfirmationStore };
|
|
187
197
|
// On the declaration, so there is no `agent({ confirmations })` for an author to forget. A worker
|
|
188
198
|
// holds it only if it imports this module, which is `X_ROLE_LOAD_INCOMPLETE` when it would not.
|
|
189
199
|
registerToolGate(confirmations.tools, confirmationToolGate(confirmations));
|