// MCP arg-coercion + JSON response helpers. Pure functions — no server state.
import { ValidationError } from '../errors.js';
export function readProtocolVersion(params) {
    return typeof params?.protocolVersion === 'string' && params.protocolVersion.trim()
        ? params.protocolVersion
        : '2025-06-18';
}
export function readRequiredMcpString(value, path) {
    if (typeof value !== 'string' || value.trim().length === 0) {
        throw new ValidationError(`${path} must be a non-empty string.`, [
            { path, message: `${path} must be a non-empty string.` },
        ]);
    }
    return value;
}
export function readOptionalMcpString(value, path) {
    if (value === undefined || value === null)
        return undefined;
    if (typeof value !== 'string' || value.trim().length === 0) {
        throw new ValidationError(`${path} must be a non-empty string when provided.`, [
            { path, message: `${path} must be a non-empty string when provided.` },
        ]);
    }
    return value;
}
export function readMcpStringArray(value, path) {
    if (value === undefined || value === null)
        return [];
    if (!Array.isArray(value)) {
        throw new ValidationError(`${path} must be an array of strings.`, [
            { path, message: `${path} must be an array of strings.` },
        ]);
    }
    return value.map((entry, index) => {
        if (typeof entry !== 'string' || entry.trim().length === 0) {
            throw new ValidationError(`${path}[${index}] must be a non-empty string.`, [
                { path: `${path}[${index}]`, message: 'must be a non-empty string.' },
            ]);
        }
        return entry;
    });
}
export function readToolName(request) {
    if (request.method !== 'tools/call') {
        return undefined;
    }
    const params = request.params;
    return typeof params?.name === 'string' ? params.name : undefined;
}
export function readToolArguments(request) {
    if (request.method !== 'tools/call') {
        return {};
    }
    const args = request.params?.arguments;
    return args && typeof args === 'object' && !Array.isArray(args) ? args : {};
}
export function readString(value) {
    return typeof value === 'string' && value.trim() ? value : undefined;
}
export function readStringArray(value) {
    if (!Array.isArray(value)) {
        return undefined;
    }
    return value.filter((item) => typeof item === 'string' && item.trim().length > 0);
}
export function toolJson(value) {
    return {
        content: [
            {
                type: 'text',
                text: JSON.stringify(value, null, 2),
            },
        ],
        structuredContent: value,
    };
}
export function resourceJson(uri, value) {
    return {
        contents: [
            {
                uri,
                mimeType: 'application/json',
                text: JSON.stringify(value, null, 2),
            },
        ],
    };
}
