From 408e574b2cf99e1ff0a3f38cfd7a0705129aee2f Mon Sep 17 00:00:00 2001 From: Scriptwonder <1300285021@qq.com> Date: Wed, 19 Mar 2025 00:08:12 -0400 Subject: [PATCH] Update MCPEditorWindow.cs Appending to the config instead of overwrite --- Editor/MCPEditorWindow.cs | 55 +++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/Editor/MCPEditorWindow.cs b/Editor/MCPEditorWindow.cs index f581d2854..f3579b114 100644 --- a/Editor/MCPEditorWindow.cs +++ b/Editor/MCPEditorWindow.cs @@ -354,31 +354,54 @@ private void ConfigureClaudeDesktop() UnityEngine.Debug.Log($"Using server.py at: {serverPath}"); UnityEngine.Debug.Log($"Python directory: {pythonDir}"); - // Create configuration object - var config = new MCPConfig + // Load existing configuration if it exists + dynamic existingConfig = null; + if (File.Exists(configPath)) { - mcpServers = new MCPConfigServers + try { - unityMCP = new MCPConfigServer - { - command = "uv", - args = new[] - { - "--directory", - pythonDir, - "run", - "server.py" - } - } + string existingJson = File.ReadAllText(configPath); + existingConfig = JsonConvert.DeserializeObject(existingJson); } - }; + catch (Exception ex) + { + UnityEngine.Debug.LogWarning($"Failed to parse existing Claude config: {ex.Message}. Creating new config."); + } + } + // If no existing config or parsing failed, create a new one + if (existingConfig == null) + { + existingConfig = new + { + mcpServers = new Dictionary() + }; + } + + // Create the Unity MCP server configuration + var unityMCPConfig = new MCPConfigServer + { + command = "uv", + args = new[] + { + "--directory", + pythonDir, + "run", + "server.py" + } + }; + // Add or update the Unity MCP configuration while preserving the rest + var mcpServers = existingConfig.mcpServers as Newtonsoft.Json.Linq.JObject + ?? new Newtonsoft.Json.Linq.JObject(); + + mcpServers["unityMCP"] = Newtonsoft.Json.Linq.JToken.FromObject(unityMCPConfig); + existingConfig.mcpServers = mcpServers; // Serialize and write to file with proper formatting var jsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented }; - string jsonConfig = JsonConvert.SerializeObject(config, jsonSettings); + string jsonConfig = JsonConvert.SerializeObject(existingConfig, jsonSettings); File.WriteAllText(configPath, jsonConfig); claudeConfigStatus = "Configured successfully";