-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPXcodeServerInstallView.swift
More file actions
224 lines (196 loc) · 8 KB
/
MCPXcodeServerInstallView.swift
File metadata and controls
224 lines (196 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import GitHubCopilotService
import Logger
import SharedUIComponents
import SwiftUI
import SystemUtils
struct MCPXcodeServerInstallView: View {
@State private var xcodeVersion: String? = SystemUtils.xcodeVersion
@State private var isConfigured: Bool = false
@State private var isInstalling: Bool = false
@State private var installError: String? = nil
/// Server names from mcp.json whose config matches xcrun mcpbridge.
/// Cached to avoid repeated file I/O during SwiftUI rendering.
@State private var configuredXcodeServerNames: Set<String> = []
@ObservedObject private var mcpToolManager = CopilotMCPToolManagerObservable.shared
private let requiredXcodeVersion = "26.4"
private let serverName = "xcode"
private var meetsVersionRequirement: Bool {
guard let version = xcodeVersion else { return false }
return version.compare(requiredXcodeVersion, options: .numeric) != .orderedAscending
}
private var isConnected: Bool {
mcpToolManager.availableMCPServerTools.contains { server in
configuredXcodeServerNames.contains(server.name) &&
server.status == .running &&
!server.tools.isEmpty
}
}
/// Configured in mcp.json but not yet showing in available tools from the language server
private var isConfiguredButNotConnected: Bool {
isConfigured && !isConnected
}
private var isAlreadyInstalled: Bool {
isConfigured || isConnected
}
var body: some View {
HStack(alignment: .center, spacing: 16) {
VStack(alignment: .leading, spacing: 0) {
Text("Xcode MCP Server")
.font(.headline)
.padding(.vertical, 4)
subtitleView()
.font(.subheadline)
.foregroundColor(.secondary)
}
Spacer()
actionsView()
.padding(.vertical, 12)
}
.padding(EdgeInsets(top: 8, leading: 20, bottom: 8, trailing: 20))
.background(QuaternarySystemFillColor.opacity(0.75))
.settingsContainerStyle(isExpanded: false)
.onAppear {
checkInstallationStatus()
}
.onChange(of: mcpToolManager.availableMCPServerTools) { _ in
checkInstallationStatus()
}
}
// MARK: - Subviews
@ViewBuilder
private func subtitleView() -> some View {
if !meetsVersionRequirement {
let versionText = xcodeVersion ?? "unknown"
Text("Requires Xcode \(requiredXcodeVersion) or later. Current version: \(versionText).")
} else if isConnected {
Text("Xcode's built-in MCP server is connected, enabling richer editor integration.")
} else if isConfiguredButNotConnected {
Text("Please confirm in Xcode to allow the built-in MCP server.")
} else {
VStack(alignment: .leading, spacing: 4) {
Text("Connect Copilot to Xcode’s built‑in MCP server to enable richer editor integration.")
if let installError {
Text(installError)
.font(.caption)
.foregroundColor(.red)
}
}
}
}
@ViewBuilder
private func actionsView() -> some View {
if !meetsVersionRequirement {
EmptyView()
} else if isConnected {
Text("Connected").foregroundColor(.secondary)
} else if isConfiguredButNotConnected {
HStack(spacing: 6) {
ProgressView()
.controlSize(.small)
Text("Waiting for connection...")
.foregroundColor(.secondary)
}
} else {
Button {
installXcodeMCPServer()
} label: {
HStack(spacing: 4) {
if isInstalling {
ProgressView()
.controlSize(.small)
} else {
Image(systemName: "plus.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 12, height: 12, alignment: .center)
.padding(2)
}
Text("Install")
}
.conditionalFontWeight(.semibold)
}
.buttonStyle(.bordered)
.disabled(isInstalling)
}
}
// MARK: - Actions
private func checkInstallationStatus() {
let (configured, names) = readXcodeMCPServerNamesFromConfig()
isConfigured = configured
configuredXcodeServerNames = names
}
/// Returns (isConfigured, setOfMatchingServerNames) by reading mcp.json once.
private func readXcodeMCPServerNamesFromConfig() -> (Bool, Set<String>) {
let configFileURL = URL(fileURLWithPath: mcpConfigFilePath)
guard FileManager.default.fileExists(atPath: configFileURL.path),
let data = try? Data(contentsOf: configFileURL),
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let servers = json["servers"] as? [String: Any]
else {
return (false, [])
}
var names = Set<String>()
for (key, value) in servers {
guard let serverConfig = value as? [String: Any] else { continue }
let command = serverConfig["command"] as? String ?? ""
let args = serverConfig["args"] as? [String] ?? []
if command.contains("xcrun") && args.contains(where: { $0.contains("mcpbridge") }) {
names.insert(key)
}
}
return (!names.isEmpty, names)
}
private func installXcodeMCPServer() {
isInstalling = true
installError = nil
let configFileURL = URL(fileURLWithPath: mcpConfigFilePath)
let fileManager = FileManager.default
do {
if !fileManager.fileExists(atPath: configDirectory.path) {
try fileManager.createDirectory(
at: configDirectory,
withIntermediateDirectories: true
)
}
var config: [String: Any]
if fileManager.fileExists(atPath: configFileURL.path),
let data = try? Data(contentsOf: configFileURL),
let existing = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
{
config = existing
} else {
config = ["servers": [String: Any]()]
}
var servers = config["servers"] as? [String: Any] ?? [:]
// Skip write if the entry already points to xcrun mcpbridge
if let existing = servers[serverName] as? [String: Any],
let command = existing["command"] as? String,
let args = existing["args"] as? [String],
command.contains("xcrun") && args.contains(where: { $0.contains("mcpbridge") })
{
isConfigured = true
configuredXcodeServerNames.insert(serverName)
isInstalling = false
return
}
servers[serverName] = [
"type": "stdio",
"command": "xcrun",
"args": ["mcpbridge"]
]
config["servers"] = servers
let jsonData = try JSONSerialization.data(
withJSONObject: config,
options: [.prettyPrinted, .sortedKeys]
)
try jsonData.write(to: configFileURL, options: .atomic)
isConfigured = true
configuredXcodeServerNames.insert(serverName)
Logger.client.info("Successfully added Xcode MCP Server to configuration")
} catch {
installError = "Failed to update configuration: \(error.localizedDescription)"
Logger.client.error("Failed to install Xcode MCP Server: \(error)")
}
isInstalling = false
}
}