Client connection reuse and server-side query cancellation#152
Client connection reuse and server-side query cancellation#152joe-clickhouse wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the mcp_clickhouse MCP server for sustained/high-concurrency usage by reusing ClickHouse clients across tool calls, implementing true server-side query cancellation, and aligning network timeouts to prevent worker exhaustion.
Changes:
- Added a cached
clickhouse_connectclient layer keyed by resolved client config, with eviction on liveness/connection failures and limited retries for safe metadata tools. - Implemented query ID tracking and server-side cancellation via
KILL QUERYon MCP timeouts. - Introduced
CLICKHOUSE_MCP_MAX_WORKERSand alignedsend_receive_timeouttoquery_timeout + 5unless explicitly overridden.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
mcp_clickhouse/mcp_server.py |
Implements client caching, active query tracking, cancellation via KILL QUERY, timeout alignment, and worker pool sizing/logging. |
mcp_clickhouse/mcp_env.py |
Adds CLICKHOUSE_MCP_MAX_WORKERS configuration property. |
mcp_clickhouse/__init__.py |
Exposes additional helpers at package level via imports/__all__. |
README.md |
Documents the updated timeout behavior, cancellation semantics, and new max-workers config. |
tests/test_client_cache.py |
Adds coverage for caching behavior, eviction, and timeout alignment. |
tests/test_query_cancellation.py |
Adds coverage for query_id propagation, active query tracking, and timeout-triggered cancellation. |
tests/test_context_config_override.py |
Ensures test isolation by clearing the client cache around config override tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
peter-leonov-ch
left a comment
There was a problem hiding this comment.
Not sure if this PR has integration tests, if not, that'd be nice to add.
| CLICKHOUSE_MCP_BIND_HOST: Bind host for HTTP/SSE (default: 127.0.0.1) | ||
| CLICKHOUSE_MCP_BIND_PORT: Bind port for HTTP/SSE (default: 8000) | ||
| CLICKHOUSE_MCP_QUERY_TIMEOUT: SELECT tool timeout in seconds (default: 30) | ||
| CLICKHOUSE_MCP_MAX_WORKERS: Maximum thread pool workers for query execution (default: 10) |
There was a problem hiding this comment.
Fun fact: the JS client proxies all the CLICKHOUSE_* parameters verbatim to CH. Seems to have worked fine without the need to explicitly enumerate then. It does not forward env vars though.
There was a problem hiding this comment.
Nice! Thanks for the context.
| with _active_queries_lock: | ||
| entry = _active_queries.pop(query_id, None) |
There was a problem hiding this comment.
Not sure if it's relevant anymore, but a helper function that does both: respects the lock and mutates the guarder value might be of use.
There was a problem hiding this comment.
Thanks! I did consider, but the 3 lock sites each do different things under the lock so a shared helper seemed to obscure more than help.
| logger.warning( | ||
| "Query %s timed out after %s seconds: %s", query_id, timeout_secs, query | ||
| ) | ||
| _cancel_query(query_id) |
There was a problem hiding this comment.
The code says it's reusing the same connection. I'm new here, is this a native connection or an HTTP connection? If it's the HTTP connection then running another query on a "busy" connection might not work if we're timing out on the client side.
There was a problem hiding this comment.
This uses clickhouse-connect under the hood, so it's HTTP. However, the cached client wraps a connection pool, so the KILL grabs a different socket from the in-flight query rather than queueing. We also set autogenerate_session_id=False, which is what lets two calls on the same client run concurrently.
Summary
The goal of this PR is to harden the MCP server for heavy, sustained usage by reusing client connections, adding real server-side query cancellation, and preventing worker thread pool exhaustion from zombie queries.
clickhouse_connectclients by config key instead of creating a new client on every tool call. Eliminates hundreds of ms of overhead per call that a significant contributor to intermittent timeouts under sustained use.query_idto every query and issueKILL QUERYon timeout instead of the no-opfuture.cancel(). Timed-out queries no longer continue running as zombies consuming worker threads and ClickHouse server resources.send_receive_timeouttoquery_timeout+ 5, unless explicitly overridden so worker threads unblock shortly after the MCP timeout fires, preventing thread pool exhaustion.list_databasesandlist_tablesretry once with a fresh client.run_queryevicts but does not retry because writes could duplicate.ContextVaris available before dispatching to the worker thread, fixing a latent bug where PR Client config override support via MCP Context Session states #115 overrides were silently missed inside the executor.