|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
5 | | -using System.Text; |
6 | | -using System.Text.RegularExpressions; |
7 | 5 | using MCPForUnity.External.Tommy; |
8 | | -using Newtonsoft.Json; |
9 | 6 |
|
10 | 7 | namespace MCPForUnity.Editor.Helpers |
11 | 8 | { |
@@ -42,108 +39,107 @@ public static bool IsCodexConfigured(string pythonDir) |
42 | 39 |
|
43 | 40 | public static string BuildCodexServerBlock(string uvPath, string serverSrc) |
44 | 41 | { |
45 | | - string argsArray = FormatTomlStringArray(new[] { "run", "--directory", serverSrc, "server.py" }); |
46 | | - return $"[mcp_servers.unityMCP]{Environment.NewLine}" + |
47 | | - $"command = \"{EscapeTomlString(uvPath)}\"{Environment.NewLine}" + |
48 | | - $"args = {argsArray}"; |
| 42 | + var table = new TomlTable(); |
| 43 | + var mcpServers = new TomlTable(); |
| 44 | + |
| 45 | + mcpServers["unityMCP"] = CreateUnityMcpTable(uvPath, serverSrc); |
| 46 | + table["mcp_servers"] = mcpServers; |
| 47 | + |
| 48 | + using var writer = new StringWriter(); |
| 49 | + table.WriteTo(writer); |
| 50 | + return writer.ToString(); |
49 | 51 | } |
50 | 52 |
|
51 | | - public static string UpsertCodexServerBlock(string existingToml, string newBlock) |
| 53 | + public static string UpsertCodexServerBlock(string existingToml, string uvPath, string serverSrc) |
52 | 54 | { |
53 | | - if (string.IsNullOrWhiteSpace(existingToml)) |
| 55 | + // Parse existing TOML or create new root table |
| 56 | + var root = TryParseToml(existingToml) ?? new TomlTable(); |
| 57 | + |
| 58 | + // Ensure mcp_servers table exists |
| 59 | + if (!root.TryGetNode("mcp_servers", out var mcpServersNode) || !(mcpServersNode is TomlTable)) |
54 | 60 | { |
55 | | - return newBlock.TrimEnd() + Environment.NewLine; |
| 61 | + root["mcp_servers"] = new TomlTable(); |
56 | 62 | } |
| 63 | + var mcpServers = root["mcp_servers"] as TomlTable; |
57 | 64 |
|
58 | | - StringBuilder sb = new StringBuilder(); |
59 | | - using StringReader reader = new StringReader(existingToml); |
60 | | - string line; |
61 | | - bool inTarget = false; |
62 | | - bool replaced = false; |
63 | | - while ((line = reader.ReadLine()) != null) |
64 | | - { |
65 | | - string trimmed = line.Trim(); |
66 | | - bool isSection = trimmed.StartsWith("[") && trimmed.EndsWith("]") && !trimmed.StartsWith("[["); |
67 | | - if (isSection) |
68 | | - { |
69 | | - bool isTarget = string.Equals(trimmed, "[mcp_servers.unityMCP]", StringComparison.OrdinalIgnoreCase); |
70 | | - if (isTarget) |
71 | | - { |
72 | | - if (!replaced) |
73 | | - { |
74 | | - if (sb.Length > 0 && sb[^1] != '\n') sb.AppendLine(); |
75 | | - sb.AppendLine(newBlock.TrimEnd()); |
76 | | - replaced = true; |
77 | | - } |
78 | | - inTarget = true; |
79 | | - continue; |
80 | | - } |
| 65 | + // Create or update unityMCP table |
| 66 | + mcpServers["unityMCP"] = CreateUnityMcpTable(uvPath, serverSrc); |
81 | 67 |
|
82 | | - if (inTarget) |
83 | | - { |
84 | | - inTarget = false; |
85 | | - } |
86 | | - } |
| 68 | + // Serialize back to TOML |
| 69 | + using var writer = new StringWriter(); |
| 70 | + root.WriteTo(writer); |
| 71 | + return writer.ToString(); |
| 72 | + } |
87 | 73 |
|
88 | | - if (inTarget) |
89 | | - { |
90 | | - continue; |
91 | | - } |
| 74 | + public static bool TryParseCodexServer(string toml, out string command, out string[] args) |
| 75 | + { |
| 76 | + command = null; |
| 77 | + args = null; |
92 | 78 |
|
93 | | - sb.AppendLine(line); |
| 79 | + var root = TryParseToml(toml); |
| 80 | + if (root == null) return false; |
| 81 | + |
| 82 | + if (!TryGetTable(root, "mcp_servers", out var servers) |
| 83 | + && !TryGetTable(root, "mcpServers", out servers)) |
| 84 | + { |
| 85 | + return false; |
94 | 86 | } |
95 | 87 |
|
96 | | - if (!replaced) |
| 88 | + if (!TryGetTable(servers, "unityMCP", out var unity)) |
97 | 89 | { |
98 | | - if (sb.Length > 0 && sb[^1] != '\n') sb.AppendLine(); |
99 | | - sb.AppendLine(newBlock.TrimEnd()); |
| 90 | + return false; |
100 | 91 | } |
101 | 92 |
|
102 | | - return sb.ToString().TrimEnd() + Environment.NewLine; |
| 93 | + command = GetTomlString(unity, "command"); |
| 94 | + args = GetTomlStringArray(unity, "args"); |
| 95 | + |
| 96 | + return !string.IsNullOrEmpty(command) && args != null; |
103 | 97 | } |
104 | 98 |
|
105 | | - public static bool TryParseCodexServer(string toml, out string command, out string[] args) |
| 99 | + /// <summary> |
| 100 | + /// Safely parses TOML string, returning null on failure |
| 101 | + /// </summary> |
| 102 | + private static TomlTable TryParseToml(string toml) |
106 | 103 | { |
107 | | - command = null; |
108 | | - args = null; |
109 | | - if (string.IsNullOrWhiteSpace(toml)) return false; |
| 104 | + if (string.IsNullOrWhiteSpace(toml)) return null; |
110 | 105 |
|
111 | 106 | try |
112 | 107 | { |
113 | 108 | using var reader = new StringReader(toml); |
114 | | - TomlTable root = TOML.Parse(reader); |
115 | | - if (root == null) return false; |
116 | | - |
117 | | - if (!TryGetTable(root, "mcp_servers", out var servers) |
118 | | - && !TryGetTable(root, "mcpServers", out servers)) |
119 | | - { |
120 | | - return false; |
121 | | - } |
122 | | - |
123 | | - if (!TryGetTable(servers, "unityMCP", out var unity)) |
124 | | - { |
125 | | - return false; |
126 | | - } |
127 | | - |
128 | | - command = GetTomlString(unity, "command"); |
129 | | - args = GetTomlStringArray(unity, "args"); |
130 | | - |
131 | | - return !string.IsNullOrEmpty(command) && args != null; |
| 109 | + return TOML.Parse(reader); |
132 | 110 | } |
133 | 111 | catch (TomlParseException) |
134 | 112 | { |
135 | | - return false; |
| 113 | + return null; |
136 | 114 | } |
137 | 115 | catch (TomlSyntaxException) |
138 | 116 | { |
139 | | - return false; |
| 117 | + return null; |
140 | 118 | } |
141 | 119 | catch (FormatException) |
142 | 120 | { |
143 | | - return false; |
| 121 | + return null; |
144 | 122 | } |
145 | 123 | } |
146 | 124 |
|
| 125 | + /// <summary> |
| 126 | + /// Creates a TomlTable for the unityMCP server configuration |
| 127 | + /// </summary> |
| 128 | + private static TomlTable CreateUnityMcpTable(string uvPath, string serverSrc) |
| 129 | + { |
| 130 | + var unityMCP = new TomlTable(); |
| 131 | + unityMCP["command"] = new TomlString { Value = uvPath }; |
| 132 | + |
| 133 | + var argsArray = new TomlArray(); |
| 134 | + argsArray.Add(new TomlString { Value = "run" }); |
| 135 | + argsArray.Add(new TomlString { Value = "--directory" }); |
| 136 | + argsArray.Add(new TomlString { Value = serverSrc }); |
| 137 | + argsArray.Add(new TomlString { Value = "server.py" }); |
| 138 | + unityMCP["args"] = argsArray; |
| 139 | + |
| 140 | + return unityMCP; |
| 141 | + } |
| 142 | + |
147 | 143 | private static bool TryGetTable(TomlTable parent, string key, out TomlTable table) |
148 | 144 | { |
149 | 145 | table = null; |
@@ -211,33 +207,5 @@ private static string[] GetTomlStringArray(TomlTable table, string key) |
211 | 207 |
|
212 | 208 | return null; |
213 | 209 | } |
214 | | - |
215 | | - private static string FormatTomlStringArray(IEnumerable<string> values) |
216 | | - { |
217 | | - if (values == null) return "[]"; |
218 | | - StringBuilder sb = new StringBuilder(); |
219 | | - sb.Append('['); |
220 | | - bool first = true; |
221 | | - foreach (string value in values) |
222 | | - { |
223 | | - if (!first) |
224 | | - { |
225 | | - sb.Append(", "); |
226 | | - } |
227 | | - sb.Append('"').Append(EscapeTomlString(value ?? string.Empty)).Append('"'); |
228 | | - first = false; |
229 | | - } |
230 | | - sb.Append(']'); |
231 | | - return sb.ToString(); |
232 | | - } |
233 | | - |
234 | | - private static string EscapeTomlString(string value) |
235 | | - { |
236 | | - if (string.IsNullOrEmpty(value)) return string.Empty; |
237 | | - return value |
238 | | - .Replace("\\", "\\\\") |
239 | | - .Replace("\"", "\\\""); |
240 | | - } |
241 | | - |
242 | 210 | } |
243 | 211 | } |
0 commit comments