Jotley / app / macOS / Views / SettingsView+MCPTab.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | extension SettingsView { |
| 5 | var mcpView: some View { |
| 6 | ScrollView { |
| 7 | VStack(alignment: .leading, spacing: 22) { |
| 8 | HStack(alignment: .top, spacing: 14) { |
| 9 | Image(systemName: "point.3.connected.trianglepath.dotted") |
| 10 | .font(.system(size: 26, weight: .medium)).foregroundStyle(Color.themePrimaryAccent) |
| 11 | .frame(width: 52, height: 52) |
| 12 | .background(Color.primary.opacity(0.08), in: RoundedRectangle(cornerRadius: 14)) |
| 13 | VStack(alignment: .leading, spacing: 6) { |
| 14 | Text("Connect your AI agents").font(.title2.bold()) |
| 15 | Text("Work with your notes from Claude Code, Codex or Claude chat on your Mac. Set up directly in the desktop apps.") |
| 16 | .foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | VStack(alignment: .leading, spacing: 14) { |
| 21 | Toggle(isOn: Binding(get: { mcp.enabled }, set: { value in |
| 22 | mcpCopiedItem = nil |
| 23 | Task { await mcp.setEnabled(value) } |
| 24 | })) { |
| 25 | VStack(alignment: .leading, spacing: 4) { |
| 26 | Text("Enable MCP access").font(.headline) |
| 27 | Text("Only on this Mac · You control what agents can do").font(.caption).foregroundStyle(.secondary) |
| 28 | } |
| 29 | } |
| 30 | .disabled(mcp.isChanging) |
| 31 | .accessibilityIdentifier("mcpEnableToggle") |
| 32 | Picker("Note access", selection: Binding(get: { mcp.allowsWriting }, set: { mcp.setAllowsWriting($0) })) { |
| 33 | Text("Read only").tag(false) |
| 34 | Text("Read & write").tag(true) |
| 35 | }.pickerStyle(.segmented).accessibilityIdentifier("mcpNoteAccess") |
| 36 | Text(mcp.allowsWriting |
| 37 | ? "Edit, organize, summarize, copy and delete notes. Manage tags, folders and Custom Words." |
| 38 | : "Agents can read notes, tags, folders and Custom Words. Choose Read & write to allow changes.") |
| 39 | .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 40 | Divider() |
| 41 | HStack(spacing: 8) { |
| 42 | Circle().fill(mcp.enabled ? Color.green : Color.secondary).frame(width: 7, height: 7) |
| 43 | Text(mcp.enabled ? "Server ready" : mcp.status) |
| 44 | if mcp.isChanging { ProgressView().controlSize(.small) } |
| 45 | Spacer() |
| 46 | Button(mcp.isTesting ? "Checking…" : "Test connection") { Task { await mcp.testConnection() } } |
| 47 | .disabled(!mcp.enabled || mcp.isChanging || mcp.isTesting) |
| 48 | .accessibilityIdentifier("mcpTestConnection") |
| 49 | }.font(.callout).accessibilityIdentifier("mcpConnectionStatus") |
| 50 | if let result = mcp.testMessage { |
| 51 | Text(result).font(.caption).foregroundStyle(.secondary) |
| 52 | .accessibilityIdentifier("mcpTestResult") |
| 53 | } |
| 54 | if let name = mcp.lastClientName, let date = mcp.lastClientDate { |
| 55 | Label { |
| 56 | Text("Last agent seen: \(name) · \(date.formatted(date: .omitted, time: .shortened))") |
| 57 | } icon: { Image(systemName: "checkmark.circle.fill").foregroundStyle(.green) } |
| 58 | .font(.caption).accessibilityIdentifier("mcpLastAgent") |
| 59 | } else { |
| 60 | Text(mcp.enabled ? "Waiting for your agent. A server check does not connect your agent." : "Turn access on to begin setup.") |
| 61 | .font(.caption).foregroundStyle(.secondary) |
| 62 | } |
| 63 | if let tool = mcp.lastToolName { |
| 64 | Text("Last successful request: \(tool) · \(mcp.requestCount) this session") |
| 65 | .font(.caption).foregroundStyle(.secondary) |
| 66 | .accessibilityIdentifier("mcpLastTool") |
| 67 | } |
| 68 | } |
| 69 | .padding(18) |
| 70 | .background(.primary.opacity(0.035), in: RoundedRectangle(cornerRadius: 14)) |
| 71 | .overlay { |
| 72 | RoundedRectangle(cornerRadius: 14) |
| 73 | .strokeBorder(.primary.opacity(0.12)) |
| 74 | .allowsHitTesting(false) |
| 75 | } |
| 76 | |
| 77 | if let error = mcp.errorMessage { |
| 78 | Label(error, systemImage: "exclamationmark.triangle").foregroundStyle(.red).font(.callout).textSelection(.enabled) |
| 79 | } |
| 80 | |
| 81 | mcpDesktopSetup |
| 82 | |
| 83 | AgentSkillSection() |
| 84 | .padding(18) |
| 85 | .background(.primary.opacity(0.035), in: RoundedRectangle(cornerRadius: 14)) |
| 86 | |
| 87 | Label("Connected agents can read your library and may send retrieved text to their AI provider. Read & write also allows changes and permanent deletion. Keep Jotley open while using MCP.", systemImage: "lock.shield") |
| 88 | .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 89 | |
| 90 | DisclosureGroup("Advanced: other clients & access controls", isExpanded: $mcpShowingDetails) { |
| 91 | VStack(alignment: .leading, spacing: 12) { |
| 92 | Text(mcp.endpoint).font(.system(.callout, design: .monospaced)).textSelection(.enabled) |
| 93 | Text("The HTTP configuration contains a private access token. Jotley stores its original in Keychain. Desktop bridge setup does not include it in copied fields or project files.") |
| 94 | Button(mcpCopiedItem == "configuration" ? "Configuration copied ✓" : "Copy HTTP configuration") { |
| 95 | copyMCP(mcp.clientConfiguration, item: "configuration") |
| 96 | }.disabled(!mcp.enabled).accessibilityIdentifier("mcpCopyClientConfiguration") |
| 97 | HStack { |
| 98 | Button("Rotate access token") { |
| 99 | mcpCopiedItem = nil |
| 100 | Task { await mcp.rotateToken() } |
| 101 | }.disabled(mcp.isChanging || !mcp.enabled).accessibilityIdentifier("mcpRotateToken") |
| 102 | Link("Claude Code setup guide", destination: URL(string: "https://code.claude.com/docs/en/mcp")!) |
| 103 | } |
| 104 | Text("Rotation revokes old credentials. Restart the Jotley server in Claude Code or Codex afterward; the bundled bridge reads the new token from Keychain. For Claude chat, replace the extension token. Update direct HTTP clients with fresh configuration. Turning access off or quitting Jotley stops access.") |
| 105 | }.font(.caption).foregroundStyle(.secondary).padding(.top, 12) |
| 106 | }.font(.callout) |
| 107 | }.padding(28) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func mcpSetupStep<Content: View>(_ number: Int, title: String, detail: String, @ViewBuilder content: () -> Content) -> some View { |
| 112 | HStack(alignment: .top, spacing: 12) { |
| 113 | Text("\(number)").font(.system(.caption, design: .rounded).bold()) |
| 114 | .foregroundStyle(.primary).frame(width: 25, height: 25) |
| 115 | .background(Color.primary.opacity(0.08), in: RoundedRectangle(cornerRadius: 7)) |
| 116 | VStack(alignment: .leading, spacing: 8) { |
| 117 | Text(title).font(.callout.bold()) |
| 118 | Text(detail).font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 119 | content() |
| 120 | }.frame(maxWidth: .infinity, alignment: .leading) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func copyMCP(_ value: String?, item: String) { |
| 125 | guard let value else { return } |
| 126 | NSPasteboard.general.clearContents() |
| 127 | if NSPasteboard.general.setString(value, forType: .string) { mcpCopiedItem = item } |
| 128 | } |
| 129 | } |