-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathmcp_env.py
More file actions
405 lines (316 loc) · 13.7 KB
/
Copy pathmcp_env.py
File metadata and controls
405 lines (316 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""Environment configuration for the MCP ClickHouse server.
This module handles all environment variable configuration with sensible defaults
and type conversion.
"""
from dataclasses import dataclass
import os
from typing import Optional
from enum import Enum
class TransportType(str, Enum):
"""Supported MCP server transport types."""
STDIO = "stdio"
HTTP = "http"
SSE = "sse"
@classmethod
def values(cls) -> list[str]:
"""Get all valid transport values."""
return [transport.value for transport in cls]
@dataclass
class ClickHouseConfig:
"""Configuration for ClickHouse connection settings.
This class handles all environment variable configuration with sensible defaults
and type conversion. It provides typed methods for accessing each configuration value.
Required environment variables (only when CLICKHOUSE_ENABLED=true):
CLICKHOUSE_HOST: The hostname of the ClickHouse server
CLICKHOUSE_USER: The username for authentication
CLICKHOUSE_PASSWORD: The password for authentication
Optional environment variables (with defaults):
CLICKHOUSE_ROLE: The role to use for authentication (default: None)
CLICKHOUSE_PORT: The port number (default: 8443 if secure=True, 8123 if secure=False)
CLICKHOUSE_SECURE: Enable HTTPS (default: true)
CLICKHOUSE_VERIFY: Verify SSL certificates (default: true)
CLICKHOUSE_SERVER_HOST_NAME: Server hostname for SNI override and certificate validation (default: None)
CLICKHOUSE_CONNECT_TIMEOUT: Connection timeout in seconds (default: 30)
CLICKHOUSE_SEND_RECEIVE_TIMEOUT: Send/receive timeout in seconds (default: 300)
CLICKHOUSE_DATABASE: Default database to use (default: None)
CLICKHOUSE_PROXY_PATH: Path to be added to the host URL. For instance, for servers behind an HTTP proxy (default: None)
CLICKHOUSE_ENABLED: Enable ClickHouse server (default: true)
CLICKHOUSE_ALLOW_WRITE_ACCESS: Allow write operations (DDL and DML) (default: false)
CLICKHOUSE_ALLOW_DROP: Allow destructive operations (DROP, TRUNCATE) when writes are also enabled (default: false)
"""
def __init__(self):
"""Initialize the configuration from environment variables."""
if self.enabled:
self._validate_required_vars()
@property
def enabled(self) -> bool:
"""Get whether ClickHouse server is enabled.
Default: True
"""
return os.getenv("CLICKHOUSE_ENABLED", "true").lower() == "true"
@property
def host(self) -> str:
"""Get the ClickHouse host."""
return os.environ["CLICKHOUSE_HOST"]
@property
def port(self) -> int:
"""Get the ClickHouse port.
Defaults to 8443 if secure=True, 8123 if secure=False.
Can be overridden by CLICKHOUSE_PORT environment variable.
"""
if "CLICKHOUSE_PORT" in os.environ:
return int(os.environ["CLICKHOUSE_PORT"])
return 8443 if self.secure else 8123
@property
def username(self) -> str:
"""Get the ClickHouse username."""
return os.environ["CLICKHOUSE_USER"]
@property
def password(self) -> str:
"""Get the ClickHouse password."""
return os.environ["CLICKHOUSE_PASSWORD"]
@property
def role(self) -> Optional[str]:
"""Get the ClickHouse role."""
return os.getenv("CLICKHOUSE_ROLE")
@property
def database(self) -> Optional[str]:
"""Get the default database name if set."""
return os.getenv("CLICKHOUSE_DATABASE")
@property
def secure(self) -> bool:
"""Get whether HTTPS is enabled.
Default: True
"""
return os.getenv("CLICKHOUSE_SECURE", "true").lower() == "true"
@property
def verify(self) -> bool:
"""Get whether SSL certificate verification is enabled.
Default: True
"""
return os.getenv("CLICKHOUSE_VERIFY", "true").lower() == "true"
@property
def server_host_name(self) -> Optional[str]:
"""Get the server hostname for SNI override."""
return os.getenv("CLICKHOUSE_SERVER_HOST_NAME")
@property
def connect_timeout(self) -> int:
"""Get the connection timeout in seconds.
Default: 30
"""
return int(os.getenv("CLICKHOUSE_CONNECT_TIMEOUT", "30"))
@property
def send_receive_timeout(self) -> int:
"""Get the send/receive timeout in seconds.
Default: 300 (ClickHouse default)
"""
return int(os.getenv("CLICKHOUSE_SEND_RECEIVE_TIMEOUT", "300"))
@property
def proxy_path(self) -> str:
return os.getenv("CLICKHOUSE_PROXY_PATH")
@property
def allow_write_access(self) -> bool:
"""Get whether write operations (DDL and DML) are allowed.
Default: False
"""
return os.getenv("CLICKHOUSE_ALLOW_WRITE_ACCESS", "false").lower() == "true"
@property
def allow_drop(self) -> bool:
"""Get whether DROP operations (DROP TABLE, DROP DATABASE) are allowed.
This setting provides an additional safety layer when write access is enabled.
Even with CLICKHOUSE_ALLOW_WRITE_ACCESS=true, DROP operations require this flag.
Default: False
"""
return os.getenv("CLICKHOUSE_ALLOW_DROP", "false").lower() == "true"
def get_client_config(self) -> dict:
"""Get the configuration dictionary for clickhouse_connect client.
Returns:
dict: Configuration ready to be passed to clickhouse_connect.get_client()
"""
config = {
"host": self.host,
"port": self.port,
"username": self.username,
"password": self.password,
"interface": "https" if self.secure else "http",
"secure": self.secure,
"verify": self.verify,
"connect_timeout": self.connect_timeout,
"send_receive_timeout": self.send_receive_timeout,
"client_name": "mcp_clickhouse",
}
# Add optional role if set
if self.role:
config.setdefault("settings", {})["role"] = self.role
# Add optional database if set
if self.database:
config["database"] = self.database
if self.proxy_path:
config["proxy_path"] = self.proxy_path
if self.server_host_name:
config["server_host_name"] = self.server_host_name
return config
def _validate_required_vars(self) -> None:
"""Validate that all required environment variables are set.
Raises:
ValueError: If any required environment variable is missing.
"""
missing_vars = []
for var in ["CLICKHOUSE_HOST", "CLICKHOUSE_USER", "CLICKHOUSE_PASSWORD"]:
if var not in os.environ:
missing_vars.append(var)
if missing_vars:
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
@dataclass
class ChDBConfig:
"""Configuration for chDB connection settings.
This class handles all environment variable configuration with sensible defaults
and type conversion. It provides typed methods for accessing each configuration value.
Required environment variables:
CHDB_DATA_PATH: The path to the chDB data directory (only required if CHDB_ENABLED=true)
Optional environment variables (with defaults):
CHDB_ALLOW_WRITE_ACCESS: Allow writes; when false, the session runs under
SET readonly=2 (table functions stay usable) (default: false)
CHDB_MAX_RESULT_BYTES: Engine result-size cap, also used as the final
Python truncation budget (default: 1048576, i.e. 1 MiB)
CHDB_FILE_ALLOWLIST: Colon-separated path prefixes. When set, the chDB
query tool is sandboxed: raw SQL may not call external/file table
functions (file/url/s3/remote/...) (default: unset)
"""
# Engine result-size cap (1 MiB) when CHDB_MAX_RESULT_BYTES is unset/invalid.
DEFAULT_MAX_RESULT_BYTES = 1024 * 1024
def __init__(self):
"""Initialize the configuration from environment variables."""
if self.enabled:
self._validate_required_vars()
@property
def enabled(self) -> bool:
"""Get whether chDB is enabled.
Default: False
"""
return os.getenv("CHDB_ENABLED", "false").lower() == "true"
@property
def data_path(self) -> str:
"""Get the chDB data path."""
return os.getenv("CHDB_DATA_PATH", ":memory:")
@property
def allow_write_access(self) -> bool:
"""Get whether writes are allowed.
When false (the default), the chDB session is put under SET readonly=2,
which rejects INSERT/CREATE/DROP/ALTER on persistent tables while still
allowing SET and table functions.
Default: False
"""
return os.getenv("CHDB_ALLOW_WRITE_ACCESS", "false").lower() == "true"
@property
def max_result_bytes(self) -> int:
"""Get the result-size cap in bytes (engine cap + Python truncation).
Falls back to DEFAULT_MAX_RESULT_BYTES when unset or not a positive int.
Default: 1048576 (1 MiB)
"""
raw = os.getenv("CHDB_MAX_RESULT_BYTES")
if not raw:
return self.DEFAULT_MAX_RESULT_BYTES
try:
value = int(raw)
except ValueError:
return self.DEFAULT_MAX_RESULT_BYTES
return value if value > 0 else self.DEFAULT_MAX_RESULT_BYTES
@property
def file_allowlist(self) -> tuple[str, ...]:
"""Colon-separated file-path allowlist prefixes (like DuckDB's ``allowed_directories``).
When non-empty, chDB may read file-like sources (file/s3/url/…) only when
their path is under one of these prefixes; paths outside — and DSN-based
external sources that can't be path-checked — are refused. Empty by
default (no restriction). See ``CHDB_FILE_ALLOWLIST``.
"""
raw = os.getenv("CHDB_FILE_ALLOWLIST")
if not raw:
return ()
return tuple(p for p in (part.strip() for part in raw.split(":")) if p)
def get_client_config(self) -> dict:
"""Get the configuration dictionary for chDB client.
Returns:
dict: Configuration ready to be passed to chDB client
"""
return {
"data_path": self.data_path,
}
def _validate_required_vars(self) -> None:
"""Validate that all required environment variables are set.
Raises:
ValueError: If any required environment variable is missing.
"""
pass
# Global instance placeholders for the singleton pattern
_CONFIG_INSTANCE = None
_CHDB_CONFIG_INSTANCE = None
def get_config():
"""
Gets the singleton instance of ClickHouseConfig.
Instantiates it on the first call.
"""
global _CONFIG_INSTANCE
if _CONFIG_INSTANCE is None:
# Instantiate the config object here, ensuring load_dotenv() has likely run
_CONFIG_INSTANCE = ClickHouseConfig()
return _CONFIG_INSTANCE
def get_chdb_config() -> ChDBConfig:
"""
Gets the singleton instance of ChDBConfig.
Instantiates it on the first call.
Returns:
ChDBConfig: The chDB configuration instance
"""
global _CHDB_CONFIG_INSTANCE
if _CHDB_CONFIG_INSTANCE is None:
_CHDB_CONFIG_INSTANCE = ChDBConfig()
return _CHDB_CONFIG_INSTANCE
@dataclass
class MCPServerConfig:
"""Configuration for MCP server-level settings.
These settings control the server transport and tool behavior and are
intentionally independent of ClickHouse connection validation.
Optional environment variables (with defaults):
CLICKHOUSE_MCP_SERVER_TRANSPORT: "stdio", "http", or "sse" (default: stdio)
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_AUTH_TOKEN: Static bearer token for HTTP/SSE transports.
One authentication mode must be configured for HTTP/SSE; the other two
options are FASTMCP_SERVER_AUTH (FastMCP OAuth/OIDC providers) and
CLICKHOUSE_MCP_AUTH_DISABLED=true.
CLICKHOUSE_MCP_AUTH_DISABLED: Disable authentication entirely (default: false,
development only)
"""
@property
def server_transport(self) -> str:
transport = os.getenv("CLICKHOUSE_MCP_SERVER_TRANSPORT", TransportType.STDIO.value).lower()
if transport not in TransportType.values():
valid_options = ", ".join(f'"{t}"' for t in TransportType.values())
raise ValueError(f"Invalid transport '{transport}'. Valid options: {valid_options}")
return transport
@property
def bind_host(self) -> str:
return os.getenv("CLICKHOUSE_MCP_BIND_HOST", "127.0.0.1")
@property
def bind_port(self) -> int:
return int(os.getenv("CLICKHOUSE_MCP_BIND_PORT", "8000"))
@property
def query_timeout(self) -> int:
return int(os.getenv("CLICKHOUSE_MCP_QUERY_TIMEOUT", "30"))
@property
def auth_token(self) -> Optional[str]:
"""Get the authentication token for HTTP/SSE transports."""
return os.getenv("CLICKHOUSE_MCP_AUTH_TOKEN", None)
@property
def auth_disabled(self) -> bool:
"""Get whether authentication is disabled."""
return os.getenv("CLICKHOUSE_MCP_AUTH_DISABLED", "false").lower() == "true"
_MCP_CONFIG_INSTANCE = None
def get_mcp_config() -> MCPServerConfig:
"""Gets the singleton instance of MCPServerConfig."""
global _MCP_CONFIG_INSTANCE
if _MCP_CONFIG_INSTANCE is None:
_MCP_CONFIG_INSTANCE = MCPServerConfig()
return _MCP_CONFIG_INSTANCE