From d456d4240051ccf4482b32451ed810d3fd86da84 Mon Sep 17 00:00:00 2001 From: Rui Date: Tue, 16 Jun 2026 02:04:54 +0100 Subject: [PATCH] fix: run project-context routes on the main thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The /api/context and /api/context/{category} endpoints were handled inline on the HTTP listener's ThreadPool worker thread. GetContextResponse reads MCPSettingsManager.ContextEnabled / ContextPath, which call EditorPrefs — a main-thread-only API. Every request threw "GetBool can only be called from the main thread" and the bridge returned HTTP 500, so unity_get_project_context never worked even with valid context files. Marshal both routes through ExecuteOnMainThread (already used by the legacy synchronous path), which short-circuits when already on the main thread. --- Editor/MCPBridgeServer.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Editor/MCPBridgeServer.cs b/Editor/MCPBridgeServer.cs index 3c597ba..c7a80c4 100644 --- a/Editor/MCPBridgeServer.cs +++ b/Editor/MCPBridgeServer.cs @@ -344,16 +344,17 @@ private static void HandleRequest(HttpListenerContext context) return; } - // ═══ Project Context endpoints (read-only, no queue needed) ═══ + // ═══ Project Context endpoints (read-only file I/O, but EditorPrefs-backed + // settings must be read on the main thread) ═══ if (apiPath == "context") { - SendJson(response, 200, MCPContextManager.GetContextResponse()); + SendJson(response, 200, ExecuteOnMainThread(() => MCPContextManager.GetContextResponse())); return; } if (apiPath.StartsWith("context/")) { string category = apiPath.Substring("context/".Length); - SendJson(response, 200, MCPContextManager.GetContextResponse(category)); + SendJson(response, 200, ExecuteOnMainThread(() => MCPContextManager.GetContextResponse(category))); return; }