Skip to content

Commit 2c96c64

Browse files
explicitly set interface based on secure value (#87)
* explicitly set interface based on secure value * use monkeypatch to prevent env var leakage * fix deprecated deps in fastmcp constructor * ruff fix
1 parent 415c3b3 commit 2c96c64

File tree

4 files changed

+101
-9
lines changed

4 files changed

+101
-9
lines changed

fastmcp.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://gofastmcp.com/public/schemas/fastmcp.json/v1.json",
3+
"source": {
4+
"path": "mcp_clickhouse/mcp_server.py",
5+
"entrypoint": "mcp"
6+
},
7+
"environment": {
8+
"dependencies": [
9+
"clickhouse-connect",
10+
"python-dotenv",
11+
"pip-system-certs",
12+
"chdb"
13+
]
14+
}
15+
}
16+

mcp_clickhouse/mcp_env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def get_client_config(self) -> dict:
173173
"port": self.port,
174174
"username": self.username,
175175
"password": self.password,
176+
"interface": "https" if self.secure else "http",
176177
"secure": self.secure,
177178
"verify": self.verify,
178179
"connect_timeout": self.connect_timeout,

mcp_clickhouse/mcp_server.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,7 @@ class Table:
6767

6868
load_dotenv()
6969

70-
mcp = FastMCP(
71-
name=MCP_SERVER_NAME,
72-
dependencies=[
73-
"clickhouse-connect",
74-
"python-dotenv",
75-
"pip-system-certs",
76-
"chdb",
77-
],
78-
)
70+
mcp = FastMCP(name=MCP_SERVER_NAME)
7971

8072

8173
@mcp.custom_route("/health", methods=["GET"])

tests/test_config_interface.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pytest
2+
3+
from mcp_clickhouse.mcp_env import ClickHouseConfig
4+
5+
6+
def test_interface_http_when_secure_false(monkeypatch: pytest.MonkeyPatch):
7+
"""Test that interface is set to 'http' when CLICKHOUSE_SECURE=false."""
8+
monkeypatch.setenv("CLICKHOUSE_HOST", "localhost")
9+
monkeypatch.setenv("CLICKHOUSE_USER", "test")
10+
monkeypatch.setenv("CLICKHOUSE_PASSWORD", "test")
11+
monkeypatch.setenv("CLICKHOUSE_SECURE", "false")
12+
monkeypatch.setenv("CLICKHOUSE_PORT", "8123")
13+
14+
config = ClickHouseConfig()
15+
client_config = config.get_client_config()
16+
17+
assert client_config["interface"] == "http"
18+
assert client_config["secure"] is False
19+
assert client_config["port"] == 8123
20+
21+
22+
def test_interface_https_when_secure_true(monkeypatch: pytest.MonkeyPatch):
23+
"""Test that interface is set to 'https' when CLICKHOUSE_SECURE=true."""
24+
monkeypatch.setenv("CLICKHOUSE_HOST", "example.com")
25+
monkeypatch.setenv("CLICKHOUSE_USER", "test")
26+
monkeypatch.setenv("CLICKHOUSE_PASSWORD", "test")
27+
monkeypatch.setenv("CLICKHOUSE_SECURE", "true")
28+
monkeypatch.setenv("CLICKHOUSE_PORT", "8443")
29+
30+
config = ClickHouseConfig()
31+
client_config = config.get_client_config()
32+
33+
assert client_config["interface"] == "https"
34+
assert client_config["secure"] is True
35+
assert client_config["port"] == 8443
36+
37+
38+
def test_interface_https_by_default(monkeypatch: pytest.MonkeyPatch):
39+
"""Test that interface defaults to 'https' when CLICKHOUSE_SECURE is not set."""
40+
monkeypatch.setenv("CLICKHOUSE_HOST", "example.com")
41+
monkeypatch.setenv("CLICKHOUSE_USER", "test")
42+
monkeypatch.setenv("CLICKHOUSE_PASSWORD", "test")
43+
monkeypatch.delenv("CLICKHOUSE_SECURE", raising=False)
44+
monkeypatch.delenv("CLICKHOUSE_PORT", raising=False)
45+
46+
config = ClickHouseConfig()
47+
client_config = config.get_client_config()
48+
49+
assert client_config["interface"] == "https"
50+
assert client_config["secure"] is True
51+
assert client_config["port"] == 8443
52+
53+
54+
def test_interface_http_with_custom_port(monkeypatch: pytest.MonkeyPatch):
55+
"""Test that interface is 'http' with custom port when CLICKHOUSE_SECURE=false."""
56+
monkeypatch.setenv("CLICKHOUSE_HOST", "localhost")
57+
monkeypatch.setenv("CLICKHOUSE_USER", "test")
58+
monkeypatch.setenv("CLICKHOUSE_PASSWORD", "test")
59+
monkeypatch.setenv("CLICKHOUSE_SECURE", "false")
60+
monkeypatch.setenv("CLICKHOUSE_PORT", "9000")
61+
62+
config = ClickHouseConfig()
63+
client_config = config.get_client_config()
64+
65+
assert client_config["interface"] == "http"
66+
assert client_config["secure"] is False
67+
assert client_config["port"] == 9000
68+
69+
70+
def test_interface_https_with_custom_port(monkeypatch: pytest.MonkeyPatch):
71+
"""Test that interface is 'https' with custom port when CLICKHOUSE_SECURE=true."""
72+
monkeypatch.setenv("CLICKHOUSE_HOST", "example.com")
73+
monkeypatch.setenv("CLICKHOUSE_USER", "test")
74+
monkeypatch.setenv("CLICKHOUSE_PASSWORD", "test")
75+
monkeypatch.setenv("CLICKHOUSE_SECURE", "true")
76+
monkeypatch.setenv("CLICKHOUSE_PORT", "9443")
77+
78+
config = ClickHouseConfig()
79+
client_config = config.get_client_config()
80+
81+
assert client_config["interface"] == "https"
82+
assert client_config["secure"] is True
83+
assert client_config["port"] == 9443

0 commit comments

Comments
 (0)