// tool-list-compactor.ts — Compacts MCP tools/list payload to stay under host size limits.
// Claude Code and other hosts silently drop MCP servers whose tools/list exceeds ~128 KB.
// This module trims description fields in the outgoing JSON-RPC response without modifying
// tool functionality or removing any tools from the registry. (SPEC-1005)
const DESCRIPTION_MAX = 200;
const PROPERTY_DESC_MAX = 100;
function trimText(text, max) {
    if (typeof text !== 'string' || text.length === 0) {
        return '';
    }
    if (text.length <= max) {
        return text;
    }
    // Prefer cutting at a sentence boundary within the budget.
    const boundary = text.slice(0, max).lastIndexOf('. ');
    const cut = boundary > max / 2 ? boundary + 1 : max;
    return text.slice(0, cut).trimEnd() + '…';
}
function compactProperties(properties) {
    const out = {};
    for (const [key, val] of Object.entries(properties)) {
        if (val === null || typeof val !== 'object') {
            out[key] = val;
            continue;
        }
        const prop = { ...val };
        if (typeof prop.description === 'string') {
            prop.description = trimText(prop.description, PROPERTY_DESC_MAX);
        }
        // Recurse into nested properties (e.g. object types inside arrays).
        if (prop.properties !== null && typeof prop.properties === 'object') {
            prop.properties = compactProperties(prop.properties);
        }
        out[key] = prop;
    }
    return out;
}
function compactTool(tool) {
    const compacted = { ...tool };
    if (typeof compacted.description === 'string') {
        compacted.description = trimText(compacted.description, DESCRIPTION_MAX);
    }
    const schema = compacted.inputSchema;
    if (schema !== null && typeof schema === 'object') {
        const s = { ...schema };
        if (s.properties !== null && typeof s.properties === 'object') {
            s.properties = compactProperties(s.properties);
        }
        compacted.inputSchema = s;
    }
    return compacted;
}
/** Compact an array of MCP tool objects in-place (returns new array). */
export function compactToolList(tools) {
    return tools.map((t) => t !== null && typeof t === 'object'
        ? compactTool(t)
        : t);
}
/** Returns true when an outgoing JSON-RPC message is a tools/list result. */
export function isToolsListResponse(message) {
    if (message === null || typeof message !== 'object') {
        return false;
    }
    const m = message;
    if (!('result' in m)) {
        return false;
    }
    const result = m.result;
    if (result === null || typeof result !== 'object') {
        return false;
    }
    const tools = result.tools;
    return Array.isArray(tools);
}
/** Compact a tools/list JSON-RPC response message. Returns a new object. */
export function compactToolsListMessage(message) {
    const result = message.result;
    const tools = result.tools;
    return {
        ...message,
        result: {
            ...result,
            tools: compactToolList(tools),
        },
    };
}
//# sourceMappingURL=tool-list-compactor.js.map