mirror of
https://github.com/morluto/rea.git
synced 2026-10-27 09:55:46 +00:00
29 lines
907 B
TypeScript
29 lines
907 B
TypeScript
import type { CallToolResult } from "@modelcontextprotocol/server";
|
|
|
|
import type { HopperError } from "../domain/errors.js";
|
|
import type { Result } from "../domain/result.js";
|
|
import type { JsonValue } from "../hopper/protocol.js";
|
|
|
|
/** Translate an application result into caller-visible MCP content. */
|
|
export const toCallToolResult = (
|
|
result: Result<JsonValue, HopperError>,
|
|
): CallToolResult =>
|
|
result.ok
|
|
? { content: [{ type: "text", text: formatValue(result.value) }] }
|
|
: {
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: `${result.error._tag}: ${result.error.message}`,
|
|
},
|
|
],
|
|
isError: true,
|
|
};
|
|
|
|
const formatValue = (value: JsonValue): string => {
|
|
if (value === null) return "OK";
|
|
if (typeof value === "string") return value;
|
|
if (typeof value === "object") return JSON.stringify(value, null, 2);
|
|
return String(value);
|
|
};
|