feat(gateway): forward request.state metadata into run config for tool interceptors#3135
Open
IECspace wants to merge 3 commits into
Open
feat(gateway): forward request.state metadata into run config for tool interceptors#3135IECspace wants to merge 3 commits into
IECspace wants to merge 3 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
httpMiddlewaresextension point inextensions_config.jsonfor config-driven HTTP middleware registration (mirrors the existingmcpInterceptorspattern)request.state.run_metadata→config["metadata"]forwarding so plugin middlewares can inject per-request context that MCP tool interceptors consume viaget_config()["metadata"]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.pyandservices.pydirectly, 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 fromextensions_config.jsonviaresolve_variable()with graceful error handlingbackend/app/gateway/services.py—inject_run_metadata(): forwardsrequest.state.run_metadataintoconfig["metadata"]during run creationextensions_config.example.json— AddedhttpMiddlewaresexample entrybackend/tests/test_http_middlewares.py— 10 unit tests for middleware loadingbackend/tests/test_run_metadata_forwarding.py— 7 unit tests for metadata forwardingREADME.mdandCLAUDE.mdTest Plan
pytest tests/test_http_middlewares.py tests/test_run_metadata_forwarding.py)httpMiddlewaresentry, verify middleware executes and metadata reaches tool interceptors