@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,394 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CatamorphicCore,
|
|
3
|
+
Identity,
|
|
4
|
+
McpToolMetadata,
|
|
5
|
+
TriggerBindingInfo,
|
|
6
|
+
TriggerFireOutcome,
|
|
7
|
+
} from "@catamorphic/core";
|
|
8
|
+
import { agentCapabilityTools } from "@catamorphic/sandbox";
|
|
9
|
+
import type { FastifyInstance } from "fastify";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import type { RouteContext } from "../app.js";
|
|
12
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
13
|
+
import {
|
|
14
|
+
callPollRunTool,
|
|
15
|
+
handleMcpPost,
|
|
16
|
+
McpRequestError,
|
|
17
|
+
negotiateProtocolVersion,
|
|
18
|
+
POLL_RUN_TOOL,
|
|
19
|
+
POLL_RUN_TOOL_DEFINITION,
|
|
20
|
+
toolError,
|
|
21
|
+
toolValue,
|
|
22
|
+
} from "../mcp-shared.js";
|
|
23
|
+
import { programTools, projectInstructions } from "../project-mcp-program.js";
|
|
24
|
+
import {
|
|
25
|
+
allowedWorkflowNames,
|
|
26
|
+
mayUseProject,
|
|
27
|
+
type SurfaceTool,
|
|
28
|
+
surfaceTools,
|
|
29
|
+
} from "../project-mcp-surface.js";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* MCP server for a project (ADR 0042, ADR 0055): the single "bring your own
|
|
33
|
+
* agent" door. Serves the project's AI-callable workflows — one tool per
|
|
34
|
+
* trigger binding of every kind the host registered under `mcpToolKinds` —
|
|
35
|
+
* beside the documents surface, skills and `ask_agent`
|
|
36
|
+
* (`project-mcp-surface.ts`), everything narrowed to the caller's scope by
|
|
37
|
+
* the core services themselves. Workflow tools:
|
|
38
|
+
* the tool's input schema is the binding's frozen per-workflow schema (a
|
|
39
|
+
* hole instantiated by the workflow's own input type), its metadata comes
|
|
40
|
+
* from the binding's constant config, and calling it fires the trigger
|
|
41
|
+
* sync-until-first-wait — a settled run returns its output inline, a run
|
|
42
|
+
* that needs to wait detaches and hands back `{runId}` for the shared
|
|
43
|
+
* `catamorphic_poll_run` tool.
|
|
44
|
+
*
|
|
45
|
+
* Unlike `/apps-mcp` (which serves published apps and their UI resources),
|
|
46
|
+
* this endpoint needs no app: binding a tool kind in workflow code is the
|
|
47
|
+
* whole opt-in. Stateless Streamable-HTTP JSON-RPC, same era and identity
|
|
48
|
+
* model as the rest of the plugin.
|
|
49
|
+
*
|
|
50
|
+
* The workflow's output schema deliberately rides `_meta`, NOT the tool's
|
|
51
|
+
* `outputSchema`: MCP clients validate `structuredContent` against an
|
|
52
|
+
* advertised `outputSchema`, and the detach answer (`{status, runId, …}`)
|
|
53
|
+
* must remain a valid result for every tool.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wall-clock budget a tool call may hold the request before detaching.
|
|
58
|
+
* Deliberately half the MCP SDK's default 60s request timeout, so a
|
|
59
|
+
* budget-detach answer (with its pollable runId) always beats the
|
|
60
|
+
* client-side abort that would lose it.
|
|
61
|
+
*/
|
|
62
|
+
const TOOL_SYNC_BUDGET_MS = 30_000;
|
|
63
|
+
|
|
64
|
+
interface ProjectTool {
|
|
65
|
+
name: string;
|
|
66
|
+
kind: string;
|
|
67
|
+
metadata: McpToolMetadata;
|
|
68
|
+
binding: TriggerBindingInfo;
|
|
69
|
+
/**
|
|
70
|
+
* Non-object workflow inputs cannot be MCP tool arguments directly; the
|
|
71
|
+
* tool then takes `{"input": ...}` and unwraps it before firing.
|
|
72
|
+
*/
|
|
73
|
+
wrapped: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function registerProjectMcpRoutes(
|
|
77
|
+
app: FastifyInstance,
|
|
78
|
+
ctx: RouteContext,
|
|
79
|
+
) {
|
|
80
|
+
// Stateless Streamable HTTP: no server-initiated stream, no session to end.
|
|
81
|
+
const notAllowed = async (
|
|
82
|
+
_request: unknown,
|
|
83
|
+
reply: import("fastify").FastifyReply,
|
|
84
|
+
) => reply.status(405).header("allow", "POST").send();
|
|
85
|
+
app.get("/projects/:projectId/mcp", notAllowed);
|
|
86
|
+
app.delete("/projects/:projectId/mcp", notAllowed);
|
|
87
|
+
|
|
88
|
+
app.post("/projects/:projectId/mcp", async (request, reply) => {
|
|
89
|
+
const core = ctx.core;
|
|
90
|
+
if (!core) {
|
|
91
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
92
|
+
}
|
|
93
|
+
if (!ctx.features.mcp) {
|
|
94
|
+
return reply
|
|
95
|
+
.status(404)
|
|
96
|
+
.send({ error: "MCP is turned off on this server" });
|
|
97
|
+
}
|
|
98
|
+
const { projectId } = request.params as { projectId: string };
|
|
99
|
+
const { sessionId, allocationId } = request.query as {
|
|
100
|
+
sessionId?: string;
|
|
101
|
+
allocationId?: string;
|
|
102
|
+
};
|
|
103
|
+
const identity = resolveIdentity(request);
|
|
104
|
+
// Someone the project grants nothing sees no project here at all.
|
|
105
|
+
if (!mayUseProject(identity, projectId)) {
|
|
106
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
107
|
+
}
|
|
108
|
+
const abort = new AbortController();
|
|
109
|
+
reply.raw.once("close", () => abort.abort());
|
|
110
|
+
const gatewayTools = sessionId
|
|
111
|
+
? agentCapabilityTools(
|
|
112
|
+
core.agentCapabilities.forSession({
|
|
113
|
+
identity,
|
|
114
|
+
projectId,
|
|
115
|
+
sessionId,
|
|
116
|
+
allocationId,
|
|
117
|
+
}),
|
|
118
|
+
abort.signal,
|
|
119
|
+
)
|
|
120
|
+
: [];
|
|
121
|
+
const surface: SurfaceTool[] = [
|
|
122
|
+
...surfaceTools(core, identity, projectId, ctx.features, sessionId),
|
|
123
|
+
...programTools(core, identity, projectId),
|
|
124
|
+
...(ctx.projectMcp?.tools?.({ identity, projectId }) ?? []).map(
|
|
125
|
+
(tool) => ({
|
|
126
|
+
definition: tool.definition,
|
|
127
|
+
call: async (args: Record<string, unknown>) => {
|
|
128
|
+
try {
|
|
129
|
+
return toolValue(await tool.call(args));
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return toolError(
|
|
132
|
+
error instanceof Error ? error.message : String(error),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
),
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
return handleMcpPost(reply, request.body, async (method, params) => {
|
|
141
|
+
switch (method) {
|
|
142
|
+
case "initialize":
|
|
143
|
+
return {
|
|
144
|
+
protocolVersion: negotiateProtocolVersion(params.protocolVersion),
|
|
145
|
+
capabilities: { tools: { listChanged: false } },
|
|
146
|
+
serverInfo: {
|
|
147
|
+
version: "1.0.0",
|
|
148
|
+
...(ctx.projectMcp?.serverInfo ?? {
|
|
149
|
+
name: "catamorphic",
|
|
150
|
+
title: "Catamorphic project",
|
|
151
|
+
}),
|
|
152
|
+
},
|
|
153
|
+
instructions: await projectInstructions({
|
|
154
|
+
core,
|
|
155
|
+
identity,
|
|
156
|
+
projectId,
|
|
157
|
+
toolNames: new Set(
|
|
158
|
+
surface.map((tool) => String(tool.definition.name)),
|
|
159
|
+
),
|
|
160
|
+
...(ctx.projectMcp?.instructions
|
|
161
|
+
? { host: ctx.projectMcp.instructions }
|
|
162
|
+
: {}),
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
case "ping":
|
|
166
|
+
return {};
|
|
167
|
+
case "tools/list": {
|
|
168
|
+
const tools = await loadProjectTools(core, identity, projectId);
|
|
169
|
+
// A workflow tool that claims a surface name wins the name (the
|
|
170
|
+
// project chose it); the surface tool steps aside for this project.
|
|
171
|
+
const taken = new Set(tools.map((tool) => tool.name));
|
|
172
|
+
return {
|
|
173
|
+
tools: [
|
|
174
|
+
...gatewayTools.map((tool) => ({
|
|
175
|
+
name: tool.name,
|
|
176
|
+
description: tool.description,
|
|
177
|
+
inputSchema: z.toJSONSchema(z.object(tool.parameters)),
|
|
178
|
+
})),
|
|
179
|
+
...tools
|
|
180
|
+
.filter(
|
|
181
|
+
(tool) =>
|
|
182
|
+
!gatewayTools.some((gateway) => gateway.name === tool.name),
|
|
183
|
+
)
|
|
184
|
+
.map(toolDefinition),
|
|
185
|
+
...(tools.length > 0 || core.mcpToolKinds.length > 0
|
|
186
|
+
? [POLL_RUN_TOOL_DEFINITION]
|
|
187
|
+
: []),
|
|
188
|
+
...surface
|
|
189
|
+
.filter((tool) => !taken.has(String(tool.definition.name)))
|
|
190
|
+
.map((tool) => tool.definition),
|
|
191
|
+
],
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
case "resources/list":
|
|
195
|
+
return { resources: [] };
|
|
196
|
+
case "resources/templates/list":
|
|
197
|
+
return { resourceTemplates: [] };
|
|
198
|
+
case "prompts/list":
|
|
199
|
+
return { prompts: [] };
|
|
200
|
+
case "tools/call": {
|
|
201
|
+
const name = typeof params.name === "string" ? params.name : "";
|
|
202
|
+
const args =
|
|
203
|
+
typeof params.arguments === "object" && params.arguments !== null
|
|
204
|
+
? (params.arguments as Record<string, unknown>)
|
|
205
|
+
: {};
|
|
206
|
+
const gatewayTool = gatewayTools.find((tool) => tool.name === name);
|
|
207
|
+
if (gatewayTool) {
|
|
208
|
+
try {
|
|
209
|
+
return toolValue(
|
|
210
|
+
await gatewayTool.execute(args, {
|
|
211
|
+
projectId,
|
|
212
|
+
sessionId,
|
|
213
|
+
// An MCP call hears nothing of its turn: it waits while
|
|
214
|
+
// asked (ADR 0264).
|
|
215
|
+
stopWaiting: abort.signal,
|
|
216
|
+
}),
|
|
217
|
+
);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
return toolError(
|
|
220
|
+
error instanceof Error ? error.message : "Capability failed",
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const workflowTools = await loadProjectTools(
|
|
225
|
+
core,
|
|
226
|
+
identity,
|
|
227
|
+
projectId,
|
|
228
|
+
);
|
|
229
|
+
const surfaced = workflowTools.some((tool) => tool.name === name)
|
|
230
|
+
? undefined
|
|
231
|
+
: surface.find((tool) => tool.definition.name === name);
|
|
232
|
+
if (surfaced) return surfaced.call(args);
|
|
233
|
+
return callTool(core, identity, projectId, name, args);
|
|
234
|
+
}
|
|
235
|
+
default:
|
|
236
|
+
throw new McpRequestError(-32601, `Method not found: ${method}`);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The project's tool roster at its production commit: every binding of
|
|
244
|
+
* every registered tool kind, from one scan. Name uniqueness is enforced
|
|
245
|
+
* at deploy scan; the throw here is a backstop for pre-validation commits.
|
|
246
|
+
*/
|
|
247
|
+
export async function loadProjectTools(
|
|
248
|
+
core: CatamorphicCore,
|
|
249
|
+
identity: Identity,
|
|
250
|
+
projectId: string,
|
|
251
|
+
): Promise<ProjectTool[]> {
|
|
252
|
+
const specs = new Map(core.mcpToolKinds.map((spec) => [spec.kind, spec]));
|
|
253
|
+
if (specs.size === 0) return [];
|
|
254
|
+
const bindings = await core.triggers.list({ identity, projectId });
|
|
255
|
+
// A scoped caller sees only the workflows its scope resolves to (calls are
|
|
256
|
+
// re-checked at trigger time; this keeps the roster from enumerating).
|
|
257
|
+
const allowed = await allowedWorkflowNames(core, identity, projectId);
|
|
258
|
+
const tools = new Map<string, ProjectTool>();
|
|
259
|
+
for (const binding of bindings) {
|
|
260
|
+
const spec = specs.get(binding.kind);
|
|
261
|
+
if (!spec) continue;
|
|
262
|
+
if (allowed && !allowed.has(binding.workflowName)) continue;
|
|
263
|
+
const metadata = spec.tool(binding.config);
|
|
264
|
+
const name = metadata.name ?? binding.workflowName;
|
|
265
|
+
const existing = tools.get(name);
|
|
266
|
+
if (existing || name === POLL_RUN_TOOL) {
|
|
267
|
+
throw new Error(
|
|
268
|
+
`MCP tool name '${name}' is ${existing ? `claimed by both workflow '${existing.binding.workflowName}' and '${binding.workflowName}'` : "reserved"}; redeploy to surface the scan error`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
tools.set(name, {
|
|
272
|
+
name,
|
|
273
|
+
kind: binding.kind,
|
|
274
|
+
metadata,
|
|
275
|
+
binding,
|
|
276
|
+
wrapped: !isObjectSchema(binding.inputSchema),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return [...tools.values()];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function toolDefinition(tool: ProjectTool): Record<string, unknown> {
|
|
283
|
+
const notes: string[] = [];
|
|
284
|
+
if (tool.wrapped) notes.push('Pass the workflow input as {"input": ...}.');
|
|
285
|
+
if (tool.binding.canSuspend) {
|
|
286
|
+
notes.push(
|
|
287
|
+
`May detach if the workflow needs to wait; you then get {"runId"} — poll it with ${POLL_RUN_TOOL}.`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
name: tool.name,
|
|
292
|
+
description: [tool.metadata.description, ...notes].join(" "),
|
|
293
|
+
inputSchema: tool.wrapped
|
|
294
|
+
? {
|
|
295
|
+
type: "object",
|
|
296
|
+
properties: {
|
|
297
|
+
input: {
|
|
298
|
+
description: "The workflow's input (JSON)",
|
|
299
|
+
...(asObject(tool.binding.inputSchema) ?? {}),
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
required: ["input"],
|
|
303
|
+
}
|
|
304
|
+
: tool.binding.inputSchema,
|
|
305
|
+
...(tool.metadata.annotations
|
|
306
|
+
? { annotations: tool.metadata.annotations }
|
|
307
|
+
: {}),
|
|
308
|
+
_meta: {
|
|
309
|
+
catamorphic: {
|
|
310
|
+
canSuspend: tool.binding.canSuspend,
|
|
311
|
+
// Introspection only — see the module doc for why this is not the
|
|
312
|
+
// tool's `outputSchema`.
|
|
313
|
+
outputSchema: tool.binding.outputSchema,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export async function callTool(
|
|
320
|
+
core: CatamorphicCore,
|
|
321
|
+
identity: Identity,
|
|
322
|
+
projectId: string,
|
|
323
|
+
name: string,
|
|
324
|
+
args: Record<string, unknown>,
|
|
325
|
+
): Promise<Record<string, unknown>> {
|
|
326
|
+
if (name === POLL_RUN_TOOL) {
|
|
327
|
+
return callPollRunTool(core, identity, args);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const tools = await loadProjectTools(core, identity, projectId);
|
|
331
|
+
const tool = tools.find((candidate) => candidate.name === name);
|
|
332
|
+
if (!tool) return toolError(`Unknown tool: ${name}`);
|
|
333
|
+
|
|
334
|
+
const payload = tool.wrapped ? (args.input ?? null) : args;
|
|
335
|
+
let outcome: TriggerFireOutcome;
|
|
336
|
+
try {
|
|
337
|
+
const result = await core.triggers.fire({
|
|
338
|
+
identity,
|
|
339
|
+
projectId,
|
|
340
|
+
kind: tool.kind,
|
|
341
|
+
payload: payload as never,
|
|
342
|
+
// Always sync: a tool call wants its answer inline. A kind that
|
|
343
|
+
// disallows sync fails loudly here rather than silently degrading.
|
|
344
|
+
mode: "sync",
|
|
345
|
+
interactive: true,
|
|
346
|
+
workflows: [tool.binding.workflowName],
|
|
347
|
+
budgetMs: TOOL_SYNC_BUDGET_MS,
|
|
348
|
+
});
|
|
349
|
+
const first = result.runs[0];
|
|
350
|
+
if (!first) {
|
|
351
|
+
return toolError(
|
|
352
|
+
`Workflow '${tool.binding.workflowName}' is not bound at the current production commit`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
outcome = first;
|
|
356
|
+
} catch (error) {
|
|
357
|
+
// Run-input validation failures, binding-scan failures, enrollment
|
|
358
|
+
// conflicts — all model-actionable, so they ride the tool-error
|
|
359
|
+
// channel instead of failing the JSON-RPC request.
|
|
360
|
+
return toolError(
|
|
361
|
+
error instanceof Error ? error.message : "Workflow trigger failed",
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
switch (outcome.status) {
|
|
366
|
+
case "completed":
|
|
367
|
+
return toolValue(outcome.output);
|
|
368
|
+
case "failed":
|
|
369
|
+
return toolError(outcome.error);
|
|
370
|
+
case "suspended":
|
|
371
|
+
return toolValue({
|
|
372
|
+
status: "running",
|
|
373
|
+
runId: outcome.runId,
|
|
374
|
+
suspendedOn: outcome.suspendedOn,
|
|
375
|
+
note: `Still running; poll with ${POLL_RUN_TOOL}.`,
|
|
376
|
+
});
|
|
377
|
+
case "started":
|
|
378
|
+
return toolValue({
|
|
379
|
+
status: "running",
|
|
380
|
+
runId: outcome.runId,
|
|
381
|
+
note: `Started asynchronously; poll with ${POLL_RUN_TOOL}.`,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
387
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
388
|
+
? (value as Record<string, unknown>)
|
|
389
|
+
: undefined;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function isObjectSchema(schema: unknown): boolean {
|
|
393
|
+
return asObject(schema)?.type === "object";
|
|
394
|
+
}
|