> ## Documentation Index
> Fetch the complete documentation index at: https://agentclientprotocol.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cancellation

> Mechanisms for request cancellation

ACP uses JSON-RPC 2.0 for making requests and getting responses.

The JSON-RPC specification doesn't define any standard mechanism for request cancellation and keeps it up to the implementation.

## `$/cancel_request` notification

To make cancellation consistent, ACP defines a `$/cancel_request` notification that can be sent to cancel requests. Its `requestId` identifies a request that the sender of `$/cancel_request` issued. Receivers match it only against requests they received from that sender, since each side chooses its own request IDs.

Cancellation remains optional as it might not be implementable in all clients or servers. For example if the implementation uses a single threaded synchronous programming language then there is little it can do to react to a `$/cancel_request` notification.

When a `$/cancel_request` notification is received by a supporting implementation, the implementation:

* **MAY** cancel the corresponding request activity and all nested activities related to that request
* **MAY** finish sending any pending notifications before responding
* **MUST** still send a response for the original request

If the request already finished, or the receiver doesn't cancel it, the receiver sends its normal response.

Receivers **MUST** ignore a `$/cancel_request` for a request ID that is unknown or already answered, and **MUST NOT** send a second response for it.

If the receiver does cancel the request, it sends one of these responses:

* A successful response, where the method defines one for cancellation: `session/prompt` after its user message is inserted (with its `messageId`), `session/request_permission` with the `cancelled` outcome, or `elicitation/create` with the `cancel` action
* An error response with code [`-32800` (Request Cancelled)](https://agentclientprotocol.com/protocol/v2/schema#errorcode)

A sender that stops waiting for a response, for example after a timeout or because the work that needed it ended, **SHOULD** send `$/cancel_request` for that request so the receiver can stop work or dismiss UI. The calling side **MAY** implement graceful cancellation processing by waiting for the response from the remote side.

Cancellation **MAY** also be done explicitly on a per-feature basis within the protocol to cover specific scenarios, such as cancellation of [active session work](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle.md#cancellation).

`session/cancel` and `$/cancel_request` have different scopes. Cancelling one
permission or elicitation request does not automatically cancel the whole
foreground workflow or determine its state. The Agent applies the aggregate
[state-transition rules](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle.md#state-transitions).

On `session/cancel`, the Agent stops waiting for pending permission requests of the cancelled foreground work and **SHOULD** cancel each of them with `$/cancel_request`. Clients **MAY** answer such a request with the `cancelled` outcome without waiting for `$/cancel_request`. After cancellation completes, Agents **SHOULD** ignore late
answers rather than use them to resume the cancelled work. After aborting active
foreground work and sending its final updates, the Agent **MUST** report `idle`
with `stopReason: "cancelled"`. No additional completion update is required if
no foreground work was active. See [Elicitation Cancellation](https://agentclientprotocol.com/protocol/v2/elicitation.md#session-cancellation)
for selective cancellation of related requests and external URL interactions.

## Internal cancellation

Requests can also be cancelled internally by the executing party without receiving `$/cancel_request`:

* On the Client, the user closes the IDE or switches projects, or a file becomes unavailable.
* On the Agent, the LLM context limit is reached, an internal timeout fires, or resources run out.

When internal cancellation occurs, the executing party **SHOULD**:

* Send the same response it would send for `$/cancel_request`: the method's defined cancellation result where one exists (an inserted prompt's `messageId`, a `cancelled` permission outcome, or an elicitation `cancel` action), otherwise `-32800` (Request Cancelled)
* Ensure consistent behavior regardless of cancellation source

Both kinds of cancellation answer `session/prompt` with `-32800` only before the user message is inserted. Once it is inserted, the Agent responds successfully with its `messageId`, and the work it started is stopped with [`session/cancel`](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle#cancellation) or ends with the matching [stop reason](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle.md#stop-reasons). See [Prompt Accepted](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle.md#2-prompt-accepted).

## Example: cascading cancellation flow

Sending `session/cancel` does not automatically cancel outstanding Agent-to-Client requests. The Agent sends `$/cancel_request` for the permission requests of the cancelled work, as specified in [active session work cancellation](https://agentclientprotocol.com/protocol/v2/prompt-lifecycle.md#cancellation), and the Client answers each one with the `cancelled` outcome.

```mermaid theme={null}
sequenceDiagram
    participant Client
    participant Agent

    Note over Client,Agent: 1. Session prompt in progress
    Client->>Agent: session/prompt (id=1, "Analyze file X")
    Agent-->>Client: response to id=1 (messageId)
    Agent->>Client: session/update (user_message)
    Agent->>Client: session/update (state_update: running)

    Note over Client,Agent: 2. Agent makes concurrent permission requests
    Agent->>Client: session/request_permission (id=2, "read sensitive file")
    Agent->>Client: session/request_permission (id=3, "apply workspace changes")

    Note over Client,Agent: 3. Client cancels active session work
    Client->>Agent: session/cancel (sessionId)

    Note over Client,Agent: 4. Agent cancels pending permission requests
    Agent->>Client: $/cancel_request (requestId=2)
    Agent->>Client: $/cancel_request (requestId=3)
    Client->>Agent: response to id=2 (outcome: cancelled)
    Client->>Agent: response to id=3 (outcome: cancelled)

    Note over Client,Agent: 5. Agent stops active work and reports cancellation completion
    Agent->>Client: session/update (state_update: idle, stopReason: "cancelled")
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.