Skip to content

Commit 6c0dc7c

Browse files
committed
Instruct on ClickHouse
Signed-off-by: Zhaoyuan (Ryan) Fu <[email protected]>
1 parent e4b488e commit 6c0dc7c

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ Dockerfile.local
109109
security.properties
110110
*.crt
111111
*.key
112-
*~
112+
*~
113+
# MCP server configuration with credentials
114+
src/main/resources/application-mcp.properties

src/main/java/org/cbioportal/infrastructure/ai/tools/DatabaseQueryTools.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ public DatabaseQueryTools(@Qualifier("clickhouseDataSource") DataSource dataSour
3737
+ "Use this to retrieve cancer genomics data including studies, patients, samples, mutations, and clinical information. "
3838
+ "Only SELECT queries are allowed for security. "
3939
+ "The database contains base tables and optimized derived tables (*_derived). "
40+
+ "CRITICAL - ClickHouse Syntax Requirements: "
41+
+ "1. Column names are CASE-SENSITIVE and stored in LOWERCASE (use 'cancer_study_id' NOT 'CANCER_STUDY_ID'). "
42+
+ "2. Table names are lowercase (use 'cancer_study' NOT 'CANCER_STUDY'). "
43+
+ "3. Always use lowercase in WHERE, JOIN, and SELECT clauses. "
44+
+ "4. If unsure about column names, use 'SELECT * FROM table LIMIT 1' first to see actual column names. "
4045
+ "Returns formatted query results with clear explanations.")
4146
public String queryDatabase(
4247
@ToolParam(
4348
description =
44-
"SQL SELECT query to execute. Example: 'SELECT * FROM cancer_study LIMIT 10'")
49+
"SQL SELECT query to execute using ClickHouse syntax. "
50+
+ "IMPORTANT: Use lowercase for ALL column and table names! "
51+
+ "Examples: "
52+
+ "'SELECT * FROM cancer_study LIMIT 10' (discover columns), "
53+
+ "'SELECT cancer_study_id, name, type_of_cancer_id FROM cancer_study LIMIT 5' (specific columns), "
54+
+ "'SELECT p.stable_id, s.stable_id FROM patient p JOIN sample s ON p.internal_id = s.patient_id LIMIT 5' (with JOIN)")
4555
String query,
4656
@ToolParam(
4757
description = "Maximum number of rows to return (optional, default: 100, max: 1000)",
@@ -96,20 +106,26 @@ public String queryDatabase(
96106
if (errorMsg.contains("Table") && errorMsg.contains("doesn't exist")) {
97107
return "Error: The specified table doesn't exist. "
98108
+ "Use the listTables tool to see available tables, "
99-
+ "or check the table name spelling.";
100-
} else if (errorMsg.contains("Unknown column")) {
109+
+ "or check the table name spelling (remember: use lowercase).";
110+
} else if (errorMsg.contains("Unknown column") || errorMsg.contains("Unknown identifier")) {
101111
return "Error: Unknown column in the query. "
102-
+ "Use the getTableSchema tool to see available columns for the table.";
103-
} else if (errorMsg.contains("syntax")) {
104-
return "Error: SQL syntax error. Please check the query structure. "
112+
+ "ClickHouse column names are CASE-SENSITIVE and stored in lowercase! "
113+
+ "Use 'SELECT * FROM table LIMIT 1' to see actual column names, "
114+
+ "or use the getTableSchema tool. "
115+
+ "Example: use 'cancer_study_id' instead of 'CANCER_STUDY_ID'.";
116+
} else if (errorMsg.contains("syntax") || errorMsg.contains("bad SQL grammar")) {
117+
return "Error: SQL syntax error. "
118+
+ "Common issue: ClickHouse requires lowercase column names! "
119+
+ "Use 'SELECT * FROM table LIMIT 1' first to see the correct column names. "
105120
+ "Error details: "
106121
+ errorMsg;
107122
}
108123
}
109124

110125
return "Error executing query: "
111126
+ errorMsg
112-
+ "\nTip: Use getTableSchema to verify table structure before querying.";
127+
+ "\n\nClickHouse Reminder: Column names are case-sensitive and stored in lowercase. "
128+
+ "Try 'SELECT *' first to see actual column names.";
113129
}
114130
}
115131

src/main/java/org/cbioportal/infrastructure/ai/tools/DatabaseSchemaTools.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public class DatabaseSchemaTools {
3434
"Get the schema definition (CREATE TABLE statement) for a specific database table. "
3535
+ "Use this tool when you need to understand the structure, columns, data types, or relationships "
3636
+ "of a table before writing a query. "
37-
+ "Tables are available in the ClickHouse database. Many tables also exist in MySQL with the same structure.")
37+
+ "IMPORTANT: This shows the original MySQL schema definition. "
38+
+ "In ClickHouse, column names are stored in LOWERCASE. "
39+
+ "When querying, always use lowercase column names (e.g., 'cancer_study_id' not 'CANCER_STUDY_ID').")
3840
public String getTableSchema(
3941
@ToolParam(
4042
description =
@@ -65,7 +67,12 @@ public String getTableSchema(
6567
tableName);
6668
}
6769

68-
return String.format("Schema for table '%s' (ClickHouse database):\n\n%s", tableName, schema);
70+
return String.format(
71+
"Schema for table '%s' (ClickHouse database):\n\n%s\n\n"
72+
+ "⚠️ IMPORTANT: In ClickHouse, all column names are stored in LOWERCASE.\n"
73+
+ "When writing queries, use lowercase column names (e.g., 'cancer_study_id' not 'CANCER_STUDY_ID').\n"
74+
+ "The schema above shows the original MySQL definition with uppercase, but queries must use lowercase.",
75+
tableName, schema);
6976

7077
} catch (Exception e) {
7178
logger.error("Error retrieving schema for table: {}", tableName, e);

src/main/resources/application-mcp.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ spring.ai.mcp.server.version=1.0.0
2222
# =====================================================
2323
# ClickHouse Database Configuration
2424
# =====================================================
25-
spring.datasource.clickhouse.url=
26-
spring.datasource.clickhouse.username=
27-
spring.datasource.clickhouse.password=
25+
spring.datasource.clickhouse.url=jdbc:ch:https://YOUR_CLICKHOUSE_HOST:8443/YOUR_DATABASE?zeroDateTimeBehavior=convertToNull
26+
spring.datasource.clickhouse.username=YOUR_USERNAME
27+
spring.datasource.clickhouse.password=YOUR_PASSWORD
2828
spring.datasource.clickhouse.driver-class-name=com.clickhouse.jdbc.ClickHouseDriver
2929

3030
# Connection pool settings for fast startup

0 commit comments

Comments
 (0)