Module

middleware

Middleware stack for MCP and CLI call interception.

Classes

MCPCall 4 ▼
Represents an interceptable MCP or CLI call.

Represents an interceptable MCP or CLI call.

Attributes

Name Type Description
method str —
name str —
arguments dict[str, Any] —
metadata dict[str, Any] —
MiddlewareStack 3 ▼
Ordered middleware pipeline for intercepting calls. Middleware signature: ``def mw(ctx, call: MCPC…

Ordered middleware pipeline for intercepting calls.

Middleware signature:def mw(ctx, call: MCPCall, next_fn: NextFn) -> Any

Usage::

stack = MiddlewareStack()

@stack.use
def log_calls(ctx, call, next_fn):
    print(f"Calling {call.name}")
    result = next_fn(call)
    print(f"Done {call.name}")
    return result

result = stack.execute(ctx, call, handler)

Methods

use 1 MiddlewareFn ▼
Register a middleware function. Can be used as a decorator.
def use(self, fn: MiddlewareFn) -> MiddlewareFn
Parameters
Name Type Description
fn —
Returns
MiddlewareFn
execute 3 Any ▼
Execute the middleware chain, ending with the handler.
def execute(self, ctx: Any, call: MCPCall, handler: Callable[..., Any]) -> Any
Parameters
Name Type Description
ctx —
call —
handler —
Returns
Any
Internal Methods 1 ▼
__init__ 0 ▼
def __init__(self) -> None