@catamorphic/fastify-plugin 0.0.0-stage → 0.1.0-alpha.30

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 (160) hide show
  1. package/LICENSE.txt +202 -0
  2. package/NOTICE.txt +32 -0
  3. package/README.md +143 -2
  4. package/dist/app-presentation-tools.d.ts +4 -0
  5. package/dist/app-presentation-tools.d.ts.map +1 -0
  6. package/dist/app.d.ts +27 -0
  7. package/dist/app.d.ts.map +1 -0
  8. package/dist/chunk-baagvvm3.js +164 -0
  9. package/dist/chunk-baagvvm3.js.map +10 -0
  10. package/dist/http-identity.d.ts +52 -0
  11. package/dist/http-identity.d.ts.map +1 -0
  12. package/dist/hung-up.d.ts +7 -0
  13. package/dist/hung-up.d.ts.map +1 -0
  14. package/dist/index.d.ts +11 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10884 -0
  17. package/dist/index.js.map +56 -0
  18. package/dist/mcp-shared.d.ts +117 -0
  19. package/dist/mcp-shared.d.ts.map +1 -0
  20. package/dist/mcp.d.ts +6 -0
  21. package/dist/mcp.d.ts.map +1 -0
  22. package/dist/mcp.js +13 -0
  23. package/dist/mcp.js.map +9 -0
  24. package/dist/plugin.d.ts +113 -0
  25. package/dist/plugin.d.ts.map +1 -0
  26. package/dist/project-capabilities.d.ts +25 -0
  27. package/dist/project-capabilities.d.ts.map +1 -0
  28. package/dist/project-mcp-program.d.ts +15 -0
  29. package/dist/project-mcp-program.d.ts.map +1 -0
  30. package/dist/project-mcp-surface.d.ts +16 -0
  31. package/dist/project-mcp-surface.d.ts.map +1 -0
  32. package/dist/request-logging.d.ts +18 -0
  33. package/dist/request-logging.d.ts.map +1 -0
  34. package/dist/routes/agent-capabilities.d.ts +4 -0
  35. package/dist/routes/agent-capabilities.d.ts.map +1 -0
  36. package/dist/routes/agent.d.ts +4 -0
  37. package/dist/routes/agent.d.ts.map +1 -0
  38. package/dist/routes/apps-mcp.d.ts +4 -0
  39. package/dist/routes/apps-mcp.d.ts.map +1 -0
  40. package/dist/routes/apps.d.ts +12 -0
  41. package/dist/routes/apps.d.ts.map +1 -0
  42. package/dist/routes/client-runners.d.ts +11 -0
  43. package/dist/routes/client-runners.d.ts.map +1 -0
  44. package/dist/routes/code-hosts.d.ts +11 -0
  45. package/dist/routes/code-hosts.d.ts.map +1 -0
  46. package/dist/routes/connection-mcp.d.ts +10 -0
  47. package/dist/routes/connection-mcp.d.ts.map +1 -0
  48. package/dist/routes/connections.d.ts +4 -0
  49. package/dist/routes/connections.d.ts.map +1 -0
  50. package/dist/routes/documents.d.ts +11 -0
  51. package/dist/routes/documents.d.ts.map +1 -0
  52. package/dist/routes/environments.d.ts +4 -0
  53. package/dist/routes/environments.d.ts.map +1 -0
  54. package/dist/routes/git-gateway.d.ts +21 -0
  55. package/dist/routes/git-gateway.d.ts.map +1 -0
  56. package/dist/routes/http-gateway.d.ts +13 -0
  57. package/dist/routes/http-gateway.d.ts.map +1 -0
  58. package/dist/routes/me.d.ts +12 -0
  59. package/dist/routes/me.d.ts.map +1 -0
  60. package/dist/routes/memberships.d.ts +12 -0
  61. package/dist/routes/memberships.d.ts.map +1 -0
  62. package/dist/routes/model-gateway.d.ts +12 -0
  63. package/dist/routes/model-gateway.d.ts.map +1 -0
  64. package/dist/routes/notifications.d.ts +4 -0
  65. package/dist/routes/notifications.d.ts.map +1 -0
  66. package/dist/routes/parse.d.ts +5 -0
  67. package/dist/routes/parse.d.ts.map +1 -0
  68. package/dist/routes/personal-environment.d.ts +13 -0
  69. package/dist/routes/personal-environment.d.ts.map +1 -0
  70. package/dist/routes/plugins.d.ts +4 -0
  71. package/dist/routes/plugins.d.ts.map +1 -0
  72. package/dist/routes/project-mcp.d.ts +25 -0
  73. package/dist/routes/project-mcp.d.ts.map +1 -0
  74. package/dist/routes/projects.d.ts +4 -0
  75. package/dist/routes/projects.d.ts.map +1 -0
  76. package/dist/routes/publications.d.ts +15 -0
  77. package/dist/routes/publications.d.ts.map +1 -0
  78. package/dist/routes/run-errors.d.ts +8 -0
  79. package/dist/routes/run-errors.d.ts.map +1 -0
  80. package/dist/routes/runs.d.ts +4 -0
  81. package/dist/routes/runs.d.ts.map +1 -0
  82. package/dist/routes/session-artifacts.d.ts +27 -0
  83. package/dist/routes/session-artifacts.d.ts.map +1 -0
  84. package/dist/routes/session-mailboxes.d.ts +4 -0
  85. package/dist/routes/session-mailboxes.d.ts.map +1 -0
  86. package/dist/routes/session-previews.d.ts +40 -0
  87. package/dist/routes/session-previews.d.ts.map +1 -0
  88. package/dist/routes/session-terminals.d.ts +15 -0
  89. package/dist/routes/session-terminals.d.ts.map +1 -0
  90. package/dist/routes/triggers.d.ts +20 -0
  91. package/dist/routes/triggers.d.ts.map +1 -0
  92. package/dist/routes/watchers.d.ts +4 -0
  93. package/dist/routes/watchers.d.ts.map +1 -0
  94. package/dist/routes/webhooks.d.ts +11 -0
  95. package/dist/routes/webhooks.d.ts.map +1 -0
  96. package/dist/routes/workflow-enablements.d.ts +4 -0
  97. package/dist/routes/workflow-enablements.d.ts.map +1 -0
  98. package/dist/routes/workflows.d.ts +4 -0
  99. package/dist/routes/workflows.d.ts.map +1 -0
  100. package/dist/schemas.d.ts +17776 -0
  101. package/dist/schemas.d.ts.map +1 -0
  102. package/dist/session-artifact-tools.d.ts +5 -0
  103. package/dist/session-artifact-tools.d.ts.map +1 -0
  104. package/dist/session-event-stream.d.ts +45 -0
  105. package/dist/session-event-stream.d.ts.map +1 -0
  106. package/dist/spa.d.ts +24 -0
  107. package/dist/spa.d.ts.map +1 -0
  108. package/dist/telemetry.d.ts +4 -0
  109. package/dist/telemetry.d.ts.map +1 -0
  110. package/package.json +73 -4
  111. package/src/app-presentation-tools.ts +46 -0
  112. package/src/app.ts +115 -0
  113. package/src/http-identity.ts +145 -0
  114. package/src/hung-up.ts +13 -0
  115. package/src/index.ts +25 -0
  116. package/src/mcp-shared.ts +331 -0
  117. package/src/mcp.ts +10 -0
  118. package/src/plugin.ts +332 -0
  119. package/src/project-capabilities.ts +153 -0
  120. package/src/project-mcp-program.ts +607 -0
  121. package/src/project-mcp-surface.ts +881 -0
  122. package/src/request-logging.ts +47 -0
  123. package/src/routes/agent-capabilities.ts +109 -0
  124. package/src/routes/agent.ts +1566 -0
  125. package/src/routes/apps-mcp.ts +303 -0
  126. package/src/routes/apps.ts +692 -0
  127. package/src/routes/client-runners.ts +251 -0
  128. package/src/routes/code-hosts.ts +247 -0
  129. package/src/routes/connection-mcp.ts +135 -0
  130. package/src/routes/connections.ts +494 -0
  131. package/src/routes/documents.ts +538 -0
  132. package/src/routes/environments.ts +72 -0
  133. package/src/routes/git-gateway.ts +106 -0
  134. package/src/routes/http-gateway.ts +141 -0
  135. package/src/routes/me.ts +122 -0
  136. package/src/routes/memberships.ts +173 -0
  137. package/src/routes/model-gateway.ts +134 -0
  138. package/src/routes/notifications.ts +101 -0
  139. package/src/routes/parse.ts +32 -0
  140. package/src/routes/personal-environment.ts +146 -0
  141. package/src/routes/plugins.ts +341 -0
  142. package/src/routes/project-mcp.ts +394 -0
  143. package/src/routes/projects.ts +708 -0
  144. package/src/routes/publications.ts +222 -0
  145. package/src/routes/run-errors.ts +86 -0
  146. package/src/routes/runs.ts +253 -0
  147. package/src/routes/session-artifacts.ts +293 -0
  148. package/src/routes/session-mailboxes.ts +80 -0
  149. package/src/routes/session-previews.ts +224 -0
  150. package/src/routes/session-terminals.ts +209 -0
  151. package/src/routes/triggers.ts +241 -0
  152. package/src/routes/watchers.ts +94 -0
  153. package/src/routes/webhooks.ts +171 -0
  154. package/src/routes/workflow-enablements.ts +331 -0
  155. package/src/routes/workflows.ts +323 -0
  156. package/src/schemas.ts +2735 -0
  157. package/src/session-artifact-tools.ts +170 -0
  158. package/src/session-event-stream.ts +122 -0
  159. package/src/spa.ts +135 -0
  160. package/src/telemetry.ts +93 -0
