Skip to content

Fix: Implement AbortSignal propagation for request cancellation#20

Open
onne wants to merge 1 commit into
rawveg:mainfrom
onne:fix/request-cancellation
Open

Fix: Implement AbortSignal propagation for request cancellation#20
onne wants to merge 1 commit into
rawveg:mainfrom
onne:fix/request-cancellation

Conversation

@onne
Copy link
Copy Markdown

@onne onne commented May 19, 2026

Description

This PR implements request cancellation, allowing clients to cleanly abort long-running LLM generation tasks via the MCP AbortSignal.

Why this approach?

  1. Scoped Client Injection: We create a lightweight, per-request scoped Ollama client. We override its internal fetch implementation to inject the extra.signal provided by the MCP server. This natively cancels the underlying HTTP request the moment the client aborts.
  2. Safe Signal Merging: Instead of blindly overwriting the fetch options' signal, we use AbortSignal.any() to merge the MCP cancellation signal with any internal signals the Ollama SDK might be using (e.g., for timeouts or keep-alives). This prevents us from unintentionally breaking SDK internals.
  3. Early Short-Circuit: We check extra.signal.aborted at the very beginning of the handler. If the request was cancelled while waiting in the queue, we immediately throw an AbortError, saving the overhead of tool discovery and API calls entirely.

Resolves #16

- Pass AbortSignal from MCP extra payload down into scoped Ollama fetch calls
- Gracefully handle pre-aborted requests and map AbortError
- Add tests to ensure cancellation is properly propagated to the SDK layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Implement request cancellation (AbortSignal) to prevent runaway processes

1 participant