Add MCP server layer: tool result, server, tool registration

This commit is contained in:
morluto
2026-07-12 03:25:23 +08:00
parent 63b33abcf8
commit 09bec111d5
4 changed files with 147 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
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);
};