@cloudhal/mcp-server 0.6.4 → 0.7.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/README.md CHANGED
@@ -18,6 +18,16 @@ import { createCloudhalMcpHandler } from "@cloudhal/mcp-server/http"; // fet
18
18
 
19
19
  `tools.ts` is SDK-independent (`defineTools()`, `runTool()` = policy → audit → handler) and is what the tests exercise first; `test/e2e.test.ts` runs a real client ↔ server pair over the in-memory transport and proves the Sprint 5 exit criterion.
20
20
 
21
+ ## Authentication
22
+
23
+ `@cloudhal/mcp-server` never authenticates a caller itself: the host resolves a `Principal` and hands it to `createCloudhalMcpHandler(request, principal)`. When `@cloudhal/api` mounts `/mcp` (the admin Worker and `cloudhal studio`), a call carries one of:
24
+
25
+ - **A Studio session cookie** (same-origin only; `/mcp` refuses any other `Origin`).
26
+ - **An agent key:** `Authorization: Bearer <key>` or `x-api-key: <key>`. Its scopes are narrowed to its creator's current role on every call (RFC-0005, ADR-0011).
27
+ - **An OAuth access token** (RFC-0044, ADR-0045). The deployment is its own OAuth 2.1 authorization server at `<origin>/api/auth`. Clients find it through the `401` challenge (`WWW-Authenticate: Bearer resource_metadata=…, scope=…`) and the RFC 9728 document at `/.well-known/oauth-protected-resource/mcp`. They register with a Client ID Metadata Document or, as a fallback, dynamic client registration. They authorize with PKCE (S256) after a member approves on Studio's consent screen, and present an RFC 9068 JWT bound to the `/mcp` resource (`aud`). `/mcp` verifies it locally and reads the member's live connection (`agent_grants`) on every call: the connection's scopes narrowed to the member's role, its mode, its revocation. The token yields the same `Principal` an agent key does, so policy, approvals, modes, elevations and the audit log treat both alike. A call whose only missing permission is a scope the member's role holds gets a `403` with `error="insufficient_scope"` (step-up). DPoP is refused in v1.
28
+
29
+ The playground and the local stdio server (`cloudhal mcp`) take none of these: a playground has one fixed principal, and stdio is the person's own machine. For members, connecting clients by URL is in [docs/guides/mcp-oauth.md](../../docs/guides/mcp-oauth.md); the per-client setup is in [docs/agents/clients.md](../../docs/agents/clients.md).
30
+
21
31
  ## Tool reference
22
32
 
23
33
  <!-- tools:start (generated by scripts/mcp-tools-doc.ts; do not edit) -->
