Skip to content

Commit 7f0bf04

Browse files
dsarnoakshay-kiddopiaclaude
committed
feat: Add OpenCode (opencode.ai) client configurator
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 <[email protected]> Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 9682e3c commit 7f0bf04

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using MCPForUnity.Editor.Helpers;
5+
using MCPForUnity.Editor.Models;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
namespace MCPForUnity.Editor.Clients.Configurators
10+
{
11+
/// <summary>
12+
/// Configurator for OpenCode (opencode.ai) - a Go-based terminal AI coding assistant.
13+
/// OpenCode uses ~/.config/opencode/opencode.json with a custom "mcp" format.
14+
/// </summary>
15+
public class OpenCodeConfigurator : McpClientConfiguratorBase
16+
{
17+
private const string ServerName = "unityMCP";
18+
private const string SchemaUrl = "https://opencode.ai/config.json";
19+
20+
public OpenCodeConfigurator() : base(new McpClient
21+
{
22+
name = "OpenCode",
23+
windowsConfigPath = BuildConfigPath(),
24+
macConfigPath = BuildConfigPath(),
25+
linuxConfigPath = BuildConfigPath()
26+
})
27+
{ }
28+
29+
private static string BuildConfigPath()
30+
{
31+
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
32+
return Path.Combine(home, ".config", "opencode", "opencode.json");
33+
}
34+
35+
public override string GetConfigPath() => CurrentOsPath();
36+
37+
public override McpStatus CheckStatus(bool attemptAutoRewrite = true)
38+
{
39+
try
40+
{
41+
string path = GetConfigPath();
42+
if (!File.Exists(path))
43+
{
44+
client.SetStatus(McpStatus.NotConfigured);
45+
return client.status;
46+
}
47+
48+
var config = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path));
49+
var unityMcp = config?["mcp"]?[ServerName] as JObject;
50+
51+
if (unityMcp == null)
52+
{
53+
client.SetStatus(McpStatus.NotConfigured);
54+
return client.status;
55+
}
56+
57+
string configuredUrl = unityMcp["url"]?.ToString();
58+
string expectedUrl = HttpEndpointUtility.GetMcpRpcUrl();
59+
60+
if (UrlsEqual(configuredUrl, expectedUrl))
61+
{
62+
client.SetStatus(McpStatus.Configured);
63+
}
64+
else if (attemptAutoRewrite)
65+
{
66+
Configure();
67+
}
68+
else
69+
{
70+
client.SetStatus(McpStatus.IncorrectPath);
71+
}
72+
}
73+
catch (Exception ex)
74+
{
75+
client.SetStatus(McpStatus.Error, ex.Message);
76+
}
77+
78+
return client.status;
79+
}
80+
81+
public override void Configure()
82+
{
83+
string path = GetConfigPath();
84+
McpConfigurationHelper.EnsureConfigDirectoryExists(path);
85+
86+
JObject config = File.Exists(path)
87+
? JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path)) ?? new JObject()
88+
: new JObject { ["$schema"] = SchemaUrl };
89+
90+
var mcpSection = config["mcp"] as JObject ?? new JObject();
91+
config["mcp"] = mcpSection;
92+
93+
mcpSection[ServerName] = BuildServerEntry();
94+
95+
McpConfigurationHelper.WriteAtomicFile(path, JsonConvert.SerializeObject(config, Formatting.Indented));
96+
client.SetStatus(McpStatus.Configured);
97+
}
98+
99+
public override string GetManualSnippet()
100+
{
101+
var snippet = new JObject
102+
{
103+
["mcp"] = new JObject { [ServerName] = BuildServerEntry() }
104+
};
105+
return JsonConvert.SerializeObject(snippet, Formatting.Indented);
106+
}
107+
108+
public override IList<string> GetInstallationSteps() => new List<string>
109+
{
110+
"Install OpenCode (https://opencode.ai)",
111+
"Click Configure to add Unity MCP to ~/.config/opencode/opencode.json",
112+
"Restart OpenCode",
113+
"The Unity MCP server should be detected automatically"
114+
};
115+
116+
private static JObject BuildServerEntry() => new JObject
117+
{
118+
["type"] = "remote",
119+
["url"] = HttpEndpointUtility.GetMcpRpcUrl(),
120+
["enabled"] = true
121+
};
122+
}
123+
}

MCPForUnity/Editor/Clients/Configurators/OpenCodeConfigurator.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)