@catamorphic/fastify-plugin 0.0.0-stage → 0.1.0-alpha.31
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/LICENSE.txt +202 -0
- package/NOTICE.txt +32 -0
- package/README.md +143 -2
- package/dist/app-presentation-tools.d.ts +4 -0
- package/dist/app-presentation-tools.d.ts.map +1 -0
- package/dist/app.d.ts +27 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/chunk-baagvvm3.js +164 -0
- package/dist/chunk-baagvvm3.js.map +10 -0
- package/dist/http-identity.d.ts +52 -0
- package/dist/http-identity.d.ts.map +1 -0
- package/dist/hung-up.d.ts +7 -0
- package/dist/hung-up.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10884 -0
- package/dist/index.js.map +56 -0
- package/dist/mcp-shared.d.ts +117 -0
- package/dist/mcp-shared.d.ts.map +1 -0
- package/dist/mcp.d.ts +6 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +13 -0
- package/dist/mcp.js.map +9 -0
- package/dist/plugin.d.ts +113 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/project-capabilities.d.ts +25 -0
- package/dist/project-capabilities.d.ts.map +1 -0
- package/dist/project-mcp-program.d.ts +15 -0
- package/dist/project-mcp-program.d.ts.map +1 -0
- package/dist/project-mcp-surface.d.ts +16 -0
- package/dist/project-mcp-surface.d.ts.map +1 -0
- package/dist/request-logging.d.ts +18 -0
- package/dist/request-logging.d.ts.map +1 -0
- package/dist/routes/agent-capabilities.d.ts +4 -0
- package/dist/routes/agent-capabilities.d.ts.map +1 -0
- package/dist/routes/agent.d.ts +4 -0
- package/dist/routes/agent.d.ts.map +1 -0
- package/dist/routes/apps-mcp.d.ts +4 -0
- package/dist/routes/apps-mcp.d.ts.map +1 -0
- package/dist/routes/apps.d.ts +12 -0
- package/dist/routes/apps.d.ts.map +1 -0
- package/dist/routes/client-runners.d.ts +11 -0
- package/dist/routes/client-runners.d.ts.map +1 -0
- package/dist/routes/code-hosts.d.ts +11 -0
- package/dist/routes/code-hosts.d.ts.map +1 -0
- package/dist/routes/connection-mcp.d.ts +10 -0
- package/dist/routes/connection-mcp.d.ts.map +1 -0
- package/dist/routes/connections.d.ts +4 -0
- package/dist/routes/connections.d.ts.map +1 -0
- package/dist/routes/documents.d.ts +11 -0
- package/dist/routes/documents.d.ts.map +1 -0
- package/dist/routes/environments.d.ts +4 -0
- package/dist/routes/environments.d.ts.map +1 -0
- package/dist/routes/git-gateway.d.ts +21 -0
- package/dist/routes/git-gateway.d.ts.map +1 -0
- package/dist/routes/http-gateway.d.ts +13 -0
- package/dist/routes/http-gateway.d.ts.map +1 -0
- package/dist/routes/me.d.ts +12 -0
- package/dist/routes/me.d.ts.map +1 -0
- package/dist/routes/memberships.d.ts +12 -0
- package/dist/routes/memberships.d.ts.map +1 -0
- package/dist/routes/model-gateway.d.ts +12 -0
- package/dist/routes/model-gateway.d.ts.map +1 -0
- package/dist/routes/notifications.d.ts +4 -0
- package/dist/routes/notifications.d.ts.map +1 -0
- package/dist/routes/parse.d.ts +5 -0
- package/dist/routes/parse.d.ts.map +1 -0
- package/dist/routes/personal-environment.d.ts +13 -0
- package/dist/routes/personal-environment.d.ts.map +1 -0
- package/dist/routes/plugins.d.ts +4 -0
- package/dist/routes/plugins.d.ts.map +1 -0
- package/dist/routes/project-mcp.d.ts +25 -0
- package/dist/routes/project-mcp.d.ts.map +1 -0
- package/dist/routes/projects.d.ts +4 -0
- package/dist/routes/projects.d.ts.map +1 -0
- package/dist/routes/publications.d.ts +15 -0
- package/dist/routes/publications.d.ts.map +1 -0
- package/dist/routes/run-errors.d.ts +8 -0
- package/dist/routes/run-errors.d.ts.map +1 -0
- package/dist/routes/runs.d.ts +4 -0
- package/dist/routes/runs.d.ts.map +1 -0
- package/dist/routes/session-artifacts.d.ts +27 -0
- package/dist/routes/session-artifacts.d.ts.map +1 -0
- package/dist/routes/session-mailboxes.d.ts +4 -0
- package/dist/routes/session-mailboxes.d.ts.map +1 -0
- package/dist/routes/session-previews.d.ts +40 -0
- package/dist/routes/session-previews.d.ts.map +1 -0
- package/dist/routes/session-terminals.d.ts +15 -0
- package/dist/routes/session-terminals.d.ts.map +1 -0
- package/dist/routes/triggers.d.ts +20 -0
- package/dist/routes/triggers.d.ts.map +1 -0
- package/dist/routes/watchers.d.ts +4 -0
- package/dist/routes/watchers.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +11 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflow-enablements.d.ts +4 -0
- package/dist/routes/workflow-enablements.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +4 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/schemas.d.ts +17776 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/session-artifact-tools.d.ts +5 -0
- package/dist/session-artifact-tools.d.ts.map +1 -0
- package/dist/session-event-stream.d.ts +45 -0
- package/dist/session-event-stream.d.ts.map +1 -0
- package/dist/spa.d.ts +24 -0
- package/dist/spa.d.ts.map +1 -0
- package/dist/telemetry.d.ts +4 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/package.json +73 -4
- package/src/app-presentation-tools.ts +46 -0
- package/src/app.ts +115 -0
- package/src/http-identity.ts +145 -0
- package/src/hung-up.ts +13 -0
- package/src/index.ts +25 -0
- package/src/mcp-shared.ts +331 -0
- package/src/mcp.ts +10 -0
- package/src/plugin.ts +332 -0
- package/src/project-capabilities.ts +153 -0
- package/src/project-mcp-program.ts +607 -0
- package/src/project-mcp-surface.ts +881 -0
- package/src/request-logging.ts +47 -0
- package/src/routes/agent-capabilities.ts +109 -0
- package/src/routes/agent.ts +1566 -0
- package/src/routes/apps-mcp.ts +303 -0
- package/src/routes/apps.ts +692 -0
- package/src/routes/client-runners.ts +251 -0
- package/src/routes/code-hosts.ts +247 -0
- package/src/routes/connection-mcp.ts +135 -0
- package/src/routes/connections.ts +494 -0
- package/src/routes/documents.ts +538 -0
- package/src/routes/environments.ts +72 -0
- package/src/routes/git-gateway.ts +106 -0
- package/src/routes/http-gateway.ts +141 -0
- package/src/routes/me.ts +122 -0
- package/src/routes/memberships.ts +173 -0
- package/src/routes/model-gateway.ts +134 -0
- package/src/routes/notifications.ts +101 -0
- package/src/routes/parse.ts +32 -0
- package/src/routes/personal-environment.ts +146 -0
- package/src/routes/plugins.ts +341 -0
- package/src/routes/project-mcp.ts +394 -0
- package/src/routes/projects.ts +708 -0
- package/src/routes/publications.ts +222 -0
- package/src/routes/run-errors.ts +86 -0
- package/src/routes/runs.ts +253 -0
- package/src/routes/session-artifacts.ts +293 -0
- package/src/routes/session-mailboxes.ts +80 -0
- package/src/routes/session-previews.ts +224 -0
- package/src/routes/session-terminals.ts +209 -0
- package/src/routes/triggers.ts +241 -0
- package/src/routes/watchers.ts +94 -0
- package/src/routes/webhooks.ts +171 -0
- package/src/routes/workflow-enablements.ts +331 -0
- package/src/routes/workflows.ts +323 -0
- package/src/schemas.ts +2735 -0
- package/src/session-artifact-tools.ts +170 -0
- package/src/session-event-stream.ts +122 -0
- package/src/spa.ts +135 -0
- package/src/telemetry.ts +93 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import type { CatamorphicCore, Identity } from "@catamorphic/core";
|
|
2
|
+
import { MCP_POLL_RUN_TOOL, RunNotFoundError } from "@catamorphic/core";
|
|
3
|
+
import type { FastifyReply } from "fastify";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The JSON-RPC / MCP plumbing shared by the plugin's MCP endpoints — the
|
|
8
|
+
* apps server (`/projects/:id/apps-mcp`), the workflow-tools server
|
|
9
|
+
* (`/projects/:id/mcp`) and the connection broker (`/connection-mcp`) —
|
|
10
|
+
* and by hosts that forward MCP servers of their own. All are stateless
|
|
11
|
+
* Streamable-HTTP handlers speaking the `initialize`-handshake protocol era
|
|
12
|
+
* (≤2025-11-25): requests are independent, no sessions, notifications
|
|
13
|
+
* answered with a bare 202.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface JsonRpcMessage {
|
|
17
|
+
jsonrpc?: string;
|
|
18
|
+
id?: number | string | null;
|
|
19
|
+
method?: string;
|
|
20
|
+
params?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const SUPPORTED_PROTOCOL_VERSIONS = new Set([
|
|
24
|
+
"2024-11-05",
|
|
25
|
+
"2025-03-26",
|
|
26
|
+
"2025-06-18",
|
|
27
|
+
"2025-11-25",
|
|
28
|
+
]);
|
|
29
|
+
export const DEFAULT_PROTOCOL_VERSION = "2025-06-18";
|
|
30
|
+
|
|
31
|
+
/** The requested revision when supported, our default otherwise. */
|
|
32
|
+
export function negotiateProtocolVersion(requested: unknown): string {
|
|
33
|
+
return typeof requested === "string" &&
|
|
34
|
+
SUPPORTED_PROTOCOL_VERSIONS.has(requested)
|
|
35
|
+
? requested
|
|
36
|
+
: DEFAULT_PROTOCOL_VERSION;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A dispatch failure that should ride a specific JSON-RPC error code. */
|
|
40
|
+
export class McpRequestError extends Error {
|
|
41
|
+
constructor(
|
|
42
|
+
readonly code: number,
|
|
43
|
+
message: string,
|
|
44
|
+
) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.name = "McpRequestError";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The stateless request shell the endpoints share: body-shape validation,
|
|
52
|
+
* the notification 202, missing-method rejection, and error mapping.
|
|
53
|
+
* `dispatch` returns the JSON-RPC result for a method; it throws
|
|
54
|
+
* {@link McpRequestError} for coded failures (-32601 method-not-found,
|
|
55
|
+
* -32602 bad params) and anything else becomes -32603.
|
|
56
|
+
*/
|
|
57
|
+
export async function handleMcpPost(
|
|
58
|
+
reply: FastifyReply,
|
|
59
|
+
body: unknown,
|
|
60
|
+
dispatch: (
|
|
61
|
+
method: string,
|
|
62
|
+
params: Record<string, unknown>,
|
|
63
|
+
) => Promise<unknown>,
|
|
64
|
+
): Promise<FastifyReply> {
|
|
65
|
+
const message = body as JsonRpcMessage | null;
|
|
66
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
67
|
+
return rpcError(reply, null, -32600, "Expected a single JSON-RPC message");
|
|
68
|
+
}
|
|
69
|
+
// Notifications (initialized, cancelled, …) need no body in return.
|
|
70
|
+
if (message.id === undefined || message.id === null) {
|
|
71
|
+
return reply.status(202).send();
|
|
72
|
+
}
|
|
73
|
+
if (typeof message.method !== "string") {
|
|
74
|
+
return rpcError(reply, message.id, -32600, "Missing method");
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const result = await dispatch(message.method, message.params ?? {});
|
|
78
|
+
return rpcResult(reply, message.id, result);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof McpRequestError) {
|
|
81
|
+
return rpcError(reply, message.id, error.code, error.message);
|
|
82
|
+
}
|
|
83
|
+
return rpcError(
|
|
84
|
+
reply,
|
|
85
|
+
message.id,
|
|
86
|
+
-32603,
|
|
87
|
+
error instanceof Error ? error.message : "Internal error",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function rpcResult(
|
|
93
|
+
reply: FastifyReply,
|
|
94
|
+
id: number | string,
|
|
95
|
+
result: unknown,
|
|
96
|
+
): FastifyReply {
|
|
97
|
+
return reply
|
|
98
|
+
.header("content-type", "application/json")
|
|
99
|
+
.send({ jsonrpc: "2.0", id, result });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function rpcError(
|
|
103
|
+
reply: FastifyReply,
|
|
104
|
+
id: number | string | null,
|
|
105
|
+
code: number,
|
|
106
|
+
message: string,
|
|
107
|
+
): FastifyReply {
|
|
108
|
+
return reply
|
|
109
|
+
.header("content-type", "application/json")
|
|
110
|
+
.send({ jsonrpc: "2.0", id, error: { code, message } });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Tool result: JSON text for model readability plus `structuredContent`
|
|
115
|
+
* for structured consumers. MCP defines structured content as a JSON
|
|
116
|
+
* object, and clients such as Claude Code reject anything else, so arrays
|
|
117
|
+
* and primitives ride the text channel alone.
|
|
118
|
+
*/
|
|
119
|
+
export function toolValue(value: unknown): Record<string, unknown> {
|
|
120
|
+
return {
|
|
121
|
+
content: [{ type: "text", text: JSON.stringify(value) ?? "null" }],
|
|
122
|
+
...(typeof value === "object" && value !== null && !Array.isArray(value)
|
|
123
|
+
? { structuredContent: value }
|
|
124
|
+
: {}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function toolError(message: string): Record<string, unknown> {
|
|
129
|
+
return { content: [{ type: "text", text: message }], isError: true };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Another MCP server's tool, listed as that server declared it (ADR 0225).
|
|
134
|
+
* These endpoints speak revisions where an output schema describes an
|
|
135
|
+
* object, so any other is left out: advertising it would make clients
|
|
136
|
+
* reject the results it describes.
|
|
137
|
+
*/
|
|
138
|
+
export function forwardedTool(tool: {
|
|
139
|
+
name: string;
|
|
140
|
+
title?: string;
|
|
141
|
+
description?: string;
|
|
142
|
+
inputSchema: unknown;
|
|
143
|
+
outputSchema?: unknown;
|
|
144
|
+
annotations?: unknown;
|
|
145
|
+
}): Record<string, unknown> {
|
|
146
|
+
return {
|
|
147
|
+
name: tool.name,
|
|
148
|
+
...(tool.title ? { title: tool.title } : {}),
|
|
149
|
+
...(tool.description ? { description: tool.description } : {}),
|
|
150
|
+
inputSchema: tool.inputSchema,
|
|
151
|
+
...(isObjectSchema(tool.outputSchema)
|
|
152
|
+
? { outputSchema: tool.outputSchema }
|
|
153
|
+
: {}),
|
|
154
|
+
...(tool.annotations ? { annotations: tool.annotations } : {}),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const ForwardedToolResultSchema = z.looseObject({
|
|
159
|
+
content: z.array(z.unknown()),
|
|
160
|
+
structuredContent: z.unknown().optional(),
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Another MCP server's tool result, forwarded as it was sent: content,
|
|
165
|
+
* structured content, `isError` and `_meta` unchanged (ADR 0225). These
|
|
166
|
+
* revisions require structured content to be an object, so any other
|
|
167
|
+
* (which newer revisions allow) is left out, and becomes the text when the
|
|
168
|
+
* server sent none. Anything that is not a tool result answers as a value,
|
|
169
|
+
* never as a failure: the call has already run.
|
|
170
|
+
*/
|
|
171
|
+
export function forwardedToolResult(result: unknown): Record<string, unknown> {
|
|
172
|
+
const parsed = ForwardedToolResultSchema.safeParse(result);
|
|
173
|
+
if (!parsed.success) return toolValue(result);
|
|
174
|
+
const { structuredContent, ...rest } = parsed.data;
|
|
175
|
+
if (structuredContent === undefined) return rest;
|
|
176
|
+
if (isObject(structuredContent)) return { ...rest, structuredContent };
|
|
177
|
+
return rest.content.length > 0
|
|
178
|
+
? rest
|
|
179
|
+
: {
|
|
180
|
+
...rest,
|
|
181
|
+
content: [{ type: "text", text: JSON.stringify(structuredContent) }],
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
186
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function isObjectSchema(schema: unknown): boolean {
|
|
190
|
+
return isObject(schema) && schema.type === "object";
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Shared contract with `@catamorphic/app`'s MCP-host guest adapter (which
|
|
195
|
+
* duplicates the name — it cannot depend on core).
|
|
196
|
+
*/
|
|
197
|
+
export const POLL_RUN_TOOL = MCP_POLL_RUN_TOOL;
|
|
198
|
+
|
|
199
|
+
/** The poll tool served beside the real tools on both MCP endpoints. */
|
|
200
|
+
export const POLL_RUN_TOOL_DEFINITION: Record<string, unknown> = {
|
|
201
|
+
name: POLL_RUN_TOOL,
|
|
202
|
+
annotations: { readOnlyHint: true },
|
|
203
|
+
description:
|
|
204
|
+
"Poll a workflow run by its runId — one started asynchronously or one " +
|
|
205
|
+
"that detached mid-call. Returns the run's status, output (once " +
|
|
206
|
+
"completed), and batch progress.",
|
|
207
|
+
inputSchema: {
|
|
208
|
+
type: "object",
|
|
209
|
+
properties: { runId: { type: "string" } },
|
|
210
|
+
required: ["runId"],
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/** The poll tool's shared implementation, same on both endpoints. */
|
|
215
|
+
export async function callPollRunTool(
|
|
216
|
+
core: CatamorphicCore,
|
|
217
|
+
identity: Identity,
|
|
218
|
+
args: Record<string, unknown>,
|
|
219
|
+
): Promise<Record<string, unknown>> {
|
|
220
|
+
const runId = typeof args.runId === "string" ? args.runId : "";
|
|
221
|
+
try {
|
|
222
|
+
return toolValue(await fetchRunSnapshot(core, identity, runId));
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error instanceof RunNotFoundError) return toolError("Run not found");
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface RunSnapshotShape {
|
|
230
|
+
runId: string;
|
|
231
|
+
status: string;
|
|
232
|
+
output: unknown;
|
|
233
|
+
error: string | null;
|
|
234
|
+
progress?: {
|
|
235
|
+
discovered: number;
|
|
236
|
+
succeeded: number;
|
|
237
|
+
failed: number;
|
|
238
|
+
skipped: number;
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Mirrors the in-product broker's run snapshot (AppMount's shape). */
|
|
243
|
+
export async function fetchRunSnapshot(
|
|
244
|
+
core: CatamorphicCore,
|
|
245
|
+
identity: Identity,
|
|
246
|
+
runId: string,
|
|
247
|
+
): Promise<RunSnapshotShape> {
|
|
248
|
+
const run = await core.runs.get({ identity, runId });
|
|
249
|
+
const batch = (
|
|
250
|
+
run as {
|
|
251
|
+
batchScopes?: Array<{
|
|
252
|
+
discovered: number;
|
|
253
|
+
succeeded: number;
|
|
254
|
+
failed: number;
|
|
255
|
+
skipped: number;
|
|
256
|
+
}>;
|
|
257
|
+
}
|
|
258
|
+
).batchScopes?.[0];
|
|
259
|
+
return {
|
|
260
|
+
runId: run.id,
|
|
261
|
+
status: run.status,
|
|
262
|
+
output: (run as { result?: unknown }).result ?? null,
|
|
263
|
+
error: (run as { error?: string | null }).error ?? null,
|
|
264
|
+
...(batch
|
|
265
|
+
? {
|
|
266
|
+
progress: {
|
|
267
|
+
discovered: batch.discovered,
|
|
268
|
+
succeeded: batch.succeeded,
|
|
269
|
+
failed: batch.failed,
|
|
270
|
+
skipped: batch.skipped,
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
: {}),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** A tool another MCP server declared, as a forwarding endpoint lists it. */
|
|
278
|
+
export interface ForwardedToolDefinition {
|
|
279
|
+
name: string;
|
|
280
|
+
title?: string;
|
|
281
|
+
description?: string;
|
|
282
|
+
inputSchema: unknown;
|
|
283
|
+
outputSchema?: unknown;
|
|
284
|
+
annotations?: unknown;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const CallParamsSchema = z.object({
|
|
288
|
+
name: z.string().min(1),
|
|
289
|
+
arguments: z.record(z.string(), z.unknown()).default({}),
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A stateless MCP endpoint serving another server's tools: tools listed
|
|
294
|
+
* and results returned as that server declared and sent them (ADR 0225).
|
|
295
|
+
* `callTool` answers with the server's own result (any tool result, such
|
|
296
|
+
* as {@link toolError}, passes through), or throws {@link McpRequestError}
|
|
297
|
+
* for a refusal at the protocol boundary.
|
|
298
|
+
*/
|
|
299
|
+
export function serveForwardedTools(args: {
|
|
300
|
+
reply: FastifyReply;
|
|
301
|
+
body: unknown;
|
|
302
|
+
serverName: string;
|
|
303
|
+
listTools(): Promise<readonly ForwardedToolDefinition[]>;
|
|
304
|
+
callTool(call: {
|
|
305
|
+
name: string;
|
|
306
|
+
arguments: Record<string, unknown>;
|
|
307
|
+
}): Promise<unknown>;
|
|
308
|
+
}): Promise<FastifyReply> {
|
|
309
|
+
return handleMcpPost(args.reply, args.body, async (method, params) => {
|
|
310
|
+
switch (method) {
|
|
311
|
+
case "initialize":
|
|
312
|
+
return {
|
|
313
|
+
protocolVersion: negotiateProtocolVersion(params.protocolVersion),
|
|
314
|
+
capabilities: { tools: {} },
|
|
315
|
+
serverInfo: { name: args.serverName, version: "1" },
|
|
316
|
+
};
|
|
317
|
+
case "ping":
|
|
318
|
+
return {};
|
|
319
|
+
case "tools/list":
|
|
320
|
+
return { tools: (await args.listTools()).map(forwardedTool) };
|
|
321
|
+
case "tools/call": {
|
|
322
|
+
const call = CallParamsSchema.safeParse(params);
|
|
323
|
+
if (!call.success)
|
|
324
|
+
throw new McpRequestError(-32602, "Invalid tool call");
|
|
325
|
+
return forwardedToolResult(await args.callTool(call.data));
|
|
326
|
+
}
|
|
327
|
+
default:
|
|
328
|
+
throw new McpRequestError(-32601, "Method not found");
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
package/src/mcp.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP serving for hosts: an endpoint that forwards another MCP server's
|
|
3
|
+
* tools the way the plugin's own connection endpoint does (ADR 0225).
|
|
4
|
+
*/
|
|
5
|
+
export {
|
|
6
|
+
type ForwardedToolDefinition,
|
|
7
|
+
McpRequestError,
|
|
8
|
+
serveForwardedTools,
|
|
9
|
+
toolError,
|
|
10
|
+
} from "./mcp-shared.js";
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import type { CatamorphicCore, Identity } from "@catamorphic/core";
|
|
2
|
+
import {
|
|
3
|
+
AccessDeniedError,
|
|
4
|
+
DeploymentBlockedError,
|
|
5
|
+
EnvironmentPolicyInvalidError,
|
|
6
|
+
ProjectNotFoundError,
|
|
7
|
+
ServerDraftError,
|
|
8
|
+
SessionArtifactConflictError,
|
|
9
|
+
SessionArtifactNotFoundError,
|
|
10
|
+
SessionArtifactValidationError,
|
|
11
|
+
} from "@catamorphic/core";
|
|
12
|
+
import {
|
|
13
|
+
DraftBinaryResolutionError,
|
|
14
|
+
DraftBusyError,
|
|
15
|
+
DraftIgnoredPathError,
|
|
16
|
+
DraftPathError,
|
|
17
|
+
DraftRefNotAllowedError,
|
|
18
|
+
DraftsUnsupportedError,
|
|
19
|
+
DraftUnresolvedError,
|
|
20
|
+
InvalidBaseError,
|
|
21
|
+
InvalidRefNameError,
|
|
22
|
+
NoDraftError,
|
|
23
|
+
RefMovedError,
|
|
24
|
+
} from "@catamorphic/git";
|
|
25
|
+
import type { FastifyPluginAsync } from "fastify";
|
|
26
|
+
import {
|
|
27
|
+
serializerCompiler,
|
|
28
|
+
validatorCompiler,
|
|
29
|
+
} from "fastify-type-provider-zod";
|
|
30
|
+
import {
|
|
31
|
+
attachIdentity,
|
|
32
|
+
HttpIdentityError,
|
|
33
|
+
type IdentityResolver,
|
|
34
|
+
} from "./http-identity.js";
|
|
35
|
+
import { apiErrorResponse } from "./request-logging.js";
|
|
36
|
+
import { registerAgentRoutes } from "./routes/agent.js";
|
|
37
|
+
import { registerAgentCapabilityRoutes } from "./routes/agent-capabilities.js";
|
|
38
|
+
import { registerAppRoutes } from "./routes/apps.js";
|
|
39
|
+
import { registerAppsMcpRoutes } from "./routes/apps-mcp.js";
|
|
40
|
+
import { registerClientRunnerRoutes } from "./routes/client-runners.js";
|
|
41
|
+
import { registerCodeHostRoutes } from "./routes/code-hosts.js";
|
|
42
|
+
import { registerConnectionMcpRoutes } from "./routes/connection-mcp.js";
|
|
43
|
+
import { registerConnectionRoutes } from "./routes/connections.js";
|
|
44
|
+
import { registerDocumentRoutes } from "./routes/documents.js";
|
|
45
|
+
import { registerEnvironmentRoutes } from "./routes/environments.js";
|
|
46
|
+
import { registerGitGatewayRoutes } from "./routes/git-gateway.js";
|
|
47
|
+
import { registerHttpGatewayRoutes } from "./routes/http-gateway.js";
|
|
48
|
+
import { registerMeRoutes } from "./routes/me.js";
|
|
49
|
+
import { registerMembershipRoutes } from "./routes/memberships.js";
|
|
50
|
+
import { registerModelGatewayRoutes } from "./routes/model-gateway.js";
|
|
51
|
+
import { registerNotificationRoutes } from "./routes/notifications.js";
|
|
52
|
+
import { registerParseRoutes } from "./routes/parse.js";
|
|
53
|
+
import { registerPersonalEnvironmentRoutes } from "./routes/personal-environment.js";
|
|
54
|
+
import { registerPluginRoutes } from "./routes/plugins.js";
|
|
55
|
+
import { registerProjectMcpRoutes } from "./routes/project-mcp.js";
|
|
56
|
+
import { registerProjectRoutes } from "./routes/projects.js";
|
|
57
|
+
import { registerPublicationRoutes } from "./routes/publications.js";
|
|
58
|
+
import { registerRunRoutes } from "./routes/runs.js";
|
|
59
|
+
import { registerSessionArtifactRoutes } from "./routes/session-artifacts.js";
|
|
60
|
+
import { registerSessionMailboxRoutes } from "./routes/session-mailboxes.js";
|
|
61
|
+
import { registerSessionPreviewRoutes } from "./routes/session-previews.js";
|
|
62
|
+
import { registerSessionTerminalRoutes } from "./routes/session-terminals.js";
|
|
63
|
+
import { registerTriggerRoutes } from "./routes/triggers.js";
|
|
64
|
+
import { registerWatcherRoutes } from "./routes/watchers.js";
|
|
65
|
+
import { registerWebhookRoutes } from "./routes/webhooks.js";
|
|
66
|
+
import { registerWorkflowEnablementRoutes } from "./routes/workflow-enablements.js";
|
|
67
|
+
import { registerWorkflowRoutes } from "./routes/workflows.js";
|
|
68
|
+
|
|
69
|
+
export interface CatamorphicPluginOptions {
|
|
70
|
+
/**
|
|
71
|
+
* Wired catamorphic services (build them with `@catamorphic/server-sdk` or
|
|
72
|
+
* `createCatamorphicCore`). Required for any non-trivial route; when
|
|
73
|
+
* omitted, project / run / workflow endpoints respond 503.
|
|
74
|
+
*/
|
|
75
|
+
core?: CatamorphicCore;
|
|
76
|
+
/**
|
|
77
|
+
* Who is calling. Runs on every request before any route; returns the root
|
|
78
|
+
* identity for the host itself, a scoped identity with project permissions
|
|
79
|
+
* for a member (an admin, an app user, a customer), or `null` for 401. Typically a few lines that read the host's
|
|
80
|
+
* own session and its entitlement table:
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* identity: async (req) => {
|
|
84
|
+
* const session = await verifySession(req);
|
|
85
|
+
* if (!session) return null;
|
|
86
|
+
* const base = { tenantId: session.orgId, externalUserId: session.userId };
|
|
87
|
+
* if (session.isEmployee) return base;
|
|
88
|
+
* return { ...base, scope: await entitlementsFor(session.userId) };
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* Hosts whose auth terminates in front of the plugin pass
|
|
93
|
+
* `identityFromHeaders()`.
|
|
94
|
+
*/
|
|
95
|
+
identity: IdentityResolver;
|
|
96
|
+
/**
|
|
97
|
+
* Coarse host feature switches (ADR 0055). Enforced server-side by the
|
|
98
|
+
* routes concerned AND advertised on `GET /me`, so a client (the desktop,
|
|
99
|
+
* a member's own agent) can show what is possible instead of discovering
|
|
100
|
+
* it by 403. Scope stays the mechanism for "may not"; these are the few
|
|
101
|
+
* things a host decides for everyone.
|
|
102
|
+
*/
|
|
103
|
+
features?: Partial<HostFeatures>;
|
|
104
|
+
/**
|
|
105
|
+
* The API base outsiders reach this plugin at (e.g.
|
|
106
|
+
* `https://brain.acme.com/api`), for URLs handed to other services such
|
|
107
|
+
* as webhook URLs. Defaults to the request's own origin and mount prefix.
|
|
108
|
+
*/
|
|
109
|
+
publicApiBase?: string;
|
|
110
|
+
/** Host additions to the project MCP endpoint (ADR 0166). */
|
|
111
|
+
projectMcp?: ProjectMcpHost;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* What a host adds to `POST /projects/:projectId/mcp`: the name a client
|
|
116
|
+
* shows, a paragraph appended to the orientation every client receives at
|
|
117
|
+
* `initialize`, and tools the host owns (the Work server's shares). Host
|
|
118
|
+
* tools see the caller's identity and enforce their own permissions.
|
|
119
|
+
*/
|
|
120
|
+
export interface ProjectMcpHost {
|
|
121
|
+
serverInfo?: { name: string; title: string; version?: string };
|
|
122
|
+
instructions?: string;
|
|
123
|
+
tools?: (args: {
|
|
124
|
+
identity: Identity;
|
|
125
|
+
projectId: string;
|
|
126
|
+
}) => ProjectMcpHostTool[];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface ProjectMcpHostTool {
|
|
130
|
+
definition: {
|
|
131
|
+
name: string;
|
|
132
|
+
description: string;
|
|
133
|
+
inputSchema: Record<string, unknown>;
|
|
134
|
+
annotations?: Record<string, unknown>;
|
|
135
|
+
};
|
|
136
|
+
/** Returns the tool's result; a thrown error becomes a tool error. */
|
|
137
|
+
call: (args: Record<string, unknown>) => Promise<unknown>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** What a host enables for all callers; defaults are the most permissive. */
|
|
141
|
+
export interface HostFeatures {
|
|
142
|
+
/** Which publication audiences may be created: both, members only, or none. */
|
|
143
|
+
publications: "public" | "members" | false;
|
|
144
|
+
/** Whether members may propose program changes. */
|
|
145
|
+
proposals: boolean;
|
|
146
|
+
/** Whether the project MCP endpoint is served. */
|
|
147
|
+
mcp: boolean;
|
|
148
|
+
/** Largest store document accepted (bytes). */
|
|
149
|
+
storeUploadMaxBytes: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const DEFAULT_HOST_FEATURES: HostFeatures = {
|
|
153
|
+
publications: "public",
|
|
154
|
+
proposals: true,
|
|
155
|
+
mcp: true,
|
|
156
|
+
storeUploadMaxBytes: 64 * 1024 * 1024,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export interface RouteContext {
|
|
160
|
+
core?: CatamorphicCore;
|
|
161
|
+
features: HostFeatures;
|
|
162
|
+
publicApiBase?: string;
|
|
163
|
+
projectMcp?: ProjectMcpHost;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Mountable Fastify plugin exposing the catamorphic HTTP API. Hosts register
|
|
168
|
+
* it on their own Fastify instance:
|
|
169
|
+
*
|
|
170
|
+
* ```ts
|
|
171
|
+
* app.register(catamorphicPlugin, { core, prefix: "/api" });
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* The generated api-client (`@catamorphic/api-client`) expects the routes
|
|
175
|
+
* under `/api`, so mount with that prefix unless you also change the client's
|
|
176
|
+
* `baseUrl`. The plugin is fully encapsulated: it sets Zod validator /
|
|
177
|
+
* serializer compilers and an error handler for its own scope only, and it
|
|
178
|
+
* registers no CORS — the host owns cross-origin policy.
|
|
179
|
+
*
|
|
180
|
+
* Identity comes from the host's `identity` resolver — the only identity
|
|
181
|
+
* mechanism; there is no header fallback unless the host opts into
|
|
182
|
+
* `identityFromHeaders()` explicitly.
|
|
183
|
+
*/
|
|
184
|
+
export const catamorphicPlugin: FastifyPluginAsync<
|
|
185
|
+
CatamorphicPluginOptions
|
|
186
|
+
> = async (app, opts) => {
|
|
187
|
+
app.setValidatorCompiler(validatorCompiler);
|
|
188
|
+
app.setSerializerCompiler(serializerCompiler);
|
|
189
|
+
|
|
190
|
+
app.setErrorHandler((err, _req, reply) => {
|
|
191
|
+
if (err instanceof HttpIdentityError) {
|
|
192
|
+
return reply.status(400).send({ error: err.message });
|
|
193
|
+
}
|
|
194
|
+
if (
|
|
195
|
+
err instanceof SessionArtifactNotFoundError ||
|
|
196
|
+
err instanceof SessionArtifactConflictError ||
|
|
197
|
+
err instanceof SessionArtifactValidationError
|
|
198
|
+
) {
|
|
199
|
+
return reply.status(err.statusCode).send({ error: err.message });
|
|
200
|
+
}
|
|
201
|
+
if (err instanceof AccessDeniedError) {
|
|
202
|
+
return reply.status(403).send({ error: err.message });
|
|
203
|
+
}
|
|
204
|
+
if (err instanceof ProjectNotFoundError) {
|
|
205
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
206
|
+
}
|
|
207
|
+
// The project's committed policy is invalid: its owners fix it.
|
|
208
|
+
if (err instanceof EnvironmentPolicyInvalidError) {
|
|
209
|
+
return reply
|
|
210
|
+
.status(422)
|
|
211
|
+
.send({ error: err.message, code: "environment_policy_invalid" });
|
|
212
|
+
}
|
|
213
|
+
// A normal state the person resolves (record changes first), not a fault.
|
|
214
|
+
if (
|
|
215
|
+
err instanceof DeploymentBlockedError ||
|
|
216
|
+
err instanceof ServerDraftError ||
|
|
217
|
+
err instanceof DraftBusyError ||
|
|
218
|
+
err instanceof DraftUnresolvedError ||
|
|
219
|
+
err instanceof DraftsUnsupportedError
|
|
220
|
+
) {
|
|
221
|
+
return reply.status(409).send({ error: err.message });
|
|
222
|
+
}
|
|
223
|
+
// A publish that lost its race to another change of the project.
|
|
224
|
+
if (err instanceof RefMovedError) {
|
|
225
|
+
return reply.status(409).send({
|
|
226
|
+
error:
|
|
227
|
+
"The project changed on the server while publishing. Download its changes, then publish again.",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
// A malformed ref or base, a path the program never holds, a text
|
|
231
|
+
// resolution for a binary file, or a change to a read-only view.
|
|
232
|
+
if (
|
|
233
|
+
err instanceof InvalidRefNameError ||
|
|
234
|
+
err instanceof InvalidBaseError ||
|
|
235
|
+
err instanceof DraftIgnoredPathError ||
|
|
236
|
+
err instanceof DraftBinaryResolutionError ||
|
|
237
|
+
err instanceof NoDraftError
|
|
238
|
+
) {
|
|
239
|
+
return reply.status(400).send({ error: err.message });
|
|
240
|
+
}
|
|
241
|
+
// Another member's draft, or a path the draft does not hold.
|
|
242
|
+
if (
|
|
243
|
+
err instanceof DraftRefNotAllowedError ||
|
|
244
|
+
(err instanceof DraftPathError && err.code === "ENOENT")
|
|
245
|
+
) {
|
|
246
|
+
return reply.status(404).send({ error: err.message });
|
|
247
|
+
}
|
|
248
|
+
if (err instanceof DraftPathError) {
|
|
249
|
+
return reply.status(409).send({ error: err.message });
|
|
250
|
+
}
|
|
251
|
+
return apiErrorResponse(err, _req, reply);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// Identity is resolved once per request, before any route runs, and
|
|
255
|
+
// stashed on the request for `resolveIdentity`. A `null` is the host
|
|
256
|
+
// saying "not signed in".
|
|
257
|
+
app.addHook("onRequest", async (request, reply) => {
|
|
258
|
+
// Public publications (ADR 0055) are the one anonymous surface: the
|
|
259
|
+
// route mints its own document-scoped identity. A missing/invalid
|
|
260
|
+
// session is still resolved (a signed-in member reading a public link
|
|
261
|
+
// is fine) but never required.
|
|
262
|
+
const isPublic =
|
|
263
|
+
(request.routeOptions.config as { public?: boolean } | undefined)
|
|
264
|
+
?.public === true;
|
|
265
|
+
let identity: Awaited<ReturnType<typeof opts.identity>> = null;
|
|
266
|
+
try {
|
|
267
|
+
identity = await opts.identity(request);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (!isPublic) throw error;
|
|
270
|
+
}
|
|
271
|
+
if (!identity) {
|
|
272
|
+
if (isPublic) return;
|
|
273
|
+
return reply.status(401).send({ error: "Unauthorized" });
|
|
274
|
+
}
|
|
275
|
+
const runnerId = request.headers["x-catamorphic-runner"];
|
|
276
|
+
attachIdentity(
|
|
277
|
+
request,
|
|
278
|
+
typeof runnerId === "string" && /^[0-9a-f-]{36}$/i.test(runnerId)
|
|
279
|
+
? { ...identity, clientRunnerId: runnerId }
|
|
280
|
+
: identity,
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const ctx: RouteContext = {
|
|
285
|
+
core: opts.core,
|
|
286
|
+
...(opts.projectMcp ? { projectMcp: opts.projectMcp } : {}),
|
|
287
|
+
...(opts.publicApiBase
|
|
288
|
+
? { publicApiBase: opts.publicApiBase.replace(/\/$/, "") }
|
|
289
|
+
: {}),
|
|
290
|
+
// Explicit `undefined` from a host (a field computed from env) must not
|
|
291
|
+
// erase the default.
|
|
292
|
+
features: {
|
|
293
|
+
...DEFAULT_HOST_FEATURES,
|
|
294
|
+
...Object.fromEntries(
|
|
295
|
+
Object.entries(opts.features ?? {}).filter(([, v]) => v !== undefined),
|
|
296
|
+
),
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
registerMeRoutes(app, ctx);
|
|
301
|
+
registerProjectRoutes(app, ctx);
|
|
302
|
+
registerEnvironmentRoutes(app, ctx);
|
|
303
|
+
registerPersonalEnvironmentRoutes(app, ctx);
|
|
304
|
+
registerConnectionRoutes(app, ctx);
|
|
305
|
+
registerConnectionMcpRoutes(app, ctx);
|
|
306
|
+
registerGitGatewayRoutes(app, ctx);
|
|
307
|
+
registerModelGatewayRoutes(app, ctx);
|
|
308
|
+
registerHttpGatewayRoutes(app, ctx);
|
|
309
|
+
registerWorkflowRoutes(app, ctx);
|
|
310
|
+
registerWorkflowEnablementRoutes(app, ctx);
|
|
311
|
+
registerTriggerRoutes(app, ctx);
|
|
312
|
+
registerRunRoutes(app, ctx);
|
|
313
|
+
registerAgentRoutes(app, ctx);
|
|
314
|
+
registerClientRunnerRoutes(app, ctx);
|
|
315
|
+
registerSessionMailboxRoutes(app, ctx);
|
|
316
|
+
registerSessionTerminalRoutes(app, ctx);
|
|
317
|
+
registerSessionPreviewRoutes(app, ctx);
|
|
318
|
+
registerNotificationRoutes(app, ctx);
|
|
319
|
+
registerWatcherRoutes(app, ctx);
|
|
320
|
+
registerMembershipRoutes(app, ctx);
|
|
321
|
+
registerDocumentRoutes(app, ctx);
|
|
322
|
+
registerPublicationRoutes(app, ctx);
|
|
323
|
+
registerWebhookRoutes(app, ctx);
|
|
324
|
+
registerAppRoutes(app, ctx);
|
|
325
|
+
registerSessionArtifactRoutes(app, ctx);
|
|
326
|
+
registerAppsMcpRoutes(app, ctx);
|
|
327
|
+
registerProjectMcpRoutes(app, ctx);
|
|
328
|
+
registerAgentCapabilityRoutes(app, ctx);
|
|
329
|
+
registerCodeHostRoutes(app, ctx);
|
|
330
|
+
registerPluginRoutes(app, ctx);
|
|
331
|
+
registerParseRoutes(app, ctx);
|
|
332
|
+
};
|