Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210---title: extensionssidebarTitle: extensions---
# `fastmcp.server.extensions`
FastMCP-native server extension API (SEP-2133).
An MCP extension is an opt-in, capability-negotiated bundle of protocolbehaviour identified by a reverse-DNS string (e.g. `io.modelcontextprotocol/tasks`).Unlike the SDK's `mcp.server.extension.Extension`, a FastMCP `ServerExtension`is bound to its `FastMCP` instance at registration, so its request handlers andits `tools/call` interceptor can reach the component registry, `Context`, andauth scope that the SDK's model withholds.
An extension contributes any subset of four things:
- **A negotiated capability.** `settings()` is spliced into `ServerCapabilities.extensions[identifier]` (see `LowLevelServer.get_capabilities`).- **New request methods.** `methods()` returns `MethodBinding`s, each wired onto the low-level server via `add_request_handler` when the extension is registered.- **A `tools/call` interceptor.** `intercept_tool_call()` is the last gate before a tool body runs — it composes *after* the FastMCP middleware chain and *before* component execution, so it can observe, short-circuit, or pass a call through.- **A lifespan.** `lifespan()` is entered with the server's lifespan and exited on shutdown — the hook the SDK's `Extension` lacks, needed to start backends/workers.
The base class follows the SDK's httpx-style shape: every contribution method hasa default, so a subclass overrides only what it needs.
## Functions
### `read_client_extension_settings` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L302" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonread_client_extension_settings(ctx: ServerRequestContext[Any, Any], identifier: str) -> dict[str, Any] | None```
Read a client's per-request extension opt-in from the request `_meta`.
SEP-2133 extensions negotiate per request: the client repeats its extensioncapabilities in each request's `_meta` under`io.modelcontextprotocol/clientCapabilities` → `extensions` → `identifier`.Returns the declared settings dict (possibly empty) when the extension wasopted in for this request, or `None` when it was not.
### `build_method_handler` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L316" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonbuild_method_handler(binding: MethodBinding) -> ExtensionRequestHandler```
Wrap a `MethodBinding` into a low-level request handler.
The adapter enforces `protocol_versions` gating (rejecting other versions as`METHOD_NOT_FOUND`, since `add_request_handler` registers unconditionally)and binds the FastMCP request context so the handler can use `get_context()`,auth, and other request-scoped dependencies.
### `wrap_tool_call_interceptor` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L345" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonwrap_tool_call_interceptor(extension: ServerExtension, call_next: Callable[[Any], Awaitable[Any]]) -> Callable[[Any], Awaitable[Any]]```
Fold one extension's `intercept_tool_call` around a middleware `call_next`.
The returned wrapper is a FastMCP `CallNext`: it hands the extension thevalidated `tools/call` params, the FastMCP `Context`, and a zero-argcontinuation that runs the rest of the chain and, finally, the tool body.
## Classes
### `MethodBinding` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L126" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
A new request method an extension serves, e.g. `tasks/get`.
`params_type` validates incoming params before `handler` runs; it shouldsubclass `RequestParams` so `_meta` parses uniformly. `protocol_versions`,when set, restricts the method to those wire versions — a request at anyother version is rejected as `METHOD_NOT_FOUND`, mirroring the spec's`(method, version)` boundary. `None` (the default) admits every version.
Extension methods are additive: `method` must not name a spec-definedrequest method (`tools/call`, `completion/complete`, ...). Binding one wouldsilently shadow the server's own handler. Both constraints are enforced atconstruction.
### `ServerExtension` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L160" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Base class for an opt-in FastMCP server extension (SEP-2133).
Subclass, set `identifier`, and override the contribution methods thatapply. Every method has a default, so a minimal extension overrides only`identifier` and one contribution. `identifier` is validated atsubclass-definition time when set as a class attribute, and again atregistration (which covers per-instance identifiers assigned in `__init__`).
Register an instance with `FastMCP.add_extension(...)`, which binds theextension to the server so `self.server`, `intercept_tool_call`, and methodhandlers can reach FastMCP-level constructs.
**Methods:**
#### `clone` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L184" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonclone(self) -> ServerExtension```
Return an unbound instance with the same configuration.
Automatic registration clones bundled extensions so each server ownsits instance. The default deep-copies instance attributes and clearsthe server binding. Override to reconstruct configuration when theextension owns objects that cannot be deep-copied or runtime statethat should be reset.
#### `server` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L216" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonserver(self) -> FastMCP```
The FastMCP server this extension is registered on.
Handlers, interceptors, and lifespan code reach the component registry,`Context`, and auth scope through here. Raises if the extension has notbeen registered with `FastMCP.add_extension()`.
#### `settings` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L232" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonsettings(self) -> dict[str, Any]```
Per-extension settings advertised at `capabilities.extensions[identifier]`.
An empty dict (the default) advertises the extension with no settings.
#### `methods` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L239" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonmethods(self) -> Sequence[MethodBinding]```
New request methods this extension serves (additive).
#### `lifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L243" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonlifespan(self) -> AbstractAsyncContextManager[None]```
A context manager entered with the server's lifespan, exited on shutdown.
Default: a no-op. Override to start and stop resources an extension owns(a task-queue backend and worker, say). Entered once per runtime tree, atthe root — a mounted child defers to the root, as the shared Docket does.
#### `intercept_tool_call` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L252" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonintercept_tool_call(self, params: CallToolRequestParams, context: Context, call_next: ToolCallContinuation) -> ToolCallOutcome```
Wrap `tools/call`. Default: pass through unchanged.
Runs after the FastMCP middleware chain and before the tool body, so itis the last gate before execution. Override to observe the call, toshort-circuit (return a result without awaiting `call_next`), or to passit through (`return await call_next()`). `params` is the validated`tools/call` params; `context` is the FastMCP `Context`, from which thetool being called (`context.fastmcp.get_tool(params.name)`), auth scope,and the server are reachable. Multiple extensions nest with thefirst-registered outermost.
#### `client_settings` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/extensions.py#L271" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonclient_settings(self, ctx: ServerRequestContext[Any, Any]) -> dict[str, Any] | None```
This extension's per-request opt-in settings declared by the client.
Reads the request's `_meta` client-capabilities block. Returns thedeclared settings dict (possibly empty) when the client opted thisextension in for the request, or `None` when it did not. Convenience for`read_client_extension_settings(ctx, self.identifier)`.