33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
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 MCP text content.
|
|
* Error tags and safe messages remain visible while underlying causes, process
|
|
* output, and other potentially sensitive details stay private.
|
|
*/
|
|
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);
|
|
};
|