Update MCPEditorWindow.cs

Appending to the config instead of overwrite
main
Scriptwonder 2025-03-19 00:08:12 -04:00
parent be47f39b12
commit 408e574b2c
1 changed files with 39 additions and 16 deletions

View File

@ -354,31 +354,54 @@ public class MCPEditorWindow : EditorWindow
UnityEngine.Debug.Log($"Using server.py at: {serverPath}"); UnityEngine.Debug.Log($"Using server.py at: {serverPath}");
UnityEngine.Debug.Log($"Python directory: {pythonDir}"); UnityEngine.Debug.Log($"Python directory: {pythonDir}");
// Create configuration object // Load existing configuration if it exists
var config = new MCPConfig dynamic existingConfig = null;
if (File.Exists(configPath))
{ {
mcpServers = new MCPConfigServers try
{ {
unityMCP = new MCPConfigServer string existingJson = File.ReadAllText(configPath);
{ existingConfig = JsonConvert.DeserializeObject(existingJson);
command = "uv", }
args = new[] catch (Exception ex)
{ {
"--directory", UnityEngine.Debug.LogWarning($"Failed to parse existing Claude config: {ex.Message}. Creating new config.");
pythonDir, }
"run", }
"server.py"
} // If no existing config or parsing failed, create a new one
} if (existingConfig == null)
{
existingConfig = new
{
mcpServers = new Dictionary<string, object>()
};
}
// 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 // Serialize and write to file with proper formatting
var jsonSettings = new JsonSerializerSettings var jsonSettings = new JsonSerializerSettings
{ {
Formatting = Formatting.Indented Formatting = Formatting.Indented
}; };
string jsonConfig = JsonConvert.SerializeObject(config, jsonSettings); string jsonConfig = JsonConvert.SerializeObject(existingConfig, jsonSettings);
File.WriteAllText(configPath, jsonConfig); File.WriteAllText(configPath, jsonConfig);
claudeConfigStatus = "Configured successfully"; claudeConfigStatus = "Configured successfully";