@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.
- 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,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AccessDeniedError,
|
|
3
|
+
RemoteExecutorLeaseLostError,
|
|
4
|
+
RemoteOperationResultSchema,
|
|
5
|
+
RemoteReceiptRefusedError,
|
|
6
|
+
SealedRemoteOperationSchema,
|
|
7
|
+
} from "@catamorphic/core";
|
|
8
|
+
import { ExecutorPublicKeySchema } from "@catamorphic/sandbox";
|
|
9
|
+
import type { FastifyInstance } from "fastify";
|
|
10
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import type { RouteContext } from "../app.js";
|
|
13
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
14
|
+
import { hungUp } from "../hung-up.js";
|
|
15
|
+
import { ErrorSchema, ProjectIdParamsSchema } from "../schemas.js";
|
|
16
|
+
|
|
17
|
+
const Lease = z.object({ id: z.string().uuid(), token: z.string().uuid() });
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A member's This machine runner (ADRs 0098, 0187). It registers the public
|
|
21
|
+
* key its operations are sealed to, and poll hands them out sealed (ADR
|
|
22
|
+
* 0207). Poll long-polls; a 409 from poll or renew means the lease moved on
|
|
23
|
+
* and the runner registers again, a 409 from complete refuses only that
|
|
24
|
+
* receipt, and a 403 means the member may no longer serve this Environment.
|
|
25
|
+
*/
|
|
26
|
+
export function registerClientRunnerRoutes(
|
|
27
|
+
app: FastifyInstance,
|
|
28
|
+
ctx: RouteContext,
|
|
29
|
+
) {
|
|
30
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
31
|
+
typed.post(
|
|
32
|
+
"/projects/:projectId/client-runners",
|
|
33
|
+
{
|
|
34
|
+
schema: {
|
|
35
|
+
params: ProjectIdParamsSchema,
|
|
36
|
+
body: z.object({
|
|
37
|
+
id: z.string().uuid(),
|
|
38
|
+
environment: z.string().min(1),
|
|
39
|
+
label: z.string().min(1).max(100),
|
|
40
|
+
workspaceRoot: z.string().min(1).max(4096),
|
|
41
|
+
resourceLimits: z
|
|
42
|
+
.array(z.enum(["cpuMillis", "memoryMb", "storageMb", "gpu"]))
|
|
43
|
+
.optional(),
|
|
44
|
+
isolation: z.enum(["none", "process", "sandbox"]).optional(),
|
|
45
|
+
/** The runner's provider runs background processes (ADR 0174). */
|
|
46
|
+
processes: z.boolean().optional(),
|
|
47
|
+
/** What its sandboxes can be given (ADRs 0176, 0208). */
|
|
48
|
+
capabilities: z
|
|
49
|
+
.array(
|
|
50
|
+
z.enum([
|
|
51
|
+
"images",
|
|
52
|
+
"images.build",
|
|
53
|
+
"containers",
|
|
54
|
+
"network.policy",
|
|
55
|
+
"network.setup",
|
|
56
|
+
"volumes",
|
|
57
|
+
]),
|
|
58
|
+
)
|
|
59
|
+
.optional(),
|
|
60
|
+
/**
|
|
61
|
+
* The runner's X25519 public key, raw and base64: its operations
|
|
62
|
+
* are sealed to it (ADR 0207). A runner without one receives
|
|
63
|
+
* nothing.
|
|
64
|
+
*/
|
|
65
|
+
publicKey: ExecutorPublicKeySchema.optional(),
|
|
66
|
+
}),
|
|
67
|
+
response: {
|
|
68
|
+
200: Lease,
|
|
69
|
+
400: ErrorSchema,
|
|
70
|
+
403: ErrorSchema,
|
|
71
|
+
409: ErrorSchema,
|
|
72
|
+
503: ErrorSchema,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
async (request, reply) => {
|
|
77
|
+
if (!ctx.core?.clientRunners)
|
|
78
|
+
return reply
|
|
79
|
+
.status(503)
|
|
80
|
+
.send({ error: "Client execution is not enabled by this host" });
|
|
81
|
+
const { publicKey, ...body } = request.body;
|
|
82
|
+
if (!publicKey)
|
|
83
|
+
return reply.status(400).send({
|
|
84
|
+
error:
|
|
85
|
+
"This version of Work cannot receive operations from this server. Update Work on this computer.",
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
return reply.send(
|
|
89
|
+
await ctx.core.clientRunners.register({
|
|
90
|
+
...body,
|
|
91
|
+
publicKey,
|
|
92
|
+
projectId: request.params.projectId,
|
|
93
|
+
identity: resolveIdentity(request),
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof AccessDeniedError)
|
|
98
|
+
return reply.status(403).send({ error: error.message });
|
|
99
|
+
return reply.status(409).send({
|
|
100
|
+
error:
|
|
101
|
+
error instanceof Error
|
|
102
|
+
? error.message
|
|
103
|
+
: "Client registration failed",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
typed.post(
|
|
109
|
+
"/client-runners/poll",
|
|
110
|
+
{
|
|
111
|
+
schema: {
|
|
112
|
+
// A retried poll repeats its id and receives what that poll took;
|
|
113
|
+
// `max` is the runner's free slots.
|
|
114
|
+
body: Lease.extend({
|
|
115
|
+
pollId: z.string().uuid(),
|
|
116
|
+
max: z.number().int().min(1).max(64).optional(),
|
|
117
|
+
}),
|
|
118
|
+
response: {
|
|
119
|
+
// Each operation sealed to the runner's key (ADR 0207).
|
|
120
|
+
200: z.array(
|
|
121
|
+
z.object({
|
|
122
|
+
id: z.string().uuid(),
|
|
123
|
+
operation: SealedRemoteOperationSchema,
|
|
124
|
+
}),
|
|
125
|
+
),
|
|
126
|
+
403: ErrorSchema,
|
|
127
|
+
409: ErrorSchema,
|
|
128
|
+
503: ErrorSchema,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
async (request, reply) => {
|
|
133
|
+
if (!ctx.core?.clientRunners)
|
|
134
|
+
return reply
|
|
135
|
+
.status(503)
|
|
136
|
+
.send({ error: "Client execution is not enabled by this host" });
|
|
137
|
+
try {
|
|
138
|
+
return reply.send(
|
|
139
|
+
await ctx.core.clientRunners.poll({
|
|
140
|
+
...request.body,
|
|
141
|
+
identity: resolveIdentity(request),
|
|
142
|
+
signal: hungUp(reply),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof RemoteExecutorLeaseLostError)
|
|
147
|
+
return reply.status(409).send({ error: error.message });
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
typed.post(
|
|
153
|
+
"/client-runners/renew",
|
|
154
|
+
{
|
|
155
|
+
schema: {
|
|
156
|
+
body: Lease,
|
|
157
|
+
response: {
|
|
158
|
+
200: z.object({ ok: z.boolean() }),
|
|
159
|
+
403: ErrorSchema,
|
|
160
|
+
409: ErrorSchema,
|
|
161
|
+
503: ErrorSchema,
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
async (request, reply) => {
|
|
166
|
+
if (!ctx.core?.clientRunners)
|
|
167
|
+
return reply
|
|
168
|
+
.status(503)
|
|
169
|
+
.send({ error: "Client execution is not enabled by this host" });
|
|
170
|
+
try {
|
|
171
|
+
await ctx.core.clientRunners.renew({
|
|
172
|
+
...request.body,
|
|
173
|
+
identity: resolveIdentity(request),
|
|
174
|
+
});
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (error instanceof RemoteExecutorLeaseLostError)
|
|
177
|
+
return reply.status(409).send({ error: error.message });
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
return { ok: true };
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
typed.post(
|
|
184
|
+
"/client-runners/complete",
|
|
185
|
+
{
|
|
186
|
+
// A receipt carries a whole result: a 1 MiB process read or a
|
|
187
|
+
// downloaded file, escaped as JSON. Same bound as worker receipts.
|
|
188
|
+
bodyLimit: 64 * 1024 * 1024,
|
|
189
|
+
schema: {
|
|
190
|
+
body: Lease.extend({
|
|
191
|
+
jobId: z.string().uuid(),
|
|
192
|
+
response: RemoteOperationResultSchema.optional(),
|
|
193
|
+
error: z.string().max(4000).optional(),
|
|
194
|
+
}),
|
|
195
|
+
response: {
|
|
196
|
+
200: z.object({ ok: z.boolean() }),
|
|
197
|
+
403: ErrorSchema,
|
|
198
|
+
409: ErrorSchema,
|
|
199
|
+
503: ErrorSchema,
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
async (request, reply) => {
|
|
204
|
+
if (!ctx.core?.clientRunners)
|
|
205
|
+
return reply
|
|
206
|
+
.status(503)
|
|
207
|
+
.send({ error: "Client execution is not enabled by this host" });
|
|
208
|
+
try {
|
|
209
|
+
return reply.send(
|
|
210
|
+
await ctx.core.clientRunners.complete({
|
|
211
|
+
...request.body,
|
|
212
|
+
identity: resolveIdentity(request),
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// The operation settled or was abandoned, or the lease moved on:
|
|
217
|
+
// the runner drops this receipt.
|
|
218
|
+
if (
|
|
219
|
+
error instanceof RemoteReceiptRefusedError ||
|
|
220
|
+
error instanceof RemoteExecutorLeaseLostError
|
|
221
|
+
)
|
|
222
|
+
return reply.status(409).send({ error: error.message });
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
);
|
|
227
|
+
typed.post(
|
|
228
|
+
"/client-runners/disconnect",
|
|
229
|
+
{
|
|
230
|
+
schema: {
|
|
231
|
+
body: Lease,
|
|
232
|
+
response: {
|
|
233
|
+
200: z.object({ ok: z.boolean() }),
|
|
234
|
+
403: ErrorSchema,
|
|
235
|
+
503: ErrorSchema,
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
async (request, reply) => {
|
|
240
|
+
if (!ctx.core?.clientRunners)
|
|
241
|
+
return reply
|
|
242
|
+
.status(503)
|
|
243
|
+
.send({ error: "Client execution is not enabled by this host" });
|
|
244
|
+
await ctx.core.clientRunners.disconnect({
|
|
245
|
+
...request.body,
|
|
246
|
+
identity: resolveIdentity(request),
|
|
247
|
+
});
|
|
248
|
+
return { ok: true };
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AccessDeniedError,
|
|
3
|
+
CodeHostNotConnectedError,
|
|
4
|
+
CodeHostUnsupportedError,
|
|
5
|
+
ConnectionPermissionDeniedError,
|
|
6
|
+
ConnectionUnavailableError,
|
|
7
|
+
ProjectAlreadyLinkedError,
|
|
8
|
+
ProjectNotDeployedError,
|
|
9
|
+
ProjectNotFoundError,
|
|
10
|
+
ReplicaClaimBusyError,
|
|
11
|
+
} from "@catamorphic/core";
|
|
12
|
+
import type { FastifyInstance, FastifyReply } from "fastify";
|
|
13
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
import type { RouteContext } from "../app.js";
|
|
16
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
17
|
+
import {
|
|
18
|
+
AuthorizationChallengeSchema,
|
|
19
|
+
CodeHostImportSchema,
|
|
20
|
+
CodeHostParamsSchema,
|
|
21
|
+
CodeHostPublishResultSchema,
|
|
22
|
+
CodeHostPublishSchema,
|
|
23
|
+
CodeHostRepositorySchema,
|
|
24
|
+
ConnectionRecordSchema,
|
|
25
|
+
ErrorSchema,
|
|
26
|
+
ProjectIdParamsSchema,
|
|
27
|
+
ProjectSchema,
|
|
28
|
+
} from "../schemas.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Code hosts over connections (ADR 0177): the caller's personal connection
|
|
32
|
+
* to a code host's provider, the repositories it reaches, repository import,
|
|
33
|
+
* and publishing a project. A personal authorization completes through
|
|
34
|
+
* `/connection-authorizations/complete` (or the provider's callback) and is
|
|
35
|
+
* revoked with `DELETE /connections/:id`, like any connection.
|
|
36
|
+
*/
|
|
37
|
+
export function registerCodeHostRoutes(
|
|
38
|
+
app: FastifyInstance,
|
|
39
|
+
ctx: RouteContext,
|
|
40
|
+
): void {
|
|
41
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
42
|
+
const unavailable = { error: "No code host is configured" };
|
|
43
|
+
|
|
44
|
+
typed.route({
|
|
45
|
+
method: "GET",
|
|
46
|
+
url: "/code-hosts",
|
|
47
|
+
schema: {
|
|
48
|
+
response: {
|
|
49
|
+
200: z.array(
|
|
50
|
+
z.object({
|
|
51
|
+
provider: z.string(),
|
|
52
|
+
displayName: z.string(),
|
|
53
|
+
/** The caller's personal connection, when there is one. */
|
|
54
|
+
connection: ConnectionRecordSchema.nullable(),
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
handler: async (request, reply) => {
|
|
60
|
+
const codeHosts = ctx.core?.codeHosts;
|
|
61
|
+
if (!codeHosts?.available) return reply.send([]);
|
|
62
|
+
const identity = resolveIdentity(request);
|
|
63
|
+
return reply.send(
|
|
64
|
+
await Promise.all(
|
|
65
|
+
codeHosts.list().map(async (host) => ({
|
|
66
|
+
...host,
|
|
67
|
+
connection:
|
|
68
|
+
(await codeHosts.personalConnection({
|
|
69
|
+
identity,
|
|
70
|
+
provider: host.provider,
|
|
71
|
+
})) ?? null,
|
|
72
|
+
})),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
typed.route({
|
|
79
|
+
method: "POST",
|
|
80
|
+
url: "/code-hosts/:provider/connection/authorize",
|
|
81
|
+
schema: {
|
|
82
|
+
params: CodeHostParamsSchema,
|
|
83
|
+
body: z.object({ redirectUri: z.string().url() }),
|
|
84
|
+
response: {
|
|
85
|
+
200: z.object({
|
|
86
|
+
authorizationId: z.string().min(1),
|
|
87
|
+
challenge: AuthorizationChallengeSchema,
|
|
88
|
+
}),
|
|
89
|
+
404: ErrorSchema,
|
|
90
|
+
409: ErrorSchema,
|
|
91
|
+
503: ErrorSchema,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
handler: async (request, reply) => {
|
|
95
|
+
const connections = ctx.core?.connections;
|
|
96
|
+
if (!connections || !ctx.core?.codeHosts.available)
|
|
97
|
+
return reply.status(503).send(unavailable);
|
|
98
|
+
if (
|
|
99
|
+
!ctx.core.codeHosts
|
|
100
|
+
.list()
|
|
101
|
+
.some((host) => host.provider === request.params.provider)
|
|
102
|
+
)
|
|
103
|
+
return reply.status(404).send({ error: "Unknown code host" });
|
|
104
|
+
try {
|
|
105
|
+
return reply.send(
|
|
106
|
+
await connections.beginPersonalAuthorization({
|
|
107
|
+
identity: resolveIdentity(request),
|
|
108
|
+
providerKind: request.params.provider,
|
|
109
|
+
redirectUri: request.body.redirectUri,
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return codeHostError(error, reply);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
typed.route({
|
|
119
|
+
method: "GET",
|
|
120
|
+
url: "/code-hosts/:provider/repositories",
|
|
121
|
+
schema: {
|
|
122
|
+
params: CodeHostParamsSchema,
|
|
123
|
+
response: {
|
|
124
|
+
200: z.array(CodeHostRepositorySchema),
|
|
125
|
+
401: ErrorSchema,
|
|
126
|
+
404: ErrorSchema,
|
|
127
|
+
503: ErrorSchema,
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
handler: async (request, reply) => {
|
|
131
|
+
const codeHosts = ctx.core?.codeHosts;
|
|
132
|
+
if (!codeHosts?.available) return reply.status(503).send(unavailable);
|
|
133
|
+
try {
|
|
134
|
+
return reply.send(
|
|
135
|
+
await codeHosts.listRepositories({
|
|
136
|
+
identity: resolveIdentity(request),
|
|
137
|
+
provider: request.params.provider,
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return codeHostError(error, reply);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
typed.route({
|
|
147
|
+
method: "POST",
|
|
148
|
+
url: "/code-hosts/:provider/import",
|
|
149
|
+
schema: {
|
|
150
|
+
params: CodeHostParamsSchema,
|
|
151
|
+
body: CodeHostImportSchema,
|
|
152
|
+
response: {
|
|
153
|
+
201: ProjectSchema,
|
|
154
|
+
401: ErrorSchema,
|
|
155
|
+
403: ErrorSchema,
|
|
156
|
+
404: ErrorSchema,
|
|
157
|
+
503: ErrorSchema,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
handler: async (request, reply) => {
|
|
161
|
+
const codeHosts = ctx.core?.codeHosts;
|
|
162
|
+
if (!codeHosts?.available) return reply.status(503).send(unavailable);
|
|
163
|
+
try {
|
|
164
|
+
const project = await codeHosts.importRepository({
|
|
165
|
+
identity: resolveIdentity(request),
|
|
166
|
+
provider: request.params.provider,
|
|
167
|
+
fullName: request.body.fullName,
|
|
168
|
+
...(request.body.name ? { name: request.body.name } : {}),
|
|
169
|
+
});
|
|
170
|
+
return reply.status(201).send({
|
|
171
|
+
id: project.id,
|
|
172
|
+
name: project.name,
|
|
173
|
+
storageType: project.storageType,
|
|
174
|
+
remoteUrl: project.remoteUrl,
|
|
175
|
+
remoteOwnership: project.remoteOwnership,
|
|
176
|
+
remoteDivergedAt: project.remoteDivergedAt,
|
|
177
|
+
defaultBranch: project.defaultBranch,
|
|
178
|
+
createdAt: project.createdAt,
|
|
179
|
+
updatedAt: project.updatedAt,
|
|
180
|
+
});
|
|
181
|
+
} catch (error) {
|
|
182
|
+
return codeHostError(error, reply);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
typed.route({
|
|
188
|
+
method: "POST",
|
|
189
|
+
url: "/projects/:projectId/code-host/publish",
|
|
190
|
+
schema: {
|
|
191
|
+
params: ProjectIdParamsSchema,
|
|
192
|
+
body: CodeHostPublishSchema,
|
|
193
|
+
response: {
|
|
194
|
+
201: CodeHostPublishResultSchema,
|
|
195
|
+
401: ErrorSchema,
|
|
196
|
+
403: ErrorSchema,
|
|
197
|
+
404: ErrorSchema,
|
|
198
|
+
409: ErrorSchema,
|
|
199
|
+
503: ErrorSchema,
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
handler: async (request, reply) => {
|
|
203
|
+
const codeHosts = ctx.core?.codeHosts;
|
|
204
|
+
if (!codeHosts?.available) return reply.status(503).send(unavailable);
|
|
205
|
+
try {
|
|
206
|
+
return reply.status(201).send(
|
|
207
|
+
await codeHosts.publishProject({
|
|
208
|
+
identity: resolveIdentity(request),
|
|
209
|
+
projectId: request.params.projectId,
|
|
210
|
+
...request.body,
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
return codeHostError(error, reply);
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Known code-host failures as HTTP answers; anything else is rethrown. */
|
|
221
|
+
function codeHostError(error: unknown, reply: FastifyReply): FastifyReply {
|
|
222
|
+
if (error instanceof CodeHostNotConnectedError)
|
|
223
|
+
return reply.status(401).send({ error: error.message });
|
|
224
|
+
if (
|
|
225
|
+
error instanceof AccessDeniedError ||
|
|
226
|
+
error instanceof ConnectionPermissionDeniedError
|
|
227
|
+
)
|
|
228
|
+
return reply.status(403).send({ error: error.message });
|
|
229
|
+
if (
|
|
230
|
+
error instanceof CodeHostUnsupportedError ||
|
|
231
|
+
error instanceof ProjectNotFoundError
|
|
232
|
+
)
|
|
233
|
+
return reply.status(404).send({ error: error.message });
|
|
234
|
+
if (
|
|
235
|
+
error instanceof ProjectAlreadyLinkedError ||
|
|
236
|
+
error instanceof ProjectNotDeployedError ||
|
|
237
|
+
error instanceof ConnectionUnavailableError ||
|
|
238
|
+
error instanceof ReplicaClaimBusyError
|
|
239
|
+
)
|
|
240
|
+
return reply.status(409).send({ error: error.message });
|
|
241
|
+
if (error instanceof Error && "status" in error) {
|
|
242
|
+
// A code host's own API refusal (e.g. GitHub 404 for a repository).
|
|
243
|
+
const status = error.status === 404 ? 404 : 401;
|
|
244
|
+
return reply.status(status).send({ error: error.message });
|
|
245
|
+
}
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConnectionActionDeniedError,
|
|
3
|
+
ConnectionActionRefusedError,
|
|
4
|
+
} from "@catamorphic/core";
|
|
5
|
+
import type { Json } from "@catamorphic/db";
|
|
6
|
+
import type { FastifyInstance } from "fastify";
|
|
7
|
+
import type { RouteContext } from "../app.js";
|
|
8
|
+
import {
|
|
9
|
+
McpRequestError,
|
|
10
|
+
serveForwardedTools,
|
|
11
|
+
toolError,
|
|
12
|
+
toolValue,
|
|
13
|
+
} from "../mcp-shared.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One brokered connection as an agent's MCP server (`connection_<alias>`).
|
|
17
|
+
* An MCP connection's tools and results pass through as its server
|
|
18
|
+
* declared and sent them; other connections' actions answer as JSON tool
|
|
19
|
+
* results and declare no output schema (ADR 0225).
|
|
20
|
+
*/
|
|
21
|
+
export function registerConnectionMcpRoutes(
|
|
22
|
+
app: FastifyInstance,
|
|
23
|
+
ctx: RouteContext,
|
|
24
|
+
): void {
|
|
25
|
+
// Stateless Streamable HTTP: no server-initiated stream, no session to end.
|
|
26
|
+
for (const method of ["GET", "DELETE"] as const) {
|
|
27
|
+
app.route({
|
|
28
|
+
method,
|
|
29
|
+
url: "/connection-mcp",
|
|
30
|
+
config: { public: true },
|
|
31
|
+
handler: async (_request, reply) =>
|
|
32
|
+
reply.status(405).header("allow", "POST").send(),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
app.post(
|
|
36
|
+
"/connection-mcp",
|
|
37
|
+
{ config: { public: true } },
|
|
38
|
+
async (request, reply) => {
|
|
39
|
+
const grants = ctx.core?.connectionGrants;
|
|
40
|
+
const broker = ctx.core?.connectionBroker;
|
|
41
|
+
if (!grants || !broker || !ctx.core) {
|
|
42
|
+
return reply
|
|
43
|
+
.status(503)
|
|
44
|
+
.send({ error: "Connection broker not configured" });
|
|
45
|
+
}
|
|
46
|
+
const token = bearer(request.headers.authorization);
|
|
47
|
+
const grant = token ? await grants.validate({ token }) : null;
|
|
48
|
+
if (grant?.channel !== "mcp")
|
|
49
|
+
return reply.status(401).send({ error: "Invalid connection grant" });
|
|
50
|
+
const identity = {
|
|
51
|
+
tenantId: grant.tenantId,
|
|
52
|
+
externalUserId: `connection-grant:${grant.agentSessionId ?? grant.allocationId}`,
|
|
53
|
+
};
|
|
54
|
+
const allocation = await ctx.core.executionAllocations.get({
|
|
55
|
+
identity,
|
|
56
|
+
allocationId: grant.allocationId,
|
|
57
|
+
});
|
|
58
|
+
const binding = allocation?.policy.connections?.find(
|
|
59
|
+
(candidate) => candidate.alias === grant.alias,
|
|
60
|
+
);
|
|
61
|
+
if (allocation?.status !== "active" || !binding) {
|
|
62
|
+
return reply
|
|
63
|
+
.status(401)
|
|
64
|
+
.send({ error: "Connection grant is no longer active" });
|
|
65
|
+
}
|
|
66
|
+
const mcp = broker.servesMcp(binding.providerKind);
|
|
67
|
+
return serveForwardedTools({
|
|
68
|
+
reply,
|
|
69
|
+
body: request.body,
|
|
70
|
+
serverName: "catamorphic-connection-broker",
|
|
71
|
+
listTools: async () => {
|
|
72
|
+
try {
|
|
73
|
+
const actions = await broker.listActions({
|
|
74
|
+
identity,
|
|
75
|
+
allocationId: allocation.id,
|
|
76
|
+
alias: binding.alias,
|
|
77
|
+
...(grant.agentSessionId
|
|
78
|
+
? { agentSessionId: grant.agentSessionId }
|
|
79
|
+
: {}),
|
|
80
|
+
});
|
|
81
|
+
// Only an MCP server's own results match its output schema.
|
|
82
|
+
return mcp
|
|
83
|
+
? actions
|
|
84
|
+
: actions.map((action) => ({
|
|
85
|
+
...action,
|
|
86
|
+
outputSchema: undefined,
|
|
87
|
+
}));
|
|
88
|
+
} catch {
|
|
89
|
+
throw new McpRequestError(-32_000, "Connection action failed");
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
callTool: async (call) => {
|
|
93
|
+
if (!binding.capabilities.includes(call.name)) {
|
|
94
|
+
throw new McpRequestError(
|
|
95
|
+
-32602,
|
|
96
|
+
"Tool is outside the connection grant",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const output = await broker.invoke({
|
|
101
|
+
identity,
|
|
102
|
+
allocationId: allocation.id,
|
|
103
|
+
alias: binding.alias,
|
|
104
|
+
action: call.name,
|
|
105
|
+
input: toJson(call.arguments),
|
|
106
|
+
caller: "agent",
|
|
107
|
+
...(grant.agentSessionId
|
|
108
|
+
? { agentSessionId: grant.agentSessionId }
|
|
109
|
+
: {}),
|
|
110
|
+
});
|
|
111
|
+
return mcp ? output : toolValue(output);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
// A policy refusal is information the agent can act on (narrow
|
|
114
|
+
// the query, ask differently); other failures stay opaque.
|
|
115
|
+
if (
|
|
116
|
+
error instanceof ConnectionActionDeniedError ||
|
|
117
|
+
error instanceof ConnectionActionRefusedError
|
|
118
|
+
) {
|
|
119
|
+
return toolError(error.message);
|
|
120
|
+
}
|
|
121
|
+
throw new McpRequestError(-32_000, "Connection action failed");
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function bearer(value: string | undefined): string | undefined {
|
|
130
|
+
return value?.match(/^Bearer\s+(.+)$/i)?.[1];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function toJson(value: unknown): Json {
|
|
134
|
+
return JSON.parse(JSON.stringify(value ?? null)) as Json;
|
|
135
|
+
}
|