Merge pull request #8 from Scriptwonder/master

Update MCPEditorWindow.cs
main
Justin P Barnett 2025-03-19 10:05:04 -04:00 committed by GitHub
commit 991c80c49e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 16 deletions

View File

@ -354,12 +354,32 @@ 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);
}
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<string, object>()
};
}
// Create the Unity MCP server configuration
var unityMCPConfig = new MCPConfigServer
{ {
command = "uv", command = "uv",
args = new[] args = new[]
@ -369,16 +389,19 @@ public class MCPEditorWindow : EditorWindow
"run", "run",
"server.py" "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";