Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(mcp): 辅助启动脚本退回单文件指纹,入口才做目录树扫描(!9045 评审 P2)

huxiaokaif2b6afd3f807
diff --git a/jiuwenswarm/common/mcp_config.py b/jiuwenswarm/common/mcp_config.py
index 89ceccaf98..f5fe85660a 100644
--- a/jiuwenswarm/common/mcp_config.py
+++ b/jiuwenswarm/common/mcp_config.py
@@ -814,6 +814,11 @@ def _office_claw_mcp_schema_cache_enabled() -> bool:
_OFFICE_CLAW_MCP_BUNDLE_MAX_FILES = 256
+# Node 启动参数中“取文件/模块值”的 flag:其值为启动辅助脚本(--require
+# polyfill 等),只做单文件指纹;第一个位置参数才是 MCP 入口,做目录树扫描。
+_NODE_FILE_VALUED_FLAGS = frozenset(
+ {"--require", "-r", "--import", "--experimental-loader", "--loader"}
+)
def _fingerprint_bundle_tree(root: Path) -> list[dict[str, Any]] | None:
@@ -840,6 +845,12 @@ def _fingerprint_bundle_tree(root: Path) -> list[dict[str, Any]] | None:
if child.name.endswith(".map"):
continue
if len(entries) >= _OFFICE_CLAW_MCP_BUNDLE_MAX_FILES:
+ logger.warning(
+ "OfficeClaw MCP bundle tree exceeded %d files under %s; "
+ "bypassing the schema cache for this config",
+ _OFFICE_CLAW_MCP_BUNDLE_MAX_FILES,
+ root,
+ )
return None
try:
stat = child.stat()
@@ -867,11 +878,15 @@ def _office_claw_mcp_build_fingerprint(
either miss the real bundle or stat the wrong file, so a bundle rebuild
would not change the fingerprint and the cache would serve a stale schema.
- The interpreter (``command``) is fingerprinted as a single file. Every
- script entry in ``args`` additionally fingerprints its owning directory
- tree: the entry file is typically a thin shell that imports the toolset
- modules next to it, so a rebuild that only touches those imports must
- still rotate the key. ``node_modules`` and ``*.map`` files are skipped.
+ The interpreter (``command``) is fingerprinted as a single file. The
+ first positional script arg (the Node entry) additionally fingerprints
+ its owning directory tree: the entry file is typically a thin shell that
+ imports the toolset modules next to it, so a rebuild that only touches
+ those imports must still rotate the key. ``node_modules`` and ``*.map``
+ files are skipped. File-valued Node flags (``--require``/``--import``
+ startup polyfills) are single-file fingerprinted instead — their owning
+ directory (e.g. a full-source ``scripts/`` checkout with hundreds of
+ files) must not trip the bundle cap and silently disable the cache.
Returns ``None`` when a bundle tree exceeds ``_OFFICE_CLAW_MCP_BUNDLE_MAX_FILES``
files — the caller then bypasses the cache instead of trusting a
@@ -922,15 +937,45 @@ def _office_claw_mcp_build_fingerprint(
if resolved is not None and resolved.is_file():
_stat_once(resolved)
- for value in params.get("args") or []:
- raw = str(value or "").strip()
+ args = [str(value or "").strip() for value in params.get("args") or []]
+ entry_scanned = False
+ index = 0
+ while index < len(args):
+ raw = args[index]
+ index += 1
if not raw:
continue
+ if raw.startswith("-"):
+ flag, _, inline_value = raw.partition("=")
+ value_raw = inline_value or None
+ if (
+ value_raw is None
+ and flag in _NODE_FILE_VALUED_FLAGS
+ and index < len(args)
+ ):
+ value_raw = args[index]
+ index += 1
+ if value_raw is None:
+ continue
+ resolved = _resolve(value_raw)
+ if resolved is not None and resolved.is_file():
+ # --require/--import 启动辅助脚本:单文件指纹(同 #5347 基线)。
+ # 其所在目录(如完整源码的 scripts/,数百个受版本管理文件)
+ # 不做树扫描,否则会撞 _OFFICE_CLAW_MCP_BUNDLE_MAX_FILES 把
+ # 整个缓存废掉(OpenHarmony 启动形态,见 !9045 评审复现)。
+ _stat_once(resolved)
+ continue
resolved = _resolve(raw)
if resolved is None or not resolved.is_file():
continue
+ if entry_scanned:
+ # 入口之后的位置参数是传给脚本的参数,单文件指纹
+ _stat_once(resolved)
+ continue
+ entry_scanned = True
parent_key = _normalized_path(str(resolved.parent))
if parent_key in expanded_dirs:
+ _stat_once(resolved)
continue
expanded_dirs.add(parent_key)
tree = _fingerprint_bundle_tree(resolved.parent)
diff --git a/tests/unit_tests/agentserver/test_mcp_schema_cache.py b/tests/unit_tests/agentserver/test_mcp_schema_cache.py
index 805123df57..1079fbd817 100644
--- a/tests/unit_tests/agentserver/test_mcp_schema_cache.py
+++ b/tests/unit_tests/agentserver/test_mcp_schema_cache.py
@@ -362,3 +362,69 @@ async def test_capacity_bound_clears_orphaned_entries(
# the cleared entries are gone: a repeat of the first variant re-discovers
await mcp_config.list_office_claw_mcp_tools(variants[0])
assert calls == 4, "evicted entry must re-discover"
+
+
+async def test_require_import_aux_scripts_do_not_disable_cache(
+ tmp_path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """OpenHarmony-style --require/--import aux args must not disable the cache.
+
+ Regression for the review on !9045: with the full-source checkout the
+ relay prepends ``--require scripts/ohos-fetch-polyfill.cjs`` and
+ ``--import scripts/ohos-undici-register.mjs`` to the bundle entry, and
+ ``scripts/`` holds far more files than the bundle cap. Tree-scanning an
+ aux flag's owning directory tripped the cap and forced uncached
+ discovery on every request; aux scripts are single-file fingerprinted
+ instead (matching the #5347 baseline), so only the entry's directory
+ gets the tree scan.
+ """
+ scripts = tmp_path / "scripts"
+ scripts.mkdir()
+ polyfill = scripts / "ohos-fetch-polyfill.cjs"
+ polyfill.write_text("polyfill v1")
+ register = scripts / "ohos-undici-register.mjs"
+ register.write_text("register v1")
+ # push scripts/ far past the 256-file bundle cap
+ for i in range(300):
+ (scripts / f"tool-{i:03d}.cjs").write_text("x")
+
+ dist = tmp_path / "dist"
+ dist.mkdir()
+ entry = dist / "index.js"
+ entry.write_text("shell v1")
+ (dist / "server-toolsets.js").write_text("toolset v1")
+
+ # cover both node flag forms: "--require value" and "--import=value"
+ params = {
+ "command": "node",
+ "args": ["--require", str(polyfill), f"--import={register}", str(entry)],
+ "cwd": str(tmp_path),
+ "env": {},
+ }
+
+ calls = 0
+
+ async def _discover(p):
+ nonlocal calls
+ calls += 1
+ return [_make_tool("office_claw_post_message")]
+
+ monkeypatch.setattr(mcp_config, "_list_office_claw_mcp_tools_uncached", _discover)
+
+ await mcp_config.list_office_claw_mcp_tools(params)
+ await mcp_config.list_office_claw_mcp_tools(params)
+ assert calls == 1, (
+ "aux --require/--import directory over the cap must not disable the cache"
+ )
+
+ # aux script rebuild rotates the key via its single-file fingerprint
+ polyfill.write_text("polyfill v2-with-more-bytes")
+ await mcp_config.list_office_claw_mcp_tools(params)
+ assert calls == 2, "aux script rebuild must rotate the key"
+
+ # entry tree change still rotates the key
+ (dist / "server-toolsets.js").write_text("toolset v2-with-more-bytes")
+ await mcp_config.list_office_claw_mcp_tools(params)
+ assert calls == 3, "entry tree change must rotate the key"