Skip to content

Commit db1fc81

Browse files
authored
Fix mcp-serve to read connection from sidemantic.yaml config (#129)
mcp-serve was ignoring the config file's connection settings, so init_sql with ATTACH/LOAD statements and non-default connections didn't work. Now resolves connection string from config like the query and api-serve commands do.
1 parent 7f102c2 commit db1fc81

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

sidemantic/cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,24 @@ def mcp_serve(
268268
else:
269269
db_path = str(db) if db else None
270270

271-
# Merge CLI --init-sql with config-based init_sql
271+
# Resolve connection from CLI args or config
272+
connection_str = None
272273
effective_init_sql: list[str] | None = init_sql if init_sql else None
273-
if not effective_init_sql and _loaded_config:
274+
275+
if db_path and db_path != ":memory:":
276+
connection_str = f"duckdb:///{Path(db_path).absolute()}"
277+
elif db_path == ":memory:":
278+
connection_str = "duckdb:///:memory:"
279+
elif _loaded_config and _loaded_config.connection:
280+
connection_str = build_connection_string(_loaded_config)
281+
if not effective_init_sql:
282+
effective_init_sql = get_init_sql(_loaded_config)
283+
elif not effective_init_sql and _loaded_config:
274284
effective_init_sql = get_init_sql(_loaded_config)
275285

276286
try:
277287
# Initialize the semantic layer
278-
initialize_layer(str(directory), db_path, init_sql=effective_init_sql)
288+
initialize_layer(str(directory), connection=connection_str, init_sql=effective_init_sql)
279289

280290
# If demo mode, populate with demo data
281291
if demo:

sidemantic/mcp_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
def initialize_layer(
2020
directory: str,
2121
db_path: str | None = None,
22+
connection: str | None = None,
2223
init_sql: list[str] | None = None,
2324
) -> SemanticLayer:
2425
"""Initialize the semantic layer with models from directory."""
2526
global _layer
2627

27-
# Create connection string
28-
connection = None
29-
if db_path:
28+
# Use explicit connection string, or build one from db_path
29+
if connection is None and db_path:
3030
if db_path == ":memory:":
3131
connection = "duckdb:///:memory:"
3232
else:

tests/test_cli_commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ def test_mcp_serve_calls_initialize(monkeypatch, tmp_path):
157157
pytest.importorskip("mcp")
158158
called = {}
159159

160-
def fake_initialize_layer(directory, db_path=None, init_sql=None):
160+
def fake_initialize_layer(directory, db_path=None, connection=None, init_sql=None):
161161
called["directory"] = directory
162162
called["db_path"] = db_path
163+
called["connection"] = connection
163164
called["init_sql"] = init_sql
164165

165166
def fake_run(*args, **kwargs):

0 commit comments

Comments
 (0)