Skip to content

Commit fa9a009

Browse files
committed
Fixed cursor add config
1 parent 5b43242 commit fa9a009

File tree

4 files changed

+81
-56
lines changed

4 files changed

+81
-56
lines changed

Editor/Tools/UpdateComponentTool.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Reflection;
4-
using System.Threading.Tasks;
53
using McpUnity.Unity;
64
using UnityEngine;
75
using UnityEditor;

Editor/UnityBridge/McpUnityEditorWindow.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,47 @@ private void DrawServerTab()
186186

187187
if (GUILayout.Button("Configure Windsurf IDE", GUILayout.Height(30)))
188188
{
189-
McpConfigUtils.AddToWindsurfIdeConfig(_tabsIndentationJson);
189+
bool added = McpConfigUtils.AddToWindsurfIdeConfig(_tabsIndentationJson);
190+
if (added)
191+
{
192+
EditorUtility.DisplayDialog("Success", "The MCP configuration was successfully added to the Windsurf config file.", "OK");
193+
}
194+
else
195+
{
196+
EditorUtility.DisplayDialog("Error", "The MCP configuration could not be added to the Windsurf config file.", "OK");
197+
}
190198
}
191199

192200
EditorGUILayout.Space();
193201

194-
if (GUILayout.Button("Configure Cursor IDE", GUILayout.Height(30)))
202+
//EditorGUILayout.BeginVertical(_connectedClientBoxStyle);
203+
//EditorGUILayout.LabelField("Cursor IDE configuration is done automatically. Restart your Cursor IDE to apply the changes.", EditorStyles.boldLabel);
204+
//EditorGUILayout.EndVertical();
205+
206+
//EditorGUILayout.Space();
207+
208+
if (GUILayout.Button("Configure Claude Desktop", GUILayout.Height(30)))
195209
{
196-
McpConfigUtils.AddToCursorIdeConfig();
210+
bool added = McpConfigUtils.AddToClaudeDesktopConfig(_tabsIndentationJson);
211+
if (added)
212+
{
213+
EditorUtility.DisplayDialog("Success", "The MCP configuration was successfully added to the Claude Desktop config file.", "OK");
214+
}
215+
else
216+
{
217+
EditorUtility.DisplayDialog("Error", "The MCP configuration could not be added to the Claude Desktop config file.", "OK");
218+
}
197219
}
198220

199221
EditorGUILayout.Space();
200222

201-
if (GUILayout.Button("Configure Claude Desktop", GUILayout.Height(30)))
223+
if (GUILayout.Button("Configure Cursor", GUILayout.Height(30)))
202224
{
203-
McpConfigUtils.AddToClaudeDesktopConfig(_tabsIndentationJson);
225+
bool added = McpConfigUtils.AddToCursorConfig(_tabsIndentationJson);
226+
if (added)
227+
{
228+
EditorUtility.DisplayDialog("Success", "The MCP configuration was successfully added to the Cursor config file.", "OK");
229+
}
204230
}
205231

206232
EditorGUILayout.EndVertical();

Editor/Utils/McpConfigUtils.cs

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,28 @@ public static string GetServerPath()
106106
/// <summary>
107107
/// Adds the MCP configuration to the Windsurf MCP config file
108108
/// </summary>
109-
public static void AddToWindsurfIdeConfig(bool useTabsIndentation)
109+
public static bool AddToWindsurfIdeConfig(bool useTabsIndentation)
110110
{
111111
string configFilePath = GetWindsurfMcpConfigPath();
112-
AddToConfigFile(configFilePath, useTabsIndentation, "Windsurf");
112+
return AddToConfigFile(configFilePath, useTabsIndentation, "Windsurf");
113113
}
114114

115115
/// <summary>
116116
/// Adds the MCP configuration to the Claude Desktop config file
117117
/// </summary>
118-
public static void AddToClaudeDesktopConfig(bool useTabsIndentation)
118+
public static bool AddToClaudeDesktopConfig(bool useTabsIndentation)
119119
{
120120
string configFilePath = GetClaudeDesktopConfigPath();
121-
AddToConfigFile(configFilePath, useTabsIndentation, "Claude Desktop");
121+
return AddToConfigFile(configFilePath, useTabsIndentation, "Claude Desktop");
122+
}
123+
124+
/// <summary>
125+
/// Adds the MCP configuration to the Cursor config file
126+
/// </summary>
127+
public static bool AddToCursorConfig(bool useTabsIndentation)
128+
{
129+
string configFilePath = GetCursorConfigPath();
130+
return AddToConfigFile(configFilePath, useTabsIndentation, "Cursor");
122131
}
123132

