@catamorphic/fastify-plugin 0.1.0-alpha.39 → 0.1.0-alpha.42

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.
Files changed (81) hide show
  1. package/NOTICE.txt +7 -0
  2. package/dist/app.d.ts +2 -0
  3. package/dist/app.d.ts.map +1 -1
  4. package/dist/chunk-jytcc42y.js +592 -0
  5. package/dist/chunk-jytcc42y.js.map +11 -0
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1673 -411
  9. package/dist/index.js.map +29 -25
  10. package/dist/mcp-scopes.d.ts +12 -0
  11. package/dist/mcp-scopes.d.ts.map +1 -0
  12. package/dist/mcp-server.d.ts +158 -0
  13. package/dist/mcp-server.d.ts.map +1 -0
  14. package/dist/mcp-shared.d.ts +3 -50
  15. package/dist/mcp-shared.d.ts.map +1 -1
  16. package/dist/mcp.d.ts +4 -2
  17. package/dist/mcp.d.ts.map +1 -1
  18. package/dist/mcp.js +6 -4
  19. package/dist/mcp.js.map +1 -1
  20. package/dist/plugin.d.ts +26 -2
  21. package/dist/plugin.d.ts.map +1 -1
  22. package/dist/project-capabilities.d.ts.map +1 -1
  23. package/dist/project-mcp-surface.d.ts +3 -4
  24. package/dist/project-mcp-surface.d.ts.map +1 -1
  25. package/dist/routes/agent.d.ts.map +1 -1
  26. package/dist/routes/apps-mcp.d.ts.map +1 -1
  27. package/dist/routes/client-runners.d.ts.map +1 -1
  28. package/dist/routes/connection-mcp.d.ts.map +1 -1
  29. package/dist/routes/documents.d.ts.map +1 -1
  30. package/dist/routes/environments.d.ts.map +1 -1
  31. package/dist/routes/project-mcp.d.ts.map +1 -1
  32. package/dist/routes/projects.d.ts.map +1 -1
  33. package/dist/routes/publications.d.ts.map +1 -1
  34. package/dist/routes/run-errors.d.ts.map +1 -1
  35. package/dist/routes/runs.d.ts.map +1 -1
  36. package/dist/routes/server-mcp.d.ts +17 -0
  37. package/dist/routes/server-mcp.d.ts.map +1 -0
  38. package/dist/routes/session-mailboxes.d.ts.map +1 -1
  39. package/dist/routes/triggers.d.ts.map +1 -1
  40. package/dist/routes/watchers.d.ts.map +1 -1
  41. package/dist/routes/workflow-enablements.d.ts.map +1 -1
  42. package/dist/routes/workflows.d.ts.map +1 -1
  43. package/dist/schemas.d.ts +3684 -28
  44. package/dist/schemas.d.ts.map +1 -1
  45. package/dist/session-mcp.d.ts +54 -0
  46. package/dist/session-mcp.d.ts.map +1 -0
  47. package/dist/session-tools.d.ts +49 -0
  48. package/dist/session-tools.d.ts.map +1 -0
  49. package/package.json +12 -12
  50. package/src/app.ts +3 -0
  51. package/src/index.ts +1 -0
  52. package/src/mcp-scopes.ts +69 -0
  53. package/src/mcp-server.ts +827 -0
  54. package/src/mcp-shared.ts +3 -151
  55. package/src/mcp.ts +6 -5
  56. package/src/plugin.ts +73 -2
  57. package/src/project-capabilities.ts +5 -3
  58. package/src/project-mcp-program.ts +2 -2
  59. package/src/project-mcp-surface.ts +18 -239
  60. package/src/routes/agent.ts +384 -41
  61. package/src/routes/apps-mcp.ts +84 -82
  62. package/src/routes/client-runners.ts +3 -1
  63. package/src/routes/connection-mcp.ts +6 -13
  64. package/src/routes/documents.ts +4 -0
  65. package/src/routes/environments.ts +49 -1
  66. package/src/routes/project-mcp.ts +176 -154
  67. package/src/routes/projects.ts +6 -0
  68. package/src/routes/publications.ts +2 -0
  69. package/src/routes/run-errors.ts +5 -1
  70. package/src/routes/runs.ts +4 -0
  71. package/src/routes/server-mcp.ts +151 -0
  72. package/src/routes/session-mailboxes.ts +1 -0
  73. package/src/routes/triggers.ts +6 -1
  74. package/src/routes/watchers.ts +1 -0
  75. package/src/routes/workflow-enablements.ts +132 -1
  76. package/src/routes/workflows.ts +4 -0
  77. package/src/schemas.ts +347 -5
  78. package/src/session-mcp.ts +696 -0
  79. package/src/session-tools.ts +169 -0
  80. package/dist/chunk-baagvvm3.js +0 -164
  81. package/dist/chunk-baagvvm3.js.map +0 -10
