Commit 5b59b6d
fix: mcp component dyanamic tool (#12779)
* fix(mcp): support real-time tool onboarding via per-request auth headers
Problem
-------
When Langflow runs an agent that has an MCPTools component attached and an
API request supplies authentication headers via tweaks, the MCP server can
legitimately return an *expanded* tool list that is broader than the
unauthenticated base set (the server filters its listing by the caller's
identity). Today those extra tools never reach the agent at runtime: the
component always binds the first preloaded tool list and ignores the new
auth context for the lifetime of the flow.
Root cause
----------
Several caching / binding layers combine to prevent per-request tool
refresh:
1. Flow JSON persists ``component_as_tool`` with ``cache: true`` and
Langflow's ``Output`` memoization reuses the first ``to_toolkit()``
result for all subsequent requests regardless of headers.
2. The shared MCP server cache was keyed by server name only, so
requests with different auth contexts collided on the same cache slot.
3. In tool-mode, ``_get_tools()`` returned ``[]`` when
``_not_load_actions`` was true, starving the agent of MCP tools.
4. Concurrent ``update_tool_list`` calls raced on the same Streamable
HTTP client session, producing intermittent HTTP 404 /
"Session terminated" errors from the MCP SDK.
What changed
------------
``src/lfx/src/lfx/components/models_and_agents/mcp_component.py``:
- FIX 1 - Output cache bypass: ``_build_tool_output`` declares the
Toolset output with ``cache=False``, and ``map_outputs`` overrides any
``cache: true`` value persisted in saved flow JSON. Together these
guarantee every run resolves a fresh ``to_toolkit()`` call regardless
of whether the flow was loaded from disk. Independent of the
user-facing ``use_cache`` / "Use Cached Server" toggle.
- FIX 2 - Header-aware cache key: ``_mcp_servers_cache_key`` now hashes
the component headers into the shared server-cache key so requests
with different auth contexts land in distinct cache slots instead of
masking each other. The shared cache is bounded at
``SHARED_SERVERS_CACHE_MAX_ENTRIES`` = 64 with FIFO eviction so a
tenant rotating session tokens does not grow the map without limit.
- FIX 3 - ``_get_tools`` always fetches: removed the ``_not_load_actions``
short-circuit that returned ``[]`` in tool-mode, so the agent always
binds the current (possibly expanded) tool list. ``_not_load_actions``
still gates only the UI build_config dropdown.
- FIX 4 - Serialized ``update_tool_list``: an ``asyncio.Lock`` prevents
concurrent refreshes from racing on the same Streamable HTTP client
session, eliminating the intermittent HTTP 404 / "Session terminated"
errors the MCP SDK raised when session DELETE and POST overlapped.
- FIX 5 - TTL tool cache: ``_get_tools`` keeps a per-instance,
header-hash-keyed TTL cache (``TOOL_TTL_SECS`` = 30s, disable with 0)
bounded at ``TOOL_TTL_MAX_ENTRIES`` = 32 with FIFO eviction and
stale-on-read drop. Parallel agent steps that share the same auth
reuse the tool list instead of each paying for a fresh MCP round-trip.
This is deliberately distinct from ``use_cache`` / "Use Cached Server"
which controls the cross-request shared cache. The dict is initialized
in ``__init__`` so every instance gets its own cache.
Impact on users
---------------
- Backward compatible. Unauthenticated / non-tweaked flows behave
identically: the TTL cache is per-instance and scoped to a single
(server, header-hash) pair, and the base-class ``to_toolkit`` chain is
unchanged (no override of filtering via ``tools_metadata`` /
``enabled_tools``).
- New behaviour only triggers when tweaks/UI headers include an auth
context; the header-hash cache key then isolates per-caller tool lists
and the agent binds the correctly-filtered expanded set.
- No changes to ``lfx/services/settings/base.py``: the global
``mcp_server_timeout`` default stays at its existing value.
Test plan
---------
- ``python -m py_compile`` passes on the edited module.
- ``python -m ruff check`` reports no new violations introduced by this
change.
- Local verification against an MCP server that returns different tool
lists per caller identity:
* Unauthenticated request returns the base tools as before.
* Authenticated request (auth headers via tweaks) returns the
expanded tool set on the first call and on subsequent calls with
the same auth - TTL cache hits log
``MCP _get_tools: TTL cache hit``.
* Switching to a different auth context on the same flow returns the
correct per-caller tool list (header-hash cache key isolates the
two contexts).
* Parallel agent runs no longer surface HTTP 404 / "Session
terminated" from the MCP SDK.
* Disabling a tool via the UI (``tools_metadata``) correctly hides it
from the bound agent - base-class filtering is preserved.
* [autofix.ci] apply automated fixes
* [autofix.ci] apply automated fixes (attempt 2/3)
* [autofix.ci] apply automated fixes (attempt 3/3)
* test(mcp): cover dynamic-tool onboarding fixes
Add unit coverage for the fixes introduced in this PR so regressions in the
cache-key, TTL cache, concurrency lock, shared-cache eviction, and Toolset
Output.cache=False behaviour are caught by CI instead of by users:
- Header normalization across list / dict / None / malformed shapes
- `_mcp_servers_cache_key` determinism + header-hash scoping across auth
contexts (different headers → different keys; order-independent)
- Per-instance `_ttl_tool_cache` isolation (no cross-instance leakage)
- `_get_tools` TTL cache: hit skips `update_tool_list`, expired entries are
refetched, FIFO-eviction caps growth at `TOOL_TTL_MAX_ENTRIES`, and TTL=0
disables the cache
- `_update_tool_list_lock` serialises concurrent calls (peak concurrency 1)
- Shared `servers` cross-request cache evicts oldest entry when the map
reaches `SHARED_SERVERS_CACHE_MAX_ENTRIES`
- `_build_tool_output` declares `cache=False`; `map_outputs` overrides
persisted `cache=True` from saved flow JSON
* [autofix.ci] apply automated fixes
---------
Co-authored-by: Mansura <mansura.nw@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Hare <ericrhare@gmail.com>1 parent 358fca9 commit 5b59b6d
4 files changed
Lines changed: 662 additions & 156 deletions
File tree
- src
- backend
- base/langflow/initial_setup/starter_projects
- tests/unit/components/models_and_agents
- lfx/src/lfx
- _assets
- components/models_and_agents
Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 319 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
Large diffs are not rendered by default.
0 commit comments