Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions MCPForUnity/Editor/McpHttpCiBoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Services;
using MCPForUnity.Editor.Services.Transport;
using UnityEditor;

namespace MCPForUnity.Editor
{
// HTTP is a plugin-hub pull model: the bridge waits at ws://127.0.0.1:<port>/hub/plugin
// and the editor must dial in. StartStdioForCi forces stdio and only listens, so an
// editor booted that way never registers and every call returns no_unity_session.
//
// The port comes from UNITY_MCP_HTTP_PORT rather than a pref because sibling checkouts
// of one project share an EditorPrefs file, so a persisted HttpUrl is clobbered by
// whichever editor wrote last.
public static class McpHttpCiBoot
{
private const string PortEnv = "UNITY_MCP_HTTP_PORT";

// Re-asserted on every domain load so a reload cannot resume a sibling's port
[InitializeOnLoadMethod]
private static void ReassertEndpointFromEnv()
{
ApplyEndpointFromEnv();
}

public static void StartHttpForCi()
{
if (!ApplyEndpointFromEnv())
{
McpLog.Error($"[MCPForUnity] StartHttpForCi: {PortEnv} not set or invalid; cannot start HTTP transport");
return;
}

// Defer the connect so MCPServiceLocator and friends are initialized.
EditorApplication.delayCall += () =>
{
_ = MCPServiceLocator.TransportManager.StartAsync(TransportMode.Http);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
};
}

private static bool ApplyEndpointFromEnv()
{
string portStr = Environment.GetEnvironmentVariable(PortEnv);
if (string.IsNullOrWhiteSpace(portStr) || !int.TryParse(portStr, out int port) || port <= 0)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
{
return false;
}

EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true);
EditorPrefs.SetString(EditorPrefKeys.HttpTransportScope, "local");
EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, $"http://127.0.0.1:{port}");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return true;
}
}
}
11 changes: 11 additions & 0 deletions MCPForUnity/Editor/McpHttpCiBoot.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.