Jotley / app · jotley.ai / macOS / Utilities / JotleyMCPStdioBridge.swift
| 1 | import Foundation |
| 2 | import MCP |
| 3 | |
| 4 | /// The agent launches the existing app executable in this mode. It does not |
| 5 | /// instantiate SwiftUI, open Core Data or enable access. Only the running app |
| 6 | /// accesses the library and enforces permission/revision checks. |
| 7 | enum JotleyMCPStdioBridge { |
| 8 | static func run() async { |
| 9 | do { |
| 10 | let connection = try configuration() |
| 11 | try await forward(endpoint: connection.endpoint, token: connection.token) |
| 12 | } catch { |
| 13 | // Never include requests, note content or credentials in diagnostics. |
| 14 | FileHandle.standardError.write(Data("Open Jotley, enable MCP access in Settings → Integrations → MCP, and restart this server in your agent. If macOS asks, approve Jotley’s existing Keychain access.\n".utf8)) |
| 15 | exit(1) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | private static func configuration() throws -> (endpoint: URL, token: String) { |
| 20 | #if DEBUG |
| 21 | // Integration harness only; these overrides are absent from Release. |
| 22 | if let address = ProcessInfo.processInfo.environment["JOTLEY_MCP_TEST_ENDPOINT"], |
| 23 | let token = ProcessInfo.processInfo.environment["JOTLEY_MCP_TEST_TOKEN"], |
| 24 | let endpoint = URL(string: address), endpoint.scheme == "http", endpoint.host == "127.0.0.1", |
| 25 | endpoint.path == "/mcp", token.count == 64 { |
| 26 | return (endpoint, token) |
| 27 | } |
| 28 | #endif |
| 29 | guard let token = try JotleyMCPToken.loadExisting() else { |
| 30 | throw LocalError.message("Enable MCP in Jotley first.") |
| 31 | } |
| 32 | return (URL(string: "http://127.0.0.1:\(JotleyMCPService.defaultPort)/mcp")!, token) |
| 33 | } |
| 34 | |
| 35 | static func forward(endpoint: URL, token: String) async throws { |
| 36 | let client = Client(name: "Jotley desktop bridge", version: "1.0.0") |
| 37 | let configuration = URLSessionConfiguration.ephemeral |
| 38 | configuration.timeoutIntervalForRequest = 600 |
| 39 | configuration.timeoutIntervalForResource = 660 |
| 40 | let transport = HTTPClientTransport(endpoint: endpoint, configuration: configuration, streaming: false, requestModifier: { request in |
| 41 | var request = request |
| 42 | request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") |
| 43 | return request |
| 44 | }) |
| 45 | do { |
| 46 | let upstream = try await client.connect(transport: transport) |
| 47 | let server = Server(name: "Jotley", version: "1.0.0", instructions: upstream.instructions, capabilities: .init(tools: .init())) |
| 48 | await server.withMethodHandler(ListTools.self) { parameters in |
| 49 | let context = try await client.send(ListTools.request(parameters)) |
| 50 | return try await context.value |
| 51 | } |
| 52 | await server.withMethodHandler(CallTool.self) { parameters in |
| 53 | let context = try await client.send(CallTool.request(parameters)) |
| 54 | return try await context.value |
| 55 | } |
| 56 | try await server.start(transport: StdioTransport()) |
| 57 | await server.waitUntilCompleted() |
| 58 | await server.stop() |
| 59 | await client.disconnect() |
| 60 | } catch { |
| 61 | await client.disconnect() |
| 62 | throw error |
| 63 | } |
| 64 | } |
| 65 | } |