@@ -0,0 +1,303 @@
1
+ import { buildAppGuestDocument } from "@catamorphic/app";
2
+ import type { FastifyInstance } from "fastify";
3
+ import type { RouteContext } from "../app.js";
4
+ import { resolveIdentity } from "../http-identity.js";
5
+ import {
6
+ callPollRunTool,
7
+ handleMcpPost,
8
+ McpRequestError,
9
+ negotiateProtocolVersion,
10
+ POLL_RUN_TOOL,
11
+ POLL_RUN_TOOL_DEFINITION,
12
+ toolError,
13
+ toolValue,
14
+ } from "../mcp-shared.js";
15
+
16
+ /**
17
+ * MCP server for a project's published apps — the outbound half of MCP
18
+ * Apps support. Any MCP Apps host (Claude, ChatGPT, VS Code, Cursor, …)
19
+ * that connects to `/projects/:projectId/apps-mcp` gets:
20
+ *
21
+ * - one tool per app-callable workflow (the union of the published apps'
22
+ * frozen `allowed_workflows` sets), each carrying the standard
23
+ * `_meta.ui.resourceUri` linkage (extension io.modelcontextprotocol/ui);
24
+ * - a `ui://apps/<name>` resource per published app whose HTML embeds the
25
+ * app's bundle — the `@catamorphic/app` guest runtime detects the MCP
26
+ * host and speaks `tools/call` back through it;
27
+ * - a `catamorphic_poll_run` tool so durable runs keep their poll loop.
28
+ *
29
+ * Tool calls execute under the caller's identity narrowed to the owning
30
+ * app, so the frozen-workflow-set authorization applies to MCP callers
31
+ * exactly as it does to the in-product iframe.
32
+ *
33
+ * The endpoint is a stateless Streamable-HTTP JSON-RPC handler speaking
34
+ * the `initialize`-handshake protocol era (≤2025-11-25) — what every
35
+ * shipping client negotiates by default today (the official v2 SDK's
36
+ * client also defaults to it). Requests are independent; no sessions.
37
+ */
38
+
39
+ const RUN_CALL_BUDGET_MS = 120_000;
40
+
41
+ interface PublishedApp {
42
+ name: string;
43
+ appId: string;
44
+ versionId: string;
45
+ code: string;
46
+ css: string;
47
+ allowedWorkflows: string[];
48
+ workflowShapes: Record<
49
+ string,
50
+ { inputSchema: unknown; outputSchema: unknown }
51
+ >;
52
+ }
53
+
54
+ export function registerAppsMcpRoutes(app: FastifyInstance, ctx: RouteContext) {
55
+ app.post("/projects/:projectId/apps-mcp", async (request, reply) => {
56
+ const core = ctx.core;
57
+ if (!core?.apps || !core.runs) {
58
+ return reply.status(503).send({ error: "Apps not configured" });
59
+ }
60
+ const { projectId } = request.params as { projectId: string };
61
+
62
+ return handleMcpPost(reply, request.body, async (method, params) => {
63
+ const identity = resolveIdentity(request);
64
+ switch (method) {
65
+ case "initialize":
66
+ return {
67
+ protocolVersion: negotiateProtocolVersion(params.protocolVersion),
68
+ capabilities: { tools: { listChanged: false }, resources: {} },
69
+ serverInfo: {
70
+ name: "catamorphic-apps",
71
+ title: "Catamorphic apps",
72
+ version: "1.0.0",
73
+ },
74
+ };
75
+ case "ping":
76
+ return {};
77
+ case "tools/list": {
78
+ const apps = await loadPublishedApps(core, identity, projectId);
79
+ return { tools: buildTools(apps) };
80
+ }
81
+ case "resources/list": {
82
+ const apps = await loadPublishedApps(core, identity, projectId);
83
+ return {
84
+ resources: apps.map((published) => ({
85
+ uri: appResourceUri(published.name),
86
+ name: `${published.name} app`,
87
+ mimeType: MCP_APP_MIME_TYPE,
88
+ })),
89
+ };
90
+ }
91
+ case "resources/templates/list":
92
+ return { resourceTemplates: [] };
93
+ case "prompts/list":
94
+ return { prompts: [] };
95
+ case "resources/read": {
96
+ const uri = typeof params.uri === "string" ? params.uri : "";
97
+ const apps = await loadPublishedApps(core, identity, projectId);
98
+ const published = apps.find(
99
+ (candidate) => appResourceUri(candidate.name) === uri,
100
+ );
101
+ if (!published) {
102
+ throw new McpRequestError(-32602, `Unknown resource ${uri}`);
103
+ }
104
+ return {
105
+ contents: [
106
+ {
107
+ uri,
108
+ mimeType: MCP_APP_MIME_TYPE,
109
+ text: buildAppDocument(published),
110
+ _meta: {
111
+ ui: {
112
+ // Catamorphic apps have no network of their own —
113
+ // every effect is a workflow call through the host.
114
+ csp: { connectDomains: [], resourceDomains: [] },
115
+ prefersBorder: true,
116
+ },
117
+ },
118
+ },
119
+ ],
120
+ };
121
+ }
122
+ case "tools/call": {
123
+ const name = typeof params.name === "string" ? params.name : "";
124
+ const args =
125
+ typeof params.arguments === "object" && params.arguments !== null
126
+ ? (params.arguments as Record<string, unknown>)
127
+ : {};
128
+ return callTool(core, identity, projectId, name, args);
129
+ }
130
+ default:
131
+ throw new McpRequestError(-32601, `Method not found: ${method}`);
132
+ }
133
+ });
134
+ });
135
+ }
136
+
137
+ const MCP_APP_MIME_TYPE = "text/html;profile=mcp-app";
138
+
139
+ const appResourceUri = (appName: string) => `ui://apps/${appName}`;
140
+
141
+ type Core = NonNullable<RouteContext["core"]>;
142
+ type Identity = Parameters<NonNullable<Core["apps"]>["list"]>[0]["identity"];
143
+
144
+ async function loadPublishedApps(
145
+ core: Core,
146
+ identity: Identity,
147
+ projectId: string,
148
+ ): Promise<PublishedApp[]> {
149
+ const apps = core.apps;
150
+ if (!apps) return [];
151
+ const summaries = await apps.list({ identity, projectId });
152
+ const published: PublishedApp[] = [];
153
+ for (const summary of summaries) {
154
+ if (!summary.activeVersionId) continue;
155
+ const view = await apps.viewState({
156
+ identity,
157
+ projectId,
158
+ appName: summary.name,
159
+ });
160
+ if (view.state !== "ready") continue;
161
+ published.push({
162
+ name: summary.name,
163
+ appId: view.appId,
164
+ versionId: view.versionId,
165
+ code: view.code,
166
+ css: view.css,
167
+ allowedWorkflows: view.allowedWorkflows,
168
+ workflowShapes: view.workflowShapes,
169
+ });
170
+ }
171
+ return published;
172
+ }
173
+
174
+ function buildTools(apps: PublishedApp[]) {
175
+ const tools: Array<Record<string, unknown>> = [];
176
+ const owners = workflowOwners(apps);
177
+ for (const [workflowName, owner] of owners) {
178
+ tools.push({
179
+ name: workflowName,
180
+ description:
181
+ `Run the "${workflowName}" workflow of the "${owner.name}" app. ` +
182
+ `Pass {"input": ...}. Default mode waits for the result; ` +
183
+ `{"mode": "start"} returns {"runId"} for long-running workflows — ` +
184
+ `poll it with ${POLL_RUN_TOOL}.`,
185
+ inputSchema: {
186
+ type: "object",
187
+ properties: {
188
+ input: {
189
+ description: "The workflow's input (JSON)",
190
+ // The workflow's real input schema, frozen at app build from
191
+ // the extracted TS types.
192
+ ...((owner.workflowShapes?.[workflowName]?.inputSchema ??
193
+ {}) as object),
194
+ },
195
+ mode: { type: "string", enum: ["invoke", "start"] },
196
+ },
197
+ required: ["input"],
198
+ },
199
+ _meta: {
200
+ ui: {
201
+ resourceUri: appResourceUri(owner.name),
202
+ visibility: ["model", "app"],
203
+ },
204
+ },
205
+ });
206
+ }
207
+ tools.push(POLL_RUN_TOOL_DEFINITION);
208
+ return tools;
209
+ }
210
+
211
+ /** Workflow → the first published app whose frozen set allows it. */
212
+ function workflowOwners(apps: PublishedApp[]): Map<string, PublishedApp> {
213
+ const owners = new Map<string, PublishedApp>();
214
+ for (const published of apps) {
215
+ for (const workflowName of published.allowedWorkflows) {
216
+ if (!owners.has(workflowName)) owners.set(workflowName, published);
217
+ }
218
+ }
219
+ return owners;
220
+ }
221
+
222
+ async function callTool(
223
+ core: Core,
224
+ identity: Identity,
225
+ projectId: string,
226
+ name: string,
227
+ args: Record<string, unknown>,
228
+ ): Promise<Record<string, unknown>> {
229
+ if (name === POLL_RUN_TOOL) {
230
+ return callPollRunTool(core, identity, args);
231
+ }
232
+
233
+ const apps = await loadPublishedApps(core, identity, projectId);
234
+ const owner = workflowOwners(apps).get(name);
235
+ if (!owner || !core.apps) {
236
+ return toolError(`Unknown tool: ${name}`);
237
+ }
238
+ // The owning app's identity puts MCP callers on the exact authorization
239
+ // path the in-product iframe uses: the run is re-authorized against the
240
+ // app's active version's frozen workflow set server-side (ADR 0053), it
241
+ // keeps the caller's execution reach, and it carries the version's
242
+ // declared session access (ADR 0148).
243
+ const scoped = await core.apps.identityForApp({
244
+ identity,
245
+ projectId,
246
+ appName: owner.name,
247
+ });
248
+
249
+ if (args.mode === "start") {
250
+ let run: { id: string };
251
+ try {
252
+ run = await core.runs.triggerProduction({
253
+ identity: scoped,
254
+ projectId,
255
+ workflowName: name,
256
+ input: (args.input ?? null) as never,
257
+ });
258
+ } catch (error) {
259
+ return toolError(
260
+ error instanceof Error ? error.message : "Workflow trigger failed",
261
+ );
262
+ }
263
+ return toolValue({ runId: run.id });
264
+ }
265
+
266
+ // Sync call: settles inline unless the workflow reaches a durable wait or
267
+ // the budget, in which case the caller polls the run.
268
+ let outcome: Awaited<ReturnType<Core["runs"]["call"]>>;
269
+ try {
270
+ outcome = await core.runs.call({
271
+ identity: scoped,
272
+ projectId,
273
+ workflowName: name,
274
+ input: (args.input ?? null) as never,
275
+ budgetMs: RUN_CALL_BUDGET_MS,
276
+ });
277
+ } catch (error) {
278
+ return toolError(
279
+ error instanceof Error ? error.message : "Workflow trigger failed",
280
+ );
281
+ }
282
+ switch (outcome.status) {
283
+ case "completed":
284
+ return toolValue(outcome.output);
285
+ case "failed":
286
+ return toolError(outcome.error);
287
+ case "suspended":
288
+ return toolError(
289
+ `Workflow still running (${outcome.suspendedOn}); ` +
290
+ `poll run ${outcome.runId} with ${POLL_RUN_TOOL}.`,
291
+ );
292
+ }
293
+ }
294
+
295
+ /**
296
+ * The app document: same construction and CSP as the in-product mount
297
+ * (including the `process` shim the bundle needs to boot), minus a theme —
298
+ * an MCP host has no shared token vocabulary to seed, so the base layer's
299
+ * neutral defaults apply.
300
+ */
301
+ function buildAppDocument(app: PublishedApp): string {
302
+ return buildAppGuestDocument({ code: app.code, css: app.css });
303
+ }