Skip to content

Commit 17d1fca

Browse files
Merge pull request #59 from ichimi-abunai/main
feat: add remote host configuration support for Unity WebSocket connection
2 parents f0d0bcf + 9ce66cd commit 17d1fca

File tree

7 files changed

+60
-3
lines changed

7 files changed

+60
-3
lines changed

Editor/UnityBridge/McpUnityEditorWindow.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ private void DrawServerTab()
135135

136136
EditorGUILayout.Space();
137137

138+
// Allow remote connections toggle
139+
bool allowRemoteConnections = EditorGUILayout.Toggle(new GUIContent("Allow Remote Connections", "Allow connections from remote MCP bridges. When disabled, only localhost connections are allowed (default)."), settings.AllowRemoteConnections);
140+
if (allowRemoteConnections != settings.AllowRemoteConnections)
141+
{
142+
settings.AllowRemoteConnections = allowRemoteConnections;
143+
settings.SaveSettings();
144+
// Restart server to apply binding change
145+
mcpUnityServer.StopServer();
146+
mcpUnityServer.StartServer();
147+
}
148+
149+
EditorGUILayout.Space();
150+
138151
// Enable info logs toggle
139152
bool enableInfoLogs = EditorGUILayout.Toggle(new GUIContent("Enable Info Logs", "Show informational logs in the Unity console"), settings.EnableInfoLogs);
140153
if (enableInfoLogs != settings.EnableInfoLogs)

Editor/UnityBridge/McpUnityServer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ public void StartServer()
125125

