Start built-in web and reconnect enabled MCP profiles

This commit is contained in:
Mark Gregory
2026-11-16 17:43:02 +00:00
parent a2ccafa613
commit dd06e337fc
4 changed files with 111 additions and 25 deletions
+26 -8
View File
@@ -243,9 +243,10 @@ struct MCPSettings: View {
HSplitView {
Form {
Section("Built-In Tools") {
connectionRow(.web, title: BuiltInWebTools.name, subtitle: "Retrieve pages and search via SearXNG", icon: "globe") {
let search = webSearchEndpoint
studio.connectMCP(.web, name: BuiltInWebTools.name) { BuiltInWebTools(searchEndpoint: search) }
LabeledContent {
Text("Runs on demand").foregroundStyle(.secondary)
} label: {
Label(BuiltInWebTools.name, systemImage: "globe")
}
}
Section("Custom MCP Servers") {
@@ -261,11 +262,27 @@ struct MCPSettings: View {
}.buttonStyle(.bordered)
ForEach(profiles.keys.sorted(), id: \.self) { name in
if let profile = profiles[name] {
connectionRow(.profile(name), title: name,
subtitle: profile.command == nil ? "Streamable HTTP" : "Local stdio",
icon: profile.command == nil ? "network" : "terminal",
local: profile.command != nil) {
studio.connectMCP(.profile(name), name: name) { try profile.makeClient() }
let id = MCPConnectionID.profile(name)
let state = studio.mcpConnections[id]
VStack(alignment: .leading, spacing: 5) {
Toggle(name, isOn: Binding(
get: { studio.enabledMCPProfiles(path: configPath).contains(name) },
set: { enabled in
if enabled { selected = id }
studio.setMCPProfile(name, enabled: enabled, path: configPath)
}
))
if let state {
Text(state.progress ?? state.output)
.font(.caption)
.foregroundStyle(state.failed ? Color.red : Color.secondary)
.textSelection(.enabled)
if state.failed {
Button("Retry") { studio.restoreToolsets() }
} else if state.toolset != nil {
Button("Tools") { selected = id }
}
}
}
}
}
@@ -439,6 +456,7 @@ struct MCPSettings: View {
}
profiles = file.mcpServers
configPath = url.path
if invalidateProfiles { studio.restoreToolsets() }
configStatus = "\(profiles.count) custom server(s) loaded from \(url.lastPathComponent)."
} catch {
configStatus = "Could not read \(url.lastPathComponent): \(error.localizedDescription)"
+41 -4
View File
@@ -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…") {