mirror of
https://github.com/morluto/rea
synced 2026-10-28 15:07:26 -04:00
185bf90c41
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
// Own the complete MCP tool-result body, including the second JSON escaping layer.
|
|
import { TARGET_CONTENT_RESULT_META } from "../../dist/server/toolResultEncoding.js";
|
|
import { MAX_MCP_RESULT_BYTES } from "./limits.mjs";
|
|
import { measureJson, serializeJson } from "./json-capacity.mjs";
|
|
import { serializationFailure } from "./fact-provenance.mjs";
|
|
|
|
const envelope = (text) => ({
|
|
content: [{ type: "text", text }],
|
|
_meta: TARGET_CONTENT_RESULT_META,
|
|
});
|
|
const attempt = (value) => {
|
|
const encoded = serializeJson(value, MAX_MCP_RESULT_BYTES);
|
|
if (!encoded.ok) return encoded;
|
|
const reply = envelope(encoded.text),
|
|
measured = measureJson(reply, MAX_MCP_RESULT_BYTES);
|
|
return measured.ok ? { ok: true, reply } : measured;
|
|
};
|
|
|
|
export const encodeMcpResult = (value) => {
|
|
const encoded = attempt(value);
|
|
if (encoded.ok) return { result: value, reply: encoded.reply };
|
|
const result = serializationFailure(
|
|
value,
|
|
encoded.error,
|
|
"mcp_reply",
|
|
"result_budget",
|
|
),
|
|
fallback = attempt(result);
|
|
if (!fallback.ok)
|
|
throw new Error("MCP capacity diagnostic could not be encoded");
|
|
return { result, reply: fallback.reply };
|
|
};
|