-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPServerDetailSheet.swift
More file actions
577 lines (516 loc) · 21.3 KB
/
MCPServerDetailSheet.swift
File metadata and controls
577 lines (516 loc) · 21.3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import SwiftUI
import AppKit
import GitHubCopilotService
import SharedUIComponents
import Foundation
@available(macOS 13.0, *)
struct MCPServerDetailSheet: View {
let server: MCPRegistryServerDetail
let meta: ServerMeta
@State private var selectedTab = TabType.Packages
@State private var expandedPackages: Set<Int> = []
@State private var expandedRemotes: Set<Int> = []
@State private var packageConfigs: [Int: [String: Any]] = [:]
@State private var remoteConfigs: [Int: [String: Any]] = [:]
// Track installation progress per item so we can disable buttons / show feedback
@State private var installingPackages: Set<Int> = []
@State private var installingRemotes: Set<Int> = []
// Track whether the server (any option) is already installed
@State private var isInstalled: Bool
// Overwrite confirmation alert
@State private var showOverwriteAlert: Bool = false
@State private var pendingInstallAction: (() -> Void)? = nil
@Environment(\.dismiss) private var dismiss
enum TabType: String, CaseIterable, Identifiable {
case Packages, Remotes, Metadata
var id: Self { self }
}
init(response: MCPRegistryServerResponse) {
self.server = response.server
self.meta = response.meta
// Determine installed status using registry service (same logic as gallery view)
_isInstalled = State(initialValue: MCPRegistryService.shared.isServerInstalled(server))
}
// Shared visual constants
private let labelColumnWidth: CGFloat = 80
private let detailTopPadding: CGFloat = 6
var body: some View {
VStack(spacing: 0) {
// Header
headerSection
// Tab selector
tabSelector
// Content
OverlayScrollView {
VStack(alignment: .leading, spacing: 16) {
switch selectedTab {
case .Packages:
packagesTab
case .Remotes:
remotesTab
case .Metadata:
metadataTab
}
}
.padding(28)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxHeight: 400)
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: { dismiss() }) { Text("Close") }
}
ToolbarItem(placement: .secondaryAction) {
if isInstalled {
Button("Open Config") { openConfig() }
.help("Open mcp.json")
}
}
}
.toolbarRole(.automatic)
.frame(width: 600, height: 450)
.background(Color(nsColor: .controlBackgroundColor))
.onAppear {
isInstalled = MCPRegistryService.shared.isServerInstalled(server)
}
.alert("Overwrite Existing Installation?", isPresented: $showOverwriteAlert) {
Button("Cancel", role: .cancel) { pendingInstallAction = nil }
Button("Overwrite", role: .destructive) {
pendingInstallAction?()
pendingInstallAction = nil
}
} message: {
Text("Installing this option will replace the currently installed variant of this server.")
}
}
// MARK: - Header Section
private var headerSection: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .center) {
Text(server.title ?? server.name)
.font(.system(size: 18, weight: .semibold))
if meta.official.status == .deprecated {
statusBadge(meta.official.status)
}
Spacer()
}
HStack(spacing: 24) {
HStack(spacing: 6) {
Image(systemName: "tag")
Text(server.version)
}
.font(.system(size: 12, design: .monospaced))
.foregroundColor(.secondary)
dateMetadataTag(title: "Published ", dateString: meta.official.publishedAt, image: "clock.arrow.trianglehead.counterclockwise.rotate.90")
dateMetadataTag(title: "Updated ", dateString: meta.official.updatedAt, image: "icloud.and.arrow.up")
if let repo = server.repository, !repo.url.isEmpty, !repo.source.isEmpty {
if let repoURL = URL(string: repo.url) {
HStack(spacing: 6) {
Image(systemName: "link")
Link(destination: repoURL) {
Text("Repository")
}
.onHover { hovering in
if hovering {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}
.font(.system(size: 12))
.foregroundColor(.secondary)
}
}
}
Text(server.description)
.font(.system(size: 13))
.foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
.lineSpacing(2)
.padding(.top, 4)
}
.padding(28)
.background(Color(nsColor: .windowBackgroundColor))
}
private func dateMetadataTag(title: String, dateString: String, image: String) -> some View {
HStack(spacing: 6) {
Image(systemName: image)
if let date = parseDate(dateString) {
(Text("\(title)\(relativeDateString(date))"))
.help(formatExactDate(date))
} else {
Text("\(title) \(dateString)").help(dateString)
}
}
.font(.system(size: 12))
.foregroundColor(.secondary)
}
// MARK: - Tab Selector
private var tabSelector: some View {
HStack(spacing: 0) {
Picker("", selection: $selectedTab) {
Text("Packages (\(server.packages?.count ?? 0))")
.tag(TabType.Packages)
Text("Remotes (\(server.remotes?.count ?? 0))")
.tag(TabType.Remotes)
Text("Metadata")
.tag(TabType.Metadata)
}
.pickerStyle(.segmented)
}
.padding(.horizontal, 20)
.padding(.vertical, 12)
.background(Color(nsColor: .controlBackgroundColor).opacity(0.3))
.overlay(
Rectangle()
.fill(Color(nsColor: .separatorColor))
.frame(height: 1),
alignment: .bottom
)
}
// MARK: - Packages Tab
private var packagesTab: some View {
Group {
if let packages = server.packages, !packages.isEmpty {
ForEach(Array(packages.enumerated()), id: \.offset) { index, package in
packageItem(package, index: index)
}
} else {
EmptyStateView(message: "No packages available for this server", type: .Packages)
}
}
}
private func packageItem(_ package: Package, index: Int) -> some View {
let isExpanded = expandedPackages.contains(index)
let optionInstalled = MCPRegistryService.shared.isPackageOptionInstalled(serverDetail: server, package: package)
let metadata: [ServerInstallationOptionView.Metadata] = {
var rows: [ServerInstallationOptionView.Metadata] = []
rows.append(.init(label: "ID", value: package.identifier, monospaced: true))
if let registryURL = package.registryBaseUrl {
rows.append(.init(label: "Registry", value: registryURL))
}
if let runtime = package.runtimeHint { rows.append(.init(label: "Runtime", value: runtime)) }
return rows
}()
return ServerInstallationOptionView(
title: package.registryType.registryDisplayText,
iconSystemName: "shippingbox",
versionTag: package.version,
metadata: metadata,
isExpanded: isExpanded,
isInstalled: isInstalled, // overall server installed
isInstalling: installingPackages.contains(index),
showUninstall: optionInstalled,
labelColumnWidth: labelColumnWidth,
onToggleExpand: {
if isExpanded {
expandedPackages.remove(index)
} else {
expandedPackages.insert(index)
if packageConfigs[index] == nil { packageConfigs[index] = generateServerConfig(for: package) }
}
},
onInstall: { handlePackageInstallButton(package, index: index, optionInstalled: optionInstalled) },
onUninstall: { uninstallServer() },
config: packageConfigs[index]
)
}
// MARK: - Remotes Tab
private var remotesTab: some View {
Group {
if let remotes = server.remotes, !remotes.isEmpty {
ForEach(Array(remotes.enumerated()), id: \.offset) { index, remote in
remoteItem(remote, index: index)
}
} else {
EmptyStateView(
message: "No remote endpoints configured for this server",
type: .Remotes
)
}
}
}
private func remoteItem(_ remote: Remote, index: Int) -> some View {
let isExpanded = expandedRemotes.contains(index)
let optionInstalled = MCPRegistryService.shared.isRemoteOptionInstalled(serverDetail: server, remote: remote)
let metadata: [ServerInstallationOptionView.Metadata] = [
.init(label: "URL", value: remote.url, monospaced: true)
]
return ServerInstallationOptionView(
title: remote.transportType.displayText,
iconSystemName: "globe",
versionTag: nil,
metadata: metadata,
isExpanded: isExpanded,
isInstalled: isInstalled,
isInstalling: installingRemotes.contains(index),
showUninstall: optionInstalled,
labelColumnWidth: labelColumnWidth,
onToggleExpand: {
if isExpanded {
expandedRemotes.remove(index)
} else {
expandedRemotes.insert(index)
if remoteConfigs[index] == nil { remoteConfigs[index] = generateServerConfig(for: remote) }
}
},
onInstall: { handleRemoteInstallButton(remote, index: index, optionInstalled: optionInstalled) },
onUninstall: { uninstallServer() },
config: remoteConfigs[index]
)
}
// MARK: - Metadata Tab
private var metadataTab: some View {
VStack(alignment: .leading, spacing: 16) {
officialMetadataSection(meta.official)
}
}
private func officialMetadataSection(_ official: OfficialMeta) -> some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Text("Official Registry")
.font(.system(size: 14, weight: .medium))
}
VStack(alignment: .leading, spacing: 8) {
metadataRow(
label: "Published",
value: parseDate(official.publishedAt) != nil ? formatExactDate(
parseDate(official.publishedAt)!
) : official.publishedAt
)
metadataRow(
label: "Updated",
value: parseDate(official.updatedAt) != nil ? formatExactDate(
parseDate(official.updatedAt)!
) : official.updatedAt
)
}
}
.padding(16)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color(nsColor: .controlBackgroundColor))
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
)
)
}
private func metadataRow(label: String, value: String, isLink: Bool = false) -> some View {
HStack(spacing: 8) {
Text(label)
.font(.system(size: 12, weight: .medium))
.foregroundColor(.secondary)
.frame(width: 80, alignment: .leading)
if isLink, let url = URL(string: value) {
Link(value, destination: url)
.font(.system(size: 12, design: .monospaced))
.foregroundColor(.blue)
} else {
Text(value)
.font(.system(size: 12, design: label.contains("ID") || label.contains("Commit") ? .monospaced : .default))
.foregroundColor(.primary)
.textSelection(.enabled)
}
}
}
private func serverConfigView(_ config: [String: Any]) -> some View {
ZStack(alignment: .topTrailing) {
VStack(alignment: .leading, spacing: 8) {
Text(formatConfigAsJSON(config))
.font(.system(.callout, design: .monospaced))
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.fixedSize(horizontal: false, vertical: true)
.padding(.bottom, 2)
}
.padding(12)
CopyButton {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(formatConfigAsJSON(config), forType: .string)
}
.padding(6)
.help("Copy configuration to clipboard")
}
.background(
RoundedRectangle(cornerRadius: 6)
.fill(Color(nsColor: .textBackgroundColor).opacity(0.5))
)
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
)
}
private func formatConfigAsJSON(_ config: [String: Any]) -> String {
do {
let jsonData = try JSONSerialization.data(withJSONObject: config, options: [.prettyPrinted, .sortedKeys])
return String(data: jsonData, encoding: .utf8) ?? "{}"
} catch {
return "{}"
}
}
// MARK: - Configuration Generation Helpers
private func generateServerConfig(for package: Package) -> [String: Any] {
return MCPRegistryService.shared.createServerConfig(for: server, package: package)
}
private func generateServerConfig(for remote: Remote) -> [String: Any] {
return MCPRegistryService.shared.createServerConfig(for: server, remote: remote)
}
// MARK: - Install Helpers
private func performPackageInstall(_ package: Package, index: Int) {
guard !installingPackages.contains(index) else { return }
installingPackages.insert(index)
Task {
let config = packageConfigs[index] ?? generateServerConfig(for: package)
// Cache generated config for preview if needed later
if packageConfigs[index] == nil { packageConfigs[index] = config }
let option = InstallationOption(
displayName: package.registryType.registryDisplayText,
description: "Install \(package.identifier)",
config: config
)
do {
try await MCPRegistryService.shared.installMCPServer(server, installationOption: option)
// Mark installed locally so UI reflects the state immediately
isInstalled = true
} catch {
// Silently fail for now; could surface error UI later
}
installingPackages.remove(index)
}
}
private func handlePackageInstallButton(_ package: Package, index: Int, optionInstalled: Bool) {
if isInstalled && !optionInstalled {
// Show overwrite confirmation
pendingInstallAction = { performPackageInstall(package, index: index) }
showOverwriteAlert = true
} else {
performPackageInstall(package, index: index)
}
}
private func performRemoteInstall(_ remote: Remote, index: Int) {
guard !installingRemotes.contains(index) else { return }
installingRemotes.insert(index)
Task {
let config = remoteConfigs[index] ?? generateServerConfig(for: remote)
if remoteConfigs[index] == nil { remoteConfigs[index] = config }
let option = InstallationOption(
displayName: "\(remote.transportType.rawValue)",
description: "Install remote endpoint \(remote.url)",
config: config
)
do {
try await MCPRegistryService.shared.installMCPServer(server, installationOption: option)
isInstalled = true
} catch {
// Silently fail for now
}
installingRemotes.remove(index)
}
}
private func handleRemoteInstallButton(_ remote: Remote, index: Int, optionInstalled: Bool) {
if isInstalled && !optionInstalled {
pendingInstallAction = { performRemoteInstall(remote, index: index) }
showOverwriteAlert = true
} else {
performRemoteInstall(remote, index: index)
}
}
private func uninstallServer() {
Task {
do {
try await MCPRegistryService.shared.uninstallMCPServer(server)
isInstalled = false
} catch {
// TODO: Consider surfacing error to user
}
}
}
// MARK: - Helper Views
private func statusBadge(_ status: ServerStatus) -> some View {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 14, weight: .medium))
.foregroundColor(Color.orange)
.padding(.horizontal, 6)
.help("The server is deprecated.")
}
private struct EmptyStateView: View {
let message: String
let type: PackageType
enum PackageType: String {
case Packages, Remotes, Metadata
}
var Logo: some View {
switch type {
case .Packages:
return Image(systemName: "shippingbox")
case .Remotes:
return Image(systemName: "globe")
case .Metadata:
return Image(systemName: "info.circle")
}
}
var body: some View {
VStack(spacing: 12) {
Logo.font(.system(size: 32))
Text(message)
.font(.system(size: 13))
}
.frame(maxWidth: .infinity)
.padding(.vertical, 40)
}
}
// MARK: - Utilities
private func parseDate(_ dateString: String) -> Date? {
// Try multiple ISO8601 formatters in order of specificity
let formatters: [ISO8601DateFormatter] = [
{
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter
}(),
{
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime]
return formatter
}(),
{
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withYear, .withMonth, .withDay, .withTime, .withTimeZone]
return formatter
}(),
{
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withYear, .withMonth, .withDay, .withTime]
return formatter
}()
]
// Try each formatter until one succeeds
for formatter in formatters {
if let date = formatter.date(from: dateString) {
return date
}
}
return nil
}
private func formatExactDate(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.dateStyle = .full
formatter.timeStyle = .medium
return formatter.string(from: date)
}
private func relativeDateString(_ date: Date) -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: date, relativeTo: Date())
}
// MARK: - Open Config / Selection Support
private func openConfig() {
// Simplified to just open the MCP config file, mirroring manual install behavior.
let url = URL(fileURLWithPath: mcpConfigFilePath)
NSWorkspace.shared.open(url)
}
}