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 stdio reader so Claude Desktop connect never aborts

DangHuynhf56467512e1b
diff --git a/windows/Gridex/App.xaml.cpp b/windows/Gridex/App.xaml.cpp
index 2bb1ba2..1eb4692 100644
--- a/windows/Gridex/App.xaml.cpp
+++ b/windows/Gridex/App.xaml.cpp
@@ -173,12 +173,23 @@ namespace winrt::Gridex::implementation
// StdioTransport uses raw Win32 HANDLE I/O so the C
// runtime stdio state doesn't matter.
- auto s = DBModels::AppSettings::Load();
- DBModels::MCPServerHost::ensureCreated(
- s,
- GridexVersionAscii(),
- DBModels::MCPTransportMode::Stdio);
- DBModels::MCPServerHost::start();
+ // Wrap server bootstrap in try/catch so a constructor
+ // failure (e.g. audit logger can't create its folder,
+ // settings parse error) doesn't abort() the process
+ // before the client sees any response.
+ try
+ {
+ auto s = DBModels::AppSettings::Load();
+ DBModels::MCPServerHost::ensureCreated(
+ s,
+ GridexVersionAscii(),
+ DBModels::MCPTransportMode::Stdio);
+ DBModels::MCPServerHost::start();
+ }
+ catch (...)
+ {
+ ExitProcess(2);
+ }
// Park the main thread; stdio reader runs on its own
// thread inside the server. Exit when the reader thread
diff --git a/windows/Gridex/Services/MCP/MCPServer.cpp b/windows/Gridex/Services/MCP/MCPServer.cpp
index 8f86531..09e5372 100644
--- a/windows/Gridex/Services/MCP/MCPServer.cpp
+++ b/windows/Gridex/Services/MCP/MCPServer.cpp
@@ -139,18 +139,36 @@ namespace DBModels
JSONRPCResponse resp = JSONRPCResponse::fail(req.id, JSONRPCError::methodNotFound());
const std::string& m = req.method;
- if (m == "initialize") resp = handleInitialize(req);
- else if (m == "initialized") return; // notification — no response
- else if (m == "tools/list") resp = handleToolsList(req);
- else if (m == "tools/call") resp = handleToolsCall(req);
- else if (m == "ping")
- resp = JSONRPCResponse::ok(req.id, nlohmann::json{{"pong", true}});
- else if (m == "shutdown")
+ // Each handler is wrapped in try/catch so a single bad
+ // request (e.g. json serialization failure on a weird
+ // param type) cannot tear down the stdio reader thread.
+ try
+ {
+ if (m == "initialize") resp = handleInitialize(req);
+ else if (m == "initialized") return; // notification — no response
+ else if (m == "tools/list") resp = handleToolsList(req);
+ else if (m == "tools/call") resp = handleToolsCall(req);
+ else if (m == "ping")
+ resp = JSONRPCResponse::ok(req.id, nlohmann::json{{"pong", true}});
+ else if (m == "shutdown")
+ {
+ resp = JSONRPCResponse::ok(req.id, nullptr);
+ sendResponse(resp);
+ stop();
+ return;
+ }
+ }
+ catch (const std::exception& e)
+ {
+ JSONRPCError err{
+ static_cast<int>(JSONRPCError::internalError().code),
+ std::string("handler exception: ") + e.what()
+ };
+ resp = JSONRPCResponse::fail(req.id, err);
+ }
+ catch (...)
{
- resp = JSONRPCResponse::ok(req.id, nullptr);
- sendResponse(resp);
- stop();
- return;
+ resp = JSONRPCResponse::fail(req.id, JSONRPCError::internalError());
}
sendResponse(resp);
diff --git a/windows/Gridex/Services/MCP/Transport/StdioTransport.cpp b/windows/Gridex/Services/MCP/Transport/StdioTransport.cpp
index 45b28c0..38aed48 100644
--- a/windows/Gridex/Services/MCP/Transport/StdioTransport.cpp
+++ b/windows/Gridex/Services/MCP/Transport/StdioTransport.cpp
@@ -27,7 +27,10 @@
// %APPDATA%\Gridex\mcp-stdio-debug.log. Flip kDebug to true while
// bringing up a new MCP client; keep false in ship builds so we
// don't spam the audit folder.
-static constexpr bool kDebug = false;
+// Flipped on while Claude Desktop integration is stabilizing —
+// the log at %APPDATA%\Gridex\mcp-stdio-debug.log is the only
+// thing we have for post-mortem diagnosis of stdio crashes.
+static constexpr bool kDebug = true;
static void dbg(const std::string& line)
{
@@ -132,9 +135,25 @@ namespace DBModels
}
catch (const std::exception& e)
{
- dbg(std::string("readLoop parse error: ") + e.what());
- auto resp = JSONRPCResponse::fail(nullptr, JSONRPCError::parseError());
- send(resp);
+ dbg(std::string("readLoop parse/handler std::exception: ") + e.what());
+ try
+ {
+ auto resp = JSONRPCResponse::fail(nullptr, JSONRPCError::parseError());
+ send(resp);
+ } catch (...) {}
+ }
+ catch (...)
+ {
+ // SEH or non-std exception — catch-all so reader
+ // thread can never call std::terminate (which Debug
+ // CRT shows as abort() + popup).
+ dbg("readLoop unknown exception, sending internalError");
+ try
+ {
+ auto resp = JSONRPCResponse::fail(
+ nullptr, JSONRPCError::internalError());
+ send(resp);
+ } catch (...) {}
}
}
running_.store(false);