* feat(mcp): map request-scoped secrets to HTTP/SSE headers `user_auth` binds a credential to a configured DeerFlow user, so a caller that picks the credential per request — a multi-tenant gateway, a per-run API key, one shared MCP server fronting several environments — had to register one MCP server entry per credential. Add a declarative `mcpServers.<server>.headers_from_context` block mapping HTTP header names to keys of the run request's `config.context.secrets` carrier. A new built-in interceptor resolves the mapping on every tool call and rewrites those headers, mirroring `user_scoped_auth`. The config file stores names only, never a credential, so the Gateway returns the block unmasked. Registered after OAuth and `user_auth` in the interceptor chain: the later interceptor runs closer to the transport, and the value chosen for this one request is the most specific, so it wins. Fail-closed by default — a mapped key missing from the request raises a `ToolException` naming only that key, because falling back to the server's discovery credential would send one tenant's call under another tenant's authority. `on_missing: "passthrough"` opts out. Durable background tasks are excluded: `McpTaskToolCaller` drives status and cancel polls after the Agent run ends, where no run context exists, so the fail-closed interceptor would deny every poll. Those calls keep using server-level credentials, and a server declaring both `headers_from_context` and `task_toolsets` now logs a warning. Also corrects the custom-interceptor example in docs/MCP_SERVER.md (and the matching claim in skills/AGENTS.md), which read request secrets from `langgraph.config.get_config()["context"]`. That key is `None` inside a tool call — the run context rides the LangGraph runtime, not the RunnableConfig propagated to child runnables — so interceptors written from that example never saw a value. The example now reads `request.runtime`, and tests/test_mcp_context_headers.py pins LangGraph's runtime-injection rule by driving a real langchain-mcp-adapters tool through a real graph with the ambient-runtime fallback disabled. Closes #5005 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(mcp): resolve credential headers case-insensitively, carry them on durable submit Review follow-ups on `headers_from_context`. HTTP field names are case-insensitive, but every dict on the path to the wire is not: `build_server_params` copies the operator's static `headers` spelling verbatim, and langchain-mcp-adapters merges interceptor overrides into the connection with a plain `{**connection_headers, **override_headers}` splat. A static `authorization` and an injected `Authorization` therefore both reached httpx as separate field lines, and a server reading the field with a single-value accessor got the static discovery credential — inverting the documented `headers` < `oauth` < `user_auth` < `headers_from_context` precedence and running a per-request call under the shared credential. Normalizing inside the interceptor cannot fix that on its own: the adapter builds the request with `headers=None`, so an interceptor never sees the connection's static headers and cannot displace them however it spells its own key. A new `mcp/headers.py::apply_header_overrides` therefore drops any key differing only in case and emits the spelling the connection already uses. Applied to `headers_from_context`, `user_auth`, the OAuth interceptor, the OAuth discovery-header write, and the durable-task connection merge, which all carried the same collision. `headers_from_context.headers` now also rejects one header mapped under two spellings at config load, in both the harness model and the Gateway mirror. Durable submit now carries the mapped headers, as docs/MCP_SERVER.md already promised. `McpTaskToolCaller` disabled the interceptor for the whole caller, but that caller serves submit as well as the polls, and submit is awaited inline inside the Agent's tool call — where the run's LangGraph runtime is still the ambient contextvar, so no secret has to be threaded through `TaskSubmitRequest` or reach durable storage. The caller builds one chain and keeps a second view of it without the context-headers interceptor; `call_tool` takes `request_scoped_headers`, set only by `OrdinaryMcpTaskDriver.submit`. Status and cancel keep server-level credentials, so background polls still cannot fail closed, and the startup warning now describes the half it actually covers. `_merge_preserving_secrets` restores masked extras inside `headers_from_context` instead of writing the `***` sentinel back over the stored value, matching the treatment `user_auth` extras and server-level extras already get; extras a PUT omits carry over as well, while the declared mapping still replaces verbatim so a round trip can remove an entry. `extra="allow"` plus name-based sensitivity detection means the usual casualty is a name-valued key such as `tokenHeader`, not only a credential. The existing override test seeded the static header onto `request.headers`, which production never does, so it modelled a merge that really happens one layer down; the new tests drive a real adapter tool through a real connection and assert on the headers the session is opened with, and the durable-submit test runs through a real tool node with no runtime patching. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(mcp): reject case-insensitive duplicate static header names * fix(mcp): preserve omitted headers_from_context fields on partial updates --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
18 KiB
MCP System (packages/harness/deerflow/mcp/)
-
Uses
langchain-mcp-adaptersMultiServerMCPClientfor multi-server management -
Long-running task foundation:
mcp/tasks/defines the protocol-neutralMcpTaskDrivercontract and normalizedTaskSnapshotstates (submitted,working,input_required,completed,failed,cancelled). A driver-suppliedpoll_after_secondsmust be a finite positive number, validated at theTaskSnapshotboundary so every driver is held to the same invariant rather than each one guarding the consumer that turns the interval into atimedelta.persistence/mcp_tasks/owns the durable remote-handle mapping, poll schedule, notification state, lease owner, and separate consecutive poll/delivery error counters;app/mcp_tasks/McpTaskServiceperforms status, cancellation, and notification work outside the Agent/LLM loop. Notification retries keep their idempotency attempt separate from the delivery-failure count, use capped exponential backoff, and stop after five failures; strict existing-thread admission dead-letters a deleted/mismatched target immediately. A status result is applied only when the worker still owns an unexpired lease, so a stale result cannot be written after expiry even before another worker reclaims the row. Poll timestamps and retry schedules are based on the remote call's completion time rather than the scan start. If submission succeeds but persistence fails or the caller is cancelled while persistence is in flight, the service best-effort cancels the remote task and preserves the original error or cancellation if that compensation also fails. The exactuq_mcp_tasks_user_server_remoteconflict is different: an existing durable row already owns the remote handle, so the conflict surfaces without cancelling that tracked task. Unexpected per-task poll failures are isolated from sibling claims and remain recoverable through lease expiry; Gateway shutdown cancels the poller so a hung external status call cannot block process exit.input_requiredand terminal states stop polling and becomenotification_status=pendingfor later Agent/UI delivery. Durable recovery requires a SQL database backend (sqliteorpostgres); the in-memory backend leaves the repository/service unavailable. The runtime is startup-configured bymcp_tasksand disabled by default until a concrete driver is registered; this foundation does not alter ordinary MCP tool behavior on its own. -
Runtime availability boundary: the installed process-local submitter is the source of truth for durable task-management tool exposure.
mcp_tasksis startup-only; changing it on disk does not alter the live toolset until the Gateway restarts. -
Long-running ordinary task driver:
extensions_config.json -> mcpServers.<server>.task_toolsetsbinds exact raw submit/status/cancel names; one raw tool may occupy only one role across that server's groups.mcp/tools.pyhides status/cancel and replaces submit with a wrapper that returns only the local task ID after persistence.ordinary.pyreads only MCPstructuredContent, maps remoterunningtoworking, and treatserror_code=task_not_foundor malformed structured output as permanent failure. A status call withisError=trueis a retryable call failure: the first text content block is retained as a bounded diagnostic, while a permanent remote-task outcome must arrive in a normal result with structuredstatus=failed.task_tool_caller.pyrestores the same(server_name, user_id:thread_id)stdio session scope; HTTP/SSE calls remain ephemeral, applysession_init_timeoutto initialization andtool_call_timeoutto task calls, and support server-level OAuth refresh outside an Agent run.McpTaskServiceexponentially backs off transient status/cancel errors without a maximum attempt count, derives APItracking_degradedfrom the consecutive-error threshold, keepsinput_requiredon a slower poll, and caps finite positive remote poll hints at 24 hours. Task-enabled server runtime/binding configuration andmcpInterceptorsare frozen to the Gateway startup snapshot; hot drift fails clearly before tool discovery can diverge from background calls, while presentation-only fields and non-task servers remain reloadable. Configured task toolsets fail startup when the runtime is disabled or persistence is memory. Users still cannot submit an answer back to aninput_requiredremote task. -
Durable task payload bounds: persisted task errors are capped at 4,000 characters.
input_requiredandresult_artifactmust each serialize as valid JSON within 64 KiB; an invalid or oversized payload becomes a permanent protocol failure rather than being truncated and changing its semantics. Remote task IDs/task names are limited to 255 characters and task-enabled server names to 128, matching the SQL schema; an oversized submitted remote ID is rejected only after the Service has the handle so compensation cancellation still runs. Oversized results retain the existing bounded preview/truncation/artifact behavior. -
Lazy initialization: Tools loaded on first use via
get_cached_mcp_tools() -
Cache invalidation: Detects extensions-config changes by comparing the resolved config path and a
(mtime, size, sha256)content signature against the values recorded at initialization, not a strict mtime>comparison. This catches same-second edits, mtime that stays put or moves backward (git checkout,cp -p/ backup restore,tar/rsync, object-store / network mounts), and a switch to a different config file with an equal-or-older mtime. The signature helper (config/file_signature.py::get_config_signature) is shared withconfig/app_config.py::get_app_config()for the sibling runtime-editable config file, rather than each maintaining its own copy.ExtensionsConfig.resolve_config_path()raisesFileNotFoundErrorfor an explicitconfig_path/DEER_FLOW_EXTENSIONS_CONFIG_PATHthat points at a missing file — an operator-asserted path going missing is a real misconfiguration, so this is intentionally loud for callers that load the config for actual use (e.g.from_file()viaget_mcp_tools()); only the fallback search mode returnsNone. The MCP cache's own path resolution (mcp/cache.py::_resolve_config_path) is narrower: it catches that specificFileNotFoundErrorlocally and treats it the same as "unconfigured", so this staleness check degrades to "not stale" instead of propagating an exception when a previously-valid explicit/env-var config disappears mid-run -
Transports: stdio (command-based), SSE, HTTP
-
Per-server tool-name prefixing:
mcpServers.<server>.tool_name_prefixdefaults totrue, preserving the collision-safe<server_name>_prefix. Servers whose tools already carry a stable namespace may set it tofalse; discovery then callslangchain_mcp_adapters.tools.load_mcp_toolswith that server's flag. Source routing and stdio session-pool wrapping are based on the producing server and transport, never on whether the visible tool name starts with the server prefix. -
OAuth (HTTP/SSE): Supports token endpoint flows (
client_credentials,refresh_token) with automatic token refresh + Authorization header injection -
Per-user credentials (HTTP/SSE):
mcpServers.<server>.user_authmaps DeerFlow user ids to credential header values ($ENV_VARreferences supported).mcp/user_scoped_auth.py::build_user_scoped_auth_interceptorrewrites the configured header on every tool call from the authenticated runtime user (registered after OAuth inmcp/interceptors.py, so its per-user value wins the header for servers declaring both). Fail-closed: an unmapped user or an empty resolved credential raises aToolExceptionunlesson_missing: "passthrough"is set. The server's staticheadersserve startup tool discovery only. Gateway GET masksuser_auth.usersvalues; PUT round-trips masked values by preserving stored credentials. -
Per-request credentials (HTTP/SSE):
mcpServers.<server>.headers_from_contextmaps HTTP header names to keys of the run request'sconfig.context.secretscarrier, for credentials the caller chooses per request (multi-tenant gateways, per-run API keys) rather than per configured user.mcp/context_headers.py::build_context_headers_interceptorresolves the mapping on every tool call and rewrites those headers. Registered afteruser_authinmcp/interceptors.py, so for a server declaring several sources the per-request value wins the final header: precedence is staticheaders<oauth<user_auth<headers_from_context. Fail-closed: a mapped key absent from the request secrets (or resolved empty) raises aToolExceptionnaming only the missing key, unlesson_missing: "passthrough"is set — a silent fallback would send one tenant's call under the discovery credential's authority.sse/httponly; a stdio server warns and is skipped, as withuser_auth. The block stores names, never a credential, so the Gateway returns it unmasked and aPUTreplaces the declared mapping verbatim; onlyextra="allow"keys inside it get sensitive-key masking, and those are restored from the stored block on a round-trip like every other masked extra. Durabletask_toolsetscalls split: submit is awaited inside the Agent run and carries the mapped headers (McpTaskToolCaller.call_tool(request_scoped_headers=True), set only byOrdinaryMcpTaskDriver.submit), while status and cancel run after that run ended and keep the server-level credentials — soon_missing: "deny"covers submit but not those polls, which is what the startup warning is about. -
Header names are case-insensitive (
mcp/headers.py): every credential interceptor writes throughapply_header_overrides, which drops a key differing only in case and emits the spelling the connection already uses. Without it a staticauthorizationand an injectedAuthorizationboth reach httpx — the adapter merges connection and override headers with a plain{**static, **override}splat — and a server reading the field with a single-value accessor gets the static entry, silently inverting the precedence above.headers_from_context.headersalso rejects two spellings of one header at config load. -
Reading the run context from an interceptor: use
request.runtime(LangGraph's tool node injects aToolRuntimeinto any tool parameter namedruntime, which covers both the pooled stdio wrapper andlangchain-mcp-adapters' own HTTP/SSE tool), falling back to ambientlanggraph.runtime.get_runtime(). Do not uselanggraph.config.get_config()["context"]: the run context rides the runtime, not theRunnableConfigpropagated to child runnables, so that key isNoneinside a tool call.tests/test_mcp_context_headers.py::test_adapter_tool_receives_the_runtime_langgraph_injectspins the injection rule against an upstream rename by disabling the ambient fallback and driving a real adapter tool through a real graph. -
Routing hints:
extensions_config.json -> mcpServers.<server>.routingandtools.<original_tool_name>.routingare soft preference metadata. The effective routing is resolved whilemcp/tools.py::get_mcp_tools()still has bothsource_nameand the original MCP tool name, then stored ontool.metadataunderdeerflow_mcp_routing. Prompt rendering usestools/builtins/tool_search.py::get_mcp_routing_hints_prompt_section, which referencestool_searchwhen a hinted MCP tool is currently deferred; do not add a parallel routing middleware for PR1-style preference hints. -
Stdio file outputs: Persistent stdio sessions are scoped by
user_id:thread_id. For stdio transports only, DeerFlow pins the subprocess defaultcwdto the thread workspace andTMPDIR/TMP/TEMPtoworkspace/.mcp/tmp/, unless the operator explicitly configuredcwdor temp env values..mcpis a DeerFlow-owned internal namespace: its temporary/debug files remain addressable when returned by a tool but are excluded from run workspace-change summaries — by directory name at any depth, consistent with the other reserved names inEXCLUDED_DIR_NAMES(.git,node_modules, …) and robust if a server ever creates a relative.mcpfrom a different cwd. Both launch paths pin it at the workspace root today. SSE/HTTP transports skip this filesystem prep entirely. -
Stdio disconnect recovery: An ordinary Agent tool call that receives the MCP SDK's explicit
Connection closederror or an AnyIO closed-stream error evicts only that(server_name, user_id:thread_id)session when the registered entry is still the sameClientSessionthat failed. A late error from an old concurrent call cannot evict its replacement or a new in-flight creation. The failing call still surfaces its original error and is never replayed automatically; a later Agent retry creates a fresh subprocess/session. Protocol timeouts, normalisError=truetool results, and interceptor failures do not evict a healthy stateful session. -
Stdio path translation: MCP-returned local file references are not copied. If a
ResourceLinkor conservative free-text path resolves to an existing file inside the thread's mounted user-data tree, it is translated deterministically to/mnt/user-data/...; paths outside that tree remain unchanged. -
Runtime updates: Gateway API saves to extensions_config.json; the Gateway-embedded runtime detects changes via the resolved-path + content-signature check above, so multi-worker / stale-mtime deployments still pick up an added/removed MCP server without a restart (
PUT /api/mcp/configkeeps whole-payload validation, whilePATCH /api/mcp/configchanges only one server'senabledfield, normalizes the sametype/MCP-spectransportalias as the runtime config model, and validates the target only when enabling it; either endpoint's reset clears the cache only in its own worker). MCP, skill, and embedded-client writers hold the process-localextensions_config_write_lockplus the sidecar advisoryextensions_config_file_lockfor the complete read-modify-write/reload cycle, then shareatomic_write_extensions_config(), which writes and fsyncs a same-directory temporary file beforeos.replace()and preserves an existing file's mode and symlink target; failed serialization or replacement leaves the prior config intact and cleans up the temporary file. -
Stdio launch policy at the HTTP boundary (
routers/mcp.py::_validate_mcp_update_request, shared byPUTand the enable branch ofPATCH): a config file may express anything, but the API is untrusted input, so an API-registered stdio server must (a) name a bare executable from the allowlist —_DEFAULT_MCP_STDIO_COMMAND_ALLOWLIST={npx, uvx}, extended byDEER_FLOW_MCP_STDIO_COMMAND_ALLOWLIST, with path separators, whitespace, and shell metacharacters rejected incommand; (b) carry noargsflag in_ARBITRARY_EXEC_ARGS; and (c) set noenvname in_CODE_INJECTING_ENV_VARS. Checks (b) and (c) exist because the command check alone names a binary without constraining what that binary runs. Theenvdenylist applies to every allowlisted command, and both denylists match--flag=valueas well as--flag value. Theargsdenylist's scope depends on the command, because where a launcher stops parsing its own flags is what decides whether a token is an exec flag at all:- For a package launcher in
_PACKAGE_LAUNCHERS({npx, uvx}) only the launcher's own option region is screened.npx/uvxstop parsing their flags at the package name and hand every later token to the spawned server's argv, where-cis routinely "config" and-e"env" — screening those rejected ordinary third-party servers while covering nothing. A bare--ends the region too: only the first token after it is the package name. Finding that boundary needs each launcher's option arity, since a value is not a positional —npx -p <pkg> -c '<command>'runs the command (-pisnpm exec's--package, so<pkg>is its value and npm keeps parsing), so ending the region at the first non-flag token would walk straight past it._NPX_BOOLEAN_ARGSis generated from@npmcli/config's definitions (npm 10.9.4) minus the-pexec override;_UVX_VALUE_ARGScomes fromuvx --help(uv 0.11.1). Regenerate these against a newer launcher rather than hand-editing. The unknown-option default is deliberately opposite per launcher, following the exec set rather than symmetry: npx owns real exec flags (-c/--call), so an unknown option consumes a value and keeps the region open (npm errors on options it does not define, so this cannot reject a working invocation); uvx owns no string-eval flag at all, so its screen is a tripwire, an unknown option consumes nothing, and uv's large boolean surface cannot over-block. uvx's exec set also drops the short spellings, because-cis uv's--constraintsand-pits--python. - Every other command is screened whole, with two extra rules, because it is an interpreter rather than a package runner:
-pcounts as an exec flag there (node's--print), and single-dash short-option clusters are decomposed letter by letter sonode -pecannot pass a check that only splits on=.
Verdicts are pinned against the real launchers: for npx, every argument vector the validator rejects is one
npxactually executes, and every vector it allows is onenpxpasses through to the server.envscreening covers names that execute code unconditionally at process startup, e.g.PYTHONPATH/PYTHONHOME, which run a caller-controlledsitecustomize.pyat interpreter startup under plainuvx. Caller-controlled search paths are a weaker, conditional class and are an accepted residual:LD_LIBRARY_PATH/DYLD_LIBRARY_PATH(conditional on the process loading a shadowable library, and legitimately set by native-dependency servers) andNODE_PATH(searched after the localnode_moduleschain, so it cannot shadow an installed dependency, and ignored entirely by ESMimport— it can only supply a CJS module that would otherwise fail to resolve). Do not move a search path into the set: it would make the "unconditional" rule untrue, which is how a defense-in-depth list starts being mistaken for a boundary. Remote transports skip all three — they spawn nothing. This is defense in depth, not a trust boundary.npx/uvxexist to fetch and execute remote packages, so an admin can still point one at a package they published; the boundary is admin authentication plus network reachability. Do not add a check here on the assumption that it makes MCP registration safe for untrusted admins — it does not, and the fix for that is not a bigger denylist. - For a package launcher in