Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

feat(mcp): support query alias and restrict auto LIMIT to SELECT statements

philau2512707eef785405
diff --git a/linux/src/Services/MCP/MCPServer.cpp b/linux/src/Services/MCP/MCPServer.cpp
index 6196f66..5669f99 100644
--- a/linux/src/Services/MCP/MCPServer.cpp
+++ b/linux/src/Services/MCP/MCPServer.cpp
@@ -186,6 +186,7 @@ JSONRPCResponse MCPServer::handleToolCall(const JSONRPCRequest& req) {
std::optional<std::string> sqlOpt;
std::optional<int> paramsCountOpt;
if (args.contains("sql") && args["sql"].is_string()) sqlOpt = args["sql"].get<std::string>();
+ else if (args.contains("query") && args["query"].is_string()) sqlOpt = args["query"].get<std::string>();
if (args.contains("params") && args["params"].is_array()) paramsCountOpt = static_cast<int>(args["params"].size());
e.input = MCPAuditInput::fromSql(sqlOpt, paramsCountOpt);
e.result = {status, std::nullopt, std::nullopt, durationMs, std::nullopt};
diff --git a/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp b/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
index 2648a3f..0c9006e 100644
--- a/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
@@ -18,17 +18,22 @@ public:
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
{"sql", {{"type", "string"}, {"description", "SQL query to explain"}}},
+ {"query", {{"type", "string"}, {"description", "SQL query to explain (alias for 'sql')"}}},
}},
- {"required", nlohmann::json::array({"connection_id", "sql"})},
+ {"required", nlohmann::json::array({"connection_id"})},
};
}
MCPToolResult execute(const nlohmann::json& params, const MCPToolContext& ctx) override {
const std::string connectionId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string()) {
+ std::string sql;
+ if (params.contains("sql") && params["sql"].is_string()) {
+ sql = params["sql"].get<std::string>();
+ } else if (params.contains("query") && params["query"].is_string()) {
+ sql = params["query"].get<std::string>();
+ } else {
throw MCPToolError::invalidParameters("sql is required");
}
- const std::string sql = params["sql"].get<std::string>();
auto perm = ctx.checkPermission(tier(), connectionId);
if (!perm.isAllowed() && !perm.requiresUserApproval()) {
diff --git a/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp b/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
index 404a9bb..6668131 100644
--- a/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
@@ -23,21 +23,26 @@ public:
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
{"sql", {{"type", "string"}, {"description", "SQL query"}}},
+ {"query", {{"type", "string"}, {"description", "SQL query (alias for 'sql')"}}},
{"params", {{"type", "array"}, {"description", "Parameters for placeholders"}}},
{"row_limit", {{"type", "integer"},
{"description", "Maximum rows (default 1000, max 10000)"},
{"default", 1000}, {"maximum", 10000}}},
}},
- {"required", nlohmann::json::array({"connection_id", "sql"})},
+ {"required", nlohmann::json::array({"connection_id"})},
};
}
MCPToolResult execute(const nlohmann::json& params, const MCPToolContext& ctx) override {
const std::string connectionId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string()) {
+ std::string sql;
+ if (params.contains("sql") && params["sql"].is_string()) {
+ sql = params["sql"].get<std::string>();
+ } else if (params.contains("query") && params["query"].is_string()) {
+ sql = params["query"].get<std::string>();
+ } else {
throw MCPToolError::invalidParameters("sql is required");
}
- const std::string sql = params["sql"].get<std::string>();
int rowLimit = 1000;
if (params.contains("row_limit") && params["row_limit"].is_number_integer()) {
rowLimit = std::clamp(params["row_limit"].get<int>(), 1, 10000);
@@ -59,11 +64,12 @@ public:
auto [adapter, config] = ctx.getAdapter(connectionId);
std::string finalSql = sql;
- std::string upper = toUpperAscii(sql);
- if (upper.find("LIMIT") == std::string::npos && isSQL(config.databaseType)) {
- std::string trimmed = trimSpaces(sql);
+ std::string trimmed = trimSpaces(sql);
+ std::string upper = toUpperAscii(trimmed);
+ bool isSelectOrWith = (upper.rfind("SELECT", 0) == 0 || upper.rfind("WITH", 0) == 0);
+ if (isSelectOrWith && upper.find("LIMIT") == std::string::npos && isSQL(config.databaseType)) {
while (!trimmed.empty() && trimmed.back() == ';') trimmed.pop_back();
- finalSql = trimmed + " LIMIT " + std::to_string(rowLimit);
+ finalSql = trimSpaces(trimmed) + " LIMIT " + std::to_string(rowLimit);
}
std::vector<RowValue> queryParams;
diff --git a/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp b/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
index cd7d09e..98f0af3 100644
--- a/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
@@ -37,18 +37,23 @@ public:
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
{"sql", {{"type", "string"}, {"description", "A single SQL statement"}}},
+ {"query", {{"type", "string"}, {"description", "A single SQL statement (alias for 'sql')"}}},
{"params", {{"type", "array"}, {"description", "Parameters for placeholders"}}},
}},
- {"required", nlohmann::json::array({"connection_id", "sql"})},
+ {"required", nlohmann::json::array({"connection_id"})},
};
}
MCPToolResult execute(const nlohmann::json& params, const MCPToolContext& ctx) override {
const std::string connectionId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string()) {
+ std::string sql;
+ if (params.contains("sql") && params["sql"].is_string()) {
+ sql = params["sql"].get<std::string>();
+ } else if (params.contains("query") && params["query"].is_string()) {
+ sql = params["query"].get<std::string>();
+ } else {
throw MCPToolError::invalidParameters("sql is required");
}
- const std::string sql = params["sql"].get<std::string>();
const std::string codeOnly = stripCommentsAndStrings(sql);
std::string upper = toUpperAscii(trimSpaces(codeOnly));
diff --git a/macos/Services/MCP/MCPServer.swift b/macos/Services/MCP/MCPServer.swift
index 3e42e75..ececb76 100644
--- a/macos/Services/MCP/MCPServer.swift
+++ b/macos/Services/MCP/MCPServer.swift
@@ -237,7 +237,7 @@ actor MCPServer: StdioTransportDelegate {
connectionType: connectionType,
client: client,
input: MCPAuditInput(
- sql: toolParams["sql"]?.stringValue,
+ sql: toolParams["sql"]?.stringValue ?? toolParams["query"]?.stringValue,
paramsCount: toolParams["params"]?.arrayValue?.count
),
result: MCPAuditResult(
@@ -283,7 +283,7 @@ actor MCPServer: StdioTransportDelegate {
connectionType: connectionType,
client: client,
input: MCPAuditInput(
- sql: toolParams["sql"]?.stringValue,
+ sql: toolParams["sql"]?.stringValue ?? toolParams["query"]?.stringValue,
paramsCount: toolParams["params"]?.arrayValue?.count
),
result: MCPAuditResult(status: .error, durationMs: durationMs),
diff --git a/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift b/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
index cd120e0..d069974 100644
--- a/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
@@ -20,15 +20,19 @@ struct ExplainQueryTool: MCPTool {
"sql": [
"type": "string",
"description": "SQL query to explain"
+ ],
+ "query": [
+ "type": "string",
+ "description": "SQL query to explain (alias for 'sql')"
]
],
- "required": ["connection_id", "sql"]
+ "required": ["connection_id"]
]
func execute(params: JSONValue, context: MCPToolContext) async throws -> MCPToolResult {
let connectionId = try extractConnectionId(from: params)
- guard let sql = params["sql"]?.stringValue else {
+ guard let sql = params["sql"]?.stringValue ?? params["query"]?.stringValue else {
throw MCPToolError.invalidParameters("sql is required")
}
diff --git a/macos/Services/MCP/Tools/Tier2/QueryTool.swift b/macos/Services/MCP/Tools/Tier2/QueryTool.swift
index 0507439..1835ee6 100644
--- a/macos/Services/MCP/Tools/Tier2/QueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier2/QueryTool.swift
@@ -21,6 +21,10 @@ struct QueryTool: MCPTool {
"type": "string",
"description": "SQL query. Use parameterized placeholders."
],
+ "query": [
+ "type": "string",
+ "description": "SQL query to execute (alias for 'sql')"
+ ],
"params": [
"type": "array",
"description": "Parameters for placeholders"
@@ -32,13 +36,13 @@ struct QueryTool: MCPTool {
"maximum": 10000
]
],
- "required": ["connection_id", "sql"]
+ "required": ["connection_id"]
]
func execute(params: JSONValue, context: MCPToolContext) async throws -> MCPToolResult {
let connectionId = try extractConnectionId(from: params)
- guard let sql = params["sql"]?.stringValue else {
+ guard let sql = params["sql"]?.stringValue ?? params["query"]?.stringValue else {
throw MCPToolError.invalidParameters("sql is required")
}
@@ -61,11 +65,13 @@ struct QueryTool: MCPTool {
let (adapter, config) = try await context.getAdapter(for: connectionId)
- // Apply row limit if not already present
+ // Apply row limit if not already present (only for SELECT / WITH queries)
var limitedSQL = sql
- let upperSQL = sql.uppercased()
- if !upperSQL.contains("LIMIT") && config.databaseType.isSQL {
- limitedSQL = "\(sql.trimmingCharacters(in: .whitespacesAndNewlines).trimmingCharacters(in: CharacterSet(charactersIn: ";"))) LIMIT \(rowLimit)"
+ let trimmedSQL = sql.trimmingCharacters(in: .whitespacesAndNewlines)
+ let upperSQL = trimmedSQL.uppercased()
+ let isSelectOrWith = upperSQL.hasPrefix("SELECT") || upperSQL.hasPrefix("WITH")
+ if isSelectOrWith && !upperSQL.contains("LIMIT") && config.databaseType.isSQL {
+ limitedSQL = "\(trimmedSQL.trimmingCharacters(in: CharacterSet(charactersIn: "; \t\r\n"))) LIMIT \(rowLimit)"
}
// Extract parameters
diff --git a/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift b/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift
index 2f46bdb..67446d1 100644
--- a/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift
@@ -21,18 +21,22 @@ struct ExecuteWriteQueryTool: MCPTool {
"type": "string",
"description": "A single SQL statement to execute. Must not contain multiple statements."
],
+ "query": [
+ "type": "string",
+ "description": "A single SQL statement to execute (alias for 'sql')"
+ ],
"params": [
"type": "array",
"description": "Parameters for placeholders"
]
],
- "required": ["connection_id", "sql"]
+ "required": ["connection_id"]
]
func execute(params: JSONValue, context: MCPToolContext) async throws -> MCPToolResult {
let connectionId = try extractConnectionId(from: params)
- guard let sql = params["sql"]?.stringValue else {
+ guard let sql = params["sql"]?.stringValue ?? params["query"]?.stringValue else {
throw MCPToolError.invalidParameters("sql is required")
}
diff --git a/windows/Gridex/Services/MCP/MCPServer.cpp b/windows/Gridex/Services/MCP/MCPServer.cpp
index aa1652c..dcc230b 100644
--- a/windows/Gridex/Services/MCP/MCPServer.cpp
+++ b/windows/Gridex/Services/MCP/MCPServer.cpp
@@ -277,12 +277,18 @@ namespace DBModels
audit.connectionId = connIdStr;
audit.connectionType = connTypeStr;
+ std::string sqlInput;
if (args.contains("sql") && args["sql"].is_string())
+ sqlInput = args["sql"].get<std::string>();
+ else if (args.contains("query") && args["query"].is_string())
+ sqlInput = args["query"].get<std::string>();
+
+ if (!sqlInput.empty())
{
std::optional<int> pc;
if (args.contains("params") && args["params"].is_array())
pc = static_cast<int>(args["params"].size());
- audit.input = MCPAuditInput::fromSQL(args["sql"].get<std::string>(), pc);
+ audit.input = MCPAuditInput::fromSQL(sqlInput, pc);
}
try
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
index 3321f32..da9b565 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
@@ -11,9 +11,13 @@ namespace DBModels
{
const auto connId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string())
+ std::string sqlUtf8;
+ if (params.contains("sql") && params["sql"].is_string())
+ sqlUtf8 = params["sql"].get<std::string>();
+ else if (params.contains("query") && params["query"].is_string())
+ sqlUtf8 = params["query"].get<std::string>();
+ else
throw MCPToolError::invalidParameters("sql is required");
- const auto sqlUtf8 = params["sql"].get<std::string>();
const auto perm = ctx.checkPermission(tier(), connId);
if (const auto* msg = perm.errorMessage())
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
index 26bd531..22ebcb9 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
@@ -25,9 +25,10 @@ namespace DBModels
{"type", "object"},
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
- {"sql", {{"type", "string"}, {"description", "SQL query to explain"}}}
+ {"sql", {{"type", "string"}, {"description", "SQL query to explain"}}},
+ {"query", {{"type", "string"}, {"description", "SQL query to explain (alias for 'sql')"}}}
}},
- {"required", {"connection_id", "sql"}}
+ {"required", {"connection_id"}}
};
}
MCPToolResult execute(const nlohmann::json& params,
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
index a4cd9d1..bb0ac67 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
@@ -23,9 +23,13 @@ namespace DBModels
{
const auto connId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string())
+ std::string sqlUtf8;
+ if (params.contains("sql") && params["sql"].is_string())
+ sqlUtf8 = params["sql"].get<std::string>();
+ else if (params.contains("query") && params["query"].is_string())
+ sqlUtf8 = params["query"].get<std::string>();
+ else
throw MCPToolError::invalidParameters("sql is required");
- const std::string sqlUtf8 = params["sql"].get<std::string>();
int rowLimit = 1000;
if (params.contains("row_limit") && params["row_limit"].is_number_integer())
@@ -47,24 +51,25 @@ namespace DBModels
auto [adapter, config] = ctx.getAdapter(connId);
- // Append LIMIT if absent and target is SQL. Keep the raw
- // sql untouched otherwise (Redis/Mongo adapters carry their
- // own shape).
+ // Append LIMIT if absent and target is a SELECT/WITH SQL statement.
+ // Keep the raw sql untouched otherwise (SHOW, DESCRIBE, EXPLAIN, Redis/Mongo
+ // adapters carry their own shape).
std::wstring sqlW = MCPToolHelpers::fromUtf8(sqlUtf8);
if (acceptsLimitSuffix(config.databaseType))
{
- std::wstring upper = sqlW;
+ std::wstring trimmed = sqlW;
+ size_t startPos = trimmed.find_first_not_of(L" \t\r\n");
+ if (startPos != std::wstring::npos)
+ trimmed = trimmed.substr(startPos);
+
+ std::wstring upper = trimmed;
std::transform(upper.begin(), upper.end(), upper.begin(), ::towupper);
- // `upper.find(L"LIMIT")` also catches the substring inside
- // a column name or comment, but since the caller sees a
- // working query either way this false-positive only costs
- // them a missing extra LIMIT clause — safer than double
- // LIMIT. String sanitizer already ran upstream.
- if (upper.find(L"LIMIT") == std::wstring::npos)
+
+ bool isSelectOrWith = (upper.rfind(L"SELECT", 0) == 0 || upper.rfind(L"WITH", 0) == 0);
+ if (isSelectOrWith && upper.find(L"LIMIT") == std::wstring::npos)
{
- std::wstring trimmed = sqlW;
while (!trimmed.empty() && (trimmed.back() == L' ' ||
- trimmed.back() == L';' || trimmed.back() == L'\n'))
+ trimmed.back() == L';' || trimmed.back() == L'\n' || trimmed.back() == L'\r' || trimmed.back() == L'\t'))
trimmed.pop_back();
sqlW = trimmed + L" LIMIT " + std::to_wstring(rowLimit);
}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
index f0bb4dd..516ef74 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
@@ -29,13 +29,14 @@ namespace DBModels
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
{"sql", {{"type", "string"}, {"description", "SQL query to execute"}}},
+ {"query", {{"type", "string"}, {"description", "SQL query to execute (alias for 'sql')"}}},
{"row_limit", {
{"type", "integer"},
{"description", "Maximum rows to return (default 1000, max 10000)"},
{"default", 1000}, {"maximum", 10000}
}}
}},
- {"required", {"connection_id", "sql"}}
+ {"required", {"connection_id"}}
};
}
MCPToolResult execute(const nlohmann::json& params,
diff --git a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
index 0e955bf..1c40a64 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
+++ b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
@@ -36,9 +36,13 @@ namespace DBModels
{
const auto connId = MCPTool::extractConnectionId(params);
- if (!params.contains("sql") || !params["sql"].is_string())
+ std::string sqlUtf8;
+ if (params.contains("sql") && params["sql"].is_string())
+ sqlUtf8 = params["sql"].get<std::string>();
+ else if (params.contains("query") && params["query"].is_string())
+ sqlUtf8 = params["query"].get<std::string>();
+ else
throw MCPToolError::invalidParameters("sql is required");
- const auto sqlUtf8 = params["sql"].get<std::string>();
// Sanitize + multi-statement guard. Run syntactic checks
// against a comment/literal-stripped copy so payloads
diff --git a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
index 16b3ed5..5a4f019 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
@@ -26,9 +26,10 @@ namespace DBModels
{"type", "object"},
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
- {"sql", {{"type", "string"}, {"description", "A single write SQL statement"}}}
+ {"sql", {{"type", "string"}, {"description", "A single write SQL statement"}}},
+ {"query", {{"type", "string"}, {"description", "A single write SQL statement (alias for 'sql')"}}}
}},
- {"required", {"connection_id", "sql"}}
+ {"required", {"connection_id"}}
};
}
MCPToolResult execute(const nlohmann::json& params,