"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SERVER_CAPABILITIES = exports.SERVER_INFO = exports.SUPPORTED_PROTOCOL_VERSIONS = exports.PROTOCOL_VERSION_BACKCOMPAT = exports.PROTOCOL_VERSION_PREFERRED = void 0;
exports.handleInitialize = handleInitialize;
exports.handleInitialized = handleInitialized;
/**
 * MCP lifecycle handlers — `initialize` and `notifications/initialized`.
 *
 * Spec: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle
 * v1 supports protocol version `2025-06-18` (preferred) and `2025-03-26`
 * (backcompat). Anything else returns 400 to the client; per the spec, the
 * server SHOULD respond with its preferred supported version so the client
 * can decide whether to connect on the older version or disconnect.
 */
const session_ts_1 = require("./session.js");
const packageUtils_js_1 = require("../../utility/packageUtils.js");
exports.PROTOCOL_VERSION_PREFERRED = '2025-06-18';
exports.PROTOCOL_VERSION_BACKCOMPAT = '2025-03-26';
exports.SUPPORTED_PROTOCOL_VERSIONS = [exports.PROTOCOL_VERSION_PREFERRED, exports.PROTOCOL_VERSION_BACKCOMPAT];
exports.SERVER_INFO = {
    name: 'harper-mcp',
    version: packageUtils_js_1.packageJson.version,
};
/** Server capabilities advertised on `initialize` for v1. */
exports.SERVER_CAPABILITIES = {
    tools: { listChanged: true },
    resources: { listChanged: true },
    logging: {},
};
/**
 * Negotiate protocol version, create a session, and return the JSON-RPC
 * result body. The caller (transport core) maps `ok: false` to HTTP 400.
 *
 * Per the MCP spec: if the client requests a version the server does not
 * support, the server MUST respond with a version it does support (so the
 * client can decide whether to downgrade or disconnect) — NOT fail. We
 * pick `PROTOCOL_VERSION_PREFERRED` for any unknown version. The only
 * `ok: false` cases are a missing or non-string `protocolVersion` field,
 * which is a protocol-level violation rather than version negotiation.
 *
 * `instructions` is optional per spec; we omit it in v1 (it can be wired
 * later when tools land in #617 and we know which profile is active).
 */
async function handleInitialize(params, user) {
    const requested = params?.protocolVersion;
    if (typeof requested !== 'string') {
        return {
            ok: false,
            reason: `protocolVersion is required and must be a string`,
            supportedVersions: exports.SUPPORTED_PROTOCOL_VERSIONS,
        };
    }
    const negotiated = exports.SUPPORTED_PROTOCOL_VERSIONS.includes(requested)
        ? requested
        : exports.PROTOCOL_VERSION_PREFERRED;
    const session = await (0, session_ts_1.createSession)({ user, protocolVersion: negotiated });
    return {
        ok: true,
        session,
        result: {
            protocolVersion: negotiated,
            serverInfo: exports.SERVER_INFO,
            capabilities: exports.SERVER_CAPABILITIES,
        },
    };
}
/**
 * Flip the session to `initialized: true`. Called when the client posts the
 * `notifications/initialized` JSON-RPC notification (per MCP §lifecycle).
 * The transport returns HTTP 202 with no body regardless of outcome here.
 */
async function handleInitialized(session) {
    if (session.initialized)
        return session;
    const updated = { ...session, initialized: true };
    await (0, session_ts_1.saveSession)(updated);
    return updated;
}
//# sourceMappingURL=lifecycle.js.map