@catamorphic/fastify-plugin 0.1.0-alpha.39 → 0.1.0-alpha.41
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/NOTICE.txt +7 -0
- package/dist/app.d.ts +2 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/chunk-jytcc42y.js +592 -0
- package/dist/chunk-jytcc42y.js.map +11 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1673 -411
- package/dist/index.js.map +29 -25
- package/dist/mcp-scopes.d.ts +12 -0
- package/dist/mcp-scopes.d.ts.map +1 -0
- package/dist/mcp-server.d.ts +158 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-shared.d.ts +3 -50
- package/dist/mcp-shared.d.ts.map +1 -1
- package/dist/mcp.d.ts +4 -2
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +6 -4
- package/dist/mcp.js.map +1 -1
- package/dist/plugin.d.ts +26 -2
- package/dist/plugin.d.ts.map +1 -1
- package/dist/project-capabilities.d.ts.map +1 -1
- package/dist/project-mcp-surface.d.ts +3 -4
- package/dist/project-mcp-surface.d.ts.map +1 -1
- package/dist/routes/agent.d.ts.map +1 -1
- package/dist/routes/apps-mcp.d.ts.map +1 -1
- package/dist/routes/client-runners.d.ts.map +1 -1
- package/dist/routes/connection-mcp.d.ts.map +1 -1
- package/dist/routes/documents.d.ts.map +1 -1
- package/dist/routes/environments.d.ts.map +1 -1
- package/dist/routes/project-mcp.d.ts.map +1 -1
- package/dist/routes/projects.d.ts.map +1 -1
- package/dist/routes/publications.d.ts.map +1 -1
- package/dist/routes/run-errors.d.ts.map +1 -1
- package/dist/routes/runs.d.ts.map +1 -1
- package/dist/routes/server-mcp.d.ts +17 -0
- package/dist/routes/server-mcp.d.ts.map +1 -0
- package/dist/routes/session-mailboxes.d.ts.map +1 -1
- package/dist/routes/triggers.d.ts.map +1 -1
- package/dist/routes/watchers.d.ts.map +1 -1
- package/dist/routes/workflow-enablements.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/schemas.d.ts +3684 -28
- package/dist/schemas.d.ts.map +1 -1
- package/dist/session-mcp.d.ts +54 -0
- package/dist/session-mcp.d.ts.map +1 -0
- package/dist/session-tools.d.ts +49 -0
- package/dist/session-tools.d.ts.map +1 -0
- package/package.json +12 -12
- package/src/app.ts +3 -0
- package/src/index.ts +1 -0
- package/src/mcp-scopes.ts +69 -0
- package/src/mcp-server.ts +827 -0
- package/src/mcp-shared.ts +3 -151
- package/src/mcp.ts +6 -5
- package/src/plugin.ts +73 -2
- package/src/project-capabilities.ts +5 -3
- package/src/project-mcp-program.ts +2 -2
- package/src/project-mcp-surface.ts +18 -239
- package/src/routes/agent.ts +384 -41
- package/src/routes/apps-mcp.ts +84 -82
- package/src/routes/client-runners.ts +3 -1
- package/src/routes/connection-mcp.ts +6 -13
- package/src/routes/documents.ts +4 -0
- package/src/routes/environments.ts +49 -1
- package/src/routes/project-mcp.ts +176 -154
- package/src/routes/projects.ts +6 -0
- package/src/routes/publications.ts +2 -0
- package/src/routes/run-errors.ts +5 -1
- package/src/routes/runs.ts +4 -0
- package/src/routes/server-mcp.ts +151 -0
- package/src/routes/session-mailboxes.ts +1 -0
- package/src/routes/triggers.ts +6 -1
- package/src/routes/watchers.ts +1 -0
- package/src/routes/workflow-enablements.ts +132 -1
- package/src/routes/workflows.ts +4 -0
- package/src/schemas.ts +347 -5
- package/src/session-mcp.ts +696 -0
- package/src/session-tools.ts +169 -0
- package/dist/chunk-baagvvm3.js +0 -164
- package/dist/chunk-baagvvm3.js.map +0 -10
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { SessionMessageAuthor } from "@catamorphic/agent-protocol";
|
|
2
|
+
import {
|
|
3
|
+
type CatamorphicCore,
|
|
4
|
+
type Identity,
|
|
5
|
+
SESSION_OPERATION_NAMES,
|
|
6
|
+
SESSION_OPERATIONS,
|
|
7
|
+
type SessionOperationName,
|
|
8
|
+
sessionOperationError,
|
|
9
|
+
} from "@catamorphic/core";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import type { McpTool, McpToolResult } from "./mcp-server.js";
|
|
12
|
+
|
|
13
|
+
export type { McpTool, McpToolResult } from "./mcp-server.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The session operations as MCP tools (ADR 0346): every entry of core's
|
|
17
|
+
* registry becomes one tool, with the registry's name, title, description,
|
|
18
|
+
* schemas and annotations. Nothing here knows an operation by name, so a
|
|
19
|
+
* tool added to the registry is served by both MCP endpoints and offered
|
|
20
|
+
* to in-product agents at once.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** The URI a chat is read and subscribed to at. */
|
|
24
|
+
export function sessionResourceUri(sessionId: string): string {
|
|
25
|
+
return `work://sessions/${sessionId}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The resource listing the chats that need the caller. */
|
|
29
|
+
export const ATTENTION_RESOURCE_URI = "work://attention";
|
|
30
|
+
|
|
31
|
+
const Linked = z.looseObject({
|
|
32
|
+
sessionId: z.string(),
|
|
33
|
+
title: z.string().nullable().optional(),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A session operation's value as a tool result: the JSON as text for the
|
|
38
|
+
* model, the same value as `structuredContent` for clients that read the
|
|
39
|
+
* declared `outputSchema`, and a link to the chat's resource.
|
|
40
|
+
*/
|
|
41
|
+
export function sessionToolResult(value: unknown): McpToolResult {
|
|
42
|
+
const linked = Linked.safeParse(value);
|
|
43
|
+
return {
|
|
44
|
+
content: [
|
|
45
|
+
{ type: "text", text: JSON.stringify(value) ?? "null" },
|
|
46
|
+
...(linked.success
|
|
47
|
+
? [
|
|
48
|
+
{
|
|
49
|
+
type: "resource_link",
|
|
50
|
+
uri: sessionResourceUri(linked.data.sessionId),
|
|
51
|
+
name: linked.data.title || "Chat",
|
|
52
|
+
mimeType: "application/json",
|
|
53
|
+
},
|
|
54
|
+
]
|
|
55
|
+
: []),
|
|
56
|
+
],
|
|
57
|
+
structuredContent: value,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A failed operation as a tool-execution error (ADR 0346): `isError` with
|
|
63
|
+
* `{"error":{"code","message","retryable","sessionId?"}}` as its text.
|
|
64
|
+
*/
|
|
65
|
+
export function sessionToolError(error: unknown): McpToolResult {
|
|
66
|
+
return {
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: "text",
|
|
70
|
+
text: JSON.stringify(sessionOperationError(error).toJSON()),
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
isError: true,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Who a session tool acts as: the calling chat's agent, or the member's assistant. */
|
|
78
|
+
export async function sessionToolAuthor(args: {
|
|
79
|
+
core: CatamorphicCore;
|
|
80
|
+
identity: Identity;
|
|
81
|
+
projectId: string | undefined;
|
|
82
|
+
currentSessionId: string | undefined;
|
|
83
|
+
}): Promise<{ author: SessionMessageAuthor; causation: string[] }> {
|
|
84
|
+
const sessions = args.core.agentSessions;
|
|
85
|
+
if (!sessions || !args.currentSessionId || !args.projectId)
|
|
86
|
+
return {
|
|
87
|
+
// Never the person's own words (ADR 0217): their assistant's.
|
|
88
|
+
author: {
|
|
89
|
+
kind: "assistant",
|
|
90
|
+
externalUserId: args.identity.externalUserId,
|
|
91
|
+
},
|
|
92
|
+
causation: [],
|
|
93
|
+
};
|
|
94
|
+
const source = await sessions.get(
|
|
95
|
+
args.identity,
|
|
96
|
+
args.projectId,
|
|
97
|
+
args.currentSessionId,
|
|
98
|
+
);
|
|
99
|
+
return {
|
|
100
|
+
author: { kind: "agent", sessionId: source.id, agentId: source.agentId },
|
|
101
|
+
causation: await sessions.causalContext({
|
|
102
|
+
identity: args.identity,
|
|
103
|
+
projectId: args.projectId,
|
|
104
|
+
sessionId: source.id,
|
|
105
|
+
}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The session tools for one caller. `projectId` binds them to a project
|
|
111
|
+
* (its own endpoint); without it they take `project` or follow the chat
|
|
112
|
+
* (the server-wide endpoint). `currentSessionId` is the chat an in-product
|
|
113
|
+
* connection serves: its agent is the author.
|
|
114
|
+
*/
|
|
115
|
+
export function sessionTools(args: {
|
|
116
|
+
core: CatamorphicCore;
|
|
117
|
+
identity: Identity;
|
|
118
|
+
projectId?: string;
|
|
119
|
+
currentSessionId?: string;
|
|
120
|
+
}): McpTool[] {
|
|
121
|
+
const operations = args.core.sessionOperations;
|
|
122
|
+
if (!operations) return [];
|
|
123
|
+
return SESSION_OPERATION_NAMES.map((name: SessionOperationName): McpTool => {
|
|
124
|
+
const definition = SESSION_OPERATIONS[name];
|
|
125
|
+
return {
|
|
126
|
+
definition: {
|
|
127
|
+
name,
|
|
128
|
+
title: definition.title,
|
|
129
|
+
description: definition.description,
|
|
130
|
+
inputSchema: z.toJSONSchema(definition.input, { io: "input" }),
|
|
131
|
+
// Compact on purpose: clients hand it to their model (ADR 0347).
|
|
132
|
+
outputSchema: definition.outputSchema,
|
|
133
|
+
annotations: {
|
|
134
|
+
title: definition.title,
|
|
135
|
+
...definition.annotations,
|
|
136
|
+
openWorldHint: false,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
scope: definition.scope,
|
|
140
|
+
call: async (input, context) => {
|
|
141
|
+
try {
|
|
142
|
+
const origin = await sessionToolAuthor({
|
|
143
|
+
core: args.core,
|
|
144
|
+
identity: args.identity,
|
|
145
|
+
projectId: args.projectId,
|
|
146
|
+
currentSessionId: args.currentSessionId,
|
|
147
|
+
});
|
|
148
|
+
const value = await operations.execute({
|
|
149
|
+
identity: args.identity,
|
|
150
|
+
...(args.projectId ? { projectId: args.projectId } : {}),
|
|
151
|
+
operation: name,
|
|
152
|
+
args: input,
|
|
153
|
+
...origin,
|
|
154
|
+
// Only a read stops with its caller: a mutation the caller
|
|
155
|
+
// stopped waiting for still completes, and its retry finds it.
|
|
156
|
+
...(context?.signal && definition.annotations.readOnlyHint
|
|
157
|
+
? { signal: context.signal }
|
|
158
|
+
: {}),
|
|
159
|
+
});
|
|
160
|
+
// What clients validate against the declared output schema.
|
|
161
|
+
const shaped = definition.output.safeParse(value);
|
|
162
|
+
return sessionToolResult(shaped.success ? shaped.data : value);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
return sessionToolError(error);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
|
+
}
|
package/dist/chunk-baagvvm3.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
// src/mcp-shared.ts
|
|
2
|
-
import { MCP_POLL_RUN_TOOL, RunNotFoundError } from "@catamorphic/core";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
var SUPPORTED_PROTOCOL_VERSIONS = new Set([
|
|
5
|
-
"2024-11-05",
|
|
6
|
-
"2025-03-26",
|
|
7
|
-
"2025-06-18",
|
|
8
|
-
"2025-11-25"
|
|
9
|
-
]);
|
|
10
|
-
var DEFAULT_PROTOCOL_VERSION = "2025-06-18";
|
|
11
|
-
function negotiateProtocolVersion(requested) {
|
|
12
|
-
return typeof requested === "string" && SUPPORTED_PROTOCOL_VERSIONS.has(requested) ? requested : DEFAULT_PROTOCOL_VERSION;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class McpRequestError2 extends Error {
|
|
16
|
-
code;
|
|
17
|
-
constructor(code, message) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.code = code;
|
|
20
|
-
this.name = "McpRequestError";
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
async function handleMcpPost(reply, body, dispatch) {
|
|
24
|
-
const message = body;
|
|
25
|
-
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
26
|
-
return rpcError(reply, null, -32600, "Expected a single JSON-RPC message");
|
|
27
|
-
}
|
|
28
|
-
if (message.id === undefined || message.id === null) {
|
|
29
|
-
return reply.status(202).send();
|
|
30
|
-
}
|
|
31
|
-
if (typeof message.method !== "string") {
|
|
32
|
-
return rpcError(reply, message.id, -32600, "Missing method");
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
const result = await dispatch(message.method, message.params ?? {});
|
|
36
|
-
return rpcResult(reply, message.id, result);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
if (error instanceof McpRequestError2) {
|
|
39
|
-
return rpcError(reply, message.id, error.code, error.message);
|
|
40
|
-
}
|
|
41
|
-
return rpcError(reply, message.id, -32603, error instanceof Error ? error.message : "Internal error");
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function rpcResult(reply, id, result) {
|
|
45
|
-
return reply.header("content-type", "application/json").send({ jsonrpc: "2.0", id, result });
|
|
46
|
-
}
|
|
47
|
-
function rpcError(reply, id, code, message) {
|
|
48
|
-
return reply.header("content-type", "application/json").send({ jsonrpc: "2.0", id, error: { code, message } });
|
|
49
|
-
}
|
|
50
|
-
function toolValue(value) {
|
|
51
|
-
return {
|
|
52
|
-
content: [{ type: "text", text: JSON.stringify(value) ?? "null" }],
|
|
53
|
-
...typeof value === "object" && value !== null && !Array.isArray(value) ? { structuredContent: value } : {}
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function toolError2(message) {
|
|
57
|
-
return { content: [{ type: "text", text: message }], isError: true };
|
|
58
|
-
}
|
|
59
|
-
function forwardedTool(tool) {
|
|
60
|
-
return {
|
|
61
|
-
name: tool.name,
|
|
62
|
-
...tool.title ? { title: tool.title } : {},
|
|
63
|
-
...tool.description ? { description: tool.description } : {},
|
|
64
|
-
inputSchema: tool.inputSchema,
|
|
65
|
-
...isObjectSchema(tool.outputSchema) ? { outputSchema: tool.outputSchema } : {},
|
|
66
|
-
...tool.annotations ? { annotations: tool.annotations } : {}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
var ForwardedToolResultSchema = z.looseObject({
|
|
70
|
-
content: z.array(z.unknown()),
|
|
71
|
-
structuredContent: z.unknown().optional()
|
|
72
|
-
});
|
|
73
|
-
function forwardedToolResult(result) {
|
|
74
|
-
const parsed = ForwardedToolResultSchema.safeParse(result);
|
|
75
|
-
if (!parsed.success)
|
|
76
|
-
return toolValue(result);
|
|
77
|
-
const { structuredContent, ...rest } = parsed.data;
|
|
78
|
-
if (structuredContent === undefined)
|
|
79
|
-
return rest;
|
|
80
|
-
if (isObject(structuredContent))
|
|
81
|
-
return { ...rest, structuredContent };
|
|
82
|
-
return rest.content.length > 0 ? rest : {
|
|
83
|
-
...rest,
|
|
84
|
-
content: [{ type: "text", text: JSON.stringify(structuredContent) }]
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function isObject(value) {
|
|
88
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
89
|
-
}
|
|
90
|
-
function isObjectSchema(schema) {
|
|
91
|
-
return isObject(schema) && schema.type === "object";
|
|
92
|
-
}
|
|
93
|
-
var POLL_RUN_TOOL = MCP_POLL_RUN_TOOL;
|
|
94
|
-
var POLL_RUN_TOOL_DEFINITION = {
|
|
95
|
-
name: POLL_RUN_TOOL,
|
|
96
|
-
annotations: { readOnlyHint: true },
|
|
97
|
-
description: "Poll a workflow run by its runId — one started asynchronously or one " + "that detached mid-call. Returns the run's status, output (once " + "completed), and batch progress.",
|
|
98
|
-
inputSchema: {
|
|
99
|
-
type: "object",
|
|
100
|
-
properties: { runId: { type: "string" } },
|
|
101
|
-
required: ["runId"]
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
async function callPollRunTool(core, identity, args) {
|
|
105
|
-
const runId = typeof args.runId === "string" ? args.runId : "";
|
|
106
|
-
try {
|
|
107
|
-
return toolValue(await fetchRunSnapshot(core, identity, runId));
|
|
108
|
-
} catch (error) {
|
|
109
|
-
if (error instanceof RunNotFoundError)
|
|
110
|
-
return toolError2("Run not found");
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async function fetchRunSnapshot(core, identity, runId) {
|
|
115
|
-
const run = await core.runs.get({ identity, runId });
|
|
116
|
-
const batch = run.batchScopes?.[0];
|
|
117
|
-
return {
|
|
118
|
-
runId: run.id,
|
|
119
|
-
status: run.status,
|
|
120
|
-
output: run.result ?? null,
|
|
121
|
-
error: run.error ?? null,
|
|
122
|
-
...batch ? {
|
|
123
|
-
progress: {
|
|
124
|
-
discovered: batch.discovered,
|
|
125
|
-
succeeded: batch.succeeded,
|
|
126
|
-
failed: batch.failed,
|
|
127
|
-
skipped: batch.skipped
|
|
128
|
-
}
|
|
129
|
-
} : {}
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
var CallParamsSchema = z.object({
|
|
133
|
-
name: z.string().min(1),
|
|
134
|
-
arguments: z.record(z.string(), z.unknown()).default({})
|
|
135
|
-
});
|
|
136
|
-
function serveForwardedTools2(args) {
|
|
137
|
-
return handleMcpPost(args.reply, args.body, async (method, params) => {
|
|
138
|
-
switch (method) {
|
|
139
|
-
case "initialize":
|
|
140
|
-
return {
|
|
141
|
-
protocolVersion: negotiateProtocolVersion(params.protocolVersion),
|
|
142
|
-
capabilities: { tools: {} },
|
|
143
|
-
serverInfo: { name: args.serverName, version: "1" }
|
|
144
|
-
};
|
|
145
|
-
case "ping":
|
|
146
|
-
return {};
|
|
147
|
-
case "tools/list":
|
|
148
|
-
return { tools: (await args.listTools()).map(forwardedTool) };
|
|
149
|
-
case "tools/call": {
|
|
150
|
-
const call = CallParamsSchema.safeParse(params);
|
|
151
|
-
if (!call.success)
|
|
152
|
-
throw new McpRequestError2(-32602, "Invalid tool call");
|
|
153
|
-
return forwardedToolResult(await args.callTool(call.data));
|
|
154
|
-
}
|
|
155
|
-
default:
|
|
156
|
-
throw new McpRequestError2(-32601, "Method not found");
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export { negotiateProtocolVersion, McpRequestError2, handleMcpPost, toolValue, toolError2, POLL_RUN_TOOL, POLL_RUN_TOOL_DEFINITION, callPollRunTool, serveForwardedTools2 };
|
|
162
|
-
|
|
163
|
-
//# debugId=74DF53EFD630A4A264756E2164756E21
|
|
164
|
-
//# sourceMappingURL=chunk-baagvvm3.js.map
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/mcp-shared.ts"],
|
|
4
|
-
"sourcesContent": [
|
|
5
|
-
"import type { CatamorphicCore, Identity } from \"@catamorphic/core\";\nimport { MCP_POLL_RUN_TOOL, RunNotFoundError } from \"@catamorphic/core\";\nimport type { FastifyReply } from \"fastify\";\nimport { z } from \"zod\";\n\n/**\n * The JSON-RPC / MCP plumbing shared by the plugin's MCP endpoints — the\n * apps server (`/projects/:id/apps-mcp`), the workflow-tools server\n * (`/projects/:id/mcp`) and the connection broker (`/connection-mcp`) —\n * and by hosts that forward MCP servers of their own. All are stateless\n * Streamable-HTTP handlers speaking the `initialize`-handshake protocol era\n * (≤2025-11-25): requests are independent, no sessions, notifications\n * answered with a bare 202.\n */\n\nexport interface JsonRpcMessage {\n jsonrpc?: string;\n id?: number | string | null;\n method?: string;\n params?: Record<string, unknown>;\n}\n\nexport const SUPPORTED_PROTOCOL_VERSIONS = new Set([\n \"2024-11-05\",\n \"2025-03-26\",\n \"2025-06-18\",\n \"2025-11-25\",\n]);\nexport const DEFAULT_PROTOCOL_VERSION = \"2025-06-18\";\n\n/** The requested revision when supported, our default otherwise. */\nexport function negotiateProtocolVersion(requested: unknown): string {\n return typeof requested === \"string\" &&\n SUPPORTED_PROTOCOL_VERSIONS.has(requested)\n ? requested\n : DEFAULT_PROTOCOL_VERSION;\n}\n\n/** A dispatch failure that should ride a specific JSON-RPC error code. */\nexport class McpRequestError extends Error {\n constructor(\n readonly code: number,\n message: string,\n ) {\n super(message);\n this.name = \"McpRequestError\";\n }\n}\n\n/**\n * The stateless request shell the endpoints share: body-shape validation,\n * the notification 202, missing-method rejection, and error mapping.\n * `dispatch` returns the JSON-RPC result for a method; it throws\n * {@link McpRequestError} for coded failures (-32601 method-not-found,\n * -32602 bad params) and anything else becomes -32603.\n */\nexport async function handleMcpPost(\n reply: FastifyReply,\n body: unknown,\n dispatch: (\n method: string,\n params: Record<string, unknown>,\n ) => Promise<unknown>,\n): Promise<FastifyReply> {\n const message = body as JsonRpcMessage | null;\n if (!message || typeof message !== \"object\" || Array.isArray(message)) {\n return rpcError(reply, null, -32600, \"Expected a single JSON-RPC message\");\n }\n // Notifications (initialized, cancelled, …) need no body in return.\n if (message.id === undefined || message.id === null) {\n return reply.status(202).send();\n }\n if (typeof message.method !== \"string\") {\n return rpcError(reply, message.id, -32600, \"Missing method\");\n }\n try {\n const result = await dispatch(message.method, message.params ?? {});\n return rpcResult(reply, message.id, result);\n } catch (error) {\n if (error instanceof McpRequestError) {\n return rpcError(reply, message.id, error.code, error.message);\n }\n return rpcError(\n reply,\n message.id,\n -32603,\n error instanceof Error ? error.message : \"Internal error\",\n );\n }\n}\n\nexport function rpcResult(\n reply: FastifyReply,\n id: number | string,\n result: unknown,\n): FastifyReply {\n return reply\n .header(\"content-type\", \"application/json\")\n .send({ jsonrpc: \"2.0\", id, result });\n}\n\nexport function rpcError(\n reply: FastifyReply,\n id: number | string | null,\n code: number,\n message: string,\n): FastifyReply {\n return reply\n .header(\"content-type\", \"application/json\")\n .send({ jsonrpc: \"2.0\", id, error: { code, message } });\n}\n\n/**\n * Tool result: JSON text for model readability plus `structuredContent`\n * for structured consumers. MCP defines structured content as a JSON\n * object, and clients such as Claude Code reject anything else, so arrays\n * and primitives ride the text channel alone.\n */\nexport function toolValue(value: unknown): Record<string, unknown> {\n return {\n content: [{ type: \"text\", text: JSON.stringify(value) ?? \"null\" }],\n ...(typeof value === \"object\" && value !== null && !Array.isArray(value)\n ? { structuredContent: value }\n : {}),\n };\n}\n\nexport function toolError(message: string): Record<string, unknown> {\n return { content: [{ type: \"text\", text: message }], isError: true };\n}\n\n/**\n * Another MCP server's tool, listed as that server declared it (ADR 0225).\n * These endpoints speak revisions where an output schema describes an\n * object, so any other is left out: advertising it would make clients\n * reject the results it describes.\n */\nexport function forwardedTool(tool: {\n name: string;\n title?: string;\n description?: string;\n inputSchema: unknown;\n outputSchema?: unknown;\n annotations?: unknown;\n}): Record<string, unknown> {\n return {\n name: tool.name,\n ...(tool.title ? { title: tool.title } : {}),\n ...(tool.description ? { description: tool.description } : {}),\n inputSchema: tool.inputSchema,\n ...(isObjectSchema(tool.outputSchema)\n ? { outputSchema: tool.outputSchema }\n : {}),\n ...(tool.annotations ? { annotations: tool.annotations } : {}),\n };\n}\n\nconst ForwardedToolResultSchema = z.looseObject({\n content: z.array(z.unknown()),\n structuredContent: z.unknown().optional(),\n});\n\n/**\n * Another MCP server's tool result, forwarded as it was sent: content,\n * structured content, `isError` and `_meta` unchanged (ADR 0225). These\n * revisions require structured content to be an object, so any other\n * (which newer revisions allow) is left out, and becomes the text when the\n * server sent none. Anything that is not a tool result answers as a value,\n * never as a failure: the call has already run.\n */\nexport function forwardedToolResult(result: unknown): Record<string, unknown> {\n const parsed = ForwardedToolResultSchema.safeParse(result);\n if (!parsed.success) return toolValue(result);\n const { structuredContent, ...rest } = parsed.data;\n if (structuredContent === undefined) return rest;\n if (isObject(structuredContent)) return { ...rest, structuredContent };\n return rest.content.length > 0\n ? rest\n : {\n ...rest,\n content: [{ type: \"text\", text: JSON.stringify(structuredContent) }],\n };\n}\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isObjectSchema(schema: unknown): boolean {\n return isObject(schema) && schema.type === \"object\";\n}\n\n/**\n * Shared contract with `@catamorphic/app`'s MCP-host guest adapter (which\n * duplicates the name — it cannot depend on core).\n */\nexport const POLL_RUN_TOOL = MCP_POLL_RUN_TOOL;\n\n/** The poll tool served beside the real tools on both MCP endpoints. */\nexport const POLL_RUN_TOOL_DEFINITION: Record<string, unknown> = {\n name: POLL_RUN_TOOL,\n annotations: { readOnlyHint: true },\n description:\n \"Poll a workflow run by its runId — one started asynchronously or one \" +\n \"that detached mid-call. Returns the run's status, output (once \" +\n \"completed), and batch progress.\",\n inputSchema: {\n type: \"object\",\n properties: { runId: { type: \"string\" } },\n required: [\"runId\"],\n },\n};\n\n/** The poll tool's shared implementation, same on both endpoints. */\nexport async function callPollRunTool(\n core: CatamorphicCore,\n identity: Identity,\n args: Record<string, unknown>,\n): Promise<Record<string, unknown>> {\n const runId = typeof args.runId === \"string\" ? args.runId : \"\";\n try {\n return toolValue(await fetchRunSnapshot(core, identity, runId));\n } catch (error) {\n if (error instanceof RunNotFoundError) return toolError(\"Run not found\");\n throw error;\n }\n}\n\nexport interface RunSnapshotShape {\n runId: string;\n status: string;\n output: unknown;\n error: string | null;\n progress?: {\n discovered: number;\n succeeded: number;\n failed: number;\n skipped: number;\n };\n}\n\n/** Mirrors the in-product broker's run snapshot (AppMount's shape). */\nexport async function fetchRunSnapshot(\n core: CatamorphicCore,\n identity: Identity,\n runId: string,\n): Promise<RunSnapshotShape> {\n const run = await core.runs.get({ identity, runId });\n const batch = (\n run as {\n batchScopes?: Array<{\n discovered: number;\n succeeded: number;\n failed: number;\n skipped: number;\n }>;\n }\n ).batchScopes?.[0];\n return {\n runId: run.id,\n status: run.status,\n output: (run as { result?: unknown }).result ?? null,\n error: (run as { error?: string | null }).error ?? null,\n ...(batch\n ? {\n progress: {\n discovered: batch.discovered,\n succeeded: batch.succeeded,\n failed: batch.failed,\n skipped: batch.skipped,\n },\n }\n : {}),\n };\n}\n\n/** A tool another MCP server declared, as a forwarding endpoint lists it. */\nexport interface ForwardedToolDefinition {\n name: string;\n title?: string;\n description?: string;\n inputSchema: unknown;\n outputSchema?: unknown;\n annotations?: unknown;\n}\n\nconst CallParamsSchema = z.object({\n name: z.string().min(1),\n arguments: z.record(z.string(), z.unknown()).default({}),\n});\n\n/**\n * A stateless MCP endpoint serving another server's tools: tools listed\n * and results returned as that server declared and sent them (ADR 0225).\n * `callTool` answers with the server's own result (any tool result, such\n * as {@link toolError}, passes through), or throws {@link McpRequestError}\n * for a refusal at the protocol boundary.\n */\nexport function serveForwardedTools(args: {\n reply: FastifyReply;\n body: unknown;\n serverName: string;\n listTools(): Promise<readonly ForwardedToolDefinition[]>;\n callTool(call: {\n name: string;\n arguments: Record<string, unknown>;\n }): Promise<unknown>;\n}): Promise<FastifyReply> {\n return handleMcpPost(args.reply, args.body, async (method, params) => {\n switch (method) {\n case \"initialize\":\n return {\n protocolVersion: negotiateProtocolVersion(params.protocolVersion),\n capabilities: { tools: {} },\n serverInfo: { name: args.serverName, version: \"1\" },\n };\n case \"ping\":\n return {};\n case \"tools/list\":\n return { tools: (await args.listTools()).map(forwardedTool) };\n case \"tools/call\": {\n const call = CallParamsSchema.safeParse(params);\n if (!call.success)\n throw new McpRequestError(-32602, \"Invalid tool call\");\n return forwardedToolResult(await args.callTool(call.data));\n }\n default:\n throw new McpRequestError(-32601, \"Method not found\");\n }\n });\n}\n"
|
|
6
|
-
],
|
|
7
|
-
"mappings": ";AACA;AAEA;AAmBO,IAAM,8BAA8B,IAAI,IAAI;AAAA,EACjD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AACM,IAAM,2BAA2B;AAGjC,SAAS,wBAAwB,CAAC,WAA4B;AAAA,EACnE,OAAO,OAAO,cAAc,YAC1B,4BAA4B,IAAI,SAAS,IACvC,YACA;AAAA;AAAA;AAIC,MAAM,yBAAwB,MAAM;AAAA,EAE9B;AAAA,EADX,WAAW,CACA,MACT,SACA;AAAA,IACA,MAAM,OAAO;AAAA,IAHJ;AAAA,IAIT,KAAK,OAAO;AAAA;AAEhB;AASO,eAAe,aAAa,CACjC,OACA,MACA,UAIuB;AAAA,EACvB,MAAM,UAAU;AAAA,EAChB,IAAI,CAAC,WAAW,OAAO,YAAY,YAAY,MAAM,QAAQ,OAAO,GAAG;AAAA,IACrE,OAA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