> ## Documentation Index
> Fetch the complete documentation index at: https://fastmcp-ts.docs.prefect.io/llms.txt
> Use this file to discover all available pages before exploring further.

# CancellationMiddleware

Defined in: [server/middleware.ts:338](https://github.com/PrefectHQ/fastmcp-ts/blob/e4b6038f642b529f601463a576229747afebe9a4/src/server/middleware.ts#L338)

Intercepts `notifications/cancelled` from the client and aborts the matching
in-flight handler via Promise.race + AbortController.

KNOWN GAP (2026-07-28): this only covers legacy (2025-era) and stdio cancellation.
On a modern Streamable HTTP connection, the SDK signals cancellation by closing the
request's response stream rather than sending `notifications/cancelled` — the client
is correctly told the call was cancelled either way, but a handler registered here
keeps running server-side to completion on the modern path, since this middleware
never observes that stream closure. The SDK exposes it per-request as
`ctx.mcpReq.signal` (a `BaseContext` field, present on every era) — unifying this
middleware with that signal (so long-running handlers actually stop executing on
both eras) is tracked as follow-up work, not yet implemented here.

## Implements

* [`Middleware`](/api/server/interfaces/Middleware)

## Constructors

### Constructor

```ts theme={null}
new CancellationMiddleware(): CancellationMiddleware;
```

#### Returns

`CancellationMiddleware`

## Methods

### onRequest()

```ts theme={null}
onRequest(ctx, next): Promise<unknown>;
```

Defined in: [server/middleware.ts:348](https://github.com/PrefectHQ/fastmcp-ts/blob/e4b6038f642b529f601463a576229747afebe9a4/src/server/middleware.ts#L348)

#### Parameters

##### ctx

[`MiddlewareContext`](/api/server/interfaces/MiddlewareContext)

##### next

[`Next`](/api/server/type-aliases/Next)

#### Returns

`Promise`\<`unknown`>

#### Implementation of

[`Middleware`](/api/server/interfaces/Middleware).[`onRequest`](/api/server/interfaces/Middleware#onrequest)

***

### setup()

```ts theme={null}
setup(server): void;
```

Defined in: [server/middleware.ts:341](https://github.com/PrefectHQ/fastmcp-ts/blob/e4b6038f642b529f601463a576229747afebe9a4/src/server/middleware.ts#L341)

Called once per Server instance. Use to register notification handlers or other server-level setup.

#### Parameters

##### server

`Server`

#### Returns

`void`

#### Implementation of

[`Middleware`](/api/server/interfaces/Middleware).[`setup`](/api/server/interfaces/Middleware#setup)
