Skip to content

feat: per-host SSH/NETCONF connection pool with idle timeout #5

Closed

Description

@shigechika

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 → several run_show_command calls)
  • 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 RLock per hostname (serialize concurrent acquires to the same host).
  • Entries evicted after idle_timeout seconds (default 60, configurable).
  • On acquire: health-check via dev.connected (or a cheap get-software-information RPC); reopen if stale.
  • Keyed by (hostname, config_path) to respect per-call config overrides.
  • Hooked into _connect_and_run so existing tools become pool-aware without changing each tool.
  • Opt-out via JUNOS_MCP_POOL=0 env var and a bypass_pool=True kwarg for check_reachability.
  • On process shutdown: close all.

Tasks

  • junos_mcp/pool.py with ConnectionPool class (per-host lock, idle-timeout eviction, health check on acquire).
  • Refactor _connect_and_run to acquire/release from the pool when enabled.
  • check_reachability bypasses 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

  1. added a commit that references this issue on May 1, 2026
    452bf65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions