Skip to content

Commit 4fe612b

Browse files
iskakaushikjovezhong
authored andcommitted
feat: Add query timeout and thread pool for SELECT queries (ClickHouse#20)
1 parent 08d65df commit 4fe612b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

mcp_timeplus/mcp_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22
from typing import Sequence
3+
import concurrent.futures
4+
import atexit
35

46
import timeplus_connect
57
from timeplus_connect.driver.binding import quote_identifier, format_query_value
@@ -20,6 +22,10 @@
2022
)
2123
logger = logging.getLogger(MCP_SERVER_NAME)
2224

25+
QUERY_EXECUTOR = concurrent.futures.ThreadPoolExecutor(max_workers=10)
26+
atexit.register(lambda: QUERY_EXECUTOR.shutdown(wait=True))
27+
SELECT_QUERY_TIMEOUT_SECS = 30
28+
2329
load_dotenv()
2430

2531
deps = [
@@ -113,6 +119,15 @@ def get_table_info(table):
113119
@mcp.tool()
114120
def run_sql(query: str):
115121
logger.info(f"Executing query: {query}")
122+
future = QUERY_EXECUTOR.submit(execute_query, query)
123+
try:
124+
result = future.result(timeout=SELECT_QUERY_TIMEOUT_SECS)
125+
return result
126+
except concurrent.futures.TimeoutError:
127+
logger.warning(f"Query timed out after {SELECT_QUERY_TIMEOUT_SECS} seconds: {query}")
128+
future.cancel()
129+
return f"Queries taking longer than {SELECT_QUERY_TIMEOUT_SECS} seconds are currently not supported."
130+
116131
client = create_timeplus_client()
117132
try:
118133
readonly = 1 if config.readonly else 0

0 commit comments

Comments
 (0)