fix: UseUrls for reliable Kestrel bind on background threads, custom GET /mcp/ handler for SSE discovery#2246
Open
velteyn wants to merge 2 commits into
Open
fix: UseUrls for reliable Kestrel bind on background threads, custom GET /mcp/ handler for SSE discovery#2246velteyn wants to merge 2 commits into
velteyn wants to merge 2 commits into
Conversation
Change Stateless=false so the SDK maps GET /mcp endpoint (required by opencode remote MCP client). The GET endpoint establishes an SSE connection for session negotiation, and POST handles JSON-RPC requests. Previously stateless mode was used to avoid session-ID issues with custom AI client code, but opencode native remote MCP support uses the official MCP SDK which handles session negotiation correctly.
…SE compatibility MCP transport fixes for opencode remote MCP client compatibility: Use builder.WebHost.UseUrls() instead of ConfigureKestrel.ListenLocalhost() which was silently failing to bind on background threads. Replace _app.Run() (premature return after ~28s) with StartAsync() + ManualResetEvent so the server stays up on a background thread. Add custom GET /mcp/ handler returning SSE endpoint: /mcp/ event so MCP clients that probe with GET first (like opencode) can discover the POST endpoint. Stateless mode retained; custom GET handler coexists with SDK POST handler.
| } | ||
|
|
||
| // Wait forever so the thread never exits. | ||
| new System.Threading.ManualResetEvent(false).WaitOne(); |
Comment on lines
+509
to
+517
| foreach (char ch in text) | ||
| { | ||
| if (TryGetPhysicalKey(ch, out PhysicalKey pk)) | ||
| { | ||
| hub.PostKeyboardEvent(new KeyboardEventArgs(pk, true)); | ||
| hub.PostKeyboardEvent(new KeyboardEventArgs(pk, false)); | ||
| count++; | ||
| } | ||
| } |
maximilien-noal
requested changes
Jun 26, 2026
maximilien-noal
left a comment
Member
There was a problem hiding this comment.
@velteyn Can the Battletech code and DOS changes be removed ?
Author
|
ops pardon, my bad I accidentally pushed my work in this !!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The MCP HTTP server has two reliability issues when running on a background thread (as it does in headless/embedded usage):
Kestrel binding fails silently:
ConfigureKestrelwithListenLocalhostdoes not bind the port on background threads. No error is thrown, but no TCP listener is created. Switching tobuilder.WebHost.UseUrls(...)resolves this.WebApplication.Run()returns prematurely: On a background thread with no console,ConsoleLifetimehas no signal to watch, causingWaitForShutdownAsync()to return after ~28 seconds. Replaced withStartAsync()+ManualResetEvent.WaitOne()for indefinite keepalive.No GET endpoint for SSE discovery: In stateless mode, MCP SDK registers only POST. Added
MapGet("/mcp/", ...)returningevent: endpoint\ndata: /mcp/\n\n, allowing SSE-based discovery while keeping stateless mode as default.Changes
UseUrlsinstead ofConfigureKestrel;StartAsync + ManualResetEventinstead ofRun(); custom GET handler.Testing
GET /mcp/returns SSE endpoint event,POST /mcp/returns tool listing, server stays alive indefinitely, VGA mode 0x69 no longer crashes.