Protocol-level middleware: hooks at MCP layer with built-in implementations #3
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally filed by @coilysiren on 2026-05-13T14:23:42Z - https://github.com/coilysiren/cli-mcp/issues/40
FastMCP ships a middleware system with a hook hierarchy distinct from any argv-level wrapping:
on_messageon_request,on_notificationon_call_tool,on_read_resource,on_get_prompt,on_list_tools,on_list_resources,on_list_prompts,on_initializeEach receives a
MiddlewareContextand acall_nextcontinuation, bidirectional pipeline shape. Built-in middlewares include:LoggingMiddleware,StructuredLoggingMiddleware,TimingMiddleware,ResponseCachingMiddleware,RateLimitingMiddleware,SlidingWindowRateLimitingMiddleware,ErrorHandlingMiddleware,RetryMiddleware,PingMiddleware,ResponseLimitingMiddleware.cli-mcp today has no protocol-layer middleware. cli-guard covers argv-level validation and audit, but several concerns are MCP-protocol-shaped and don't belong in cli-guard:
ResponseLimitingMiddleware) - prevent a tool dumping 50MB into an agent context. Critical, not currently solvable.on_list_tools).Proposal: add a middleware surface to
Options, with the hook set narrowed to what cli-mcp actually has (tools-only for v1, no resources/prompts):Ship the high-value built-ins:
ResponseLimitMiddleware(maxBytes int)TimingMiddleware()RateLimitMiddleware(perTool int, window time.Duration)ResponseCacheMiddleware(ttl)if there's demandThe cli-guard relationship stays clean: guard wraps the Action (argv truth), middleware wraps the MCP call (protocol truth). Compose both.