Skip to content

Connection pooling: persist/reuse ConnectHandler across tool calls #17

Open

Description

@h-network

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 a threading.Lock-based pool maps natively with no async bridge needed.
  • The group fan-out is already multi-threaded: run_show_command_on_group runs run_show_command across a real ThreadPoolExecutor. 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, so run_show_command and run_show_command_on_group don't change.

Proposed design

  • A ConnectionPool keyed by device name. Each entry holds the ConnectHandler, a per-router threading.Lock, and a last_used timestamp.
  • _managed_connection() borrows from the pool: reuse the session if net_connect.is_alive(), otherwise reconnect. Return-to-pool on exit instead of calling disconnect().
  • 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)

  1. Default on, or opt-in via a setting? I'd lean opt-in first, then flip once it's proven.
  2. Idle timeout — expose it as an env var or setting? What name fits your config convention?
  3. Liveness — is is_alive() sufficient for you, or do you want an optional lightweight keepalive/RPC probe before handing a pooled session back?
  4. 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

  1. ktbyers commented on Aug 11, 2026

    @ktbyers
    Owner

    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.

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