Skip to content

Commit 2d40387

Browse files
committed
Added toggle to enable and disable the logs into the Unity console. This cleans up the console for the user's interest
Added new Logger to control console logs inside MCP package
1 parent fb1e284 commit 2d40387

13 files changed

+109
-38
lines changed

Editor/Services/ConsoleLogsService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public void StopListening()
6464
// Unity 2022.3 implementation using reflection
6565
EditorApplication.update -= CheckConsoleClearViaReflection;
6666
#endif
67-
68-
Debug.Log("[MCP Unity] Console logs service shutdown");
6967
}
7068

7169
/// <summary>

Editor/Tools/AddAssetToSceneTool.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditor;
44
using Newtonsoft.Json.Linq;
55
using McpUnity.Unity;
6+
using McpUnity.Utils;
67

78
namespace McpUnity.Tools
89
{
@@ -113,7 +114,7 @@ public override JObject Execute(JObject parameters)
113114
}
114115
else
115116
{
116-
Debug.LogWarning($"[MCP Unity] Parent object not found, asset will be created at the root of the scene");
117+
McpLogger.LogWarning($"Parent object not found, asset will be created at the root of the scene");
117118
}
118119
}
119120

@@ -130,7 +131,7 @@ public override JObject Execute(JObject parameters)
130131
}
131132

132133
// Log the action
133-
Debug.Log($"[MCP Unity] Added asset '{asset.name}' to scene from path '{assetPath}'");
134+
McpLogger.LogInfo($"Added asset '{asset.name}' to scene from path '{assetPath}'");
134135

135136
// Create the response
136137
return new JObject

Editor/Tools/AddPackageTool.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using McpUnity.Tools;
1111
using McpUnity.Unity;
12+
using McpUnity.Utils;
1213

1314
namespace McpUnity.Tools
1415
{
@@ -124,7 +125,7 @@ private AddRequest AddFromRegistry(JObject parameters, TaskCompletionSource<JObj
124125
packageIdentifier = $"{packageName}@{version}";
125126
}
126127

127-
Debug.Log($"[MCP Unity] Adding package from registry: {packageIdentifier}");
128+
McpLogger.LogInfo($"Adding package from registry: {packageIdentifier}");
128129

129130
try
130131
{
@@ -188,7 +189,7 @@ private AddRequest AddFromGitHub(JObject parameters, TaskCompletionSource<JObjec
188189
}
189190
}
190191

191-
Debug.Log($"[MCP Unity] Adding package from GitHub: {packageUrl}");
192+
McpLogger.LogInfo($"Adding package from GitHub: {packageUrl}");
192193

193194
try
194195
{
@@ -225,7 +226,7 @@ private AddRequest AddFromDisk(JObject parameters, TaskCompletionSource<JObject>
225226
// Format as file URL
226227
string packageUrl = $"file:{path}";
227228

228-
Debug.Log($"[MCP Unity] Adding package from disk: {packageUrl}");
229+
McpLogger.LogInfo($"Adding package from disk: {packageUrl}");
229230

230231
try
231232
{
@@ -289,7 +290,7 @@ private void ProcessCompletedOperation(PackageOperation operation)
289290
{
290291
if (operation.CompletionSource == null)
291292
{
292-
Debug.LogError("[MCP Unity] TaskCompletionSource is null when processing completed operation");
293+
McpLogger.LogError("TaskCompletionSource is null when processing completed operation");
293294
return;
294295
}
295296

@@ -321,6 +322,8 @@ private void ProcessCompletedOperation(PackageOperation operation)
321322
["message"] = $"Package operation completed successfully, but no package information was returned."
322323
});
323324
}
325+
326+
McpLogger.LogInfo($"Added package {result.displayName} ({result.name}) version {result.version}");
324327
}
325328
else if (operation.Request.Status == StatusCode.Failure)
326329
{

Editor/Tools/MenuItemTool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using McpUnity.Unity;
4+
using McpUnity.Utils;
45
using UnityEngine;
56
using UnityEditor;
67
using Newtonsoft.Json.Linq;
@@ -35,7 +36,7 @@ public override JObject Execute(JObject parameters)
3536
}
3637

3738
// Log the execution
38-
Debug.Log($"[MCP Unity] Executing menu item: {menuPath}");
39+
McpLogger.LogInfo($"[MCP Unity] Executing menu item: {menuPath}");
3940

4041
// Execute the menu item
4142
bool success = EditorApplication.ExecuteMenuItem(menuPath);

Editor/Tools/RunTestsTool.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using UnityEditor.TestTools.TestRunner.Api;
99
using McpUnity.Services;
10+
using McpUnity.Utils;
1011

1112
namespace McpUnity.Tools
1213
{
@@ -81,7 +82,7 @@ public override void ExecuteAsync(JObject parameters, TaskCompletionSource<JObje
8182
}
8283

8384
// Log the execution
84-
Debug.Log($"[MCP Unity] Running tests: Mode={testMode}, Filter={testFilter}");
85+
McpLogger.LogInfo($"[MCP Unity] Running tests: Mode={testMode}, Filter={testFilter}");
8586

8687
// Reset state
8788
_isRunning = true;
@@ -101,7 +102,7 @@ public override void ExecuteAsync(JObject parameters, TaskCompletionSource<JObje
101102
// Called when a test run starts
102103
public void RunStarted(ITestAdaptor testsToRun)
103104
{
104-
Debug.Log($"[MCP Unity] Test run started: {testsToRun.Name}");
105+
McpLogger.LogInfo($"[MCP Unity] Test run started: {testsToRun.Name}");
105106
}
106107

107108
// Called when a test runs
@@ -122,13 +123,13 @@ public void TestFinished(ITestResultAdaptor result)
122123
Duration = result.Duration
123124
});
124125

125-
Debug.Log($"[MCP Unity] Test finished: {result.Test.Name} - {result.ResultState}");
126+
McpLogger.LogInfo($"[MCP Unity] Test finished: {result.Test.Name} - {result.ResultState}");
126127
}
127128

128129
// Called when a test run completes
129130
public void RunFinished(ITestResultAdaptor result)
130131
{
131-
Debug.Log($"[MCP Unity] Test run completed: {result.Test.Name} - {result.ResultState}");
132+
McpLogger.LogInfo($"[MCP Unity] {_testResults.Count} tests completed in {result.Duration}: {result.Test.Name} - {result.ResultState}");
132133

133134
_isRunning = false;
134135

@@ -170,7 +171,7 @@ public void RunFinished(ITestResultAdaptor result)
170171
}
171172
catch (Exception ex)
172173
{
173-
Debug.LogError($"[MCP Unity] Failed to set test results: {ex.Message}");
174+
McpLogger.LogError($"[MCP Unity] Failed to set test results: {ex.Message}");
174175
_testRunCompletionSource?.TrySetException(ex);
175176
}
176177
finally

Editor/Tools/SelectGameObjectTool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using McpUnity.Unity;
4+
using McpUnity.Utils;
45
using UnityEngine;
56
using UnityEditor;
67
using Newtonsoft.Json.Linq;
@@ -53,7 +54,7 @@ public override JObject Execute(JObject parameters)
5354
EditorGUIUtility.PingObject(Selection.activeGameObject);
5455

5556
// Log the selection
56-
Debug.Log($"[MCP Unity] Selected GameObject: " +
57+
McpLogger.LogInfo($"[MCP Unity] Selected GameObject: " +
5758
(instanceId.HasValue ? $"Instance ID {instanceId.Value}" : $"Path '{objectPath}'"));
5859

5960
// Create the response

Editor/Tools/UpdateComponentTool.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Reflection;
33
using McpUnity.Unity;
4+
using McpUnity.Utils;
45
using UnityEngine;
56
using UnityEditor;
67
using Newtonsoft.Json.Linq;
@@ -77,7 +78,7 @@ public override JObject Execute(JObject parameters)
7778
);
7879
}
7980

80-
Debug.Log($"[MCP Unity] Updating component '{componentName}' on GameObject '{gameObject.name}' (found by {identifier})");
81+
McpLogger.LogInfo($"[MCP Unity] Updating component '{componentName}' on GameObject '{gameObject.name}' (found by {identifier})");
8182

8283
// Try to find the component by name
8384
Component component = gameObject.GetComponent(componentName);
@@ -97,7 +98,7 @@ public override JObject Execute(JObject parameters)
9798

9899
component = Undo.AddComponent(gameObject, componentType);
99100
wasAdded = true;
100-
Debug.Log($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'");
101+
McpLogger.LogInfo($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'");
101102
}
102103

103104
// Update component fields
@@ -272,7 +273,7 @@ private bool UpdateComponentData(Component component, JObject componentData)
272273
}
273274
else
274275
{
275-
Debug.LogWarning($"[MCP Unity] Field '{fieldName}' not found on component '{componentType.Name}'");
276+
McpLogger.LogWarning($"Field '{fieldName}' not found on component '{componentType.Name}'");
276277
}
277278
}
278279

@@ -402,7 +403,7 @@ private object ConvertJTokenToValue(JToken token, Type targetType)
402403
}
403404
catch (Exception ex)
404405
{
405-
Debug.LogError($"[MCP Unity] Error converting value to type {targetType.Name}: {ex.Message}");
406+
McpLogger.LogError($"[MCP Unity] Error converting value to type {targetType.Name}: {ex.Message}");
406407
return null;
407408
}
408409
}

Editor/UnityBridge/McpUnityEditorWindow.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ private void DrawServerTab()
117117

118118
EditorGUILayout.Space();
119119

120+
// Enable info logs toggle
121+
bool enableInfoLogs = EditorGUILayout.Toggle(new GUIContent("Enable Info Logs", "Show informational logs in the Unity console"), settings.EnableInfoLogs);
122+
if (enableInfoLogs != settings.EnableInfoLogs)
123+
{
124+
settings.EnableInfoLogs = enableInfoLogs;
125+
settings.SaveSettings();
126+
}
127+
128+
EditorGUILayout.Space();
129+
120130
// Server control buttons
121131
EditorGUILayout.BeginHorizontal();
122132

@@ -209,12 +219,6 @@ private void DrawServerTab()
209219

210220
EditorGUILayout.Space();
211221

212-
//EditorGUILayout.BeginVertical(_connectedClientBoxStyle);
213-
//EditorGUILayout.LabelField("Cursor IDE configuration is done automatically. Restart your Cursor IDE to apply the changes.", EditorStyles.boldLabel);
214-
//EditorGUILayout.EndVertical();
215-
216-
//EditorGUILayout.Space();
217-
218222
if (GUILayout.Button("Configure Claude Desktop", GUILayout.Height(30)))
219223
{
220224
bool added = McpConfigUtils.AddToClaudeDesktopConfig(_tabsIndentationJson);

Editor/UnityBridge/McpUnityServer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using McpUnity.Tools;
1010
using McpUnity.Resources;
1111
using McpUnity.Services;
12+
using McpUnity.Utils;
1213
using WebSocketSharp;
1314
using WebSocketSharp.Server;
1415

@@ -76,7 +77,7 @@ private McpUnityServer()
7677
RegisterResources();
7778
RegisterTools();
7879

79-
Debug.Log($"[MCP Unity] Created WebSocket server on port {McpUnitySettings.Instance.Port}");
80+
McpLogger.LogInfo($"Created WebSocket server on port {McpUnitySettings.Instance.Port}");
8081

8182
if (McpUnitySettings.Instance.AutoStartServer || Application.internetReachability != NetworkReachability.NotReachable)
8283
{
@@ -101,11 +102,11 @@ public void StartServer()
101102
// Start the server
102103
_webSocketServer.Start();
103104

104-
Debug.Log("[MCP Unity] WebSocket server started");
105+
McpLogger.LogInfo("WebSocket server started");
105106
}
106107
catch (Exception ex)
107108
{
108-
Debug.LogError($"[MCP Unity] Failed to start WebSocket server: {ex.Message}");
109+
McpLogger.LogError($"Failed to start WebSocket server: {ex.Message}");
109110
}
110111
}
111112

@@ -120,11 +121,11 @@ public void StopServer()
120121
{
121122
_webSocketServer?.Stop();
122123

123-
Debug.Log("[MCP Unity] WebSocket server stopped");
124+
McpLogger.LogInfo("WebSocket server stopped");
124125
}
125126
catch (Exception ex)
126127
{
127-
Debug.LogError($"[MCP Unity] Error stopping WebSocket server: {ex.Message}");
128+
McpLogger.LogError($"Error stopping WebSocket server: {ex.Message}");
128129
}
129130
}
130131

Editor/UnityBridge/McpUnitySettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class McpUnitySettings
2424

2525
[Tooltip("Whether to automatically start the MCP server when Unity opens")]
2626
public bool AutoStartServer = true;
27+
28+
[Tooltip("Whether to show info logs in the Unity console")]
29+
public bool EnableInfoLogs = true;
2730

2831
/// <summary>
2932
/// Singleton instance of settings
@@ -72,6 +75,7 @@ public void LoadSettings()
7275
}
7376
catch (Exception ex)
7477
{
78+
// Can't use LoggerService here as it depends on settings
7579
Debug.LogError($"[MCP Unity] Failed to load settings: {ex.Message}");
7680
}
7781
}
@@ -93,6 +97,7 @@ public void SaveSettings()
9397
}
9498
catch (Exception ex)
9599
{
100+
// Can't use LoggerService here as it might create circular dependency
96101
Debug.LogError($"[MCP Unity] Failed to save settings: {ex.Message}");
97102
}
98103
}

Editor/UnityBridge/McpUnitySocketHandler.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Unity.EditorCoroutines.Editor;
1212
using System.Collections;
1313
using System.Collections.Specialized;
14+
using McpUnity.Utils;
1415

1516
namespace McpUnity.Unity
1617
{
@@ -54,7 +55,7 @@ protected override async void OnMessage(MessageEventArgs e)
5455
{
5556
try
5657
{
57-
Debug.Log($"[MCP Unity] WebSocket message received: {e.Data}");
58+
McpLogger.LogInfo($"WebSocket message received: {e.Data}");
5859

5960
var requestJson = JObject.Parse(e.Data);
6061
var method = requestJson["method"]?.ToString();
@@ -87,14 +88,14 @@ protected override async void OnMessage(MessageEventArgs e)
8788
JObject jsonRpcResponse = CreateResponse(requestId, responseJson);
8889
string responseStr = jsonRpcResponse.ToString(Formatting.None);
8990

90-
Debug.Log($"[MCP Unity] WebSocket message response: {responseStr}");
91+
McpLogger.LogInfo($"WebSocket message response: {responseStr}");
9192

9293
// Send the response back to the client
9394
Send(responseStr);
9495
}
9596
catch (Exception ex)
9697
{
97-
Debug.LogError($"[MCP Unity] Error processing message: {ex.Message}");
98+
McpLogger.LogError($"Error processing message: {ex.Message}");
9899

99100
Send(CreateErrorResponse($"Internal server error: {ex.Message}", "internal_error").ToString(Formatting.None));
100101
}
@@ -116,7 +117,7 @@ protected override void OnOpen()
116117
_server.Clients.Add(ID, clientName);
117118
}
118119

119-
Debug.Log($"[MCP Unity] WebSocket client '{clientName}' connected");
120+
McpLogger.LogInfo($"WebSocket client '{clientName}' connected");
120121
}
121122

122123
/// <summary>
@@ -129,15 +130,15 @@ protected override void OnClose(CloseEventArgs e)
129130
// Remove the client from the server
130131
_server.Clients.Remove(ID);
131132

132-
Debug.Log($"[MCP Unity] WebSocket client '{clientName}' disconnected: {e.Reason}");
133+
McpLogger.LogInfo($"WebSocket client '{clientName}' disconnected: {e.Reason}");
133134
}
134135

135136
/// <summary>
136137
/// Handle WebSocket errors
137138
/// </summary>
138139
protected override void OnError(ErrorEventArgs e)
139140
{
140-
Debug.LogError($"[MCP Unity] WebSocket error: {e.Message}");
141+
McpLogger.LogError($"WebSocket error: {e.Message}");
141142
}
142143

143144
/// <summary>
@@ -159,7 +160,7 @@ private IEnumerator ExecuteTool(McpToolBase tool, JObject parameters, TaskComple
159160
}
160161
catch (Exception ex)
161162
{
162-
Debug.LogError($"[MCP Unity] Error executing tool {tool.Name}: {ex.Message}");
163+
McpLogger.LogError($"Error executing tool {tool.Name}: {ex.Message}");
163164
tcs.SetResult(CreateErrorResponse(
164165
$"Failed to execute tool {tool.Name}: {ex.Message}",
165166
"tool_execution_error"
@@ -181,7 +182,7 @@ private IEnumerator FetchResourceCoroutine(McpResourceBase resource, JObject par
181182
}
182183
catch (Exception ex)
183184
{
184-
Debug.LogError($"[MCP Unity] Error fetching resource {resource.Name}: {ex.Message}");
185+
McpLogger.LogError($"Error fetching resource {resource.Name}: {ex.Message}");
185186
tcs.SetResult(CreateErrorResponse(
186187
$"Failed to fetch resource {resource.Name}: {ex.Message}",
187188
"resource_fetch_error"

0 commit comments

Comments
 (0)