{"TotalCount":11,"Files":[{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgeDiagnostics.cs","FileName":"BridgeDiagnostics.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing Sandbox.Diagnostics;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Loopback reachability check (mcp_bridge_selftest): connects to the listener from inside the editor\r\n/// and does one round trip, confirming the transport is reachable under Wine/Proton.\r\n/// \u003C/summary\u003E\r\ninternal static class BridgeDiagnostics\r\n{\r\n\tstatic readonly Logger log = new(\u0022ProtonMcpBridge\u0022);\r\n\r\n\tpublic static async Task SelfTest()\r\n\t{\r\n\t\tif (!TcpMcpServer.IsRunning)\r\n\t\t{\r\n\t\t\tlog.Info(\u0022[selftest] server not running - nothing to probe\u0022);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tlog.Info($\u0022[selftest] bound endpoint: {TcpMcpServer.BoundEndpoint ?? \u0022(null)\u0022}\u0022);\r\n\r\n\t\tawait Probe(IPAddress.Loopback, \u0022127.0.0.1\u0022);\r\n\t}\r\n\r\n\tstatic async Task Probe(IPAddress address, string label)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tusing var client = new TcpClient(address.AddressFamily);\r\n\r\n\t\t\tvar connect = client.ConnectAsync(address, TcpMcpServer.Port);\r\n\r\n\t\t\tif (await Task.WhenAny(connect, Task.Delay(3000)) != connect)\r\n\t\t\t{\r\n\t\t\t\tlog.Warning($\u0022[selftest] {label}:{TcpMcpServer.Port} - connect timed out (3s)\u0022);\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tawait connect; // surface any connect exception\r\n\r\n\t\t\tvar body = \u0022\u0022\u0022{\u0022jsonrpc\u0022:\u00222.0\u0022,\u0022id\u0022:\u0022selftest\u0022,\u0022method\u0022:\u0022ping\u0022}\u0022\u0022\u0022;\r\n\t\t\tvar request =\r\n\t\t\t\t\u0022POST /mcp HTTP/1.1\\r\\n\u0022\r\n\t\t\t\t\u002B $\u0022Host: {label}:{TcpMcpServer.Port}\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022Content-Type: application/json\\r\\n\u0022\r\n\t\t\t\t\u002B $\u0022Content-Length: {Encoding.UTF8.GetByteCount(body)}\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022Connection: close\\r\\n\\r\\n\u0022\r\n\t\t\t\t\u002B body;\r\n\r\n\t\t\tusing var stream = client.GetStream();\r\n\t\t\tawait stream.WriteAsync(Encoding.ASCII.GetBytes(request));\r\n\t\t\tawait stream.FlushAsync();\r\n\r\n\t\t\tvar buffer = new byte[4096];\r\n\t\t\tint read = await stream.ReadAsync(buffer);\r\n\t\t\tvar responseLine = Encoding.UTF8.GetString(buffer, 0, read).Split(\u0022\\r\\n\u0022)[0];\r\n\r\n\t\t\tlog.Info(\r\n\t\t\t\t$\u0022[selftest] {label}:{TcpMcpServer.Port} - reachable, round trip ok: {responseLine}\u0022\r\n\t\t\t);\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tlog.Warning(\r\n\t\t\t\t$\u0022[selftest] {label}:{TcpMcpServer.Port} - failed: {e.GetType().Name}: {e.Message}\u0022\r\n\t\t\t);\r\n\t\t}\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/JsonRpc.cs","FileName":"JsonRpc.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Text.Json.Nodes;\r\nusing System.Text.Json.Serialization;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// An incoming JSON-RPC 2.0 message: a request (has an id) or a notification (no id). MCP dropped\r\n/// batching in revision 2025-06-18, so arrays don\u0027t occur.\r\n/// \u003C/summary\u003E\r\ninternal class JsonRpcMessage\r\n{\r\n\t[JsonPropertyName(\u0022jsonrpc\u0022)]\r\n\tpublic string JsonRpc { get; set; }\r\n\r\n\t/// \u003Csummary\u003EString or number per the spec, so it stays a node and echoes back untouched.\u003C/summary\u003E\r\n\t[JsonPropertyName(\u0022id\u0022)]\r\n\tpublic JsonNode Id { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022method\u0022)]\r\n\tpublic string Method { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022params\u0022)]\r\n\tpublic JsonNode Params { get; set; }\r\n\r\n\t[JsonIgnore]\r\n\tpublic bool IsNotification =\u003E Method is not null \u0026\u0026 Id is null;\r\n}\r\n\r\n/// \u003Csummary\u003E\r\n/// An outgoing JSON-RPC 2.0 response. Carries either a result or an error, never both.\r\n/// \u003C/summary\u003E\r\ninternal class JsonRpcResponse\r\n{\r\n\t[JsonPropertyName(\u0022jsonrpc\u0022)]\r\n\tpublic string JsonRpc { get; set; } = \u00222.0\u0022;\r\n\r\n\t[JsonPropertyName(\u0022id\u0022)]\r\n\tpublic JsonNode Id { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022result\u0022)]\r\n\t[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]\r\n\tpublic JsonNode Result { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022error\u0022)]\r\n\t[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]\r\n\tpublic JsonRpcError Error { get; set; }\r\n\r\n\tpublic static JsonRpcResponse Success(JsonNode id, JsonNode result)\r\n\t{\r\n\t\treturn new JsonRpcResponse { Id = id, Result = result ?? new JsonObject() };\r\n\t}\r\n\r\n\tpublic static JsonRpcResponse Failure(JsonNode id, int code, string message)\r\n\t{\r\n\t\treturn new JsonRpcResponse\r\n\t\t{\r\n\t\t\tId = id,\r\n\t\t\tError = new JsonRpcError { Code = code, Message = message },\r\n\t\t};\r\n\t}\r\n}\r\n\r\ninternal class JsonRpcError\r\n{\r\n\tpublic const int ParseError = -32700;\r\n\tpublic const int InvalidRequest = -32600;\r\n\tpublic const int MethodNotFound = -32601;\r\n\tpublic const int InvalidParams = -32602;\r\n\tpublic const int InternalError = -32603;\r\n\r\n\t[JsonPropertyName(\u0022code\u0022)]\r\n\tpublic int Code { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022message\u0022)]\r\n\tpublic string Message { get; set; }\r\n\r\n\t[JsonPropertyName(\u0022data\u0022)]\r\n\t[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]\r\n\tpublic JsonNode Data { get; set; }\r\n}\r\n\r\n/// \u003Csummary\u003E\r\n/// Thrown by protocol handlers to bubble a specific JSON-RPC error code back to the client.\r\n/// \u003C/summary\u003E\r\ninternal class McpException : Exception\r\n{\r\n\tpublic int Code { get; }\r\n\r\n\tpublic McpException(int code, string message)\r\n\t\t: base(message)\r\n\t{\r\n\t\tCode = code;\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgeInit.cs","FileName":"BridgeInit.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System.Collections.Generic;\r\nusing Editor;\r\nusing Sandbox;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Owns the listener\u0027s lifecycle across the editor session.\r\n///\r\n/// Auto-start runs off the tool frame, not a load event: a library loaded after the editor core can\r\n/// miss \u0022editor.created\u0022 and the initial \u0022hotloaded\u0022, but the frame loop always ticks once we\u0027re\r\n/// live, so the first frame after any (re)load starts the server. It\u0027s one-shot, so a manual Stop\r\n/// sticks, and Start honours the enabled/platform checks itself.\r\n///\r\n/// Teardown uses IHotloadManaged.Destroyed: the listener socket lives in this assembly, so before a\r\n/// hotload swaps it out we stop the old listener while it\u0027s still reachable, freeing the port for\r\n/// the incoming version. A live instance is held in a static field so the hotloader can find it.\r\n/// \u003C/summary\u003E\r\ninternal sealed class BridgeInit : IHotloadManaged\r\n{\r\n\tstatic BridgeInit instance;\r\n\tstatic bool autoStarted;\r\n\r\n\t[EditorEvent.Frame]\r\n\tstatic void OnFrame()\r\n\t{\r\n\t\tinstance ??= new BridgeInit();\r\n\r\n\t\tif (autoStarted)\r\n\t\t\treturn;\r\n\r\n\t\tautoStarted = true;\r\n\t\tTcpMcpServer.Start();\r\n\t}\r\n\r\n\tvoid IHotloadManaged.Destroyed(Dictionary\u003Cstring, object\u003E state)\r\n\t{\r\n\t\tTcpMcpServer.Stop();\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/HttpParse.cs","FileName":"HttpParse.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// A parsed HTTP/1.1 request - just enough of the protocol for MCP\u0027s streamable HTTP transport:\r\n/// request line, headers to the blank line, then exactly Content-Length bytes. No chunked transfer\r\n/// and no keep-alive; one request per connection.\r\n/// \u003C/summary\u003E\r\ninternal sealed class HttpRequest\r\n{\r\n\tpublic string Method { get; private init; }\r\n\tpublic string Path { get; private init; }\r\n\tpublic string Body { get; private init; }\r\n\r\n\treadonly Dictionary\u003Cstring, string\u003E headers;\r\n\r\n\tHttpRequest(string method, string path, Dictionary\u003Cstring, string\u003E headers, string body)\r\n\t{\r\n\t\tMethod = method;\r\n\t\tPath = path;\r\n\t\tthis.headers = headers;\r\n\t\tBody = body;\r\n\t}\r\n\r\n\tpublic string Header(string name) =\u003E headers.TryGetValue(name, out var value) ? value : null;\r\n\r\n\t/// \u003Csummary\u003EThe path with any trailing slash trimmed, so \u0022/mcp\u0022 and \u0022/mcp/\u0022 both match.\u003C/summary\u003E\r\n\tpublic string NormalizedPath =\u003E Path?.TrimEnd(\u0027/\u0027) is { Length: \u003E 0 } trimmed ? trimmed : \u0022/\u0022;\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Read one request off the stream. Returns null on a clean close or a malformed head, and throws\r\n\t/// \u003Csee cref=\u0022HttpTooLargeException\u0022/\u003E when the request exceeds \u003Cparamref name=\u0022maxSize\u0022/\u003E. The head\r\n\t/// is read a byte at a time to the blank line; the body is read in bulk by Content-Length.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static async Task\u003CHttpRequest\u003E Read(\r\n\t\tStream stream,\r\n\t\tlong maxSize,\r\n\t\tSystem.Threading.CancellationToken cancel = default\r\n\t)\r\n\t{\r\n\t\tvar head = new MemoryStream();\r\n\t\tvar one = new byte[1];\r\n\r\n\t\t// Read until the CRLFCRLF that ends the headers.\r\n\t\twhile (!EndsWithBlankLine(head))\r\n\t\t{\r\n\t\t\tint read = await stream.ReadAsync(one, cancel);\r\n\r\n\t\t\tif (read == 0)\r\n\t\t\t\treturn null; // connection closed with no complete request\r\n\r\n\t\t\thead.WriteByte(one[0]);\r\n\r\n\t\t\tif (head.Length \u003E 64 * 1024)\r\n\t\t\t\tthrow new HttpTooLargeException(); // reject oversized header blocks\r\n\t\t}\r\n\r\n\t\tvar lines = Encoding\r\n\t\t\t.ASCII.GetString(head.GetBuffer(), 0, (int)head.Length)\r\n\t\t\t.Split(\u0022\\r\\n\u0022, StringSplitOptions.None);\r\n\r\n\t\tvar requestLine = lines[0].Split(\u0027 \u0027);\r\n\r\n\t\tif (requestLine.Length \u003C 2)\r\n\t\t\treturn null;\r\n\r\n\t\tvar method = requestLine[0];\r\n\t\tvar path = requestLine[1];\r\n\r\n\t\tvar headers = new Dictionary\u003Cstring, string\u003E(StringComparer.OrdinalIgnoreCase);\r\n\r\n\t\tfor (int i = 1; i \u003C lines.Length; i\u002B\u002B)\r\n\t\t{\r\n\t\t\tif (string.IsNullOrEmpty(lines[i]))\r\n\t\t\t\tbreak;\r\n\r\n\t\t\tvar colon = lines[i].IndexOf(\u0027:\u0027);\r\n\r\n\t\t\tif (colon \u003C= 0)\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\theaders[lines[i][..colon].Trim()] = lines[i][(colon \u002B 1)..].Trim();\r\n\t\t}\r\n\r\n\t\tvar body = \u0022\u0022;\r\n\r\n\t\tif (\r\n\t\t\theaders.TryGetValue(\u0022Content-Length\u0022, out var lengthText)\r\n\t\t\t\u0026\u0026 long.TryParse(lengthText, out var length)\r\n\t\t\t\u0026\u0026 length \u003E 0\r\n\t\t)\r\n\t\t{\r\n\t\t\tif (length \u003E maxSize)\r\n\t\t\t\tthrow new HttpTooLargeException();\r\n\r\n\t\t\tbody = await ReadBody(stream, (int)length, cancel);\r\n\t\t}\r\n\r\n\t\treturn new HttpRequest(method, path, headers, body);\r\n\t}\r\n\r\n\tstatic async Task\u003Cstring\u003E ReadBody(\r\n\t\tStream stream,\r\n\t\tint length,\r\n\t\tSystem.Threading.CancellationToken cancel\r\n\t)\r\n\t{\r\n\t\tvar buffer = new byte[length];\r\n\t\tint offset = 0;\r\n\r\n\t\twhile (offset \u003C length)\r\n\t\t{\r\n\t\t\tint read = await stream.ReadAsync(buffer.AsMemory(offset, length - offset), cancel);\r\n\r\n\t\t\tif (read == 0)\r\n\t\t\t\tbreak; // client hung up mid-body\r\n\r\n\t\t\toffset \u002B= read;\r\n\t\t}\r\n\r\n\t\treturn Encoding.UTF8.GetString(buffer, 0, offset);\r\n\t}\r\n\r\n\tstatic bool EndsWithBlankLine(MemoryStream buffer)\r\n\t{\r\n\t\tif (buffer.Length \u003C 4)\r\n\t\t\treturn false;\r\n\r\n\t\tvar raw = buffer.GetBuffer();\r\n\t\tvar end = (int)buffer.Length;\r\n\r\n\t\treturn raw[end - 4] == \u0027\\r\u0027\r\n\t\t\t\u0026\u0026 raw[end - 3] == \u0027\\n\u0027\r\n\t\t\t\u0026\u0026 raw[end - 2] == \u0027\\r\u0027\r\n\t\t\t\u0026\u0026 raw[end - 1] == \u0027\\n\u0027;\r\n\t}\r\n}\r\n\r\n/// \u003Csummary\u003E\r\n/// A request that exceeded the size cap - an oversized header block or a Content-Length past the\r\n/// limit - so the caller can answer 413 instead of buffering unbounded input.\r\n/// \u003C/summary\u003E\r\ninternal sealed class HttpTooLargeException : Exception { }\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/TcpMcpServer.cs","FileName":"TcpMcpServer.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\nusing System.Text;\r\nusing System.Text.Json;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\nusing Sandbox.Diagnostics;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Loopback MCP server over \u003Csee cref=\u0022TcpListener\u0022/\u003E, speaking HTTP/1.1 directly. The kernel-backed\r\n/// \u003Csee cref=\u0022HttpListener\u0022/\u003E the built-in server uses isn\u0027t implemented under Wine/Proton;\r\n/// TcpListener is. Binds 127.0.0.1 only and owns the whole port, so non-/mcp paths return 404.\r\n/// \u003C/summary\u003E\r\ninternal static class TcpMcpServer\r\n{\r\n\tstatic readonly Logger log = new(\u0022ProtonMcpBridge\u0022);\r\n\r\n\tconst long MaxRequestSize = 8 * 1024 * 1024;\r\n\tstatic readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(30);\r\n\r\n\tstatic TcpListener listener;\r\n\tstatic CancellationTokenSource cancel;\r\n\tstatic bool loggedWindowsSkip;\r\n\r\n\tpublic static int Port { get; private set; }\r\n\r\n\tpublic static bool IsRunning =\u003E listener is not null;\r\n\r\n\t/// \u003Csummary\u003EThe endpoint the listener reports as bound. Null when not running.\u003C/summary\u003E\r\n\tpublic static string BoundEndpoint =\u003E listener?.LocalEndpoint?.ToString();\r\n\r\n\t/// \u003Csummary\u003EThe url MCP clients connect to, null when not running.\u003C/summary\u003E\r\n\tpublic static string Url =\u003E IsRunning ? $\u0022http://127.0.0.1:{Port}/mcp\u0022 : null;\r\n\r\n\tpublic static void Start()\r\n\t{\r\n\t\tif (IsRunning)\r\n\t\t\treturn;\r\n\r\n\t\t// Linux/Proton only; the built-in server covers native Windows.\r\n\t\tif (!BridgePlatform.IsLinux)\r\n\t\t{\r\n\t\t\tif (!loggedWindowsSkip)\r\n\t\t\t{\r\n\t\t\t\tlog.Info(\r\n\t\t\t\t\t\u0022Not starting - native Windows editor; the built-in MCP server handles this platform.\u0022\r\n\t\t\t\t);\r\n\t\t\t\tloggedWindowsSkip = true;\r\n\t\t\t}\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (!BridgeConfig.Enabled)\r\n\t\t{\r\n\t\t\tlog.Info(\u0022Not starting - disabled. Enable it in View \u003E MCP Bridge (Start).\u0022);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// Refuse to start if the registry reflection target is missing.\r\n\t\tif (!EngineBridge.Available)\r\n\t\t{\r\n\t\t\tlog.Warning(\r\n\t\t\t\t$\u0022Not starting - engine bridge unavailable: {EngineBridge.Diagnose()}. The editor\u0027s MCP registry (Editor.Mcp.ToolRegistry) may have moved or been renamed.\u0022\r\n\t\t\t);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tPort = BridgeConfig.Port;\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tlistener = new TcpListener(IPAddress.Loopback, Port);\r\n\r\n\t\t\t// Reuse the address so a restart can rebind over a socket still in TIME_WAIT. Set before Start().\r\n\t\t\tlistener.ExclusiveAddressUse = false;\r\n\r\n\t\t\tlistener.Start();\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tlog.Warning($\u0022Couldn\u0027t start Proton MCP bridge on port {Port} ({e.Message})\u0022);\r\n\t\t\tlistener = null;\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tcancel = new CancellationTokenSource();\r\n\t\t_ = ListenLoop(listener, cancel.Token);\r\n\r\n\t\tlog.Info($\u0022Proton MCP bridge listening on {Url}\u0022);\r\n\r\n\t\tBridgeDock.NotifyChanged();\r\n\t}\r\n\r\n\tpublic static void Stop()\r\n\t{\r\n\t\tif (listener is null)\r\n\t\t\treturn;\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tcancel?.Cancel();\r\n\t\t\tlistener.Stop();\r\n\t\t}\r\n\t\tcatch (Exception)\r\n\t\t{\r\n\t\t\t// already stopped\r\n\t\t}\r\n\r\n\t\tcancel?.Dispose();\r\n\t\tcancel = null;\r\n\t\tlistener = null;\r\n\r\n\t\tBridgeDock.NotifyChanged();\r\n\t}\r\n\r\n\tpublic static void Restart()\r\n\t{\r\n\t\tStop();\r\n\t\tStart();\r\n\t}\r\n\r\n\tstatic async Task ListenLoop(TcpListener incoming, CancellationToken token)\r\n\t{\r\n\t\twhile (!token.IsCancellationRequested)\r\n\t\t{\r\n\t\t\tTcpClient client;\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tclient = await incoming.AcceptTcpClientAsync(token);\r\n\t\t\t}\r\n\t\t\tcatch (Exception)\r\n\t\t\t{\r\n\t\t\t\t// listener was stopped\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\t_ = Task.Run(() =\u003E HandleClient(client, token));\r\n\t\t}\r\n\t}\r\n\r\n\tstatic async Task HandleClient(TcpClient client, CancellationToken token)\r\n\t{\r\n\t\tusing (client)\r\n\t\tusing (var stream = client.GetStream())\r\n\t\tusing (var timeout = CancellationTokenSource.CreateLinkedTokenSource(token))\r\n\t\t{\r\n\t\t\ttimeout.CancelAfter(RequestTimeout);\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tawait HandleRequest(stream, timeout.Token);\r\n\t\t\t}\r\n\t\t\tcatch (HttpTooLargeException)\r\n\t\t\t{\r\n\t\t\t\tawait TryWriteStatus(stream, HttpStatusCode.RequestEntityTooLarge);\r\n\t\t\t}\r\n\t\t\tcatch (OperationCanceledException)\r\n\t\t\t{\r\n\t\t\t\t// timed out or shutting down\r\n\t\t\t}\r\n\t\t\tcatch (Exception e)\r\n\t\t\t{\r\n\t\t\t\tlog.Warning(e, $\u0022Unhandled exception handling MCP request: {e.Message}\u0022);\r\n\t\t\t\tawait TryWriteStatus(stream, HttpStatusCode.InternalServerError);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tstatic async Task HandleRequest(NetworkStream stream, CancellationToken token)\r\n\t{\r\n\t\tvar request = await HttpRequest.Read(stream, MaxRequestSize, token);\r\n\r\n\t\tif (request is null)\r\n\t\t\treturn; // clean close or unparseable request\r\n\r\n\t\t// A browser sends Origin on cross-site requests, and a malicious page can resolve its own\r\n\t\t// domain to 127.0.0.1 (dns rebinding), so only loopback origins are allowed.\r\n\t\tif (request.Header(\u0022Origin\u0022) is string origin \u0026\u0026 !IsLoopbackOrigin(origin))\r\n\t\t{\r\n\t\t\tawait WriteStatus(stream, HttpStatusCode.Forbidden, token);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (request.NormalizedPath != \u0022/mcp\u0022)\r\n\t\t{\r\n\t\t\tawait WriteStatus(stream, HttpStatusCode.NotFound, token);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (request.Method != \u0022POST\u0022)\r\n\t\t{\r\n\t\t\t// No server-initiated streams (GET) or sessions (DELETE)\r\n\t\t\tawait WriteStatus(\r\n\t\t\t\tstream,\r\n\t\t\t\tHttpStatusCode.MethodNotAllowed,\r\n\t\t\t\ttoken,\r\n\t\t\t\textraHeaders: \u0022Allow: POST\\r\\n\u0022\r\n\t\t\t);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tawait HandlePost(request, stream, token);\r\n\t}\r\n\r\n\tstatic async Task HandlePost(HttpRequest request, NetworkStream stream, CancellationToken token)\r\n\t{\r\n\t\tJsonRpcMessage message;\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tmessage = JsonSerializer.Deserialize\u003CJsonRpcMessage\u003E(request.Body);\r\n\t\t}\r\n\t\tcatch (JsonException)\r\n\t\t{\r\n\t\t\t// Batches (arrays) were removed in protocol revision 2025-06-18\r\n\t\t\tvar error = request.Body.AsSpan().TrimStart().StartsWith(\u0022[\u0022)\r\n\t\t\t\t? JsonRpcResponse.Failure(\r\n\t\t\t\t\tnull,\r\n\t\t\t\t\tJsonRpcError.InvalidRequest,\r\n\t\t\t\t\t\u0022Batching is not supported\u0022\r\n\t\t\t\t)\r\n\t\t\t\t: JsonRpcResponse.Failure(null, JsonRpcError.ParseError, \u0022Parse error\u0022);\r\n\r\n\t\t\tawait WriteJson(stream, HttpStatusCode.BadRequest, error, token);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (message is null)\r\n\t\t{\r\n\t\t\tawait WriteJson(\r\n\t\t\t\tstream,\r\n\t\t\t\tHttpStatusCode.BadRequest,\r\n\t\t\t\tJsonRpcResponse.Failure(null, JsonRpcError.InvalidRequest, \u0022Invalid request\u0022),\r\n\t\t\t\ttoken\r\n\t\t\t);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tvar result = await Protocol.Handle(message);\r\n\r\n\t\t// Notifications are acknowledged without a body\r\n\t\tif (result is null)\r\n\t\t{\r\n\t\t\tawait WriteStatus(stream, HttpStatusCode.Accepted, token);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tawait WriteJson(stream, HttpStatusCode.OK, result, token);\r\n\t}\r\n\r\n\tstatic async Task WriteJson(\r\n\t\tNetworkStream stream,\r\n\t\tHttpStatusCode status,\r\n\t\tJsonRpcResponse payload,\r\n\t\tCancellationToken token\r\n\t)\r\n\t{\r\n\t\tvar body = JsonSerializer.SerializeToUtf8Bytes(payload);\r\n\r\n\t\tvar head = Encoding.ASCII.GetBytes(\r\n\t\t\t$\u0022HTTP/1.1 {(int)status} {ReasonPhrase(status)}\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022Content-Type: application/json\\r\\n\u0022\r\n\t\t\t\t\u002B $\u0022Content-Length: {body.Length}\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022Connection: close\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022\\r\\n\u0022\r\n\t\t);\r\n\r\n\t\tawait stream.WriteAsync(head, token);\r\n\t\tawait stream.WriteAsync(body, token);\r\n\t\tawait stream.FlushAsync(token);\r\n\t}\r\n\r\n\tstatic async Task WriteStatus(\r\n\t\tNetworkStream stream,\r\n\t\tHttpStatusCode status,\r\n\t\tCancellationToken token,\r\n\t\tstring extraHeaders = null\r\n\t)\r\n\t{\r\n\t\tvar head = Encoding.ASCII.GetBytes(\r\n\t\t\t$\u0022HTTP/1.1 {(int)status} {ReasonPhrase(status)}\\r\\n\u0022\r\n\t\t\t\t\u002B (extraHeaders ?? \u0022\u0022)\r\n\t\t\t\t\u002B \u0022Content-Length: 0\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022Connection: close\\r\\n\u0022\r\n\t\t\t\t\u002B \u0022\\r\\n\u0022\r\n\t\t);\r\n\r\n\t\tawait stream.WriteAsync(head, token);\r\n\t\tawait stream.FlushAsync(token);\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EBest-effort status write from catch blocks, where the token may already be tripped.\u003C/summary\u003E\r\n\tstatic async Task TryWriteStatus(NetworkStream stream, HttpStatusCode status)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tawait WriteStatus(stream, status, CancellationToken.None);\r\n\t\t}\r\n\t\tcatch (Exception)\r\n\t\t{\r\n\t\t\t// connection gone\r\n\t\t}\r\n\t}\r\n\r\n\tstatic string ReasonPhrase(HttpStatusCode status) =\u003E\r\n\t\tstatus switch\r\n\t\t{\r\n\t\t\tHttpStatusCode.OK =\u003E \u0022OK\u0022,\r\n\t\t\tHttpStatusCode.Accepted =\u003E \u0022Accepted\u0022,\r\n\t\t\tHttpStatusCode.BadRequest =\u003E \u0022Bad Request\u0022,\r\n\t\t\tHttpStatusCode.Forbidden =\u003E \u0022Forbidden\u0022,\r\n\t\t\tHttpStatusCode.NotFound =\u003E \u0022Not Found\u0022,\r\n\t\t\tHttpStatusCode.MethodNotAllowed =\u003E \u0022Method Not Allowed\u0022,\r\n\t\t\tHttpStatusCode.RequestEntityTooLarge =\u003E \u0022Payload Too Large\u0022,\r\n\t\t\tHttpStatusCode.InternalServerError =\u003E \u0022Internal Server Error\u0022,\r\n\t\t\t_ =\u003E status.ToString(),\r\n\t\t};\r\n\r\n\tstatic bool IsLoopbackOrigin(string origin)\r\n\t{\r\n\t\treturn Uri.TryCreate(origin, UriKind.Absolute, out var uri) \u0026\u0026 uri.IsLoopback;\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgeCommands.cs","FileName":"BridgeCommands.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using Sandbox;\r\nusing Sandbox.Diagnostics;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Console commands for the bridge - the dock covers everyday start/stop; these are for scripting\r\n/// and the loopback self-test: \u003Cc\u003Emcp_bridge_status\u003C/c\u003E, \u003Cc\u003Emcp_bridge_selftest\u003C/c\u003E,\r\n/// \u003Cc\u003Emcp_bridge_restart\u003C/c\u003E, \u003Cc\u003Emcp_bridge_port\u003C/c\u003E.\r\n/// \u003C/summary\u003E\r\npublic static class BridgeCommands\r\n{\r\n\tstatic readonly Logger log = new(\u0022ProtonMcpBridge\u0022);\r\n\r\n\t[ConCmd(\r\n\t\t\u0022mcp_bridge_status\u0022,\r\n\t\tHelp = \u0022Show whether the Proton MCP bridge is running and where it\u0027s bound\u0022\r\n\t)]\r\n\tpublic static void Status()\r\n\t{\r\n\t\tif (!TcpMcpServer.IsRunning)\r\n\t\t{\r\n\t\t\tif (!BridgePlatform.IsLinux)\r\n\t\t\t{\r\n\t\t\t\tlog.Info(\r\n\t\t\t\t\t\u0022[mcp bridge] not running - native Windows editor; the built-in MCP server handles this platform.\u0022\r\n\t\t\t\t);\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tlog.Info(\r\n\t\t\t\t$\u0022[mcp bridge] not running. enabled: {BridgeConfig.Enabled}, engine bridge available: {EngineBridge.Available} ({EngineBridge.Diagnose()})\u0022\r\n\t\t\t);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tlog.Info(\r\n\t\t\t$\u0022[mcp bridge] running - url {TcpMcpServer.Url}, bound endpoint {TcpMcpServer.BoundEndpoint}\u0022\r\n\t\t);\r\n\t}\r\n\r\n\t[ConCmd(\r\n\t\t\u0022mcp_bridge_selftest\u0022,\r\n\t\tHelp = \u0022Probe the bridge\u0027s loopback listener from inside the editor process\u0022\r\n\t)]\r\n\tpublic static void SelfTest()\r\n\t{\r\n\t\t_ = BridgeDiagnostics.SelfTest();\r\n\t}\r\n\r\n\t[ConCmd(\u0022mcp_bridge_restart\u0022, Help = \u0022Stop and restart the Proton MCP bridge listener\u0022)]\r\n\tpublic static void Restart()\r\n\t{\r\n\t\tTcpMcpServer.Restart();\r\n\t}\r\n\r\n\t[ConCmd(\r\n\t\t\u0022mcp_bridge_port\u0022,\r\n\t\tHelp = \u0022Set the loopback port and rebind, e.g. mcp_bridge_port 7300\u0022\r\n\t)]\r\n\tpublic static void SetPort(int port)\r\n\t{\r\n\t\tBridgeConfig.Port = port;\r\n\t\tlog.Info($\u0022[mcp bridge] port set to {BridgeConfig.Port}\u0022);\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgeDock.cs","FileName":"BridgeDock.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using Editor;\r\nusing Sandbox;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Dockable panel (Window \u003E MCP Bridge): a status light, the connect URL, and a start/stop button.\r\n/// \u003C/summary\u003E\r\n[Dock(\u0022Editor\u0022, \u0022MCP Bridge\u0022, \u0022cable\u0022)]\r\ninternal class BridgeDock : Widget\r\n{\r\n\tstatic readonly Color RunningColor = Color.Parse(\u0022#4caf50\u0022) ?? Color.Green;\r\n\r\n\t/// \u003Csummary\u003EThe live instance, so state changes elsewhere can refresh the panel.\u003C/summary\u003E\r\n\tinternal static BridgeDock Instance { get; private set; }\r\n\r\n\treadonly Label statusLabel;\r\n\treadonly Label urlLabel;\r\n\treadonly Button toggleButton;\r\n\r\n\tpublic BridgeDock(Widget parent)\r\n\t\t: base(parent)\r\n\t{\r\n\t\tInstance = this;\r\n\r\n\t\tLayout = Layout.Column();\r\n\t\tLayout.Margin = 16;\r\n\t\tLayout.Spacing = 8;\r\n\r\n\t\tstatusLabel = new Label(\u0022\u0022, this);\r\n\t\tLayout.Add(statusLabel);\r\n\r\n\t\turlLabel = new Label(\u0022\u0022, this) { Color = Theme.TextControl.WithAlpha(0.6f) };\r\n\t\tLayout.Add(urlLabel);\r\n\r\n\t\tLayout.AddSpacingCell(4);\r\n\r\n\t\ttoggleButton = new Button(\u0022\u0022, this) { Clicked = OnToggle };\r\n\t\tLayout.Add(toggleButton);\r\n\r\n\t\tLayout.AddStretchCell();\r\n\r\n\t\tRefresh();\r\n\t}\r\n\r\n\tvoid OnToggle()\r\n\t{\r\n\t\tif (!BridgePlatform.IsLinux)\r\n\t\t\treturn;\r\n\r\n\t\t// Act on the real listener state and persist the intent. Start/Stop are idempotent, and we\r\n\t\t// can\u0027t rely on the Enabled setter alone - its side effect is skipped when the cookie already\r\n\t\t// matches (e.g. enabled-but-not-running), which left the Start button doing nothing.\r\n\t\tif (TcpMcpServer.IsRunning)\r\n\t\t{\r\n\t\t\tBridgeConfig.Enabled = false;\r\n\t\t\tTcpMcpServer.Stop();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tBridgeConfig.Enabled = true;\r\n\t\t\tTcpMcpServer.Start();\r\n\t\t}\r\n\r\n\t\tRefresh();\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EPush current state into the widgets.\u003C/summary\u003E\r\n\tpublic void Refresh()\r\n\t{\r\n\t\tif (!BridgePlatform.IsLinux)\r\n\t\t{\r\n\t\t\tstatusLabel.Text = \u0022\u26AA Inactive\u0022;\r\n\t\t\tstatusLabel.Color = Theme.TextControl.WithAlpha(0.6f);\r\n\t\t\turlLabel.Text =\r\n\t\t\t\t\u0022Native Windows - the built-in editor MCP server handles this platform.\u0022;\r\n\t\t\ttoggleButton.Text = \u0022Unavailable on Windows\u0022;\r\n\t\t\ttoggleButton.Enabled = false;\r\n\t\t\tUpdate();\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tvar running = TcpMcpServer.IsRunning;\r\n\r\n\t\tstatusLabel.Text = running ? \u0022\uD83D\uDFE2 Running\u0022 : \u0022\u26AB Stopped\u0022;\r\n\t\tstatusLabel.Color = running ? RunningColor : Theme.TextControl.WithAlpha(0.6f);\r\n\r\n\t\turlLabel.Text = running\r\n\t\t\t? $\u0022MCP endpoint (streamable HTTP):\\n{TcpMcpServer.Url}\u0022\r\n\t\t\t: $\u0022Port {BridgeConfig.Port}\u0022;\r\n\r\n\t\ttoggleButton.Text = running ? \u0022Stop\u0022 : \u0022Start\u0022;\r\n\t\ttoggleButton.Enabled = true;\r\n\r\n\t\tUpdate();\r\n\t}\r\n\r\n\t/// \u003Csummary\u003ERefresh the panel if it\u0027s open. Called when the listener starts or stops.\u003C/summary\u003E\r\n\tinternal static void NotifyChanged()\r\n\t{\r\n\t\t// IsValid() is null-safe and also false for a destroyed widget - the static Instance can\r\n\t\t// outlive the panel (closed/reopened popout), and touching its dead QLabel would throw.\r\n\t\tif (Instance.IsValid())\r\n\t\t\tInstance.Refresh();\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgeConfig.cs","FileName":"BridgeConfig.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Bridge settings, persisted per-user via EditorCookie. The port is shared with the editor\u0027s\r\n/// built-in \u0022MCP Server\u0022 preferences (it reads the same McpServerPort cookie), so its default is the\r\n/// built-in\u0027s 7269 - free on Linux, since that server can\u0027t bind it under Proton - and setting the\r\n/// port in that tab configures the bridge too (applies on the next start/restart). Enable/disable is\r\n/// the bridge\u0027s own, driven by the dock\u0027s Start/Stop.\r\n/// \u003C/summary\u003E\r\ninternal static class BridgeConfig\r\n{\r\n\tconst string EnabledKey = \u0022ProtonMcpBridge.Enabled\u0022;\r\n\tconst string PortKey = \u0022McpServerPort\u0022;\r\n\r\n\tpublic const int DefaultPort = 7269;\r\n\r\n\t/// \u003Csummary\u003EWhether the listener should run. Default on.\u003C/summary\u003E\r\n\tpublic static bool Enabled\r\n\t{\r\n\t\tget =\u003E EditorCookie.Get(EnabledKey, true);\r\n\t\tset\r\n\t\t{\r\n\t\t\tif (Enabled == value)\r\n\t\t\t\treturn;\r\n\r\n\t\t\tEditorCookie.Set(EnabledKey, value);\r\n\r\n\t\t\tif (value)\r\n\t\t\t\tTcpMcpServer.Start();\r\n\t\t\telse\r\n\t\t\t\tTcpMcpServer.Stop();\r\n\t\t}\r\n\t}\r\n\r\n\t/// \u003Csummary\u003ELoopback port, shared with the built-in MCP Server tab. Rebinds if already running.\u003C/summary\u003E\r\n\tpublic static int Port\r\n\t{\r\n\t\tget =\u003E EditorCookie.Get(PortKey, DefaultPort);\r\n\t\tset\r\n\t\t{\r\n\t\t\tvalue = Math.Clamp(value, 1, 65535);\r\n\r\n\t\t\tif (Port == value)\r\n\t\t\t\treturn;\r\n\r\n\t\t\tEditorCookie.Set(PortKey, value);\r\n\r\n\t\t\tif (TcpMcpServer.IsRunning)\r\n\t\t\t\tTcpMcpServer.Restart();\r\n\t\t}\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/BridgePlatform.cs","FileName":"BridgePlatform.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Runtime.InteropServices;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Gates the bridge to Linux. Under Proton the editor is a Windows build, so the runtime reports\r\n/// Windows and \u003Csee cref=\u0022RuntimeInformation.IsOSPlatform\u0022/\u003E can\u0027t detect Linux directly; the test\r\n/// is instead whether ntdll exports \u003Cc\u003Ewine_get_version\u003C/c\u003E, which only Wine\u0027s does. On genuine\r\n/// Windows the built-in editor MCP server works, so the bridge does nothing.\r\n/// \u003C/summary\u003E\r\ninternal static class BridgePlatform\r\n{\r\n\t// Field initializer, not an auto-property: the code generator can\u0027t capture a non-constant\r\n\t// property default as a constant attribute value.\r\n\tstatic readonly bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsWine();\r\n\r\n\t/// \u003Csummary\u003ETrue on a native Linux runtime, or a Windows build under Wine/Proton.\u003C/summary\u003E\r\n\tpublic static bool IsLinux =\u003E isLinux;\r\n\r\n\tstatic bool IsWine()\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar ntdll = GetModuleHandle(\u0022ntdll.dll\u0022);\r\n\t\t\treturn ntdll != IntPtr.Zero \u0026\u0026 GetProcAddress(ntdll, \u0022wine_get_version\u0022) != IntPtr.Zero;\r\n\t\t}\r\n\t\tcatch (Exception)\r\n\t\t{\r\n\t\t\t// Native interop unavailable or ntdll not shaped as expected - treat as not-Wine.\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t[DllImport(\r\n\t\t\u0022kernel32.dll\u0022,\r\n\t\tEntryPoint = \u0022GetModuleHandleA\u0022,\r\n\t\tCharSet = CharSet.Ansi,\r\n\t\tExactSpelling = true\r\n\t)]\r\n\tstatic extern IntPtr GetModuleHandle(string moduleName);\r\n\r\n\t[DllImport(\u0022kernel32.dll\u0022, ExactSpelling = true, CharSet = CharSet.Ansi)]\r\n\tstatic extern IntPtr GetProcAddress(IntPtr module, string procName);\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/EngineBridge.cs","FileName":"EngineBridge.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Reflection;\r\nusing System.Text.Json.Nodes;\r\nusing System.Threading.Tasks;\r\nusing Sandbox.Diagnostics;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// Reflection seam into the editor\u0027s MCP registry (Editor.Mcp.ToolRegistry in Sandbox.Tools), which\r\n/// handles tool discovery, argument binding, main-thread marshalling and result shaping. The type is\r\n/// internal, so reflection is the only access path; every lookup is null-guarded so a rename fails\r\n/// loudly instead of silently.\r\n/// \u003C/summary\u003E\r\ninternal static class EngineBridge\r\n{\r\n\tstatic readonly Logger log = new(\u0022ProtonMcpBridge\u0022);\r\n\r\n\tstatic readonly Type registry = Type.GetType(\u0022Editor.Mcp.ToolRegistry, Sandbox.Tools\u0022);\r\n\r\n\t// Public methods on an internal type; bind broadly to tolerate accessibility changes.\r\n\tconst BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;\r\n\r\n\tstatic readonly MethodInfo callMethod = registry?.GetMethod(\r\n\t\t\u0022Call\u0022,\r\n\t\tFlags,\r\n\t\t[typeof(string), typeof(JsonObject)]\r\n\t);\r\n\tstatic readonly MethodInfo listMethod = registry?.GetMethod(\u0022ListJson\u0022, Flags, Type.EmptyTypes);\r\n\tstatic readonly MethodInfo asArgumentsMethod = registry?.GetMethod(\r\n\t\t\u0022AsArguments\u0022,\r\n\t\tFlags,\r\n\t\t[typeof(JsonNode), typeof(string)]\r\n\t);\r\n\r\n\t/// \u003Csummary\u003ETrue when the reflection targets resolved. False means the registry moved or was renamed.\u003C/summary\u003E\r\n\tpublic static bool Available =\u003E\r\n\t\tregistry is not null \u0026\u0026 callMethod is not null \u0026\u0026 listMethod is not null;\r\n\r\n\t/// \u003Csummary\u003EWhat\u0027s missing, for the startup log.\u003C/summary\u003E\r\n\tpublic static string Diagnose()\r\n\t{\r\n\t\tif (registry is null)\r\n\t\t\treturn \u0022couldn\u0027t find type \u0027Editor.Mcp.ToolRegistry, Sandbox.Tools\u0027\u0022;\r\n\t\tif (listMethod is null)\r\n\t\t\treturn \u0022couldn\u0027t find ToolRegistry.ListJson()\u0022;\r\n\t\tif (callMethod is null)\r\n\t\t\treturn \u0022couldn\u0027t find ToolRegistry.Call(string, JsonObject)\u0022;\r\n\t\treturn \u0022ok\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EThe tools/list payload from ToolRegistry.ListJson - only [McpListed] tools appear.\u003C/summary\u003E\r\n\tpublic static JsonNode ListJson()\r\n\t{\r\n\t\treturn (JsonNode)listMethod.Invoke(null, null);\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Invoke a tool by name. ToolRegistry.Call marshals onto the main thread, binds arguments, runs\r\n\t/// the tool and shapes the result (including in-band isError results).\r\n\t/// \u003C/summary\u003E\r\n\tpublic static async Task\u003CJsonNode\u003E Call(string name, JsonNode arguments)\r\n\t{\r\n\t\tvar args = AsArguments(arguments);\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar task = (Task\u003CJsonNode\u003E)callMethod.Invoke(null, [name, args]);\r\n\t\t\treturn await task;\r\n\t\t}\r\n\t\tcatch (TargetInvocationException e) when (e.InnerException is not null)\r\n\t\t{\r\n\t\t\t// Unwrap so the real failure surfaces instead of the reflection wrapper.\r\n\t\t\tthrow e.InnerException;\r\n\t\t}\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Coerce the tool-call \u0027arguments\u0027 node to the JsonObject ToolRegistry.Call expects, reusing the\r\n\t/// engine\u0027s AsArguments (which tolerates JSON encoded inside a string) when available.\r\n\t/// \u003C/summary\u003E\r\n\tstatic JsonObject AsArguments(JsonNode node)\r\n\t{\r\n\t\tif (node is null)\r\n\t\t\treturn null;\r\n\r\n\t\tif (asArgumentsMethod is not null)\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\treturn (JsonObject)asArgumentsMethod.Invoke(null, [node, \u0022arguments\u0022]);\r\n\t\t\t}\r\n\t\t\tcatch (TargetInvocationException e) when (e.InnerException is not null)\r\n\t\t\t{\r\n\t\t\t\tthrow e.InnerException;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn node as JsonObject\r\n\t\t\t?? throw new McpException(\r\n\t\t\t\tJsonRpcError.InvalidParams,\r\n\t\t\t\t\u0022arguments must be a json object like {\\\u0022key\\\u0022: value}\u0022\r\n\t\t\t);\r\n\t}\r\n}\r\n"},{"Ident":"ccr.proton-mcp-bridge","Path":"Editor/Protocol.cs","FileName":"Protocol.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":308293,"IsPrivate":false,"Code":"using System;\r\nusing System.Linq;\r\nusing System.Text.Json.Nodes;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ProtonMcpBridge;\r\n\r\n/// \u003Csummary\u003E\r\n/// The JSON-RPC 2.0 / MCP shell: handles initialize and ping, routes tools/list and tools/call to\r\n/// \u003Csee cref=\u0022EngineBridge\u0022/\u003E.\r\n/// \u003C/summary\u003E\r\ninternal static class Protocol\r\n{\r\n\t/// \u003Csummary\u003E\r\n\t/// Protocol revisions this speaks, newest first. An unknown request is answered with the newest.\r\n\t/// \u003C/summary\u003E\r\n\tstatic readonly string[] SupportedProtocolVersions =\r\n\t[\r\n\t\t\u00222025-11-25\u0022,\r\n\t\t\u00222025-06-18\u0022,\r\n\t\t\u00222025-03-26\u0022,\r\n\t\t\u00222024-11-05\u0022,\r\n\t];\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Sent at initialize and injected into the agent\u0027s context - registry-wide conventions that apply\r\n\t/// across every tool.\r\n\t/// \u003C/summary\u003E\r\n\tstatic readonly string Instructions = \u0022\u0022\u0022\r\n\t\tThis server is embedded in the s\u0026box editor and operates on the project that is currently open in it. Start with editor_status to see what\u0027s open and whether play mode is running. Your tool list is just the entry points - the real tools live in editor and addon code and come and go as code hotloads. Find them with search_tools, invoke them with call_tool, or batch several invocations with call_tools. After doing anything, read_console shows what the editor logged - errors and exceptions land there. Tool failures come back as results with isError rather than protocol errors, so read them and adapt.\r\n\r\n\t\tConventions, everywhere in the registry:\r\n\t\t- Paging is \u0027limit\u0027 and \u0027offset\u0027. Defaults and ranges are part of each parameter\u0027s schema, and out of range values clamp rather than error.\r\n\t\t- Vectors and angles are comma strings, not arrays or objects - a position is \u0027x,y,z\u0027, view angles are \u0027pitch,yaw,roll\u0027.\r\n\t\t- The coordinate system is Source engine convention, like Half-Life 2: one unit is one inch, \u002Bx is forward, \u002By is left, \u002Bz is up. Angles are degrees.\r\n\t\t- Game objects and components are identified by guid. Assets are identified by the relative path asset_search returns.\r\n\t\t- Every tool that edits the scene pushes an undo step, so the user can ctrl\u002Bz your changes like their own.\r\n\t\t\u0022\u0022\u0022;\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Handle a single message, returning null when it doesn\u0027t warrant a response (notifications).\r\n\t/// Runs on the thread pool - tool calls marshal onto the main thread inside the registry.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static async Task\u003CJsonRpcResponse\u003E Handle(JsonRpcMessage message)\r\n\t{\r\n\t\t// Notifications get no response; a missing method means a response to a server-initiated request.\r\n\t\tif (message.IsNotification || message.Method is null)\r\n\t\t\treturn null;\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar result = message.Method switch\r\n\t\t\t{\r\n\t\t\t\t\u0022initialize\u0022 =\u003E Initialize(message.Params),\r\n\t\t\t\t\u0022ping\u0022 =\u003E new JsonObject(),\r\n\t\t\t\t\u0022tools/list\u0022 =\u003E EngineBridge.ListJson(),\r\n\t\t\t\t\u0022tools/call\u0022 =\u003E await ToolsCall(message.Params),\r\n\t\t\t\t_ =\u003E throw new McpException(\r\n\t\t\t\t\tJsonRpcError.MethodNotFound,\r\n\t\t\t\t\t$\u0022Method not found: {message.Method}\u0022\r\n\t\t\t\t),\r\n\t\t\t};\r\n\r\n\t\t\treturn JsonRpcResponse.Success(message.Id, result);\r\n\t\t}\r\n\t\tcatch (McpException e)\r\n\t\t{\r\n\t\t\treturn JsonRpcResponse.Failure(message.Id, e.Code, e.Message);\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\treturn JsonRpcResponse.Failure(message.Id, JsonRpcError.InternalError, e.Message);\r\n\t\t}\r\n\t}\r\n\r\n\tstatic JsonNode Initialize(JsonNode param)\r\n\t{\r\n\t\tvar requested = param?[\u0022protocolVersion\u0022]?.ToString();\r\n\t\tvar version = SupportedProtocolVersions.Contains(requested)\r\n\t\t\t? requested\r\n\t\t\t: SupportedProtocolVersions[0];\r\n\r\n\t\treturn new JsonObject\r\n\t\t{\r\n\t\t\t[\u0022protocolVersion\u0022] = version,\r\n\t\t\t[\u0022capabilities\u0022] = new JsonObject\r\n\t\t\t{\r\n\t\t\t\t[\u0022tools\u0022] = new JsonObject { [\u0022listChanged\u0022] = false },\r\n\t\t\t},\r\n\t\t\t[\u0022serverInfo\u0022] = new JsonObject\r\n\t\t\t{\r\n\t\t\t\t[\u0022name\u0022] = \u0022sbox-editor\u0022,\r\n\t\t\t\t[\u0022title\u0022] = \u0022s\u0026box Editor (Proton bridge)\u0022,\r\n\t\t\t\t[\u0022version\u0022] = $\u0022{Sandbox.Application.Version}\u0022,\r\n\t\t\t\t[\u0022description\u0022] =\r\n\t\t\t\t\t\u0022The s\u0026box editor - live tools for the project open in it, served over a Wine-safe TCP transport\u0022,\r\n\t\t\t},\r\n\t\t\t[\u0022instructions\u0022] = Instructions,\r\n\t\t};\r\n\t}\r\n\r\n\tstatic async Task\u003CJsonNode\u003E ToolsCall(JsonNode param)\r\n\t{\r\n\t\tvar name = param?[\u0022name\u0022]?.ToString()?.Trim();\r\n\r\n\t\tif (string.IsNullOrEmpty(name))\r\n\t\t\tthrow new McpException(JsonRpcError.InvalidParams, \u0022Missing tool name\u0022);\r\n\r\n\t\treturn await EngineBridge.Call(name, param?[\u0022arguments\u0022]);\r\n\t}\r\n}\r\n"}]}