Textley / app · textley.ai / Sources / Textley / MCPSettingsView.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | struct MCPSettingsView: View { |
| 5 | let service: TextleyMCPService |
| 6 | |
| 7 | private enum Client: String, CaseIterable, Identifiable { |
| 8 | case claudeCode = "Claude Code" |
| 9 | case codex = "Codex" |
| 10 | case claudeChat = "Claude chat" |
| 11 | var id: Self { self } |
| 12 | } |
| 13 | |
| 14 | @State private var client: Client = .claudeCode |
| 15 | @State private var message: String? |
| 16 | |
| 17 | private var connectionUnavailable: Bool { !service.enabled || service.isChanging } |
| 18 | |
| 19 | var body: some View { |
| 20 | Form { |
| 21 | Section("MCP access") { |
| 22 | Toggle("Enable MCP access", isOn: Binding( |
| 23 | get: { service.enabled }, |
| 24 | set: { value in Task { await service.setEnabled(value) } } |
| 25 | )) |
| 26 | .disabled(service.isChanging) |
| 27 | .tint(TextleyTheme.accent) |
| 28 | .accessibilityIdentifier("mcpEnableToggle") |
| 29 | Toggle("Allow MCP to write", isOn: Binding( |
| 30 | get: { service.allowsWriting }, |
| 31 | set: service.setAllowsWriting |
| 32 | )) |
| 33 | .disabled(connectionUnavailable) |
| 34 | .tint(TextleyTheme.accent) |
| 35 | .accessibilityIdentifier("mcpWriteToggle") |
| 36 | Text("Agents can access open tabs and folders. A connected agent may send retrieved text to its AI provider.") |
| 37 | HStack { |
| 38 | VStack(alignment: .leading, spacing: 3) { |
| 39 | Label(service.status, systemImage: service.enabled ? "checkmark.circle.fill" : "circle") |
| 40 | .fontWeight(.medium) |
| 41 | if service.enabled { |
| 42 | Text(service.allowsWriting ? "Read & write" : "Read only") |
| 43 | .foregroundStyle(.secondary) |
| 44 | } |
| 45 | } |
| 46 | Spacer() |
| 47 | if service.isTesting { ProgressView().controlSize(.small) } |
| 48 | Button("Test Connection") { Task { await service.testConnection() } } |
| 49 | .disabled(connectionUnavailable || service.isTesting) |
| 50 | } |
| 51 | if !service.enabled { |
| 52 | Text("Turn on MCP access to test the connection and use the setup actions below.") |
| 53 | .foregroundStyle(.secondary) |
| 54 | } |
| 55 | if let error = service.errorMessage { Text(error).foregroundStyle(.red) } |
| 56 | if let test = service.testMessage { Text(test) } |
| 57 | } |
| 58 | |
| 59 | Section("Connect an app") { |
| 60 | Picker("Client", selection: $client) { |
| 61 | ForEach(Client.allCases) { Text($0.rawValue).tag($0) } |
| 62 | } |
| 63 | .pickerStyle(.segmented) |
| 64 | .controlSize(.large) |
| 65 | .tint(TextleyTheme.accent) |
| 66 | switch client { |
| 67 | case .claudeCode: |
| 68 | Text("Save the connection in a project folder. Open that same folder in Claude’s Code tab in a local session.") |
| 69 | Button("Save Project Connection…") { |
| 70 | perform { |
| 71 | guard let config = TextleyMCPDesktopSetup.projectConfiguration else { return nil } |
| 72 | return try TextleyMCPDesktopSetup.saveProject(configuration: config) |
| 73 | } |
| 74 | } |
| 75 | .disabled(connectionUnavailable) |
| 76 | case .codex: |
| 77 | Text("Add a STDIO server named textley in Codex Settings → MCP servers. Paste the Command and Argument in their own fields.") |
| 78 | HStack { |
| 79 | Button("Copy Command") { copy(TextleyMCPDesktopSetup.command, name: "Command") } |
| 80 | Button("Copy Argument") { copy(TextleyMCPDesktopSetup.argument, name: "Argument") } |
| 81 | } |
| 82 | .disabled(connectionUnavailable) |
| 83 | case .claudeChat: |
| 84 | Text("Save the local extension, install it in Claude’s Extensions settings, then paste the access token.") |
| 85 | HStack { |
| 86 | Button("Save Claude Extension…") { |
| 87 | perform { try TextleyMCPDesktopSetup.saveClaudeExtension(endpoint: service.endpoint) } |
| 88 | } |
| 89 | Button("Copy Access Token") { |
| 90 | if let token = service.accessToken { copy(token, name: "Access token") } |
| 91 | } |
| 92 | } |
| 93 | .disabled(connectionUnavailable) |
| 94 | } |
| 95 | if let message { Text(message).foregroundStyle(.secondary) } |
| 96 | } |
| 97 | .onChange(of: client) { _, _ in message = nil } |
| 98 | |
| 99 | Section { AgentSkillSection() } |
| 100 | |
| 101 | Section("Activity and access controls") { |
| 102 | if let name = service.lastClientName, let date = service.lastClientDate { |
| 103 | LabeledContent("Last agent", value: name + " · " + date.formatted(date: .omitted, time: .shortened)) |
| 104 | } |
| 105 | if let tool = service.lastToolName, let date = service.lastToolDate { |
| 106 | LabeledContent("Last successful call", value: tool + " · " + date.formatted(date: .omitted, time: .shortened)) |
| 107 | } |
| 108 | Button("Rotate Access Token…") { confirmRotation() }.disabled(connectionUnavailable) |
| 109 | Button("Copy HTTP Configuration") { |
| 110 | if let configuration = service.clientConfiguration { copy(configuration, name: "HTTP configuration") } |
| 111 | } |
| 112 | .disabled(connectionUnavailable) |
| 113 | } |
| 114 | } |
| 115 | .formStyle(.grouped) |
| 116 | .font(.system(size: 14)) |
| 117 | .buttonStyle(SettingsActionButtonStyle()) |
| 118 | } |
| 119 | |
| 120 | private func copy(_ value: String, name: String) { |
| 121 | let board = NSPasteboard.general |
| 122 | board.clearContents() |
| 123 | board.setString(value, forType: .string) |
| 124 | message = name + " copied." |
| 125 | } |
| 126 | |
| 127 | private func perform(_ action: () throws -> String?) { |
| 128 | do { message = try action() } catch { NSApp.presentError(error) } |
| 129 | } |
| 130 | |
| 131 | private func confirmRotation() { |
| 132 | let alert = NSAlert() |
| 133 | alert.messageText = "Rotate the Text Editor MCP token?" |
| 134 | alert.informativeText = "Existing connections will need to reconnect. Claude chat and direct HTTP clients need the new token." |
| 135 | alert.addButton(withTitle: "Rotate Token") |
| 136 | alert.addButton(withTitle: "Cancel") |
| 137 | guard alert.runModal() == .alertFirstButtonReturn else { return } |
| 138 | Task { await service.rotateToken() } |
| 139 | } |
| 140 | } |