HTTP Request Access in Hyperia Servers

Added in v2.2.11 – seamlessly reach Starlette Request objects from inside tools, resources, prompts, or any helper buried five calls deep.

When a Hyperia server is exposed via Streamable HTTP (or legacy SSE), each MCP request rides an HTTP envelope. Sometimes you need headers, cookies, query params, or the client’s IP to tailor behaviour—rate limiting, per‑tenant routing, feature flags, etc.

Hyperia exposes a dependency helper so you never have to pass Request objects by hand.


1 get_http_request() Helper

from hyperia.server.dependencies import get_http_request   # starlette.req proxy
from starlette.requests import Request

@mcp.tool()
async def whois():
    req: Request = get_http_request()              # 👍 safe anywhere in call‑stack
    return {
        "ua": req.headers.get("user-agent", "?"),
        "ip": req.client.host if req.client else "?",
        "path": req.url.path,
    }

Key Points

  • Works only in an active HTTP context. If you call it from STDIO / in‑memory runs you’ll get RuntimeError("No active HTTP request").

  • Returns the Starlette Request (FastAPI is Starlette under the hood).

  • Zero coupling—keep function signatures clean; no phantom request params exposed to LLMs.


2 Typical Use‑Cases

2.1 Auth Header Introspection

2.2 Tenant Resolution via Custom Header

2.3 Query Param Parsing


3 Advanced Patterns

3.1 Store Request on Context for Down‑Stream Helpers

3.2 Custom Middleware Injection

Need CORS, tracing, or IP‑whitelisting?

Inside any tool you can then get_http_request() and read request.state.trace_id.

3.3 Ensuring Availability for Mounted Servers

When you mount servers under a Starlette/FastAPI parent, the parent’s middleware executes first. get_http_request() still works because the dependency walks the ASGI scope chain.


4 Security & Edge‑Cases

Scenario
Behaviour

Called from STDIO / in‑memory transport

Raises RuntimeError

Behind reverse proxy

You may need X‑Forwarded‑For logic to get client IP; implement Starlette’s TrustedHostMiddleware or similar

Large file uploads

Use await req.body() or req.stream() inside the tool—mind memory limits

HTTP/2 push trailers

Available via req.headers.raw but uncommon


5 Unit‑Testing Helpers


6 Migration Notes (≤ v2.2.10)

Pre‑2.2.11 you had to call ctx.get_http_request() from within a tool. That helper is deprecated; switch to the standalone get_http_request() function for clarity and for use in non‑tool helpers.


7 Recap Checklist

  1. Import once: from hyperia.server.dependencies import get_http_request.

  2. Call it inside HTTP‑backed requests only.

  3. Treat returned object as Starlette Request.

  4. Use for headers, cookies, query‑params, file uploads, client IP, etc.

  5. For STDIO/in‑memory modes, guard with if transport.is_http or let RuntimeError propagate.

Last updated