126126
try
127127
{
128-
_webSocketServer = new WebSocketServer($"ws://localhost:{McpUnitySettings.Instance.Port}");
128+
var host = McpUnitySettings.Instance.AllowRemoteConnections ? "0.0.0.0" : "localhost";
129+
_webSocketServer = new WebSocketServer($"ws://{host}:{McpUnitySettings.Instance.Port}");
129130
_webSocketServer.AddWebSocketService("/McpUnity", () => new McpUnitySocketHandler(this));
130131
_webSocketServer.Start();
131-
McpLogger.LogInfo($"WebSocket server started successfully on port {McpUnitySettings.Instance.Port}.");
132+
McpLogger.LogInfo($"WebSocket server started successfully on {host}:{McpUnitySettings.Instance.Port}.");
132133
}
133134
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
134135
{

Editor/UnityBridge/McpUnitySettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class McpUnitySettings
3636

3737
[Tooltip("Optional: Full path to the npm executable (e.g., /Users/user/.asdf/shims/npm or C:\\path\\to\\npm.cmd). If not set, 'npm' from the system PATH will be used.")]
3838
public string NpmExecutablePath = string.Empty;
39+
40+
[Tooltip("Allow connections from remote MCP bridges. When disabled, only localhost connections are allowed (default).")]
41+
public bool AllowRemoteConnections = false;
3942

4043
/// <summary>
4144
/// Singleton instance of settings

README-ja.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ MCP Unityサーバーを起動するには2つの方法があります:
255255
> [!TIP]
256256
> AIコーディングIDE(Claude Desktop、Cursor IDE、Windsurf IDEなど)とMCPサーバー間のタイムアウトは、使用するIDEによって異なる場合があります。
257257
258+
## オプション:リモート MCP ブリッジ接続を許可する
259+
260+
デフォルトでは、WebSocket サーバーは 'localhost' にバインドされています。他のマシンから MCP ブリッジ接続を許可するには、以下の手順に従ってください:
261+
262+
1. Unity エディターを開く
263+
2. メニューから「Tools > MCP Unity > Server Window」を選択
264+
3. 「Allow Remote Connections(リモート接続を許可)」チェックボックスを有効にする
265+
4. Unity は WebSocket サーバーを '0.0.0.0'(すべてのインターフェース)にバインドします
266+
5. Node.js サーバーを再起動して新しいホスト設定を適用する
267+
6. リモートで MCP ブリッジを実行する場合は、環境変数 UNITY_HOST を Unity 実行マシンの IP アドレスに設定して起動:
268+
`UNITY_HOST=192.168.1.100 node server.js`
269+
258270
## サポート & フィードバック
259271

260272
ご質問やサポートが必要な場合は、このリポジトリの[Issue](https://github.com/CoderGamester/mcp-unity/issues)を開くか、以下までご連絡ください:

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ You can change depending on the OS you are using:
245245
> [!TIP]
246246
> The timeout between your AI Coding IDE (e.g., Claude Desktop, Cursor IDE, Windsurf IDE) and the MCP Server depends on the IDE.
247247
248+
## Optional: Allow Remote MCP Bridge Connections
249+
250+
By default, the WebSocket server binds to 'localhost'. To allow MCP bridge connections from other machines:
251+
252+
1. Open the Unity Editor
253+
2. Navigate to Tools > MCP Unity > Server Window
254+
3. Enable the "Allow Remote Connections" checkbox
255+
4. Unity will bind the WebSocket server to '0.0.0.0' (all interfaces)
256+
5. Restart the Node.js server to apply the new host configuration
257+
6. Set the environment variable UNITY_HOST to your Unity machine's IP address when running the MCP bridge remotely: `UNITY_HOST=192.168.1.100 node server.js`
258+
248259
## <a name="debug-server"></a>Debugging the Server
249260

250261
<details>

README_zh-CN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ MCP Unity 通过将 Unity `Library/PackedCache` 文件夹添加到您的工作
244244
> [!TIP]
245245
> 您的 AI 编码 IDE(例如 Claude Desktop、Cursor IDE、Windsurf IDE)与 MCP 服务器之间的超时时间取决于 IDE。
246246
247+
## 可选:允许远程 MCP Bridge 连接
248+
249+
默认情况下,WebSocket 服务器绑定到 'localhost'。要允许来自其他设备的 MCP Bridge 连接,请执行以下步骤:
250+
251+
1. 打开 Unity 编辑器
252+
2. 依次点击菜单「Tools > MCP Unity > Server Window」
253+
3. 勾选"Allow Remote Connections(允许远程连接)"复选框
254+
4. Unity 将 WebSocket 服务器绑定到 '0.0.0.0'(所有网络接口)
255+
5. 重新启动 Node.js 服务器以应用新的主机配置
256+
6. 在远程运行 MCP Bridge 时,将环境变量 UNITY_HOST 设置为 Unity 所在机器的 IP 地址:
257+
`UNITY_HOST=192.168.1.100 node server.js`
258+
247259
## <a name="debug-server"></a>调试服务器
248260

249261
<details>

Server~/src/unity/mcpUnity.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface UnityResponse {
3434
export class McpUnity {
3535
private logger: Logger;
3636
private port: number | null = null;
37+
private host: string = 'localhost';
3738
private ws: WebSocket | null = null;
3839
private pendingRequests: Map<string, PendingRequest> = new Map<string, PendingRequest>();
3940
private requestTimeout = 10000;
@@ -80,6 +81,10 @@ export class McpUnity {
8081
this.port = configPort ? parseInt(configPort, 10) : 8090;
8182
this.logger.info(`Using port: ${this.port} for Unity WebSocket connection`);
8283

84+
// Check environment variable first, then config file, then default to localhost
85+
const configHost = process.env.UNITY_HOST || config.Host;
86+
this.host = configHost || 'localhost';
87+
8388
// Initialize timeout from environment variable (in seconds; it is the same as Cline) or use default (10 seconds)
8489
const configTimeout = config.RequestTimeoutSeconds;
8590
this.requestTimeout = configTimeout ? parseInt(configTimeout, 10) * 1000 : 10000;
@@ -100,7 +105,7 @@ export class McpUnity {
100105
this.disconnect();
101106

102107
return new Promise<void>((resolve, reject) => {
103-
const wsUrl = `ws://localhost:${this.port}/McpUnity`;
108+
const wsUrl = `ws://${this.host}:${this.port}/McpUnity`;
104109
this.logger.debug(`Connecting to ${wsUrl}...`);
105110

106111
// Create connection options with headers for client identification

0 commit comments

Comments
 (0)