@@ -6,19 +6,14 @@ import {
6
6
  import type { CatamorphicCore, Identity } from "@catamorphic/core";
7
7
  import {
8
8
  AccessDeniedError,
9
- AgentTurnUnsettledError,
10
- EVERY_ARTIFACT,
11
9
  mayUseProject,
12
- parseWorkspaceRequest,
13
- projectAgentId,
14
10
  resolveScope,
15
- SESSION_ACTION_SCHEMAS,
16
- type SessionActionOperation,
17
11
  } from "@catamorphic/core";
18
12
  import { z } from "zod";
19
13
  import { appPresentationTool } from "./app-presentation-tools.js";
20
14
  import { toolError, toolValue } from "./mcp-shared.js";
21
15
  import { sessionArtifactTool } from "./session-artifact-tools.js";
16
+ import { type McpTool, sessionTools } from "./session-tools.js";
22
17
 
23
18
  /**
24
19
  * The project's "bring your own agent" surface (ADR 0055): everything a
@@ -29,19 +24,20 @@ import { sessionArtifactTool } from "./session-artifact-tools.js";
29
24
  * document refs by `DocumentsService` itself;
30
25
  * - skills: list / read, for anyone who may use the project (with
31
26
  * `program:read`, or an agent ref);
32
- * - agents: `ask_agent` — a synchronous chat turn with a project agent the
33
- * caller may open sessions on (`AgentSessionsService` enforces the ref).
27
+ * - chats: the session operations (`session_start`, `session_wait`,
28
+ * `session_answer` and the rest of core's registry, ADR 0346), each
29
+ * enforced by `AgentSessionsService`.
34
30
  *
35
31
  * Nothing here checks scope by hand: every call goes through the core
36
- * service that already enforces it, with the request's identity.
32
+ * service that already enforces it, with the request's identity. Each tool
33
+ * is one definition (name, schemas, annotations, handler) in the list this
34
+ * returns: a tool core defines joins it in one line.
37
35
  */
38
36
 
39
37
  const READ_ONLY = { readOnlyHint: true } as const;
40
38
 
41
- export interface SurfaceTool {
42
- definition: Record<string, unknown>;
43
- call: (args: Record<string, unknown>) => Promise<Record<string, unknown>>;
44
- }
39
+ /** One tool of the surface: see {@link McpTool}. */
40
+ export type SurfaceTool = McpTool;
45
41
 
46
42
  export { mayUseProject };
47
43
 
@@ -517,86 +513,15 @@ export function surfaceTools(
517
513
 
518
514
  if (core.agentSessions) {
519
515
  const sessions = core.agentSessions;
520
- const agentNames = (identity.scope ?? [])
521
- .filter((ref) => ref.kind === "agent" && ref.projectId === projectId)
522
- .map((ref) => (ref as { name: string }).name);
523
- const hint =
524
- agentNames.includes(EVERY_ARTIFACT) || identity.scope === undefined
525
- ? "any committed project agent (.work/agents/<slug>.json)"
526
- : agentNames.length > 0
527
- ? `one of: ${agentNames.join(", ")}`
528
- : "none available to you";
529
- tools.push({
530
- definition: {
531
- name: "ask_agent",
532
- description: `Ask one of the project's agents (${hint}) and get its reply — a full turn with the agent's own tools and persona. Pass sessionId to continue an earlier conversation, or environment to choose where a new one runs.`,
533
- inputSchema: {
534
- type: "object",
535
- properties: {
536
- agent: { type: "string", description: "The agent's slug" },
537
- message: { type: "string" },
538
- sessionId: { type: "string" },
539
- environment: {
540
- type: "string",
541
- description:
542
- "Environment for a new conversation (project_overview lists yours)",
543
- },
544
- },
545
- required: ["agent", "message"],
546
- },
547
- },
548
- call: guarded(async (args) => {
549
- const agent = str(args.agent);
550
- const message = str(args.message);
551
- if (!agent || !message)
552
- throw new Error("agent and message are required");
553
- const agentId = projectAgentId(projectId, agent);
554
- const sessionId =
555
- str(args.sessionId) ??
556
- (
557
- await sessions.create(identity, projectId, {
558
- agentId,
559
- source: "mcp",
560
- ...(str(args.environment)
561
- ? { environment: str(args.environment) }
562
- : {}),
563
- })
564
- ).id;
565
- // Never the person's own words (ADR 0217): an agent's in Work, or
566
- // the person's own assistant asking through this server.
567
- const from = currentSessionId
568
- ? {
569
- kind: "agent" as const,
570
- sessionId: currentSessionId,
571
- agentId: (
572
- await sessions.get(identity, projectId, currentSessionId)
573
- ).agentId,
574
- }
575
- : {
576
- kind: "assistant" as const,
577
- externalUserId: identity.externalUserId,
578
- };
579
- try {
580
- const { reply } = await sessions.sendMessage(
581
- identity,
582
- projectId,
583
- sessionId,
584
- message,
585
- { from },
586
- );
587
- return {
588
- sessionId,
589
- reply: reply?.kind === "assistant_message" ? reply.text : "",
590
- };
591
- } catch (error) {
592
- // No reply yet: the message waits for a machine, is held or
593
- // cancelled, or its machine stopped. The caller reads the chat.
594
- if (error instanceof AgentTurnUnsettledError)
595
- return { sessionId, state: error.state, reply: error.message };
596
- throw error;
597
- }
516
+ // Start, send, wait, answer and the rest of the registry (ADR 0346).
517
+ tools.push(
518
+ ...sessionTools({
519
+ core,
520
+ identity,
521
+ projectId,
522
+ ...(currentSessionId ? { currentSessionId } : {}),
598
523
  }),
599
- });
524
+ );
600
525
 
601
526
  // A key of the calling chat's metadata (ADR 0226): bound to the
602
527
  // session this connection serves, so there is none without one.
@@ -624,123 +549,6 @@ export function surfaceTools(
624
549
  });
625
550
  }
626
551
 
627
- tools.push({
628
- definition: {
629
- name: "send_agent_message",
630
- description:
631
- "Deliver an attributed message from your current session to this or another agent session. queue (the default) wakes an idle session or runs after its active turn; steer adds it to the active turn so the agent sees it while it works; interrupt stops the active turn and runs this next; message_only records it without waking the agent. Set attention to required to alert the user to this message, independently of whether the agent should run. Use interrupt only when delay would make the work wrong.",
632
- inputSchema: {
633
- type: "object",
634
- properties: {
635
- fromSessionId: { type: "string" },
636
- toSessionId: { type: "string" },
637
- message: { type: "string" },
638
- mode: {
639
- type: "string",
640
- enum: ["queue", "steer", "interrupt", "message_only"],
641
- default: "queue",
642
- },
643
- attention: { type: "string", enum: ["none", "required"] },
644
- idempotencyKey: { type: "string" },
645
- workspace: {
646
- type: "object",
647
- description:
648
- "Move the receiving session's workspace to a ref of the project's linked remote before its next turn (a branch, tag, commit, or refs/pull/<n>/head). update: rebase (default) keeps its commits, reset discards them.",
649
- properties: {
650
- ref: { type: "string" },
651
- update: { type: "string", enum: ["reset", "rebase"] },
652
- },
653
- required: ["ref"],
654
- additionalProperties: false,
655
- },
656
- },
657
- required: ["toSessionId", "message"],
658
- },
659
- },
660
- call: guarded(async (args) => {
661
- const fromSessionId = str(args.fromSessionId) ?? currentSessionId;
662
- const toSessionId = str(args.toSessionId);
663
- const message = str(args.message);
664
- const mode = args.mode === undefined ? "queue" : args.mode;
665
- if (
666
- !fromSessionId ||
667
- !toSessionId ||
668
- !message ||
669
- (mode !== "queue" &&
670
- mode !== "steer" &&
671
- mode !== "interrupt" &&
672
- mode !== "message_only")
673
- ) {
674
- throw new Error(
675
- "fromSessionId, toSessionId, message, and a valid mode are required",
676
- );
677
- }
678
- if (
679
- args.attention !== undefined &&
680
- args.attention !== "none" &&
681
- args.attention !== "required"
682
- )
683
- throw new Error("attention must be none or required");
684
- const source = await sessions.get(identity, projectId, fromSessionId);
685
- return sessions.deliver(identity, projectId, toSessionId, {
686
- content: message,
687
- author: {
688
- kind: "agent",
689
- sessionId: fromSessionId,
690
- agentId: source.agentId,
691
- },
692
- mode,
693
- attention: args.attention,
694
- ...(str(args.idempotencyKey)
695
- ? { idempotencyKey: str(args.idempotencyKey) }
696
- : {}),
697
- ...(args.workspace === undefined
698
- ? {}
699
- : { workspace: parseWorkspaceRequest(args.workspace) }),
700
- });
701
- }),
702
- });
703
-
704
- if (core.sessionActions) {
705
- const actions = core.sessionActions;
706
- for (const operation of Object.keys(
707
- SESSION_ACTION_SCHEMAS,
708
- ) as SessionActionOperation[]) {
709
- tools.push({
710
- definition: {
711
- name: `session_${operation}`,
712
- description: `Session ${operation}. Uses the same authorized operation as workflows. Name the chat by sessionId, or by the project's key for it (key, with audience "project" for the project chat). Mutations require a stable idempotencyKey; retries return the existing result. Load the session-workflows skill for examples.`,
713
- inputSchema: z.toJSONSchema(SESSION_ACTION_SCHEMAS[operation]),
714
- },
715
- call: guarded(async (args) => {
716
- const source = currentSessionId
717
- ? await sessions.get(identity, projectId, currentSessionId)
718
- : undefined;
719
- return actions.execute({
720
- identity,
721
- projectId,
722
- operation,
723
- args,
724
- author: source
725
- ? {
726
- kind: "agent",
727
- sessionId: source.id,
728
- agentId: source.agentId,
729
- }
730
- : { kind: "user", externalUserId: identity.externalUserId },
731
- causation: source
732
- ? await sessions.causalContext({
733
- identity,
734
- projectId,
735
- sessionId: source.id,
736
- })
737
- : [],
738
- });
739
- }),
740
- });
741
- }
742
- }
743
-
744
552
  if (core.watchers) {
745
553
  const watchers = core.watchers;
746
554
  const eventSources = core.projectEventSources.map(
@@ -750,7 +558,7 @@ export function surfaceTools(
750
558
  {
751
559
  definition: {
752
560
  name: "create_watcher",
753
- description: `Temporarily enable an ordinary TypeScript workflow owned by this session. To repeat a shell check (a URL, a file, a CLI), use the host's watch_command when it is offered: it runs where your commands run. For periodic monitoring with workflow IO, declare a normal schedule trigger. For event-driven work, use normalized Project Events already supplied by this host. Stop, expiry, or session close/archive disables future invocations. The source must export the named defineWorkflow and declare one or more inline triggers, for example triggers: [trigger("issue.changed")]. Event triggers receive the normalized Project Event envelope; schedule triggers receive their normal scheduled payload. To notify or wake a session, return context.host["catamorphic.sessions"].deliver({ sessionId, content, mode, idempotencyKey }) from a boundary. Pass source directly; do not write it to the user working tree. The host places it at .work/workflows/src/artifacts/<id>.ts in an isolated committed-origin checkout, so imports must already exist in that origin. workflowName must be exported by this source. The workflow is committed to an isolated work/artifacts/<id> ref, pinned, and never merged into project main. This is temporary execution, not private storage. Call stop_watcher when the task is complete. Load the workflow-lifecycle skill for lifetime and publishing guidance.${
561
+ description: `Temporarily enable an ordinary TypeScript workflow owned by this session. To repeat a shell check (a URL, a file, a CLI), use the host's watch_command when it is offered: it runs where your commands run. For periodic monitoring with workflow IO, declare a normal schedule trigger. For event-driven work, use normalized Project Events already supplied by this host. Stop, expiry, or session close/archive disables future invocations. The source must export the named defineWorkflow and declare one or more inline triggers, for example triggers: [trigger("issue.changed")]. Event triggers receive the normalized Project Event envelope; schedule triggers receive their normal scheduled payload. To notify or wake a session, return context.host["catamorphic.sessions"].deliver({ sessionId, content, mode, idempotencyKey }) from a boundary. Pass source directly; do not write it to the user working tree. The host places it at .work/workflows/src/artifacts/<id>.ts in an isolated committed-origin checkout, so imports must already exist in that origin. workflowName must be exported by this source. The workflow is committed to an isolated work/artifacts/<id> ref, pinned, and never merged into project main. This is temporary execution, not private storage. Call session_stop_watcher with the watcher's id when the task is complete. Load the workflow-lifecycle skill for lifetime and publishing guidance.${
754
562
  eventSources.length
755
563
  ? ` This host can also observe outside systems that cannot reach it with a webhook: pass eventSource (${eventSources.join(", ")}) and the host polls for new events from now on and records them as Project Events shaped like the webhook deliveries the source replaces, so bind the project's trigger kinds for that webhook (for example from .work/triggers/).`
756
564
  : ""
@@ -844,35 +652,6 @@ export function surfaceTools(
844
652
  });
845
653
  }),
846
654
  },
847
- {
848
- definition: {
849
- name: "stop_watcher",
850
- description: "Stop a temporary watcher owned by a session.",
851
- inputSchema: {
852
- type: "object",
853
- properties: {
854
- sessionId: { type: "string" },
855
- watcherId: { type: "string" },
856
- },
857
- required: ["watcherId"],
858
- },
859
- },
860
- call: guarded(async (args) => {
861
- const sessionId = str(args.sessionId) ?? currentSessionId;
862
- const watcherId = str(args.watcherId);
863
- if (!sessionId || !watcherId) {
864
- throw new Error("sessionId and watcherId are required");
865
- }
866
- return {
867
- stopped: await watchers.stop({
868
- identity,
869
- projectId,
870
- sessionId,
871
- watcherId,
872
- }),
873
- };
874
- }),
875
- },
876
655
  );
877
656
  }
878
657
  }