Skip to content

feat(gateway): forward request.state metadata into run config for tool interceptors#3135

Open
IECspace wants to merge 3 commits into
bytedance:mainfrom
IECspace:feat/forward-request-state-metadata
Open

feat(gateway): forward request.state metadata into run config for tool interceptors#3135
IECspace wants to merge 3 commits into
bytedance:mainfrom
IECspace:feat/forward-request-state-metadata

Conversation

@IECspace
Copy link
Copy Markdown
Contributor

@IECspace IECspace commented May 21, 2026

Summary

  • Add httpMiddlewares extension point in extensions_config.json for config-driven HTTP middleware registration (mirrors the existing mcpInterceptors pattern)
  • Add automatic request.state.run_metadataconfig["metadata"] forwarding so plugin middlewares can inject per-request context that MCP tool interceptors consume via get_config()["metadata"]
  • These two features together allow downstream integrators to inject auth tokens, project identifiers, or other per-session context into the tool execution pipeline without modifying Gateway source

Motivation

Downstream deployments often need to pass per-request metadata (e.g. authentication tokens, tenant identifiers) through to MCP tool interceptors. Currently this requires patching auth_middleware.py and services.py directly, which creates upgrade conflicts on every upstream sync.

This PR provides a clean extension contract: middlewares extract context from the request, stamp it on request.state.run_metadata, and the Gateway automatically carries it into the run config.

Changes

  • backend/app/gateway/app.py_load_http_middlewares(): loads middleware classes from extensions_config.json via resolve_variable() with graceful error handling
  • backend/app/gateway/services.pyinject_run_metadata(): forwards request.state.run_metadata into config["metadata"] during run creation
  • extensions_config.example.json — Added httpMiddlewares example entry
  • backend/tests/test_http_middlewares.py — 10 unit tests for middleware loading
  • backend/tests/test_run_metadata_forwarding.py — 7 unit tests for metadata forwarding
  • Documentation updates in README.md and CLAUDE.md

Test Plan

  • All 17 new tests pass (pytest tests/test_http_middlewares.py tests/test_run_metadata_forwarding.py)
  • Full test suite passes (3527 passed, 4 skipped live tests unrelated to this change)
  • Integration test: configure httpMiddlewares entry, verify middleware executes and metadata reaches tool interceptors

IECspace and others added 3 commits May 21, 2026 16:52
…l interceptors

Add two complementary extension points that allow downstream integrators
to inject per-request metadata into the agent run config without
modifying Gateway source code:

1. **httpMiddlewares** in extensions_config.json — config-driven HTTP
   middleware registration (mirrors the existing mcpInterceptors pattern).
   Each entry is a `module:builder` path resolved at startup via
   resolve_variable(). The builder returns a Starlette middleware class.

2. **request.state.run_metadata forwarding** — any HTTP middleware that
   stamps `request.state.run_metadata` (a dict) will have those values
   automatically merged into `config["metadata"]` when a run starts.
   MCP tool interceptors can then read them via `get_config()["metadata"]`
   at execution time.

Together these features decouple per-request context injection (e.g.
auth tokens, project identifiers) from DeerFlow's run lifecycle layer.
Middlewares handle extraction, tools consume the result.

Includes:
- _load_http_middlewares() in app.py with graceful error handling
- inject_run_metadata() in services.py
- 17 unit tests covering both features
- Updated extensions_config.example.json with httpMiddlewares example
- Documentation updates in README.md and CLAUDE.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant