-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMcpUnityServer.cs
More file actions
331 lines (295 loc) · 10.4 KB
/
McpUnityServer.cs
File metadata and controls
331 lines (295 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace McpUnity
{
/// <summary>
/// MCP Unity WebSocket服务器
/// 运行在Unity编辑器中,接收外部MCP服务的命令
/// </summary>
public class McpUnityServer : EditorWindow
{
private HttpListener _listener;
private Thread _serverThread;
private bool _isRunning;
private string _status = "Stopped";
private Vector2 _logScrollPosition;
private List<string> _logs = new List<string>();
private const int MaxLogs = 100;
// 异步处理相关
private readonly Queue<PendingRequest> _pendingRequests = new Queue<PendingRequest>();
private readonly object _queueLock = new object();
[MenuItem("MCP/Server")]
public static void ShowWindow()
{
GetWindow<McpUnityServer>("MCP Server");
}
private void OnEnable()
{
Application.logMessageReceived += OnLogMessage;
EditorApplication.update += OnEditorUpdate;
}
private void OnDisable()
{
Application.logMessageReceived -= OnLogMessage;
EditorApplication.update -= OnEditorUpdate;
StopServer();
}
/// <summary>
/// 每帧检查待处理的请求 - 非阻塞方式
/// </summary>
private void OnEditorUpdate()
{
lock (_queueLock)
{
while (_pendingRequests.Count > 0)
{
var request = _pendingRequests.Dequeue();
try
{
request.Process();
}
catch (Exception ex)
{
Debug.LogError($"Error processing request: {ex.Message}");
request.SetError(ex.Message);
}
}
}
}
private void OnLogMessage(string condition, string stackTrace, LogType type)
{
string prefix = type switch
{
LogType.Error => "[ERROR]",
LogType.Warning => "[WARN]",
_ => "[INFO]"
};
AddLog($"{prefix} {condition}");
}
private void AddLog(string message)
{
_logs.Add($"[{DateTime.Now:HH:mm:ss}] {message}");
if (_logs.Count > MaxLogs)
_logs.RemoveAt(0);
Repaint();
}
private void OnGUI()
{
EditorGUILayout.Space(10);
// 状态显示
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Status:", GUILayout.Width(50));
GUI.color = _isRunning ? Color.green : Color.red;
EditorGUILayout.LabelField(_status, EditorStyles.boldLabel);
GUI.color = Color.white;
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(10);
// 控制按钮
EditorGUILayout.BeginHorizontal();
GUI.enabled = !_isRunning;
if (GUILayout.Button("Start Server", GUILayout.Height(30)))
{
StartServer();
}
GUI.enabled = _isRunning;
if (GUILayout.Button("Stop Server", GUILayout.Height(30)))
{
StopServer();
}
GUI.enabled = true;
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(10);
// 日志显示
EditorGUILayout.LabelField("Logs:", EditorStyles.boldLabel);
_logScrollPosition = EditorGUILayout.BeginScrollView(_logScrollPosition, GUILayout.Height(300));
foreach (var log in _logs)
{
EditorGUILayout.LabelField(log, EditorStyles.miniLabel);
}
EditorGUILayout.EndScrollView();
}
private void StartServer()
{
if (_isRunning) return;
try
{
_listener = new HttpListener();
_listener.Prefixes.Add("http://localhost:8090/McpUnity/");
_listener.Start();
_isRunning = true;
_status = "Running (Port 8090)";
AddLog("Server started on http://localhost:8090/McpUnity/");
_serverThread = new Thread(ServerLoop)
{
IsBackground = true
};
_serverThread.Start();
}
catch (Exception ex)
{
_status = $"Error: {ex.Message}";
AddLog($"Failed to start server: {ex.Message}");
}
}
private void StopServer()
{
if (!_isRunning) return;
_isRunning = false;
_listener?.Stop();
_listener?.Close();
_listener = null;
_status = "Stopped";
AddLog("Server stopped");
}
private void ServerLoop()
{
while (_isRunning)
{
try
{
var context = _listener.GetContext();
ThreadPool.QueueUserWorkItem(_ => ProcessRequest(context));
}
catch (Exception ex)
{
if (_isRunning)
{
Debug.LogError($"Server error: {ex.Message}");
}
}
}
}
private void ProcessRequest(HttpListenerContext context)
{
try
{
var request = context.Request;
var response = context.Response;
// 设置CORS头
response.Headers.Add("Access-Control-Allow-Origin", "*");
response.Headers.Add("Access-Control-Allow-Methods", "POST, OPTIONS");
response.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
if (request.HttpMethod == "OPTIONS")
{
response.StatusCode = 200;
response.Close();
return;
}
if (request.HttpMethod != "POST")
{
response.StatusCode = 405;
response.Close();
return;
}
// 读取请求体 - 强制使用 UTF-8 编码以正确处理中文
string requestBody;
using (var reader = new System.IO.StreamReader(request.InputStream, System.Text.Encoding.UTF8))
{
requestBody = reader.ReadToEnd();
}
Debug.Log($"Received: {requestBody}");
// 创建待处理请求并加入队列 - 非阻塞方式
var pendingRequest = new PendingRequest(context, requestBody);
lock (_queueLock)
{
_pendingRequests.Enqueue(pendingRequest);
}
// 使用异步等待,不阻塞线程
pendingRequest.WaitForCompletionAsync();
}
catch (Exception ex)
{
Debug.LogError($"Request processing error: {ex.Message}");
try
{
context.Response.StatusCode = 500;
context.Response.Close();
}
catch { }
}
}
/// <summary>
/// 待处理请求类 - 用于主线程和工作线程间协调
/// </summary>
private class PendingRequest
{
private readonly HttpListenerContext _context;
private readonly string _requestBody;
private string _result;
private bool _isCompleted;
private readonly ManualResetEventSlim _completionEvent;
public PendingRequest(HttpListenerContext context, string requestBody)
{
_context = context;
_requestBody = requestBody;
_completionEvent = new ManualResetEventSlim(false);
}
/// <summary>
/// 在主线程执行命令处理
/// </summary>
public void Process()
{
try
{
_result = CommandProcessor.Process(_requestBody);
}
catch (Exception ex)
{
_result = $"{{\"error\":\"{ex.Message}\"}}";
}
_isCompleted = true;
_completionEvent.Set();
}
public void SetError(string error)
{
_result = $"{{\"error\":\"{error}\"}}";
_isCompleted = true;
_completionEvent.Set();
}
/// <summary>
/// 异步等待完成并发送响应
/// </summary>
public async void WaitForCompletionAsync()
{
// 在后台线程等待完成信号
await Task.Run(() =>
{
// 等待最多10秒
if (!_completionEvent.Wait(TimeSpan.FromSeconds(10)))
{
_result = "{\"error\":\"Request timeout\"}";
}
});
// 发送响应
try
{
byte[] buffer = Encoding.UTF8.GetBytes(_result ?? "{}");
_context.Response.ContentType = "application/json";
_context.Response.ContentLength64 = buffer.Length;
await _context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
_context.Response.Close();
}
catch (Exception ex)
{
Debug.LogError($"Error sending response: {ex.Message}");
try
{
_context.Response.StatusCode = 500;
_context.Response.Close();
}
catch { }
}
finally
{
_completionEvent.Dispose();
}
}
}
}
}