Skip to content

Commit 408e574

Browse files
committed
Update MCPEditorWindow.cs
Appending to the config instead of overwrite
1 parent be47f39 commit 408e574

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

Editor/MCPEditorWindow.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,31 +354,54 @@ private void ConfigureClaudeDesktop()
354354
UnityEngine.Debug.Log($"Using server.py at: {serverPath}");
355355
UnityEngine.Debug.Log($"Python directory: {pythonDir}");
356356

357-
// Create configuration object
358-
var config = new MCPConfig
357+
// Load existing configuration if it exists
358+
dynamic existingConfig = null;
359+
if (File.Exists(configPath))
359360
{
360-
mcpServers = new MCPConfigServers
361+
try
361362
{
362-
unityMCP = new MCPConfigServer
363-
{
364-
command = "uv",
365-
args = new[]
366-
{
367-
"--directory",
368-
pythonDir,
369-
"run",
370-
"server.py"
371-
}
372-
}
363+
string existingJson = File.ReadAllText(configPath);
364+
existingConfig = JsonConvert.DeserializeObject(existingJson);
373365
}
374-
};
366+
catch (Exception ex)
367+
{
368+
UnityEngine.Debug.LogWarning($"Failed to parse existing Claude config: {ex.Message}. Creating new config.");
369+
}
370+
}
375371

372+
// If no existing config or parsing failed, create a new one
373+
if (existingConfig == null)
374+
{
375+
existingConfig = new
376+
{
377+
mcpServers = new Dictionary<string, object>()
378+
};
379+
}
380+
381+
// Create the Unity MCP server configuration
382+
var unityMCPConfig = new MCPConfigServer
383+
{
384+
command = "uv",
385+
args = new[]
386+
{
387+
"--directory",
388+
pythonDir,
389+
"run",
390+
"server.py"
391+
}
392+
};
393+
// Add or update the Unity MCP configuration while preserving the rest
394+
var mcpServers = existingConfig.mcpServers as Newtonsoft.Json.Linq.JObject
395+
?? new Newtonsoft.Json.Linq.JObject();
396+
397+
mcpServers["unityMCP"] = Newtonsoft.Json.Linq.JToken.FromObject(unityMCPConfig);
398+
existingConfig.mcpServers = mcpServers;
376399
// Serialize and write to file with proper formatting
377400
var jsonSettings = new JsonSerializerSettings
378401
{
379402
Formatting = Formatting.Indented
380403
};
381-
string jsonConfig = JsonConvert.SerializeObject(config, jsonSettings);
404+
string jsonConfig = JsonConvert.SerializeObject(existingConfig, jsonSettings);
382405
File.WriteAllText(configPath, jsonConfig);
383406

384407
claudeConfigStatus = "Configured successfully";

0 commit comments

Comments
 (0)