-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPServerToolsSection.swift
More file actions
188 lines (164 loc) · 7.36 KB
/
MCPServerToolsSection.swift
File metadata and controls
188 lines (164 loc) · 7.36 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
import SwiftUI
import Persist
import GitHubCopilotService
/// Section for a single server's tools
struct MCPServerToolsSection: View {
let serverTools: MCPServerToolsCollection
@Binding var isServerEnabled: Bool
var forceExpand: Bool = false
@State private var toolEnabledStates: [String: Bool] = [:]
@State private var isExpanded: Bool = true
private var originalServerName: String { serverTools.name }
// Function to check if the MCP config contains unsupported server types
private func hasUnsupportedServerType() -> Bool {
let mcpConfig = UserDefaults.shared.value(for: \.gitHubCopilotMCPConfig)
// Check if config contains a URL field for this server
guard !mcpConfig.isEmpty else { return false }
do {
guard let jsonData = mcpConfig.data(using: .utf8),
let jsonObject = try JSONSerialization.jsonObject(with: jsonData) as? [String: Any],
let serverConfig = jsonObject[serverTools.name] as? [String: Any],
let url = serverConfig["url"] as? String else {
return false
}
return true
} catch {
return false
}
}
// Get the warning message for unsupported server types
private func getUnsupportedServerTypeMessage() -> String {
return "SSE/HTTP transport is not yet supported"
}
var body: some View {
VStack(spacing: 0) {
DisclosureGroup(isExpanded: $isExpanded) {
VStack(spacing: 0) {
Divider()
.padding(.leading, 32)
.padding(.top, 2)
.padding(.bottom, 4)
ForEach(serverTools.tools, id: \.name) { tool in
MCPToolRow(
tool: tool,
isServerEnabled: isServerEnabled,
isToolEnabled: toolBindingFor(tool),
onToolToggleChanged: { handleToolToggleChange(tool: tool, isEnabled: $0) }
)
}
}
} label: {
// Server name with checkbox
Toggle(isOn: Binding(
get: { isServerEnabled },
set: { updateAllToolsStatus(enabled: $0) }
)) {
HStack(spacing: 8) {
Text("MCP Server: \(serverTools.name)").fontWeight(.medium)
if serverTools.status == .error {
if hasUnsupportedServerType() {
Badge(text: getUnsupportedServerTypeMessage(), level: .danger, icon: "xmark.circle.fill")
} else {
let message = extractErrorMessage(serverTools.error?.description ?? "")
Badge(text: message, level: .danger, icon: "xmark.circle.fill")
}
}
}
}
.toggleStyle(.checkbox)
.padding(.leading, 4)
.disabled(serverTools.status == .error)
}
.onAppear {
initializeToolStates()
if forceExpand {
isExpanded = true
}
}
.onChange(of: forceExpand) { newForceExpand in
if newForceExpand {
isExpanded = true
}
}
if !isExpanded {
Divider()
.padding(.leading, 32)
.padding(.top, 2)
.padding(.bottom, 4)
}
}
}
private func extractErrorMessage(_ description: String) -> String {
guard let messageRange = description.range(of: "message:"),
let stackRange = description.range(of: "stack:") else {
return description
}
let start = description.index(messageRange.upperBound, offsetBy: 0)
let end = description.index(stackRange.lowerBound, offsetBy: 0)
return description[start..<end].trimmingCharacters(in: .whitespacesAndNewlines)
}
private func initializeToolStates() {
let savedToolsStatus = AppState.shared.getMCPToolsStatus()
let serverStatus = savedToolsStatus?.first(where: { $0.name == serverTools.name })
// Initialize tool states from saved status or defaults
toolEnabledStates = serverTools.tools.reduce(into: [:]) { result, tool in
let savedStatus = serverStatus?.tools.first(where: { $0.name == tool.name })
result[tool.name] = savedStatus?.status == .enabled ||
(savedStatus == nil && tool._status == .enabled)
}
// Check if all tools are disabled to properly set server state
if !toolEnabledStates.isEmpty && toolEnabledStates.values.allSatisfy({ !$0 }) {
DispatchQueue.main.async {
isServerEnabled = false
}
}
}
private func toolBindingFor(_ tool: MCPTool) -> Binding<Bool> {
Binding(
get: { toolEnabledStates[tool.name] ?? (tool._status == .enabled) },
set: { toolEnabledStates[tool.name] = $0 }
)
}
private func handleToolToggleChange(tool: MCPTool, isEnabled: Bool) {
toolEnabledStates[tool.name] = isEnabled
// Update server state based on tool states
updateServerState()
// Update only this specific tool status
updateToolStatus(tool: tool, isEnabled: isEnabled)
}
private func updateServerState() {
// If any tool is enabled, server should be enabled
// If all tools are disabled, server should be disabled
let allToolsDisabled = serverTools.tools.allSatisfy { tool in
!(toolEnabledStates[tool.name] ?? (tool._status == .enabled))
}
isServerEnabled = !allToolsDisabled
}
private func updateToolStatus(tool: MCPTool, isEnabled: Bool) {
let serverUpdate = UpdateMCPToolsStatusServerCollection(
name: serverTools.name,
tools: [UpdatedMCPToolsStatus(name: tool.name, status: isEnabled ? .enabled : .disabled)]
)
AppState.shared.updateMCPToolsStatus([serverUpdate])
CopilotMCPToolManager.updateMCPToolsStatus([serverUpdate])
}
private func updateAllToolsStatus(enabled: Bool) {
isServerEnabled = enabled
// Get all tools for this server from the original collection
let allServerTools = CopilotMCPToolManagerObservable.shared.availableMCPServerTools
.first(where: { $0.name == originalServerName })?.tools ?? serverTools.tools
// Update all tool states - includes both visible and filtered-out tools
for tool in allServerTools {
toolEnabledStates[tool.name] = enabled
}
// Create status update for all tools
let serverUpdate = UpdateMCPToolsStatusServerCollection(
name: serverTools.name,
tools: allServerTools.map {
UpdatedMCPToolsStatus(name: $0.name, status: enabled ? .enabled : .disabled)
}
)
AppState.shared.updateMCPToolsStatus([serverUpdate])
CopilotMCPToolManager.updateMCPToolsStatus([serverUpdate])
}
}