-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathCopilotMCPToolManagerObservable.swift
More file actions
58 lines (50 loc) · 2 KB
/
CopilotMCPToolManagerObservable.swift
File metadata and controls
58 lines (50 loc) · 2 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
import SwiftUI
import Combine
import Persist
import GitHubCopilotService
import Client
import Logger
class CopilotMCPToolManagerObservable: ObservableObject {
static let shared = CopilotMCPToolManagerObservable()
@Published var availableMCPServerTools: [MCPServerToolsCollection] = []
private var cancellables = Set<AnyCancellable>()
private init() {
DistributedNotificationCenter.default()
.publisher(for: .gitHubCopilotMCPToolsDidChange)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
Logger.client.info("MCP tools change notification received")
Task {
await self.refreshMCPServerTools()
}
}
.store(in: &cancellables)
Task {
// Initial load of MCP server tools collections from ExtensionService process
await refreshMCPServerTools()
}
}
@MainActor
private func refreshMCPServerTools() async {
Logger.client.info("Refreshing MCP server tools...")
do {
let service = try getService()
let mcpTools = try await service.getAvailableMCPServerToolsCollections()
refreshTools(tools: mcpTools)
} catch {
Logger.client.error("Failed to fetch MCP server tools: \(error)")
}
}
private func refreshTools(tools: [MCPServerToolsCollection]?) {
guard let tools = tools else {
// nil means the tools data is ready, and skip it first.
Logger.client.info("MCP tools data not ready yet, skipping refresh")
return
}
let totalToolsCount = tools.reduce(0) { $0 + $1.tools.count }
let serverNames = tools.map { $0.name }.joined(separator: ", ")
Logger.client.info("Refreshed MCP tools - Servers: \(tools.count), Total tools: \(totalToolsCount), Server names: [\(serverNames)]")
self.availableMCPServerTools = tools
}
}