124133
/// <summary>
@@ -127,14 +136,15 @@ public static void AddToClaudeDesktopConfig(bool useTabsIndentation)
127136
/// <param name="configFilePath">Path to the config file</param>
128137
/// <param name="useTabsIndentation">Whether to use tabs for indentation</param>
129138
/// <param name="productName">Name of the product (for error messages)</param>
130-
private static void AddToConfigFile(string configFilePath, bool useTabsIndentation, string productName)
139+
/// <returns>True if successfuly added the config, false otherwise</returns>
140+
private static bool AddToConfigFile(string configFilePath, bool useTabsIndentation, string productName)
131141
{
132142
try
133143
{
134144
if (string.IsNullOrEmpty(configFilePath))
135145
{
136-
EditorUtility.DisplayDialog("Error", $"{productName} config file not found. Please make sure {productName} is installed.", "OK");
137-
return;
146+
Debug.LogError($"{productName} config file not found. Please make sure {productName} is installed.");
147+
return false;
138148
}
139149

140150
// Generate fresh MCP config JSON
@@ -148,17 +158,7 @@ private static void AddToConfigFile(string configFilePath, bool useTabsIndentati
148158
{
149159
// Read the existing config
150160
string existingConfigJson = File.ReadAllText(configFilePath);
151-
JObject existingConfig;
152-
153-
try
154-
{
155-
existingConfig = JObject.Parse(existingConfigJson);
156-
}
157-
catch (JsonException)
158-
{
159-
// If the file exists but isn't valid JSON, create a new one
160-
existingConfig = new JObject();
161-
}
161+
JObject existingConfig = JObject.Parse(existingConfigJson);
162162

163163
// Merge the mcpServers from our config into the existing config
164164
if (mcpConfig["mcpServers"] != null && mcpConfig["mcpServers"] is JObject mcpServers)
@@ -177,26 +177,26 @@ private static void AddToConfigFile(string configFilePath, bool useTabsIndentati
177177

178178
// Write the updated config back to the file
179179
File.WriteAllText(configFilePath, existingConfig.ToString(Formatting.Indented));
180-
181-
EditorUtility.DisplayDialog("Success", $"MCP Unity configuration added to {productName}.", "OK");
180+
return true;
182181
}
183182
}
184183
else if(Directory.Exists(Path.GetDirectoryName(configFilePath)))
185184
{
186185
// Create a new config file with just our config
187186
File.WriteAllText(configFilePath, mcpConfigJson);
188-
EditorUtility.DisplayDialog("Success", $"Created new {productName} config file with MCP Unity configuration.", "OK");
187+
return true;
189188
}
190189
else
191190
{
192-
EditorUtility.DisplayDialog("Failed", $"Cannot find {productName} config file or {productName} is currently not installed. Expecting {productName} to be installed in the {configFilePath} path", "OK");
191+
Debug.LogError($"Cannot find {productName} config file or {productName} is currently not installed. Expecting {productName} to be installed in the {configFilePath} path");
193192
}
194193
}
195194
catch (Exception ex)
196195
{
197-
EditorUtility.DisplayDialog("Error", $"Failed to add MCP configuration to {productName}: {ex.Message}", "OK");
198196
Debug.LogError($"Failed to add MCP configuration to {productName}: {ex}");
199197
}
198+
199+
return false;
200200
}
201201

202202
/// <summary>
@@ -262,37 +262,38 @@ private static string GetClaudeDesktopConfigPath()
262262
return Path.Combine(basePath, "claude_desktop_config.json");
263263
}
264264

265+
266+
265267
/// <summary>
266-
/// Adds the MCP configuration to Cursor IDE by generating the command-line format
268+
/// Gets the path to the Cursor config file based on the current OS
267269
/// </summary>
268-
public static void AddToCursorIdeConfig()
270+
/// <returns>The path to the Cursor config file</returns>
271+
private static string GetCursorConfigPath()
269272
{
270-
try
273+
// Base path depends on the OS
274+
string basePath;
275+
276+
if (Application.platform == RuntimePlatform.WindowsEditor)
271277
{
272-
string serverPath = GetServerPath();
273-
string port = McpUnitySettings.Instance.Port.ToString();
274-
275-
// Generate the command-line format for Cursor IDE
276-
string cursorCommand = $"env UNITY_PORT={port} node {serverPath}/build/index.js";
277-
278-
// Copy to clipboard
279-
EditorGUIUtility.systemCopyBuffer = cursorCommand;
280-
281-
// Show instructions to the user
282-
EditorUtility.DisplayDialog(
283-
"Cursor IDE Configuration",
284-
"The Cursor IDE command has been copied to your clipboard. Please add it to Cursor IDE with these settings:\n\n" +
285-
"Name: MCP Unity\n" +
286-
"Type: command\n" +
287-
$"Command: {cursorCommand}\n\n" +
288-
"Go to Cursor → Settings → MCP → Configure and paste this command.",
289-
"OK");
278+
// Windows: %USERPROFILE%/.cursor
279+
basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cursor");
280+
Debug.Log(basePath);
290281
}
291-
catch (Exception ex)
282+
else if (Application.platform == RuntimePlatform.OSXEditor)
292283
{
293-
EditorUtility.DisplayDialog("Error", $"Failed to generate Cursor IDE configuration: {ex.Message}", "OK");
294-
Debug.LogError($"Failed to generate Cursor IDE configuration: {ex}");
284+
// macOS: ~/Library/Application Support/.cursor
285+
string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
286+
basePath = Path.Combine(homeDir, "Library", "Application Support", ".cursor");
287+
}
288+
else
289+
{
290+
// Unsupported platform
291+
Debug.LogError("Unsupported platform for Cursor MCP config");
292+
return null;
295293
}
294+
295+
// Return the path to the mcp_config.json file
296+
return Path.Combine(basePath, "mcp.json");
296297
}
297298
}
298299
}

Server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
"@types/uuid": "^10.0.0",
3333
"@types/winreg": "^1.2.36",
3434
"@types/ws": "^8.18.0",
35-
"typescript": "^5.8.2",
36-
"uuid": "^11.1.0"
35+
"typescript": "^5.8.2"
3736
},
3837
"dependencies": {
3938
"@modelcontextprotocol/sdk": "^1.7.0",
4039
"axios": "^1.8.4",
4140
"cors": "^2.8.5",
4241
"express": "^5.0.1",
42+
"uuid": "^11.1.0",
4343
"winreg": "^1.2.5",
4444
"ws": "^8.18.1",
4545
"zod": "^3.24.2",

0 commit comments

Comments
 (0)