From c849b751d3ffe43951fc8b1e48392f96d45a9ca3 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Tue, 17 Nov 2026 11:00:02 +0000 Subject: [PATCH] Give Toolbox a full-width server list and focused tool inspector --- Sources/MCP.swift | 281 ++++++++++++++++++++++++++-------------------- 1 file changed, 160 insertions(+), 121 deletions(-) diff --git a/Sources/MCP.swift b/Sources/MCP.swift index 14ee1f3..263ab5f 100644 --- a/Sources/MCP.swift +++ b/Sources/MCP.swift @@ -235,127 +235,16 @@ struct MCPSettings: View { @State private var arguments = "{}" @State private var confirm = false - private var selection: MCPConnectionID? { - if let selected, studio.mcpConnections[selected]?.toolset != nil { return selected } - return studio.activeToolsets.first?.id - } + private var selection: MCPConnectionID? { selected } var body: some View { - HSplitView { - Form { - Section("Built-In Tools") { - Toggle("Web Search and Page Retrieval", isOn: $builtInWebEnabled) - .onChange(of: builtInWebEnabled) { _, _ in studio.restoreToolsets() } - Text(builtInWebEnabled ? "Ready for web requests in chat." : "Automatic web lookup is off.") - .font(.caption).foregroundStyle(.secondary) - } - Section("Custom MCP Servers") { - Menu("Manage Servers", systemImage: "ellipsis.circle") { - Button("Import mcp.json…") { importingConfig = true } - Button("Use LM Studio Config") { - loadConfig(FileManager.default.homeDirectoryForCurrentUser.appending(path: ".lmstudio/mcp.json")) - } - if !configPath.isEmpty { - Button("Reload") { loadConfig(URL(fileURLWithPath: configPath)) } - Button("Edit File") { NSWorkspace.shared.open(URL(fileURLWithPath: configPath)) } - } - }.buttonStyle(.bordered) - ForEach(profiles.keys.sorted(), id: \.self) { name in - 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 } - } - } - } - } - if !configStatus.isEmpty { Text(configStatus).foregroundStyle(.secondary).textSelection(.enabled) } - } - Section("Manual Connection") { - DisclosureGroup("Configure a manual connection") { - Picker("Transport", selection: $transport) { - Text("Streamable HTTP").tag("http") - Text("Local stdio").tag("stdio") - } - if transport == "stdio" { - TextField("Executable or command", text: $executable) - TextField("Arguments (one per line)", text: $launchArguments, axis: .vertical) - } else { - TextField("Endpoint", text: $endpoint) - SecureField("Bearer token (optional)", text: $token) - } - connectionRow(.manual, title: "Manual server", subtitle: transport == "stdio" ? executable : endpoint, - icon: "slider.horizontal.3", local: transport == "stdio", - available: transport == "stdio" ? !executable.isEmpty : !endpoint.isEmpty, - connect: connectManual) - } - } + Group { + if let selected { + toolInspector(selected) + } else { + serverList } - .formStyle(.grouped) - .frame(minWidth: 300, idealWidth: 340) - Form { - if let selection, let state = studio.mcpConnections[selection], let toolset = state.toolset { - Section("Run a Tool") { - Picker("Connected server", selection: Binding( - get: { self.selection }, set: { selected = $0 } - )) { - ForEach(studio.activeToolsets, id: \.id) { entry in - Text(entry.toolset.name).tag(Optional(entry.id)) - } - } - if !toolset.tools.isEmpty { - Picker("Tool", selection: $tool) { - ForEach(toolset.tools, id: \.name) { Text($0.name).tag($0.name) } - } - if let entry = toolset.tools.first(where: { $0.name == tool }) { - if !entry.description.isEmpty { Text(entry.description).foregroundStyle(.secondary) } - DisclosureGroup("Input Schema") { - Text(String(decoding: entry.inputSchemaJSON, as: UTF8.self)) - .font(.system(.caption, design: .monospaced)).textSelection(.enabled) - } - } - DisclosureGroup("Arguments") { - TextEditor(text: $arguments).font(.system(.body, design: .monospaced)) - .frame(height: 85).accessibilityLabel("JSON arguments") - } - Button("Review Tool Call…") { confirm = true } - .disabled(tool.isEmpty || state.progress != nil) - } - if !state.output.isEmpty && (state.completedTool != nil || state.failed) { - ScrollView { - Text(state.output).font(.system(.caption, design: .monospaced)).textSelection(.enabled) - .frame(maxWidth: .infinity, alignment: .leading) - }.frame(maxHeight: 160) - if let completed = state.completedTool { - Button("Attach Result to Chat") { studio.attachToolResult(name: completed, output: state.output) } - .disabled(studio.running) - } - } - } - } else { - ContentUnavailableView("Select a connected server", systemImage: "wrench.and.screwdriver", - description: Text("Connect a server on the left to see and run its tools.")) - } - } - .formStyle(.grouped) - .frame(minWidth: 320) } - .formStyle(.grouped) .fileImporter(isPresented: $importingConfig, allowedContentTypes: [.json]) { result in do { loadConfig(try result.get()) } catch { configStatus = error.localizedDescription } @@ -392,6 +281,158 @@ struct MCPSettings: View { } } + private var serverList: some View { + Form { + Section { + Toggle(isOn: $builtInWebEnabled) { + Label("Web Search & Page Retrieval", systemImage: "globe") + } + .onChange(of: builtInWebEnabled) { _, _ in studio.restoreToolsets() } + if studio.mcpConnections[.web]?.toolset != nil { + Button("Inspect Web Tools") { selected = .web } + } + } header: { + Text("Built-In") + } + + Section { + if profiles.isEmpty { + Text(configStatus.isEmpty ? "Import a configuration to add servers." : configStatus) + .foregroundStyle(.secondary) + } + ForEach(profiles.keys.sorted(), id: \.self) { name in + if let profile = profiles[name] { profileRow(name, profile: profile) } + } + } header: { + HStack { + Text("MCP Servers") + Spacer() + Menu("Manage") { + Button("Import Configuration…") { importingConfig = true } + Button("Use LM Studio Configuration") { + loadConfig(FileManager.default.homeDirectoryForCurrentUser.appending(path: ".lmstudio/mcp.json")) + } + if !configPath.isEmpty { + Button("Reload Configuration") { loadConfig(URL(fileURLWithPath: configPath)) } + Button("Open Configuration File") { NSWorkspace.shared.open(URL(fileURLWithPath: configPath)) } + } + } + .fixedSize() + } + } footer: { + if !profiles.isEmpty && !configStatus.isEmpty { Text(configStatus).textSelection(.enabled) } + } + + Section { + DisclosureGroup("Manual Connection") { + Picker("Transport", selection: $transport) { + Text("Streamable HTTP").tag("http") + Text("Local stdio").tag("stdio") + } + if transport == "stdio" { + TextField("Executable or command", text: $executable) + TextField("Arguments (one per line)", text: $launchArguments, axis: .vertical) + } else { + TextField("Endpoint", text: $endpoint) + SecureField("Bearer token (optional)", text: $token) + } + connectionRow(.manual, title: "Manual server", subtitle: transport == "stdio" ? executable : endpoint, + icon: "slider.horizontal.3", local: transport == "stdio", + available: transport == "stdio" ? !executable.isEmpty : !endpoint.isEmpty, + connect: connectManual) + } + } + } + .formStyle(.grouped) + } + + private func profileRow(_ name: String, profile: MCPServerConfiguration) -> some View { + let id = MCPConnectionID.profile(name) + let state = studio.mcpConnections[id] + let enabled = Binding( + get: { studio.enabledMCPProfiles(path: configPath).contains(name) }, + set: { studio.setMCPProfile(name, enabled: $0, path: configPath) } + ) + let host = profile.url.flatMap { URL(string: $0)?.host } + let transportName = host ?? "Local process" + return VStack(alignment: .leading, spacing: 6) { + HStack(spacing: 12) { + Image(systemName: host == nil ? "terminal" : "network") + .foregroundStyle(.secondary) + .frame(width: 20) + VStack(alignment: .leading, spacing: 3) { + Text(name).fontWeight(.medium) + Text(transportName).font(.caption).foregroundStyle(.secondary) + } + Spacer() + if let toolset = state?.toolset { + Button("\(toolset.tools.count) tools", systemImage: "chevron.right") { selected = id } + .buttonStyle(.borderless) + } else if state?.failed == true { + Button("Retry") { studio.restoreToolsets() } + } else if state?.progress != nil { + ProgressView().controlSize(.small) + } + Toggle("Enable \(name)", isOn: enabled).labelsHidden().toggleStyle(.switch) + } + if state?.failed == true, let output = state?.output { + Text(output).font(.caption).foregroundStyle(.red).textSelection(.enabled) + } + } + .padding(.vertical, 5) + } + + private func toolInspector(_ id: MCPConnectionID) -> some View { + Form { + Section { + Button("Back to Servers", systemImage: "chevron.left") { selected = nil } + .buttonStyle(.borderless) + } + if let state = studio.mcpConnections[id], let toolset = state.toolset { + Section(toolset.name) { + if toolset.tools.isEmpty { + Text("This server exposes no tools.").foregroundStyle(.secondary) + } else { + Picker("Tool", selection: $tool) { + ForEach(toolset.tools, id: \.name) { Text($0.name).tag($0.name) } + } + if let entry = toolset.tools.first(where: { $0.name == tool }) { + if !entry.description.isEmpty { Text(entry.description).foregroundStyle(.secondary) } + DisclosureGroup("Input Schema") { + Text(String(decoding: entry.inputSchemaJSON, as: UTF8.self)) + .font(.system(.caption, design: .monospaced)).textSelection(.enabled) + } + } + DisclosureGroup("Arguments") { + TextEditor(text: $arguments).font(.system(.body, design: .monospaced)) + .frame(height: 100).accessibilityLabel("JSON arguments") + } + if let progress = state.progress { + LabeledContent(progress) { Button("Cancel") { studio.cancelMCP(id) } } + } else { + Button("Review Tool Call…") { confirm = true } + .disabled(tool.isEmpty) + } + } + } + if state.completedTool != nil || state.failed { + Section("Result") { + Text(state.output).font(.system(.caption, design: .monospaced)).textSelection(.enabled) + if let completed = state.completedTool { + Button("Attach Result to Chat") { studio.attachToolResult(name: completed, output: state.output) } + .disabled(studio.running) + } + } + } + } else { + Section { + Text("This server disconnected.").foregroundStyle(.secondary) + } + } + } + .formStyle(.grouped) + } + private func connectionRow( _ id: MCPConnectionID, title: String, subtitle: String, icon: String, local: Bool = false, available: Bool = true, connect: @escaping () -> Void @@ -409,10 +450,8 @@ struct MCPSettings: View { Button("Tools") { selected = id } Button("Disconnect") { studio.disconnectMCP(id) } } else { - Button(local ? "Start & Connect" : "Connect") { - selected = id - connect() - }.disabled(!available) + Button(local ? "Start & Connect" : "Connect", action: connect) + .disabled(!available) } }.buttonStyle(.bordered) if !subtitle.isEmpty { Text(subtitle).font(.caption).foregroundStyle(.secondary).lineLimit(1).help(subtitle) }