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): Round 1A — 5 more Tier-1/2 tools (9 total)

DangHuynhc814240b5164
diff --git a/windows/Gridex/Gridex.items.props b/windows/Gridex/Gridex.items.props
index 3913708..9a2dc47 100644
--- a/windows/Gridex/Gridex.items.props
+++ b/windows/Gridex/Gridex.items.props
@@ -53,7 +53,12 @@
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListConnectionsTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListTablesTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\DescribeTableTool.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListSchemasTool.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListRelationshipsTool.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\GetSampleRowsTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\QueryTool.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\ExplainQueryTool.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\SearchAcrossTablesTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\MCPServerHost.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Models\DatabaseAdapter.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Models\ConnectionManager.h" />
@@ -351,9 +356,24 @@
<ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\DescribeTableTool.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListSchemasTool.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\ListRelationshipsTool.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier1\GetSampleRowsTool.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\QueryTool.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\ExplainQueryTool.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier2\SearchAcrossTablesTool.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\MCPServerHost.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
diff --git a/windows/Gridex/Services/MCP/Tools/MCPBuiltinTools.cpp b/windows/Gridex/Services/MCP/Tools/MCPBuiltinTools.cpp
index a8ebff1..266c8a3 100644
--- a/windows/Gridex/Services/MCP/Tools/MCPBuiltinTools.cpp
+++ b/windows/Gridex/Services/MCP/Tools/MCPBuiltinTools.cpp
@@ -1,31 +1,38 @@
//
// MCPBuiltinTools.cpp
//
-// Phase 5 shipped tools. Remaining (list_schemas, list_relationships,
-// get_sample_rows, explain_query, search_across_tables, insert_rows,
-// update_rows, delete_rows, execute_write_query) land in later sprints.
+// All Phase 5 read/schema tools. Tier-3 write tools (insert_rows,
+// update_rows, delete_rows, execute_write_query) land in Round 1B.
#include "MCPBuiltinTools.h"
#include "Tier1/ListConnectionsTool.h"
#include "Tier1/ListTablesTool.h"
#include "Tier1/DescribeTableTool.h"
+#include "Tier1/ListSchemasTool.h"
+#include "Tier1/ListRelationshipsTool.h"
+#include "Tier1/GetSampleRowsTool.h"
#include "Tier2/QueryTool.h"
+#include "Tier2/ExplainQueryTool.h"
+#include "Tier2/SearchAcrossTablesTool.h"
namespace DBModels { namespace MCPBuiltinTools {
void registerAll(MCPToolRegistry& r)
{
- // Tier 1 — schema introspection (always on for non-Locked).
+ // Tier 1 — schema introspection (available in any non-Locked mode).
r.registerTool(std::make_shared<ListConnectionsTool>());
r.registerTool(std::make_shared<ListTablesTool>());
r.registerTool(std::make_shared<DescribeTableTool>());
+ r.registerTool(std::make_shared<ListSchemasTool>());
+ r.registerTool(std::make_shared<ListRelationshipsTool>());
+ r.registerTool(std::make_shared<GetSampleRowsTool>());
- // Tier 2 — read queries (ReadOnly mode allowed, ReadWrite too).
+ // Tier 2 — read queries (ReadOnly allowed, ReadWrite too).
r.registerTool(std::make_shared<QueryTool>());
+ r.registerTool(std::make_shared<ExplainQueryTool>());
+ r.registerTool(std::make_shared<SearchAcrossTablesTool>());
- // Tier 3 — write tools (TODO: insert_rows, update_rows,
- // delete_rows, execute_write_query). Until they land Tier 3 is
- // surfaced via the AI as unavailable.
+ // Tier 3 — write tools arrive in Round 1B.
}
}} // namespace
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.cpp
new file mode 100644
index 0000000..ef606a1
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.cpp
@@ -0,0 +1,70 @@
+//
+// GetSampleRowsTool.cpp
+//
+
+#include "GetSampleRowsTool.h"
+#include "../MCPToolHelpers.h"
+#include <algorithm>
+
+namespace DBModels
+{
+ MCPToolResult GetSampleRowsTool::execute(const nlohmann::json& params, MCPToolContext& ctx)
+ {
+ const auto connId = MCPTool::extractConnectionId(params);
+
+ if (!params.contains("table_name") || !params["table_name"].is_string())
+ throw MCPToolError::invalidParameters("table_name is required");
+ const auto tableUtf8 = params["table_name"].get<std::string>();
+ const std::wstring tableW = MCPToolHelpers::fromUtf8(tableUtf8);
+
+ const std::wstring schemaW = (params.contains("schema") && params["schema"].is_string())
+ ? MCPToolHelpers::fromUtf8(params["schema"].get<std::string>()) : std::wstring{};
+
+ int limit = 10;
+ if (params.contains("limit") && params["limit"].is_number_integer())
+ limit = (std::min)(100, (std::max)(1, params["limit"].get<int>()));
+
+ const auto perm = ctx.checkPermission(tier(), connId);
+ if (const auto* msg = perm.errorMessage())
+ throw MCPToolError::permissionDenied(*msg);
+
+ auto [adapter, config] = ctx.getAdapter(connId);
+
+ // fetchRows signature: (table, schema, limit, offset, orderBy, ascending).
+ QueryResult r = adapter->fetchRows(tableW, schemaW, limit, 0, L"", true);
+ if (!r.success)
+ throw MCPToolError::queryFailed(MCPToolHelpers::toUtf8(r.error));
+
+ if (r.rows.empty())
+ return MCPToolResult::text("Table '" + tableUtf8 + "' is empty.");
+
+ // Columns header summary.
+ std::string cols = "Columns: ";
+ for (size_t i = 0; i < r.columnNames.size(); ++i)
+ {
+ if (i) cols += ", ";
+ cols += MCPToolHelpers::toUtf8(r.columnNames[i]);
+ if (i < r.columnTypes.size())
+ cols += " (" + MCPToolHelpers::toUtf8(r.columnTypes[i]) + ")";
+ }
+
+ nlohmann::json rows = nlohmann::json::array();
+ for (const auto& row : r.rows)
+ {
+ nlohmann::json o = nlohmann::json::object();
+ for (const auto& cname : r.columnNames)
+ {
+ auto it = row.find(cname);
+ const std::wstring v = (it != row.end()) ? it->second : L"";
+ o[MCPToolHelpers::toUtf8(cname)] =
+ isNullCell(v) ? nlohmann::json(nullptr)
+ : nlohmann::json(MCPToolHelpers::toUtf8(v));
+ }
+ rows.push_back(std::move(o));
+ }
+
+ return MCPToolResult::text(
+ "Sample " + std::to_string(r.rows.size()) + " row(s) from '" +
+ tableUtf8 + "':\n" + cols + "\n\n" + rows.dump(2));
+ }
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.h b/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.h
new file mode 100644
index 0000000..91941a7
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/GetSampleRowsTool.h
@@ -0,0 +1,38 @@
+#pragma once
+//
+// GetSampleRowsTool.h — Return up to N rows from a table so the
+// AI can understand the data shape. Mirrors macOS
+// GetSampleRowsTool.swift. Defaults to 10, hard-caps at 100.
+
+#include "../MCPTool.h"
+
+namespace DBModels
+{
+ class GetSampleRowsTool : public MCPTool
+ {
+ public:
+ std::string name() const override { return "get_sample_rows"; }
+ std::string description() const override
+ {
+ return "Get sample rows from a table so the AI can see the "
+ "data shape. Default limit 10, max 100.";
+ }
+ MCPPermissionTier tier() const override { return MCPPermissionTier::Schema; }
+ nlohmann::json inputSchema() const override
+ {
+ return {
+ {"type", "object"},
+ {"properties", {
+ {"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
+ {"table_name", {{"type", "string"}, {"description", "Name of the table"}}},
+ {"schema", {{"type", "string"}, {"description", "Optional schema"}}},
+ {"limit", {{"type", "integer"}, {"default", 10},
+ {"minimum", 1}, {"maximum", 100}}}
+ }},
+ {"required", {"connection_id", "table_name"}}
+ };
+ }
+ MCPToolResult execute(const nlohmann::json& params,
+ MCPToolContext& ctx) override;
+ };
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.cpp
new file mode 100644
index 0000000..55fe51c
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.cpp
@@ -0,0 +1,67 @@
+//
+// ListRelationshipsTool.cpp
+//
+
+#include "ListRelationshipsTool.h"
+#include "../MCPToolHelpers.h"
+
+namespace DBModels
+{
+ MCPToolResult ListRelationshipsTool::execute(const nlohmann::json& params, MCPToolContext& ctx)
+ {
+ const auto connId = MCPTool::extractConnectionId(params);
+
+ if (!params.contains("table_name") || !params["table_name"].is_string())
+ throw MCPToolError::invalidParameters("table_name is required");
+ const auto tableUtf8 = params["table_name"].get<std::string>();
+ const std::wstring tableW = MCPToolHelpers::fromUtf8(tableUtf8);
+
+ const std::wstring schemaW = (params.contains("schema") && params["schema"].is_string())
+ ? MCPToolHelpers::fromUtf8(params["schema"].get<std::string>()) : std::wstring{};
+
+ const auto perm = ctx.checkPermission(tier(), connId);
+ if (const auto* msg = perm.errorMessage())
+ throw MCPToolError::permissionDenied(*msg);
+
+ auto [adapter, config] = ctx.getAdapter(connId);
+
+ nlohmann::json result;
+ result["table"] = tableUtf8;
+
+ // Outgoing: this table's FKs pointing at others.
+ nlohmann::json out = nlohmann::json::array();
+ for (const auto& fk : adapter->listForeignKeys(tableW, schemaW))
+ {
+ out.push_back({
+ {"name", MCPToolHelpers::toUtf8(fk.name)},
+ {"columns", MCPToolHelpers::toUtf8(fk.column)},
+ {"references_table", MCPToolHelpers::toUtf8(fk.referencedTable)},
+ {"references_columns",MCPToolHelpers::toUtf8(fk.referencedColumn)}
+ });
+ }
+ result["outgoing"] = out.empty() ? nlohmann::json("None") : out;
+
+ // Incoming: walk all tables and grab FKs whose target is this
+ // table. Linear in total-tables * avg-FKs-per-table; acceptable
+ // for typical schemas, and the MCP call is one-shot.
+ nlohmann::json in = nlohmann::json::array();
+ for (const auto& t : adapter->listTables(schemaW))
+ {
+ if (t.name == tableW) continue;
+ for (const auto& fk : adapter->listForeignKeys(t.name, schemaW))
+ {
+ if (fk.referencedTable == tableW)
+ {
+ in.push_back({
+ {"from_table", MCPToolHelpers::toUtf8(t.name)},
+ {"from_columns", MCPToolHelpers::toUtf8(fk.column)},
+ {"to_columns", MCPToolHelpers::toUtf8(fk.referencedColumn)}
+ });
+ }
+ }
+ }
+ result["incoming"] = in.empty() ? nlohmann::json("None") : in;
+
+ return MCPToolResult::text(result.dump(2));
+ }
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.h b/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.h
new file mode 100644
index 0000000..566ffe5
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/ListRelationshipsTool.h
@@ -0,0 +1,40 @@
+#pragma once
+//
+// ListRelationshipsTool.h — FK graph for a table: outgoing (this
+// table references others) + incoming (others reference this).
+// Mirrors macOS ListRelationshipsTool.swift.
+//
+// Incoming is O(N_tables * M_FKs) — we scan every table's FK set.
+// Fine for <200 tables; above that, add a schema filter param.
+
+#include "../MCPTool.h"
+
+namespace DBModels
+{
+ class ListRelationshipsTool : public MCPTool
+ {
+ public:
+ std::string name() const override { return "list_relationships"; }
+ std::string description() const override
+ {
+ return "List foreign-key relationships for a table. Returns both "
+ "outgoing (this -> other) and incoming (other -> this) "
+ "references.";
+ }
+ MCPPermissionTier tier() const override { return MCPPermissionTier::Schema; }
+ nlohmann::json inputSchema() const override
+ {
+ return {
+ {"type", "object"},
+ {"properties", {
+ {"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
+ {"table_name", {{"type", "string"}, {"description", "Name of the table"}}},
+ {"schema", {{"type", "string"}, {"description", "Optional schema"}}}
+ }},
+ {"required", {"connection_id", "table_name"}}
+ };
+ }
+ MCPToolResult execute(const nlohmann::json& params,
+ MCPToolContext& ctx) override;
+ };
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.cpp
new file mode 100644
index 0000000..4be8098
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.cpp
@@ -0,0 +1,45 @@
+//
+// ListSchemasTool.cpp
+//
+
+#include "ListSchemasTool.h"
+#include "../MCPToolHelpers.h"
+
+namespace DBModels
+{
+ static bool supportsSchemas(DatabaseType t)
+ {
+ return t == DatabaseType::PostgreSQL || t == DatabaseType::MSSQLServer;
+ }
+
+ MCPToolResult ListSchemasTool::execute(const nlohmann::json& params, MCPToolContext& ctx)
+ {
+ const auto connId = MCPTool::extractConnectionId(params);
+
+ const auto perm = ctx.checkPermission(tier(), connId);
+ if (const auto* msg = perm.errorMessage())
+ throw MCPToolError::permissionDenied(*msg);
+
+ auto [adapter, config] = ctx.getAdapter(connId);
+
+ nlohmann::json arr = nlohmann::json::array();
+ std::wstring kind = L"Schemas";
+ if (supportsSchemas(config.databaseType))
+ {
+ for (const auto& s : adapter->listSchemas())
+ arr.push_back(MCPToolHelpers::toUtf8(s));
+ }
+ else
+ {
+ kind = L"Databases";
+ for (const auto& d : adapter->listDatabases())
+ arr.push_back(MCPToolHelpers::toUtf8(d));
+ }
+
+ if (arr.empty())
+ return MCPToolResult::text("No " + MCPToolHelpers::toUtf8(kind) + " found.");
+
+ return MCPToolResult::text(
+ MCPToolHelpers::toUtf8(kind) + ": " + arr.dump());
+ }
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.h b/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.h
new file mode 100644
index 0000000..271da2e
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier1/ListSchemasTool.h
@@ -0,0 +1,34 @@
+#pragma once
+//
+// ListSchemasTool.h — List schemas (Postgres/MSSQL) or databases
+// (MySQL / Redis) for a connection. Mirrors
+// macos/Services/MCP/Tools/Tier1/ListSchemasTool.swift.
+
+#include "../MCPTool.h"
+
+namespace DBModels
+{
+ class ListSchemasTool : public MCPTool
+ {
+ public:
+ std::string name() const override { return "list_schemas"; }
+ std::string description() const override
+ {
+ return "List schemas (PostgreSQL / MSSQL) or databases "
+ "(MySQL / MongoDB) available on a connection.";
+ }
+ MCPPermissionTier tier() const override { return MCPPermissionTier::Schema; }
+ nlohmann::json inputSchema() const override
+ {
+ return {
+ {"type", "object"},
+ {"properties", {
+ {"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}}
+ }},
+ {"required", {"connection_id"}}
+ };
+ }
+ MCPToolResult execute(const nlohmann::json& params,
+ MCPToolContext& ctx) override;
+ };
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
new file mode 100644
index 0000000..3321f32
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.cpp
@@ -0,0 +1,79 @@
+//
+// ExplainQueryTool.cpp
+//
+
+#include "ExplainQueryTool.h"
+#include "../MCPToolHelpers.h"
+
+namespace DBModels
+{
+ MCPToolResult ExplainQueryTool::execute(const nlohmann::json& params, MCPToolContext& ctx)
+ {
+ const auto connId = MCPTool::extractConnectionId(params);
+
+ if (!params.contains("sql") || !params["sql"].is_string())
+ 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())
+ throw MCPToolError::permissionDenied(*msg);
+
+ auto [adapter, config] = ctx.getAdapter(connId);
+ const std::wstring sqlW = MCPToolHelpers::fromUtf8(sqlUtf8);
+
+ std::wstring explainSQL;
+ switch (config.databaseType)
+ {
+ case DatabaseType::PostgreSQL:
+ explainSQL = L"EXPLAIN (ANALYZE false, COSTS true, FORMAT TEXT) " + sqlW;
+ break;
+ case DatabaseType::MySQL:
+ explainSQL = L"EXPLAIN " + sqlW;
+ break;
+ case DatabaseType::SQLite:
+ explainSQL = L"EXPLAIN QUERY PLAN " + sqlW;
+ break;
+ case DatabaseType::MSSQLServer:
+ // SET SHOWPLAN_TEXT is a session flag — bracket the
+ // query with it. Multi-statement; the adapter passes
+ // this through to the driver unchanged.
+ explainSQL = L"SET SHOWPLAN_TEXT ON; " + sqlW + L"; SET SHOWPLAN_TEXT OFF";
+ break;
+ default:
+ return MCPToolResult::error(
+ "EXPLAIN is not supported for " +
+ MCPToolHelpers::toUtf8(DatabaseTypeDisplayName(config.databaseType)) +
+ " connections.");
+ }
+
+ QueryResult r;
+ try { r = adapter->execute(explainSQL); }
+ catch (const std::exception& e)
+ { throw MCPToolError::queryFailed(e.what()); }
+ if (!r.success) throw MCPToolError::queryFailed(MCPToolHelpers::toUtf8(r.error));
+
+ std::string out = "Query Plan for: " + sqlUtf8 + "\n\n";
+ if (r.rows.empty())
+ {
+ out += "No plan information available.";
+ }
+ else
+ {
+ for (const auto& row : r.rows)
+ {
+ bool first = true;
+ for (const auto& cname : r.columnNames)
+ {
+ auto it = row.find(cname);
+ if (it == row.end()) continue;
+ if (!first) out += " | ";
+ first = false;
+ out += MCPToolHelpers::toUtf8(it->second);
+ }
+ out += "\n";
+ }
+ }
+ return MCPToolResult::text(out);
+ }
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
new file mode 100644
index 0000000..26bd531
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/ExplainQueryTool.h
@@ -0,0 +1,36 @@
+#pragma once
+//
+// ExplainQueryTool.h — Return the EXPLAIN plan for a SQL query
+// without executing it. Mirrors macOS ExplainQueryTool.swift.
+// Dialect-specific prefix: EXPLAIN for PG/MySQL, EXPLAIN QUERY PLAN
+// for SQLite, SHOWPLAN_TEXT for MSSQL. Redis/Mongo return an error.
+
+#include "../MCPTool.h"
+
+namespace DBModels
+{
+ class ExplainQueryTool : public MCPTool
+ {
+ public:
+ std::string name() const override { return "explain_query"; }
+ std::string description() const override
+ {
+ return "Get the EXPLAIN plan for a SQL query without executing "
+ "it. Helps the AI reason about performance.";
+ }
+ MCPPermissionTier tier() const override { return MCPPermissionTier::Read; }
+ nlohmann::json inputSchema() const override
+ {
+ return {
+ {"type", "object"},
+ {"properties", {
+ {"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
+ {"sql", {{"type", "string"}, {"description", "SQL query to explain"}}}
+ }},
+ {"required", {"connection_id", "sql"}}
+ };
+ }
+ MCPToolResult execute(const nlohmann::json& params,
+ MCPToolContext& ctx) override;
+ };
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.cpp b/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.cpp
new file mode 100644
index 0000000..36a70b0
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.cpp
@@ -0,0 +1,87 @@
+//
+// SearchAcrossTablesTool.cpp
+//
+
+#include "SearchAcrossTablesTool.h"
+#include "../MCPToolHelpers.h"
+#include <algorithm>
+
+namespace DBModels
+{
+ namespace
+ {
+ std::wstring toLower(std::wstring s)
+ {
+ std::transform(s.begin(), s.end(), s.begin(), ::towlower);
+ return s;
+ }
+ bool containsCI(const std::wstring& hay, const std::wstring& needle)
+ {
+ return toLower(hay).find(toLower(needle)) != std::wstring::npos;
+ }
+ }
+
+ MCPToolResult SearchAcrossTablesTool::execute(const nlohmann::json& params, MCPToolContext& ctx)
+ {
+ const auto connId = MCPTool::extractConnectionId(params);
+
+ if (!params.contains("keyword") || !params["keyword"].is_string())
+ throw MCPToolError::invalidParameters("keyword is required");
+ const auto kwUtf8 = params["keyword"].get<std::string>();
+ const std::wstring kwW = MCPToolHelpers::fromUtf8(kwUtf8);
+
+ const std::wstring schemaW = (params.contains("schema") && params["schema"].is_string())
+ ? MCPToolHelpers::fromUtf8(params["schema"].get<std::string>()) : std::wstring{};
+
+ const auto perm = ctx.checkPermission(tier(), connId);
+ if (const auto* msg = perm.errorMessage())
+ throw MCPToolError::permissionDenied(*msg);
+
+ auto [adapter, config] = ctx.getAdapter(connId);
+
+ nlohmann::json matches = nlohmann::json::array();
+ for (const auto& t : adapter->listTables(schemaW))
+ {
+ if (containsCI(t.name, kwW))
+ matches.push_back({
+ {"type", "table"},
+ {"table", MCPToolHelpers::toUtf8(t.name)},
+ {"match", MCPToolHelpers::toUtf8(t.name)}
+ });
+
+ if (!t.comment.empty() && containsCI(t.comment, kwW))
+ matches.push_back({
+ {"type", "table_comment"},
+ {"table", MCPToolHelpers::toUtf8(t.name)},
+ {"comment", MCPToolHelpers::toUtf8(t.comment)}
+ });
+
+ // describeTable can be slow per-table, but Tier-2 read
+ // sits behind a rate limiter — fine for exploration.
+ for (const auto& col : adapter->describeTable(t.name, schemaW))
+ {
+ if (containsCI(col.name, kwW))
+ matches.push_back({
+ {"type", "column"},
+ {"table", MCPToolHelpers::toUtf8(t.name)},
+ {"column", MCPToolHelpers::toUtf8(col.name)},
+ {"data_type", MCPToolHelpers::toUtf8(col.dataType)}
+ });
+ if (!col.comment.empty() && containsCI(col.comment, kwW))
+ matches.push_back({
+ {"type", "column_comment"},
+ {"table", MCPToolHelpers::toUtf8(t.name)},
+ {"column", MCPToolHelpers::toUtf8(col.name)},
+ {"comment", MCPToolHelpers::toUtf8(col.comment)}
+ });
+ }
+ }
+
+ if (matches.empty())
+ return MCPToolResult::text("No matches found for '" + kwUtf8 + "'.");
+
+ return MCPToolResult::text(
+ "Found " + std::to_string(matches.size()) +
+ " match(es) for '" + kwUtf8 + "':\n" + matches.dump(2));
+ }
+}
diff --git a/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.h b/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.h
new file mode 100644
index 0000000..8e545ab
--- /dev/null
+++ b/windows/Gridex/Services/MCP/Tools/Tier2/SearchAcrossTablesTool.h
@@ -0,0 +1,38 @@
+#pragma once
+//
+// SearchAcrossTablesTool.h — Keyword search over table names,
+// column names, and column comments in a connection. Mirrors
+// macOS SearchAcrossTablesTool.swift. Useful for the AI to
+// discover relevant data.
+
+#include "../MCPTool.h"
+
+namespace DBModels
+{
+ class SearchAcrossTablesTool : public MCPTool
+ {
+ public:
+ std::string name() const override { return "search_across_tables"; }
+ std::string description() const override
+ {
+ return "Search a keyword across table names, column names, "
+ "and column/table comments. Useful when the AI is "
+ "exploring an unfamiliar schema.";
+ }
+ MCPPermissionTier tier() const override { return MCPPermissionTier::Read; }
+ nlohmann::json inputSchema() const override
+ {
+ return {
+ {"type", "object"},
+ {"properties", {
+ {"connection_id", {{"type", "string"}, {"description", "Connection identifier"}}},
+ {"keyword", {{"type", "string"}, {"description", "Keyword to search for"}}},
+ {"schema", {{"type", "string"}, {"description", "Optional schema filter"}}}
+ }},
+ {"required", {"connection_id", "keyword"}}
+ };
+ }
+ MCPToolResult execute(const nlohmann::json& params,
+ MCPToolContext& ctx) override;
+ };
+}