histoire
Version:
A fast story book powered by Vite
25 lines (24 loc) • 1.23 kB
JavaScript
import { z as schema } from 'zod/v4';
import { McpDomainError, mcpErrorSchema, toMcpError } from './errors.js';
import { MCP_LIMITS, mcpByteLength } from './limits.js';
/** Strict output union accepted by every registered tool. */
export function mcpOutputSchema(data) {
return schema.union([
schema.strictObject({ ok: schema.literal(true), data }),
schema.strictObject({ ok: schema.literal(false), error: mcpErrorSchema }),
]);
}
/** Build matching text/structured success; cap the encoded structured JSON envelope. */
export function successResult(data) {
const structuredContent = { ok: true, data };
const text = JSON.stringify(structuredContent);
if (mcpByteLength(text) > MCP_LIMITS.responseBytes) {
throw new McpDomainError('RESULT_TOO_LARGE', 'MCP result exceeds response byte limit');
}
return { content: [{ type: 'text', text }], structuredContent };
}
/** Convert expected failure to tool envelope; unknown exceptions remain generic. */
export function errorResult(error) {
const structuredContent = { ok: false, error: toMcpError(error) };
return { isError: true, content: [{ type: 'text', text: JSON.stringify(structuredContent) }], structuredContent };
}