Repository navigation
Description
Motivation
Each MCP tool call currently opens a fresh NETCONF session (common.connect() → work → dev.close()). For AI-driven single-host deep-dive / troubleshooting workflows this is wasteful: a typical LLM exploration hits the same device with 5–20 sequential tool calls over a few minutes.
Per-call handshake cost is roughly:
- TCP + SSH handshake: ~1–2 s
- NETCONF
<hello>capability exchange: ~0.5–1 s - PyEZ
Device.open()facts gather (when enabled): ~5–10 RPCs
Upstream Juniper/junos-mcp-server#26 / PR #27 benchmarks 20.7× speedup (35 commands: 24.5 s → 1.2 s) by reusing the session.
Where the win is (and isn't)
Big win:
- AI assistant exploring a single host sequentially (
get_device_facts→get_version→list_remote_files→ severalrun_show_commandcalls) - Troubleshooting loops (
show log→show interfaces→show route→ ...) - Agentic workflows that iterate over a host list, 5–10 probes per host
Small / negative win:
- Cross-host parallel batch inside a single tool call (
run_show_command_batch) — already keeps each connection open for its own duration; pool helps only for the next call. check_reachability— explicitly measuring handshake latency, should bypass the pool.- Long idle gaps — pool eviction kicks in, no reuse.
Rationale: the CLI tool (junos-ops) has a short process lifecycle, so its _open_connection / close pattern is correct there. The MCP server runs for the lifetime of an assistant session, which is exactly where pooling pays off.
Design sketch
class ConnectionPool:
def acquire(self, hostname: str) -> Device: ...
def release(self, hostname: str, dev: Device) -> None: ...
def close_all(self) -> None: ...- One
RLockper hostname (serialize concurrent acquires to the same host). - Entries evicted after
idle_timeoutseconds (default 60, configurable). - On
acquire: health-check viadev.connected(or a cheapget-software-informationRPC); reopen if stale. - Keyed by
(hostname, config_path)to respect per-call config overrides. - Hooked into
_connect_and_runso existing tools become pool-aware without changing each tool. - Opt-out via
JUNOS_MCP_POOL=0env var and abypass_pool=Truekwarg forcheck_reachability. - On process shutdown: close all.
Tasks
-
junos_mcp/pool.pywithConnectionPoolclass (per-host lock, idle-timeout eviction, health check on acquire). - Refactor
_connect_and_runto acquire/release from the pool when enabled. -
check_reachabilitybypasses the pool (its purpose is measuring fresh handshake). - Env-var knobs:
JUNOS_MCP_POOL(1/0),JUNOS_MCP_POOL_IDLE(seconds, default 60). - Unit tests with a mocked
common.connect(reuse, eviction, stale-reopen, per-host lock). - Document in README: when pooling helps, when to disable it, security implications (long-lived credentials in memory).
Security note
Pooling extends the in-memory lifetime of authenticated NETCONF sessions and (in password-auth setups) credentials. The idle timeout and process-lifetime bound this, but the README should call it out so operators know what they're trading for the speedup.
Activity
feat(pool): per-host NETCONF connection pool (#8)