Skip to content

datarobot_genai.drmcp.core.clients

clients

RequestHeadersMiddleware

Bases: BaseHTTPMiddleware

ASGI middleware that sets request headers in context for custom REST routes only.

Skips the streamable-http MCP path so only routes in routes.py get headers in context; drtools token resolution can then use them in those handlers.

Source code in datarobot_genai/drmcp/core/clients.py
class RequestHeadersMiddleware(BaseHTTPMiddleware):
    """
    ASGI middleware that sets request headers in context for custom REST routes only.

    Skips the streamable-http MCP path so only routes in routes.py get headers in
    context; drtools token resolution can then use them in those handlers.
    """

    async def dispatch(self, request: Request, call_next: Any) -> Response:
        mcp_path = prefix_mount_path(MCP_PATH_ENDPOINT).rstrip("/") or "/"
        path = request.url.path
        if path == mcp_path or path.startswith(mcp_path + "/"):
            return await call_next(request)
        headers = {k.lower(): v for k, v in request.headers.items()}
        set_request_headers(headers)
        return await call_next(request)