Start built-in web and reconnect enabled MCP profiles
This commit is contained in:
@@ -34,6 +34,7 @@ final class StudioModel: ObservableObject {
|
||||
@Published private(set) var knowledgeStacks: [Conversation] = []
|
||||
@Published private(set) var snippets: [WorkspaceSnippet] = []
|
||||
@Published private(set) var mcpConnections: [MCPConnectionID: MCPConnectionState] = [:]
|
||||
private static let enabledProfilesKey = "enabledMCPProfiles"
|
||||
private var mcpOperations: [MCPConnectionID: (id: UUID, task: Task<Void, Never>)] = [:]
|
||||
|
||||
var activeToolsets: [(id: MCPConnectionID, toolset: ActiveToolset)] {
|
||||
@@ -61,6 +62,7 @@ final class StudioModel: ObservableObject {
|
||||
defer { launching = false }
|
||||
reloadRuntimeSettings()
|
||||
guard store == nil else { return }
|
||||
if databaseURL == nil { restoreToolsets() }
|
||||
if let history = UserDefaults.standard.data(forKey: GenerationMeasurement.key),
|
||||
let saved = try? JSONDecoder().decode([GenerationMeasurement].self, from: history) {
|
||||
measurements = Array(saved.prefix(50))
|
||||
@@ -238,10 +240,10 @@ final class StudioModel: ObservableObject {
|
||||
if wantsRepositories {
|
||||
webStatus = "Listing Gitea repositories…"
|
||||
do {
|
||||
let client: any MCPToolClient
|
||||
if let repositoryClient { client = repositoryClient }
|
||||
else { client = try await GiteaRepositoryLookup.configuredClient() }
|
||||
let repositories = try await GiteaRepositoryLookup.references(client: client)
|
||||
guard let repositoryClient else {
|
||||
throw StudioError("Enable the Gitea server in Toolbox → MCP Servers and check its connection.")
|
||||
}
|
||||
let repositories = try await GiteaRepositoryLookup.references(client: repositoryClient)
|
||||
try Task.checkCancellation()
|
||||
systemParts.append(repositories)
|
||||
webStatus = "Gitea repositories checked."
|
||||
@@ -578,6 +580,41 @@ final class StudioModel: ObservableObject {
|
||||
}
|
||||
|
||||
extension StudioModel {
|
||||
func restoreToolsets(defaults: UserDefaults = .standard) {
|
||||
let endpoint = defaults.string(forKey: "webSearchEndpoint") ?? ""
|
||||
connectMCP(.web, name: BuiltInWebTools.name) { BuiltInWebTools(searchEndpoint: endpoint) }
|
||||
guard let path = defaults.string(forKey: "mcpConfigPath"), !path.isEmpty else { return }
|
||||
let enabled = Set(enabledMCPProfiles(path: path, defaults: defaults))
|
||||
for name in enabled {
|
||||
connectMCP(.profile(name), name: name) {
|
||||
let file = try MCPServerConfiguration.File.read(URL(fileURLWithPath: path))
|
||||
guard let profile = file.mcpServers[name] else {
|
||||
throw StudioError("This enabled server is missing from the MCP configuration.")
|
||||
}
|
||||
return try profile.makeClient()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func enabledMCPProfiles(path: String, defaults: UserDefaults = .standard) -> [String] {
|
||||
let saved = defaults.dictionary(forKey: Self.enabledProfilesKey) as? [String: [String]]
|
||||
return saved?[path] ?? []
|
||||
}
|
||||
|
||||
func setMCPProfile(_ name: String, enabled: Bool, path: String, defaults: UserDefaults = .standard) {
|
||||
var saved = defaults.dictionary(forKey: Self.enabledProfilesKey) as? [String: [String]] ?? [:]
|
||||
var names = Set(saved[path] ?? [])
|
||||
if enabled {
|
||||
names.insert(name)
|
||||
} else {
|
||||
names.remove(name)
|
||||
disconnectMCP(.profile(name))
|
||||
}
|
||||
saved[path] = names.sorted()
|
||||
defaults.set(saved, forKey: Self.enabledProfilesKey)
|
||||
if enabled { restoreToolsets(defaults: defaults) }
|
||||
}
|
||||
|
||||
func connectMCP(_ id: MCPConnectionID, name: String, makeClient: @escaping @MainActor () throws -> any MCPToolClient) {
|
||||
guard mcpConnections[id]?.toolset == nil else { return }
|
||||
runMCP(id, progress: "Connecting…") {
|
||||
|
||||
Reference in New Issue
Block a user