@@ -6,6 +6,7 @@
6
6
  import {
7
7
 
8
8
 
9
+ agentCredential,
9
10
  ELEVATION_MAX_MINUTES,
10
11
  ELEVATION_MIN_MINUTES,
11
12
  ELEVATION_NOTICE_MINUTES,
@@ -80,7 +81,9 @@ function cannotRequest(ctx ) {
80
81
  return "an automation's key works in the mode its automation allows; ask a person to change the automation";
81
82
  if (ctx.principal.local)
82
83
  return "this is a local server: restart `cloudhal mcp` with the --mode you need";
83
- if (!ctx.principal.keyId) return "elevations apply to API keys; this principal has none";
84
+ // An OAuth connection (RFC-0044) has no elevations of its own yet (Phase 3).
85
+ if (agentCredential(ctx.principal)?.kind !== "key")
86
+ return "elevations apply to API keys; this principal has none";
84
87
  if (!ctx.approvals) return "this server has no approvals, so nobody could grant it";
85
88
  return undefined;
86
89
  }
@@ -0,0 +1,10 @@
1
+ import type { Release } from "@cloudhal/core-contracts";
2
+ import { type ToolContext } from "./common.js";
3
+ /** What `set_embargo` answers an OAuth connection on live content without out-of-band approvals. */
4
+ export declare const NO_GRANT_GATES = "The embargo is saved, but an OAuth connection cannot put it live on its own: the content stays visible until a person publishes it (or an agent key sets the embargo).";
5
+ /** What `set_embargo` answers an OAuth connection on live content: a person approves the gate. */
6
+ export declare const GRANT_GATE_PENDING = "The embargo is saved. A person must approve putting it live in Cloudhal Studio (site \u2192 Activity \u2192 Approvals); until then the content stays visible. Do not call set_embargo again for it.";
7
+ /** The agent key of a call that passed `autoReleaseVerdict` (which refuses anything else). */
8
+ export declare function keyOf(ctx: ToolContext): string;
9
+ /** The approval, or `undefined` when the caller is an OAuth connection (it waits for a person). */
10
+ export declare function directApproval(ctx: ToolContext, at: string): Release["approval"] | undefined;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The approval a gate change (RFC-0008 §6.4) carries when no request is needed: the person's, or
3
+ * an agent key's `auto` approval for a reduction. An OAuth connection (RFC-0044) is an agent too,
4
+ * never the person behind it, and has no `auto` approval of its own (the release contract's
5
+ * `auto` approval names a key), so its gate change is filed for a person to approve.
6
+ */
7
+ import { agentCredential } from "@cloudhal/agent-runtime";
8
+
9
+ import { canonicalInstant } from "@cloudhal/schedules";
10
+ import { ToolError } from "./common.js";
11
+ import { personApproval } from "./schedules.js";
12
+
13
+ /** What `set_embargo` answers an OAuth connection on live content without out-of-band approvals. */
14
+ export const NO_GRANT_GATES =
15
+ "The embargo is saved, but an OAuth connection cannot put it live on its own: the content stays visible until a person publishes it (or an agent key sets the embargo).";
16
+
17
+ /** What `set_embargo` answers an OAuth connection on live content: a person approves the gate. */
18
+ export const GRANT_GATE_PENDING =
19
+ "The embargo is saved. A person must approve putting it live in Cloudhal Studio (site → Activity → Approvals); until then the content stays visible. Do not call set_embargo again for it.";
20
+
21
+ /** The agent key of a call that passed `autoReleaseVerdict` (which refuses anything else). */
22
+ export function keyOf(ctx ) {
23
+ const credential = agentCredential(ctx.principal);
24
+ if (credential?.kind !== "key")
25
+ throw new ToolError("denied", "auto-mode releases need an API key");
26
+ return credential.id;
27
+ }
28
+
29
+ /** The approval, or `undefined` when the caller is an OAuth connection (it waits for a person). */
30
+ export function directApproval(ctx , at ) {
31
+ const credential = agentCredential(ctx.principal);
32
+ if (credential && !ctx.confirmed) {
33
+ if (credential.kind !== "key") return undefined;
34
+ return {
35
+ kind: "auto",
36
+ by: ctx.principal.actor,
37
+ keyId: credential.id,
38
+ at,
39
+ windowEnds: canonicalInstant(Date.parse(at) + 24 * 3_600_000),
40
+ };
41
+ }
42
+ return personApproval(ctx.principal, at);
43
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The gate helpers of the schedule tools (RFC-0008 §7.6, §10.8): the article or page a gate tool
3
+ * names, the own-work check, the gate write (CAS on `rev`, then a snapshot) and whether a working
4
+ * copy differs from the live version only in its gates. Split from `schedule-tools.ts` (size
5
+ * ratchet).
6
+ */
7
+ import type { ContentEntry, Page, Site } from "@cloudhal/core-contracts";
8
+ import { type ToolContext } from "./common.js";
9
+ export type GatedNode = (ContentEntry | Page) & {
10
+ kind: "contentEntry" | "page";
11
+ };
12
+ export declare function gatedNode(ctx: ToolContext, siteId: string, nodeId: string): Promise<GatedNode>;
13
+ export declare function assertOwnNode(ctx: ToolContext, node: GatedNode): Promise<void>;
14
+ /** Writes new gates on the working copy (CAS on `expectedRev`) and snapshots, like any edit. */
15
+ export declare function writeGates(ctx: ToolContext, node: GatedNode, availability: ContentEntry["availability"], expectedRev: number): Promise<ContentEntry | Page>;
16
+ /** True when the working copy differs from the live version only in its gates (a gate-only change). */
17
+ export declare function gateOnly(ctx: ToolContext, site: Site, node: ContentEntry | Page): Promise<boolean>;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * The gate helpers of the schedule tools (RFC-0008 §7.6, §10.8): the article or page a gate tool
3
+ * names, the own-work check, the gate write (CAS on `rev`, then a snapshot) and whether a working
4
+ * copy differs from the live version only in its gates. Split from `schedule-tools.ts` (size
5
+ * ratchet).
6
+ */
7
+
8
+ import { canonicalJson } from "@cloudhal/core-ts";
9
+ import { ACTOR_OWNERSHIP, callerPerson, ToolError } from "./common.js";
10
+ import { liveRefs } from "./schedules.js";
11
+
12
+
13
+
14
+ export async function gatedNode(
15
+ ctx ,
16
+ siteId ,
17
+ nodeId ,
18
+ ) {
19
+ const entry = await ctx.store.getContentEntry(nodeId);
20
+ if (entry && entry.siteId === siteId) return { ...entry, kind: "contentEntry" };
21
+ const page = await ctx.store.getPage(nodeId);
22
+ if (page && page.siteId === siteId) return { ...page, kind: "page" };
23
+ throw new ToolError("not_found", `no article or page ${nodeId} on this site`);
24
+ }
25
+
26
+ export async function assertOwnNode(ctx , node ) {
27
+ if (ctx.limit?.target !== "release") return;
28
+ const owned =
29
+ node.kind === "contentEntry" &&
30
+ (await (ctx.ownership ?? ACTOR_OWNERSHIP).personOf((node ).author)) ===
31
+ callerPerson(ctx);
32
+ if (!owned) throw new ToolError("denied", "your role schedules only your own articles");
33
+ }
34
+
35
+ /** Writes new gates on the working copy (CAS on `expectedRev`) and snapshots, like any edit. */
36
+ export async function writeGates(
37
+ ctx ,
38
+ node ,
39
+ availability ,
40
+ expectedRev ,
41
+ ) {
42
+ if (node.rev !== expectedRev)
43
+ throw new ToolError(
44
+ "conflict",
45
+ `${node.id} is at rev ${node.rev}, you sent ${expectedRev}; read it again`,
46
+ );
47
+ const { kind: _k, availability: _a, ...rest } = node;
48
+ const next = { ...rest, ...(availability ? { availability } : {}) };
49
+ const saved =
50
+ node.kind === "contentEntry"
51
+ ? await ctx.store.updateContentEntry(next )
52
+ : await ctx.store.updatePage(next );
53
+ await ctx.store.createSnapshot(node.siteId, {
54
+ actor: ctx.principal.actor,
55
+ reason: "agentMutation",
56
+ label: `gates by ${ctx.principal.actor.name ?? ctx.principal.actor.id}`,
57
+ });
58
+ return saved;
59
+ }
60
+
61
+ /** True when the working copy differs from the live version only in its gates (a gate-only change). */
62
+ export async function gateOnly(
63
+ ctx ,
64
+ site ,
65
+ node ,
66
+ ) {
67
+ const ref = (await liveRefs(ctx.store, site)).find((r) => r.id === node.id);
68
+ if (!ref) return false;
69
+ const live = (await ctx.store.readSnapshotNode(ref).catch(() => undefined))
70
+
71
+ ;
72
+ if (!live) return false;
73
+ const strip = (n ) => {
74
+ const { availability: _a, rev: _r, updatedAt: _u, ...rest } = n;
75
+ return canonicalJson(rest);
76
+ };
77
+ return strip(live) === strip(node);
78
+ }
@@ -33,7 +33,7 @@ import { effectiveMode, holds } from "@cloudhal/agent-runtime";
33
33
 
34
34
 
35
35
 
36
- import { canonicalJson, gateChanges } from "@cloudhal/core-ts";
36
+ import { gateChanges } from "@cloudhal/core-ts";
37
37
  import {
38
38
  budgetLeft,
39
39
  canonicalInstant,
@@ -62,6 +62,8 @@ import {
62
62
 
63
63
  ToolError,
64
64
  } from "./common.js";
65
+ import { directApproval, GRANT_GATE_PENDING, keyOf, NO_GRANT_GATES } from "./gate-approval.js";
66
+ import { assertOwnNode, gatedNode, gateOnly, writeGates } from "./schedule-gates.js";
65
67
  import {
66
68
  approvalProblem,
67
69
  autoReleaseVerdict,
@@ -306,7 +308,7 @@ async function decideRelease(
306
308
  release: await scheduleWith(ctx, record, when, {
307
309
  kind: "auto",
308
310
  by: ctx.principal.actor,
309
- keyId: ctx.principal.keyId ,
311
+ keyId: keyOf(ctx),
310
312
  at,
311
313
  windowEnds: verdict.windowEnds,
312
314
  }),
@@ -435,66 +437,6 @@ async function releaseFor(
435
437
 
436
438
  // ───────────────────────────── gates (§7.6, §10.8) ─────────────────────────────
437
439
 
438
-
439
-
440
- async function gatedNode(ctx , siteId , nodeId ) {
441
- const entry = await ctx.store.getContentEntry(nodeId);
442
- if (entry && entry.siteId === siteId) return { ...entry, kind: "contentEntry" };
443
- const page = await ctx.store.getPage(nodeId);
444
- if (page && page.siteId === siteId) return { ...page, kind: "page" };
445
- throw new ToolError("not_found", `no article or page ${nodeId} on this site`);
446
- }
447
-
448
- async function assertOwnNode(ctx , node ) {
449
- if (ctx.limit?.target !== "release") return;
450
- const owned =
451
- node.kind === "contentEntry" &&
452
- (await (ctx.ownership ?? ACTOR_OWNERSHIP).personOf((node ).author)) ===
453
- callerPerson(ctx);
454
- if (!owned) throw new ToolError("denied", "your role schedules only your own articles");
455
- }
456
-
457
- /** Writes new gates on the working copy (CAS on `expectedRev`) and snapshots, like any edit. */
458
- async function writeGates(
459
- ctx ,
460
- node ,
461
- availability ,
462
- expectedRev ,
463
- ) {
464
- if (node.rev !== expectedRev)
465
- throw new ToolError(
466
- "conflict",
467
- `${node.id} is at rev ${node.rev}, you sent ${expectedRev}; read it again`,
468
- );
469
- const { kind: _k, availability: _a, ...rest } = node;
470
- const next = { ...rest, ...(availability ? { availability } : {}) };
471
- const saved =
472
- node.kind === "contentEntry"
473
- ? await ctx.store.updateContentEntry(next )
474
- : await ctx.store.updatePage(next );
475
- await ctx.store.createSnapshot(node.siteId, {
476
- actor: ctx.principal.actor,
477
- reason: "agentMutation",
478
- label: `gates by ${ctx.principal.actor.name ?? ctx.principal.actor.id}`,
479
- });
480
- return saved;
481
- }
482
-
483
- /** True when the working copy differs from the live version only in its gates (a gate-only change). */
484
- async function gateOnly(ctx , site , node ) {
485
- const ref = (await liveRefs(ctx.store, site)).find((r) => r.id === node.id);
486
- if (!ref) return false;
487
- const live = (await ctx.store.readSnapshotNode(ref).catch(() => undefined))
488
-
489
- ;
490
- if (!live) return false;
491
- const strip = (n ) => {
492
- const { availability: _a, rev: _r, updatedAt: _u, ...rest } = n;
493
- return canonicalJson(rest);
494
- };
495
- return strip(live) === strip(node);
496
- }
497
-
498
440
  /** One node, one `on-approval` release carrying its gate change, scheduled with `approval`. */
499
441
  async function gateRelease(
500
442
  ctx ,
@@ -521,17 +463,45 @@ async function gateRelease(
521
463
  return scheduleWith(ctx, rec, { kind: "on-approval" }, approval);
522
464
  }
523
465
 
524
- /** The approval a gate change carries when no request is needed: the person's, or the key's (reduce). */
525
- function directApproval(ctx , at ) {
526
- if (isAgent(ctx) && ctx.principal.keyId && !ctx.principal.local && !ctx.confirmed)
527
- return {
528
- kind: "auto",
529
- by: ctx.principal.actor,
530
- keyId: ctx.principal.keyId,
531
- at,
532
- windowEnds: canonicalInstant(Date.parse(at) + 24 * 3_600_000),
533
- };
534
- return personApproval(ctx.principal, at);
466
+ /** What `set_embargo` answers (`approval-pending`: an OAuth connection's gate, RFC-0044). */
467
+ const EMBARGO_STATUS = ["set", "scheduled", "approval-pending", "with-next-publish"] ;
468
+ const EMBARGO_RESULT = obj(
469
+ {
470
+ node: ANY,
471
+ release: ANY,
472
+ status: { type: "string", enum: [...EMBARGO_STATUS] },
473
+ approvalId: { type: "string" },
474
+ message: { type: "string" },
475
+ },
476
+ ["node", "status"],
477
+ );
478
+
479
+ /**
480
+ * An OAuth connection's gate-only change (RFC-0044): it has no approval of its own, so the pinned
481
+ * release `gateRelease` would schedule is filed as a `schedule_release` request to run on
482
+ * approval, bound to its `rl1:` hash like every release request (Studio's Approvals, and a
483
+ * `schedule-approval-due` notification); approving it puts the embargo live. Without out-of-band
484
+ * approvals the gate waits for the next publish.
485
+ */
486
+ async function gateRequest(ctx , site , nodeId ) {
487
+ if (!ctx.approvals) return { status: "with-next-publish" , message: NO_GRANT_GATES };
488
+ const pinned = await releaseService(deps(ctx), ctx.store, ctx.now).create({
489
+ siteId: site.id,
490
+ set: [nodeId],
491
+ createdBy: ctx.principal.actor,
492
+ metadata: { [RELEASE_META.source]: "set_embargo", [RELEASE_META.reduce]: "1" },
493
+ });
494
+ const rec = await recordOf(ctx, site.id, pinned.release.id);
495
+ const when = { kind: "on-approval" } ;
496
+ const filed = await fileRequest(
497
+ ctx,
498
+ rec,
499
+ when,
500
+ "schedule_release",
501
+ "Put the embargo live",
502
+ "tool",
503
+ );
504
+ return { ...filed, message: GRANT_GATE_PENDING };
535
505
  }
536
506
 
537
507
  // ───────────────────────────── runs (§11.2) ─────────────────────────────
@@ -1024,15 +994,7 @@ export function scheduleTools() {
1024
994
  { siteId: id, nodeId: id, from: TIMESTAMP, expectedRev: { type: "integer", minimum: 0 } },
1025
995
  ["siteId", "nodeId", "from", "expectedRev"],
1026
996
  ),
1027
- outputSchema: obj(
1028
- {
1029
- node: ANY,
1030
- release: ANY,
1031
- status: { type: "string", enum: ["set", "scheduled", "with-next-publish"] },
1032
- message: { type: "string" },
1033
- },
1034
- ["node", "status"],
1035
- ),
997
+ outputSchema: EMBARGO_RESULT,
1036
998
  annotations: WRITE,
1037
999
  scopes: ["schedules:write", "content:write"],
1038
1000
  own: "release",
@@ -1071,14 +1033,9 @@ export function scheduleTools() {
1071
1033
  message:
1072
1034
  "This content has other unpublished edits, so the embargo cannot go live on its own: it is still visible until it is published. Ask a person to publish (or schedule) it.",
1073
1035
  };
1074
- const release = await gateRelease(
1075
- ctx,
1076
- site,
1077
- node.id,
1078
- "set_embargo",
1079
- directApproval(ctx, at),
1080
- true,
1081
- );
1036
+ const approval = directApproval(ctx, at);
1037
+ if (!approval) return { node: saved, ...(await gateRequest(ctx, site, node.id)) };
1038
+ const release = await gateRelease(ctx, site, node.id, "set_embargo", approval, true);
1082
1039
  return { node: saved, release, status: "scheduled" };
1083
1040
  },
1084
1041
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/mcp-server",
3
- "version": "0.6.4",
3
+ "version": "0.7.0",
4
4
  "description": "Cloudhal MCP server: semantic site tools (pages, articles, snapshots, preview, publish) governed by the policy engine",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://cloudhal.dev",
@@ -56,15 +56,15 @@
56
56
  "README.md"
57
57
  ],
58
58
  "dependencies": {
59
- "@cloudhal/analytics": "0.6.4",
60
- "@cloudhal/agent-runtime": "0.6.4",
61
- "@cloudhal/astro-renderer": "0.6.4",
62
- "@cloudhal/comments": "0.6.4",
63
- "@cloudhal/core-contracts": "0.6.4",
64
- "@cloudhal/core-ts": "0.6.4",
65
- "@cloudhal/storage": "0.6.4",
59
+ "@cloudhal/analytics": "0.7.0",
60
+ "@cloudhal/agent-runtime": "0.7.0",
61
+ "@cloudhal/astro-renderer": "0.7.0",
62
+ "@cloudhal/comments": "0.7.0",
63
+ "@cloudhal/core-contracts": "0.7.0",
64
+ "@cloudhal/core-ts": "0.7.0",
65
+ "@cloudhal/storage": "0.7.0",
66
66
  "@modelcontextprotocol/server": "2.3.1",
67
- "@cloudhal/schedules": "0.6.4"
67
+ "@cloudhal/schedules": "0.7.0"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@cloudflare/workers-types": "^5.20261007.1",