Repository navigation
Description
Right now every tool call opens a fresh ConnectHandler and disconnects at the end of _managed_connection(). For interactive MCP use — where an agent fires many send_show_command calls at the same device in a single session — that means a full SSH setup and teardown for every command.
The code already anticipates this. _managed_connection() carries the comment:
When connection pooling is implemented this function is the right place to swap close-on-exit for return-to-pool-on-exit, without touching run_show_command.
I'd like to implement exactly that, and I have a working, reviewed reference for the same pattern (see prior art below). I'm opening this first to agree on the design and the policy knobs before sending a PR.
Why it fits netmiko_mcp cleanly
- The server is synchronous and already fans out groups via
ThreadPoolExecutor, so athreading.Lock-based pool maps natively with no async bridge needed. - The group fan-out is already multi-threaded:
run_show_command_on_grouprunsrun_show_commandacross a realThreadPoolExecutor. That's safe today only because each worker owns a throwaway connection — the moment those workers share a pool dict, concurrent access to it makes a global pool lock required, not optional. And because the idle reaper runs on its own daemon thread alongside those workers, a per-router lock is required so it can't close a session a worker is mid-use. (Two separate tool invocations, by contrast, can't overlap at the pinned SDK version — sync tools run inline on the event loop — so the locking is about the group executor and the reaper, not tool-vs-tool racing.) - The swap is isolated to
_managed_connection()exactly as the comment intends, sorun_show_commandandrun_show_command_on_groupdon't change.
Proposed design
- A
ConnectionPoolkeyed by device name. Each entry holds theConnectHandler, a per-routerthreading.Lock, and alast_usedtimestamp. _managed_connection()borrows from the pool: reuse the session ifnet_connect.is_alive(), otherwise reconnect. Return-to-pool on exit instead of callingdisconnect().- Stale eviction: on
NetmikoTimeoutException,ReadTimeout, or a socket drop, close and drop the entry so the next borrow reconnects cleanly. - Background idle reaper (daemon thread): collect idle candidates under the global pool lock, then close each one under its own per-router lock outside the global lock, so a single hung device can't block cleanup of the others.
- Clean shutdown: close all sessions on server exit.
Open questions (your call)
- Default on, or opt-in via a setting? I'd lean opt-in first, then flip once it's proven.
- Idle timeout — expose it as an env var or setting? What name fits your config convention?
- Liveness — is
is_alive()sufficient for you, or do you want an optional lightweight keepalive/RPC probe before handing a pooled session back? - Any concern with a daemon reaper thread versus lazy reap-on-borrow?
Prior art
I implemented this exact pattern for the Juniper junos-mcp-server: Juniper/junos-mcp-server#27 — thread-safe pool, per-router locking, stale eviction, idle reaper. Benchmarked on my own setup with 5 routers x 7 commands (35 ops): 24.5s to 4.0s sequential (6.2x), and 1.2s parallel (20.7x). Those numbers are from the junos side, not measured against netmiko_mcp. That version needed an anyio.to_thread bridge because its handlers are async; netmiko_mcp being synchronous means the port is actually simpler, since that layer drops out.
Happy to send the PR once you're good with the shape and the knobs above.
Activity
ktbyers commentedon Aug 11, 2026
Sorry, I didn't have notifications enabled on this repo...so I just saw this issue.
Yeah, I definitely have it in my todo list to enable connection pooling/reuse in some way.
I first probably want to implement/support the new stateless MCP spec., however.