import { McpToolResult } from "./types"

export function asToolResult(payload: unknown): McpToolResult {
    const structuredContent =
        payload && typeof payload === "object" && !Array.isArray(payload)
            ? payload as Record<string, unknown>
            : { result: payload }

    return {
        content: [
            {
                type: "text",
                text: JSON.stringify(payload, null, 2),
            },
        ],
        structuredContent,
    }
}

export function asToolError(error: unknown): McpToolResult {
    const message = error instanceof Error ? error.message : "Unbekannter Fehler"

    return {
        content: [
            {
                type: "text",
                text: message,
            },
        ],
        isError: true,
        structuredContent: {
            error: message,
        },
    }
}

