// engine/elicitation/capability.ts — MCP Elicitation capability detection (SPEC-595)
//
// Elicitation (MCP spec 2025-06-18) is a client-advertised capability.
// The server checks the connected client's declared capabilities at invocation
// time and either routes through server.elicitInput() or falls back to the
// text-based interactiveQuestions[] contract to preserve no-breaking-change
// semantics for legacy clients.
/**
 * Per-session capability cache. The client capabilities do not change for the
 * lifetime of a connection, so check once per server instance. Keyed by server
 * object identity (WeakMap) so stale cache entries are GC'd automatically.
 */
const capabilityCache = new WeakMap();
/**
 * Return whether the connected client supports MCP Elicitation.
 * Fail-closed on unknown state: if capabilities cannot be read, returns false
 * so callers fall back to the text-based interactiveQuestions[] path.
 */
export function canElicit(server) {
    if (!server) {
        return false;
    }
    const cached = capabilityCache.get(server);
    if (cached) {
        return cached.supported;
    }
    const result = probeCapability(server);
    capabilityCache.set(server, result);
    return result.supported;
}
/** Test helper — production code never calls this. */
export function resetElicitationCapabilityCache() {
    // No-op: WeakMap entries are GC'd when the server reference is replaced.
}
function probeCapability(server) {
    if (typeof server.elicitInput !== 'function') {
        return { supported: false, reason: 'server.elicitInput not available' };
    }
    if (typeof server.getClientCapabilities === 'function') {
        const caps = server.getClientCapabilities();
        if (!caps) {
            return { supported: false, reason: 'client capabilities not available' };
        }
        const elicitation = caps.elicitation;
        if (!elicitation || typeof elicitation !== 'object') {
            return { supported: false, reason: 'client did not advertise elicitation capability' };
        }
        return { supported: true };
    }
    return { supported: true, reason: 'assumed from elicitInput presence' };
}
//# sourceMappingURL=capability.js.map