mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-20 19:50:35 +00:00
* fix(mcp): avoid cache resets for unrelated config changes extensions_config.json carries MCP servers, skills and middleware settings, but the MCP cache treated any whole-file signature change as MCP state change, so toggling a skill retired every cached tool and closed all pooled sessions. Record a snapshot of the effective MCP slice (enabled servers' parsed config plus mcpInterceptors) and retire MCP state only when that slice changes. Hand the exact config revision to tool discovery so a change during discovery cannot publish tools built from a superseded revision, and add a non-initializing refresh entry point so disabling the last enabled server still converges the cache and pool on the next tool assembly. * docs(mcp): align cache guide and restore error diagnostics Review feedback on #5750 flagged three non-blocking issues: the MCP module guide still described the pre-semantic-snapshot invalidation contract; the tools.py sanitization covered every exception in the MCP block, discarding diagnostics for failures that cannot carry resolved credentials; and _is_cache_stale()'s docstring presented it as a pure predicate despite adopting a new file signature. Narrow the type-only logging to ExtensionsConfig.from_file() and restore logger.exception() for tool caching, pool cleanup and plugin filtering, with a regression test asserting the message and traceback survive. Document the effective-MCP-slice comparison, the stable config handoff to discovery, refresh_mcp_cache_if_active() and the _initialized_without_config handling in the module guide, and note the signature-adoption side effect in the docstring. * fix(mcp): unify config normalization and verify signatures Review follow-up on #5750. - Normalize mcpInterceptors (bare string == single-element list; missing key == empty list) and the type/transport alias through one shared module used by the tools cache, the interceptor builder and the durable-task config snapshot, so equivalent spellings no longer retire tools/sessions or raise McpTaskConfigurationError. - Treat a resolved-path switch as a signal to re-read the effective MCP slice instead of an invalidation reason: an identical slice adopts the new path and signature and keeps pooled sessions. - Refuse to publish or adopt a signature whose sha256 digest is missing ((mtime, size, None) from a file that could not be read). - Sync the English and Chinese MCP user docs with the effective-config comparison, and document that the Gateway admin reset stays unconditional. * docs(mcp): note the unverifiable-signature fail-closed branch A present config file whose content cannot be hashed is treated as stale, unlike a deleted file which keeps the last-known-good cache. Record that asymmetry in the module guide. * docs(mcp): scope the no-hash rationale to never-initialized processes The cost-free early return in refresh_mcp_cache_if_active() only covers a process that never published an MCP cache. A process that did publish one (including an empty cache from the zero-server or LangGraph Studio lazy-init path) still pays one stat+sha256 per tool assembly. Correct the rationale in the docstring and the matching comment in get_available_tools().
95 lines
3.9 KiB
Python
95 lines
3.9 KiB
Python
"""Shared construction of MCP tool-call interceptors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from deerflow.config.extensions_config import ExtensionsConfig
|
|
from deerflow.mcp.config_normalization import normalize_mcp_interceptor_paths
|
|
from deerflow.mcp.context_headers import build_context_headers_interceptor
|
|
from deerflow.mcp.oauth import build_oauth_tool_interceptor
|
|
from deerflow.mcp.user_scoped_auth import build_user_scoped_auth_interceptor
|
|
from deerflow.reflection import resolve_variable
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def build_mcp_tool_interceptors(
|
|
extensions_config: ExtensionsConfig,
|
|
*,
|
|
oauth_builder: Any = build_oauth_tool_interceptor,
|
|
user_auth_builder: Any = build_user_scoped_auth_interceptor,
|
|
context_headers_builder: Any = build_context_headers_interceptor,
|
|
resolver: Any = resolve_variable,
|
|
target_logger: logging.Logger = logger,
|
|
) -> list[Any]:
|
|
"""Build OAuth, user-scoped auth, context headers, then custom MCP interceptors."""
|
|
interceptors: list[Any] = []
|
|
oauth_interceptor = oauth_builder(extensions_config)
|
|
if oauth_interceptor is not None:
|
|
interceptors.append(oauth_interceptor)
|
|
|
|
# After OAuth so a server declaring both gets the per-user credential:
|
|
# interceptors wrap outermost-first, so the later-registered user-scoped
|
|
# override runs closer to the transport and wins the final header value.
|
|
user_auth_interceptor = user_auth_builder(extensions_config)
|
|
if user_auth_interceptor is not None:
|
|
interceptors.append(user_auth_interceptor)
|
|
|
|
# Last of the built-ins, by the same rule: a credential the caller chose for
|
|
# this one request is more specific than a configured per-user or per-server
|
|
# credential, so it must win the final header value for a server declaring
|
|
# more than one source.
|
|
context_headers_interceptor = context_headers_builder(extensions_config)
|
|
if context_headers_interceptor is not None:
|
|
interceptors.append(context_headers_interceptor)
|
|
|
|
raw_value = (extensions_config.model_extra or {}).get("mcpInterceptors")
|
|
if raw_value is not None and not isinstance(raw_value, (str, list)):
|
|
target_logger.warning(
|
|
"mcpInterceptors must be a list of strings, got %s; skipping",
|
|
type(raw_value).__name__,
|
|
)
|
|
raw_paths = normalize_mcp_interceptor_paths(raw_value)
|
|
|
|
for interceptor_path in raw_paths:
|
|
try:
|
|
builder = resolver(interceptor_path)
|
|
interceptor = builder()
|
|
if callable(interceptor):
|
|
interceptors.append(interceptor)
|
|
target_logger.info("Loaded MCP interceptor: %s", interceptor_path)
|
|
elif interceptor is not None:
|
|
target_logger.warning(
|
|
"Builder %s returned non-callable %s; skipping",
|
|
interceptor_path,
|
|
type(interceptor).__name__,
|
|
)
|
|
except Exception:
|
|
target_logger.warning(
|
|
f"Failed to load MCP interceptor {interceptor_path}",
|
|
exc_info=True,
|
|
)
|
|
return interceptors
|
|
|
|
|
|
def compose_tool_interceptors(interceptors: list[Any], base_handler: Any) -> Any:
|
|
"""Compose interceptors onion-style around ``base_handler``: first = outermost.
|
|
|
|
The later-registered interceptor runs closer to the transport, so its
|
|
header writes win over earlier ones — the property user-scoped auth relies
|
|
on to override an OAuth-injected credential. This is the single wrap
|
|
convention; the session-pool tool path composes through here so tests that
|
|
pin the override property exercise the production composition.
|
|
"""
|
|
handler = base_handler
|
|
for interceptor in reversed(interceptors):
|
|
outer = handler
|
|
|
|
async def wrapped(req: Any, _i: Any = interceptor, _h: Any = outer) -> Any:
|
|
return await _i(req, _h)
|
|
|
|
handler = wrapped
|
|
return handler
|