-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPAppState.swift
More file actions
116 lines (96 loc) · 4.41 KB
/
MCPAppState.swift
File metadata and controls
116 lines (96 loc) · 4.41 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
import Persist
import GitHubCopilotService
import Foundation
public let MCP_TOOLS_STATUS = "mcpToolsStatus"
extension AppState {
public func getMCPToolsStatus() -> [UpdateMCPToolsStatusServerCollection]? {
guard let savedJSON = get(key: MCP_TOOLS_STATUS),
let data = try? JSONEncoder().encode(savedJSON),
let savedStatus = try? JSONDecoder().decode([UpdateMCPToolsStatusServerCollection].self, from: data) else {
return nil
}
return savedStatus
}
public func updateMCPToolsStatus(_ servers: [UpdateMCPToolsStatusServerCollection]) {
var existingServers = getMCPToolsStatus() ?? []
// Update or add servers
for newServer in servers {
if let existingIndex = existingServers.firstIndex(where: { $0.name == newServer.name }) {
// Update existing server
let updatedTools = mergeTools(original: existingServers[existingIndex].tools, new: newServer.tools)
existingServers[existingIndex].tools = updatedTools
} else {
// Add new server
existingServers.append(newServer)
}
}
update(key: MCP_TOOLS_STATUS, value: existingServers)
}
private func mergeTools(original: [UpdatedMCPToolsStatus], new: [UpdatedMCPToolsStatus]) -> [UpdatedMCPToolsStatus] {
var result = original
for newTool in new {
if let index = result.firstIndex(where: { $0.name == newTool.name }) {
result[index].status = newTool.status
} else {
result.append(newTool)
}
}
return result
}
public func createMCPToolsStatus(_ serverCollections: [MCPServerToolsCollection]) {
var existingServers = getMCPToolsStatus() ?? []
var serversChanged = false
for serverCollection in serverCollections {
// Find or create a server entry
let serverIndex = existingServers.firstIndex(where: { $0.name == serverCollection.name })
var toolsToUpdate: [UpdatedMCPToolsStatus]
if let index = serverIndex {
toolsToUpdate = existingServers[index].tools
} else {
toolsToUpdate = []
serversChanged = true
}
// Add new tools with default enabled status
let existingToolNames = Set(toolsToUpdate.map { $0.name })
let newTools = serverCollection.tools
.filter { !existingToolNames.contains($0.name) }
.map { UpdatedMCPToolsStatus(name: $0.name, status: .enabled) }
if !newTools.isEmpty {
serversChanged = true
toolsToUpdate.append(contentsOf: newTools)
}
// Update or add the server
if let index = serverIndex {
existingServers[index].tools = toolsToUpdate
} else {
existingServers.append(UpdateMCPToolsStatusServerCollection(
name: serverCollection.name,
tools: toolsToUpdate
))
}
}
// Only update storage if changes were made
if serversChanged {
update(key: MCP_TOOLS_STATUS, value: existingServers)
}
}
public func cleanupMCPToolsStatus(availableTools: [MCPServerToolsCollection]) {
guard var existingServers = getMCPToolsStatus() else { return }
// Get all available server names and their respective tool names
let availableServerMap = Dictionary(
availableTools.map { collection in
(collection.name, Set(collection.tools.map { $0.name }))
}
) { first, _ in first }
// Remove servers that don't exist in available tools
existingServers.removeAll { !availableServerMap.keys.contains($0.name) }
// For each remaining server, remove tools that don't exist in available tools
for i in 0..<existingServers.count {
if let availableToolNames = availableServerMap[existingServers[i].name] {
existingServers[i].tools.removeAll { !availableToolNames.contains($0.name) }
}
}
// Update the stored state
update(key: MCP_TOOLS_STATUS, value: existingServers)
}
}