Choose MCP middleware or HTTP middleware
Use MCP middleware when the logic depends on the parsed MCP operation. Itsctx.request also exposes the originating HTTP request, so operation-specific authorization can inspect headers without a custom wrapper. Use HTTP middleware when logic belongs before MCP parsing.
MCP middleware patterns use the
mcp: prefix. Register Hono middleware directly with server.use(); both custom routes and the MCP endpoint pass through it.
Wrap MCP tool calls
Register MCP middleware withserver.use("mcp:<pattern>", handler). The handler receives a context object and a next() function.
next() to continue to the next middleware or the final operation handler. Its result and the middleware return are typed for the selected MCP method. A replacement must therefore remain valid for that method.
Match the right operation
The pattern aftermcp: controls which MCP operations run through the middleware.
Use the narrowest pattern that covers the behavior. A narrow pattern keeps middleware easier to reason about and avoids accidental changes to unrelated operations.
Exact patterns may transform their method-specific result. The global
mcp:* pattern is a pass-through wrapper: use it for authentication, rate limiting, logging, timing, or shared state. It must call next() and cannot inspect or replace the downstream result. Register an exact pattern for result transformations.
MCP middleware currently wraps tool calls, prompt gets, resource reads, and tool/resource/prompt list operations. It does not wrap protocol setup, completion, logging level changes, resource subscribe/unsubscribe requests, or every MCP method.
Add an OAuth scope guard
When OAuth is configured, MCP middleware can read verified auth information fromctx.auth. Use this for operation-level authorization.
ctx.auth.
Rate-limit expensive operations
Use middleware when the limit applies to a class of operations rather than one tool.Map.
Filter discovery results
Middleware can also wrap list operations. Use this when different clients or users should see different capabilities.Order middleware deliberately
Middleware runs in registration order. The first middleware you register is the outermost wrapper.Use HTTP middleware for request-level behavior
Register Hono middleware directly on the server when behavior belongs at the HTTP layer.ctx.method, ctx.params, ctx.auth, or MCP session information. Inside an MCP callback, the same Hono context is available: read middleware variables with ctx.get() and the raw Web request with ctx.request.raw.
Test middleware locally
Run the server and verify both the allowed and rejected paths.http://localhost:3000/mcp/inspector to call tools, list resources, and request prompts. Check server logs for middleware output and verify that rejected operations return the expected error.
Next steps
Middleware API reference
Look up middleware context fields, pattern matching, adapter behavior, and
proxy options.
Authentication
Configure OAuth before relying on
ctx.auth.Tools
Design the tool callbacks that middleware wraps.
Middleware example
See a runnable server with logging, scope guard, rate limiting, and
filtering.