Skip to content

Fix: /api/context route in main thread - #17

Closed
rcasaleiro wants to merge 2 commits into
AnkleBreaker-Studio:integrationfrom
rcasaleiro:fix/context-route-main-thread
Closed

Fix: /api/context route in main thread#17
rcasaleiro wants to merge 2 commits into
AnkleBreaker-Studio:integrationfrom
rcasaleiro:fix/context-route-main-thread

Conversation

@rcasaleiro

Copy link
Copy Markdown

Problem

unity_get_project_context always fails with HTTP 500, so the Project Context
feature never returns anything.

Cause

The bridge handles every request on a background ThreadPool thread:

ThreadPool.QueueUserWorkItem(_ => HandleRequest(context));

The /api/context routes call GetContextResponse() directly on that thread, and it
reads EditorPrefs (ContextEnabled / ContextPath) — a main-thread-only Unity API.
So every request throws and returns 500:

GetBool can only be called from the main thread.
  at MCPSettingsManager.get_ContextEnabled()
  at MCPContextManager.GetContextResponse()
  at MCPBridgeServer.HandleRequest()

Fix

Run the two context routes through ExecuteOnMainThread(...) — the same helper every
other synchronous route already uses. One-file change; no other endpoints affected.

Testing

Tested locally with a new project with only three docs in under Assets/MCP/Context/: unity_get_project_context returned 500 before
the change and returns the full content after, for both the all-categories and
single-category calls.

TakeruGrima and others added 2 commits June 2, 2026 18:33
…tegration

Merge integration : editor-window screenshot tool + welcome window rework (v2.32.0)
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.
@rcasaleiro rcasaleiro changed the title Fix/context route main thread Fix: /api/context route in main thread Jun 16, 2026
@francoios

Copy link
Copy Markdown
Contributor

Thanks @rcasaleiro — spot-on root cause. The /api/context main-thread fix was re-implemented on main (merged via #21): both context routes now go through ExecuteOnMainThread like every other sync route, so the EditorPrefs read no longer returns HTTP 500 from a ThreadPool thread. Verified live (the exact call that 500'd now returns a graceful payload). Credited in the changelog.

Closing as resolved by the forward-port. 🙏

@francoios francoios closed this Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants