server.use(mw) or via FastMCPOptions.middleware:
This page is under construction. The outline below sketches what it will cover.
- Hook levels —
setup(server)runs once per server instance (register notification handlers here);onRequest(ctx, next)fires for every request without a more specific hook; per-method hooks (onCallTool,onListTools,onReadResource,onGetPrompt, …) take precedence overonRequest. - Built-ins —
LoggingMiddleware,CachingMiddleware(ttl, keyFn?),RateLimitingMiddleware(limit, windowMs),SizeLimitingMiddleware(maxBytes),ErrorNormalizationMiddleware,CancellationMiddleware. - Error semantics — a thrown
McpErrorfrom middleware propagates as a protocol error (never converted to a{ isError: true }tool result); non-McpErrorfrom tool handlers becomes a tool execution error. - Writing your own — implementing the
Middlewareinterface, ordering, and composing with auth and transforms.