-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPServerGalleryView.swift
More file actions
360 lines (321 loc) · 12.6 KB
/
MCPServerGalleryView.swift
File metadata and controls
360 lines (321 loc) · 12.6 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
import AppKit
import Client
import CryptoKit
import GitHubCopilotService
import Logger
import SharedUIComponents
import SwiftUI
import XPCShared
enum MCPServerGalleryWindow {
static let identifier = "MCPServerGalleryWindow"
private static weak var currentViewModel: MCPServerGalleryViewModel?
@MainActor static func open(
serverList: MCPRegistryServerList,
mcpRegistryEntry: MCPRegistryEntry? = nil
) {
if let existing = NSApp.windows.first(where: { $0.identifier?.rawValue == identifier }) {
// Update existing window with new data
update(serverList: serverList, mcpRegistryEntry: mcpRegistryEntry)
existing.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
return
}
let viewModel = MCPServerGalleryViewModel(
initialList: serverList,
mcpRegistryEntry: mcpRegistryEntry
)
currentViewModel = viewModel
let controller = NSHostingController(
rootView: MCPServerGalleryView(
viewModel: viewModel
)
)
let window = NSWindow(contentViewController: controller)
window.title = "MCP Servers Marketplace"
window.identifier = NSUserInterfaceItemIdentifier(identifier)
window.setContentSize(NSSize(width: 800, height: 600))
window.minSize = NSSize(width: 600, height: 400)
window.styleMask.insert([.titled, .closable, .resizable, .miniaturizable])
window.isReleasedWhenClosed = false
window.center()
window.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
@MainActor static func update(
serverList: MCPRegistryServerList,
mcpRegistryEntry: MCPRegistryEntry? = nil
) {
currentViewModel?.updateData(serverList: serverList, mcpRegistryEntry: mcpRegistryEntry)
}
@MainActor static func refreshFromURL(mcpRegistryEntry: MCPRegistryEntry? = nil) async -> Error? {
return await currentViewModel?.refreshFromURL(mcpRegistryEntry: mcpRegistryEntry)
}
static func isOpen() -> Bool {
return NSApp.windows.first(where: { $0.identifier?.rawValue == identifier }) != nil
}
}
// MARK: - Stable ID helper
extension MCPRegistryServerResponse {
var stableID: String {
server.name + server.version
}
}
private struct IdentifiableServerResponse: Identifiable {
let response: MCPRegistryServerResponse
var id: String { response.stableID }
}
struct MCPServerGalleryView: View {
@ObservedObject var viewModel: MCPServerGalleryViewModel
@State private var isShowingURLSheet = false
@State private var searchTask: Task<Void, Never>?
init(viewModel: MCPServerGalleryViewModel) {
self.viewModel = viewModel
}
// MARK: - Body
var body: some View {
VStack(spacing: 0) {
if let error = viewModel.lastError {
if let serviceError = error as? XPCExtensionServiceError {
Badge(text: serviceError.underlyingError?.localizedDescription ?? serviceError.localizedDescription, level: .danger, icon: "xmark.circle.fill")
} else {
Badge(text: error.localizedDescription, level: .danger, icon: "xmark.circle.fill")
}
}
tableHeaderView
serverListView
}
.padding(20)
.background(Color(nsColor: .controlBackgroundColor))
.background(.ultraThinMaterial)
.onAppear {
viewModel.loadInstalledServers()
}
.sheet(isPresented: $isShowingURLSheet) {
urlSheet
}
.sheet(isPresented: Binding(
get: { viewModel.infoSheetServer != nil },
set: { isPresented in
if !isPresented {
viewModel.dismissInfo()
}
}
)) {
if let server = viewModel.infoSheetServer {
infoSheet(server)
}
}
.searchable(text: $viewModel.searchText, prompt: "Search")
.onChange(of: viewModel.searchText) { newValue in
// Debounce search input before triggering a new server-side query
searchTask?.cancel()
searchTask = Task { @MainActor in
try? await Task.sleep(nanoseconds: 300_000_000) // 0.3s
if !Task.isCancelled {
viewModel.refreshForSearch()
}
}
}
.toolbar {
ToolbarItem {
Button(action: { viewModel.refresh() }) {
Image(systemName: "arrow.clockwise")
}
.help("Refresh")
}
ToolbarItem {
Button(action: { isShowingURLSheet = true }) {
Image(systemName: "square.and.pencil")
}
.help("Configure your MCP Registry Base URL")
}
}
}
private var tableHeaderView: some View {
VStack(spacing: 0) {
HStack {
Text("Name")
.font(.system(size: 11, weight: .bold))
.padding(.horizontal, 8)
.frame(width: 220, alignment: .leading)
Divider().frame(height: 20)
Text("Description")
.font(.system(size: 11, weight: .medium))
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
HStack {
Text("Actions")
.font(.system(size: 11, weight: .medium))
.foregroundColor(.secondary)
}
.padding(.trailing, 8)
.frame(width: 120, alignment: .leading)
}
.padding(.horizontal, 12)
.padding(.vertical, 6)
.background(Color.clear)
Divider()
}
}
private var serverListView: some View {
ZStack {
ScrollView {
LazyVStack(spacing: 0) {
serverRows
if viewModel.shouldShowLoadMoreSentinel {
Color.clear
.frame(height: 1)
.onAppear { viewModel.loadMoreIfNeeded() }
.accessibilityHidden(true)
}
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
.padding(.vertical, 12)
Spacer()
}
}
}
}
if viewModel.isRefreshing {
VStack(spacing: 12) {
ProgressView()
Text("Loading servers...")
.font(.system(size: 13))
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(nsColor: .controlBackgroundColor).opacity(0.95))
}
}
}
private var serverRows: some View {
ForEach(Array(viewModel.filteredServers.enumerated()), id: \.element.stableID) { index, server in
let isInstalled = viewModel.isServerInstalled(serverId: server.stableID)
row(for: server, index: index, isInstalled: isInstalled)
.background(rowBackground(for: index))
.cornerRadius(8)
.onAppear {
handleRowAppear(index: index)
}
}
}
private var urlSheet: some View {
MCPRegistryURLSheet(
mcpRegistryEntry: viewModel.mcpRegistryEntry,
onURLUpdated: {
viewModel.refresh()
}
)
.frame(width: 500, height: 200)
}
private func rowBackground(for index: Int) -> Color {
index.isMultiple(of: 2) ? Color.clear : Color.primary.opacity(0.03)
}
private func handleRowAppear(index: Int) {
let currentFilteredCount = viewModel.filteredServers.count
let totalServerCount = viewModel.servers.count
// Prefetch when approaching the end of filtered results
if index >= currentFilteredCount - 5 {
// If we're filtering and the filtered results are small compared to total servers,
// or if we're near the end of all available data, try to load more
if currentFilteredCount < 20 || index >= totalServerCount - 5 {
viewModel.loadMoreIfNeeded()
}
}
}
// MARK: - Subviews
private func row(for response: MCPRegistryServerResponse, index: Int, isInstalled: Bool) -> some View {
HStack {
Text(response.server.title ?? response.server.name)
.fontWeight(.medium)
.lineLimit(1)
.truncationMode(.middle)
.padding(.horizontal, 8)
.frame(width: 220, alignment: .leading)
Divider().frame(height: 20).foregroundColor(Color.clear)
Text(response.server.description)
.fontWeight(.medium)
.foregroundColor(.secondary)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
HStack(spacing: 8) {
if isInstalled {
Button("Uninstall") {
Task {
await viewModel.uninstallServer(response.server)
}
}
.buttonStyle(DestructiveButtonStyle())
.help("Uninstall")
} else {
if #available(macOS 13.0, *) {
SplitButton(
title: "Install",
isDisabled: viewModel.hasNoDeployments(response.server),
primaryAction: {
// Install with default configuration
Task {
await viewModel.installServer(response.server)
}
},
menuItems: {
let options = viewModel.getInstallationOptions(for: response.server)
guard !options.isEmpty else { return [] }
return [SplitButtonMenuItem.header("Install Server With")] + options.map { option in
SplitButtonMenuItem(title: option.displayName) {
Task {
await viewModel.installServer(response.server, configuration: option.displayName)
}
}
}
}()
)
.help("Install")
} else {
Button("Install") {
Task {
await viewModel.installServer(response.server)
}
}
.disabled(viewModel.hasNoDeployments(response.server))
.help("Install")
}
}
Button {
viewModel.showInfo(response)
} label: {
Image(systemName: "info.circle")
.font(.system(size: 13))
.foregroundColor(.primary)
.multilineTextAlignment(.trailing)
}
.buttonStyle(.plain)
.help("View Details")
}
.padding(.horizontal, 8)
.frame(width: 120, alignment: .leading)
}
.padding(.horizontal, 12)
.padding(.vertical, 10)
}
private func infoSheet(_ response: MCPRegistryServerResponse) -> some View {
if #available(macOS 13.0, *) {
return AnyView(MCPServerDetailSheet(response: response))
} else {
return AnyView(EmptyView())
}
}
}
func defaultInstallation(for server: MCPRegistryServerDetail) -> String {
// Get the first available type from remotes or packages
if let firstRemote = server.remotes?.first {
return firstRemote.transportType.rawValue
}
if let firstPackage = server.packages?.first {
return firstPackage.registryType
}
return ""
}