Expose built-in web tools in MCP Toolbox

This commit is contained in:
Mark Gregory
2026-11-16 15:28:36 +00:00
parent 49140e0863
commit 28aa677372
5 changed files with 121 additions and 12 deletions
+76 -12
View File
@@ -225,7 +225,8 @@ struct MCPSettings: View {
@AppStorage("mcpExecutable") private var executable = ""
@AppStorage("mcpArguments") private var launchArguments = ""
private var client: (any MCPToolClient)? { studio.activeToolset?.client }
@State private var confirmLaunch = false
@AppStorage("webSearchEndpoint") private var webSearchEndpoint = ""
@State private var confirmingStartup = false
@AppStorage("mcpConfigPath") private var configPath = ""
@AppStorage("mcpSelectedProfile") private var selectedProfile = ""
@State private var profiles: [String: MCPServerConfiguration] = [:]
@@ -246,6 +247,15 @@ struct MCPSettings: View {
var body: some View {
Form {
Section("Built-In Tools") {
Button("Activate Web Toolset", action: activateWeb).disabled(busy)
if client is BuiltInWebTools {
LabeledContent("Connection", value: "Active")
Button("Disconnect Web Toolset") { resetConnection() }.disabled(busy)
}
Text("Retrieve pages directly. Search uses the SearXNG endpoint configured in Toolbox → Web.")
.foregroundStyle(.secondary)
}
Section("Custom MCP Servers") {
HStack {
Button("Import mcp.json…") { importingConfig = true }
@@ -285,7 +295,7 @@ struct MCPSettings: View {
}
HStack {
Button(local ? "Start Server and Discover…" : "Discover Tools") {
if local { confirmLaunch = true }
if local { confirmingStartup = true; confirm = true }
else { discover() }
}
.disabled(busy || target.isEmpty)
@@ -311,7 +321,7 @@ struct MCPSettings: View {
}
TextEditor(text: $arguments).font(.system(.body, design: .monospaced))
.frame(height: 80).accessibilityLabel("JSON arguments")
Button("Review Tool Call…") { confirm = true }.disabled(busy)
Button("Review Tool Call…") { confirmingStartup = false; confirm = true }.disabled(busy)
}
}
if !result.isEmpty {
@@ -347,15 +357,27 @@ struct MCPSettings: View {
.onChange(of: executable) { _, _ in resetConnection() }
.onChange(of: launchArguments) { _, _ in resetConnection() }
.onDisappear { operation?.cancel() }
.confirmationDialog("Start local MCP server?", isPresented: $confirmLaunch, titleVisibility: .visible) {
Button("Start and Discover", action: discover)
.confirmationDialog(confirmingStartup ? "Start local MCP server?" : "Run \(name)?",
isPresented: $confirm, titleVisibility: .visible) {
if confirmingStartup { Button("Start and Discover", action: discover) }
else { Button("Run Tool", action: invoke) }
} message: {
Text("Launch \(target) with its configured arguments using your macOS account.")
if confirmingStartup {
Text("Launch \(target) with its configured arguments using your macOS account.")
} else {
Text("Send these arguments to \(studio.activeToolset?.name ?? target):\n\(arguments.prefix(800))")
}
}
.confirmationDialog("Run \(name)?", isPresented: $confirm, titleVisibility: .visible) {
Button("Run Tool", action: invoke)
} message: {
Text("Send these arguments to \(target):\n\(arguments.prefix(800))")
}
private func activateWeb() {
run {
let server = BuiltInWebTools(searchEndpoint: webSearchEndpoint)
let discovered = try await server.listTools()
try Task.checkCancellation()
studio.activeToolset = ActiveToolset(name: BuiltInWebTools.name, client: server, tools: discovered)
name = discovered.first?.name ?? ""
result = "\(discovered.count) built-in tools available."
}
}
@@ -368,7 +390,11 @@ struct MCPSettings: View {
profiles = file.mcpServers
configPath = url.path
if profiles[selectedProfile] == nil && (disconnect || !selectedProfile.isEmpty) {
selectedProfile = profiles.keys.sorted().first ?? ""
let names = profiles.keys.sorted()
selectedProfile = names.first { name in
guard let profile = profiles[name] else { return false }
return (try? profile.makeClient()) != nil
} ?? names.first ?? ""
}
configStatus = "\(profiles.count) custom server(s) loaded from \(url.lastPathComponent)."
} catch {
@@ -401,8 +427,10 @@ struct MCPSettings: View {
guard let client, !name.isEmpty else { return }
let tool = name
let data = Data(arguments.utf8)
let configuredClient: any MCPToolClient = client is BuiltInWebTools
? BuiltInWebTools(searchEndpoint: webSearchEndpoint) : client
run {
let output = try await client.callTool(tool, argumentsJSON: data)
let output = try await configuredClient.callTool(tool, argumentsJSON: data)
try Task.checkCancellation()
result = String(decoding: output, as: UTF8.self)
invokedTool = tool
@@ -753,3 +781,39 @@ struct ActiveToolset {
let client: any MCPToolClient
let tools: [MCPTool]
}
struct BuiltInWebTools: MCPToolClient {
static let name = "Built-In Web"
let searchEndpoint: String
func listTools() async throws -> [MCPTool] {
[
MCPTool(name: "retrieve_page", description: "Retrieve readable text from an HTTP or HTTPS page.",
inputSchemaJSON: Data(#"{"type":"object","properties":{"url":{"type":"string","format":"uri"}},"required":["url"]}"#.utf8)),
MCPTool(name: "web_search", description: "Search via the SearXNG endpoint configured in Toolbox → Web.",
inputSchemaJSON: Data(#"{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}"#.utf8))
]
}
func callTool(_ name: String, argumentsJSON: Data) async throws -> Data {
let arguments = try HTTPMCPClient.arguments(argumentsJSON)
switch name {
case "retrieve_page":
guard let address = arguments["url"] as? String else { throw StudioError("Provide a page URL in the url field.") }
let page = try await WebClipCapture.page(from: address)
return try JSONSerialization.data(withJSONObject: ["url": page.url.absoluteString, "text": page.text],
options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes])
case "web_search":
guard let query = arguments["query"] as? String else { throw StudioError("Provide a search query in the query field.") }
guard !searchEndpoint.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw StudioError("Set a SearXNG search URL in Toolbox → Web before using web_search.")
}
let results = try await WebSearch.results(endpoint: searchEndpoint, query: query)
return try JSONSerialization.data(withJSONObject: results.map {
["title": $0.title, "url": $0.url.absoluteString, "snippet": $0.snippet]
}, options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes])
default:
throw StudioError("Unknown built-in tool: \(name).")
}
}
}