ACP v2 (Preview) TypeScript SDK — EXPERIMENTAL - v2.12.1
    Preparing search index...

    Type Alias AgentRequestHandlersByMethodExperimental

    Agent request handlers keyed by ACP protocol method name.

    type AgentRequestHandlersByMethod = {
        initialize: AgentRequestHandler<InitializeRequest, InitializeResponse>;
        "auth/login": AgentRequestHandler<
            LoginAuthRequest,
            LoginAuthResponse
            | void,
        >;
        "session/new": AgentRequestHandler<NewSessionRequest, NewSessionResponse>;
        "session/set_config_option": AgentRequestHandler<
            schema.SetSessionConfigOptionRequest,
            SetSessionConfigOptionResponse,
        >;
        "session/prompt": AgentRequestHandler<PromptRequest, PromptResponse>;
        "session/list": AgentRequestHandler<
            ListSessionsRequest,
            ListSessionsResponse,
        >;
        "session/delete": AgentRequestHandler<
            DeleteSessionRequest,
            DeleteSessionResponse
            | void,
        >;
        "session/resume": AgentRequestHandler<
            ResumeSessionRequest,
            ResumeSessionResponse,
        >;
        "session/close": AgentRequestHandler<
            CloseSessionRequest,
            CloseSessionResponse
            | void,
        >;
        "tool_call/cancel": AgentRequestHandler<
            CancelToolCallRequest,
            CancelToolCallResponse
            | void,
        >;
        "terminal/cancel": AgentRequestHandler<
            CancelTerminalRequest,
            CancelTerminalResponse
            | void,
        >;
        "auth/logout": AgentRequestHandler<
            LogoutAuthRequest,
            LogoutAuthResponse
            | void,
        >;
    }
    Index

    Handles initialize.

    Runs again for each repeated initialize, which the Client may send after a previous one completed and before any other request; the latest successful exchange is authoritative. Other requests that arrive while initialize is in flight are rejected with -32600. A repeat is negotiated like the first initialize: one that requests a version below v2 gets -32022 without reaching this handler, and the previous initialization stays in effect.

    When the Client requests a newer protocol version than v2, params that do not parse under v2 are dropped instead of rejected, and a missing or malformed info is replaced with { name: "unknown", version: "unknown" }.

    Handles session/new. Return the session's configOptions and availableCommands in the response when it has any. The response replaces the lists the Client holds, including lists from updates that arrived before it, and an omitted list means none. If the new session is not idle, send its state_update after the response. Clients may drop updates for a session they do not know yet. The app sends the response once this handler's promise settles, so a notification sent inside the handler goes out before it. If the Agent cannot apply a supplied systemPrompt because of its value, reject with -32602 without creating the session. Reject a stdio MCP server whose command is not an absolute path with -32602, like a repeated server name or an unadvertised transport. assertSupportedMcpServers rejects all three. If a server fails to start or connect, continue without it and tell the user, for example with a notice.

    "session/set_config_option": AgentRequestHandler<
        schema.SetSessionConfigOptionRequest,
        SetSessionConfigOptionResponse,
    >

    Handles session/prompt. Respond with the messageId of the inserted user message once it is inserted. Do not reject a prompt only because foreground work is active: insert it or hold it, and insert held prompts in the order received. On session/cancel and session/close, answer every prompt that was received before it and is not inserted with -32800. Do not reject a prompt only because a cancellation is in progress either. Insert it, or hold it when inserting it would start its work before the cancelled work reported idle. Honoring $/cancel_request for a held prompt is optional. An inserted prompt is answered with its messageId and cannot be withdrawn.

    Reject a prompt that contains a content block type other than text, resource_link, and the types the Agent advertised with -32602, without inserting it.

    An Agent that advertises capabilities.session supports idempotencyKey. The app rejects a key that is not a string of 1 to 255 characters with -32602 before this handler runs. Look the key up in the session:

    • Nothing recorded: handle the prompt as usual and record the key with the user message when it is inserted.
    • A message inserted with this key: insert nothing and respond with that message's messageId. Do not start a second round of work.
    • A prompt with this key that is not inserted yet, such as a held prompt: do not hold a second one. Answer this request when the first one is answered, with the same outcome.

    An error response leaves the key unused. Never insert a second message for a recorded key. When a request carries a recorded key with a different prompt, answer it as a retry or reject it with -32602. Comparing prompts is optional. If you honor $/cancel_request for a pending request with the key, withdraw the prompt when it is not inserted yet and answer every pending request for the key with -32800.

    How long a key lasts depends on its message. For a message that is retained, recognize the key for as long as the message is, across connections and Agent restarts. For a message that is not retained, such as a locally handled command, recognize the key while the session stays active on a connection. After that the key may be forgotten, and a prompt with a forgotten key is a new submission. Store the key in the same step as the message, for example by deriving the messageId from the key with a one-way function such as UUIDv5. Do not echo keys in session updates, session/list results, or replayed history.

    Handles session/list. Do not return child sessions. The Agent announces them on their parent session with subagent_update.

    "session/delete": AgentRequestHandler<
        DeleteSessionRequest,
        DeleteSessionResponse
        | void,
    >

    Handles session/delete. Cancel the session's ongoing work and free its resources before deleting it, as session/close would when the session is active on a connection. Deleting a session that does not exist succeeds. A child session cannot be deleted on its own: reject its sessionId with -32602.

    Handles session/resume.

    With a replayFrom cursor, replay the history as session/update notifications, then send replay_complete exactly once, also when the replay is empty. From the time the request arrives until then, send only replayed history for the session and hold its other updates. Requests such as session/request_permission should wait too. Do not replay past usage_update, session_info_update, config_option_update, or available_commands_update notifications, which describe earlier values. Do not send replay_complete when replayFrom is omitted or null, or when the replay cannot be finished. Throw -32602 for an unsupported cursor, without replaying anything.

    A replay can include the history of child sessions. Each child that the replay announces with subagent_update gets its own replay_complete exactly once, on its own sessionId, after its announcement and its last replayed update, also when it has no replayed history. The same rules then apply to each session separately, and no session waits for another. A subagent_update is an update of the parent session, so the parent's replay_complete separates a replayed one from a current one.

    To replay an entity from its beginning as chunks, first send that entity's update with the same ID and the chunked field emptied: content: [] on user_message, agent_message, agent_thought, session_message, and tool_call_update, output: { data: "" } on terminal_update, and summary: [] on compaction_update. An update that carries the whole content needs no such update first.

    For every successful resume, send the current state_update before responding, after replay_complete when a replay was requested. Before it, send a current terminal_update, tool_call_update, or subagent_update for each item in its background list that the replay did not deliver at its current state. A replayed subagent_update never delivers a child's current capabilities or state, so send one for each listed child after the parent's replay_complete. A resume without replayFrom synchronizes the session's state, not its conversation: messages and foreground tool calls in progress continue with their next live update.

    Return the session's configOptions and availableCommands in the response when it has any. The response replaces the lists the Client holds. If the Agent cannot apply a supplied systemPrompt because of its value, reject with -32602 without resuming the session. Reject a child session's sessionId with -32602 too: the Client reaches a child through the session it created or resumed.

    "session/close": AgentRequestHandler<
        CloseSessionRequest,
        CloseSessionResponse
        | void,
    >

    Handles session/close. Before responding, cancel every pending session/request_permission request and elicitation in the session and in its child sessions with $/cancel_request, for example by aborting the cancellationSignal passed to AgentContext.request. Pass stopWaitingOnCancel: true with it so the pending request promise rejects with -32800 right away instead of waiting for the Client's answer. Closing also stops the session's background work and its child sessions, at any depth.

    After the response, send no notifications or requests for the session on this connection. A session/prompt request that is still pending is answered all the same: with -32800 when closing kept its message from being inserted, and with its messageId when the message was inserted. A child session is closed with its root session: reject a child's sessionId with -32602.

    "tool_call/cancel": AgentRequestHandler<
        CancelToolCallRequest,
        CancelToolCallResponse
        | void,
    >

    Handles tool_call/cancel.

    Respond once the cancellation is accepted, then confirm it with a tool_call_update to a final status. Throw -32602 for a tool call that is unknown, already final, or not cancellable. This includes a request the Client sends again after the first one was accepted and the tool call has since reached a final status. If the session is unknown or inactive, throw -32002 with data.sessionId. A child session that the Agent announced with subagent_update is not unknown: sessionId names the child for a tool call that the child reports. Without this handler or a "*" request fallback the app answers with -32601.

    "terminal/cancel": AgentRequestHandler<
        CancelTerminalRequest,
        CancelTerminalResponse
        | void,
    >

    Handles terminal/cancel.

    Respond once the cancellation is accepted, then confirm it with a terminal_update that sets exitStatus. Cancelling does not release the terminal. Throw -32602 for a terminal that is unknown, has already exited, or is not cancellable. This includes a request the Client sends again after the first one was accepted and the process has since exited. If the session is unknown or inactive, throw -32002 with data.sessionId. A child session that the Agent announced with subagent_update is not unknown: sessionId names the child for a terminal that the child reports. Without this handler or a "*" request fallback the app answers with -32601.

    Handles auth/logout. Logging out is idempotent: respond with the same empty result when there is no authenticated state to end, for example because the Client already logged out.