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.
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:
messageId. Do not start a second round of work.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.
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/fork.
With a cursorId, the new session's history is the source session's
history before that session cursor. Throw -32602 for a cursorId the
Agent did not announce on the source session, no longer accepts, or
whose capabilities do not include fork. Do not fork the whole session
in that case. The new session does not inherit the source session's
cursors, or the idempotencyKeys of its prompts: the same key in the
new session names an unrelated submission.
Handles session/archive. Archiving an active or loaded session first
cancels its work and frees its resources as session/close would.
Archiving an archived session succeeds. An unknown session gets -32002.
Handles session/unarchive. It does not resume the session. Unarchiving
a session that is not archived succeeds. An unknown session gets
-32002.
Handles session/status. It must not resume, create, close, or change
the session. An unknown session gets -32002. Include the current
state snapshot when the session is open.
Handles session/options. Respond with the config options and commands
a session would start with. With sessionId, respond with what
session/resume would return for that session. Clients also send the
source session's sessionId to show the options before a
session/fork. It must not create, resume, or change a session, send
session/update notifications, or add an entry to session/list. The
agent may start its runtime to resolve the options, and these rules
still hold for it. An unknown session gets -32002.
Handles session/set_title. Respond with the stored title after storing
it. A title that is invalid after normalization gets -32602, and an
unknown session gets -32002.
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.
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.
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.
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.
Handles auth/status. It must not change any agent state, and it must
not fail with -32000 (Authentication required).
Agent request handlers keyed by ACP protocol method name.