@@ -15,6 +15,7 @@ namespace MCPForUnity.Editor.Clients.Configurators
1515 public class OpenCodeConfigurator : McpClientConfiguratorBase
1616 {
1717 private const string ServerName = "unityMCP" ;
18+ private const string SchemaUrl = "https://opencode.ai/config.json" ;
1819
1920 public OpenCodeConfigurator ( ) : base ( new McpClient
2021 {
@@ -27,8 +28,8 @@ public OpenCodeConfigurator() : base(new McpClient
2728
2829 private static string BuildConfigPath ( )
2930 {
30- string configDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
31- return Path . Combine ( configDir , ".config" , "opencode" , "opencode.json" ) ;
31+ string home = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
32+ return Path . Combine ( home , ".config" , "opencode" , "opencode.json" ) ;
3233 }
3334
3435 public override string GetConfigPath ( ) => CurrentOsPath ( ) ;
@@ -44,17 +45,9 @@ public override McpStatus CheckStatus(bool attemptAutoRewrite = true)
4445 return client . status ;
4546 }
4647
47- string json = File . ReadAllText ( path ) ;
48- var config = JsonConvert . DeserializeObject < JObject > ( json ) ;
49- var mcpSection = config ? [ "mcp" ] as JObject ;
48+ var config = JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( path ) ) ;
49+ var unityMcp = config ? [ "mcp" ] ? [ ServerName ] as JObject ;
5050
51- if ( mcpSection == null || mcpSection [ ServerName ] == null )
52- {
53- client . SetStatus ( McpStatus . NotConfigured ) ;
54- return client . status ;
55- }
56-
57- var unityMcp = mcpSection [ ServerName ] as JObject ;
5851 if ( unityMcp == null )
5952 {
6053 client . SetStatus ( McpStatus . NotConfigured ) ;
@@ -87,70 +80,27 @@ public override McpStatus CheckStatus(bool attemptAutoRewrite = true)
8780
8881 public override void Configure ( )
8982 {
90- try
91- {
92- string path = GetConfigPath ( ) ;
93- string dir = Path . GetDirectoryName ( path ) ;
94- if ( ! Directory . Exists ( dir ) )
95- {
96- Directory . CreateDirectory ( dir ) ;
97- }
83+ string path = GetConfigPath ( ) ;
84+ McpConfigurationHelper . EnsureConfigDirectoryExists ( path ) ;
9885
99- JObject config ;
100- if ( File . Exists ( path ) )
101- {
102- string existingJson = File . ReadAllText ( path ) ;
103- config = JsonConvert . DeserializeObject < JObject > ( existingJson ) ?? new JObject ( ) ;
104- }
105- else
106- {
107- config = new JObject
108- {
109- [ "$schema" ] = "https://opencode.ai/config.json"
110- } ;
111- }
86+ JObject config = File . Exists ( path )
87+ ? JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( path ) ) ?? new JObject ( )
88+ : new JObject { [ "$schema" ] = SchemaUrl } ;
11289
113- var mcpSection = config [ "mcp" ] as JObject ;
114- if ( mcpSection == null )
115- {
116- mcpSection = new JObject ( ) ;
117- config [ "mcp" ] = mcpSection ;
118- }
90+ var mcpSection = config [ "mcp" ] as JObject ?? new JObject ( ) ;
91+ config [ "mcp" ] = mcpSection ;
11992
120- string httpUrl = HttpEndpointUtility . GetMcpRpcUrl ( ) ;
121-
122- mcpSection [ ServerName ] = new JObject
123- {
124- [ "type" ] = "remote" ,
125- [ "url" ] = httpUrl ,
126- [ "enabled" ] = true
127- } ;
93+ mcpSection [ ServerName ] = BuildServerEntry ( ) ;
12894
129- string output = JsonConvert . SerializeObject ( config , Formatting . Indented ) ;
130- File . WriteAllText ( path , output ) ;
131-
132- client . SetStatus ( McpStatus . Configured ) ;
133- }
134- catch ( Exception ex )
135- {
136- throw new InvalidOperationException ( "Failed to configure OpenCode." , ex ) ;
137- }
95+ McpConfigurationHelper . WriteAtomicFile ( path , JsonConvert . SerializeObject ( config , Formatting . Indented ) ) ;
96+ client . SetStatus ( McpStatus . Configured ) ;
13897 }
13998
14099 public override string GetManualSnippet ( )
141100 {
142- string httpUrl = HttpEndpointUtility . GetMcpRpcUrl ( ) ;
143101 var snippet = new JObject
144102 {
145- [ "mcp" ] = new JObject
146- {
147- [ ServerName ] = new JObject
148- {
149- [ "type" ] = "remote" ,
150- [ "url" ] = httpUrl ,
151- [ "enabled" ] = true
152- }
153- }
103+ [ "mcp" ] = new JObject { [ ServerName ] = BuildServerEntry ( ) }
154104 } ;
155105 return JsonConvert . SerializeObject ( snippet , Formatting . Indented ) ;
156106 }
@@ -162,5 +112,12 @@ public override string GetManualSnippet()
162112 "Restart OpenCode" ,
163113 "The Unity MCP server should be detected automatically"
164114 } ;
115+
116+ private static JObject BuildServerEntry ( ) => new JObject
117+ {
118+ [ "type" ] = "remote" ,
119+ [ "url" ] = HttpEndpointUtility . GetMcpRpcUrl ( ) ,
120+ [ "enabled" ] = true
121+ } ;
165122 }
166123}
0 commit comments