mirror of
https://github.com/morluto/rea.git
synced 2026-11-03 13:26:10 +00:00
76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import type { McpServer } from "@modelcontextprotocol/server";
|
|
import { z } from "zod";
|
|
|
|
import type { BinarySessionPort } from "../application/BinarySession.js";
|
|
import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
|
import { toCallToolResult } from "./toolResult.js";
|
|
import type { Logger } from "../logger.js";
|
|
import { logToolExecution } from "./toolLogging.js";
|
|
|
|
/** Register MCP-only target lifecycle operations on a long-lived session. */
|
|
export const registerSessionTools = (
|
|
server: McpServer,
|
|
session: BinarySessionPort,
|
|
logger: Logger,
|
|
): void => {
|
|
const [openContract, closeContract, statusContract] = SESSION_TOOL_CONTRACTS;
|
|
server.registerTool(
|
|
openContract.name,
|
|
{
|
|
description: openContract.description,
|
|
inputSchema: openContract.inputSchema,
|
|
outputSchema: openContract.outputSchema,
|
|
annotations: openContract.annotations,
|
|
},
|
|
async (input, context) => {
|
|
const parsed = z.object({ path: z.string().min(1) }).parse(input);
|
|
const opened = await logToolExecution(logger, openContract.name, () =>
|
|
session.open(parsed.path, { signal: context.mcpReq.signal }),
|
|
);
|
|
return opened.ok
|
|
? toCallToolResult(
|
|
{
|
|
ok: true,
|
|
value: {
|
|
path: opened.value.path,
|
|
format: opened.value.format,
|
|
kind: opened.value.kind,
|
|
loaderArgs: [...opened.value.loaderArgs],
|
|
sha256: opened.value.sha256,
|
|
architecture: opened.value.architecture ?? null,
|
|
},
|
|
},
|
|
openContract,
|
|
)
|
|
: toCallToolResult(opened, openContract);
|
|
},
|
|
);
|
|
server.registerTool(
|
|
closeContract.name,
|
|
{
|
|
description: closeContract.description,
|
|
inputSchema: closeContract.inputSchema,
|
|
outputSchema: closeContract.outputSchema,
|
|
annotations: closeContract.annotations,
|
|
},
|
|
async () =>
|
|
toCallToolResult(
|
|
await logToolExecution(logger, closeContract.name, () =>
|
|
session.close(),
|
|
),
|
|
closeContract,
|
|
),
|
|
);
|
|
server.registerTool(
|
|
statusContract.name,
|
|
{
|
|
description: statusContract.description,
|
|
inputSchema: statusContract.inputSchema,
|
|
outputSchema: statusContract.outputSchema,
|
|
annotations: statusContract.annotations,
|
|
},
|
|
() =>
|
|
toCallToolResult({ ok: true, value: session.status() }, statusContract),
|
|
);
|
|
};
|