File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 1
1
import logging
2
2
from typing import Sequence
3
+ import concurrent .futures
4
+ import atexit
3
5
4
6
import timeplus_connect
5
7
from timeplus_connect .driver .binding import quote_identifier , format_query_value
20
22
)
21
23
logger = logging .getLogger (MCP_SERVER_NAME )
22
24
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
+
23
29
load_dotenv ()
24
30
25
31
deps = [
@@ -113,6 +119,15 @@ def get_table_info(table):
113
119
@mcp .tool ()
114
120
def run_sql (query : str ):
115
121
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
+
116
131
client = create_timeplus_client ()
117
132
try :
118
133
readonly = 1 if config .readonly else 0
You can’t perform that action at this time.
0 commit comments