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++

fix(mcp): harden auto LIMIT detection and address review feedback

Tobieb3b17612bb1
diff --git a/.gitignore b/.gitignore
index f3bb875..d51095e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -108,5 +108,3 @@ windows/Gridex/GridexVersion.generated.h
dist
.wangler
.env.example
-
-.tgrep/
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 870d626..d401512 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ All notable changes to Gridex are documented in this file. Gridex ships native a
- **Redis namespace key tree (macOS)** — Redis keys can be browsed as a collapsible sidebar tree using a local configurable delimiter (default `:`). Leaf keys open the existing detail view; the flat SCAN-filtered key list remains available.
### Fixed
+- **MCP query tools** — `query`, `explain_query` and `execute_write_query` accept `query` as an alias for `sql`; auto `LIMIT` is applied only to `SELECT` / `WITH` statements so `SHOW` / `DESCRIBE` / `PRAGMA` no longer break.
+- **Windows schema fallback** — MySQL and PostgreSQL adapters fall back to the current database / `public` when no schema is passed.
- **macOS data grids** — Reloading data with Command-R retains the selected column and ascending/descending server-side sort.
- **macOS tabs** — Clicking a tab reliably activates it; Control-Tab and Control-Shift-Tab cycle forward/backward; tab close controls retain their expected left/middle-click behavior.
- **macOS MySQL passwordless accounts** — Blank passwords now authenticate correctly for non-TLS caching_sha2_password accounts.
diff --git a/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp b/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
index 0c9006e..ebb52a4 100644
--- a/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
@@ -17,7 +17,7 @@ public:
{"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. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "SQL query to explain (alias for 'sql')"}}},
}},
{"required", nlohmann::json::array({"connection_id"})},
diff --git a/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp b/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
index 6668131..e385c51 100644
--- a/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier2/QueryTool.cpp
@@ -1,6 +1,7 @@
#include <algorithm>
#include <chrono>
+#include "Services/MCP/Security/SQLSanitizer.h"
#include "Services/MCP/Tools/MCPTool.h"
#include "Services/MCP/Tools/ToolHelpers.h"
@@ -22,7 +23,7 @@ public:
{"type", "object"},
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
- {"sql", {{"type", "string"}, {"description", "SQL query"}}},
+ {"sql", {{"type", "string"}, {"description", "SQL query. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "SQL query (alias for 'sql')"}}},
{"params", {{"type", "array"}, {"description", "Parameters for placeholders"}}},
{"row_limit", {{"type", "integer"},
@@ -63,13 +64,18 @@ public:
auto [adapter, config] = ctx.getAdapter(connectionId);
+ // Only SELECT / WITH get an auto LIMIT (SHOW / DESCRIBE / PRAGMA would
+ // break). Classify on the comment/literal-stripped text so a leading
+ // comment or "(" doesn't skip the limit.
std::string finalSql = sql;
- std::string trimmed = trimSpaces(sql);
- std::string upper = toUpperAscii(trimmed);
+ std::string code = trimSpaces(stripCommentsAndStrings(sql));
+ auto codeStart = code.find_first_not_of("( \t\r\n");
+ std::string upper = toUpperAscii(codeStart == std::string::npos ? "" : code.substr(codeStart));
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 = trimSpaces(trimmed) + " LIMIT " + std::to_string(rowLimit);
+ std::string trimmed = trimSpaces(sql);
+ while (!trimmed.empty() && trimmed.back() == ';') trimmed = trimSpaces(trimmed.substr(0, trimmed.size() - 1));
+ finalSql = 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 98f0af3..6c8155b 100644
--- a/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
+++ b/linux/src/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.cpp
@@ -36,7 +36,7 @@ public:
{"type", "object"},
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
- {"sql", {{"type", "string"}, {"description", "A single SQL statement"}}},
+ {"sql", {{"type", "string"}, {"description", "A single SQL statement. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "A single SQL statement (alias for 'sql')"}}},
{"params", {{"type", "array"}, {"description", "Parameters for placeholders"}}},
}},
diff --git a/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift b/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
index d069974..fa44ca4 100644
--- a/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier2/ExplainQueryTool.swift
@@ -19,7 +19,7 @@ struct ExplainQueryTool: MCPTool {
],
"sql": [
"type": "string",
- "description": "SQL query to explain"
+ "description": "SQL query to explain. Required unless 'query' is provided"
],
"query": [
"type": "string",
diff --git a/macos/Services/MCP/Tools/Tier2/QueryTool.swift b/macos/Services/MCP/Tools/Tier2/QueryTool.swift
index 1835ee6..238abca 100644
--- a/macos/Services/MCP/Tools/Tier2/QueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier2/QueryTool.swift
@@ -19,7 +19,7 @@ struct QueryTool: MCPTool {
],
"sql": [
"type": "string",
- "description": "SQL query. Use parameterized placeholders."
+ "description": "SQL query. Use parameterized placeholders. Required unless 'query' is provided"
],
"query": [
"type": "string",
@@ -65,13 +65,16 @@ struct QueryTool: MCPTool {
let (adapter, config) = try await context.getAdapter(for: connectionId)
- // Apply row limit if not already present (only for SELECT / WITH queries)
+ // Apply row limit if not already present (only for SELECT / WITH queries).
+ // Classify on the comment/literal-stripped text so a leading comment
+ // or "(" doesn't skip the limit.
var limitedSQL = sql
- let trimmedSQL = sql.trimmingCharacters(in: .whitespacesAndNewlines)
- let upperSQL = trimmedSQL.uppercased()
+ let upperSQL = MCPSQLSanitizer.stripCommentsAndStrings(sql)
+ .trimmingCharacters(in: CharacterSet(charactersIn: "( \t\r\n"))
+ .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)"
+ limitedSQL = "\(sql.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 67446d1..a0799d4 100644
--- a/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift
+++ b/macos/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.swift
@@ -19,7 +19,7 @@ struct ExecuteWriteQueryTool: MCPTool {
],
"sql": [
"type": "string",
- "description": "A single SQL statement to execute. Must not contain multiple statements."
+ "description": "A single SQL statement to execute. Must not contain multiple statements. Required unless 'query' is provided"
],
"query": [
"type": "string",
diff --git a/setup-vcpkg.bat b/setup-vcpkg.bat
index 34e7b2f..b783ea2 100644
--- a/setup-vcpkg.bat
+++ b/setup-vcpkg.bat
@@ -8,10 +8,10 @@ echo ========================================================
if not exist "C:\vcpkg" (
echo [1/3] Cloning vcpkg to C:\vcpkg...
git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
- if %ERRORLEVEL% NEQ 0 (
+ if !ERRORLEVEL! NEQ 0 (
echo [ERROR] Failed to clone vcpkg. Please check your internet connection or git installation.
pause
- exit /b %ERRORLEVEL%
+ exit /b !ERRORLEVEL!
)
) else (
echo [1/3] C:\vcpkg already exists.
@@ -20,10 +20,10 @@ if not exist "C:\vcpkg" (
if not exist "C:\vcpkg\vcpkg.exe" (
echo [2/3] Bootstrapping vcpkg...
call C:\vcpkg\bootstrap-vcpkg.bat
- if %ERRORLEVEL% NEQ 0 (
+ if !ERRORLEVEL! NEQ 0 (
echo [ERROR] Failed to bootstrap vcpkg.
pause
- exit /b %ERRORLEVEL%
+ exit /b !ERRORLEVEL!
)
) else (
echo [2/3] vcpkg.exe is ready.
@@ -33,11 +33,11 @@ echo [3/3] Installing required libraries (sqlite3, libpq, libmariadb, openssl, e
echo This may take a few minutes...
C:\vcpkg\vcpkg.exe install sqlite3 libpq libmariadb openssl hiredis libssh2 nlohmann-json cpp-httplib mongo-cxx-driver --triplet x64-windows
-if %ERRORLEVEL% NEQ 0 (
+if !ERRORLEVEL! NEQ 0 (
echo.
echo [ERROR] vcpkg package installation failed!
pause
- exit /b %ERRORLEVEL%
+ exit /b !ERRORLEVEL!
)
echo.
diff --git a/windows/Gridex/MySQLAdapter.cpp b/windows/Gridex/MySQLAdapter.cpp
index ed2bc1a..384fc28 100644
--- a/windows/Gridex/MySQLAdapter.cpp
+++ b/windows/Gridex/MySQLAdapter.cpp
@@ -52,7 +52,9 @@ namespace DBModels
std::wstring MySQLAdapter::resolveSchema(const std::wstring& schema)
{
if (!schema.empty()) return schema;
- return currentDatabase();
+ // DATABASE() is NULL when no default database is selected.
+ auto db = currentDatabase();
+ return isNullCell(db) ? L"" : db;
}
std::string MySQLAdapter::qualifyTable(const std::wstring& schema, const std::wstring& table)
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
index 22ebcb9..0de5fa2 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
@@ -25,7 +25,7 @@ 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. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "SQL query to explain (alias for 'sql')"}}}
}},
{"required", {"connection_id"}}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
index bb0ac67..3b23629 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.cpp
@@ -4,6 +4,7 @@
#include "QueryTool.h"
#include "../MCPToolHelpers.h"
+#include "../../Security/MCPSQLSanitizer.h"
#include <algorithm>
namespace DBModels
@@ -57,17 +58,18 @@ namespace DBModels
std::wstring sqlW = MCPToolHelpers::fromUtf8(sqlUtf8);
if (acceptsLimitSuffix(config.databaseType))
{
- 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;
+ // Classify on the comment/literal-stripped text so a leading
+ // comment or "(" doesn't skip the limit.
+ std::wstring upper = MCPToolHelpers::fromUtf8(
+ MCPSQLSanitizer::stripCommentsAndStrings(sqlUtf8));
+ size_t startPos = upper.find_first_not_of(L"( \t\r\n");
+ upper = (startPos == std::wstring::npos) ? L"" : upper.substr(startPos);
std::transform(upper.begin(), upper.end(), upper.begin(), ::towupper);
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'\r' || trimmed.back() == L'\t'))
trimmed.pop_back();
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
index 516ef74..db5a410 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/QueryTool.h
@@ -28,7 +28,7 @@ namespace DBModels
{"type", "object"},
{"properties", {
{"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
- {"sql", {{"type", "string"}, {"description", "SQL query to execute"}}},
+ {"sql", {{"type", "string"}, {"description", "SQL query to execute. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "SQL query to execute (alias for 'sql')"}}},
{"row_limit", {
{"type", "integer"},
diff --git a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
index 5a4f019..af0433a 100644
--- a/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
+++ b/windows/Gridex/Services/MCP/Tools/Tier3/ExecuteWriteQueryTool.h
@@ -26,7 +26,7 @@ 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. Required unless 'query' is provided"}}},
{"query", {{"type", "string"}, {"description", "A single write SQL statement (alias for 'sql')"}}}
}},
{"required", {"connection_id"}}