Repository navigation
perf(mcp-gateway): drop agent_sdk from the Python stub's import path - #20670
chenmingwei23 wants to merge 1 commit into
Conversation
… agent_sdk The Python MCP stub runs once per session per MCP server, so what it imports is multiplied by every session on the host. It imported the 10 managed server NAMES from mcp_cleanup, which eagerly imports agent_sdk.mcp_refs for an unrelated constant and so pulled kiro_crew.agent_sdk and its 14 submodules into every stub. Spell the names out as a frozenset in stub.py, the file's own established pattern for exactly this (see _KIROCREW_MCP_SUBCOMMANDS). A fresh stub import drops from 228 to 212 modules and no longer loads agent_sdk. The module-set regression test gains kiro_crew.agent_sdk so the footprint cannot silently return, and the fallback-precheck test now pins value equality against mcp_cleanup's set instead of object identity.
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Check the comment in Suggestions
[DESIGN-REVIEWED] c7684d7 Description read[DESCRIPTION-READ] 601708b44a1b3e964b8149751953a6c255ed7b72679bf3dbddd6595e0f0ed61f That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
First Principles Review (Opus 5.5, fork) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS The stub gets a second hand-kept copy of the 10 server names, but the actual cause is one module-level import in Not justified as shipped
What this change shipsInventory (4 items) — 3 justifiedIntent: make each MCP stub process start lighter by not loading the
[FIRST-PRINCIPLES-REVIEWED] c7684d7 Description read[DESCRIPTION-READ] b0664a0fc98d0c11131dd9fa5e559fcb9261966fb619576f3d8f36cee4ee0a42 That sha256 names the PR description this verdict read. Recompute it with |
|
Superseded by #20753, re-opened from the main repository so every review lane can publish. |
Problem / Motivation
Goal: make each MCP stub process start lighter by not loading a 14-module
subtree it never uses.
The Python MCP stub (
kiro_crew.mcp_gateway.stub) runs once per session per MCPserver, so whatever it imports is multiplied by every stub on a host (on one
measured host, 965 stubs). A fresh stub import on current
mainloads 228modules, including
kiro_crew.agent_sdkand its 14 submodules — which the stubnever uses. It pulls them only because it imports the 10 managed server names
from
mcp_cleanup, andmcp_cleanupeagerly importsagent_sdk.mcp_refsfor anunrelated constant. The stub needs only the names.
Why it matters
Every byte and module the stub imports is paid again by each of the many stub
processes a host runs. Dropping the
agent_sdksubtree from the stub's importpath cuts per-stub resident memory and start cost at the only point the stub
actually touches those names, with no behaviour change.
Not a goal
PoolKey, hashing, or any security code — not touched.concurrent.futures,urllib,ctypes, orexecutors/subprocess_pool: these are not stub-local (stdlib/shared-leaf,pulled on paths used across the codebase), so they are deliberately left alone.
What changed (motivation → approach → change)
agent_sdk(14 submodules)resident though unused.
from kiro_crew.mcp_cleanup import KIROCREW_BIN_MCP_SERVERSdrags inmcp_cleanup, whose line-24 import ofagent_sdk.mcp_refspulls the wholeagent_sdkpackage.frozensetinstub.py, so a running stub never importsmcp_cleanup(oragent_sdk). Thismatches the file's own established pattern —
_KIROCREW_MCP_SUBCOMMANDSisalready spelled out in
stub.pyfor exactly this reason, kept honest by aratchet test. The two consumers (
build_register_payload,_fallback_target_is_own_control_plane) read the same 10 names, now from themodule-local set.
Before / after a fresh stub import, measured on current
main:kiro_crew.agent_sdk16 fewer modules per stub;
mcp_cleanupis also no longer imported by a runningstub.
Backwards compatibility
Removes nothing: the stub no longer re-exports
mcp_cleanup'sKIROCREW_BIN_MCP_SERVERSas the identical object, but no code readsKIROCREW_BIN_MCP_SERVERSfrom the stub module — a grep ofsrc/andtest/finds the only readers of
stub.KIROCREW_BIN_MCP_SERVERSare the stub itself andthis PR's own tests. Every other
src/consumer imports the constant frommcp_cleanupdirectly and is untouched. No default, UI, or user-facing outputchanges.
Tests
test_mcp_gateway_stub_import_budget.pygainskiro_crew.agent_sdkto itsforbidden-when-bridging set, so the footprint cannot silently return. It fails
on
mainand passes here (red-first).test_the_fallback_precheck_covers_every_control_plane(
test_mcp_gateway_stub_session_token.py) now pins the stub's spelled-out setto
mcp_cleanup's by value (and keeps the superset-of-CONTROL_PLANE_BACKENDSinvariant), replacing the object-identity assertion the old re-export relied
on.
Manual verification
N/A — unit coverage is sufficient; the change is import-path only and the
module-set bound plus the value-ratchet lock the behaviour in.
Related Issues
Closes #20642
Part of #20640
Checklist
perf: ...)CI note
Backend Tests (Windows) (7)fails on two cases intest/test_app_mcp_tool_policy_scope.py(serves_managed_tools) that areoutside this diff — this PR touches only
stub.pyand two stub test files, notmcp_shared.py. The cases pass locally in isolation; the shard failure is aknown Windows code-location fragility that #20711 fixes on
main. Not retriedhere.