Skip to content

Commit a09b71f

Browse files
committed
Expose database type to MCP clients to prevent wrong SQL dialect
- Add Config.db_type property (detects PostgreSQL/MySQL/SQLite from URL) - Include db_type in sql.query tool description and resource://config - Remove duplicated _detect_db_type() from assistant/service.py
1 parent ecbb2ef commit a09b71f

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

app/assistant/service.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def chat(
4444
schema_result = self.registry.call("sql.schema", {})
4545
schema_json = json.dumps(schema_result.get("schema", {}), indent=2)
4646

47-
db_type = _detect_db_type(self.config.db_url)
47+
db_type = self.config.db_type
4848
system = SYSTEM_PROMPT.format(db_type=db_type, schema=schema_json)
4949

5050
messages: List[Dict[str, Any]] = [{"role": "system", "content": system}]
@@ -94,16 +94,6 @@ def _parse_llm_response(text: str) -> Dict[str, Any]:
9494
return {"thought": "", "sql": None, "explanation": text}
9595

9696

97-
def _detect_db_type(db_url: str) -> str:
98-
if "postgresql" in db_url or "postgres" in db_url:
99-
return "PostgreSQL"
100-
if "mysql" in db_url:
101-
return "MySQL"
102-
if "sqlite" in db_url:
103-
return "SQLite"
104-
return "SQL"
105-
106-
10797
"""
10898
Supported LLM_PROVIDER values and their base URLs:
10999

app/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ def load(cls) -> "Config":
118118
chat_history_limit=chat_history_limit,
119119
)
120120

121+
@property
122+
def db_type(self) -> str:
123+
url = self.db_url.lower()
124+
if "postgresql" in url or "postgres" in url:
125+
return "PostgreSQL"
126+
if "mysql" in url:
127+
return "MySQL"
128+
if "sqlite" in url:
129+
return "SQLite"
130+
return "SQL"
131+
121132
@classmethod
122133
def from_env(cls) -> "Config":
123134
return cls.load()

app/mcp/resources.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def read_resource(self, uri: str) -> Dict[str, Any]:
4040
}
4141
if uri == "resource://config":
4242
config_summary = {
43+
"db_type": self.config.db_type,
4344
"mode": self.config.mode,
4445
"limit_default": self.config.limit_default,
4546
"timeout_ms": self.config.timeout_ms,

app/mcp/tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def db_migrate_plan_apply(payload: Dict[str, Any]) -> Dict[str, Any]:
385385
name="sql.query",
386386
title="SQL Query",
387387
description=(
388-
"Execute a read-only SQL query. "
388+
f"Execute a read-only SQL query against {config.db_type}. "
389+
f"Use {config.db_type}-compatible syntax. "
389390
"Only SELECT, WITH, and EXPLAIN statements are allowed. "
390391
"For INSERT, UPDATE, DELETE, or DDL statements use db.apply."
391392
),

0 commit comments

Comments
 (0)