Skip to content

Middleware

Middleware in wags are built using the amazing FastMCP sdk, which already provides many powerful features, such as hooks to intercept protocol messages e.g., on_call_tool, on_list_tools, on_read_resource, on_get_prompt, on_notification, and the MiddlewareContext provides:

  • context.message - Request/response data
  • context.fastmcp_context - Access to roots, elicitation, sampling
  • context.source - Origin ("client" or "server")
  • context.type - Message type ("request", "notification")

The wags middleware toolkit further provides fine-grained interception hooks and helpers for easy configuration of middleware capabilities. Majority of the features can be configured only by adding decorators and type annotations instead of having to write complex code.

WAGS WagsMiddlewareBase
from wags.middleware.base import WagsMiddlewareBase


class MyWAGSMiddleware(WagsMiddlewareBase):
    def __init__(self, handlers):
        super().__init__(handlers)

    async def handle_on_tool_call(self, context, handler):
        # Custom processing logic here
        return await super().handle_on_tool_call(context, handler)

Next Steps

Understand what features different middlewares provide and how to configure them:

  • TodoList to ensure agents perform complex tasks correctly.
  • Roots to enable client-configured fine-grained access control for MCP servers.
  • Elicitation add human-in-the-loop features to improve UX.