Skip to content

Commit 27f958a

Browse files
committed
escape strings/identifiers in generated queries
1 parent 04dbc57 commit 27f958a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

mcp_clickhouse/mcp_server.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Sequence
33

44
import clickhouse_connect
5+
from clickhouse_connect.driver.binding import quote_identifier, format_query_value
56
from dotenv import load_dotenv
67
from fastmcp import FastMCP
78

@@ -39,18 +40,18 @@ def list_databases():
3940
def list_tables(database: str, like: str = None):
4041
logger.info(f"Listing tables in database '{database}'")
4142
client = create_clickhouse_client()
42-
query = f"SHOW TABLES FROM {database}"
43+
query = f"SHOW TABLES FROM {format_identifier(database)}"
4344
if like:
44-
query += f" LIKE '{like}'"
45+
query += f" LIKE {format_query_value(like)}"
4546
result = client.command(query)
4647

4748
# Get all table comments in one query
48-
table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = '{database}'"
49+
table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = {format_query_value(database)}"
4950
table_comments_result = client.query(table_comments_query)
5051
table_comments = {row[0]: row[1] for row in table_comments_result.result_rows}
5152

5253
# Get all column comments in one query
53-
column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = '{database}'"
54+
column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = {format_query_value(database)}"
5455
column_comments_result = client.query(column_comments_query)
5556
column_comments = {}
5657
for row in column_comments_result.result_rows:
@@ -61,7 +62,7 @@ def list_tables(database: str, like: str = None):
6162

6263
def get_table_info(table):
6364
logger.info(f"Getting schema info for table {database}.{table}")
64-
schema_query = f"DESCRIBE TABLE {database}.`{table}`"
65+
schema_query = f"DESCRIBE TABLE {quote_identifier(database)}.{quote_identifier(table)}"
6566
schema_result = client.query(schema_query)
6667

6768
columns = []

0 commit comments

Comments
 (0)