-
Notifications
You must be signed in to change notification settings - Fork 666
feat: Add OpenCode (opencode.ai) client configurator #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add support for the OpenCode CLI client with automatic configuration. - Create OpenCodeConfigurator implementing IClientConfigurator - Configure via ~/.config/opencode/opencode.json (XDG standard path) - Use McpConfigurationHelper for atomic file writes and directory creation - Support both new config creation and merging with existing config Co-Authored-By: akshay-kiddopia <akshay@kiddopia.com> Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Reviewer's GuideAdds a new OpenCode (opencode.ai) MCP client configurator that reads/writes ~/.config/opencode/opencode.json, ensuring safe creation/merging of the OpenCode MCP configuration and exposing manual snippet and installation steps for Unity MCP integration. Class diagram for new OpenCode MCP client configuratorclassDiagram
class McpClient {
string name
string windowsConfigPath
string macConfigPath
string linuxConfigPath
McpStatus status
SetStatus(status McpStatus, message string)
}
class McpStatus {
}
class McpClientConfiguratorBase {
McpClient client
McpClientConfiguratorBase(client McpClient)
string CurrentOsPath()
string GetConfigPath()
McpStatus CheckStatus(attemptAutoRewrite bool)
void Configure()
string GetManualSnippet()
IList~string~ GetInstallationSteps()
}
class OpenCodeConfigurator {
-const string ServerName
-const string SchemaUrl
+OpenCodeConfigurator()
-static string BuildConfigPath()
+string GetConfigPath()
+McpStatus CheckStatus(attemptAutoRewrite bool)
+void Configure()
+string GetManualSnippet()
+IList~string~ GetInstallationSteps()
-static JObject BuildServerEntry()
}
class McpConfigurationHelper {
+static void EnsureConfigDirectoryExists(path string)
+static void WriteAtomicFile(path string, contents string)
}
class HttpEndpointUtility {
+static string GetMcpRpcUrl()
}
class JObject {
+JToken Item[string key]
}
McpClientConfiguratorBase <|-- OpenCodeConfigurator
OpenCodeConfigurator --> McpClient : uses
OpenCodeConfigurator --> McpStatus : sets
OpenCodeConfigurator ..> McpConfigurationHelper : calls
OpenCodeConfigurator ..> HttpEndpointUtility : calls
OpenCodeConfigurator ..> JObject : config_json
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Caution Review failedThe pull request is closed. Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughAdds an OpenCodeConfigurator class to integrate OpenCode (opencode.ai) with Unity MCP. The configurator reads/writes the per-OS OpenCode config (~/.config/opencode/opencode.json), validates and optionally auto-corrects the MCP RPC URL, exposes status and manual snippet methods, and writes the MCP entry under Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 1 issue, and left some high level feedback:
- In both
CheckStatusandConfigure,JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path))is used without handling malformed JSON; consider catchingJsonExceptionseparately and either backing up/overwriting the bad config or surfacing a clearer error state instead of treating all exceptions the same. - You currently deserialize and read the config file multiple times; consider refactoring the file read/deserialize logic into a small helper to avoid duplication and ensure consistent handling of missing/invalid config files.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In both `CheckStatus` and `Configure`, `JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path))` is used without handling malformed JSON; consider catching `JsonException` separately and either backing up/overwriting the bad config or surfacing a clearer error state instead of treating all exceptions the same.
- You currently deserialize and read the config file multiple times; consider refactoring the file read/deserialize logic into a small helper to avoid duplication and ensure consistent handling of missing/invalid config files.
## Individual Comments
### Comment 1
<location> `MCPForUnity/Editor/Clients/Configurators/OpenCodeConfigurator.cs:81-97` </location>
<code_context>
+ return client.status;
+ }
+
+ public override void Configure()
+ {
+ string path = GetConfigPath();
+ McpConfigurationHelper.EnsureConfigDirectoryExists(path);
+
+ JObject config = File.Exists(path)
+ ? JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path)) ?? new JObject()
+ : new JObject { ["$schema"] = SchemaUrl };
+
+ var mcpSection = config["mcp"] as JObject ?? new JObject();
+ config["mcp"] = mcpSection;
+
+ mcpSection[ServerName] = BuildServerEntry();
+
+ McpConfigurationHelper.WriteAtomicFile(path, JsonConvert.SerializeObject(config, Formatting.Indented));
+ client.SetStatus(McpStatus.Configured);
+ }
+
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider handling I/O/JSON errors in Configure similarly to CheckStatus
Here, any `IOException`, `UnauthorizedAccessException`, or JSON parsing error will propagate and may crash the editor, unlike in `CheckStatus`. Consider wrapping this method in a similar try/catch and setting `McpStatus.Error` with a message so the UI can show a clear failure state instead of an unhandled exception.
```suggestion
public override void Configure()
{
try
{
string path = GetConfigPath();
McpConfigurationHelper.EnsureConfigDirectoryExists(path);
JObject config = File.Exists(path)
? JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path)) ?? new JObject()
: new JObject { ["$schema"] = SchemaUrl };
var mcpSection = config["mcp"] as JObject ?? new JObject();
config["mcp"] = mcpSection;
mcpSection[ServerName] = BuildServerEntry();
McpConfigurationHelper.WriteAtomicFile(path, JsonConvert.SerializeObject(config, Formatting.Indented));
client.SetStatus(McpStatus.Configured);
}
catch (Exception ex)
{
client.SetStatus(McpStatus.Error, ex.Message);
}
}
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@MCPForUnity/Editor/Clients/Configurators/OpenCodeConfigurator.cs`:
- Around line 29-33: BuildConfigPath currently always uses
~/.config/opencode/opencode.json; update it to respect the XDG_CONFIG_HOME
environment variable by first reading
Environment.GetEnvironmentVariable("XDG_CONFIG_HOME") and, if non-empty, use
that directory as the base; otherwise fall back to
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) combined with
".config". Then combine the chosen base with "opencode" and "opencode.json" (use
Path.Combine) so BuildConfigPath returns either
$XDG_CONFIG_HOME/opencode/opencode.json or ~/.config/opencode/opencode.json.
- Add TryLoadConfig() helper to consolidate file read/parse logic - Handle JsonException separately (log warning, return empty object to overwrite) - Wrap Configure() in try/catch to prevent crashes, set McpStatus.Error on failure - Respect XDG_CONFIG_HOME environment variable per XDG Base Directory spec Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Description
Add support for the OpenCode CLI client (opencode.ai) with automatic MCP server configuration. This
allows Unity MCP to automatically configure itself for users of the OpenCode AI coding assistant.
Type of Change
Changes Made
OpenCodeConfigurator.csimplementingIClientConfigurator~/.config/opencode/opencode.json(XDG Base Directory standard)McpConfigurationHelper.EnsureConfigDirectoryExists()andWriteAtomicFile()for safe fileoperations
BuildServerEntry()helper to avoid JSON construction duplicationTesting/Screenshots/Recordings
Related Issues
Fixes #498
Additional Notes
~/.config/opencode/opencode.json)Summary by Sourcery
Add an OpenCode (opencode.ai) client configurator to integrate Unity MCP with the OpenCode CLI via its JSON config file.
New Features:
Summary by CodeRabbit
New Features
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.