Keep multiple MCP toolsets connected and restore workspace navigation
This commit is contained in:
@@ -24,13 +24,21 @@ final class StudioModel: ObservableObject {
|
||||
@Published var useExternalKnowledge = false
|
||||
|
||||
var connectedKnowledge: (any MCPToolClient)? {
|
||||
guard let activeToolset, activeToolset.tools.contains(where: { $0.name == "search_knowledge" }) else { return nil }
|
||||
return activeToolset.client
|
||||
activeToolsets.first(where: { entry in
|
||||
entry.toolset.tools.contains { $0.name == "search_knowledge" }
|
||||
})?.toolset.client
|
||||
}
|
||||
|
||||
@Published private(set) var measurements: [GenerationMeasurement] = []
|
||||
@Published private(set) var knowledgeStacks: [Conversation] = []
|
||||
@Published private(set) var snippets: [WorkspaceSnippet] = []
|
||||
@Published var activeToolset: ActiveToolset?
|
||||
@Published private(set) var mcpConnections: [MCPConnectionID: MCPConnectionState] = [:]
|
||||
private var mcpOperations: [MCPConnectionID: (id: UUID, task: Task<Void, Never>)] = [:]
|
||||
|
||||
var activeToolsets: [(id: MCPConnectionID, toolset: ActiveToolset)] {
|
||||
mcpConnections.compactMap { id, state in state.toolset.map { (id, $0) } }
|
||||
.sorted { $0.1.name.localizedStandardCompare($1.1.name) == .orderedAscending }
|
||||
}
|
||||
@Published var activeStack: UUID?
|
||||
@Published var activeSkill: UUID?
|
||||
@Published var activeContext: UUID?
|
||||
@@ -171,7 +179,7 @@ final class StudioModel: ObservableObject {
|
||||
\(toolAttachment.output)
|
||||
""")
|
||||
}
|
||||
let lookup = activeToolset?.client is BuiltInWebTools ? ChatWebLookup(message: text) : nil
|
||||
let lookup = mcpConnections[.web]?.toolset != nil ? ChatWebLookup(message: text) : nil
|
||||
let searchEndpoint = UserDefaults.standard.string(forKey: "webSearchEndpoint") ?? ""
|
||||
try store.save(user, in: chat)
|
||||
messages.append(user)
|
||||
@@ -535,3 +543,60 @@ final class StudioModel: ObservableObject {
|
||||
} catch { status = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
|
||||
extension StudioModel {
|
||||
func connectMCP(_ id: MCPConnectionID, name: String, makeClient: @escaping @MainActor () throws -> any MCPToolClient) {
|
||||
guard mcpConnections[id]?.toolset == nil else { return }
|
||||
runMCP(id, progress: "Connecting…") {
|
||||
let client = try makeClient()
|
||||
let tools = try await client.listTools()
|
||||
return MCPConnectionState(toolset: ActiveToolset(name: name, client: client, tools: tools),
|
||||
output: tools.isEmpty ? "The server exposed no tools." : "\(tools.count) tools available.")
|
||||
}
|
||||
}
|
||||
|
||||
func invokeMCP(_ id: MCPConnectionID, tool: String, arguments: Data, searchEndpoint: String) {
|
||||
guard let connected = mcpConnections[id]?.toolset, connected.tools.contains(where: { $0.name == tool }) else { return }
|
||||
runMCP(id, progress: "Running \(tool)…") {
|
||||
let client: any MCPToolClient = id == .web ? BuiltInWebTools(searchEndpoint: searchEndpoint) : connected.client
|
||||
let response = try await client.callTool(tool, argumentsJSON: arguments)
|
||||
return MCPConnectionState(toolset: connected, output: String(decoding: response, as: UTF8.self),
|
||||
completedTool: tool)
|
||||
}
|
||||
}
|
||||
|
||||
func disconnectMCP(_ id: MCPConnectionID) {
|
||||
mcpOperations.removeValue(forKey: id)?.task.cancel()
|
||||
mcpConnections[id] = nil
|
||||
if connectedKnowledge == nil { useExternalKnowledge = false }
|
||||
}
|
||||
|
||||
func cancelMCP(_ id: MCPConnectionID) {
|
||||
mcpOperations[id]?.task.cancel()
|
||||
}
|
||||
|
||||
private func runMCP(
|
||||
_ id: MCPConnectionID, progress: String,
|
||||
work: @escaping @MainActor () async throws -> MCPConnectionState
|
||||
) {
|
||||
guard mcpOperations[id] == nil else { return }
|
||||
let before = mcpConnections[id]?.toolset
|
||||
let operationID = UUID()
|
||||
mcpConnections[id] = MCPConnectionState(toolset: before, progress: progress)
|
||||
let task = Task {
|
||||
let result: MCPConnectionState
|
||||
do {
|
||||
let state = try await work()
|
||||
try Task.checkCancellation()
|
||||
result = state
|
||||
} catch {
|
||||
result = MCPConnectionState(toolset: before, output: Task.isCancelled ? "Cancelled." : error.localizedDescription,
|
||||
failed: !Task.isCancelled)
|
||||
}
|
||||
guard mcpOperations[id]?.id == operationID else { return }
|
||||
mcpOperations[id] = nil
|
||||
mcpConnections[id] = result
|
||||
}
|
||||
mcpOperations[id] = (operationID, task)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user