diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml index 47def37524..ed47f97fc2 100644 --- a/.github/workflows/build-linux.yml +++ b/.github/workflows/build-linux.yml @@ -316,7 +316,7 @@ jobs: - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master with: toolchain: "1.98" - - name: Build and install both executables + - name: Build and install both executables and the completions run: | cd "source tree/linux" meson setup "build dir" -Dprofile=development @@ -324,6 +324,9 @@ jobs: DESTDIR="$PWD/install root" meson install -C "build dir" --no-rebuild test -x "install root/usr/local/bin/tablepro" test -x "install root/usr/local/libexec/tablepro-askpass" + test -s "install root/usr/local/share/bash-completion/completions/tablepro" + test -s "install root/usr/local/share/zsh/site-functions/_tablepro" + test -s "install root/usr/local/share/fish/vendor_completions.d/tablepro.fish" supply-chain: name: Supply chain diff --git a/.github/workflows/core.yml b/.github/workflows/core.yml index f07d6caa3e..790c1af78b 100644 --- a/.github/workflows/core.yml +++ b/.github/workflows/core.yml @@ -80,7 +80,7 @@ jobs: run: | cargo check --locked --target "$TARGET" --features "$FEATURES" --features "$IN_PROCESS_SSH" \ -p tablepro-core -p tablepro-net -p tablepro-storage -p tablepro-ssh -p tablepro-session \ - -p tablepro-foreign-import -p tablepro-ai -p tablepro-mcp -p tablepro-account \ + -p tablepro-foreign-import -p tablepro-ai -p tablepro-mcp -p tablepro-account -p tablepro-cli \ -p tablepro-test-support -p tablepro-test-fixtures \ -p tablepro-driver-cassandra -p tablepro-driver-clickhouse -p tablepro-driver-duckdb \ -p tablepro-driver-mongodb -p tablepro-driver-mssql -p tablepro-driver-mysql \ diff --git a/Cargo.lock b/Cargo.lock index 849bb97055..97916c2ace 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1229,6 +1229,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037e2a1a92236d0aff7e845093f64661d6df4c02c9fcc61a60e9e1d736fa392f" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.6.7" @@ -6106,6 +6115,7 @@ dependencies = [ "pastey", "pin-project-lite", "rand 0.10.2", + "reqwest", "schemars 1.2.2", "serde", "serde_json", @@ -7581,6 +7591,7 @@ dependencies = [ "sqlformat", "tablepro-account", "tablepro-ai", + "tablepro-cli", "tablepro-core", "tablepro-driver-cassandra", "tablepro-driver-clickhouse", @@ -7676,6 +7687,36 @@ dependencies = [ "uniffi", ] +[[package]] +name = "tablepro-cli" +version = "0.0.0" +dependencies = [ + "async-trait", + "clap", + "clap_complete", + "http", + "hyper-util", + "reqwest", + "rmcp", + "secrecy", + "serde", + "serde_json", + "tablepro-core", + "tablepro-driver-sqlite", + "tablepro-mcp", + "tablepro-session", + "tablepro-ssh", + "tablepro-storage", + "tempfile", + "thiserror 2.0.20", + "tokio", + "tokio-util", + "tower", + "tracing", + "unicode-width", + "uuid", +] + [[package]] name = "tablepro-core" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 1435fb5562..33abb65cd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "core/crates/account", "core/crates/ai", "core/crates/bindgen", + "core/crates/cli", "core/crates/core", "core/crates/ffi", "core/crates/foreign-import", @@ -36,6 +37,7 @@ publish = false [workspace.dependencies] tablepro-account = { path = "core/crates/account" } tablepro-ai = { path = "core/crates/ai" } +tablepro-cli = { path = "core/crates/cli" } tablepro-core = { path = "core/crates/core" } tablepro-driver-cassandra = { path = "core/crates/drivers/cassandra" } tablepro-driver-clickhouse = { path = "core/crates/drivers/clickhouse" } diff --git a/apple/.swiftlint.yml b/apple/.swiftlint.yml index db9d25800d..6647ff85a7 100644 --- a/apple/.swiftlint.yml +++ b/apple/.swiftlint.yml @@ -485,6 +485,8 @@ custom_rules: - '.*/[^/]*Tests/' - '.*Tests\.swift$' - '.*/(WebPageOpener|DataLinkPolicy|MCPPairingService|NotificationsSettingsView|SyncSettingsView|ConnectionRecoveryPerformer|OpenConnectionIntent)\.swift$' + # Open TablePro's own links, and the user's files and URLs, in the TablePro bundle the command ships in. + - '.*/(Handshake|CLILiveEnvironment)\.swift$' - '.*/(AppDelegate\+MainMenuActions|WhatsNewWindowController|SupportAskCoordinator|MainContentCoordinator\+Rewind|GoogleSignInService|ChatGPTCodexService)\.swift$' excluded_match_kinds: [comment, doccomment] message: "Open a URL from a server, a plugin or a file through WebPageOpener. NSWorkspace opens a file: path, an smb: share or any app's scheme as readily as a web page." diff --git a/apple/Packages/TableProCore/Sources/TableProProcess/ProcessLauncher.swift b/apple/Packages/TableProCore/Sources/TableProProcess/ProcessLauncher.swift index ede4663a22..24c9f43872 100644 --- a/apple/Packages/TableProCore/Sources/TableProProcess/ProcessLauncher.swift +++ b/apple/Packages/TableProCore/Sources/TableProProcess/ProcessLauncher.swift @@ -1,4 +1,7 @@ import Foundation +#if canImport(AppKit) +import AppKit +#endif public enum ProcessLaunchError: Error, Equatable, Sendable, LocalizedError { case executableNotAbsolute(String) @@ -36,6 +39,18 @@ private extension String { #endif public enum ProcessLauncher { + #if canImport(AppKit) + public static func openConfiguration( + environment: [String: String], + activates: Bool = true + ) -> NSWorkspace.OpenConfiguration { + let configuration = NSWorkspace.OpenConfiguration() + configuration.environment = environment + configuration.activates = activates + return configuration + } + #endif + static let shellNames: Set = ["sh", "bash", "zsh", "dash", "ksh", "csh", "tcsh", "fish", "env"] // The only place production code makes a `Process`. It comes back configured and not yet running, diff --git a/apple/Packages/TableProMacCore/Package.swift b/apple/Packages/TableProMacCore/Package.swift index 4d591cf852..33a1e2a300 100644 --- a/apple/Packages/TableProMacCore/Package.swift +++ b/apple/Packages/TableProMacCore/Package.swift @@ -20,6 +20,8 @@ let package = Package( .library(name: "TableProUtilities", targets: ["TableProUtilities"]), .library(name: "TableProSSHConfig", targets: ["TableProSSHConfig"]), .library(name: "TableProMCP", targets: ["TableProMCP"]), + // Only the tablepro-cli tool links this; the app never does, so ArgumentParser stays out of it. + .library(name: "TableProCommandLine", targets: ["TableProCommandLine"]), .library(name: "TableProMCPTestSupport", targets: ["TableProMCPTestSupport"]), .library(name: "TableProCloudAPI", targets: ["TableProCloudAPI"]) ], @@ -28,7 +30,9 @@ let package = Package( // Exact: the versions clients.md (macOS, Dependencies) names. .package(url: "https://github.com/apple/swift-openapi-generator", exact: "1.14.1"), .package(url: "https://github.com/apple/swift-openapi-runtime", exact: "1.13.0"), - .package(url: "https://github.com/apple/swift-openapi-urlsession", exact: "1.4.0") + .package(url: "https://github.com/apple/swift-openapi-urlsession", exact: "1.4.0"), + // Exact: the pin swift-openapi-generator already resolves, and licenses.yml names this version. + .package(url: "https://github.com/apple/swift-argument-parser", exact: "1.8.2") ], targets: [ .target( @@ -63,6 +67,18 @@ let package = Package( path: "Sources/TableProMCP", swiftSettings: appModuleSettings ), + .target( + name: "TableProCommandLine", + dependencies: [ + "TableProMCP", + "TableProUtilities", + .product(name: "TableProCoreTypes", package: "TableProCore"), + .product(name: "TableProProcess", package: "TableProCore"), + .product(name: "ArgumentParser", package: "swift-argument-parser") + ], + path: "Sources/TableProCommandLine", + swiftSettings: appModuleSettings + ), // openapi.json is a copy of tablepro-cloud's api/v2.json; scripts/update-cloud-api.sh refreshes it. .target( name: "TableProCloudAPI", @@ -111,6 +127,16 @@ let package = Package( ], path: "Tests/TableProMCPTests", swiftSettings: appModuleSettings + ), + .testTarget( + name: "TableProCommandLineTests", + dependencies: [ + "TableProCommandLine", + "TableProMCP", + "TableProMCPTestSupport" + ], + path: "Tests/TableProCommandLineTests", + swiftSettings: appModuleSettings ) ] ) diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/BrowseCommands.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/BrowseCommands.swift new file mode 100644 index 0000000000..fd1938dcfe --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/BrowseCommands.swift @@ -0,0 +1,180 @@ +import ArgumentParser +import Foundation +import TableProMCP + +struct ConnectionsCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "connections", + abstract: "List the saved connections this command can use." + ) + + @OptionGroup var output: FormatOption + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let listing = try await session.call(client, "list_connections", [:]) + let rows = (listing["connections"]?.arrayValue ?? []).map { entry in + [entry["name"] ?? .null, entry["id"] ?? .null, entry["type"] ?? .null] + } + session.write([CLIResultSet(columns: ["name", "id", "type"], rows: rows)], format: output.format ?? session.defaultFormat) + } +} + +struct DatabasesCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "databases", + abstract: "List a connection's databases." + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var output: FormatOption + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + let listing = try await session.call(client, "list_databases", ["connection_id": .string(connectionId)]) + let rows = (listing["databases"]?.arrayValue ?? []).map { [$0] } + session.write([CLIResultSet(columns: ["name"], rows: rows)], format: output.format ?? session.defaultFormat) + } +} + +struct SchemasCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "schemas", + abstract: "List a connection's schemas." + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var database: DatabaseOption + @OptionGroup var output: FormatOption + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + var arguments: [String: JsonValue] = ["connection_id": .string(connectionId)] + if let name = database.database { arguments["database"] = .string(name) } + let listing = try await session.call(client, "list_schemas", arguments) + let rows = (listing["schemas"]?.arrayValue ?? []).map { [$0] } + session.write([CLIResultSet(columns: ["name"], rows: rows)], format: output.format ?? session.defaultFormat) + } +} + +struct TablesCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "tables", + abstract: "List the tables and views in a schema." + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var database: DatabaseOption + @OptionGroup var schema: SchemaOption + @OptionGroup var output: FormatOption + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + var arguments: [String: JsonValue] = ["connection_id": .string(connectionId)] + if let name = database.database { arguments["database"] = .string(name) } + if let name = schema.schema { arguments["schema"] = .string(name) } + let listing = try await session.call(client, "list_tables", arguments) + let rows = (listing["tables"]?.arrayValue ?? []).map { entry in + [entry["schema"] ?? .null, entry["name"] ?? .null, entry["type"] ?? .null] + } + session.write([CLIResultSet(columns: ["schema", "name", "type"], rows: rows)], format: output.format ?? session.defaultFormat) + } +} + +struct DescribeCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "describe", + abstract: "Show a table's columns, indexes and foreign keys.", + discussion: "Indexes and foreign keys follow the columns as separate result sets when the table has any." + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var database: DatabaseOption + @OptionGroup var schema: SchemaOption + + @Flag(help: "Print the CREATE statement instead.") + var ddl = false + + @OptionGroup var output: FormatOption + + @Argument(help: ArgumentHelp("The table to describe.", valueName: "table")) + var table: String + + func validate() throws { + if ddl, output.format != nil { + throw ValidationError("--ddl prints the statement as text, so it takes no --format.") + } + } + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + var arguments: [String: JsonValue] = [ + "connection_id": .string(connectionId), + "table": .string(table) + ] + if let name = database.database { arguments["database"] = .string(name) } + if let name = schema.schema { arguments["schema"] = .string(name) } + let description = try await session.call(client, "describe_table", arguments) + + if ddl { + guard let statement = description["ddl"]?.stringValue, !statement.isEmpty else { + throw CLIFailure(.failure, "TablePro could not produce a CREATE statement for \(table) on this engine.") + } + session.writeText(statement) + return + } + session.write(Self.resultSets(description), format: output.format ?? session.defaultFormat) + } + + static func resultSets(_ description: JsonValue) -> [CLIResultSet] { + let columns = (description["columns"]?.arrayValue ?? []).map { column in + [ + column["name"] ?? .null, + column["data_type"] ?? .null, + column["is_nullable"] ?? .null, + column["default_value"] ?? .null, + column["is_primary_key"] ?? .null + ] + } + var sets = [CLIResultSet(columns: ["name", "type", "nullable", "default", "primary_key"], rows: columns)] + + let indexes = (description["indexes"]?.arrayValue ?? []).map { index in + [ + index["name"] ?? .null, + joined(index["columns"]), + index["is_unique"] ?? .null, + index["is_primary"] ?? .null + ] + } + if !indexes.isEmpty { + sets.append(CLIResultSet(columns: ["index", "columns", "unique", "primary"], rows: indexes)) + } + + let foreignKeys = (description["foreign_keys"]?.arrayValue ?? []).map { key in + [ + key["name"] ?? .null, + key["column"] ?? .null, + key["referenced_table"] ?? .null, + key["referenced_column"] ?? .null, + key["on_delete"] ?? .null, + key["on_update"] ?? .null + ] + } + if !foreignKeys.isEmpty { + sets.append(CLIResultSet( + columns: ["foreign_key", "column", "referenced_table", "referenced_column", "on_delete", "on_update"], + rows: foreignKeys + )) + } + return sets + } + + private static func joined(_ value: JsonValue?) -> JsonValue { + guard let names = value?.arrayValue?.compactMap(\.stringValue) else { return .null } + return .string(names.joined(separator: ", ")) + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLICallbackListener.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLICallbackListener.swift new file mode 100644 index 0000000000..289d8c499a --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLICallbackListener.swift @@ -0,0 +1,176 @@ +import Foundation +import Network +import TableProMCP + +public final class CLICallbackListener: @unchecked Sendable { + private static let maximumHeadBytes = 16 * 1_024 + + private let state: String + private let queue = DispatchQueue(label: "com.TablePro.cli.pairing") + private let lock = NSLock() + private var listener: NWListener? + private var connections: [NWConnection] = [] + private var outcome: CLIPairingCallback? + private var waiter: CheckedContinuation? + + public init(state: String) { + self.state = state + } + + public func start() async throws -> UInt16 { + let parameters = NWParameters.tcp + parameters.requiredInterfaceType = .loopback + parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv4(.loopback), port: .any) + let listener = try NWListener(using: parameters) + lock.withLock { self.listener = listener } + + return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + let resumed = ResumeOnce() + listener.stateUpdateHandler = { @Sendable state in + switch state { + case .ready: + guard resumed.claim() else { return } + continuation.resume(returning: listener.port?.rawValue ?? 0) + case .failed(let error): + guard resumed.claim() else { return } + continuation.resume(throwing: error) + default: + break + } + } + listener.newConnectionHandler = { @Sendable [weak self] connection in + self?.accept(connection) + } + listener.start(queue: queue) + } + } + + // Nil when nothing answered in time. + public func waitForCallback(timeout: Duration) async -> CLIPairingCallback? { + let timer = Task { [weak self] in + try? await Task.sleep(for: timeout) + guard !Task.isCancelled else { return } + self?.finish(.ignored) + } + defer { timer.cancel() } + let result = await withCheckedContinuation { (continuation: CheckedContinuation) in + let ready: CLIPairingCallback? = lock.withLock { + if let outcome { return outcome } + waiter = continuation + return nil + } + if let ready { + continuation.resume(returning: ready) + } + } + return result == .ignored ? nil : result + } + + public func stop() { + let (listener, connections) = lock.withLock { () -> (NWListener?, [NWConnection]) in + let taken = (self.listener, self.connections) + self.listener = nil + self.connections = [] + return taken + } + listener?.cancel() + connections.forEach { $0.cancel() } + } + + private func finish(_ result: CLIPairingCallback) { + let waiting: CheckedContinuation? = lock.withLock { + guard outcome == nil else { return nil } + outcome = result + let taken = waiter + waiter = nil + return taken + } + waiting?.resume(returning: result) + } + + private func accept(_ connection: NWConnection) { + lock.withLock { connections.append(connection) } + connection.start(queue: queue) + receive(on: connection, buffer: Data()) + } + + private func receive(on connection: NWConnection, buffer: Data) { + connection.receive(minimumIncompleteLength: 1, maximumLength: 4_096) { @Sendable [weak self] data, _, isComplete, error in + guard let self else { return } + var accumulated = buffer + if let data { + accumulated.append(data) + } + if let head = Self.requestHead(accumulated) { + self.answer(head, on: connection) + return + } + guard error == nil, !isComplete, accumulated.count < Self.maximumHeadBytes else { + connection.cancel() + return + } + self.receive(on: connection, buffer: accumulated) + } + } + + private func answer(_ head: String, on connection: NWConnection) { + let parts = head.split(separator: " ", maxSplits: 2) + let callback: CLIPairingCallback + if parts.count >= 2, parts[0] == "GET" { + callback = CLIPairingCallback.parse(requestTarget: String(parts[1]), state: state) + } else { + callback = .ignored + } + let response = Self.response(for: callback) + connection.send(content: response, completion: .contentProcessed { @Sendable _ in + connection.cancel() + }) + guard callback != .ignored else { return } + finish(callback) + } + + static func requestHead(_ data: Data) -> String? { + guard let end = data.range(of: Data("\r\n".utf8)) else { return nil } + return String(data: data[data.startIndex.. Data { + let status: String + let message: String + switch callback { + case .approved: + status = "200 OK" + message = "TablePro paired the tablepro command. You can close this tab." + case .denied: + status = "200 OK" + message = "Pairing was declined. You can close this tab." + case .ignored: + status = "404 Not Found" + message = "Nothing here." + } + let body = "TablePro

\(message)

\n" + let head = [ + "HTTP/1.1 \(status)", + "Content-Type: text/html; charset=utf-8", + "Content-Length: \(body.utf8.count)", + "Cache-Control: no-store", + "Connection: close", + "", + "" + ].joined(separator: "\r\n") + return Data((head + body).utf8) + } +} + +private final class ResumeOnce: @unchecked Sendable { + private let lock = NSLock() + private var claimed = false + + func claim() -> Bool { + lock.withLock { + guard !claimed else { return false } + claimed = true + return true + } + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIEnvironment.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIEnvironment.swift new file mode 100644 index 0000000000..8d2e71fa04 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIEnvironment.swift @@ -0,0 +1,133 @@ +import Darwin +import Foundation +import TableProMCP + +public protocol CLIConsole: AnyObject, Sendable { + func out(_ text: String) + func err(_ text: String) +} + +public struct CLITerminal: Sendable, Equatable { + public let standardInput: Bool + public let standardOutput: Bool + public let standardError: Bool + + public init(standardInput: Bool, standardOutput: Bool, standardError: Bool) { + self.standardInput = standardInput + self.standardOutput = standardOutput + self.standardError = standardError + } + + public static var current: CLITerminal { + CLITerminal( + standardInput: isatty(STDIN_FILENO) == 1, + standardOutput: isatty(STDOUT_FILENO) == 1, + standardError: isatty(STDERR_FILENO) == 1 + ) + } +} + +// `appPath` is the bundle of the process serving MCP, which a pairing link has to reach. +public struct CLIServerEndpoint: Sendable, Equatable { + public let socketPath: String + public let serverPID: Int32 + public let appPath: String? + + public init(socketPath: String, serverPID: Int32, appPath: String?) { + self.socketPath = socketPath + self.serverPID = serverPID + self.appPath = appPath + } +} + +// What `status` and `unpair` see without launching TablePro or waiting for it. +public enum CLIServerProbe: Sendable, Equatable { + case reachable(CLIServerEndpoint) + case notRunning + case turnedOff + case unreachable(String) + case peerChanged +} + +public struct CLIHostApp: Sendable, Equatable { + public let url: URL? + public let version: String? + public let bundleIdentifier: String? + + public init(url: URL?, version: String?, bundleIdentifier: String?) { + self.url = url + self.version = version + self.bundleIdentifier = bundleIdentifier + } + + public init(executablePath: String?) { + guard let url = executablePath.flatMap(MCPHostApp.containing(executablePath:)) else { + self.init(url: nil, version: nil, bundleIdentifier: nil) + return + } + let bundle = Bundle(url: url) + self.init( + url: url, + version: bundle?.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String, + bundleIdentifier: bundle?.bundleIdentifier + ) + } + + public static let current = CLIHostApp(executablePath: MCPHandshakeTrust.executablePath(forPid: getpid())) + + var versionLine: String { + "tablepro \(version ?? "unknown")" + } +} + +public struct CLIEnvironment: Sendable { + public typealias ServerOpener = @Sendable () async throws -> CLIServerEndpoint + public typealias ServerFinder = @Sendable () async -> CLIServerProbe + public typealias Pairer = @Sendable (_ server: CLIServerEndpoint) async throws -> String + public typealias TargetOpener = @Sendable (_ targets: [URL]) async throws -> Void + public typealias TextReader = @Sendable () throws -> String + public typealias FileReader = @Sendable (_ path: String) throws -> String + public typealias FileCheck = @Sendable (_ path: String) -> Bool + + public var console: any CLIConsole + public var terminal: CLITerminal + public var tokenStore: any CLITokenStoring + public var app: CLIHostApp + // Launches TablePro when it is not running, then proves the server before any token goes out. + public var openServer: ServerOpener + public var findServer: ServerFinder + public var pair: Pairer + public var openTargets: TargetOpener + public var readStandardInput: TextReader + public var readFile: FileReader + public var fileExists: FileCheck + public var currentDirectory: URL + + public init( + console: any CLIConsole, + terminal: CLITerminal, + tokenStore: any CLITokenStoring, + app: CLIHostApp, + openServer: @escaping ServerOpener, + findServer: @escaping ServerFinder, + pair: @escaping Pairer, + openTargets: @escaping TargetOpener, + readStandardInput: @escaping TextReader, + readFile: @escaping FileReader, + fileExists: @escaping FileCheck, + currentDirectory: URL + ) { + self.console = console + self.terminal = terminal + self.tokenStore = tokenStore + self.app = app + self.openServer = openServer + self.findServer = findServer + self.pair = pair + self.openTargets = openTargets + self.readStandardInput = readStandardInput + self.readFile = readFile + self.fileExists = fileExists + self.currentDirectory = currentDirectory + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIFailure.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIFailure.swift similarity index 94% rename from apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIFailure.swift rename to apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIFailure.swift index 069624f2c9..2448423601 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIFailure.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIFailure.swift @@ -1,4 +1,5 @@ import Foundation +import TableProMCP public enum CLIExitCode: Int32, Sendable, Equatable { case success = 0 @@ -17,6 +18,8 @@ public struct CLIFailure: Error, Sendable, Equatable { self.message = message } + static let peerChanged = CLIFailure(.failure, "The local MCP server changed; run the command again.") + public static func usage(_ message: String) -> CLIFailure { CLIFailure(.usage, message) } diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLILiveEnvironment.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLILiveEnvironment.swift new file mode 100644 index 0000000000..86b19ab287 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLILiveEnvironment.swift @@ -0,0 +1,217 @@ +import AppKit +import Darwin +import Foundation +import TableProMCP +import TableProProcess +import TableProUtilities + +public extension CLIEnvironment { + static func live(console: any CLIConsole) -> CLIEnvironment { + let app = CLIHostApp.current + return CLIEnvironment( + console: console, + terminal: .current, + tokenStore: CLITokenStores.current(), + app: app, + openServer: { try await CLILive.openServer() }, + findServer: { await CLILive.findServer(app: app) }, + pair: { server in try await CLILive.pair(server: server, app: app) }, + openTargets: { targets in try await CLILive.open(targets, app: app) }, + readStandardInput: { + let data = FileHandle.standardInput.readDataToEndOfFile() + guard let text = String(data: data, encoding: .utf8) else { + throw CLIFailure.usage("Standard input is not UTF-8 text.") + } + return text + }, + readFile: { path in try String(contentsOfFile: path, encoding: .utf8) }, + fileExists: { path in FileManager.default.fileExists(atPath: path) }, + currentDirectory: URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true) + ) + } +} + +enum CLILive { + static let pairingTimeout: Duration = .seconds(330) + + // The server must prove it wrote the handshake before any token goes to its socket. + static func openServer() async throws -> CLIServerEndpoint { + let logger = MCPOSBridgeLogger(category: "CLI") + let handshake: MCPHandshakePayload + do { + handshake = try await MCPHandshakeAcquirer(logger: logger).acquire() + } catch MCPHandshakeAcquisitionError.serverTurnedOff { + throw CLIFailure( + .refused, + "TablePro's MCP server is turned off, and the tablepro command reaches TablePro through it. Turn it on under Settings > MCP." + ) + } catch { + throw CLIFailure(.failure, "TablePro did not start its MCP server: \(error.localizedDescription)") + } + do { + return try await verified(handshake, logger: logger) + } catch MCPSocketHttpError.peerChanged { + throw CLIFailure.peerChanged + } + } + + // One read of the handshake, no launch and no polling: status must answer at once. + static func findServer(app: CLIHostApp) async -> CLIServerProbe { + let logger = MCPOSBridgeLogger(category: "CLI") + let file = MCPHandshakeLocation.file(in: MCPHandshakeLocation.supportDirectory()) + guard let handshake = try? MCPHandshakeTrust.load(at: file) else { + return isRunning(app) ? .turnedOff : .notRunning + } + return await probe(handshake, logger: logger) + } + + static func probe(_ handshake: MCPHandshakePayload, logger: any MCPBridgeLogger) async -> CLIServerProbe { + do { + return .reachable(try await verified(handshake, logger: logger)) + } catch MCPSocketHttpError.peerChanged { + return .peerChanged + } catch let failure as CLIFailure { + return .unreachable(failure.message) + } catch { + return .unreachable(error.localizedDescription) + } + } + + static func pair(server: CLIServerEndpoint, app: CLIHostApp) async throws -> String { + let request = CLIPairingRequest.random() + let listener = CLICallbackListener(state: request.state) + defer { listener.stop() } + + let callbackPort: UInt16 + do { + callbackPort = try await listener.start() + } catch { + throw CLIFailure(.failure, "Could not listen for the pairing answer: \(error.localizedDescription)") + } + guard let link = request.link(callbackPort: callbackPort) else { + throw CLIFailure(.failure, "Could not build the pairing link.") + } + // The one-time code is only good on the instance serving MCP. + let target = server.appPath.map { URL(fileURLWithPath: $0, isDirectory: true) } ?? app.url + do { + try await openInApp([link], app: target) + } catch { + throw CLIFailure(.failure, "Could not open TablePro to pair: \(error.localizedDescription)") + } + + switch await listener.waitForCallback(timeout: pairingTimeout) { + case .approved(let code): + return try await CLIServerClient.exchange(code: code, verifier: request.verifier, socketPath: server.socketPath, serverPID: server.serverPID) + case .denied: + throw CLIFailure(.refused, "Pairing was declined in TablePro.") + case .ignored, nil: + throw CLIFailure(.refused, "Pairing was not approved in time.") + } + } + + static func open(_ targets: [URL], app: CLIHostApp) async throws { + guard let appURL = app.url else { + throw CLIFailure(.failure, "This command runs only from inside TablePro.app, so it cannot tell which TablePro to open.") + } + do { + try await openInApp(targets, app: appURL) + } catch { + throw CLIFailure(.failure, "Could not open TablePro: \(error.localizedDescription)") + } + } + + // The copy this command ships in, not the one LaunchServices picks for a scheme or a bundle id. + // Substitution stays on, so a copy already running elsewhere answers instead of a second instance. + private static func openInApp(_ urls: [URL], app: URL?) async throws { + let configuration = launchConfiguration(environment: ProcessInfo.processInfo.environment) + guard let app else { + for url in urls { + _ = try await NSWorkspace.shared.open(url, configuration: configuration) + } + return + } + if urls.isEmpty { + _ = try await NSWorkspace.shared.openApplication(at: app, configuration: configuration) + } else { + _ = try await NSWorkspace.shared.open(urls, withApplicationAt: app, configuration: configuration) + } + } + + static func launchConfiguration(environment: [String: String]) -> NSWorkspace.OpenConfiguration { + ProcessLauncher.openConfiguration(environment: environment) + } + + private static func verified(_ handshake: MCPHandshakePayload, logger: any MCPBridgeLogger) async throws -> CLIServerEndpoint { + do { + _ = try await BridgeUpstreamHandshake.verifiedDiscovery( + handshake: handshake, + credentials: handshake.credentials(), + logger: logger + ) + } catch MCPSocketHttpError.peerChanged { + throw MCPSocketHttpError.peerChanged + } catch { + throw CLIFailure(.failure, "The local MCP server did not prove it is TablePro: \(error.localizedDescription)") + } + let appPath = MCPHandshakeTrust.executablePath(forPid: handshake.pid) + .flatMap(MCPHandshakeTrust.appBundleRoot(forExecutable:)) + return CLIServerEndpoint(socketPath: handshake.socketPath, serverPID: handshake.pid, appPath: appPath) + } + + private static func isRunning(_ app: CLIHostApp) -> Bool { + guard let identifier = app.bundleIdentifier else { return false } + return !NSRunningApplication.runningApplications(withBundleIdentifier: identifier).isEmpty + } +} + +public final class CLIStandardConsole: CLIConsole, @unchecked Sendable { + private static let flushThreshold = 64 * 1_024 + + private let lock = NSLock() + private var pending = Data() + private var firstFailure: Error? + + public init() {} + + public var outputFailure: Error? { + lock.withLock { firstFailure } + } + + public func out(_ text: String) { + let ready: Data? = lock.withLock { + pending.append(contentsOf: text.utf8) + guard pending.count >= Self.flushThreshold else { return nil } + defer { pending.removeAll(keepingCapacity: true) } + return pending + } + if let ready { + writeOutput(ready) + } + } + + public func err(_ text: String) { + flush() + try? DescriptorWrite.allBytes(Data(text.utf8), to: STDERR_FILENO) + } + + public func flush() { + let ready: Data = lock.withLock { + defer { pending.removeAll(keepingCapacity: true) } + return pending + } + guard !ready.isEmpty else { return } + writeOutput(ready) + } + + private func writeOutput(_ data: Data) { + do { + try DescriptorWrite.allBytes(data, to: STDOUT_FILENO) + } catch { + lock.withLock { + if firstFailure == nil { + firstFailure = error + } + } + } + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIOptions.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIOptions.swift new file mode 100644 index 0000000000..ae949d5bbe --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIOptions.swift @@ -0,0 +1,74 @@ +import ArgumentParser +import Foundation + +// --format has always taken any case; the library's own match is exact. +public enum CLIOutputFormat: String, Sendable, Equatable, CaseIterable, ExpressibleByArgument { + case csv + case tsv + case json + case jsonl + case table + + public init?(argument: String) { + self.init(rawValue: argument.lowercased()) + } +} + +enum CLIExportFormat: String, Sendable, Equatable, CaseIterable, ExpressibleByArgument { + case csv + case json + case sql + + init?(argument: String) { + self.init(rawValue: argument.lowercased()) + } +} + +// An own type keeps CompletionShell free of a retroactive conformance. +enum CLIShell: String, Sendable, CaseIterable, ExpressibleByArgument { + case bash + case zsh + case fish + + var completionShell: CompletionShell { + switch self { + case .bash: .bash + case .zsh: .zsh + case .fish: .fish + } + } +} + +struct ConnectionOption: ParsableArguments { + @Option( + name: [.customShort("c"), .customLong("connection")], + help: ArgumentHelp("A saved connection's name or id.", valueName: "connection") + ) + var connection: String +} + +struct FormatOption: ParsableArguments { + @Option(help: ArgumentHelp("Output format: table on a terminal, csv otherwise.", valueName: "format")) + var format: CLIOutputFormat? +} + +struct DatabaseOption: ParsableArguments { + @Option(help: ArgumentHelp("Database to use instead of the connection's own.", valueName: "name")) + var database: String? +} + +struct SchemaOption: ParsableArguments { + @Option(help: ArgumentHelp("Schema to use instead of the connection's own.", valueName: "name")) + var schema: String? +} + +struct LimitOption: ParsableArguments { + @Option(help: ArgumentHelp("Rows to return, up to Maximum row limit in Settings > MCP.", valueName: "rows")) + var limit: Int? + + func validate() throws { + if let limit, limit < 1 { + throw ValidationError("--limit takes a whole number above 0.") + } + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIRowWriter.swift similarity index 82% rename from apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift rename to apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIRowWriter.swift index 5814fea5f6..8217bdad32 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIRowWriter.swift @@ -1,5 +1,6 @@ import Foundation import TableProCoreTypes +import TableProMCP public struct CLIResultSet: Sendable, Equatable { public let columns: [String] @@ -147,7 +148,7 @@ public enum CLIRowWriter { case .int(let number): return String(number) case .double, .array, .object: - return value.jsonString() + return jsonDocument(value.jsonString()) } } @@ -158,11 +159,8 @@ public enum CLIRowWriter { switch scalar { case "\"": escaped += "\\\"" case "\\": escaped += "\\\\" - case "\n": escaped += "\\n" - case "\r": escaped += "\\r" - case "\t": escaped += "\\t" default: - if scalar.value < 0x20 { + if CLITerminalText.isControl(scalar) { escaped += String(format: "\\u%04x", scalar.value) } else { escaped.unicodeScalars.append(scalar) @@ -173,15 +171,31 @@ public enum CLIRowWriter { return escaped } + // JSON permits raw DEL and C1 in strings, but a terminal must never receive them. + static func jsonDocument(_ text: String) -> String { + guard text.unicodeScalars.contains(where: { (0x7F...0x9F).contains($0.value) }) else { return text } + var escaped = "" + escaped.reserveCapacity(text.utf8.count) + for scalar in text.unicodeScalars { + if (0x7F...0x9F).contains(scalar.value) { + escaped += String(format: "\\u%04x", scalar.value) + } else { + escaped.unicodeScalars.append(scalar) + } + } + return escaped + } + static let nullMarker = "NULL" + // Widths are terminal cells, not characters: 日本語 is three characters drawn six cells wide. private static func writeTable(_ set: CLIResultSet, to output: inout some TextOutputStream) { - let header = set.columns.map(tableCell) - let body = set.rows.map { cells($0, count: set.columns.count).map(tableCellValue) } - var widths = header.map(\.count) + let header = set.columns.map { MeasuredCell(tableCell($0)) } + let body = set.rows.map { cells($0, count: set.columns.count).map { MeasuredCell(tableCellValue($0)) } } + var widths = header.map(\.width) for row in body { for (index, cell) in row.enumerated() { - widths[index] = max(widths[index], cell.count) + widths[index] = max(widths[index], cell.width) } } output.write(tableLine(header, widths: widths)) @@ -192,13 +206,23 @@ public enum CLIRowWriter { } } - private static func tableLine(_ cells: [String], widths: [Int]) -> String { + private static func tableLine(_ cells: [MeasuredCell], widths: [Int]) -> String { let padded = cells.enumerated().map { index, cell in - index == cells.count - 1 ? cell : cell + String(repeating: " ", count: widths[index] - cell.count) + index == cells.count - 1 ? cell.text : cell.text + String(repeating: " ", count: widths[index] - cell.width) } return padded.joined(separator: " | ") + "\n" } + private struct MeasuredCell { + let text: String + let width: Int + + init(_ text: String) { + self.text = text + self.width = CLITerminalText.width(of: text) + } + } + private static func tableCellValue(_ value: JsonValue) -> String { guard let text = text(of: value) else { return nullMarker } return tableCell(text) @@ -206,7 +230,7 @@ public enum CLIRowWriter { // The table goes to a terminal, so a stored escape sequence is shown, never run. static func tableCell(_ text: String) -> String { - guard text.unicodeScalars.contains(where: isControl) else { return text } + guard text.unicodeScalars.contains(where: CLITerminalText.isControl) else { return text } var escaped = "" for scalar in text.unicodeScalars { switch scalar { @@ -215,8 +239,8 @@ public enum CLIRowWriter { case "\n": escaped += "\\n" case "\r": escaped += "\\r" default: - if isControl(scalar) { - escaped += String(format: "\\x%02X", scalar.value) + if CLITerminalText.isControl(scalar) { + escaped += CLITerminalText.hex(scalar) } else { escaped.unicodeScalars.append(scalar) } @@ -225,10 +249,6 @@ public enum CLIRowWriter { return escaped } - private static func isControl(_ scalar: Unicode.Scalar) -> Bool { - scalar.value < 0x20 || (0x7F...0x9F).contains(scalar.value) - } - private static func cells(_ row: [JsonValue], count: Int) -> [JsonValue] { guard row.count != count else { return row } if row.count > count { diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIServerClient.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIServerClient.swift similarity index 58% rename from apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIServerClient.swift rename to apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIServerClient.swift index a2e09152d7..e4cdd37041 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIServerClient.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLIServerClient.swift @@ -1,64 +1,90 @@ import Foundation - -public enum CLIClientIdentity { - // The app files a query under Command Line when a request names this client. - public static let name = "tablepro-cli" - public static let title = "Command Line" - public static let pairingName = "tablepro command" -} +import TableProMCP public enum CLIServerClientError: Error, Sendable, Equatable { // Revoked, expired or unknown. case tokenRejected(String) } +public enum CLIRevocation: Sendable, Equatable { + // Also an answer for a token TablePro no longer knows (RFC 7009, section 2.2). + case revoked + // HTTP 503 (RFC 7009, section 2.2.1): the token still works at TablePro. + case unavailable + case refused(String) +} + public struct CLIServerClient: Sendable { static let requestTimeout: Duration = .seconds(360) + static let revocationPath = "/v1/integrations/revoke" + private static let formUnreserved = CharacterSet( + charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" + ) private let socketPath: String + private let serverPID: Int32 private let token: String - public init(socketPath: String, token: String) { + public init(socketPath: String, serverPID: Int32, token: String) { self.socketPath = socketPath + self.serverPID = serverPID self.token = token } public func callTool(_ name: String, arguments: [String: JsonValue]) async throws -> JsonValue { - // The server tracks a call in flight by token and id, and every run of this command shares one token. - let requestId = JsonRpcId.string(UUID().uuidString) let params: JsonValue = .object([ "name": .string(name), "arguments": .object(arguments), - "_meta": .object([ - MCPMetaKeys.protocolVersion: .string(MCPProtocolVersion.latest.rawValue), - MCPMetaKeys.clientCapabilities: .object([:]), - MCPMetaKeys.clientInfo: .object([ - "name": .string(CLIClientIdentity.name), - "title": .string(CLIClientIdentity.title), - "version": .string("1") - ]) + "_meta": Self.meta + ]) + let result = try await rpc(method: "tools/call", params: params, toolName: name) + return try Self.toolOutput(result) + } + + // server/discover needs no scope and touches no connection, so it only proves the token. + public func tokenIsAccepted() async throws -> Bool { + do { + _ = try await rpc(method: "server/discover", params: .object(["_meta": Self.meta]), toolName: nil) + return true + } catch CLIServerClientError.tokenRejected { + return false + } + } + + private static var meta: JsonValue { + .object([ + MCPMetaKeys.protocolVersion: .string(MCPProtocolVersion.latest.rawValue), + MCPMetaKeys.clientCapabilities: .object([:]), + MCPMetaKeys.clientInfo: .object([ + "name": .string(CLIClientIdentity.name), + "title": .string(CLIClientIdentity.title), + "version": .string(CLIHostApp.current.version ?? "unknown") ]) ]) - let body = try JsonRpcCodec.encode(.request(JsonRpcRequest(id: requestId, method: "tools/call", params: params))) + } - let request = MCPSocketHttpRequest( - path: "/mcp", - headers: [ - ("Content-Type", "application/json"), - ("Accept", "application/json, text/event-stream"), - ("Authorization", "Bearer \(token)"), - ("MCP-Protocol-Version", MCPProtocolVersion.latest.rawValue), - ("Mcp-Method", "tools/call"), - ("Mcp-Name", MCPBase64Sentinel.encodeIfNeeded(name)) - ], - body: body - ) + private func rpc(method: String, params: JsonValue, toolName: String?) async throws -> JsonValue { + // The server tracks a call in flight by token and id, and every run of this command shares one token. + let requestId = JsonRpcId.string(UUID().uuidString) + let body = try JsonRpcCodec.encode(.request(JsonRpcRequest(id: requestId, method: method, params: params))) + + var headers = [ + ("Content-Type", "application/json"), + ("Accept", "application/json, text/event-stream"), + ("Authorization", "Bearer \(token)"), + ("MCP-Protocol-Version", MCPProtocolVersion.latest.rawValue), + ("Mcp-Method", method) + ] + if let toolName { + headers.append(("Mcp-Name", MCPBase64Sentinel.encodeIfNeeded(toolName))) + } + let request = MCPSocketHttpRequest(path: "/mcp", headers: headers, body: body) - let (data, response) = try await Self.send(request, socketPath: socketPath) + let (data, response) = try await Self.send(request, socketPath: socketPath, serverPID: serverPID) let message = try await responseMessage(data: data, response: response, requestId: requestId) switch message { case .successResponse(let success): - return try Self.toolOutput(success.result) + return success.result case .errorResponse(let failure): if failure.error.code == JsonRpcErrorCode.unauthenticated || failure.error.code == JsonRpcErrorCode.expired { throw CLIServerClientError.tokenRejected(failure.error.message) @@ -116,14 +142,14 @@ public struct CLIServerClient: Sendable { } // The exchange takes no bearer token: the one-time code and the verifier are the credential. - public static func exchange(code: String, verifier: String, socketPath: String) async throws -> String { + public static func exchange(code: String, verifier: String, socketPath: String, serverPID: Int32) async throws -> String { let request = MCPSocketHttpRequest( path: "/v1/integrations/exchange", headers: [("Content-Type", "application/json")], body: try JSONSerialization.data(withJSONObject: ["code": code, "code_verifier": verifier]) ) - let (data, response) = try await send(request, socketPath: socketPath) + let (data, response) = try await send(request, socketPath: socketPath, serverPID: serverPID) let body = try? JSONDecoder().decode(JsonValue.self, from: data) guard response.status == 200, let token = body?["token"]?.stringValue, !token.isEmpty else { let detail = body?["error"]?.stringValue ?? "HTTP \(response.status)" @@ -132,11 +158,30 @@ public struct CLIServerClient: Sendable { return token } - private static func send(_ request: MCPSocketHttpRequest, socketPath: String) async throws -> (Data, CLIServerResponse) { + // RFC 7009: the token travels as a form field, and holding it is the credential. + public static func revoke(token: String, socketPath: String, serverPID: Int32) async throws -> CLIRevocation { + let value = token.addingPercentEncoding(withAllowedCharacters: formUnreserved) ?? "" + let request = MCPSocketHttpRequest( + path: revocationPath, + headers: [("Content-Type", "application/x-www-form-urlencoded")], + body: Data("token=\(value)".utf8) + ) + + let (data, response) = try await send(request, socketPath: socketPath, serverPID: serverPID) + guard response.status != 200 else { return .revoked } + if response.status == 503 { return .unavailable } + let body = try? JSONDecoder().decode(JsonValue.self, from: data) + let detail = body?["error_description"]?.stringValue ?? body?["error"]?.stringValue ?? "HTTP \(response.status)" + return .refused(detail) + } + + private static func send(_ request: MCPSocketHttpRequest, socketPath: String, serverPID: Int32) async throws -> (Data, CLIServerResponse) { do { - let response = try await MCPUnixSocketHttpClient.send(request, socketPath: socketPath, idleTimeout: requestTimeout) + let response = try await MCPUnixSocketHttpClient.send(request, socketPath: socketPath, expectedPID: serverPID, idleTimeout: requestTimeout) let data = try await response.collectBody() return (data, CLIServerResponse(status: response.status, headers: response.headers)) + } catch MCPSocketHttpError.peerChanged { + throw CLIFailure.peerChanged } catch MCPSocketHttpError.timedOut { throw CLIFailure(.timedOut, "TablePro did not answer in time.") } catch { diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLISession.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLISession.swift new file mode 100644 index 0000000000..4ae881d768 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLISession.swift @@ -0,0 +1,136 @@ +import Foundation +import TableProMCP + +enum CLISqlSource: Sendable, Equatable { + case argument(String) + case file(String) + // `-f -`: the user asked for standard input, so a terminal is read until end of file. + case standardInput + // No SQL at all: standard input only when something is piped in. + case pipedInput +} + +struct CLISession: Sendable { + let environment: CLIEnvironment + + var console: any CLIConsole { environment.console } + var terminal: CLITerminal { environment.terminal } + var tokenStore: any CLITokenStoring { environment.tokenStore } + + var defaultFormat: CLIOutputFormat { + terminal.standardOutput ? .table : .csv + } + + // Every line on standard error is escaped: server text can carry a stored escape sequence. + func writeError(_ line: String) { + console.err(CLITerminalText.message(line) + "\n") + } + + // Free text such as DDL is shown on a terminal, never run, and passes through untouched to a pipe. + func writeText(_ text: String) { + writeOutput(text.hasSuffix("\n") ? text : text + "\n") + } + + func writeOutput(_ text: String) { + console.out(terminal.standardOutput ? CLITerminalText.message(text) : text) + } + + func write(_ sets: [CLIResultSet], format: CLIOutputFormat) { + var stream = ConsoleStream(session: self) + CLIRowWriter.write(sets, as: format, to: &stream) + } + + func readSql(_ source: CLISqlSource) throws -> String { + let text: String + switch source { + case .argument(let value): + text = value + case .file(let path): + do { + text = try environment.readFile(path) + } catch { + throw CLIFailure.usage("Cannot read \(path): \(error.localizedDescription)") + } + case .standardInput: + text = try environment.readStandardInput() + case .pipedInput: + guard !terminal.standardInput else { + throw CLIFailure.usage("Pass the SQL as an argument, with --file, or on standard input.") + } + text = try environment.readStandardInput() + } + guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { + throw CLIFailure.usage("The SQL is empty.") + } + return text + } + + func connect() async throws -> CLIServerClient { + let server = try await environment.openServer() + if let token = try tokenStore.read() { + return CLIServerClient(socketPath: server.socketPath, serverPID: server.serverPID, token: token) + } + guard terminal.standardError else { + throw CLIFailure( + .refused, + "The tablepro command is not paired with TablePro yet. Run tablepro pair in Terminal." + ) + } + writeError("tablepro: approve the tablepro command in TablePro to pair it.") + let token = try await environment.pair(server) + try tokenStore.save(token) + return CLIServerClient(socketPath: server.socketPath, serverPID: server.serverPID, token: token) + } + + func call(_ client: CLIServerClient, _ tool: String, _ arguments: [String: JsonValue]) async throws -> JsonValue { + do { + return try await client.callTool(tool, arguments: arguments) + } catch CLIServerClientError.tokenRejected { + tokenStore.delete() + throw CLIFailure( + .refused, + "TablePro no longer accepts this command's token: it was revoked or has expired. Run tablepro pair to pair it again." + ) + } + } + + // One lookup for ids and names, so a connection hidden from this command and one that does not + // exist end in the same exit status. + func resolveConnection(_ value: String, client: CLIServerClient) async throws -> String { + let listing = try await call(client, "list_connections", [:]) + let entries = listing["connections"]?.arrayValue ?? [] + if let id = UUID(uuidString: value) { + let match = entries.lazy + .compactMap { $0["id"]?.stringValue } + .first { UUID(uuidString: $0) == id } + if let match { + return match + } + } + let exact = entries.filter { $0["name"]?.stringValue == value } + let matches = exact.isEmpty + ? entries.filter { $0["name"]?.stringValue?.caseInsensitiveCompare(value) == .orderedSame } + : exact + let ids = matches.compactMap { $0["id"]?.stringValue } + guard let first = ids.first else { + let subject = UUID(uuidString: value) == nil ? "named \"\(value)\"" : "with the id \(value)" + throw CLIFailure.usage( + "No connection \(subject) is open to the tablepro command. Run tablepro connections to list them." + ) + } + guard ids.count == 1 else { + throw CLIFailure.usage( + "\(ids.count) connections are named \"\(value)\". Pass one of their ids: \(ids.joined(separator: ", "))." + ) + } + return first + } +} + +private struct ConsoleStream: TextOutputStream { + let session: CLISession + + mutating func write(_ string: String) { + session.writeOutput(string) + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITerminalText.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITerminalText.swift new file mode 100644 index 0000000000..f997dd6195 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITerminalText.swift @@ -0,0 +1,55 @@ +import Darwin +import Foundation + +enum CLITerminalText { + // A Swift process runs in the C locale, where wcwidth answers -1 for anything past ASCII. + nonisolated(unsafe) private static let utf8Locale = newlocale(LC_CTYPE_MASK, "UTF-8", nil) + + static func message(_ text: String) -> String { + guard text.unicodeScalars.contains(where: escapesInMessage) else { return text } + var escaped = "" + escaped.reserveCapacity(text.utf8.count) + for scalar in text.unicodeScalars { + if escapesInMessage(scalar) { + escaped += hex(scalar) + } else { + escaped.unicodeScalars.append(scalar) + } + } + return escaped + } + + static func isControl(_ scalar: Unicode.Scalar) -> Bool { + scalar.value < 0x20 || (0x7F...0x9F).contains(scalar.value) + } + + static func hex(_ scalar: Unicode.Scalar) -> String { + String(format: "\\x%02X", scalar.value) + } + + static func width(of text: String) -> Int { + guard !text.utf8.allSatisfy({ $0 < 0x80 }) else { return text.utf8.count } + let previous = uselocale(utf8Locale) + defer { uselocale(previous) } + return text.reduce(0) { $0 + width(of: $1) } + } + + // Terminals draw a grapheme cluster as one glyph, so a joined emoji or a base with combining marks + // takes the width of its widest scalar, not the sum. wcwidth calls VS16 sequences and flags + // narrow, but terminals draw them two cells wide. + private static func width(of character: Character) -> Int { + let scalars = character.unicodeScalars + if scalars.contains(where: { $0.value == 0xFE0F }) { + return 2 + } + if let first = scalars.first, (0x1F1E6...0x1F1FF).contains(first.value) { + return 2 + } + let widest = scalars.map { Int(wcwidth(wchar_t(bitPattern: $0.value))) }.max() ?? 0 + return widest < 0 ? 1 : widest + } + + private static func escapesInMessage(_ scalar: Unicode.Scalar) -> Bool { + scalar != "\n" && scalar != "\t" && isControl(scalar) + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLITokenStore.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITokenStore.swift similarity index 99% rename from apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLITokenStore.swift rename to apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITokenStore.swift index caecd760a2..8172cb6fe4 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLITokenStore.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CLITokenStore.swift @@ -1,5 +1,6 @@ import Foundation import Security +import TableProMCP public protocol CLITokenStoring: Sendable { func read() throws -> String? diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CompletionCommand.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CompletionCommand.swift new file mode 100644 index 0000000000..b4c860a335 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/CompletionCommand.swift @@ -0,0 +1,22 @@ +import ArgumentParser +import Foundation + +struct CompletionCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "completion", + abstract: "Print a shell completion script.", + discussion: """ + zsh: tablepro completion zsh > "${fpath[1]}/_tablepro" + bash: tablepro completion bash > /usr/local/etc/bash_completion.d/tablepro + fish: tablepro completion fish > ~/.config/fish/completions/tablepro.fish + """ + ) + + @Argument(help: ArgumentHelp("bash, zsh or fish.", valueName: "shell")) + var shell: CLIShell + + func run(in session: CLISession) async throws { + let script = TableProCommand.completionScript(for: shell.completionShell) + session.console.out(script.hasSuffix("\n") ? script : script + "\n") + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/ExportCommand.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/ExportCommand.swift new file mode 100644 index 0000000000..1d6020e0b7 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/ExportCommand.swift @@ -0,0 +1,67 @@ +import ArgumentParser +import Foundation +import TableProMCP + +struct ExportCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "export", + abstract: "Export tables as CSV, JSON or SQL INSERT statements.", + discussion: """ + Writes to standard output; redirect it to save a file: + tablepro export -c Staging --format sql users > users.sql + + Each table stops at Default row limit in Settings > MCP unless --limit asks for more, up to \ + Maximum row limit. A cut-short table is named on standard error. + """ + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var database: DatabaseOption + @OptionGroup var schema: SchemaOption + + @Option(help: ArgumentHelp("Export format.", valueName: "format")) + var format: CLIExportFormat + + @OptionGroup var limit: LimitOption + + @Argument(help: ArgumentHelp("Tables to export, in order.", valueName: "table")) + var tables: [String] + + func run(in session: CLISession) async throws { + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + var arguments: [String: JsonValue] = [ + "connection_id": .string(connectionId), + "format": .string(format.rawValue), + "tables": .array(tables.map(JsonValue.string)) + ] + if let name = database.database { arguments["database"] = .string(name) } + if let name = schema.schema { arguments["schema"] = .string(name) } + if let rows = limit.limit { arguments["max_rows"] = .int(rows) } + + let result = try await session.call(client, "export_data", arguments) + let exports = result["exports"]?.arrayValue ?? [] + let text = Self.joined(exports.map { $0["data"]?.stringValue ?? "" }, format: format) + session.writeOutput(format == .json ? CLIRowWriter.jsonDocument(text) : text) + for export in exports where export["is_truncated"]?.boolValue == true { + let label = export["label"]?.stringValue ?? "a table" + let count = export["row_count"]?.intValue ?? 0 + let shown = count == 1 ? "row" : "\(count) rows" + session.writeError("tablepro: \(label): exported the first \(shown). Pass --limit to change it.") + } + } + + // Several JSON exports become one array of arrays, so the output stays one document, as on Linux. + static func joined(_ documents: [String], format: CLIExportFormat) -> String { + guard format == .json, documents.count > 1 else { + return documents + .map { $0.hasSuffix("\n") || $0.isEmpty ? $0 : $0 + "\n" } + .joined(separator: "\n") + } + let arrays = documents.map { document in + let end = document.lastIndex { !$0.isWhitespace }.map(document.index(after:)) ?? document.startIndex + return document[.. Bool) throws -> URL { + let file = URL(fileURLWithPath: target, relativeTo: directory).absoluteURL.standardizedFileURL + if !target.isEmpty, fileExists(file.path) { + return file + } + if let url = URL(string: target), let scheme = url.scheme, scheme.count > 1, target.hasPrefix(scheme + ":") { + return url + } + throw CLIFailure.usage("no command or file named \"\(target)\". Run tablepro --help.") + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/PairingCommands.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/PairingCommands.swift new file mode 100644 index 0000000000..8377fa1b46 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/PairingCommands.swift @@ -0,0 +1,164 @@ +import ArgumentParser +import Foundation +import TableProMCP + +struct StatusCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "status", + abstract: "Show whether this command can reach TablePro.", + discussion: """ + Never starts TablePro and never pairs. Exit status: 0 ready, 1 TablePro is not reachable, \ + 3 the MCP server is off or this command is not paired. + """ + ) + + @OptionGroup var output: FormatOption + + func run(in session: CLISession) async throws { + let environment = session.environment + let probe = await environment.findServer() + let token = try environment.tokenStore.read() + + var paired: Bool? + if case .reachable(let server) = probe, let token { + paired = try await CLIServerClient(socketPath: server.socketPath, serverPID: server.serverPID, token: token).tokenIsAccepted() + } else if token == nil { + paired = false + } + + let report = Self.report(app: environment.app, probe: probe, paired: paired) + session.write([report], format: output.format ?? session.defaultFormat) + if let failure = Self.failure(probe: probe, paired: paired) { + throw failure + } + } + + static func report(app: CLIHostApp, probe: CLIServerProbe, paired: Bool?) -> CLIResultSet { + let running: Bool + let serverOn: JsonValue + switch probe { + case .reachable: + running = true + serverOn = .bool(true) + case .turnedOff: + running = true + serverOn = .bool(false) + case .unreachable, .peerChanged: + running = true + serverOn = .null + case .notRunning: + running = false + serverOn = .bool(false) + } + return CLIResultSet( + columns: ["app", "version", "running", "mcp_server", "paired"], + rows: [[ + app.url.map { .string($0.path) } ?? .null, + app.version.map(JsonValue.string) ?? .null, + .bool(running), + serverOn, + paired.map(JsonValue.bool) ?? .null + ]] + ) + } + + static func failure(probe: CLIServerProbe, paired: Bool?) -> CLIFailure? { + switch probe { + case .peerChanged: + return .peerChanged + case .notRunning: + return CLIFailure(.failure, "TablePro is not running. Any command that needs it starts it.") + case .unreachable(let reason): + return CLIFailure(.failure, "TablePro's MCP server is not reachable: \(reason)") + case .turnedOff: + return CLIFailure(.refused, "TablePro's MCP server is turned off. Turn it on under Settings > MCP.") + case .reachable: + switch paired { + case true?: + return nil + case false?: + return CLIFailure(.refused, "This command is not paired with TablePro. Run tablepro pair.") + case nil: + return CLIFailure(.failure, "TablePro's MCP server did not answer whether this command is paired.") + } + } + } +} + +struct PairCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "pair", + abstract: "Pair this command with TablePro.", + discussion: """ + Starts TablePro if needed and asks you to approve this command there. Pairing again replaces \ + the token this command holds and revokes the old one. + """ + ) + + func run(in session: CLISession) async throws { + let environment = session.environment + let server = try await environment.openServer() + let previous = try? environment.tokenStore.read() + session.writeError("tablepro: approve the tablepro command in TablePro to pair it.") + let token = try await environment.pair(server) + try environment.tokenStore.save(token) + if let previous, previous != token { + // The old token would otherwise stay listed and valid with no one holding it. + let outcome = try? await CLIServerClient.revoke(token: previous, socketPath: server.socketPath, serverPID: server.serverPID) + if outcome != .revoked { + session.writeError("tablepro: The previous token is still active. Revoke it under Settings > MCP.") + } + } + session.console.out("Paired with TablePro.\n") + } +} + +struct UnpairCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "unpair", + abstract: "Remove this command's pairing.", + discussion: """ + Asks TablePro to revoke this command's token, then deletes it from the Keychain. When \ + TablePro cannot be reached, the token is deleted here and stays listed under Settings > MCP \ + until you revoke it there. When TablePro cannot save the revocation, the token stays here so \ + you can try again. + """ + ) + + func run(in session: CLISession) async throws { + let environment = session.environment + guard let token = try environment.tokenStore.read() else { + session.console.out("This command is not paired.\n") + return + } + let probe = await environment.findServer() + if case .peerChanged = probe { + throw CLIFailure.peerChanged + } + guard case .reachable(let server) = probe else { + environment.tokenStore.delete() + throw CLIFailure( + .failure, + "Deleted this command's token, but TablePro's MCP server is not reachable to revoke it. It stays listed under Settings > MCP until you revoke it there." + ) + } + let outcome = try await CLIServerClient.revoke(token: token, socketPath: server.socketPath, serverPID: server.serverPID) + switch outcome { + case .revoked: + environment.tokenStore.delete() + session.console.out("Unpaired. TablePro revoked this command's token.\n") + case .unavailable: + // TablePro still accepts the token, so the copy here stays for another try. + throw CLIFailure( + .failure, + "TablePro did not save the revocation, so this command's token still works and stays paired. Try tablepro unpair again, or revoke it under Settings > MCP." + ) + case .refused(let reason): + environment.tokenStore.delete() + throw CLIFailure( + .failure, + "Deleted this command's token, but TablePro did not revoke it (\(reason)). Revoke it under Settings > MCP." + ) + } + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/QueryCommand.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/QueryCommand.swift new file mode 100644 index 0000000000..417318d559 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/QueryCommand.swift @@ -0,0 +1,98 @@ +import ArgumentParser +import Foundation +import TableProMCP + +struct QueryCommand: ParsableCommand, CLIRunnable { + static let configuration = CommandConfiguration( + commandName: "query", + abstract: "Run one SQL statement and print the rows.", + discussion: """ + The SQL comes from the argument, from --file, or from standard input. Put SQL that starts \ + with a dash after --: + tablepro query -c Staging -- "-- weekly report + SELECT 1" + + Exit status: 0 done, 1 database or connection error, 2 usage error, 3 refused by a setting, \ + 4 timed out. + """ + ) + + @OptionGroup var connection: ConnectionOption + @OptionGroup var database: DatabaseOption + @OptionGroup var schema: SchemaOption + + @Option( + name: [.customShort("f"), .customLong("file")], + help: ArgumentHelp("Read the SQL from a file, or - for standard input.", valueName: "path"), + completion: .file() + ) + var file: String? + + @OptionGroup var output: FormatOption + @OptionGroup var limit: LimitOption + + @Option(help: ArgumentHelp("Statement timeout, 1 to 300.", valueName: "seconds")) + var timeout: Int? + + @Flag(help: "Refuse any statement that is not a read.") + var readOnly = false + + @Argument(help: ArgumentHelp("The statement to run.", valueName: "sql")) + var sql: String? + + func validate() throws { + if sql != nil, file != nil { + throw ValidationError("Pass the SQL as an argument or with --file, not both.") + } + if let timeout, timeout < 1 { + throw ValidationError("--timeout takes a whole number above 0.") + } + } + + var source: CLISqlSource { + if let sql { + return .argument(sql) + } + guard let file else { return .pipedInput } + return file == "-" ? .standardInput : .file(file) + } + + func run(in session: CLISession) async throws { + let text = try session.readSql(source) + let client = try await session.connect() + let connectionId = try await session.resolveConnection(connection.connection, client: client) + + var arguments: [String: JsonValue] = [ + "connection_id": .string(connectionId), + "query": .string(text) + ] + if let name = database.database { arguments["database"] = .string(name) } + if let name = schema.schema { arguments["schema"] = .string(name) } + if let rows = limit.limit { arguments["max_rows"] = .int(rows) } + if let timeout { arguments["timeout_seconds"] = .int(timeout) } + if readOnly { arguments["read_only"] = .bool(true) } + + let result = try await session.call(client, "execute_query", arguments) + Self.write(result, format: output.format ?? session.defaultFormat, in: session) + } + + static func write(_ result: JsonValue, format: CLIOutputFormat, in session: CLISession) { + let entries = result["result_sets"]?.arrayValue ?? [result] + let sets = entries.map(resultSet) + session.write(sets, format: format) + for (entry, set) in zip(entries, sets) where entry["is_truncated"]?.boolValue == true { + let shown = set.rows.count == 1 ? "row" : "\(set.rows.count) rows" + session.writeError("tablepro: showing the first \(shown). Pass --limit to change it.") + } + if sets.allSatisfy({ $0.columns.isEmpty }), let affected = result["rows_affected"]?.intValue { + session.writeError(affected == 1 ? "1 row affected." : "\(affected) rows affected.") + } + } + + static func resultSet(_ value: JsonValue) -> CLIResultSet { + CLIResultSet( + columns: value["columns"]?.arrayValue?.compactMap(\.stringValue) ?? [], + rows: value["rows"]?.arrayValue?.map { $0.arrayValue ?? [] } ?? [] + ) + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProCommandLine/TableProCommand.swift b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/TableProCommand.swift new file mode 100644 index 0000000000..794710578d --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProCommandLine/TableProCommand.swift @@ -0,0 +1,100 @@ +import ArgumentParser +import Foundation + +protocol CLIRunnable { + func run(in session: CLISession) async throws +} + +struct TableProCommand: AsyncParsableCommand { + static let configuration = CommandConfiguration( + commandName: "tablepro", + abstract: "Open databases in TablePro and work with them from the shell.", + discussion: """ + EXAMPLES: + tablepro "postgresql://user@127.0.0.1:5432/app" + tablepro schema.sql + tablepro query -c Staging "SELECT id, email FROM users LIMIT 5" + tablepro query -c Staging -f report.sql --format csv > report.csv + """, + version: CLIHostApp.current.versionLine, + subcommands: [ + OpenCommand.self, + QueryCommand.self, + ConnectionsCommand.self, + DatabasesCommand.self, + SchemasCommand.self, + TablesCommand.self, + DescribeCommand.self, + ExportCommand.self, + StatusCommand.self, + PairCommand.self, + UnpairCommand.self, + CompletionCommand.self + ], + defaultSubcommand: OpenCommand.self + ) + + // Owns the exit status: the library exits 64 on a usage error, and this command has always used 2. + static func run(arguments: [String], environment: CLIEnvironment) async -> CLIExitCode { + let session = CLISession(environment: environment) + let command: ParsableCommand + do { + command = try await asyncParseAsRoot(arguments) + } catch { + return report(error, in: session) + } + + guard let runnable = command as? any CLIRunnable else { + var builtIn = command + do { + try builtIn.run() + return .success + } catch { + return report(error, in: session) + } + } + do { + try await runnable.run(in: session) + return .success + } catch let failure as CLIFailure { + session.writeError("tablepro: \(failure.message)") + return failure.exitCode + } catch { + session.writeError("tablepro: \(error.localizedDescription)") + return .failure + } + } + + // Help, --version and completion scripts arrive here as errors that exit 0. + private static func report(_ error: Error, in session: CLISession) -> CLIExitCode { + let code = exitCode(for: error) + let message = fullMessage(for: error) + if code == .success { + if !message.isEmpty { + session.console.out(message.hasSuffix("\n") ? message : message + "\n") + } + return .success + } + if !message.isEmpty { + session.writeError(message) + } + if code == .validationFailure { + return .usage + } + return CLIExitCode(rawValue: code.rawValue) ?? .failure + } +} + +public enum CommandLineTool { + public static func main(arguments: [String]) async -> Int32 { + let console = CLIStandardConsole() + let code = await TableProCommand.run(arguments: arguments, environment: .live(console: console)) + console.flush() + // A full disk or a closed file must not pass for a complete export. + if let failure = console.outputFailure, code == .success { + console.err("tablepro: could not write the output: \(failure.localizedDescription)\n") + return CLIExitCode.failure.rawValue + } + return code.rawValue + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BackgroundLaunchFlag.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BackgroundLaunchFlag.swift index d50f2a88f3..e9401689f5 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BackgroundLaunchFlag.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BackgroundLaunchFlag.swift @@ -1,6 +1,6 @@ import Foundation -// Set by the bridge when it launches the app for a client; `open -g` passes its environment through. +// Set in the launch environment when a helper starts the app for a client. public enum BackgroundLaunchFlag { public static let variable = "TABLEPRO_BACKGROUND_LAUNCH" public static let value = "1" diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BridgeProxy.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BridgeProxy.swift index 6e8e582df6..b78370b988 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BridgeProxy.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/BridgeProxy.swift @@ -82,6 +82,7 @@ public enum BridgeUpstreamHandshake { logger: any MCPBridgeLogger ) async throws -> BridgeDiscovery { let transport = MCPStreamableHttpClientTransport( + configuration: MCPStreamableHttpClientConfiguration(requestTimeout: probeTimeout), credentialsProvider: MCPCachedUpstreamCredentialsProvider(initial: credentials) { credentials }, errorLogger: logger ) @@ -111,11 +112,13 @@ public enum BridgeUpstreamHandshake { ]) let request = JsonRpcRequest(id: probeRequestId, method: "server/discover", params: params) let body = try JsonRpcCodec.encode(.request(request)) - try await transport.send( - MCPUpstreamFrame(body: body, method: "server/discover", requestId: probeRequestId) - ) - - return try await withThrowingTaskGroup(of: BridgeDiscovery.self) { group in + return try await withThrowingTaskGroup(of: BridgeDiscovery?.self) { group in + group.addTask { + try await transport.sendAndWait( + MCPUpstreamFrame(body: body, method: "server/discover", requestId: probeRequestId) + ) + return nil + } group.addTask { for try await payload in transport.inbound { guard let message = try? JsonRpcCodec.decode(payload) else { continue } @@ -134,11 +137,13 @@ public enum BridgeUpstreamHandshake { try await Task.sleep(for: probeTimeout) throw MCPBridgeStartupError.discoveryFailed("timed out") } - guard let first = try await group.next() else { - throw MCPBridgeStartupError.discoveryFailed("no answer") + while let result = try await group.next() { + if let discovery = result { + group.cancelAll() + return discovery + } } - group.cancelAll() - return first + throw MCPBridgeStartupError.discoveryFailed("no answer") } } diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/Handshake.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/Handshake.swift index 975bfb1aa0..23daf32939 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/Handshake.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Bridge/Handshake.swift @@ -1,16 +1,17 @@ +import AppKit import Foundation import os import TableProProcess public enum MCPHandshakeAcquisitionError: Error, LocalizedError, Equatable { - case launchFailed(status: Int32) + case launchFailed(String) case timeout(lastFailure: String) case serverTurnedOff public var errorDescription: String? { switch self { - case .launchFailed(let status): - return "Failed to launch TablePro (open exit \(status))" + case .launchFailed(let reason): + return "Failed to launch TablePro: \(reason)" case .timeout(let lastFailure): return "Timed out waiting for a trusted TablePro MCP handshake (\(lastFailure))" case .serverTurnedOff: @@ -27,13 +28,13 @@ public struct MCPHandshakeAcquirer: Sendable { let refusalUrl: URL let logger: any MCPBridgeLogger private let pollTimeout: Duration - private let launchHostApp: @Sendable (any MCPBridgeLogger) throws -> Void + private let launchHostApp: @Sendable (any MCPBridgeLogger) async throws -> Void public init( logger: any MCPBridgeLogger, supportDirectory: URL = MCPHandshakeLocation.supportDirectory(), pollTimeout: Duration = .seconds(10), - launchHostApp: @escaping @Sendable (any MCPBridgeLogger) throws -> Void = { try openHostApp(logger: $0) } + launchHostApp: @escaping @Sendable (any MCPBridgeLogger) async throws -> Void = { try await openHostApp(logger: $0) } ) { self.handshakeUrl = MCPHandshakeLocation.file(in: supportDirectory) self.refusalUrl = MCPHandshakeLocation.refusalFile(in: supportDirectory) @@ -47,7 +48,7 @@ public struct MCPHandshakeAcquirer: Sendable { return trusted } let launchRequestedAt = Date() - try launchHostApp(logger) + try await launchHostApp(logger) return try await pollForTrustedHandshake(launchRequestedAt: launchRequestedAt) } @@ -79,23 +80,31 @@ public struct MCPHandshakeAcquirer: Sendable { return refusal.answers(launchRequestedAt: launchRequestedAt) } - // `open` hands its own environment to the app it launches, which is how the flag arrives early - // enough for the app to stay out of the Dock and the app switcher. Nobody asked for this window - // of TablePro: a client did, to answer a question. - public static func openHostApp(logger: any MCPBridgeLogger) throws { - logger.log(.info, "No trusted MCP handshake; launching TablePro via \(Self.launchUrl)") - var environment = ProcessEnvironment.inherited() - environment[BackgroundLaunchFlag.variable] = BackgroundLaunchFlag.value - let process = try ProcessLauncher.process(for: ProcessCommand( - executable: URL(fileURLWithPath: "/usr/bin/open"), - arguments: ["-g", Self.launchUrl], - environment: environment - )) - try process.run() - process.waitUntilExit() - guard process.terminationStatus == 0 else { - throw MCPHandshakeAcquisitionError.launchFailed(status: process.terminationStatus) + // The copy this helper ships in, not whichever copy owns `tablepro://`. Substitution stays on, so a + // running copy elsewhere still answers instead of a second instance starting. The flag has to be in + // the launch environment, read before NSApplicationMain, to keep the app out of the Dock. + public static func openHostApp(logger: any MCPBridgeLogger) async throws { + guard let url = URL(string: launchUrl) else { + throw MCPHandshakeAcquisitionError.launchFailed("the start link is not a URL") } + let configuration = launchConfiguration(environment: ProcessInfo.processInfo.environment) + do { + if let app = MCPHostApp.containingCurrentProcess() { + logger.log(.info, "No trusted MCP handshake; launching \(app.path) via \(Self.launchUrl)") + _ = try await NSWorkspace.shared.open([url], withApplicationAt: app, configuration: configuration) + } else { + logger.log(.info, "No trusted MCP handshake and no enclosing app; launching TablePro via \(Self.launchUrl)") + _ = try await NSWorkspace.shared.open(url, configuration: configuration) + } + } catch { + throw MCPHandshakeAcquisitionError.launchFailed(error.localizedDescription) + } + } + + static func launchConfiguration(environment: [String: String]) -> NSWorkspace.OpenConfiguration { + var environment = environment + environment[BackgroundLaunchFlag.variable] = BackgroundLaunchFlag.value + return ProcessLauncher.openConfiguration(environment: environment, activates: false) } private func pollForTrustedHandshake(launchRequestedAt: Date?) async throws -> MCPHandshakePayload { @@ -120,6 +129,24 @@ public struct MCPHandshakeAcquirer: Sendable { public extension MCPHandshakePayload { func credentials() -> MCPUpstreamCredentials { - MCPUpstreamCredentials(socketPath: socketPath, bearerToken: token) + MCPUpstreamCredentials(socketPath: socketPath, serverPID: pid, bearerToken: token) + } +} + +public enum MCPHostApp { + // proc_pidpath, not Bundle.main: run through a link, Bundle.main is the link's folder. + public static func containingCurrentProcess() -> URL? { + MCPHandshakeTrust.executablePath(forPid: getpid()).flatMap(containing(executablePath:)) + } + + // Only Contents/MacOS: a tool somewhere else inside another app, such as xctest in Xcode, has no host app. + public static func containing(executablePath: String) -> URL? { + let macOS = URL(fileURLWithPath: executablePath).resolvingSymlinksInPath().deletingLastPathComponent() + let contents = macOS.deletingLastPathComponent() + let app = contents.deletingLastPathComponent() + guard macOS.lastPathComponent == "MacOS", contents.lastPathComponent == "Contents", app.pathExtension == "app" else { + return nil + } + return app.standardizedFileURL } } diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIArguments.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIArguments.swift deleted file mode 100644 index e32c7675fa..0000000000 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIArguments.swift +++ /dev/null @@ -1,239 +0,0 @@ -import Foundation - -public enum CLIOutputFormat: String, Sendable, Equatable, CaseIterable { - case csv - case tsv - case json - case jsonl - case table -} - -public enum CLISqlSource: Sendable, Equatable { - case argument(String) - case file(String) - case standardInput -} - -public struct CLIQueryOptions: Sendable, Equatable { - public var connection: String - public var database: String? - public var schema: String? - public var format: CLIOutputFormat? - public var limit: Int? - public var timeoutSeconds: Int? - public var readOnly: Bool - public var sql: CLISqlSource -} - -public enum CLICommand: Sendable, Equatable { - case query(CLIQueryOptions) - case connections(format: CLIOutputFormat?) - case help(String) -} - -public enum CLIArguments { - public static let queryUsage = """ - Usage: tablepro query --connection [options] [SQL] - - Runs one statement through TablePro and prints the rows. The SQL comes from the - argument, from -f , or from standard input. - - Options: - -c, --connection Saved connection to run against (required) - --database Database to run against - --schema Schema to run against - --format csv, tsv, json, jsonl or table - (table on a terminal, csv otherwise) - --limit Rows to return, up to the maximum in Settings > MCP - --timeout Statement timeout, 1 to 300 - --read-only Refuse any statement that is not a read - -f, --file Read the SQL from a file, - for standard input - -h, --help Show this help - - Exit status: 0 success, 1 query error, 2 usage error, 3 refused by policy, 4 timed out. - """ - - public static let connectionsUsage = """ - Usage: tablepro connections [--format ] - - Lists the saved connections the tablepro command can reach: name, id and type. - - Options: - --format csv, tsv, json, jsonl or table (table on a terminal, csv otherwise) - -h, --help Show this help - """ - - public static func parse(_ arguments: [String]) throws -> CLICommand { - guard let subcommand = arguments.first else { - throw CLIFailure.usage("Name a command: query or connections.") - } - let rest = Array(arguments.dropFirst()) - switch subcommand { - case "query": - return try parseQuery(rest) - case "connections": - return try parseConnections(rest) - default: - throw CLIFailure.usage("Unknown command \"\(subcommand)\". Use query or connections.") - } - } - - private static func parseQuery(_ arguments: [String]) throws -> CLICommand { - var reader = OptionReader(arguments) - var connection: String? - var database: String? - var schema: String? - var format: CLIOutputFormat? - var limit: Int? - var timeout: Int? - var readOnly = false - var file: String? - var positionals: [String] = [] - - while let token = reader.next() { - switch token { - case .option(let name, let inline): - switch name { - case "-h", "--help": - return .help(queryUsage) - case "-c", "--connection": - connection = try reader.value(for: name, inline: inline) - case "--database": - database = try reader.value(for: name, inline: inline) - case "--schema": - schema = try reader.value(for: name, inline: inline) - case "--format": - format = try parseFormat(reader.value(for: name, inline: inline)) - case "--limit": - limit = try parsePositive(reader.value(for: name, inline: inline), option: name) - case "--timeout": - timeout = try parsePositive(reader.value(for: name, inline: inline), option: name) - case "--read-only": - guard inline == nil else { throw CLIFailure.usage("--read-only takes no value.") } - readOnly = true - case "-f", "--file": - file = try reader.value(for: name, inline: inline) - default: - throw CLIFailure.usage("Unknown option \(name). Run tablepro query --help.") - } - case .positional(let value): - positionals.append(value) - } - } - - guard let connection, !connection.isEmpty else { - throw CLIFailure.usage("Pass --connection with a connection's name or id.") - } - guard positionals.count <= 1 else { - throw CLIFailure.usage("Pass the SQL as one quoted argument.") - } - - let sql: CLISqlSource - switch (positionals.first, file) { - case (.some, .some): - throw CLIFailure.usage("Pass the SQL as an argument or with --file, not both.") - case (.some(let text), nil): - sql = .argument(text) - case (nil, .some(let path)): - sql = path == "-" ? .standardInput : .file(path) - case (nil, nil): - sql = .standardInput - } - - return .query(CLIQueryOptions( - connection: connection, - database: database, - schema: schema, - format: format, - limit: limit, - timeoutSeconds: timeout, - readOnly: readOnly, - sql: sql - )) - } - - private static func parseConnections(_ arguments: [String]) throws -> CLICommand { - var reader = OptionReader(arguments) - var format: CLIOutputFormat? - while let token = reader.next() { - switch token { - case .option(let name, let inline): - switch name { - case "-h", "--help": - return .help(connectionsUsage) - case "--format": - format = try parseFormat(reader.value(for: name, inline: inline)) - default: - throw CLIFailure.usage("Unknown option \(name). Run tablepro connections --help.") - } - case .positional(let value): - throw CLIFailure.usage("Unexpected argument \"\(value)\".") - } - } - return .connections(format: format) - } - - private static func parseFormat(_ value: String) throws -> CLIOutputFormat { - guard let format = CLIOutputFormat(rawValue: value.lowercased()) else { - let names = CLIOutputFormat.allCases.map(\.rawValue).joined(separator: ", ") - throw CLIFailure.usage("Unknown format \"\(value)\". Use one of \(names).") - } - return format - } - - private static func parsePositive(_ value: String, option: String) throws -> Int { - guard let number = Int(value), number > 0 else { - throw CLIFailure.usage("\(option) takes a whole number above 0.") - } - return number - } -} - -private struct OptionReader { - enum Token { - case option(name: String, inline: String?) - case positional(String) - } - - private let arguments: [String] - private var index = 0 - private var optionsEnded = false - - init(_ arguments: [String]) { - self.arguments = arguments - } - - mutating func next() -> Token? { - guard index < arguments.count else { return nil } - let argument = arguments[index] - index += 1 - // An option name never holds whitespace, so "-- note\nSELECT 1" is SQL. - let name = argument.split(separator: "=", maxSplits: 1).first ?? "" - if optionsEnded || argument == "-" || !argument.hasPrefix("-") || name.contains(where: \.isWhitespace) { - return .positional(argument) - } - if argument == "--" { - optionsEnded = true - return next() - } - if argument.hasPrefix("--"), let equals = argument.firstIndex(of: "=") { - return .option( - name: String(argument[.. String { - if let inline { - return inline - } - guard index < arguments.count else { - throw CLIFailure.usage("\(option) needs a value.") - } - let value = arguments[index] - index += 1 - return value - } -} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIClientIdentity.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIClientIdentity.swift new file mode 100644 index 0000000000..bd6896eb64 --- /dev/null +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIClientIdentity.swift @@ -0,0 +1,8 @@ +import Foundation + +public enum CLIClientIdentity { + // The app files a query under Command Line when a request names this client. + public static let name = "tablepro-cli" + public static let title = "Command Line" + public static let pairingName = "tablepro command" +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIPairing.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIPairing.swift index 1e4f87b34c..3d1f2d74e1 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIPairing.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIPairing.swift @@ -1,6 +1,5 @@ import CryptoKit import Foundation -import Network // The loopback pairing flow in developers/pairing.mdx. public struct CLIPairingRequest: Sendable, Equatable { @@ -78,176 +77,3 @@ public enum CLIPairingCallback: Sendable, Equatable { return value("error") == nil ? .ignored : .denied } } - -public final class CLICallbackListener: @unchecked Sendable { - private static let maximumHeadBytes = 16 * 1_024 - - private let state: String - private let queue = DispatchQueue(label: "com.TablePro.cli.pairing") - private let lock = NSLock() - private var listener: NWListener? - private var connections: [NWConnection] = [] - private var outcome: CLIPairingCallback? - private var waiter: CheckedContinuation? - - public init(state: String) { - self.state = state - } - - public func start() async throws -> UInt16 { - let parameters = NWParameters.tcp - parameters.requiredInterfaceType = .loopback - parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv4(.loopback), port: .any) - let listener = try NWListener(using: parameters) - lock.withLock { self.listener = listener } - - return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - let resumed = ResumeOnce() - listener.stateUpdateHandler = { @Sendable state in - switch state { - case .ready: - guard resumed.claim() else { return } - continuation.resume(returning: listener.port?.rawValue ?? 0) - case .failed(let error): - guard resumed.claim() else { return } - continuation.resume(throwing: error) - default: - break - } - } - listener.newConnectionHandler = { @Sendable [weak self] connection in - self?.accept(connection) - } - listener.start(queue: queue) - } - } - - // Nil when nothing answered in time. - public func waitForCallback(timeout: Duration) async -> CLIPairingCallback? { - let timer = Task { [weak self] in - try? await Task.sleep(for: timeout) - guard !Task.isCancelled else { return } - self?.finish(.ignored) - } - defer { timer.cancel() } - let result = await withCheckedContinuation { (continuation: CheckedContinuation) in - let ready: CLIPairingCallback? = lock.withLock { - if let outcome { return outcome } - waiter = continuation - return nil - } - if let ready { - continuation.resume(returning: ready) - } - } - return result == .ignored ? nil : result - } - - public func stop() { - let (listener, connections) = lock.withLock { () -> (NWListener?, [NWConnection]) in - let taken = (self.listener, self.connections) - self.listener = nil - self.connections = [] - return taken - } - listener?.cancel() - connections.forEach { $0.cancel() } - } - - private func finish(_ result: CLIPairingCallback) { - let waiting: CheckedContinuation? = lock.withLock { - guard outcome == nil else { return nil } - outcome = result - let taken = waiter - waiter = nil - return taken - } - waiting?.resume(returning: result) - } - - private func accept(_ connection: NWConnection) { - lock.withLock { connections.append(connection) } - connection.start(queue: queue) - receive(on: connection, buffer: Data()) - } - - private func receive(on connection: NWConnection, buffer: Data) { - connection.receive(minimumIncompleteLength: 1, maximumLength: 4_096) { @Sendable [weak self] data, _, isComplete, error in - guard let self else { return } - var accumulated = buffer - if let data { - accumulated.append(data) - } - if let head = Self.requestHead(accumulated) { - self.answer(head, on: connection) - return - } - guard error == nil, !isComplete, accumulated.count < Self.maximumHeadBytes else { - connection.cancel() - return - } - self.receive(on: connection, buffer: accumulated) - } - } - - private func answer(_ head: String, on connection: NWConnection) { - let parts = head.split(separator: " ", maxSplits: 2) - let callback: CLIPairingCallback - if parts.count >= 2, parts[0] == "GET" { - callback = CLIPairingCallback.parse(requestTarget: String(parts[1]), state: state) - } else { - callback = .ignored - } - let response = Self.response(for: callback) - connection.send(content: response, completion: .contentProcessed { @Sendable _ in - connection.cancel() - }) - guard callback != .ignored else { return } - finish(callback) - } - - static func requestHead(_ data: Data) -> String? { - guard let end = data.range(of: Data("\r\n".utf8)) else { return nil } - return String(data: data[data.startIndex.. Data { - let status: String - let message: String - switch callback { - case .approved: - status = "200 OK" - message = "TablePro paired the tablepro command. You can close this tab." - case .denied: - status = "200 OK" - message = "Pairing was declined. You can close this tab." - case .ignored: - status = "404 Not Found" - message = "Nothing here." - } - let body = "TablePro

\(message)

\n" - let head = [ - "HTTP/1.1 \(status)", - "Content-Type: text/html; charset=utf-8", - "Content-Length: \(body.utf8.count)", - "Cache-Control: no-store", - "Connection: close", - "", - "" - ].joined(separator: "\r\n") - return Data((head + body).utf8) - } -} - -private final class ResumeOnce: @unchecked Sendable { - private let lock = NSLock() - private var claimed = false - - func claim() -> Bool { - lock.withLock { - guard !claimed else { return false } - claimed = true - return true - } - } -} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRunner.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRunner.swift deleted file mode 100644 index 67c208fdec..0000000000 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRunner.swift +++ /dev/null @@ -1,329 +0,0 @@ -import Darwin -import Foundation -import TableProProcess - -public protocol CLIConsole: AnyObject, Sendable { - func out(_ text: String) - func err(_ text: String) -} - -public struct CLITerminal: Sendable, Equatable { - public let standardInput: Bool - public let standardOutput: Bool - public let standardError: Bool - - public init(standardInput: Bool, standardOutput: Bool, standardError: Bool) { - self.standardInput = standardInput - self.standardOutput = standardOutput - self.standardError = standardError - } - - public static var current: CLITerminal { - CLITerminal( - standardInput: isatty(STDIN_FILENO) == 1, - standardOutput: isatty(STDOUT_FILENO) == 1, - standardError: isatty(STDERR_FILENO) == 1 - ) - } -} - -// `appPath` is the bundle of the process serving MCP, which a pairing link has to reach. -public struct CLIServerEndpoint: Sendable, Equatable { - public let socketPath: String - public let appPath: String? - - public init(socketPath: String, appPath: String?) { - self.socketPath = socketPath - self.appPath = appPath - } -} - -public struct CLIRunner: Sendable { - public typealias ServerOpener = @Sendable () async throws -> CLIServerEndpoint - public typealias Pairer = @Sendable (_ server: CLIServerEndpoint) async throws -> String - public typealias TextReader = @Sendable () throws -> String - public typealias FileReader = @Sendable (_ path: String) throws -> String - - static let pairingTimeout: Duration = .seconds(330) - - private let console: any CLIConsole - private let terminal: CLITerminal - private let tokenStore: any CLITokenStoring - private let openServer: ServerOpener - private let pair: Pairer - private let readStandardInput: TextReader - private let readFile: FileReader - - public init( - console: any CLIConsole, - terminal: CLITerminal, - tokenStore: any CLITokenStoring, - openServer: @escaping ServerOpener, - pair: @escaping Pairer, - readStandardInput: @escaping TextReader, - readFile: @escaping FileReader - ) { - self.console = console - self.terminal = terminal - self.tokenStore = tokenStore - self.openServer = openServer - self.pair = pair - self.readStandardInput = readStandardInput - self.readFile = readFile - } - - public func run(arguments: [String]) async -> CLIExitCode { - do { - switch try CLIArguments.parse(arguments) { - case .help(let text): - console.out(text + "\n") - case .connections(let format): - try await listConnections(format: format ?? defaultFormat) - case .query(let options): - try await query(options) - } - return .success - } catch let failure as CLIFailure { - console.err("tablepro: \(failure.message)\n") - return failure.exitCode - } catch { - console.err("tablepro: \(error.localizedDescription)\n") - return .failure - } - } - - private var defaultFormat: CLIOutputFormat { - terminal.standardOutput ? .table : .csv - } - - private func listConnections(format: CLIOutputFormat) async throws { - let client = try await connect() - let listing = try await call(client, "list_connections", [:]) - var stream = ConsoleStream(console: console) - CLIRowWriter.write(Self.connectionsSet(listing), as: format, to: &stream) - } - - private func query(_ options: CLIQueryOptions) async throws { - let sql = try readSql(options.sql) - let client = try await connect() - let connectionId = try await resolveConnection(options.connection, client: client) - - var arguments: [String: JsonValue] = [ - "connection_id": .string(connectionId), - "query": .string(sql) - ] - if let database = options.database { arguments["database"] = .string(database) } - if let schema = options.schema { arguments["schema"] = .string(schema) } - if let limit = options.limit { arguments["max_rows"] = .int(limit) } - if let timeout = options.timeoutSeconds { arguments["timeout_seconds"] = .int(timeout) } - if options.readOnly { arguments["read_only"] = .bool(true) } - - let result = try await call(client, "execute_query", arguments) - writeResult(result, format: options.format ?? defaultFormat) - } - - private func writeResult(_ result: JsonValue, format: CLIOutputFormat) { - let entries = result["result_sets"]?.arrayValue ?? [result] - let sets = entries.map(Self.resultSet) - var stream = ConsoleStream(console: console) - CLIRowWriter.write(sets, as: format, to: &stream) - for (entry, set) in zip(entries, sets) where entry["is_truncated"]?.boolValue == true { - let shown = set.rows.count == 1 ? "row" : "\(set.rows.count) rows" - console.err("tablepro: showing the first \(shown). Pass --limit to change it.\n") - } - if sets.allSatisfy({ $0.columns.isEmpty }), let affected = result["rows_affected"]?.intValue { - console.err(affected == 1 ? "1 row affected.\n" : "\(affected) rows affected.\n") - } - } - - private func readSql(_ source: CLISqlSource) throws -> String { - let text: String - switch source { - case .argument(let value): - text = value - case .file(let path): - do { - text = try readFile(path) - } catch { - throw CLIFailure.usage("Cannot read \(path): \(error.localizedDescription)") - } - case .standardInput: - guard !terminal.standardInput else { - throw CLIFailure.usage("Pass the SQL as an argument, with --file, or on standard input.") - } - text = try readStandardInput() - } - guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { - throw CLIFailure.usage("The SQL is empty.") - } - return text - } - - private func connect() async throws -> CLIServerClient { - let server = try await openServer() - if let token = try tokenStore.read() { - return CLIServerClient(socketPath: server.socketPath, token: token) - } - guard terminal.standardError else { - throw CLIFailure( - .refused, - "The tablepro command is not paired with TablePro yet. Run it once in Terminal to pair it." - ) - } - console.err("tablepro: approve the tablepro command in TablePro to pair it.\n") - let token = try await pair(server) - try tokenStore.save(token) - return CLIServerClient(socketPath: server.socketPath, token: token) - } - - private func call(_ client: CLIServerClient, _ tool: String, _ arguments: [String: JsonValue]) async throws -> JsonValue { - do { - return try await client.callTool(tool, arguments: arguments) - } catch CLIServerClientError.tokenRejected { - tokenStore.delete() - throw CLIFailure( - .refused, - "TablePro no longer accepts this command's token: it was revoked or has expired. Run the command again to pair it." - ) - } - } - - private func resolveConnection(_ value: String, client: CLIServerClient) async throws -> String { - if let id = UUID(uuidString: value) { - return id.uuidString - } - let listing = try await call(client, "list_connections", [:]) - let entries = listing["connections"]?.arrayValue ?? [] - let exact = entries.filter { $0["name"]?.stringValue == value } - let matches = exact.isEmpty - ? entries.filter { $0["name"]?.stringValue?.caseInsensitiveCompare(value) == .orderedSame } - : exact - let ids = matches.compactMap { $0["id"]?.stringValue } - guard let first = ids.first else { - throw CLIFailure.usage( - "No connection named \"\(value)\" is open to the tablepro command. Run tablepro connections to list them." - ) - } - guard ids.count == 1 else { - throw CLIFailure.usage( - "\(ids.count) connections are named \"\(value)\". Pass one of their ids: \(ids.joined(separator: ", "))." - ) - } - return first - } - - static func resultSet(_ value: JsonValue) -> CLIResultSet { - CLIResultSet( - columns: value["columns"]?.arrayValue?.compactMap(\.stringValue) ?? [], - rows: value["rows"]?.arrayValue?.map { $0.arrayValue ?? [] } ?? [] - ) - } - - static func connectionsSet(_ listing: JsonValue) -> CLIResultSet { - let rows = (listing["connections"]?.arrayValue ?? []).map { entry in - [entry["name"] ?? .null, entry["id"] ?? .null, entry["type"] ?? .null] - } - return CLIResultSet(columns: ["name", "id", "type"], rows: rows) - } -} - -private struct ConsoleStream: TextOutputStream { - let console: any CLIConsole - - mutating func write(_ string: String) { - console.out(string) - } -} - -public extension CLIRunner { - static func live(console: any CLIConsole) -> CLIRunner { - CLIRunner( - console: console, - terminal: .current, - tokenStore: CLITokenStores.current(), - openServer: { try await openLiveServer() }, - pair: { server in try await pairLive(server: server) }, - readStandardInput: { - let data = FileHandle.standardInput.readDataToEndOfFile() - guard let text = String(data: data, encoding: .utf8) else { - throw CLIFailure.usage("Standard input is not UTF-8 text.") - } - return text - }, - readFile: { path in try String(contentsOfFile: path, encoding: .utf8) } - ) - } - - // The server must prove it wrote the handshake before any token goes to its socket. - private static func openLiveServer() async throws -> CLIServerEndpoint { - let logger = MCPOSBridgeLogger(category: "CLI") - let handshake: MCPHandshakePayload - do { - handshake = try await MCPHandshakeAcquirer(logger: logger).acquire() - } catch MCPHandshakeAcquisitionError.serverTurnedOff { - throw CLIFailure( - .refused, - "TablePro's MCP server is turned off, and the tablepro command reaches TablePro through it. Turn it on under Settings > MCP." - ) - } catch { - throw CLIFailure(.failure, "TablePro did not start its MCP server: \(error.localizedDescription)") - } - do { - _ = try await BridgeUpstreamHandshake.verifiedDiscovery( - handshake: handshake, - credentials: handshake.credentials(), - logger: logger - ) - } catch { - throw CLIFailure(.failure, "The local MCP server did not prove it is TablePro: \(error.localizedDescription)") - } - let appPath = MCPHandshakeTrust.executablePath(forPid: handshake.pid) - .flatMap(MCPHandshakeTrust.appBundleRoot(forExecutable:)) - return CLIServerEndpoint(socketPath: handshake.socketPath, appPath: appPath) - } - - private static func pairLive(server: CLIServerEndpoint) async throws -> String { - let request = CLIPairingRequest.random() - let listener = CLICallbackListener(state: request.state) - defer { listener.stop() } - - let callbackPort: UInt16 - do { - callbackPort = try await listener.start() - } catch { - throw CLIFailure(.failure, "Could not listen for the pairing answer: \(error.localizedDescription)") - } - guard let link = request.link(callbackPort: callbackPort) else { - throw CLIFailure(.failure, "Could not build the pairing link.") - } - try openLink(link, in: server.appPath) - - switch await listener.waitForCallback(timeout: pairingTimeout) { - case .approved(let code): - return try await CLIServerClient.exchange(code: code, verifier: request.verifier, socketPath: server.socketPath) - case .denied: - throw CLIFailure(.refused, "Pairing was declined in TablePro.") - case .ignored, nil: - throw CLIFailure(.refused, "Pairing was not approved in time.") - } - } - - // The code is only good on the instance serving MCP, not on whichever copy owns `tablepro://`. - private static func openLink(_ link: URL, in appPath: String?) throws { - let process: Process - do { - process = try ProcessLauncher.process(for: ProcessCommand( - executable: URL(fileURLWithPath: "/usr/bin/open"), - arguments: (appPath.map { ["-a", $0] } ?? []) + [link.absoluteString], - environment: ProcessEnvironment.inherited() - )) - try process.run() - } catch { - throw CLIFailure(.failure, "Could not open TablePro to pair: \(error.localizedDescription)") - } - process.waitUntilExit() - guard process.terminationStatus == 0 else { - throw CLIFailure(.failure, "Could not open TablePro to pair (open exited \(process.terminationStatus)).") - } - } -} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Lifecycle/MCPHandshakeFile.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Lifecycle/MCPHandshakeFile.swift index 70589110de..bc736e293e 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Lifecycle/MCPHandshakeFile.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Lifecycle/MCPHandshakeFile.swift @@ -279,6 +279,17 @@ public enum MCPHandshakeTrust { } } + static func verifyConnectedPeer(descriptor: Int32, expectedPID: Int32) throws { + var peerPID: Int32 = 0 + var size = socklen_t(MemoryLayout.size) + guard expectedPID > 0, + getsockopt(descriptor, SOL_LOCAL, LOCAL_PEERPID, &peerPID, &size) == 0, + size == MemoryLayout.size, peerPID == expectedPID else { + throw MCPSocketHttpError.peerChanged + } + try verifyHostExecutable(pid: peerPID) + } + private static func verifyHostExecutable(pid: Int32) throws { guard let hostPath = executablePath(forPid: pid) else { throw MCPHandshakeTrustFailure.hostIsNotTablePro(path: "unknown") diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPStreamableHttpClientTransport.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPStreamableHttpClientTransport.swift index a629ed1814..5847ef98fe 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPStreamableHttpClientTransport.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPStreamableHttpClientTransport.swift @@ -65,6 +65,11 @@ public actor MCPStreamableHttpClientTransport { } } + func sendAndWait(_ frame: MCPUpstreamFrame) async throws { + guard !isClosed else { throw MCPTransportError.closed } + try await perform(frame, allowCredentialRefresh: false) + } + public func close() async { if isClosed { return @@ -86,18 +91,16 @@ public actor MCPStreamableHttpClientTransport { private func dispatch(_ frame: MCPUpstreamFrame) async { do { try await perform(frame, allowCredentialRefresh: true) - } catch let error as MCPTransportError { - await recover(from: error, frame: frame) } catch { - emitTransportFailure(error, frame: frame) + await recover(from: error, frame: frame) } } // The app may have restarted: resend only after the provider re-acquires and verifies the listener. - private func recover(from failure: MCPTransportError, frame: MCPUpstreamFrame) async { + private func recover(from failure: Error, frame: MCPUpstreamFrame) async { guard !isClosed, !Task.isCancelled else { return } switch failure { - case .authentication, .unreachable: + case MCPTransportError.authentication, MCPTransportError.unreachable, MCPSocketHttpError.peerChanged: break default: emitTransportFailure(failure, frame: frame) @@ -129,6 +132,7 @@ public actor MCPStreamableHttpClientTransport { response = try await MCPUnixSocketHttpClient.send( request, socketPath: credentials.socketPath, + expectedPID: credentials.serverPID, idleTimeout: configuration.requestTimeout ) } catch { @@ -306,6 +310,8 @@ public actor MCPStreamableHttpClientTransport { switch failure { case .connectFailed(let detail), .invalidSocketPath(let detail): return MCPTransportError.unreachable(detail: detail) + case .peerChanged: + return failure case .timedOut: return MCPTransportError.timeout case .invalidHeader(let name): diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUnixSocketHttpClient.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUnixSocketHttpClient.swift index 6d62db49a2..fee958f8b2 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUnixSocketHttpClient.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUnixSocketHttpClient.swift @@ -1,5 +1,5 @@ +import Darwin import Foundation -import Network public struct MCPSocketHttpRequest: Sendable { public let method: String @@ -34,6 +34,7 @@ public enum MCPSocketHttpError: Error, Equatable, Sendable { case connectFailed(String) case invalidSocketPath(String) case invalidHeader(String) + case peerChanged case timedOut case connectionClosed(String) case malformedResponse(String) @@ -47,6 +48,7 @@ public enum MCPUnixSocketHttpClient { public static func send( _ request: MCPSocketHttpRequest, socketPath: String, + expectedPID: Int32, idleTimeout: Duration ) async throws -> MCPSocketHttpResponse { guard socketPath.hasPrefix("/"), MCPSocketLocation.fits(socketPath) else { @@ -54,6 +56,7 @@ public enum MCPUnixSocketHttpClient { } let exchange = MCPUnixSocketHttpExchange( socketPath: socketPath, + expectedPID: expectedPID, request: try encode(request), idleTimeout: idleTimeout ) @@ -91,7 +94,11 @@ public enum MCPUnixSocketHttpClient { private final class MCPUnixSocketHttpExchange: @unchecked Sendable { private static let receiveChunkSize = 65_536 - private let connection: NWConnection + private let socketPath: String + private let expectedPID: Int32 + private var connection: MCPUnixSocketConnection? + private var connecting: DispatchSourceWrite? + private var connected = false private let queue = DispatchQueue(label: "com.TablePro.MCP.UnixSocketHttpClient") private let request: Data private let idleTimeout: Duration @@ -102,15 +109,12 @@ private final class MCPUnixSocketHttpExchange: @unchecked Sendable { private var reader = HttpResponseReader() private var headContinuation: CheckedContinuation? private var headResolved = false - private var isReady = false private var finished = false private var idleTimer: DispatchSourceTimer? - init(socketPath: String, request: Data, idleTimeout: Duration) { - connection = NWConnection( - to: .unix(path: socketPath), - using: NWParameters(tls: nil, tcp: NWProtocolTCP.Options()) - ) + init(socketPath: String, expectedPID: Int32, request: Data, idleTimeout: Duration) { + self.socketPath = socketPath + self.expectedPID = expectedPID self.request = request self.idleTimeout = idleTimeout let (stream, continuation) = AsyncThrowingStream.makeStream() @@ -142,60 +146,117 @@ private final class MCPUnixSocketHttpExchange: @unchecked Sendable { resolveHead(.failure(CancellationError())) return } - connection.stateUpdateHandler = { [self] state in - switch state { - case .ready: - handleReady() - case .waiting(let error), .failed(let error): - // After connect, the peer's close fails the state before its last bytes are read. - guard !isReady else { return } - fail(MCPSocketHttpError.connectFailed(error.localizedDescription)) - case .cancelled: - connection.stateUpdateHandler = nil - fail(MCPSocketHttpError.connectionClosed("cancelled")) - default: - break + armIdleTimer() + do { + try connectSocket() + } catch { + fail(error) + } + } + + private func connectSocket() throws { + let descriptor = socket(AF_UNIX, SOCK_STREAM, 0) + guard descriptor >= 0 else { throw connectFailure() } + var transferred = false + defer { if !transferred { close(descriptor) } } + var noSigPipe: Int32 = 1 + guard fcntl(descriptor, F_SETFD, FD_CLOEXEC) == 0, + fcntl(descriptor, F_SETFL, O_NONBLOCK) == 0, + setsockopt(descriptor, SOL_SOCKET, SO_NOSIGPIPE, &noSigPipe, socklen_t(MemoryLayout.size)) == 0 else { + throw connectFailure() + } + var address = sockaddr_un() + address.sun_family = sa_family_t(AF_UNIX) + address.sun_len = UInt8(MemoryLayout.size) + withUnsafeMutableBytes(of: &address.sun_path) { raw in + socketPath.utf8CString.withUnsafeBytes { raw.copyMemory(from: $0) } + } + let result = withUnsafePointer(to: &address) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { + Darwin.connect(descriptor, $0, socklen_t(MemoryLayout.size)) } } - armIdleTimer() - connection.start(queue: queue) + if result == 0 { + transferred = true + acceptConnected(descriptor) + return + } + guard errno == EINPROGRESS else { throw connectFailure() } + let source = DispatchSource.makeWriteSource(fileDescriptor: descriptor, queue: queue) + source.setEventHandler { @Sendable [self] in + var code: Int32 = 0 + var size = socklen_t(MemoryLayout.size) + guard getsockopt(descriptor, SOL_SOCKET, SO_ERROR, &code, &size) == 0, code == 0 else { + fail(MCPSocketHttpError.connectFailed(String(cString: strerror(code == 0 ? errno : code)))) + return + } + connected = true + connecting?.cancel() + connecting = nil + } + source.setCancelHandler { @Sendable [self] in + if connected && !finished { + acceptConnected(descriptor) + } else { + close(descriptor) + } + } + connecting = source + transferred = true + source.resume() } - private func handleReady() { - guard !isReady, !finished else { return } - isReady = true + private func acceptConnected(_ descriptor: Int32) { + do { + try MCPHandshakeTrust.verifyConnectedPeer(descriptor: descriptor, expectedPID: expectedPID) + } catch { + close(descriptor) + fail(MCPSocketHttpError.peerChanged) + return + } + let stream = MCPUnixSocketConnection(descriptor: descriptor) + connection = stream + stream.start { _ in } // A server may answer and close before reading the request (a 503 at capacity). - connection.send(content: request, completion: .contentProcessed { _ in }) + stream.send(request) { _ in } receiveNext() } + private func connectFailure() -> MCPSocketHttpError { + .connectFailed(String(cString: strerror(errno))) + } + private func receiveNext() { - connection.receive(minimumIncompleteLength: 1, maximumLength: Self.receiveChunkSize) { [self] content, _, isComplete, error in - guard !finished else { return } - if let content, !content.isEmpty { - armIdleTimer() - do { - try consume(reader.feed(content)) - } catch { - fail(MCPSocketHttpError.malformedResponse(String(describing: error))) - return - } - } - guard !finished else { return } - if let error { - fail(MCPSocketHttpError.connectionClosed(error.localizedDescription)) + connection?.receive(maximumLength: Self.receiveChunkSize) { [self] content, isComplete, error in + queue.async { self.received(content, isComplete: isComplete, error: error) } + } + } + + private func received(_ content: Data?, isComplete: Bool, error: String?) { + guard !finished else { return } + if let content, !content.isEmpty { + armIdleTimer() + do { + consume(try reader.feed(content)) + } catch { + fail(MCPSocketHttpError.malformedResponse(String(describing: error))) return } - if isComplete { - do { - try consume(reader.finish()) - } catch { - fail(MCPSocketHttpError.connectionClosed(String(describing: error))) - } - return + } + guard !finished else { return } + if let error { + fail(MCPSocketHttpError.connectionClosed(error)) + return + } + if isComplete { + do { + consume(try reader.finish()) + } catch { + fail(MCPSocketHttpError.connectionClosed(String(describing: error))) } - receiveNext() + return } + receiveNext() } private func consume(_ events: [HttpResponseEvent]) { @@ -211,7 +272,8 @@ private final class MCPUnixSocketHttpExchange: @unchecked Sendable { finished = true stopIdleTimer() bodyContinuation.finish() - connection.cancel() + connection?.cancel() + connection = nil } } } @@ -223,7 +285,10 @@ private final class MCPUnixSocketHttpExchange: @unchecked Sendable { stopIdleTimer() resolveHead(.failure(error)) bodyContinuation.finish(throwing: error) - connection.cancel() + connecting?.cancel() + connecting = nil + connection?.cancel() + connection = nil } private func resolveHead(_ result: Result) { diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUpstreamCredentials.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUpstreamCredentials.swift index 09df3e3d65..f9466fe9b6 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUpstreamCredentials.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Transport/MCPUpstreamCredentials.swift @@ -2,10 +2,12 @@ import Foundation public struct MCPUpstreamCredentials: Sendable, Equatable { public let socketPath: String + public let serverPID: Int32 public let bearerToken: String - public init(socketPath: String, bearerToken: String) { + public init(socketPath: String, serverPID: Int32, bearerToken: String) { self.socketPath = socketPath + self.serverPID = serverPID self.bearerToken = bearerToken } } diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLICallbackListenerTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLICallbackListenerTests.swift new file mode 100644 index 0000000000..66bbae8c8b --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLICallbackListenerTests.swift @@ -0,0 +1,34 @@ +import Foundation +import TableProCommandLine +import TableProMCP +import Testing + +struct CLICallbackListenerTests { + @Test("The listener answers stray requests and hands back the code the browser delivers") + func listenerRoundTrip() async throws { + let listener = CLICallbackListener(state: "s1") + defer { listener.stop() } + let port = try await listener.start() + #expect(port > 0) + + let session = URLSession(configuration: .ephemeral) + let stray = try #require(URL(string: "http://127.0.0.1:\(port)/favicon.ico")) + let (_, strayResponse) = try await session.data(from: stray) + #expect((strayResponse as? HTTPURLResponse)?.statusCode == 404) + + let callback = try #require(URL(string: "http://127.0.0.1:\(port)\(CLIPairingRequest.callbackPath)?code=c-1&state=s1")) + let (body, response) = try await session.data(from: callback) + #expect((response as? HTTPURLResponse)?.statusCode == 200) + #expect(String(data: body, encoding: .utf8)?.contains("You can close this tab") == true) + + #expect(await listener.waitForCallback(timeout: .seconds(5)) == .approved(code: "c-1")) + } + + @Test("A listener nobody calls gives up") + func listenerTimeout() async throws { + let listener = CLICallbackListener(state: "s1") + defer { listener.stop() } + _ = try await listener.start() + #expect(await listener.waitForCallback(timeout: .milliseconds(50)) == nil) + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIConnectionLookupTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIConnectionLookupTests.swift new file mode 100644 index 0000000000..81b8685bcd --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIConnectionLookupTests.swift @@ -0,0 +1,80 @@ +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +struct CLIConnectionLookupTests { + private func sentConnection(_ rig: Rig) async throws -> String? { + let request = try #require(await rig.requests(named: "execute_query").first) + return try arguments(of: request)["connection_id"]?.stringValue + } + + @Test("An id, in any case, resolves through list_connections") + func byId() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], []))]) + #expect(await rig.run(["query", "-c", stagingId.lowercased(), "SELECT 1"]) == .success) + #expect(await rig.server.requests.map { $0.header("Mcp-Name") } == ["list_connections", "execute_query"]) + #expect(try await sentConnection(rig) == stagingId) + } + } + + @Test("An exact name wins over one that differs only in case") + func exactNameFirst() async throws { + try await withRig { rig in + let upper = UUID().uuidString + await rig.route([ + "list_connections": try listing([(stagingId, "staging"), (upper, "Staging")]), + "execute_query": try success(rows(["n"], [])) + ]) + #expect(await rig.run(["query", "-c", "Staging", "SELECT 1"]) == .success) + #expect(try await sentConnection(rig) == upper) + } + } + + @Test("A name matches ignoring case when nothing matches exactly") + func caseInsensitiveName() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], []))]) + #expect(await rig.run(["query", "-c", "STAGING", "SELECT 1"]) == .success) + #expect(try await sentConnection(rig) == stagingId) + } + } + + @Test("A connection the listing leaves out exits 2 by name and by id") + func hiddenOrUnknown() async throws { + try await withRig { rig in + await rig.route(["list_connections": try listing([])]) + #expect(await rig.run(["query", "-c", "Staging", "SELECT 1"]) == .usage) + #expect(rig.console.errors.contains("No connection named \"Staging\"")) + #expect(await rig.run(["query", "-c", stagingId, "SELECT 1"]) == .usage) + #expect(rig.console.errors.contains("with the id \(stagingId)")) + #expect(await rig.requests(named: "execute_query").isEmpty) + } + } + + @Test("Two connections with the same name are a usage error that lists their ids") + func ambiguousName() async throws { + try await withRig { rig in + await rig.route(["list_connections": try listing([("A", "Dev"), ("B", "Dev")])]) + #expect(await rig.run(["query", "-c", "Dev", "SELECT 1"]) == .usage) + #expect(rig.console.errors.contains("A, B")) + #expect(await rig.server.requests.count == 1) + } + } + + @Test("The browsing commands resolve the connection the same way") + func browsingCommands() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "list_databases": try success(.object(["databases": .array([.string("app"), .string("postgres")])])) + ]) + #expect(await rig.run(["databases", "-c", "staging", "--format", "csv"]) == .success) + #expect(rig.console.output == "name\napp\npostgres\n") + let request = try #require(await rig.requests(named: "list_databases").first) + #expect(try arguments(of: request)["connection_id"]?.stringValue == stagingId) + } + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIExportTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIExportTests.swift new file mode 100644 index 0000000000..dc19fc8b9c --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIExportTests.swift @@ -0,0 +1,107 @@ +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +private func exported(_ format: String, _ documents: [(label: String, data: String)]) throws -> MockHttpResponse { + try success(.object([ + "format": .string(format), + "rows_exported": .int(documents.count), + "is_truncated": .bool(false), + "exports": .array(documents.map { document in + .object([ + "label": .string(document.label), + "row_count": .int(1), + "is_truncated": .bool(false), + "data": .string(document.data) + ]) + }) + ])) +} + +struct CLIExportTests { + @Test("JSON exports round-trip controls on terminals and in pipes", arguments: [1, 2]) + func jsonControls(tableCount: Int) async throws { + let text = "\u{7F}\u{85}\u{1B}" + let document = "[{\"value\":\"\u{7F}\u{85}\\u001b\",\"number\":12345678901234567890.1234567890}]" + var outputs: [String] = [] + for terminal in [false, true] { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "export_data": try exported("json", Array(repeating: ("users", document), count: tableCount)) + ]) + let code = await rig.run( + ["export", "-c", "Staging", "--format", "json"] + Array(repeating: "users", count: tableCount), + stdoutIsTerminal: terminal + ) + #expect(code == .success) + let decoded = try JSONSerialization.jsonObject(with: Data(rig.console.output.utf8)) + let rows = try #require(tableCount == 1 ? decoded as? [[String: Any]] : (decoded as? [[[String: Any]]])?.first) + #expect(rows.first?["value"] as? String == text) + #expect(rig.console.output.contains("12345678901234567890.1234567890")) + outputs.append(rig.console.output) + } + } + #expect(outputs[0] == outputs[1]) + } + @Test("Exports escape terminal controls and preserve them in pipes", arguments: [true, false], ["csv", "sql"]) + func exportedControls(stdoutIsTerminal: Bool, format: String) async throws { + try await withRig { rig in + let text = "value\n\u{1B}]52;c;eA==\u{07}\u{1B}[2J\n" + await rig.route([ + "list_connections": try stagingListing(), + "export_data": try exported(format, [("users", text)]) + ]) + let code = await rig.run( + ["export", "-c", "Staging", "--format", format, "users"], + stdoutIsTerminal: stdoutIsTerminal + ) + #expect(code == .success) + let expected = stdoutIsTerminal ? "value\n\\x1B]52;c;eA==\\x07\\x1B[2J\n" : text + #expect(rig.console.output == expected) + } + } + + @Test("Two tables in JSON print one document: an array holding each table's array, in order") + func severalJsonTablesAreOneDocument() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "export_data": try exported("json", [("users", #"[{"id":1}]"#), ("orders", "[]")]) + ]) + #expect(await rig.run(["export", "-c", "Staging", "--format", "json", "users", "orders"]) == .success) + + let document = try JSONSerialization.jsonObject(with: Data(rig.console.output.utf8)) + let tables = try #require(document as? [[[String: Any]]]) + #expect(tables.count == 2) + #expect(tables[0].first?["id"] as? Int == 1) + #expect(tables[1].isEmpty) + } + } + + @Test("One table in JSON prints that table's array as it came") + func oneJsonTableStaysAnArray() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "export_data": try exported("json", [("users", #"[{"id":1}]"#)]) + ]) + #expect(await rig.run(["export", "-c", "Staging", "--format", "json", "users"]) == .success) + #expect(rig.console.output == "[{\"id\":1}]\n") + } + } + + @Test("Several CSV tables stay separate, with a blank line between them") + func severalCsvTablesStayApart() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "export_data": try exported("csv", [("users", "id\n1\n"), ("orders", "id\n2")]) + ]) + #expect(await rig.run(["export", "-c", "Staging", "--format", "csv", "users", "orders"]) == .success) + #expect(rig.console.output == "id\n1\n\nid\n2\n") + } + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIFailureTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIFailureTests.swift similarity index 96% rename from apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIFailureTests.swift rename to apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIFailureTests.swift index cceaeba593..2c42d1c929 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIFailureTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIFailureTests.swift @@ -1,5 +1,6 @@ import Foundation -@testable import TableProMCP +@testable import TableProCommandLine +import TableProMCP import Testing struct CLIFailureTests { diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIHostAppTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIHostAppTests.swift new file mode 100644 index 0000000000..60cfdf8bb0 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIHostAppTests.swift @@ -0,0 +1,53 @@ +import AppKit +import Foundation +@testable import TableProCommandLine +import Testing + +struct CLIHostAppTests { + @Test("Opening targets keeps the shell environment and activates the app") + func launchKeepsInheritedEnvironment() { + let environment = ["TABLEPRO_UI_TEST_SANDBOX": "/tmp/tablepro-cli-test", "PATH": "/usr/bin:/bin"] + + let configuration = CLILive.launchConfiguration(environment: environment) + + #expect(configuration.environment == environment) + #expect(configuration.activates) + } + + private func fakeApp() throws -> URL { + let root = FileManager.default.temporaryDirectory.appendingPathComponent("cli-host-\(UUID().uuidString)", isDirectory: true) + let app = root.appendingPathComponent("Some Copy.app", isDirectory: true) + let macOS = app.appendingPathComponent("Contents/MacOS", isDirectory: true) + try FileManager.default.createDirectory(at: macOS, withIntermediateDirectories: true) + let info: [String: Any] = [ + "CFBundleIdentifier": "com.example.fake-tablepro", + "CFBundleShortVersionString": "9.8.7", + "CFBundleExecutable": "TablePro", + "CFBundlePackageType": "APPL" + ] + let plist = try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0) + try plist.write(to: app.appendingPathComponent("Contents/Info.plist")) + FileManager.default.createFile(atPath: macOS.appendingPathComponent("tablepro-cli").path, contents: Data()) + return app + } + + @Test("A helper in Contents/MacOS belongs to that app and reports its version") + func helperInsideAnApp() throws { + let app = try fakeApp() + defer { try? FileManager.default.removeItem(at: app.deletingLastPathComponent()) } + let host = CLIHostApp(executablePath: app.appendingPathComponent("Contents/MacOS/tablepro-cli").path) + #expect(host.url?.resolvingSymlinksInPath().path == app.resolvingSymlinksInPath().path) + #expect(host.version == "9.8.7") + #expect(host.bundleIdentifier == "com.example.fake-tablepro") + #expect(host.versionLine == "tablepro 9.8.7") + } + + @Test("A tool anywhere else has no app to open, even inside another app") + func helperOutsideContentsMacOS() throws { + let app = try fakeApp() + defer { try? FileManager.default.removeItem(at: app.deletingLastPathComponent()) } + #expect(CLIHostApp(executablePath: app.appendingPathComponent("Contents/Developer/usr/bin/xctest").path).url == nil) + #expect(CLIHostApp(executablePath: "/usr/local/bin/tablepro-cli").url == nil) + #expect(CLIHostApp(executablePath: nil).versionLine == "tablepro unknown") + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIPairingCommandTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIPairingCommandTests.swift new file mode 100644 index 0000000000..bb134434e7 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIPairingCommandTests.swift @@ -0,0 +1,219 @@ +import Darwin +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +private func discovered() throws -> MockHttpResponse { + try rpc(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: .object(["supportedVersions": .array([])])))) +} + +private func rejected() throws -> MockHttpResponse { + let failure = JsonRpcErrorResponse(id: nil, error: JsonRpcError(code: JsonRpcErrorCode.unauthenticated, message: "Unauthenticated")) + return try rpc(.errorResponse(failure), status: 401) +} + +struct CLIStatusTests { + @Test("Running, serving and paired exits 0 and reports each part") + func ready() async throws { + try await withRig { rig in + await rig.route(["server/discover": try discovered()]) + #expect(await rig.run(["status", "--format", "json"]) == .success) + #expect(rig.console.output == """ + [ + {"app":"/Applications/TablePro.app","version":"9.8.7","running":true,"mcp_server":true,"paired":true} + ] + + """) + let request = try #require(await rig.server.requests.first) + #expect(request.header("Authorization") == "Bearer tp_stored") + #expect(rig.log.calls == ["findServer"]) + } + } + + @Test("A token TablePro no longer accepts exits 3 and stays where it is") + func rejectedToken() async throws { + try await withRig { rig in + await rig.route(["server/discover": try rejected()]) + #expect(await rig.run(["status", "--format", "csv"]) == .refused) + #expect(rig.console.output.hasSuffix("true,true,false\n")) + #expect(rig.console.errors.contains("tablepro pair")) + #expect(rig.tokens.current == "tp_stored") + } + } + + @Test("No token exits 3 without asking TablePro") + func unpaired() async throws { + try await withRig(token: nil) { rig in + #expect(await rig.run(["status"]) == .refused) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("A server that is off exits 3, TablePro not running exits 1, and neither launches it", arguments: [ + (CLIServerProbe.turnedOff, CLIExitCode.refused), + (.notRunning, .failure), + (.unreachable("no answer"), .failure) + ]) + func notServing(probe: CLIServerProbe, expected: CLIExitCode) async throws { + try await withRig { rig in + #expect(await rig.run(["status"], probe: probe) == expected) + #expect(rig.log.calls == ["findServer"]) + #expect(await rig.server.requests.isEmpty) + #expect(!rig.console.errors.isEmpty) + } + } +} + +struct CLIUnpairTests { + @Test("unpair revokes the token at TablePro with an RFC 7009 form, then forgets it") + func revokes() async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{}", status: 200)]) + #expect(await rig.run(["unpair"]) == .success) + let request = try #require(await rig.server.requests.first) + #expect(request.method == "POST") + #expect(request.path == "/v1/integrations/revoke") + #expect(request.header("Content-Type") == "application/x-www-form-urlencoded") + #expect(request.header("Authorization") == nil) + #expect(String(data: request.body, encoding: .utf8) == "token=tp_stored") + #expect(rig.tokens.current == nil) + #expect(rig.console.output.contains("revoked")) + #expect(!rig.log.calls.contains("openServer")) + } + } + + @Test("A changed socket peer refuses unpair before sending and keeps the token") + func changedPeerKeepsToken() async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{}", status: 200)]) + let endpoint = CLIServerEndpoint(socketPath: rig.server.socketPath, serverPID: getpid() + 1, appPath: nil) + #expect(await rig.run(["unpair"], probe: .reachable(endpoint)) == .failure) + #expect(rig.tokens.current == "tp_stored") + #expect(rig.console.output.isEmpty) + #expect(rig.console.errors.contains("The local MCP server changed; run the command again.")) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("A changed peer during discovery keeps unpair credentials") + func discoveryPeerChangeKeepsToken() async throws { + try await withRig { rig in + let handshake = MCPHandshakePayload( + socketPath: rig.server.socketPath, + token: "tp_bridge", + pid: getpid() + 1, + instanceId: "test-instance", + protocolVersion: MCPProtocolVersion.latest.rawValue, + expiresAt: Date().addingTimeInterval(60) + ) + let probe = await CLILive.probe(handshake, logger: RecordingBridgeLogger()) + #expect(probe == .peerChanged) + #expect(await rig.run(["unpair"], probe: probe) == .failure) + #expect(rig.tokens.current == "tp_stored") + #expect(rig.console.output.isEmpty) + #expect(rig.console.errors.contains("The local MCP server changed; run the command again.")) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("A refused revocation still forgets the token and points at Settings") + func refused() async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{\"error\":\"unsupported_token_type\"}", status: 400)]) + #expect(await rig.run(["unpair"]) == .failure) + #expect(rig.tokens.current == nil) + #expect(rig.console.errors.contains("unsupported_token_type")) + #expect(rig.console.errors.contains("Settings > MCP")) + } + } + + @Test("A revocation TablePro could not save keeps the token, exits 1 and says to try again") + func notSaved() async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{\"error\":\"temporarily_unavailable\"}", status: 503)]) + #expect(await rig.run(["unpair"]) == .failure) + #expect(rig.tokens.current == "tp_stored") + #expect(rig.console.output.isEmpty) + #expect(rig.console.errors.contains("did not save the revocation")) + #expect(rig.console.errors.contains("tablepro unpair again")) + #expect(rig.console.errors.contains("Settings > MCP")) + } + } + + @Test("With TablePro unreachable the token is forgotten here and the user is told it stays listed") + func unreachable() async throws { + try await withRig { rig in + #expect(await rig.run(["unpair"], probe: .notRunning) == .failure) + #expect(rig.tokens.current == nil) + #expect(await rig.server.requests.isEmpty) + #expect(rig.console.errors.contains("stays listed under Settings > MCP")) + #expect(!rig.log.calls.contains("openServer")) + } + } + + @Test("Unpairing a command that is not paired does nothing and exits 0") + func notPaired() async throws { + try await withRig(token: nil) { rig in + #expect(await rig.run(["unpair"]) == .success) + #expect(rig.console.output == "This command is not paired.\n") + #expect(rig.log.calls.isEmpty) + } + } +} + +struct CLIPairTests { + @Test("pair keeps the new token and revokes the one it replaces") + func replacesToken() async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{}", status: 200)]) + #expect(await rig.run(["pair"]) == .success) + #expect(rig.tokens.current == "tp_paired") + #expect(rig.log.calls == ["openServer", "pair"]) + let request = try #require(await rig.server.requests.first) + #expect(String(data: request.body, encoding: .utf8) == "token=tp_stored") + #expect(!rig.console.errors.contains("previous token")) + #expect(rig.console.output == "Paired with TablePro.\n") + } + } + + @Test("A failed revocation keeps the new pairing and warns about the old token", arguments: [503, 400]) + func revocationFails(status: Int) async throws { + try await withRig { rig in + await rig.route([CLIServerClient.revocationPath: plainJson("{}", status: status)]) + #expect(await rig.run(["pair"]) == .success) + #expect(rig.tokens.current == "tp_paired") + #expect(rig.console.output == "Paired with TablePro.\n") + #expect(rig.console.errors == """ + tablepro: approve the tablepro command in TablePro to pair it. + tablepro: The previous token is still active. Revoke it under Settings > MCP. + + """) + } + } + + @Test("A thrown revocation keeps the new pairing and warns about the old token") + func revocationThrows() async throws { + try await withRig { rig in + await rig.server.stop() + #expect(await rig.run(["pair"]) == .success) + #expect(rig.tokens.current == "tp_paired") + #expect(rig.console.output == "Paired with TablePro.\n") + #expect(rig.console.errors == """ + tablepro: approve the tablepro command in TablePro to pair it. + tablepro: The previous token is still active. Revoke it under Settings > MCP. + + """) + } + } + + @Test("A first pairing revokes nothing") + func firstPairing() async throws { + try await withRig(token: nil) { rig in + #expect(await rig.run(["pair"]) == .success) + #expect(rig.tokens.current == "tp_paired") + #expect(await rig.server.requests.isEmpty) + } + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIParseTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIParseTests.swift new file mode 100644 index 0000000000..5ce4327017 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIParseTests.swift @@ -0,0 +1,119 @@ +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +struct CLIParseTests { + @Test("Help prints the command list on standard output and exits 0", arguments: [ + ["--help"], ["-h"], ["help"] + ]) + func help(arguments: [String]) async throws { + try await withRig { rig in + #expect(await rig.run(arguments) == .success) + #expect(rig.console.output.contains("USAGE: tablepro")) + for command in ["open", "query", "connections", "databases", "schemas", "tables", "describe", "export", "status", "pair", "unpair", "completion"] { + #expect(rig.console.output.contains(command), "\(command)") + } + #expect(rig.console.errors.isEmpty) + #expect(rig.log.calls.isEmpty) + } + } + + @Test("A command's help explains it", arguments: [["help", "query"], ["query", "--help"]]) + func commandHelp(arguments: [String]) async throws { + try await withRig { rig in + #expect(await rig.run(arguments) == .success) + #expect(rig.console.output.contains("Run one SQL statement")) + #expect(rig.console.output.contains("--connection")) + } + } + + @Test("--version prints the version of the app the command ships in") + func version() async throws { + try await withRig { rig in + #expect(await rig.run(["--version"]) == .success) + #expect(rig.console.output == CLIHostApp.current.versionLine + "\n") + } + } + + @Test("No argument opens TablePro and nothing else") + func bareCommandOpensTheApp() async throws { + try await withRig { rig in + #expect(await rig.run([]) == .success) + #expect(rig.log.opened == [[]]) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("A URL or an existing file opens, with or without the open command") + func openTargets() async throws { + try await withRig { rig in + let url = "postgresql://user@127.0.0.1:5432/app" + #expect(await rig.run([url, "schema.sql"], files: ["/work/schema.sql"]) == .success) + #expect(await rig.run(["open", "./status"], files: ["/work/status"]) == .success) + #expect(rig.log.opened == [ + [URL(string: url), URL(fileURLWithPath: "/work/schema.sql")].compactMap { $0 }, + [URL(fileURLWithPath: "/work/status")] + ]) + } + } + + @Test("A word that is neither a command, a file nor a URL exits 2 and opens nothing", arguments: [ + ["qeury"], ["version"], ["open", "nosuch.sql"], ["c:query"] + ]) + func unknownTarget(arguments: [String]) async throws { + try await withRig { rig in + #expect(await rig.run(arguments) == .usage) + #expect(rig.console.errors.contains("no command or file named")) + #expect(rig.log.opened.isEmpty) + } + } + + @Test("Usage mistakes exit 2 before anything reaches TablePro", arguments: [ + ["query", "SELECT 1"], + ["query", "-c", "x", "--format", "xml", "SELECT 1"], + ["query", "-c", "x", "--limit", "0", "SELECT 1"], + ["query", "-c", "x", "--timeout", "soon", "SELECT 1"], + ["query", "-c", "x", "--bogus", "SELECT 1"], + ["query", "-c", "x", "SELECT", "1"], + ["query", "-c", "x", "-f", "q.sql", "SELECT 1"], + ["query", "-c"], + ["query", "-c", "x", "--read-only=yes", "SELECT 1"], + ["connections", "extra"], + ["--bogus"], + ["describe", "-c", "x", "--ddl", "--format", "json", "users"], + ["describe", "-c", "x"], + ["export", "-c", "x", "--format", "csv"], + ["export", "-c", "x", "users"], + ["export", "-c", "x", "--format", "xml", "users"], + ["completion", "powershell"] + ]) + func usageMistakes(arguments: [String]) async throws { + try await withRig { rig in + #expect(await rig.run(arguments) == .usage) + #expect(!rig.console.errors.isEmpty) + #expect(rig.console.output.isEmpty) + #expect(rig.log.calls.isEmpty) + } + } + + @Test("completion prints a script for the shell") + func completion() async throws { + try await withRig { rig in + #expect(await rig.run(["completion", "zsh"]) == .success) + #expect(rig.console.output.hasPrefix("#compdef tablepro")) + #expect(rig.log.calls.isEmpty) + } + } + + @Test("An open target resolves against the working directory, a URL needs a real scheme") + func targetResolution() throws { + let exists: (String) -> Bool = { $0 == "/work/data/app.db" || $0 == "/srv/app.db" } + #expect(try OpenCommand.resolve("data/app.db", in: workDirectory, fileExists: exists) == URL(fileURLWithPath: "/work/data/app.db")) + #expect(try OpenCommand.resolve("/srv/app.db", in: workDirectory, fileExists: exists) == URL(fileURLWithPath: "/srv/app.db")) + #expect(try OpenCommand.resolve("tablepro://integrations/start-mcp", in: workDirectory, fileExists: exists).scheme == "tablepro") + #expect(throws: CLIFailure.self) { try OpenCommand.resolve("", in: workDirectory, fileExists: exists) } + #expect(throws: CLIFailure.self) { try OpenCommand.resolve("x:y", in: workDirectory, fileExists: exists) } + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIQueryTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIQueryTests.swift new file mode 100644 index 0000000000..0579a7c90d --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIQueryTests.swift @@ -0,0 +1,267 @@ +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +struct CLIQueryTests { + @Test("JSON controls round-trip on terminals and in pipes", arguments: ["json", "jsonl"]) + func jsonControls(format: String) async throws { + let text = "\u{7F}\u{85}\u{1B}" + var outputs: [String] = [] + for terminal in [false, true] { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try success(rows([text, "nested"], [[.string(text), .object([text: .array([.string(text)])])]])) + ]) + let code = await rig.run( + ["query", "-c", "Staging", "--format", format, "SELECT value FROM users"], + stdoutIsTerminal: terminal + ) + #expect(code == .success) + let decoded = try JSONSerialization.jsonObject(with: Data(rig.console.output.utf8)) + let row = try #require(format == "json" ? (decoded as? [[String: Any]])?.first : decoded as? [String: Any]) + #expect(row[text] as? String == text) + #expect((row["nested"] as? [String: [String]])?[text] == [text]) + outputs.append(rig.console.output) + } + } + #expect(outputs[0] == outputs[1]) + } + @Test("CSV and TSV escape terminal controls and preserve pipe bytes", arguments: [true, false], ["csv", "tsv"]) + func delimitedControls(stdoutIsTerminal: Bool, format: String) async throws { + try await withRig { rig in + let text = "\u{1B}]52;c;eA==\u{07}\u{1B}[2J" + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try success(rows(["value"], [[.string(text)]])) + ]) + let code = await rig.run( + ["query", "-c", "Staging", "--format", format, "SELECT value FROM users"], + stdoutIsTerminal: stdoutIsTerminal + ) + #expect(code == .success) + let expected = stdoutIsTerminal ? "value\n\\x1B]52;c;eA==\\x07\\x1B[2J\n" : "value\n" + text + "\n" + #expect(rig.console.output == expected) + } + } + + @Test("SELECT 1 AS n prints a CSV header and one row and exits 0") + func csvQuery() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], [[.string("1")]]))]) + let code = await rig.run(["query", "-c", "Staging", "--format", "csv", "SELECT 1 AS n"]) + #expect(code == .success) + #expect(rig.console.output == "n\n1\n") + #expect(rig.console.errors.isEmpty) + + let request = try #require(await rig.requests(named: "execute_query").first) + #expect(request.path == "/mcp") + #expect(request.header("Authorization") == "Bearer tp_stored") + #expect(request.header("Mcp-Method") == "tools/call") + let body = try JSONDecoder().decode(JsonValue.self, from: request.body) + #expect(body["params"]?["_meta"]?[MCPMetaKeys.clientInfo]?["name"]?.stringValue == CLIClientIdentity.name) + let sent = try arguments(of: request) + #expect(sent["connection_id"]?.stringValue == stagingId) + #expect(sent["query"]?.stringValue == "SELECT 1 AS n") + #expect(sent["read_only"] == nil) + } + } + + @Test("--format takes any case") + func formatIgnoresCase() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], [[.string("1")]]))]) + #expect(await rig.run(["query", "-c", "Staging", "--format", "JSON", "SELECT 1 AS n"]) == .success) + #expect(rig.console.output == "[\n {\"n\":\"1\"}\n]\n") + } + } + + @Test("Every option reaches execute_query under its own key") + func everyOption() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], []))]) + let code = await rig.run([ + "query", "--connection=Staging", "--database", "app", "--schema=public", + "--limit", "20", "--timeout", "5", "--read-only", "SELECT 1" + ]) + #expect(code == .success) + let sent = try arguments(of: try #require(await rig.requests(named: "execute_query").first)) + #expect(sent["database"]?.stringValue == "app") + #expect(sent["schema"]?.stringValue == "public") + #expect(sent["max_rows"]?.intValue == 20) + #expect(sent["timeout_seconds"]?.intValue == 5) + #expect(sent["read_only"]?.boolValue == true) + } + } + + @Test("SQL that starts with a dash is refused as an option, and taken after --") + func dashSql() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], []))]) + let sql = "-- weekly report\nSELECT 1" + #expect(await rig.run(["query", "-c", "Staging", sql]) == .usage) + #expect(await rig.server.requests.isEmpty) + + #expect(await rig.run(["query", "-c", "Staging", "--", sql]) == .success) + let sent = try arguments(of: try #require(await rig.requests(named: "execute_query").first)) + #expect(sent["query"]?.stringValue == sql) + } + } + + @Test("--read-only goes to the server, and its refusal exits 3") + func readOnlyRefusal() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try toolError("denied: This request is read only, and the statement is not a read.") + ]) + let code = await rig.run(["query", "-c", "Staging", "--read-only", "DELETE FROM t"]) + #expect(code == .refused) + #expect(rig.console.output.isEmpty) + #expect(rig.console.errors.contains("read only")) + } + } + + @Test("A statement timeout exits 4") + func timeout() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try toolError("timeout: The query did not finish within 5 seconds.") + ]) + #expect(await rig.run(["query", "-c", "Staging", "--timeout", "5", "SELECT pg_sleep(10)"]) == .timedOut) + } + } + + @Test("A revoked token is forgotten and the run exits 3") + func revokedToken() async throws { + try await withRig { rig in + let failure = JsonRpcErrorResponse(id: nil, error: JsonRpcError(code: JsonRpcErrorCode.unauthenticated, message: "Unauthenticated")) + await rig.server.setResponse(try rpc(.errorResponse(failure), status: 401)) + let code = await rig.run(["query", "-c", "Staging", "SELECT 1"]) + #expect(code == .refused) + #expect(rig.tokens.current == nil) + #expect(rig.console.errors.contains("revoked")) + #expect(!rig.log.calls.contains("pair")) + } + } + + @Test("An unpaired command outside a terminal refuses instead of opening a pairing sheet") + func noPairingWithoutTerminal() async throws { + try await withRig(token: nil) { rig in + let code = await rig.run(["query", "-c", "Staging", "SELECT 1"], stderrIsTerminal: false) + #expect(code == .refused) + #expect(!rig.log.calls.contains("pair")) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("An unpaired command in a terminal pairs once and keeps the token") + func pairsOnce() async throws { + try await withRig(token: nil) { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], [[.string("1")]]))]) + #expect(await rig.run(["query", "-c", "Staging", "SELECT 1"]) == .success) + #expect(rig.log.calls.filter { $0 == "pair" }.count == 1) + #expect(rig.tokens.current == "tp_paired") + #expect(await rig.server.requests.first?.header("Authorization") == "Bearer tp_paired") + } + } + + @Test("connections prints name, id and type, and nothing else") + func connections() async throws { + try await withRig { rig in + let entries: JsonValue = .object(["connections": .array([ + .object([ + "id": .string(stagingId), "name": .string("Staging"), "type": .string("PostgreSQL"), + "host": .string("db.internal"), "port": .int(5_432) + ]) + ])]) + await rig.server.setResponse(try success(entries)) + #expect(await rig.run(["connections", "--format", "json"]) == .success) + #expect(rig.console.output == "[\n {\"name\":\"Staging\",\"id\":\"\(stagingId)\",\"type\":\"PostgreSQL\"}\n]\n") + } + } + + @Test("A cut-short result says so on standard error") + func truncatedNote() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try success(rows(["n"], [[.string("1")]], truncated: true)) + ]) + #expect(await rig.run(["query", "-c", "Staging", "--limit", "1", "SELECT n FROM t"]) == .success) + #expect(rig.console.errors == "tablepro: showing the first row. Pass --limit to change it.\n") + } + } + + @Test("Every result set of a script is printed") + func resultSets() async throws { + try await withRig { rig in + var primary = rows(["a"], [[.string("1")]]).objectValue ?? [:] + primary["result_sets"] = .array([rows(["a"], [[.string("1")]]), rows(["b"], [[.string("2")]])]) + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(.object(primary))]) + let code = await rig.run(["query", "-c", "Staging", "--format", "csv", "SELECT 1 AS a\nGO\nSELECT 2 AS b"]) + #expect(code == .success) + #expect(rig.console.output == "a\n1\n\nb\n2\n") + } + } + + @Test("SQL comes from standard input when it is piped") + func pipedInput() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], [[.string("1")]]))]) + #expect(await rig.run(["query", "-c", "Staging"], stdinIsTerminal: false, stdin: "SELECT 1\n") == .success) + let sent = try arguments(of: try #require(await rig.requests(named: "execute_query").first)) + #expect(sent["query"]?.stringValue == "SELECT 1\n") + } + } + + @Test("-f - reads standard input even from a terminal") + func explicitStandardInputOnTerminal() async throws { + try await withRig { rig in + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(rows(["n"], [[.string("1")]]))]) + #expect(await rig.run(["query", "-c", "Staging", "-f", "-"], stdinIsTerminal: true, stdin: "SELECT 2\n") == .success) + let sent = try arguments(of: try #require(await rig.requests(named: "execute_query").first)) + #expect(sent["query"]?.stringValue == "SELECT 2\n") + } + } + + @Test("No SQL from a terminal, an empty statement, or an unreadable file is a usage error") + func missingSql() async throws { + try await withRig { rig in + #expect(await rig.run(["query", "-c", "Staging"]) == .usage) + #expect(await rig.run(["query", "-c", "Staging", " "]) == .usage) + #expect(await rig.run(["query", "-c", "Staging", "-f", "/nope.sql"]) == .usage) + #expect(await rig.server.requests.isEmpty) + } + } + + @Test("A write that returns no rows reports the count on standard error only") + func rowsAffected() async throws { + try await withRig { rig in + let result: JsonValue = .object([ + "columns": .array([]), "rows": .array([]), "row_count": .int(0), + "rows_affected": .int(3), "is_truncated": .bool(false) + ]) + await rig.route(["list_connections": try stagingListing(), "execute_query": try success(result)]) + #expect(await rig.run(["query", "-c", "Staging", "UPDATE t SET a = 1"]) == .success) + #expect(rig.console.output.isEmpty) + #expect(rig.console.errors == "3 rows affected.\n") + } + } + + @Test("A database error is shown with its escape sequences escaped") + func errorTextIsEscaped() async throws { + try await withRig { rig in + await rig.route([ + "list_connections": try stagingListing(), + "execute_query": try toolError("query_failed: invalid input syntax for type integer: \"x\u{1B}]52;c;SGk=\u{07}\u{1B}[2J\"") + ]) + #expect(await rig.run(["query", "-c", "Staging", "SELECT note::int FROM t"]) == .failure) + #expect(!rig.console.errors.unicodeScalars.contains { $0.value == 0x1B || $0.value == 0x07 }) + #expect(rig.console.errors.contains("\\x1B]52;c;SGk=\\x07\\x1B[2J")) + } + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRowWriterTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIRowWriterTests.swift similarity index 81% rename from apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRowWriterTests.swift rename to apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIRowWriterTests.swift index 3ef2a74787..cfcdc1d4c4 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRowWriterTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLIRowWriterTests.swift @@ -1,5 +1,6 @@ import Foundation -@testable import TableProMCP +@testable import TableProCommandLine +import TableProMCP import Testing struct CLIRowWriterTests { @@ -80,7 +81,19 @@ struct CLIRowWriterTests { @Test("Control characters are escaped in JSON strings") func jsonControlCharacters() { - #expect(CLIRowWriter.jsonString("a\u{1}b\nc") == "\"a\\u0001b\\nc\"") + #expect(CLIRowWriter.jsonString("a\u{1}b\nc") == "\"a\\u0001b\\u000ac\"") + } + + @Test("Every C0, DEL and C1 character uses a Unicode escape in JSON") + func allJsonControlCharacters() throws { + for value in Array(0...0x1F) + Array(0x7F...0x9F) { + let scalar = try #require(Unicode.Scalar(value)) + let text = String(scalar) + let encoded = CLIRowWriter.jsonString(text) + #expect(encoded == String(format: "\"\\u%04x\"", value)) + #expect(try JSONDecoder().decode(String.self, from: Data(encoded.utf8)) == text) + } + #expect(CLIRowWriter.jsonString(" ~\u{A0}日本語😀") == "\" ~\u{A0}日本語😀\"") } @Test("The table pads every column but the last and shows NULL") @@ -98,6 +111,22 @@ struct CLIRowWriterTests { """) } + @Test("The table pads by terminal cells, so wide characters keep the columns lined up") + func tableWideCharacters() { + let set = CLIResultSet( + columns: ["name", "id"], + rows: [[.string("日本語"), .string("1")], [.string("abcdef"), .string("2")], [.string("😀"), .string("3")]] + ) + #expect(render(set, .table) == """ + name | id + -------+--- + 日本語 | 1 + abcdef | 2 + 😀 | 3 + + """) + } + @Test("The table shows a stored escape sequence instead of sending it to the terminal") func tableEscapesControlCharacters() { #expect(CLIRowWriter.tableCell("\u{1B}[2Jgone") == "\\x1B[2Jgone") diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITerminalTextTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITerminalTextTests.swift new file mode 100644 index 0000000000..901c1e99ac --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITerminalTextTests.swift @@ -0,0 +1,65 @@ +import Foundation +@testable import TableProCommandLine +import TableProMCP +import Testing + +struct CLITerminalTextTests { + @Test("DDL escapes terminal controls and preserves pipe bytes", arguments: [true, false]) + func ddlControls(stdoutIsTerminal: Bool) async throws { + try await withRig { rig in + let ddl = "CREATE TABLE users (value TEXT DEFAULT '\u{1B}]52;c;eA==\u{07}');\n" + await rig.route([ + "list_connections": try stagingListing(), + "describe_table": try success(.object(["ddl": .string(ddl)])) + ]) + let code = await rig.run(["describe", "-c", "Staging", "--ddl", "users"], stdoutIsTerminal: stdoutIsTerminal) + #expect(code == .success) + let expected = stdoutIsTerminal ? "CREATE TABLE users (value TEXT DEFAULT '\\x1B]52;c;eA==\\x07');\n" : ddl + #expect(rig.console.output == expected) + } + } + + @Test("A message keeps its line breaks and tabs and shows every other control character") + func messageEscaping() { + #expect(CLITerminalText.message("plain\ttext\nnext") == "plain\ttext\nnext") + #expect(CLITerminalText.message("a\u{1B}[2Jb\u{07}\r\u{9B}") == "a\\x1B[2Jb\\x07\\x0D\\x9B") + #expect(CLITerminalText.message("日本語 \\ ok") == "日本語 \\ ok") + } + + @Test("Width counts terminal cells, in any process locale", arguments: [ + ("abc", 3), + ("日本語", 6), + ("한국", 4), + ("😀", 2), + ("e\u{301}", 1), + ("👨\u{200D}👩\u{200D}👧", 2), + ("🇯🇵", 2), + ("❤\u{FE0F}", 2), + ("A", 2), + ("", 0) + ]) + func width(text: String, cells: Int) { + #expect(CLITerminalText.width(of: text) == cells) + } + + @Test("Standard error lines are escaped even when the text comes from TablePro") + func writeErrorEscapes() { + let console = RecordingConsole() + let environment = CLIEnvironment( + console: console, + terminal: CLITerminal(standardInput: false, standardOutput: false, standardError: true), + tokenStore: MemoryTokenStore(nil), + app: CLIHostApp(url: nil, version: nil, bundleIdentifier: nil), + openServer: { throw CLIFailure(.failure, "unused") }, + findServer: { .notRunning }, + pair: { _ in "" }, + openTargets: { _ in }, + readStandardInput: { "" }, + readFile: { _ in "" }, + fileExists: { _ in false }, + currentDirectory: workDirectory + ) + CLISession(environment: environment).writeError("tablepro: \u{1B}]0;title\u{07}") + #expect(console.errors == "tablepro: \\x1B]0;title\\x07\n") + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITestRig.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITestRig.swift new file mode 100644 index 0000000000..e2ddb7408f --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITestRig.swift @@ -0,0 +1,199 @@ +import Darwin +import Foundation +@testable import TableProCommandLine +import TableProMCP +import TableProMCPTestSupport +import Testing + +final class RecordingConsole: CLIConsole, @unchecked Sendable { + private let lock = NSLock() + private var standardOutput = "" + private var standardError = "" + + var output: String { lock.withLock { standardOutput } } + var errors: String { lock.withLock { standardError } } + + func out(_ text: String) { + lock.withLock { standardOutput += text } + } + + func err(_ text: String) { + lock.withLock { standardError += text } + } +} + +final class MemoryTokenStore: CLITokenStoring, @unchecked Sendable { + private let lock = NSLock() + private var token: String? + + init(_ token: String?) { + self.token = token + } + + var current: String? { lock.withLock { token } } + + func read() throws -> String? { current } + + func save(_ token: String) throws { + lock.withLock { self.token = token } + } + + func delete() { + lock.withLock { token = nil } + } +} + +final class CallLog: @unchecked Sendable { + private let lock = NSLock() + private var entries: [String] = [] + private var openedTargets: [[URL]] = [] + + var calls: [String] { lock.withLock { entries } } + var opened: [[URL]] { lock.withLock { openedTargets } } + + func record(_ call: String) { + lock.withLock { entries.append(call) } + } + + func open(_ targets: [URL]) { + lock.withLock { + entries.append("open") + openedTargets.append(targets) + } + } +} + +let stagingId = "6B9F7C2E-1D3A-4C5B-8E9F-0A1B2C3D4E5F" +let workDirectory = URL(fileURLWithPath: "/work", isDirectory: true) + +struct Rig { + let server: MockHttpServer + let console = RecordingConsole() + let tokens: MemoryTokenStore + let log = CallLog() + + var reachable: CLIServerProbe { + .reachable(CLIServerEndpoint(socketPath: server.socketPath, serverPID: getpid(), appPath: nil)) + } + + func run( + _ arguments: [String], + stdinIsTerminal: Bool = true, + stdoutIsTerminal: Bool = false, + stderrIsTerminal: Bool = true, + stdin: String = "", + probe: CLIServerProbe? = nil, + files: Set = [] + ) async -> CLIExitCode { + let socketPath = server.socketPath + let log = log + let found = probe ?? reachable + let environment = CLIEnvironment( + console: console, + terminal: CLITerminal( + standardInput: stdinIsTerminal, + standardOutput: stdoutIsTerminal, + standardError: stderrIsTerminal + ), + tokenStore: tokens, + app: CLIHostApp( + url: URL(fileURLWithPath: "/Applications/TablePro.app", isDirectory: true), + version: "9.8.7", + bundleIdentifier: "com.TablePro" + ), + openServer: { + log.record("openServer") + return CLIServerEndpoint(socketPath: socketPath, serverPID: getpid(), appPath: nil) + }, + findServer: { + log.record("findServer") + return found + }, + pair: { _ in + log.record("pair") + return "tp_paired" + }, + openTargets: { targets in log.open(targets) }, + readStandardInput: { stdin }, + readFile: { path in throw CocoaError(.fileNoSuchFile, userInfo: [NSFilePathErrorKey: path]) }, + fileExists: { path in files.contains(path) }, + currentDirectory: workDirectory + ) + return await TableProCommand.run(arguments: arguments, environment: environment) + } + + // Answers by tool name, RPC method or path, so a test does not depend on the order of calls. + func route(_ answers: [String: MockHttpResponse]) async { + await server.setHandler { request in + answers[request.header("Mcp-Name") ?? ""] + ?? answers[request.header("Mcp-Method") ?? ""] + ?? answers[request.path] + } + } + + func requests(named name: String) async -> [MockHttpRequest] { + await server.requests.filter { $0.header("Mcp-Name") == name } + } +} + +func withRig(token: String? = "tp_stored", _ body: (Rig) async throws -> Void) async throws { + let server = MockHttpServer() + try await server.start() + do { + try await body(Rig(server: server, tokens: MemoryTokenStore(token))) + } catch { + await server.stop() + throw error + } + await server.stop() +} + +func success(_ structured: JsonValue) throws -> MockHttpResponse { + let result: JsonValue = .object([ + "content": .array([.object(["type": .string("text"), "text": .string(structured.jsonString())])]), + "structuredContent": structured, + "isError": .bool(false) + ]) + return try rpc(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: result))) +} + +func toolError(_ text: String) throws -> MockHttpResponse { + let result: JsonValue = .object([ + "content": .array([.object(["type": .string("text"), "text": .string(text)])]), + "isError": .bool(true) + ]) + return try rpc(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: result))) +} + +func rpc(_ message: JsonRpcMessage, status: Int = 200) throws -> MockHttpResponse { + MockHttpResponse(status: status, headers: [("Content-Type", "application/json")], body: try JsonRpcCodec.encode(message)) +} + +func plainJson(_ text: String, status: Int) -> MockHttpResponse { + MockHttpResponse(status: status, headers: [("Content-Type", "application/json")], body: Data(text.utf8)) +} + +func rows(_ columns: [String], _ values: [[JsonValue]], truncated: Bool = false) -> JsonValue { + .object([ + "columns": .array(columns.map { .string($0) }), + "rows": .array(values.map { .array($0) }), + "row_count": .int(values.count), + "rows_affected": .int(0), + "is_truncated": .bool(truncated) + ]) +} + +func listing(_ connections: [(id: String, name: String)]) throws -> MockHttpResponse { + try success(.object(["connections": .array(connections.map { entry in + .object(["id": .string(entry.id), "name": .string(entry.name), "type": .string("PostgreSQL")]) + })])) +} + +func stagingListing() throws -> MockHttpResponse { + try listing([(stagingId, "Staging")]) +} + +func arguments(of request: MockHttpRequest) throws -> JsonValue { + let body = try JSONDecoder().decode(JsonValue.self, from: request.body) + return try #require(body["params"]?["arguments"]) +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLITokenStoreTests.swift b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITokenStoreTests.swift similarity index 97% rename from apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLITokenStoreTests.swift rename to apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITokenStoreTests.swift index fa86224f44..e624aa048a 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLITokenStoreTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProCommandLineTests/CLITokenStoreTests.swift @@ -1,5 +1,5 @@ import Foundation -@testable import TableProMCP +@testable import TableProCommandLine import Testing struct CLITokenStoreTests { diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIArgumentsTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIArgumentsTests.swift deleted file mode 100644 index d672a83d3e..0000000000 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIArgumentsTests.swift +++ /dev/null @@ -1,95 +0,0 @@ -import Foundation -@testable import TableProMCP -import Testing - -struct CLIArgumentsTests { - private func query(_ arguments: [String]) throws -> CLIQueryOptions { - guard case .query(let options) = try CLIArguments.parse(["query"] + arguments) else { - Issue.record("Expected a query command") - throw CLIFailure.usage("not a query") - } - return options - } - - private func usageError(_ arguments: [String]) -> CLIFailure? { - do { - _ = try CLIArguments.parse(arguments) - return nil - } catch let failure as CLIFailure { - return failure - } catch { - return nil - } - } - - @Test("Every query option is read, in both spellings") - func readsEveryOption() throws { - let options = try query([ - "--connection", "Staging", "--database=app", "--schema", "public", "--format", "CSV", - "--limit", "20", "--timeout=5", "--read-only", "SELECT 1 AS n" - ]) - #expect(options == CLIQueryOptions( - connection: "Staging", - database: "app", - schema: "public", - format: .csv, - limit: 20, - timeoutSeconds: 5, - readOnly: true, - sql: .argument("SELECT 1 AS n") - )) - } - - @Test("The SQL comes from the argument, a file, or standard input") - func sqlSources() throws { - #expect(try query(["-c", "x", "SELECT 1"]).sql == .argument("SELECT 1")) - #expect(try query(["-c", "x", "-f", "q.sql"]).sql == .file("q.sql")) - #expect(try query(["-c", "x", "--file", "-"]).sql == .standardInput) - #expect(try query(["-c", "x"]).sql == .standardInput) - } - - @Test("SQL that starts with a comment is SQL, not an option") - func leadingCommentIsSql() throws { - let sql = "-- report\nSELECT 1" - #expect(try query(["-c", "x", sql]).sql == .argument(sql)) - #expect(try query(["-c", "x", "--", "-1"]).sql == .argument("-1")) - } - - @Test("A connection name with a space survives the inline spelling") - func inlineValueWithSpace() throws { - #expect(try query(["--connection=My Prod", "SELECT 1"]).connection == "My Prod") - } - - @Test("Usage mistakes exit 2") - func usageMistakes() { - let cases: [[String]] = [ - [], - ["select"], - ["query", "SELECT 1"], - ["query", "-c", "x", "--format", "xml", "SELECT 1"], - ["query", "-c", "x", "--limit", "0", "SELECT 1"], - ["query", "-c", "x", "--timeout", "soon", "SELECT 1"], - ["query", "-c", "x", "--bogus", "SELECT 1"], - ["query", "-c", "x", "SELECT", "1"], - ["query", "-c", "x", "-f", "q.sql", "SELECT 1"], - ["query", "-c"], - ["query", "-c", "x", "--read-only=yes", "SELECT 1"], - ["connections", "extra"] - ] - for arguments in cases { - #expect(usageError(arguments)?.exitCode == .usage, "\(arguments)") - } - } - - @Test("Help is a command of its own and wins over a missing connection") - func help() throws { - #expect(try CLIArguments.parse(["query", "--help"]) == .help(CLIArguments.queryUsage)) - #expect(try CLIArguments.parse(["connections", "-h"]) == .help(CLIArguments.connectionsUsage)) - } - - @Test("connections takes a format") - func connections() throws { - #expect(try CLIArguments.parse(["connections"]) == .connections(format: nil)) - #expect(try CLIArguments.parse(["connections", "--format", "json"]) == .connections(format: .json)) - } -} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIPairingTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIPairingTests.swift index 5bc71b9517..4751d89d9c 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIPairingTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIPairingTests.swift @@ -45,32 +45,4 @@ struct CLIPairingTests { #expect(CLIPairingCallback.parse(requestTarget: "/favicon.ico", state: "s1") == .ignored) #expect(CLIPairingCallback.parse(requestTarget: "\(path)?state=s1", state: "s1") == .ignored) } - - @Test("The listener answers stray requests and hands back the code the browser delivers") - func listenerRoundTrip() async throws { - let listener = CLICallbackListener(state: "s1") - defer { listener.stop() } - let port = try await listener.start() - #expect(port > 0) - - let session = URLSession(configuration: .ephemeral) - let stray = try #require(URL(string: "http://127.0.0.1:\(port)/favicon.ico")) - let (_, strayResponse) = try await session.data(from: stray) - #expect((strayResponse as? HTTPURLResponse)?.statusCode == 404) - - let callback = try #require(URL(string: "http://127.0.0.1:\(port)\(CLIPairingRequest.callbackPath)?code=c-1&state=s1")) - let (body, response) = try await session.data(from: callback) - #expect((response as? HTTPURLResponse)?.statusCode == 200) - #expect(String(data: body, encoding: .utf8)?.contains("You can close this tab") == true) - - #expect(await listener.waitForCallback(timeout: .seconds(5)) == .approved(code: "c-1")) - } - - @Test("A listener nobody calls gives up") - func listenerTimeout() async throws { - let listener = CLICallbackListener(state: "s1") - defer { listener.stop() } - _ = try await listener.start() - #expect(await listener.waitForCallback(timeout: .milliseconds(50)) == nil) - } } diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRunnerTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRunnerTests.swift deleted file mode 100644 index 6b0e3876a5..0000000000 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/CommandLine/CLIRunnerTests.swift +++ /dev/null @@ -1,314 +0,0 @@ -import Foundation -@testable import TableProMCP -import TableProMCPTestSupport -import Testing - -private final class RecordingConsole: CLIConsole, @unchecked Sendable { - private let lock = NSLock() - private var standardOutput = "" - private var standardError = "" - - var output: String { lock.withLock { standardOutput } } - var errors: String { lock.withLock { standardError } } - - func out(_ text: String) { - lock.withLock { standardOutput += text } - } - - func err(_ text: String) { - lock.withLock { standardError += text } - } -} - -private final class MemoryTokenStore: CLITokenStoring, @unchecked Sendable { - private let lock = NSLock() - private var token: String? - - init(_ token: String?) { - self.token = token - } - - var current: String? { lock.withLock { token } } - - func read() throws -> String? { current } - - func save(_ token: String) throws { - lock.withLock { self.token = token } - } - - func delete() { - lock.withLock { token = nil } - } -} - -private final class PairingRecorder: @unchecked Sendable { - private let lock = NSLock() - private var count = 0 - - var calls: Int { lock.withLock { count } } - - func pair() -> String { - lock.withLock { count += 1 } - return "tp_paired" - } -} - -private struct Rig { - let server: MockHttpServer - let console = RecordingConsole() - let tokens: MemoryTokenStore - let pairing = PairingRecorder() - - func runner(stdinIsTerminal: Bool = true, stderrIsTerminal: Bool = true, stdin: String = "") -> CLIRunner { - let socketPath = server.socketPath - let pairing = pairing - return CLIRunner( - console: console, - terminal: CLITerminal(standardInput: stdinIsTerminal, standardOutput: false, standardError: stderrIsTerminal), - tokenStore: tokens, - openServer: { CLIServerEndpoint(socketPath: socketPath, appPath: nil) }, - pair: { _ in pairing.pair() }, - readStandardInput: { stdin }, - readFile: { path in throw CocoaError(.fileNoSuchFile, userInfo: [NSFilePathErrorKey: path]) } - ) - } -} - -private func withRig(token: String? = "tp_stored", _ body: (Rig) async throws -> Void) async throws { - let server = MockHttpServer() - try await server.start() - do { - try await body(Rig(server: server, tokens: MemoryTokenStore(token))) - } catch { - await server.stop() - throw error - } - await server.stop() -} - -private func success(_ structured: JsonValue) throws -> MockHttpResponse { - let result: JsonValue = .object([ - "content": .array([.object(["type": .string("text"), "text": .string(structured.jsonString())])]), - "structuredContent": structured, - "isError": .bool(false) - ]) - return try rpc(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: result))) -} - -private func toolError(_ text: String) throws -> MockHttpResponse { - let result: JsonValue = .object([ - "content": .array([.object(["type": .string("text"), "text": .string(text)])]), - "isError": .bool(true) - ]) - return try rpc(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: result))) -} - -private func rpc(_ message: JsonRpcMessage, status: Int = 200) throws -> MockHttpResponse { - MockHttpResponse(status: status, headers: [("Content-Type", "application/json")], body: try JsonRpcCodec.encode(message)) -} - -private func rows(_ columns: [String], _ values: [[JsonValue]], truncated: Bool = false) -> JsonValue { - .object([ - "columns": .array(columns.map { .string($0) }), - "rows": .array(values.map { .array($0) }), - "row_count": .int(values.count), - "rows_affected": .int(0), - "is_truncated": .bool(truncated) - ]) -} - -private func arguments(of request: MockHttpRequest) throws -> JsonValue { - let body = try JSONDecoder().decode(JsonValue.self, from: request.body) - return try #require(body["params"]?["arguments"]) -} - -private let stagingId = "6B9F7C2E-1D3A-4C5B-8E9F-0A1B2C3D4E5F" - -struct CLIRunnerTests { - @Test("SELECT 1 AS n prints a CSV header and one row and exits 0") - func csvQuery() async throws { - try await withRig { rig in - await rig.server.setResponse(try success(rows(["n"], [[.string("1")]]))) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "--format", "csv", "SELECT 1 AS n"]) - #expect(code == .success) - #expect(rig.console.output == "n\n1\n") - #expect(rig.console.errors.isEmpty) - - let request = try #require(await rig.server.requests.first) - #expect(request.path == "/mcp") - #expect(request.header("Authorization") == "Bearer tp_stored") - #expect(request.header("Mcp-Method") == "tools/call") - #expect(request.header("Mcp-Name") == "execute_query") - let body = try JSONDecoder().decode(JsonValue.self, from: request.body) - #expect(body["params"]?["_meta"]?[MCPMetaKeys.clientInfo]?["name"]?.stringValue == CLIClientIdentity.name) - let sent = try arguments(of: request) - #expect(sent["connection_id"]?.stringValue == stagingId) - #expect(sent["query"]?.stringValue == "SELECT 1 AS n") - #expect(sent["read_only"] == nil) - } - } - - @Test("--read-only goes to the server, and its refusal exits 3") - func readOnlyRefusal() async throws { - try await withRig { rig in - await rig.server.setResponse(try toolError("denied: This request is read only, and the statement is not a read.")) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "--read-only", "DELETE FROM t"]) - #expect(code == .refused) - #expect(rig.console.output.isEmpty) - #expect(rig.console.errors.contains("read only")) - let request = try #require(await rig.server.requests.first) - #expect(try arguments(of: request)["read_only"]?.boolValue == true) - } - } - - @Test("A statement timeout exits 4") - func timeout() async throws { - try await withRig { rig in - await rig.server.setResponse(try toolError("timeout: The query did not finish within 5 seconds.")) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "--timeout", "5", "SELECT pg_sleep(10)"]) - #expect(code == .timedOut) - let request = try #require(await rig.server.requests.first) - #expect(try arguments(of: request)["timeout_seconds"]?.intValue == 5) - } - } - - @Test("A revoked token is forgotten and the run exits 3") - func revokedToken() async throws { - try await withRig { rig in - let failure = JsonRpcErrorResponse(id: nil, error: JsonRpcError(code: JsonRpcErrorCode.unauthenticated, message: "Unauthenticated")) - await rig.server.setResponse(try rpc(.errorResponse(failure), status: 401)) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "SELECT 1"]) - #expect(code == .refused) - #expect(rig.tokens.current == nil) - #expect(rig.console.errors.contains("revoked")) - #expect(rig.pairing.calls == 0) - } - } - - @Test("A connection name is resolved through list_connections, ignoring case") - func nameResolution() async throws { - try await withRig { rig in - let listing: JsonValue = .object(["connections": .array([ - .object(["id": .string(stagingId), "name": .string("Staging"), "type": .string("PostgreSQL")]), - .object(["id": .string(UUID().uuidString), "name": .string("Production"), "type": .string("MySQL")]) - ])]) - await rig.server.setResponses([try success(listing), try success(rows(["n"], [[.string("1")]]))]) - let code = await rig.runner().run(arguments: ["query", "-c", "staging", "SELECT 1"]) - #expect(code == .success) - let requests = await rig.server.requests - #expect(requests.map { $0.header("Mcp-Name") } == ["list_connections", "execute_query"]) - #expect(try arguments(of: requests[1])["connection_id"]?.stringValue == stagingId) - } - } - - @Test("Two connections with the same name are a usage error that lists their ids") - func ambiguousName() async throws { - try await withRig { rig in - let listing: JsonValue = .object(["connections": .array([ - .object(["id": .string("A"), "name": .string("Dev"), "type": .string("SQLite")]), - .object(["id": .string("B"), "name": .string("Dev"), "type": .string("SQLite")]) - ])]) - await rig.server.setResponse(try success(listing)) - let code = await rig.runner().run(arguments: ["query", "-c", "Dev", "SELECT 1"]) - #expect(code == .usage) - #expect(rig.console.errors.contains("A, B")) - #expect(await rig.server.requests.count == 1) - } - } - - @Test("An unpaired command outside a terminal refuses instead of opening a pairing sheet") - func noPairingWithoutTerminal() async throws { - try await withRig(token: nil) { rig in - let code = await rig.runner(stderrIsTerminal: false).run(arguments: ["query", "-c", stagingId, "SELECT 1"]) - #expect(code == .refused) - #expect(rig.pairing.calls == 0) - #expect(await rig.server.requests.isEmpty) - } - } - - @Test("An unpaired command in a terminal pairs once and keeps the token") - func pairsOnce() async throws { - try await withRig(token: nil) { rig in - await rig.server.setResponse(try success(rows(["n"], [[.string("1")]]))) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "SELECT 1"]) - #expect(code == .success) - #expect(rig.pairing.calls == 1) - #expect(rig.tokens.current == "tp_paired") - #expect(await rig.server.requests.first?.header("Authorization") == "Bearer tp_paired") - } - } - - @Test("connections prints name, id and type, and nothing else") - func connections() async throws { - try await withRig { rig in - let listing: JsonValue = .object(["connections": .array([ - .object([ - "id": .string(stagingId), "name": .string("Staging"), "type": .string("PostgreSQL"), - "host": .string("db.internal"), "port": .int(5_432) - ]) - ])]) - await rig.server.setResponse(try success(listing)) - let code = await rig.runner().run(arguments: ["connections", "--format", "json"]) - #expect(code == .success) - #expect(rig.console.output == "[\n {\"name\":\"Staging\",\"id\":\"\(stagingId)\",\"type\":\"PostgreSQL\"}\n]\n") - } - } - - @Test("A cut-short result says so on standard error") - func truncatedNote() async throws { - try await withRig { rig in - await rig.server.setResponse(try success(rows(["n"], [[.string("1")]], truncated: true))) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "--limit", "1", "SELECT n FROM t"]) - #expect(code == .success) - #expect(rig.console.errors == "tablepro: showing the first row. Pass --limit to change it.\n") - } - } - - @Test("Every result set of a script is printed") - func resultSets() async throws { - try await withRig { rig in - var primary = rows(["a"], [[.string("1")]]).objectValue ?? [:] - primary["result_sets"] = .array([rows(["a"], [[.string("1")]]), rows(["b"], [[.string("2")]])]) - await rig.server.setResponse(try success(.object(primary))) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "--format", "csv", "SELECT 1 AS a\nGO\nSELECT 2 AS b"]) - #expect(code == .success) - #expect(rig.console.output == "a\n1\n\nb\n2\n") - } - } - - @Test("SQL comes from standard input when it is piped") - func standardInput() async throws { - try await withRig { rig in - await rig.server.setResponse(try success(rows(["n"], [[.string("1")]]))) - let code = await rig.runner(stdinIsTerminal: false, stdin: "SELECT 1\n").run(arguments: ["query", "-c", stagingId]) - #expect(code == .success) - let request = try #require(await rig.server.requests.first) - #expect(try arguments(of: request)["query"]?.stringValue == "SELECT 1\n") - } - } - - @Test("No SQL from a terminal, an empty statement, or an unreadable file is a usage error") - func missingSql() async throws { - try await withRig { rig in - #expect(await rig.runner().run(arguments: ["query", "-c", stagingId]) == .usage) - #expect(await rig.runner().run(arguments: ["query", "-c", stagingId, " "]) == .usage) - #expect(await rig.runner().run(arguments: ["query", "-c", stagingId, "-f", "/nope.sql"]) == .usage) - #expect(await rig.server.requests.isEmpty) - } - } - - @Test("A write that returns no rows reports the count on standard error only") - func rowsAffected() async throws { - try await withRig { rig in - let result: JsonValue = .object([ - "columns": .array([]), "rows": .array([]), "row_count": .int(0), - "rows_affected": .int(3), "is_truncated": .bool(false) - ]) - await rig.server.setResponse(try success(result)) - let code = await rig.runner().run(arguments: ["query", "-c", stagingId, "UPDATE t SET a = 1"]) - #expect(code == .success) - #expect(rig.console.output.isEmpty) - #expect(rig.console.errors == "3 rows affected.\n") - } - } -} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeLaunchTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeLaunchTests.swift new file mode 100644 index 0000000000..82827ad152 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeLaunchTests.swift @@ -0,0 +1,21 @@ +import AppKit +@testable import TableProMCP +import Testing + +struct MCPHandshakeLaunchTests { + @Test("A background launch keeps the helper environment and overrides the background flag") + func launchKeepsInheritedEnvironment() { + let environment = [ + "TABLEPRO_UI_TEST_SANDBOX": "/tmp/tablepro-mcp-test", + "PATH": "/usr/bin:/bin", + BackgroundLaunchFlag.variable: "inherited" + ] + + let configuration = MCPHandshakeAcquirer.launchConfiguration(environment: environment) + + var expected = environment + expected[BackgroundLaunchFlag.variable] = BackgroundLaunchFlag.value + #expect(configuration.environment == expected) + #expect(!configuration.activates) + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeSocketTrustTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeSocketTrustTests.swift index 8f03d8845f..25390b16de 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeSocketTrustTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Lifecycle/MCPHandshakeSocketTrustTests.swift @@ -72,7 +72,7 @@ struct MCPHandshakeSocketTrustTests { let loaded = try MCPHandshakeTrust.load(at: folder.handshake) #expect(loaded.socketPath == folder.socketPath) - #expect(loaded.credentials() == MCPUpstreamCredentials(socketPath: folder.socketPath, bearerToken: "tp_test")) + #expect(loaded.credentials() == MCPUpstreamCredentials(socketPath: folder.socketPath, serverPID: getpid(), bearerToken: "tp_test")) } } diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPStreamableHttpClientTransportTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPStreamableHttpClientTransportTests.swift index 626f293bb6..15d34df1b0 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPStreamableHttpClientTransportTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPStreamableHttpClientTransportTests.swift @@ -1,7 +1,9 @@ +import Darwin import Foundation @testable import TableProMCP import TableProMCPTestSupport import TableProTestSupport +import Testing import XCTest final class MCPStreamableHttpClientTransportTests: XCTestCase { @@ -257,10 +259,11 @@ final class MCPStreamableHttpClientTransportTests: XCTestCase { func testUnauthorizedRefreshesCredentialsAndRetriesExactlyOnce() async throws { let refreshed = MCPUpstreamCredentials( socketPath: server.socketPath, + serverPID: getpid(), bearerToken: "refreshed-token" ) let provider = QueuedCredentialsProvider( - initial: MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "stale-token"), + initial: MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "stale-token"), queued: [refreshed] ) await server.setResponses([ @@ -293,8 +296,8 @@ final class MCPStreamableHttpClientTransportTests: XCTestCase { func testASecondUnauthorizedIsReportedRatherThanRetriedAgain() async throws { let provider = QueuedCredentialsProvider( - initial: MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "stale-token"), - queued: [MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "still-stale")] + initial: MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "stale-token"), + queued: [MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "still-stale")] ) await server.setResponse(MockHttpResponse( status: 401, @@ -321,7 +324,7 @@ final class MCPStreamableHttpClientTransportTests: XCTestCase { func testAFailedRefreshSurfacesAnErrorRatherThanHanging() async throws { let provider = QueuedCredentialsProvider( - initial: MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "stale-token"), + initial: MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "stale-token"), queued: [] ) await server.setResponse(MockHttpResponse( @@ -345,6 +348,7 @@ final class MCPStreamableHttpClientTransportTests: XCTestCase { func testAnUnreachableEndpointTellsTheUserToStartTablePro() async throws { let credentials = MCPUpstreamCredentials( socketPath: MCPSocketLocation.primarySocket(in: MCPTestSocketFolder.url()).path, + serverPID: getpid(), bearerToken: "token" ) let transport = MCPStreamableHttpClientTransport( @@ -436,7 +440,7 @@ final class MCPStreamableHttpClientTransportTests: XCTestCase { credentialsProvider: (any MCPUpstreamCredentialsProviding)? = nil, errorLogger: (any MCPBridgeLogger)? = nil ) -> MCPStreamableHttpClientTransport { - let credentials = MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "test-token") + let credentials = MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "test-token") let provider = credentialsProvider ?? MCPCachedUpstreamCredentialsProvider(initial: credentials) { credentials } return MCPStreamableHttpClientTransport( @@ -534,3 +538,37 @@ actor QueuedCredentialsProvider: MCPUpstreamCredentialsProviding { return current } } + +struct MCPStreamableHttpPeerTests { + @Test("A changed PID refreshes credentials once before sending to the verified process") + func changedPeerRefreshesOnce() async throws { + let server = MockHttpServer() + try await server.start() + do { + let credentials = MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "new-token") + let provider = QueuedCredentialsProvider( + initial: MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid() + 1, bearerToken: "old-token"), + queued: [credentials] + ) + let response = try JsonRpcCodec.encode(.successResponse(JsonRpcSuccessResponse(id: .number(1), result: .object([:])))) + await server.respondWithJson(response) + let transport = MCPStreamableHttpClientTransport(credentialsProvider: provider) + var inbound = transport.inbound.makeAsyncIterator() + try await transport.send(MCPUpstreamFrame( + body: Data(#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#.utf8), + method: "tools/list", + requestId: .number(1) + )) + #expect(try await inbound.next() == response) + #expect(await provider.refreshCount == 1) + let requests = await server.requests + #expect(requests.count == 1) + #expect(requests.first?.header("Authorization") == "Bearer new-token") + await transport.close() + } catch { + await server.stop() + throw error + } + await server.stop() + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPUnixSocketHttpClientTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPUnixSocketHttpClientTests.swift index 0be680c0ca..4d39102e8e 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPUnixSocketHttpClientTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Transport/MCPUnixSocketHttpClientTests.swift @@ -1,3 +1,4 @@ +import Darwin import Foundation @testable import TableProMCP import TableProMCPTestSupport @@ -17,7 +18,7 @@ private func withServer(_ body: (MockHttpServer) async throws -> Void) async thr } private func upstream(_ server: MockHttpServer) -> MCPStreamableHttpClientTransport { - let credentials = MCPUpstreamCredentials(socketPath: server.socketPath, bearerToken: "tp_bridge") + let credentials = MCPUpstreamCredentials(socketPath: server.socketPath, serverPID: getpid(), bearerToken: "tp_bridge") return MCPStreamableHttpClientTransport( configuration: MCPStreamableHttpClientConfiguration(requestTimeout: .seconds(10)), credentialsProvider: MCPCachedUpstreamCredentialsProvider(initial: credentials) { credentials } @@ -74,7 +75,7 @@ struct MCPUnixSocketHttpClientTests { framing: .chunked(size: 4_096) )) - let response = try await MCPUnixSocketHttpClient.send(request(), socketPath: server.socketPath, idleTimeout: .seconds(10)) + let response = try await MCPUnixSocketHttpClient.send(request(), socketPath: server.socketPath, expectedPID: getpid(), idleTimeout: .seconds(10)) #expect(response.status == 200) #expect(response.headers.value(for: "Transfer-Encoding") == "chunked") #expect(try await response.collectBody() == body) @@ -111,11 +112,95 @@ struct MCPUnixSocketHttpClientTests { } } + @Test("Discovery accepts its answer before an event stream closes") + func discoveryOnOpenEventStream() async throws { + let folder = try MCPTestSocketFolder.make() + defer { try? FileManager.default.removeItem(at: folder) } + let held = HeldConnections() + defer { held.closeAll() } + let response = JsonRpcSuccessResponse(id: .string("tablepro-bridge-discover"), result: .object([ + "supportedVersions": .array([.string(MCPProtocolVersion.latest.rawValue)]), + "capabilities": .object([:]), + "_meta": .object([MCPServerInstanceIdentity.metaKey: .string("test-instance")]) + ])) + let payload = try JsonRpcCodec.encode(.successResponse(response)) + let reply = Data("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\ndata: ".utf8) + + payload + Data("\n\n".utf8) + let listener = MCPUnixSocketListener(path: MCPSocketLocation.primarySocket(in: folder).path) { connection in + held.keep(connection) + connection.start { _ in } + connection.receive(maximumLength: 65_536) { _, _, _ in + connection.send(reply) { _ in } + } + } + try listener.start() + defer { listener.stop() } + let handshake = MCPHandshakePayload( + socketPath: listener.path, token: "tp_bridge", pid: getpid(), instanceId: "test-instance", + protocolVersion: MCPProtocolVersion.latest.rawValue, expiresAt: Date().addingTimeInterval(60) + ) + let discovery = try await BridgeUpstreamHandshake.verifiedDiscovery( + handshake: handshake, credentials: handshake.credentials(), logger: RecordingBridgeLogger() + ) + #expect(discovery.instanceId == "test-instance") + } + + @Test("A changed peer is refused before the listener receives any bytes") + func changedPeerReceivesNothing() async throws { + let folder = try MCPTestSocketFolder.make() + defer { try? FileManager.default.removeItem(at: folder) } + let (received, continuation) = AsyncStream.makeStream() + let listener = MCPUnixSocketListener(path: MCPSocketLocation.primarySocket(in: folder).path) { connection in + connection.start { _ in } + connection.receive(maximumLength: 65_536) { bytes, _, _ in + continuation.yield(bytes ?? Data()) + continuation.finish() + connection.cancel() + } + } + try listener.start() + defer { listener.stop() } + + await #expect(throws: MCPSocketHttpError.peerChanged) { + _ = try await MCPUnixSocketHttpClient.send( + request(headers: [("Authorization", "Bearer tp_secret")]), + socketPath: listener.path, + expectedPID: getpid() + 1, + idleTimeout: .seconds(5) + ) + } + var iterator = received.makeAsyncIterator() + #expect(await iterator.next() == Data()) + } + + @Test("Cancelling a request before its headers arrive closes the socket") + func cancelPendingResponse() async throws { + let folder = try MCPTestSocketFolder.make() + defer { try? FileManager.default.removeItem(at: folder) } + let (accepted, continuation) = AsyncStream.makeStream() + let listener = MCPUnixSocketListener(path: MCPSocketLocation.primarySocket(in: folder).path) { connection in + continuation.yield(connection) + continuation.finish() + } + try listener.start() + defer { listener.stop() } + let task = Task { + try await MCPUnixSocketHttpClient.send( + request(), socketPath: listener.path, expectedPID: getpid(), idleTimeout: .seconds(10) + ) + } + var iterator = accepted.makeAsyncIterator() + let connection = try #require(await iterator.next()) + defer { connection.cancel() } + task.cancel() + await #expect(throws: CancellationError.self) { try await task.value } + } + @Test("A socket nobody listens on is a connect failure, so nothing was sent") func missingSocket() async throws { let path = MCPSocketLocation.primarySocket(in: MCPTestSocketFolder.url()).path await #expect { - _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: path, idleTimeout: .seconds(5)) + _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: path, expectedPID: getpid(), idleTimeout: .seconds(5)) } throws: { error in guard case MCPSocketHttpError.connectFailed = error else { return false } return true @@ -126,7 +211,7 @@ struct MCPUnixSocketHttpClientTests { func overlongPath() async throws { let path = "/" + String(repeating: "a", count: MCPSocketLocation.maxPathBytes) await #expect(throws: MCPSocketHttpError.invalidSocketPath(path)) { - _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: path, idleTimeout: .seconds(5)) + _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: path, expectedPID: getpid(), idleTimeout: .seconds(5)) } } @@ -137,7 +222,7 @@ struct MCPUnixSocketHttpClientTests { _ = try await MCPUnixSocketHttpClient.send( request(headers: [("Mcp-Method", "tools/list\r\nX-Injected: 1")]), socketPath: server.socketPath, - idleTimeout: .seconds(5) + expectedPID: getpid(), idleTimeout: .seconds(5) ) } #expect(await server.requests.isEmpty) @@ -156,7 +241,7 @@ struct MCPUnixSocketHttpClientTests { defer { listener.stop() } await #expect(throws: MCPSocketHttpError.timedOut) { - _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: listener.path, idleTimeout: .milliseconds(200)) + _ = try await MCPUnixSocketHttpClient.send(request(), socketPath: listener.path, expectedPID: getpid(), idleTimeout: .milliseconds(200)) } held.closeAll() } diff --git a/apple/TablePro/CLI/CLIMain.swift b/apple/TablePro/CLI/CLIMain.swift index 967e9bcc68..5c17d4392f 100644 --- a/apple/TablePro/CLI/CLIMain.swift +++ b/apple/TablePro/CLI/CLIMain.swift @@ -1,73 +1,9 @@ import Darwin -import Foundation -import TableProMCP -import TableProUtilities +import TableProCommandLine @main struct TableProCLI { static func main() async { - let console = StandardConsole() - let code = await CLIRunner.live(console: console).run(arguments: Array(CommandLine.arguments.dropFirst())) - console.flush() - // A full disk or a closed file must not pass for a complete export. - if let failure = console.outputFailure, code == .success { - console.err("tablepro: could not write the output: \(failure.localizedDescription)\n") - exit(CLIExitCode.failure.rawValue) - } - exit(code.rawValue) - } -} - -private final class StandardConsole: CLIConsole, @unchecked Sendable { - private static let flushThreshold = 64 * 1_024 - - private let lock = NSLock() - private var pending = Data() - private var firstFailure: Error? - - var outputFailure: Error? { - lock.withLock { firstFailure } - } - - func out(_ text: String) { - let ready: Data? = lock.withLock { - pending.append(contentsOf: text.utf8) - guard pending.count >= Self.flushThreshold else { return nil } - defer { pending.removeAll(keepingCapacity: true) } - return pending - } - if let ready { - writeOutput(ready) - } - } - - func err(_ text: String) { - flush() - Self.write(Data(text.utf8), to: STDERR_FILENO) - } - - func flush() { - let ready: Data = lock.withLock { - defer { pending.removeAll(keepingCapacity: true) } - return pending - } - guard !ready.isEmpty else { return } - writeOutput(ready) - } - - private func writeOutput(_ data: Data) { - do { - try DescriptorWrite.allBytes(data, to: STDOUT_FILENO) - } catch { - lock.withLock { - if firstFailure == nil { - firstFailure = error - } - } - } - } - - private static func write(_ data: Data, to descriptor: Int32) { - try? DescriptorWrite.allBytes(data, to: descriptor) + exit(await CommandLineTool.main(arguments: Array(CommandLine.arguments.dropFirst()))) } } diff --git a/apple/TablePro/CLI/tablepro b/apple/TablePro/CLI/tablepro index 742b4b05f0..e9cf9bfa12 100755 --- a/apple/TablePro/CLI/tablepro +++ b/apple/TablePro/CLI/tablepro @@ -1,13 +1,2 @@ #!/bin/sh -case "$1" in - query | connections) - # Linked into a PATH directory, so the bundle is found through the link. - cli="$(/usr/bin/dirname "$(/usr/bin/readlink -f "$0")")/../../MacOS/tablepro-cli" - if [ ! -x "$cli" ]; then - echo "tablepro: $1 needs this script linked from TablePro.app, not copied out of it." >&2 - exit 2 - fi - exec "$cli" "$@" - ;; -esac -exec open -b com.TablePro "$@" +exec "$(/usr/bin/dirname "$(/usr/bin/readlink -f "$0")")/../../MacOS/tablepro-cli" "$@" diff --git a/apple/TablePro/Core/MCP/Lifecycle/MCPServerComposition.swift b/apple/TablePro/Core/MCP/Lifecycle/MCPServerComposition.swift index 6cc354fa0e..52006906ec 100644 --- a/apple/TablePro/Core/MCP/Lifecycle/MCPServerComposition.swift +++ b/apple/TablePro/Core/MCP/Lifecycle/MCPServerComposition.swift @@ -25,7 +25,8 @@ internal struct MCPServerComposition { let transport = MCPHttpServerTransport( configuration: .loopback(port: port, socketPath: socketPath), authenticator: authenticator, - pairingService: .shared + pairingService: .shared, + tokenRevoker: tokenStore ) let subscriptions = MCPSubscriptionRegistry() let services = MCPToolServices( diff --git a/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift b/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift index 1aac839762..ad761974f2 100644 --- a/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift +++ b/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift @@ -150,20 +150,29 @@ internal final class MCPServerManager: ObservableObject { composition?.authPolicy ?? MCPAuthPolicy() } - internal func disconnectClient(_ clientId: String) async { - guard let composition else { return } + // False when the Keychain write failed: the token works again after a restart. + @discardableResult + internal func revokeToken(id: UUID) async -> Bool { + guard let tokenStore else { return true } + return await tokenStore.revoke(tokenId: id) + } + + @discardableResult + internal func disconnectClient(_ clientId: String) async -> Bool { + guard let composition else { return true } let entries = await composition.activityLedger.snapshot(now: Date()) guard let tokenId = entries.first(where: { $0.id == clientId })?.tokenId else { await composition.activityLedger.forget(id: clientId) await refreshClients() - return + return true } - await tokenStore?.revoke(tokenId: tokenId) + let persisted = await revokeToken(id: tokenId) await composition.authPolicy.clearApprovals(tokenId: tokenId) await composition.activityLedger.forget(tokenId: tokenId) await refreshClients() + return persisted } // The one check every start path reaches, so no caller can bind a listener while MCP is off. diff --git a/apple/TablePro/Core/MCP/MCPTokenStore.swift b/apple/TablePro/Core/MCP/MCPTokenStore.swift index 4a01a9a1f3..65893f05b2 100644 --- a/apple/TablePro/Core/MCP/MCPTokenStore.swift +++ b/apple/TablePro/Core/MCP/MCPTokenStore.swift @@ -31,8 +31,7 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { var lastUsedAt: Date? let expiresAt: Date? var isActive: Bool - /// Set only by the bundled bridge's own mint. Never derived from the token's name, which a - /// pairing request supplies and could therefore claim. + // Set only by the bridge's own mint, never from the name, which a pairing request supplies. let isBridgeCredential: Bool let extraScopes: Set @@ -107,8 +106,7 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { self.isActive = try container.decode(Bool.self, forKey: .isActive) self.connectionAccess = try container.decodeIfPresent(ConnectionAccess.self, forKey: .connectionAccess) ?? .all self.isBridgeCredential = try container.decodeIfPresent(Bool.self, forKey: .isBridgeCredential) ?? false - /// Lenient: a bad value here must cost this grant, not every saved token. Filtering to - /// `optionalGrants` keeps a tier scope such as `admin` from ever arriving through this field. + // Lenient, so a bad value costs this grant only; the filter keeps a tier scope such as admin out. let storedExtraScopes = (try? container.decodeIfPresent([String].self, forKey: .extraScopes)) ?? [] self.extraScopes = Set(storedExtraScopes.compactMap(MCPScope.init(rawValue:))) .intersection(MCPScope.optionalGrants) @@ -196,9 +194,7 @@ actor MCPTokenStore { revocationObservers.removeValue(forKey: id) } - /// The bridge's reserved name carries no privilege, and nothing but the bridge's own mint may - /// claim it: a pairing request supplies its client name, so a caller could otherwise ask to be - /// called the bridge and inherit every grant the bridge had earned. + // Only the bridge's own mint may take its name: a pairing client could otherwise claim the bridge's grants. func generate( name: String, permissions: TokenPermissions, @@ -239,10 +235,7 @@ actor MCPTokenStore { } func resolve(bearerToken: String) -> Result { - let match = tokens.firstIndex { token in - Self.constantTimeCompare(Self.computeHash(salt: token.salt, plaintext: bearerToken), token.tokenHash) - } - guard let index = match else { return .failure(.unknownToken) } + guard let index = index(presenting: bearerToken) else { return .failure(.unknownToken) } if tokens[index].isExpired { return .failure(.expired) } if !tokens[index].isActive { return .failure(.revoked) } @@ -255,20 +248,35 @@ actor MCPTokenStore { try? resolve(bearerToken: bearerToken).get() } - func revoke(tokenId: UUID) { + @discardableResult + func revoke(tokenId: UUID) -> Bool { guard let index = tokens.firstIndex(where: { $0.id == tokenId }) else { Self.logger.warning("Attempted to revoke non-existent token \(tokenId.uuidString, privacy: .public)") - return + // A token the store no longer holds leaves nothing unsaved to warn about. + return true } tokens[index].isActive = false let revokedName = tokens[index].name let wasBridge = tokens[index].isBridgeCredential - save() + let persisted = save() notifyRevocationObservers(tokenId: tokenId, wasBridgeCredential: wasBridge) Self.logger.info("Revoked MCP token '\(revokedName, privacy: .private(mask: .hash))'") MCPAuditLogger.logTokenRevoked(tokenId: tokenId, tokenName: revokedName) + return persisted + } + + // The shared bridge credential is refused: any process that reads the handshake file holds it. + func revoke(presentedToken: String) -> MCPTokenRevocationOutcome { + guard let index = index(presenting: presentedToken) else { return .unknown } + let token = tokens[index] + if token.isBridgeCredential { return .bridgeCredential } + guard token.isActive else { + // An earlier revoke may have failed to save, so a retry saves again before it confirms. + return save() ? .alreadyRevoked : .notPersisted + } + return revoke(tokenId: token.id) ? .revoked : .notPersisted } func delete(tokenId: UUID) { @@ -294,6 +302,12 @@ actor MCPTokenStore { } } + private func index(presenting plaintext: String) -> Int? { + tokens.firstIndex { token in + Self.constantTimeCompare(Self.computeHash(salt: token.salt, plaintext: plaintext), token.tokenHash) + } + } + func list() -> [MCPAuthToken] { tokens } @@ -356,16 +370,19 @@ actor MCPTokenStore { save() } - private func save() { + @discardableResult + private func save() -> Bool { lastSavedAt = .now do { let data = try Self.encoder.encode(tokens) guard credentialStore.write(data) else { Self.logger.error("Failed to write MCP tokens to the keychain") - return + return false } + return true } catch { Self.logger.error("Failed to encode MCP tokens: \(error.publicLogShape, privacy: .public)") + return false } } @@ -419,6 +436,21 @@ actor MCPTokenStore { } } +enum MCPTokenRevocationOutcome: Sendable, Equatable { + case revoked + case alreadyRevoked + case unknown + case bridgeCredential + // Revoked in memory only: the Keychain write failed, so a restart brings the token back. + case notPersisted +} + +protocol MCPTokenRevoking: Sendable { + func revoke(presentedToken: String) async -> MCPTokenRevocationOutcome +} + +extension MCPTokenStore: MCPTokenRevoking {} + protocol MCPTokenCredentialStoring: Sendable { func read() -> Data? @discardableResult diff --git a/apple/TablePro/Core/MCP/Transport/Extensions/MCPHttpRequestRouter+Revocation.swift b/apple/TablePro/Core/MCP/Transport/Extensions/MCPHttpRequestRouter+Revocation.swift new file mode 100644 index 0000000000..1f5b52a941 --- /dev/null +++ b/apple/TablePro/Core/MCP/Transport/Extensions/MCPHttpRequestRouter+Revocation.swift @@ -0,0 +1,62 @@ +import Foundation +import os +import TableProMCP + +internal extension MCPHttpRequestRouter { + static let maxRevocationTokenBytes = 1_024 + + // RFC 7009: holding the token is the credential, and an unknown token answers 200 like a revoked one. + func handleTokenRevocation(body: Data, context: HttpConnectionContext) async { + Self.logger.info("Integrations revoke request received (\(body.count, privacy: .public) bytes)") + + guard let token = Self.revocationToken(fromForm: body) else { + Self.logger.warning("Integrations revoke refused: no single token parameter") + await respondRevocationError(context: context, error: "invalid_request") + return + } + + let outcome = await tokenRevoker.revoke(presentedToken: token) + switch outcome { + case .revoked, .alreadyRevoked, .unknown: + Self.logger.info("Integrations revoke answered: outcome=\(String(describing: outcome), privacy: .public)") + await context.writePlainJsonResponse(status: .ok, body: Data("{}".utf8)) + await context.completeResponse() + case .bridgeCredential: + Self.logger.warning("Integrations revoke refused: the bridge credential is not revocable here") + await respondRevocationError(context: context, error: "unsupported_token_type") + case .notPersisted: + Self.logger.error("Integrations revoke not saved: the token store could not be written") + // RFC 7009 section 2.2.1: on 503 the client must assume the token still exists. + await context.writePlainJsonError(status: .serviceUnavailable, message: "temporarily_unavailable") + await context.completeResponse() + } + } + + // RFC 6749 section 3.2: a parameter without a value counts as omitted, a repeated one is invalid_request. + static func revocationToken(fromForm body: Data) -> String? { + guard let form = String(data: body, encoding: .utf8) else { return nil } + var tokens: [String] = [] + for pair in form.split(separator: "&") { + let parts = pair.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false) + guard let name = formDecoded(parts[0]) else { return nil } + guard name == "token" else { continue } + guard let value = parts.count == 2 ? formDecoded(parts[1]) : "" else { return nil } + if !value.isEmpty { + tokens.append(value) + } + } + guard tokens.count == 1, let token = tokens.first, token.utf8.count <= maxRevocationTokenBytes else { + return nil + } + return token + } + + private static func formDecoded(_ component: Substring) -> String? { + component.replacingOccurrences(of: "+", with: " ").removingPercentEncoding + } + + private func respondRevocationError(context: HttpConnectionContext, error: String) async { + await context.writePlainJsonError(status: .badRequest, message: error) + await context.completeResponse() + } +} diff --git a/apple/TablePro/Core/MCP/Transport/MCPHttpRequestRouter.swift b/apple/TablePro/Core/MCP/Transport/MCPHttpRequestRouter.swift index b1a2cfea11..a722e02170 100644 --- a/apple/TablePro/Core/MCP/Transport/MCPHttpRequestRouter.swift +++ b/apple/TablePro/Core/MCP/Transport/MCPHttpRequestRouter.swift @@ -9,9 +9,11 @@ struct MCPHttpRequestRouter: Sendable { static let mcpPath = "/mcp" static let pairingPath = "/v1/integrations/exchange" + static let revocationPath = "/v1/integrations/revoke" let authenticator: any MCPAuthenticator let pairingService: MCPPairingService + let tokenRevoker: any MCPTokenRevoking let clock: any MCPClock let boundPort: UInt16 let emitInbound: InboundEmitter @@ -54,6 +56,10 @@ struct MCPHttpRequestRouter: Sendable { await handlePairingExchange(body: body, context: context) return } + if await servesRevocation(path: path, context: context) { + await handleTokenRevocation(body: body, context: context) + return + } guard Self.isMcpPath(path) else { await respondNotFound(context: context) return @@ -62,7 +68,8 @@ struct MCPHttpRequestRouter: Sendable { } private func respondMethodNotAllowedOrNotFound(path: String, context: HttpConnectionContext) async { - guard Self.isMcpPath(path) || path == Self.pairingPath else { + let isRevocation = await servesRevocation(path: path, context: context) + guard Self.isMcpPath(path) || path == Self.pairingPath || isRevocation else { await respondNotFound(context: context) return } @@ -74,6 +81,12 @@ struct MCPHttpRequestRouter: Sendable { path == mcpPath || path == mcpPath + "/" } + // Only the Unix socket, which admits this user's processes alone; over TCP the path does not exist. + private func servesRevocation(path: String, context: HttpConnectionContext) async -> Bool { + guard path == Self.revocationPath else { return false } + return await context.clientAddress() == .localSocket + } + func authenticate( headers: HttpHeaders, clientAddress: MCPClientAddress diff --git a/apple/TablePro/Core/MCP/Transport/MCPHttpServerTransport.swift b/apple/TablePro/Core/MCP/Transport/MCPHttpServerTransport.swift index a4b24bf43f..8f3a4faec1 100644 --- a/apple/TablePro/Core/MCP/Transport/MCPHttpServerTransport.swift +++ b/apple/TablePro/Core/MCP/Transport/MCPHttpServerTransport.swift @@ -30,6 +30,7 @@ public actor MCPHttpServerTransport { private let configuration: MCPHttpServerConfiguration private let authenticator: any MCPAuthenticator private let pairingService: MCPPairingService + private let tokenRevoker: any MCPTokenRevoking private let clock: any MCPClock private let allowedSocketPeerUid: uid_t @@ -51,12 +52,14 @@ public actor MCPHttpServerTransport { configuration: MCPHttpServerConfiguration, authenticator: any MCPAuthenticator, pairingService: MCPPairingService, + tokenRevoker: any MCPTokenRevoking, clock: any MCPClock = MCPSystemClock(), allowedSocketPeerUid: uid_t = getuid() ) { self.configuration = configuration self.authenticator = authenticator self.pairingService = pairingService + self.tokenRevoker = tokenRevoker self.clock = clock self.allowedSocketPeerUid = allowedSocketPeerUid @@ -281,6 +284,7 @@ public actor MCPHttpServerTransport { return MCPHttpRequestRouter( authenticator: authenticator, pairingService: pairingService, + tokenRevoker: tokenRevoker, clock: clock, boundPort: boundPort == 0 ? configuration.port : boundPort, emitInbound: { exchange in diff --git a/apple/TablePro/Core/Services/Infrastructure/AppActivationPolicy.swift b/apple/TablePro/Core/Services/Infrastructure/AppActivationPolicy.swift index 45d0520955..323be30207 100644 --- a/apple/TablePro/Core/Services/Infrastructure/AppActivationPolicy.swift +++ b/apple/TablePro/Core/Services/Infrastructure/AppActivationPolicy.swift @@ -28,10 +28,8 @@ internal enum AppActivationRole: Equatable, Sendable { /// The decisions, separated from acting on them so they can be tested without a running /// `NSApplication`. `AppActivationPolicyController` is the only place that applies them. internal enum AppActivationPolicyDecision { - /// `BackgroundLaunchFlag` is set by `MCPHandshakeAcquirer.launchHostApp` on the `open` process it - /// spawns, and read here before `NSApplicationMain` runs. That timing is the whole point: - /// deciding any later, even in `application(_:open:)`, still leaves the app registered with - /// LaunchServices as a foreground app for ~160ms, which is a Dock icon the person can see. + // Read before NSApplicationMain: deciding later, even in application(_:open:), leaves the app + // registered as a foreground app for about 160 ms, a Dock icon the person can see. internal static func launchOrigin(for environment: [String: String]) -> AppSessionOrigin { BackgroundLaunchFlag.isSet(in: environment) ? .machine : .user } diff --git a/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift b/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift index 00f16a5fc6..cd37fc9f49 100644 --- a/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift +++ b/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift @@ -1,8 +1,3 @@ -// -// CommandLineShim.swift -// TablePro -// - import Foundation import TableProProcess @@ -12,22 +7,35 @@ internal enum CommandLineShimStatus: Equatable, Sendable { case otherFile } -/// The `tablepro` shim ships in the bundle and the person links it onto their PATH with a command -/// they run themselves, so the app never writes outside its own container or asks for admin rights. +// The person runs the link line themselves, so the app never writes outside its bundle or asks for admin rights. internal struct CommandLineShim: Sendable { internal static let linkPath = "/usr/local/bin/tablepro" internal let shimPath: String internal let linkPath: String + private let readOnlyVolumeCheck: @Sendable (URL) -> Bool - internal init(bundle: Bundle = .main, linkPath: String = CommandLineShim.linkPath) { + internal init( + bundle: Bundle = .main, + linkPath: String = CommandLineShim.linkPath, + isOnReadOnlyVolume: @escaping @Sendable (URL) -> Bool = { CommandLineShim.isOnReadOnlyVolume($0) } + ) { let sharedSupport = bundle.sharedSupportURL ?? bundle.bundleURL.appendingPathComponent("Contents/SharedSupport") - self.init(shimPath: sharedSupport.appendingPathComponent("bin/tablepro").path, linkPath: linkPath) + self.init( + shimPath: sharedSupport.appendingPathComponent("bin/tablepro").path, + linkPath: linkPath, + isOnReadOnlyVolume: isOnReadOnlyVolume + ) } - internal init(shimPath: String, linkPath: String) { + internal init( + shimPath: String, + linkPath: String, + isOnReadOnlyVolume: @escaping @Sendable (URL) -> Bool = { CommandLineShim.isOnReadOnlyVolume($0) } + ) { self.shimPath = shimPath self.linkPath = linkPath + self.readOnlyVolumeCheck = isOnReadOnlyVolume } internal func status(fileManager: FileManager = .default) -> CommandLineShimStatus { @@ -40,8 +48,17 @@ internal struct CommandLineShim: Sendable { return fileManager.fileExists(atPath: linkPath) ? .otherFile : .notLinked } - /// `rm -f` first: `ln -sf` onto a link to a directory writes inside that directory, and `rm` - /// refuses a real directory, so the line stops instead of touching one. + // A disk image or a translocated copy is read-only and gone after eject or relaunch, so a link to it dangles. + internal func command(for status: CommandLineShimStatus) -> String? { + switch status { + case .linked: + unlinkCommand + case .notLinked, .otherFile: + readOnlyVolumeCheck(URL(fileURLWithPath: shimPath)) ? nil : linkCommand + } + } + + // rm -f first: ln -sf onto a link to a directory writes inside it, and rm refuses a real directory. internal var linkCommand: String { let directory = (linkPath as NSString).deletingLastPathComponent let link = Self.quote(linkPath) @@ -56,4 +73,8 @@ internal struct CommandLineShim: Sendable { internal static func quote(_ value: String) -> String { (try? ShellQuoting.singleQuoted(value)) ?? ShellQuoting.displayWord(value) } + + internal static func isOnReadOnlyVolume(_ url: URL) -> Bool { + (try? url.resourceValues(forKeys: [.volumeIsReadOnlyKey]).volumeIsReadOnly) ?? false + } } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 8ecb808b3c..56f90e9981 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -16501,6 +16501,9 @@ } } } + }, + "A plan works on up to 3 devices at a time. Removing one signs TablePro out there." : { + }, "A point's id cannot change. Insert a new row with the new id instead." : { "extractionState" : "manual", @@ -16508,9 +16511,6 @@ }, "A publish to the team library is still running. Try again when it finishes." : { - }, - "A plan works on up to 3 devices at a time. Removing one signs TablePro out there." : { - }, "a query" : { "localizations" : { @@ -55239,6 +55239,9 @@ } } } + }, + "Couldn't Save the Revocation" : { + }, "Couldn't start" : { @@ -129341,6 +129344,9 @@ } } } + }, + "Opens database URLs and files, runs queries and exports tables from the terminal. Run `tablepro help` to see every command." : { + }, "Opens database URLs from the terminal, such as a DDEV project database." : { "localizations" : { @@ -168745,15 +168751,15 @@ } } } - }, - "Signing in with credentials from this Mac was not approved." : { - }, "Signing in" : { }, "Signing in by email? Type the 6-digit code from the email in the sign-in sheet. The link in the email opens your browser instead." : { + }, + "Signing in with credentials from this Mac was not approved." : { + }, "Signing in…" : { "localizations" : { @@ -180963,6 +180969,9 @@ }, "TablePro could not read your saved connections, so it did not save over them." : { + }, + "TablePro could not save the change to the Keychain. The token works again after TablePro restarts." : { + }, "TablePro could not tell how this Dameng server treats backslashes, so it will not send a value that contains one. Reconnect, then try again." : { "extractionState" : "manual", @@ -181546,6 +181555,9 @@ "TablePro is reading or writing rows on this connection with settings of its own." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "TablePro is running from a disk image or a temporary location. Move it to the Applications folder first, then open it from there." : { + }, "TablePro is still trying to reconnect." : { "localizations" : { @@ -194656,9 +194668,6 @@ } } } - }, - "The text at byte %lld is not valid UTF-8." : { - }, "The team library asked TablePro to download it again." : { @@ -194674,6 +194683,9 @@ }, "The team library refused the change. It may be too large or hold a field the library never stores." : { + }, + "The text at byte %lld is not valid UTF-8." : { + }, "The text could not be parsed as JSON." : { "localizations" : { @@ -217859,6 +217871,15 @@ } } } + }, + "You were signed out. Sign in again." : { + + }, + "You're on Free" : { + + }, + "You're signed in" : { + }, "You've used TablePro on %lld days" : { "localizations" : { @@ -217899,15 +217920,6 @@ } } } - }, - "You were signed out. Sign in again." : { - - }, - "You're on Free" : { - - }, - "You're signed in" : { - }, "Your account is not allowed to read the source of %@" : { "extractionState" : "manual", diff --git a/apple/TablePro/Resources/ThirdPartyLicenses/licenses.yml b/apple/TablePro/Resources/ThirdPartyLicenses/licenses.yml index ece2ba0632..ad828ffcdf 100644 --- a/apple/TablePro/Resources/ThirdPartyLicenses/licenses.yml +++ b/apple/TablePro/Resources/ThirdPartyLicenses/licenses.yml @@ -655,6 +655,21 @@ SQLite and libSQL driver plugins, with its symbols hidden. The app itself still uses the macOS libsqlite3 for its own storage. Public domain; the SPDX id "blessing" names the notice the sources carry in place of a license.' +- id: swift-argument-parser + name: swift-argument-parser + version: 1.8.2 + spdx: Apache-2.0 WITH Swift-exception + copyrights: + - Copyright (c) 2020 Apple Inc. and the Swift project authors + homepageURL: https://github.com/apple/swift-argument-parser + licenseTextURL: https://github.com/apple/swift-argument-parser/blob/1.8.2/LICENSE.txt + textFile: texts/swift-argument-parser.txt + source: spm:swift-argument-parser + patched: false + platforms: [macos] + notes: '11751 bytes: Apache 2.0 plus the Runtime Library Exception section (verified by reading the + file). No NOTICE.txt. Linked only into Contents/MacOS/tablepro-cli through TableProCommandLine; the + app itself does not link it. Copyright line taken from the manifest header.' - id: swift-asn1 name: swift-asn1 version: 1.5.1, 1.7.1 diff --git a/apple/TablePro/Resources/ThirdPartyLicenses/texts/swift-argument-parser.txt b/apple/TablePro/Resources/ThirdPartyLicenses/texts/swift-argument-parser.txt new file mode 100644 index 0000000000..65aa1ded6a --- /dev/null +++ b/apple/TablePro/Resources/ThirdPartyLicenses/texts/swift-argument-parser.txt @@ -0,0 +1,211 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + +## Runtime Library Exception to the Apache 2.0 License: ## + + + As an exception, if you use this Software to compile your source code and + portions of this Software are embedded into the binary product as a result, + you may redistribute such product without providing attribution as would + otherwise be required by Sections 4(a), 4(b) and 4(d) of the License. diff --git a/apple/TablePro/Views/Integrations/IntegrationsConnectedClientsPane.swift b/apple/TablePro/Views/Integrations/IntegrationsConnectedClientsPane.swift index 136e22745c..6e75c0694c 100644 --- a/apple/TablePro/Views/Integrations/IntegrationsConnectedClientsPane.swift +++ b/apple/TablePro/Views/Integrations/IntegrationsConnectedClientsPane.swift @@ -9,6 +9,7 @@ struct IntegrationsConnectedClientsPane: View { @ObservedObject private var manager = MCPServerManager.shared @State private var selection: MCPServerManager.SessionSnapshot.ID? @State private var disconnectCandidate: MCPServerManager.SessionSnapshot? + @State private var revocationNotSaved = false @State private var sortOrder: [KeyPathComparator] = [ KeyPathComparator(\MCPServerManager.SessionSnapshot.connectedSince, order: .reverse) ] @@ -40,6 +41,11 @@ struct IntegrationsConnectedClientsPane: View { actions: alertActions, message: alertMessage ) + .alert(String(localized: "Couldn't Save the Revocation"), isPresented: $revocationNotSaved) { + Button(String(localized: "OK")) {} + } message: { + Text(String(localized: "TablePro could not save the change to the Keychain. The token works again after TablePro restarts.")) + } } private var sortedClients: [MCPServerManager.SessionSnapshot] { @@ -68,7 +74,11 @@ struct IntegrationsConnectedClientsPane: View { disconnectCandidate = nil } Button(String(localized: "Disconnect"), role: .destructive) { - Task { await manager.disconnectClient(client.id) } + Task { + if !(await manager.disconnectClient(client.id)) { + revocationNotSaved = true + } + } disconnectCandidate = nil } } diff --git a/apple/TablePro/Views/Settings/Sections/CommandLineToolSection.swift b/apple/TablePro/Views/Settings/Sections/CommandLineToolSection.swift index b13ec1fd10..5cda970d70 100644 --- a/apple/TablePro/Views/Settings/Sections/CommandLineToolSection.swift +++ b/apple/TablePro/Views/Settings/Sections/CommandLineToolSection.swift @@ -1,8 +1,3 @@ -// -// CommandLineToolSection.swift -// TablePro -// - import AppKit import SwiftUI @@ -10,9 +5,11 @@ struct CommandLineToolSection: View { private let tool: CommandLineShim @State private var status: CommandLineShimStatus = .notLinked + @State private var command: String? init(tool: CommandLineShim = CommandLineShim()) { self.tool = tool + _command = State(initialValue: tool.linkCommand) } var body: some View { @@ -31,12 +28,14 @@ struct CommandLineToolSection: View { .font(.caption) .foregroundStyle(.secondary) - CopyableCodeBlock(text: status == .linked ? tool.unlinkCommand : tool.linkCommand) + if let command { + CopyableCodeBlock(text: command) + } } } header: { Text("Command Line") } footer: { - Text("Opens database URLs from the terminal, such as a DDEV project database.") + Text("Opens database URLs and files, runs queries and exports tables from the terminal. Run `tablepro help` to see every command.") } .onAppear(perform: syncStatus) .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in @@ -56,17 +55,21 @@ struct CommandLineToolSection: View { } private var instruction: String { + guard command != nil else { + return String(localized: "TablePro is running from a disk image or a temporary location. Move it to the Applications folder first, then open it from there.") + } switch status { case .linked: - String(localized: "To remove it, run this in Terminal:") + return String(localized: "To remove it, run this in Terminal:") case .notLinked: - String(localized: "To add it to your PATH, run this in Terminal:") + return String(localized: "To add it to your PATH, run this in Terminal:") case .otherFile: - String(localized: "To replace that file with a link to this app, run this in Terminal:") + return String(localized: "To replace that file with a link to this app, run this in Terminal:") } } private func syncStatus() { status = tool.status() + command = tool.command(for: status) } } diff --git a/apple/TablePro/Views/Settings/Sections/MCPSection.swift b/apple/TablePro/Views/Settings/Sections/MCPSection.swift index cb74a2c5c1..f86c8a8841 100644 --- a/apple/TablePro/Views/Settings/Sections/MCPSection.swift +++ b/apple/TablePro/Views/Settings/Sections/MCPSection.swift @@ -11,6 +11,7 @@ struct MCPSection: View { @State private var showRevealSheet = false @State private var revealedToken: MCPAuthToken? @State private var revealedPlaintext: String = "" + @State private var revocationNotSaved = false var body: some View { Section { @@ -104,7 +105,14 @@ struct MCPSection: View { MCPTokenListView( tokens: tokenList, onGenerate: { showCreateSheet = true }, - onRevoke: { id in Task { await manager.tokenStore?.revoke(tokenId: id); await refreshTokens() } }, + onRevoke: { id in + Task { + if !(await manager.revokeToken(id: id)) { + revocationNotSaved = true + } + await refreshTokens() + } + }, onDelete: { id in Task { await manager.tokenStore?.delete(tokenId: id); await refreshTokens() } } ) } @@ -121,6 +129,11 @@ struct MCPSection: View { ) } } + .alert(String(localized: "Couldn't Save the Revocation"), isPresented: $revocationNotSaved) { + Button(String(localized: "OK")) {} + } message: { + Text(String(localized: "TablePro could not save the change to the Keychain. The token works again after TablePro restarts.")) + } } private var helpSection: some View { diff --git a/apple/TableProTests/Core/MCP/Helpers/MCPTransportTestStubs.swift b/apple/TableProTests/Core/MCP/Helpers/MCPTransportTestStubs.swift index fba777f59b..8f74343ddb 100644 --- a/apple/TableProTests/Core/MCP/Helpers/MCPTransportTestStubs.swift +++ b/apple/TableProTests/Core/MCP/Helpers/MCPTransportTestStubs.swift @@ -28,6 +28,12 @@ actor StubAlwaysAllowAuthenticator: MCPAuthenticator { } } +struct StubUnknownTokenRevoker: MCPTokenRevoking { + func revoke(presentedToken: String) async -> MCPTokenRevocationOutcome { + .unknown + } +} + actor StubBearerAuthenticator: MCPAuthenticator { private let validToken: String private let principal: MCPPrincipal @@ -419,6 +425,7 @@ enum MCPTransportTestHarness { port requestedPort: UInt16 = 0, authenticator: any MCPAuthenticator = StubAlwaysAllowAuthenticator(), pairingService: MCPPairingService? = nil, + tokenRevoker: any MCPTokenRevoking = StubUnknownTokenRevoker(), limits: MCPHttpServerLimits = .standard, clock: any MCPClock = MCPSystemClock(), timeout: Duration = .seconds(5) @@ -434,6 +441,7 @@ enum MCPTransportTestHarness { configuration: MCPHttpServerConfiguration.loopback(port: requestedPort, limits: limits), authenticator: authenticator, pairingService: pairing, + tokenRevoker: tokenRevoker, clock: clock ) try await transport.start() @@ -467,6 +475,7 @@ enum MCPTransportTestHarness { port requestedPort: UInt16 = 0, authenticator: any MCPAuthenticator = StubAlwaysAllowAuthenticator(), pairingService: MCPPairingService? = nil, + tokenRevoker: any MCPTokenRevoking = StubUnknownTokenRevoker(), limits: MCPHttpServerLimits = .standard, handler: @escaping @Sendable (MCPInboundExchange) async -> Void = MCPTransportTestHandlers.echo, body: (UInt16) async throws -> Void @@ -475,6 +484,7 @@ enum MCPTransportTestHarness { port: requestedPort, authenticator: authenticator, pairingService: pairingService, + tokenRevoker: tokenRevoker, limits: limits ) let consumer = StubExchangeConsumer() diff --git a/apple/TableProTests/Core/MCP/Integration/MCPBridgeIntegrationTests.swift b/apple/TableProTests/Core/MCP/Integration/MCPBridgeIntegrationTests.swift index e3e4983e1d..e5d9c2fabb 100644 --- a/apple/TableProTests/Core/MCP/Integration/MCPBridgeIntegrationTests.swift +++ b/apple/TableProTests/Core/MCP/Integration/MCPBridgeIntegrationTests.swift @@ -1,3 +1,4 @@ +import Darwin import Foundation import TableProMCP import TableProMCPTestSupport @@ -336,6 +337,7 @@ final class MCPBridgeIntegrationTests: XCTestCase { private func startBridge(socketPath: String? = nil) async throws -> BridgeHarness { let credentials = MCPUpstreamCredentials( socketPath: socketPath ?? upstream.socketPath, + serverPID: getpid(), bearerToken: "integration-token" ) let transport = MCPStreamableHttpClientTransport( diff --git a/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift b/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift index 395c80f377..1027f84e81 100644 --- a/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift +++ b/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift @@ -21,11 +21,25 @@ import Testing private final class InMemoryCredentialStore: MCPTokenCredentialStoring, @unchecked Sendable { private let lock = NSLock() private var stored: Data? + private var refusesWrites = false init(seed: Data? = Data("[]".utf8)) { self.stored = seed } + var failsWrites: Bool { + get { + lock.lock() + defer { lock.unlock() } + return refusesWrites + } + set { + lock.lock() + defer { lock.unlock() } + refusesWrites = newValue + } + } + func read() -> Data? { lock.lock() defer { lock.unlock() } @@ -36,6 +50,7 @@ private final class InMemoryCredentialStore: MCPTokenCredentialStoring, @uncheck func write(_ data: Data) -> Bool { lock.lock() defer { lock.unlock() } + guard !refusesWrites else { return false } stored = data return true } @@ -365,6 +380,81 @@ struct MCPTokenStoreTests { #expect(await recorder.keys().contains(key)) } + @Test("Revoking by id reports whether the Keychain took the change") + func revokeByIdReportsPersistence() async throws { + let credentials = InMemoryCredentialStore() + let store = makeStore(credentials) + let result = try await store.generate( + name: "settings", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + extraScopes: [] + ) + + credentials.failsWrites = true + #expect(await store.revoke(tokenId: result.token.id) == false) + #expect(await store.validate(bearerToken: result.plaintext) == nil) + + credentials.failsWrites = false + #expect(await store.revoke(tokenId: result.token.id)) + let reloaded = makeStore(credentials) + await reloaded.loadFromDisk() + #expect(await reloaded.token(id: result.token.id)?.isActive == false) + } + + @Test("A presented token whose revoke the Keychain refused reports it, and a retry saves it as already revoked") + func unsavedRevocationIsRetried() async throws { + let credentials = InMemoryCredentialStore() + let store = makeStore(credentials) + let result = try await store.generate( + name: "tablepro command", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + extraScopes: [] + ) + + credentials.failsWrites = true + #expect(await store.revoke(presentedToken: result.plaintext) == .notPersisted) + #expect(await store.validate(bearerToken: result.plaintext) == nil) + #expect(await store.revoke(presentedToken: result.plaintext) == .notPersisted) + + let afterFailure = makeStore(credentials) + await afterFailure.loadFromDisk() + #expect(await afterFailure.token(id: result.token.id)?.isActive == true) + + credentials.failsWrites = false + #expect(await store.revoke(presentedToken: result.plaintext) == .alreadyRevoked) + + let afterRetry = makeStore(credentials) + await afterRetry.loadFromDisk() + #expect(await afterRetry.token(id: result.token.id)?.isActive == false) + #expect(await afterRetry.validate(bearerToken: result.plaintext) == nil) + } + + @Test("A presented active token whose revoke the Keychain took reports revoked") + func savedRevocationReportsRevoked() async throws { + let credentials = InMemoryCredentialStore() + let store = makeStore(credentials) + let result = try await store.generate( + name: "tablepro command", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + extraScopes: [] + ) + + #expect(await store.revoke(presentedToken: result.plaintext) == .revoked) + + let reloaded = makeStore(credentials) + await reloaded.loadFromDisk() + #expect(await reloaded.token(id: result.token.id)?.isActive == false) + } + @Test("Deleting drops the token from the list entirely") func deleteRemovesTheToken() async throws { let store = makeStore() diff --git a/apple/TableProTests/Core/MCP/Transport/MCPTokenRevocationRouteTests.swift b/apple/TableProTests/Core/MCP/Transport/MCPTokenRevocationRouteTests.swift new file mode 100644 index 0000000000..e1d9bd5444 --- /dev/null +++ b/apple/TableProTests/Core/MCP/Transport/MCPTokenRevocationRouteTests.swift @@ -0,0 +1,216 @@ +import Darwin +import Foundation +@testable import TablePro +import TableProMCP +import TableProMCPTestSupport +import Testing + +private final class MemoryTokenCredentials: MCPTokenCredentialStoring, @unchecked Sendable { + private let lock = NSLock() + private var stored: Data? = Data("[]".utf8) + private var refusesWrites = false + + var failsWrites: Bool { + get { + lock.lock() + defer { lock.unlock() } + return refusesWrites + } + set { + lock.lock() + defer { lock.unlock() } + refusesWrites = newValue + } + } + + func read() -> Data? { + lock.lock() + defer { lock.unlock() } + return stored + } + + @discardableResult + func write(_ data: Data) -> Bool { + lock.lock() + defer { lock.unlock() } + guard !refusesWrites else { return false } + stored = data + return true + } + + func delete() { + lock.lock() + defer { lock.unlock() } + stored = nil + } +} + +private struct RevocationServer { + let transport: MCPHttpServerTransport + let socketPath: String + let store: MCPTokenStore + let credentials: MemoryTokenCredentials + + func revoke(form: String) async throws -> (status: Int, error: String?) { + let request = MCPSocketHttpRequest( + path: "/v1/integrations/revoke", + headers: [("Content-Type", "application/x-www-form-urlencoded")], + body: Data(form.utf8) + ) + let response = try await MCPUnixSocketHttpClient.send(request, socketPath: socketPath, expectedPID: getpid(), idleTimeout: .seconds(10)) + let body = try await response.collectBody() + let object = try JSONSerialization.jsonObject(with: body) as? [String: Any] + return (response.status, object?["error"] as? String) + } + + func mint(bridge: Bool = false) async throws -> (id: UUID, plaintext: String) { + let minted = try await store.generate( + name: bridge ? MCPTokenStore.stdioBridgeTokenName : "tablepro command", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: bridge, + extraScopes: [] + ) + return (minted.token.id, minted.plaintext) + } + + func isActive(_ id: UUID) async -> Bool? { + await store.token(id: id)?.isActive + } +} + +private func withRevocationServer(body: (RevocationServer) async throws -> Void) async throws { + let folder = try MCPTestSocketFolder.make() + defer { try? FileManager.default.removeItem(at: folder) } + let socketPath = MCPSocketLocation.primarySocket(in: folder).path + let credentials = MemoryTokenCredentials() + let store = MCPTokenStore(credentialStore: credentials) + let transport = MCPHttpServerTransport( + configuration: .loopback(port: 0, socketPath: socketPath), + authenticator: StubAlwaysAllowAuthenticator(), + pairingService: await MCPPairingService(), + tokenRevoker: store + ) + try await transport.start() + do { + try await body(RevocationServer(transport: transport, socketPath: socketPath, store: store, credentials: credentials)) + } catch { + await MCPTransportTestHarness.stop(transport) + throw error + } + await MCPTransportTestHarness.stop(transport) +} + +struct MCPTokenRevocationRouteTests { + @Test("A paired token presented over the socket is revoked, and presenting it again still answers 200") + func revokesAPairedToken() async throws { + try await withRevocationServer { server in + let paired = try await server.mint() + let other = try await server.mint() + + let first = try await server.revoke(form: "token=\(paired.plaintext)&token_type_hint=refresh_token") + #expect(first.status == 200) + #expect(await server.isActive(paired.id) == false) + #expect(await server.store.validate(bearerToken: paired.plaintext)?.id == nil) + #expect(await server.isActive(other.id) == true) + + let again = try await server.revoke(form: "token=\(paired.plaintext)") + #expect(again.status == 200) + } + } + + @Test("A revocation the Keychain did not take answers 503 temporarily_unavailable, and a retry once it saves answers 200") + func unsavedRevocationAnswers503() async throws { + try await withRevocationServer { server in + let paired = try await server.mint() + + server.credentials.failsWrites = true + let unsaved = try await server.revoke(form: "token=\(paired.plaintext)") + #expect(unsaved.status == 503) + #expect(unsaved.error == "temporarily_unavailable") + + server.credentials.failsWrites = false + let retried = try await server.revoke(form: "token=\(paired.plaintext)") + #expect(retried.status == 200) + #expect(retried.error == nil) + #expect(await server.isActive(paired.id) == false) + } + } + + @Test("An unknown token answers 200 and revokes nothing") + func unknownTokenAnswersOk() async throws { + try await withRevocationServer { server in + let paired = try await server.mint() + + let response = try await server.revoke(form: "token=tp_never-issued") + + #expect(response.status == 200) + #expect(response.error == nil) + #expect(await server.isActive(paired.id) == true) + } + } + + @Test("The shared bridge credential is refused as an unsupported token type and stays active") + func refusesTheBridgeCredential() async throws { + try await withRevocationServer { server in + let bridge = try await server.mint(bridge: true) + + let response = try await server.revoke(form: "token=\(bridge.plaintext)") + + #expect(response.status == 400) + #expect(response.error == "unsupported_token_type") + #expect(await server.isActive(bridge.id) == true) + } + } + + @Test("A request without exactly one token is refused as invalid_request", arguments: [ + "", + "token=", + "token_type_hint=access_token", + "token=tp_a&token=tp_b", + "token=%zz" + ]) + func missingTokenIsInvalidRequest(form: String) async throws { + try await withRevocationServer { server in + let response = try await server.revoke(form: form) + + #expect(response.status == 400) + #expect(response.error == "invalid_request") + } + } + + @Test("Over the TCP port the route does not exist, and the token stays active") + func tcpDoesNotServeTheRoute() async throws { + try await withRevocationServer { server in + let paired = try await server.mint() + let port = try #require(await server.transport.listeningPort) + let client = RawHttpTestClient(port: port) + try await client.connect() + defer { Task { await client.close() } } + + try await client.send(MCPTransportTestRequests.raw( + path: "/v1/integrations/revoke", + port: port, + headers: [("Content-Type", "application/x-www-form-urlencoded")], + body: Data("token=\(paired.plaintext)".utf8) + )) + let response = try await client.readResponse() + + #expect(response.statusCode == 404) + #expect(await server.isActive(paired.id) == true) + } + } + + @Test("The form parser decodes the token and ignores other parameters") + func formParserDecodesTheToken() { + let token = MCPHttpRequestRouter.revocationToken(fromForm: Data("token_type_hint=x&token=tp%5Fab+c".utf8)) + #expect(token == "tp_ab c") + } + + @Test("A token longer than the size cap is refused") + func oversizedTokenIsRefused() { + let long = String(repeating: "a", count: MCPHttpRequestRouter.maxRevocationTokenBytes + 1) + #expect(MCPHttpRequestRouter.revocationToken(fromForm: Data("token=\(long)".utf8)) == nil) + } +} diff --git a/apple/TableProTests/Core/MCP/Transport/MCPUnixSocketTransportTests.swift b/apple/TableProTests/Core/MCP/Transport/MCPUnixSocketTransportTests.swift index 780c6ee56f..b4cba9657a 100644 --- a/apple/TableProTests/Core/MCP/Transport/MCPUnixSocketTransportTests.swift +++ b/apple/TableProTests/Core/MCP/Transport/MCPUnixSocketTransportTests.swift @@ -46,6 +46,7 @@ private func withSocketServer( configuration: .loopback(port: 0, socketPath: socketPath, limits: limits), authenticator: authenticator, pairingService: pairing, + tokenRevoker: StubUnknownTokenRevoker(), allowedSocketPeerUid: allowedPeerUid ) try await transport.start() @@ -90,7 +91,7 @@ private func overTcp(_ request: Data, port: UInt16) async throws -> RawHttpTestR } private func overSocket(_ request: MCPSocketHttpRequest, path: String) async throws -> (Int, Data) { - let response = try await MCPUnixSocketHttpClient.send(request, socketPath: path, idleTimeout: .seconds(10)) + let response = try await MCPUnixSocketHttpClient.send(request, socketPath: path, expectedPID: getpid(), idleTimeout: .seconds(10)) return (response.status, try await response.collectBody()) } @@ -129,7 +130,7 @@ struct MCPUnixSocketTransportTests { let response = try await MCPUnixSocketHttpClient.send( try toolsList(), socketPath: server.socketPath, - idleTimeout: .seconds(10) + expectedPID: getpid(), idleTimeout: .seconds(10) ) #expect(response.status == 200) #expect(response.headers.value(for: "Content-Type") == "application/json") @@ -152,7 +153,7 @@ struct MCPUnixSocketTransportTests { _ = try await MCPUnixSocketHttpClient.send( try toolsList(), socketPath: server.socketPath, - idleTimeout: .seconds(10) + expectedPID: getpid(), idleTimeout: .seconds(10) ) } throws: { error in switch error { @@ -181,7 +182,7 @@ struct MCPUnixSocketTransportTests { let response = try await MCPUnixSocketHttpClient.send( try toolsList(), socketPath: server.socketPath, - idleTimeout: .seconds(10) + expectedPID: getpid(), idleTimeout: .seconds(10) ) #expect(response.status == 200) } @@ -197,7 +198,8 @@ struct MCPUnixSocketTransportTests { let transport = MCPHttpServerTransport( configuration: .loopback(port: 0, socketPath: socketPath), authenticator: StubAlwaysAllowAuthenticator(), - pairingService: await MCPPairingService() + pairingService: await MCPPairingService(), + tokenRevoker: StubUnknownTokenRevoker() ) await #expect(throws: MCPHttpServerError.self) { try await transport.start() diff --git a/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift b/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift index 4c83397a00..9459e9d234 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift @@ -9,7 +9,10 @@ struct CommandLineShimTests { return path } - private func tool(in directory: String) throws -> CommandLineShim { + private func tool( + in directory: String, + isOnReadOnlyVolume: @escaping @Sendable (URL) -> Bool = { CommandLineShim.isOnReadOnlyVolume($0) } + ) throws -> CommandLineShim { let shim = (directory as NSString).appendingPathComponent("Tablé Pro's.app/bin/tablepro") try FileManager.default.createDirectory( atPath: (shim as NSString).deletingLastPathComponent, @@ -18,7 +21,8 @@ struct CommandLineShimTests { FileManager.default.createFile(atPath: shim, contents: Data("#!/bin/sh\n".utf8)) return CommandLineShim( shimPath: shim, - linkPath: (directory as NSString).appendingPathComponent("local/bin/tablepro") + linkPath: (directory as NSString).appendingPathComponent("local/bin/tablepro"), + isOnReadOnlyVolume: isOnReadOnlyVolume ) } @@ -31,13 +35,77 @@ struct CommandLineShimTests { return process.terminationStatus } - @Test("The app bundle carries an executable shim that opens TablePro by bundle id") - func bundleCarriesTheShim() throws { - let shim = CommandLineShim().shimPath - #expect(shim.hasSuffix("/Contents/SharedSupport/bin/tablepro")) - #expect(FileManager.default.isExecutableFile(atPath: shim)) - let contents = try String(contentsOfFile: shim, encoding: .utf8) - #expect(contents.contains("exec open -b com.TablePro \"$@\"")) + private func makeExecutable(atPath path: String, contents: String) throws { + try FileManager.default.createDirectory( + atPath: (path as NSString).deletingLastPathComponent, + withIntermediateDirectories: true + ) + FileManager.default.createFile(atPath: path, contents: Data(contents.utf8)) + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: path) + } + + private func runCapturingOutput(_ executable: String, arguments: [String]) throws -> (status: Int32, output: String) { + let process = Process() + process.executableURL = URL(fileURLWithPath: executable) + process.arguments = arguments + let pipe = Pipe() + process.standardOutput = pipe + try process.run() + let output = pipe.fileHandleForReading.readDataToEndOfFile() + process.waitUntilExit() + return (process.terminationStatus, String(decoding: output, as: UTF8.self)) + } + + @Test("Through a link, the bundled shim hands every argument to its own bundle's tablepro-cli") + func bundledShimRunsTheBundledCommand() throws { + let bundled = CommandLineShim().shimPath + #expect(bundled.hasSuffix("/Contents/SharedSupport/bin/tablepro")) + #expect(FileManager.default.isExecutableFile(atPath: bundled)) + + let directory = try makeDirectory() + let app = (directory as NSString).appendingPathComponent("Copy of TablePro.app/Contents") + let shim = (app as NSString).appendingPathComponent("SharedSupport/bin/tablepro") + try FileManager.default.createDirectory( + atPath: (shim as NSString).deletingLastPathComponent, + withIntermediateDirectories: true + ) + try FileManager.default.copyItem(atPath: bundled, toPath: shim) + try makeExecutable( + atPath: (app as NSString).appendingPathComponent("MacOS/tablepro-cli"), + contents: "#!/bin/sh\nprintf '%s\\n' \"$@\"\n" + ) + let link = (directory as NSString).appendingPathComponent("bin/tablepro") + try FileManager.default.createDirectory( + atPath: (link as NSString).deletingLastPathComponent, + withIntermediateDirectories: true + ) + try FileManager.default.createSymbolicLink(atPath: link, withDestinationPath: shim) + + for arguments in [["--help"], ["--version"], ["help"], ["query", "-c", "My DB", "SELECT 1"]] { + let result = try runCapturingOutput(link, arguments: arguments) + #expect(result.status == 0) + #expect(result.output == arguments.map { $0 + "\n" }.joined()) + } + } + + @Test("On a read-only volume no link line is offered, and a link already made can still be removed") + func readOnlyVolumeWithholdsTheLinkLine() throws { + let directory = try makeDirectory() + let shimPath = (directory as NSString).appendingPathComponent("Tablé Pro's.app/bin/tablepro") + let tool = try tool(in: directory, isOnReadOnlyVolume: { $0.path == shimPath }) + + #expect(tool.command(for: .notLinked) == nil) + #expect(tool.command(for: .otherFile) == nil) + #expect(tool.command(for: .linked) == tool.unlinkCommand) + } + + @Test("On a writable volume the link line is offered until the link exists") + func writableVolumeOffersTheLinkLine() throws { + let tool = try tool(in: try makeDirectory()) + + #expect(tool.command(for: .notLinked) == tool.linkCommand) + #expect(tool.command(for: .otherFile) == tool.linkCommand) + #expect(tool.command(for: .linked) == tool.unlinkCommand) } @Test("Nothing at the link path reads as not linked") diff --git a/apple/TestPlans/Unit.xctestplan b/apple/TestPlans/Unit.xctestplan index 49c3bc45d5..2ac2d49434 100644 --- a/apple/TestPlans/Unit.xctestplan +++ b/apple/TestPlans/Unit.xctestplan @@ -109,6 +109,13 @@ "name" : "TableProGrammarsTests" } }, + { + "target" : { + "containerPath" : "container:Packages\/TableProMacCore", + "identifier" : "TableProCommandLineTests", + "name" : "TableProCommandLineTests" + } + }, { "target" : { "containerPath" : "container:Packages\/TableProMacCore", diff --git a/apple/changelog.d/4145.added.1.md b/apple/changelog.d/4145.added.1.md new file mode 100644 index 0000000000..7128a1a730 --- /dev/null +++ b/apple/changelog.d/4145.added.1.md @@ -0,0 +1 @@ +tablepro pair and tablepro unpair pair the command with TablePro or revoke its token. diff --git a/apple/changelog.d/4145.added.2.md b/apple/changelog.d/4145.added.2.md new file mode 100644 index 0000000000..e569fa233c --- /dev/null +++ b/apple/changelog.d/4145.added.2.md @@ -0,0 +1 @@ +tablepro completion prints a completion script for zsh, bash and fish. diff --git a/apple/changelog.d/4145.added.md b/apple/changelog.d/4145.added.md new file mode 100644 index 0000000000..151101adfc --- /dev/null +++ b/apple/changelog.d/4145.added.md @@ -0,0 +1 @@ +The tablepro command lists databases, schemas and tables, describes and exports tables, and reports its status. diff --git a/apple/changelog.d/4145.changed.1.md b/apple/changelog.d/4145.changed.1.md new file mode 100644 index 0000000000..49e5883d50 --- /dev/null +++ b/apple/changelog.d/4145.changed.1.md @@ -0,0 +1 @@ +tablepro exits 2 for a target that is neither a URL nor a file; open a file named like a command with tablepro open. diff --git a/apple/changelog.d/4145.changed.2.md b/apple/changelog.d/4145.changed.2.md new file mode 100644 index 0000000000..05fb30fbf7 --- /dev/null +++ b/apple/changelog.d/4145.changed.2.md @@ -0,0 +1 @@ +tablepro opens files and links in the TablePro it ships in, and a hidden connection exits 2 like an unknown one. diff --git a/apple/changelog.d/4145.changed.md b/apple/changelog.d/4145.changed.md new file mode 100644 index 0000000000..60477c93be --- /dev/null +++ b/apple/changelog.d/4145.changed.md @@ -0,0 +1 @@ +SQL passed to tablepro query that starts with a dash now goes after --. diff --git a/apple/changelog.d/4145.fixed.1.md b/apple/changelog.d/4145.fixed.1.md new file mode 100644 index 0000000000..2a269f0fc2 --- /dev/null +++ b/apple/changelog.d/4145.fixed.1.md @@ -0,0 +1 @@ +tablepro query with TablePro closed could start another copy of TablePro, or the iPad app, and then time out. diff --git a/apple/changelog.d/4145.fixed.2.md b/apple/changelog.d/4145.fixed.2.md new file mode 100644 index 0000000000..c601ace06e --- /dev/null +++ b/apple/changelog.d/4145.fixed.2.md @@ -0,0 +1 @@ +Revoking an MCP token showed it as revoked even when TablePro could not save the change. diff --git a/apple/changelog.d/4145.fixed.3.md b/apple/changelog.d/4145.fixed.3.md new file mode 100644 index 0000000000..e4eb9a04f8 --- /dev/null +++ b/apple/changelog.d/4145.fixed.3.md @@ -0,0 +1 @@ +Settings offered a tablepro link line that broke once TablePro left its disk image. diff --git a/apple/changelog.d/4145.fixed.md b/apple/changelog.d/4145.fixed.md new file mode 100644 index 0000000000..3ae0e86ffa --- /dev/null +++ b/apple/changelog.d/4145.fixed.md @@ -0,0 +1 @@ +tablepro --help and tablepro --version opened TablePro instead of printing help or the version. diff --git a/apple/changelog.d/4145.security.1.md b/apple/changelog.d/4145.security.1.md new file mode 100644 index 0000000000..5210da1204 --- /dev/null +++ b/apple/changelog.d/4145.security.1.md @@ -0,0 +1 @@ +The tablepro command and the MCP bridge could send their token to a program that replaced TablePro's socket. diff --git a/apple/changelog.d/4145.security.md b/apple/changelog.d/4145.security.md new file mode 100644 index 0000000000..850f8ca55a --- /dev/null +++ b/apple/changelog.d/4145.security.md @@ -0,0 +1 @@ +tablepro export printed control codes stored in the database to the terminal, where they could run. diff --git a/apple/project.yml b/apple/project.yml index d16fbd0d11..41c0ec9abe 100644 --- a/apple/project.yml +++ b/apple/project.yml @@ -296,7 +296,7 @@ targets: excludes: # main() for the tablepro-mcp helper. - CLI/BridgeMain.swift - # main() for the tablepro-cli helper behind `tablepro query`. + # main() for the tablepro-cli helper behind the tablepro command. - CLI/CLIMain.swift # main() for the tablepro-imagerender helper, which is a whole executable on # its own: nothing in it is reachable from the app. @@ -530,7 +530,7 @@ targets: SWIFT_APPROACHABLE_CONCURRENCY: YES SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY: YES - # ─── tablepro query and tablepro connections, embedded as Contents/MacOS/tablepro-cli ─ + # ─── The tablepro command, embedded as Contents/MacOS/tablepro-cli ─────────── command-line: type: tool @@ -539,7 +539,7 @@ targets: - TablePro/CLI/CLIMain.swift dependencies: - package: TableProMacCore - products: [TableProMCP, TableProUtilities] + products: [TableProCommandLine] scheme: {} settings: base: diff --git a/core/AGENTS.md b/core/AGENTS.md index fe48bb58d8..46cfbbea1c 100644 --- a/core/AGENTS.md +++ b/core/AGENTS.md @@ -15,7 +15,7 @@ This is the guide for work under `core/`. The root `AGENTS.md` holds the rules e ## Map -- `crates/core`, `crates/net`, `crates/ssh`, `crates/storage`, `crates/session`, `crates/ai`, `crates/mcp`, `crates/account`: the libraries. `crates/drivers/*`: one crate per engine. `crates/ffi`: the library the Windows and Apple apps load (`ARCHITECTURE.md`, Bindings). `crates/bindgen`: the Swift binding generator. `crates/test-fixtures` and `crates/test-support`: test-only. +- `crates/core`, `crates/net`, `crates/ssh`, `crates/storage`, `crates/session`, `crates/ai`, `crates/mcp`, `crates/account`, `crates/cli`: the libraries. `crates/drivers/*`: one crate per engine. `crates/ffi`: the library the Windows and Apple apps load (`ARCHITECTURE.md`, Bindings). `crates/bindgen`: the Swift binding generator. `crates/test-fixtures` and `crates/test-support`: test-only. - `ARCHITECTURE.md`: the crates, the graph, the platform seams and the driver layer. `docs/decisions/`: the cross-platform decisions. - The Cargo workspace and its tool settings are at the top of the repository: `Cargo.toml`, `Cargo.lock`, `rust-toolchain.toml`, `rustfmt.toml`, `clippy.toml`, `deny.toml`, `.config/nextest.toml`. - Until another app builds the core, the Linux docs hold its contracts: `linux/docs/adding-drivers.md`, `linux/docs/drivers/`, `linux/docs/error-handling.md`, `linux/docs/testing.md` and `linux/docs/storage.md`. diff --git a/core/ARCHITECTURE.md b/core/ARCHITECTURE.md index 063dc18fc9..4d1731ca24 100644 --- a/core/ARCHITECTURE.md +++ b/core/ARCHITECTURE.md @@ -17,6 +17,7 @@ core/crates/ ├── ai/ the AI assistant's providers, prompts and replies ├── account/ the TablePro account: sign-in, device, entitlement and the feature gate ├── mcp/ the MCP server: transport, token and tools +├── cli/ the tablepro command line: parsing, the MCP client and the output formats ├── ffi/ the library C# and Swift load: one UniFFI API and the grid's C ABI ├── bindgen/ uniffi-bindgen-swift, which writes the Swift bindings from a built ffi ├── test-fixtures/ dev-only container fixtures with a generated PKI @@ -45,6 +46,7 @@ core/crates/ | `drivers/sqlite`, `drivers/duckdb` | `core` | | `session` | `core`, `net`, `ssh`, `storage` | | `mcp` | `core`, `session`, `storage` | +| `cli` | no workspace crate | | `ai` | `core`, `net` | | `account` | `core`, `storage` | | `ffi` | `core`, `session`, `ssh`, `storage`, and every driver behind its default `engines` feature | @@ -58,6 +60,7 @@ Rules, enforced by review: - Each `drivers/` crate depends on `core`, and on `net` when it speaks TLS or dials TCP. **Drivers never depend on each other, on `session` or on `ssh`.** - `session` depends on `core`, `net`, `ssh` and `storage`, and on no driver outside its tests: it reaches drivers through `DriverRegistry`, and SSH through the `SshTunnels` the app passes in. Its Docker tests take `tablepro-driver-postgres`, `tablepro-driver-redis` and `tablepro-driver-mongodb` as dev-dependencies. - `mcp` depends on `core`, `session` and `storage`, and on no driver outside its tests. It reaches the windows and the user through the `McpHost` trait the app implements. +- `cli` depends on no workspace crate: it speaks to the app's MCP server over HTTP and reaches the port, the token and the app's state through the `Host` trait the app implements ([0018](docs/decisions/0018-command-line.md)). - `ai` depends on `core` and `net`. It speaks the providers' protocols and builds the prompts from what the app hands it; the app decides what a prompt may carry. - `account` depends on `core` and `storage`. It reaches the browser, the keyring and the machine ID through what the app passes in ([0016](docs/decisions/0016-tablepro-accounts.md)). - `ffi` stands in for an app outside Rust: it names every driver and passes the host's services in. No other core crate depends on it. @@ -82,6 +85,7 @@ A value or a trait the app passes in: | `SshTunnels` | `tablepro_ssh::SshServices`, the system OpenSSH client | `tablepro_ssh::InProcessSsh`, behind `tablepro-session`'s `in-process-ssh` feature ([ADR 0009](docs/decisions/0009-in-process-ssh-on-windows.md)) | | `ProcessHold` | `gio::Application::hold` | none | | `RefreshTokenVault`, `BrowserLauncher`, `MachineId` and `AppClient` (`tablepro-account`) | the keyring through `SecretStore`, `gtk::UriLauncher`, `MachineId::derive` of what `tablepro_linux_platform::read_machine_id` reads, `AppClient::LINUX` | none yet | +| `Host` (`tablepro-cli`) | `LinuxHost` in the app crate: the port and switch from GSettings, the token read from the keyring, `NameHasOwner` for a running app, each connection's peer checked by `Host::verify_peer` through `tablepro_net::loopback_peer_is_ours` | none yet | | `ForeignAppDirs` and `ForeignHost` (`tablepro-foreign-import`) | `tablepro_linux_platform::foreign_app_dirs`, the host's XDG directories inside a Flatpak, and `PlainHost`: no other app's keychain | none yet | A `cfg` split inside a crate: diff --git a/core/crates/cli/Cargo.toml b/core/crates/cli/Cargo.toml new file mode 100644 index 0000000000..30438a1672 --- /dev/null +++ b/core/crates/cli/Cargo.toml @@ -0,0 +1,48 @@ +[package] +name = "tablepro-cli" +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +# No colour: output is never coloured, and the feature pulls in anstream. +# string: the version printed is the app's, known only at run time. +clap = { version = "4.6.7", default-features = false, features = [ + "std", + "derive", + "help", + "usage", + "error-context", + "suggestions", + "string", +] } +clap_complete = "4.6.11" +http.workspace = true +hyper-util.workspace = true +reqwest.workspace = true +tower = { version = "0.5", features = ["util"] } +rmcp = { version = "3.9.0", default-features = false, features = ["client", "transport-streamable-http-client-reqwest"] } +serde.workspace = true +serde_json.workspace = true +thiserror.workspace = true +tokio = { workspace = true, features = ["rt"] } +tracing.workspace = true +unicode-width.workspace = true + +[dev-dependencies] +tablepro-core.workspace = true +tablepro-driver-sqlite.workspace = true +tablepro-mcp.workspace = true +tablepro-session.workspace = true +tablepro-ssh.workspace = true +tablepro-storage.workspace = true +async-trait.workspace = true +secrecy.workspace = true +tempfile.workspace = true +tokio = { workspace = true, features = ["rt", "test-util"] } +tokio-util.workspace = true +uuid.workspace = true + +[lints] +workspace = true diff --git a/core/crates/cli/src/answers.rs b/core/crates/cli/src/answers.rs new file mode 100644 index 0000000000..ee6bcb3616 --- /dev/null +++ b/core/crates/cli/src/answers.rs @@ -0,0 +1,289 @@ +// What the tools in core/crates/mcp answer. Fields this command does not show are left out. + +use std::fmt; + +use serde::Deserialize; +use serde::de::{DeserializeOwned, Deserializer, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use serde_json::value::RawValue; + +use crate::command_error::CommandError; +use crate::output::{Cell, Table}; + +pub(crate) fn parse(text: &str) -> Result { + serde_json::from_str(text).map_err(|error| CommandError::Answer(error.to_string())) +} + +#[derive(Deserialize)] +pub(crate) struct ConnectionList { + pub(crate) connections: Vec, +} + +#[derive(Debug, Clone, Deserialize)] +pub(crate) struct ListedConnection { + pub(crate) id: String, + pub(crate) name: String, + #[serde(rename = "type")] + pub(crate) kind: String, + #[serde(default)] + pub(crate) is_open: bool, +} + +#[derive(Deserialize)] +pub(crate) struct ConnectAnswer { + pub(crate) status: String, +} + +// core/crates/mcp/src/collected_results.rs. This command says it in its own words, naming --limit. +const TRUNCATED_NOTE: &str = + "A result set has more rows than the limit; ask for fewer with a WHERE clause or raise max_rows."; + +#[derive(Deserialize)] +pub(crate) struct Results { + #[serde(default)] + pub(crate) result_sets: Vec, + pub(crate) rows_affected: Option, + #[serde(default)] + pub(crate) notices: Vec, + #[serde(default)] + notes: Vec, +} + +impl Results { + pub(crate) fn notes(&self) -> impl Iterator { + self.notes + .iter() + .map(String::as_str) + .filter(|note| *note != TRUNCATED_NOTE) + } +} + +#[derive(Deserialize)] +pub(crate) struct ResultSet { + columns: Vec, + rows: Vec, + #[serde(default)] + pub(crate) truncated: bool, +} + +#[derive(Deserialize)] +struct ResultColumn { + name: String, +} + +impl ResultSet { + pub(crate) fn to_table(&self) -> Table { + Table { + columns: self.columns.iter().map(|column| column.name.clone()).collect(), + rows: self.rows.iter().map(|row| row.0.clone()).collect(), + } + } +} + +// A row object lists its keys in column order, so its values are read by position: +// a key is the export's unique name, which can differ from the column's. +struct Row(Vec); + +impl<'de> Deserialize<'de> for Row { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_any(RowVisitor) + } +} + +struct RowVisitor; + +impl<'de> Visitor<'de> for RowVisitor { + type Value = Row; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a row object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut cells = Vec::new(); + while map.next_key::()?.is_some() { + let value: Box = map.next_value()?; + cells.push(Cell::from_raw(&value)); + } + Ok(Row(cells)) + } + + fn visit_seq>(self, mut seq: A) -> Result { + let mut cells = Vec::new(); + while let Some(value) = seq.next_element::>()? { + cells.push(Cell::from_raw(&value)); + } + Ok(Row(cells)) + } +} + +#[derive(Deserialize)] +pub(crate) struct DatabaseList { + pub(crate) databases: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct ListedDatabase { + pub(crate) name: String, + #[serde(default)] + pub(crate) is_current: bool, +} + +#[derive(Deserialize)] +pub(crate) struct SchemaList { + pub(crate) schemas: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct ListedSchema { + pub(crate) name: String, + #[serde(default)] + pub(crate) is_default: bool, + #[serde(default)] + pub(crate) is_system: bool, +} + +#[derive(Deserialize)] +pub(crate) struct TableList { + pub(crate) tables: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct ListedTable { + pub(crate) name: String, + pub(crate) schema: Option, + pub(crate) kind: String, +} + +#[derive(Deserialize)] +pub(crate) struct TableDescription { + pub(crate) columns: Vec, + #[serde(default)] + pub(crate) indexes: Vec, + #[serde(default)] + pub(crate) foreign_keys: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct DescribedColumn { + pub(crate) name: String, + pub(crate) data_type: String, + #[serde(default)] + pub(crate) is_nullable: bool, + #[serde(default)] + pub(crate) is_primary_key: bool, + #[serde(default)] + pub(crate) is_auto_increment: bool, + #[serde(default)] + pub(crate) is_generated: bool, + pub(crate) default_value: Option, + pub(crate) comment: Option, +} + +#[derive(Deserialize)] +pub(crate) struct DescribedIndex { + pub(crate) name: String, + pub(crate) columns: Vec, + #[serde(default)] + pub(crate) is_unique: bool, + #[serde(default)] + pub(crate) is_primary: bool, + pub(crate) predicate: Option, +} + +#[derive(Deserialize)] +pub(crate) struct DescribedForeignKey { + pub(crate) name: String, + pub(crate) columns: Vec, + pub(crate) referenced_schema: Option, + pub(crate) referenced_table: String, + pub(crate) referenced_columns: Vec, + pub(crate) on_delete: Option, + pub(crate) on_update: Option, +} + +#[derive(Deserialize)] +pub(crate) struct TableDdl { + pub(crate) ddl: String, +} + +#[derive(Deserialize)] +pub(crate) struct ExportAnswer { + pub(crate) exports: Vec, + #[serde(default)] + pub(crate) notes: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct Export { + pub(crate) table: String, + pub(crate) row_count: usize, + #[serde(default)] + pub(crate) truncated: bool, + pub(crate) data: String, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_result_set_reads_its_rows_by_position_with_exact_numbers() { + let text = r#"{"result_sets":[{"columns":[{"name":"id","type":"bigint"},{"name":"id","type":"text"}], + "rows":[{"id":18446744073709551615,"id_2":"x"},{"id":null,"id_2":""}],"row_count":2,"truncated":true}], + "execution_time_ms":3}"#; + + let results: Results = parse(text).expect("a results document"); + + let set = &results.result_sets[0]; + assert!(set.truncated); + assert_eq!( + set.to_table(), + Table { + columns: vec!["id".to_owned(), "id".to_owned()], + rows: vec![ + vec![Cell::literal("18446744073709551615"), Cell::text("x")], + vec![Cell::Null, Cell::text("")], + ], + } + ); + assert_eq!(results.rows_affected, None); + } + + #[test] + fn a_write_reads_its_rows_affected() { + let results: Results = + parse(r#"{"result_sets":[],"rows_affected":2,"execution_time_ms":1}"#).expect("a results document"); + + assert!(results.result_sets.is_empty()); + assert_eq!(results.rows_affected, Some(2)); + } + + #[test] + fn notes_are_read_and_the_row_limit_one_is_left_to_this_command() { + let results: Results = parse( + r#"{"result_sets":[],"execution_time_ms":1,"notes":["Some values could not be decoded by TablePro and are shown as null.", + "A result set has more rows than the limit; ask for fewer with a WHERE clause or raise max_rows."]}"#, + ) + .expect("a results document"); + let export: ExportAnswer = parse( + r#"{"format":"csv","exports":[],"notes":["Some values could not be decoded by TablePro and are written as null."]}"#, + ) + .expect("an export document"); + + assert_eq!( + results.notes().collect::>(), + ["Some values could not be decoded by TablePro and are shown as null."] + ); + assert_eq!( + export.notes, + ["Some values could not be decoded by TablePro and are written as null."] + ); + } + + #[test] + fn an_answer_of_another_shape_is_reported() { + let refused = parse::("not json"); + + assert!(matches!(refused, Err(CommandError::Answer(_)))); + } +} diff --git a/core/crates/cli/src/client.rs b/core/crates/cli/src/client.rs new file mode 100644 index 0000000000..7b13ab167a --- /dev/null +++ b/core/crates/cli/src/client.rs @@ -0,0 +1,371 @@ +use std::error::Error; +use std::time::Duration; + +use rmcp::model::{ + CallToolRequest, CallToolRequestParams, ClientCapabilities, ClientConfig, ClientRequest, ContentBlock, + Implementation, ServerResult, +}; +use rmcp::service::{ClientInitializeError, PeerRequestOptions, RunningService}; +use rmcp::transport::StreamableHttpClientTransport; +use rmcp::transport::streamable_http_client::{ + AuthRequiredError, StreamableHttpClientTransportConfig, StreamableHttpError, +}; +use rmcp::{RoleClient, ServiceError, ServiceExt}; +use serde::Serialize; + +use crate::command_error::CommandError; +use crate::endpoint::{Endpoint, endpoint}; +use crate::host::{Host, HostError}; +use crate::peer_verification::http_client; + +const CLIENT_NAME: &str = "tablepro-cli"; +const CLIENT_TITLE: &str = "Command Line"; +const SERVER_NAME: &str = "tablepro"; +// core/crates/mcp/src/query_tools.rs, DEFAULT_TIMEOUT_SECONDS. +const SERVER_STATEMENT_SECONDS: u32 = 30; +// Past a deadline rmcp tells the server to drop the call; this bounds a server too stalled to take that. +const CANCEL_GRACE: Duration = Duration::from_secs(2); + +#[derive(Debug, Clone, Copy)] +pub(crate) struct Deadlines { + pub(crate) handshake: Duration, + pub(crate) tool: Duration, + pub(crate) statement_margin: Duration, +} + +impl Deadlines { + // The handshake waits on no window and no database. Other tools wait on the app's windows and on reads + // the server stops at 15 s, connect at 10 s. A statement may first wait on the user's answer to a write. + pub(crate) const TABLEPRO: Self = Self { + handshake: Duration::from_secs(15), + tool: Duration::from_secs(60), + statement_margin: Duration::from_secs(120), + }; + + pub(crate) fn statement(&self, timeout_seconds: Option) -> Duration { + Duration::from_secs(u64::from(timeout_seconds.unwrap_or(SERVER_STATEMENT_SECONDS))) + .saturating_add(self.statement_margin) + } + + // The server reads each table on its own, under the bound one tool call has. + pub(crate) fn tables(&self, tables: usize) -> Duration { + self.tool + .saturating_mul(u32::try_from(tables.max(1)).unwrap_or(u32::MAX)) + } +} + +pub(crate) struct McpClient { + service: RunningService, + port: u16, + deadlines: Deadlines, +} + +impl McpClient { + pub(crate) async fn connect(host: &(impl Host + Clone + Send + Sync + 'static)) -> Result { + Self::connect_to(&endpoint(host)?, &host.app_version(), host).await + } + + pub(crate) async fn connect_to( + endpoint: &Endpoint, + version: &str, + host: &(impl Host + Clone + Send + Sync + 'static), + ) -> Result { + Self::connect_within(endpoint, version, Deadlines::TABLEPRO, host).await + } + + pub(crate) async fn connect_within( + endpoint: &Endpoint, + version: &str, + deadlines: Deadlines, + host: &(impl Host + Clone + Send + Sync + 'static), + ) -> Result { + let port = endpoint.port; + let http = http_client(host, port).map_err(|error| CommandError::Transport(error.to_string()))?; + let config = StreamableHttpClientTransportConfig::with_uri(format!("http://127.0.0.1:{port}/mcp")) + .auth_header(endpoint.token.clone()); + let transport = StreamableHttpClientTransport::with_client(http, config); + let identity = ClientConfig::new( + ClientCapabilities::default(), + Implementation::new(CLIENT_NAME, version).with_title(CLIENT_TITLE), + ); + let service = tokio::time::timeout(deadlines.handshake, identity.serve(transport)) + .await + .map_err(|_| no_answer(deadlines.handshake))? + .map_err(|error| initialize_failure(&error, port))?; + let client = Self { + service, + port, + deadlines, + }; + if client.server_name().as_deref() != Some(SERVER_NAME) { + return Err(CommandError::NotTablePro { port }); + } + Ok(client) + } + + pub(crate) fn server_version(&self) -> Option { + self.service + .peer_info() + .and_then(|info| info.server_info.as_ref().map(|server| server.version.clone())) + } + + fn server_name(&self) -> Option { + self.service + .peer_info() + .and_then(|info| info.server_info.as_ref().map(|server| server.name.clone())) + } + + pub(crate) fn deadlines(&self) -> Deadlines { + self.deadlines + } + + pub(crate) async fn call(&self, tool: &'static str, arguments: &impl Serialize) -> Result { + self.call_within(tool, arguments, self.deadlines.tool).await + } + + // The text of a tool's answer, or its failure with the tool's code. + pub(crate) async fn call_within( + &self, + tool: &'static str, + arguments: &impl Serialize, + deadline: Duration, + ) -> Result { + let arguments = serde_json::to_value(arguments).map_err(|error| CommandError::Answer(error.to_string()))?; + let params = match arguments { + serde_json::Value::Object(arguments) => CallToolRequestParams::new(tool).with_arguments(arguments), + _ => CallToolRequestParams::new(tool), + }; + tracing::debug!(tool, ?deadline, "calling a TablePro tool"); + let request = ClientRequest::from(CallToolRequest::new(params)); + let peer = self.service.peer(); + let answered = tokio::time::timeout(deadline.saturating_add(CANCEL_GRACE), async { + peer.send_request_with_option(request, PeerRequestOptions::with_timeout(deadline)) + .await? + .await_response() + .await + }) + .await; + let result = match answered { + Err(_) | Ok(Err(ServiceError::Timeout { .. })) => return Err(no_answer(deadline)), + Ok(Err(error)) => return Err(service_failure(error, self.port)), + Ok(Ok(ServerResult::CallToolResult(result))) => result, + Ok(Ok(_)) => return Err(service_failure(ServiceError::UnexpectedResponse, self.port)), + }; + let text: String = result + .content + .iter() + .filter_map(ContentBlock::as_text) + .map(|content| content.text.as_str()) + .collect(); + match result.is_error { + Some(true) => Err(CommandError::from_tool_text(&text)), + _ => Ok(text), + } + } +} + +fn no_answer(deadline: Duration) -> CommandError { + CommandError::NoAnswer { + seconds: deadline.as_secs(), + } +} + +fn initialize_failure(error: &ClientInitializeError, port: u16) -> CommandError { + match error { + ClientInitializeError::TransportError { error, .. } => transport_failure(error, port), + other => CommandError::Transport(other.to_string()), + } +} + +fn service_failure(error: ServiceError, port: u16) -> CommandError { + match error { + ServiceError::McpError(data) => CommandError::Transport(data.message.into_owned()), + ServiceError::TransportSend(error) => transport_failure(&error, port), + other => CommandError::Transport(other.to_string()), + } +} + +// rmcp nests the HTTP failure inside its own errors; the layer that knows what went wrong is found by type. +fn transport_failure(error: &(dyn Error + 'static), port: u16) -> CommandError { + let mut layer = Some(error); + while let Some(current) = layer { + if current.is::() { + return CommandError::TokenRefused { port }; + } + if let Some(host) = current.downcast_ref::() { + return CommandError::Host(host.clone()); + } + match current.downcast_ref::>() { + Some(StreamableHttpError::ResponseBodyTooLarge { max_size }) => { + return CommandError::AnswerTooLarge { + mebibytes: max_size / (1024 * 1024), + }; + } + Some(StreamableHttpError::Client(http)) => { + if let Some(failure) = http_failure(http, port) { + return failure; + } + } + _ => {} + } + if let Some(http) = current.downcast_ref::() + && let Some(failure) = http_failure(http, port) + { + return failure; + } + layer = current.source(); + } + CommandError::Transport(error.to_string()) +} + +fn http_failure(error: &reqwest::Error, port: u16) -> Option { + let mut layer = error.source(); + while let Some(current) = layer { + if let Some(host) = current.downcast_ref::() { + return Some(CommandError::Host(host.clone())); + } + layer = current.source(); + } + if error.is_connect() { + return Some(CommandError::NotAnswering { port }); + } + match error.status() { + Some(reqwest::StatusCode::UNAUTHORIZED) => Some(CommandError::TokenRefused { port }), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use async_trait::async_trait; + use secrecy::SecretString; + use tablepro_core::dialect::SqlDialect; + use tablepro_mcp::{AccessToken, McpHost, McpServer, OpenOutcome, ToolContext, WriteApproval}; + use tablepro_session::Session; + use tablepro_session::runtime::Tasks; + use tablepro_storage::query_history::NewEntry; + use tablepro_storage::{ConnectionStore, SavedConnection, StoragePaths}; + use uuid::Uuid; + + use super::*; + use crate::exit::Exit; + use crate::tool_arguments::NoArguments; + + const TOKEN: &str = "tp_test_token"; + + // Real time: the deadlines are short, and a paused clock could fire one before a loopback connect lands. + const SHORT: Deadlines = Deadlines { + handshake: Duration::from_millis(300), + tool: Duration::from_millis(300), + statement_margin: Duration::from_millis(300), + }; + + // An app whose GTK thread never answers. + struct Stalled; + + #[async_trait] + impl McpHost for Stalled { + async fn session(&self, _id: Uuid) -> Option> { + std::future::pending().await + } + + async fn open_connections(&self) -> Vec { + std::future::pending().await + } + + async fn open(&self, _connection: SavedConnection) -> OpenOutcome { + std::future::pending().await + } + + async fn approve(&self, _approval: WriteApproval) -> bool { + std::future::pending().await + } + + async fn record(&self, _entry: NewEntry, _dialect: Arc) {} + } + + fn endpoint_at(port: u16) -> Endpoint { + Endpoint { + port, + token: TOKEN.to_owned(), + } + } + + #[tokio::test] + async fn a_listener_that_never_answers_the_handshake_stops_at_its_deadline() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a port"); + let port = listener.local_addr().expect("an address").port(); + let silent = tokio::spawn(async move { + let mut held = Vec::new(); + while let Ok((stream, _)) = listener.accept().await { + held.push(stream); + } + }); + + let refused = McpClient::connect_within(&endpoint_at(port), "1.0.0", SHORT, &crate::runner::tests::STOPPED) + .await + .err(); + + assert!(matches!(refused, Some(CommandError::NoAnswer { .. })), "{refused:?}"); + assert_eq!(refused.map(|error| error.exit()), Some(Exit::TimedOut)); + silent.abort(); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn a_tool_call_the_app_never_answers_stops_at_its_deadline() { + let dir = tempfile::tempdir().expect("a temporary directory"); + let paths = StoragePaths::under(dir.path(), "tablepro-test", "app.tablepro.TablePro.Test"); + let store = ConnectionStore::new(&paths); + store.load_blocking().expect("an empty library"); + let token = AccessToken::from_secret(SecretString::from(TOKEN.to_owned())).expect("a token"); + let context = ToolContext { + host: Arc::new(Stalled), + connections: store, + }; + let tasks = Tasks::new(tokio::runtime::Handle::current()); + let server = McpServer::start(0, token, context, "0.0.0-test", &tasks) + .await + .expect("a listening server"); + let client = McpClient::connect_within( + &endpoint_at(server.address().port()), + "1.0.0", + SHORT, + &crate::runner::tests::STOPPED, + ) + .await + .expect("a handshake the server answers itself"); + + let refused = tokio::time::timeout( + Duration::from_secs(10), + client.call("list_connections", &NoArguments {}), + ) + .await + .expect("the call returns on its own"); + + assert!(matches!(refused, Err(CommandError::NoAnswer { .. })), "{refused:?}"); + } + + #[test] + fn a_statement_waits_for_its_own_timeout_and_an_export_for_each_table() { + let deadlines = Deadlines::TABLEPRO; + + assert_eq!(deadlines.statement(None), Duration::from_secs(150)); + assert_eq!(deadlines.statement(Some(300)), Duration::from_secs(420)); + assert_eq!(deadlines.tables(0), Duration::from_secs(60)); + assert_eq!(deadlines.tables(3), Duration::from_secs(180)); + } + + #[test] + fn an_answer_past_the_body_limit_says_to_ask_for_fewer_rows() { + let error = StreamableHttpError::::ResponseBodyTooLarge { + max_size: 16 * 1024 * 1024, + }; + + let failure = transport_failure(&error, 1); + + assert!(matches!(failure, CommandError::AnswerTooLarge { mebibytes: 16 })); + assert!(failure.to_string().contains("--limit")); + } +} diff --git a/core/crates/cli/src/command_error.rs b/core/crates/cli/src/command_error.rs new file mode 100644 index 0000000000..2c9a328a2c --- /dev/null +++ b/core/crates/cli/src/command_error.rs @@ -0,0 +1,208 @@ +use std::io; + +use crate::exit::Exit; +use crate::host::HostError; + +// core/crates/mcp/src/tool_error.rs: a failed tool answers ": ". +const TOOL_CODES: [&str; 9] = [ + "invalid_argument", + "not_found", + "not_connected", + "denied", + "user_cancelled", + "timeout", + "query_failed", + "unsupported", + "internal_failure", +]; + +#[derive(Debug, thiserror::Error)] +pub(crate) enum CommandError { + #[error("{0}")] + Usage(String), + #[error("Files and links open in TablePro itself: run tablepro with them and no command.")] + OpensInApp, + #[error("TablePro is not running. Start it, then run the command again.")] + NotRunning, + #[error("TablePro's MCP server is off. Turn on Preferences > MCP > Enable MCP Server, then run the command again.")] + ServerOff, + #[error("TablePro's MCP server has not started. Preferences > MCP shows why.")] + NoToken, + #[error("TablePro's MCP server does not answer on port {port}. Preferences > MCP shows why.")] + NotAnswering { port: u16 }, + #[error( + "The MCP token changed while the command ran. Run it again; if it keeps failing, another program answers on port {port}." + )] + TokenRefused { port: u16 }, + #[error("Port {port} is answered by a program that is not TablePro.")] + NotTablePro { port: u16 }, + #[error("The request to TablePro failed: {0}")] + Transport(String), + #[error("TablePro did not answer within {seconds} seconds.")] + NoAnswer { seconds: u64 }, + #[error( + "TablePro's answer is larger than the {mebibytes} MiB this command reads. Ask for fewer rows with --limit." + )] + AnswerTooLarge { mebibytes: usize }, + #[error("{message}")] + Tool { code: String, message: String }, + #[error("No connection named “{0}” is open to the tablepro command. Run tablepro connections to list them.")] + UnknownConnection(String), + #[error("{} connections are named “{name}”. Pass one of their ids: {}.", .ids.len(), .ids.join(", "))] + AmbiguousConnection { name: String, ids: Vec }, + #[error("“{0}” is not open in TablePro. Open it there, or run the command in a terminal to have it opened.")] + NotOpen(String), + #[error("“{0}” did not open in TablePro within 5 minutes.")] + OpenTimedOut(String), + #[error("TablePro sent an answer this command cannot read: {0}")] + Answer(String), + #[error(transparent)] + Host(#[from] HostError), + #[error("Could not start: {0}")] + Runtime(String), + #[error("Could not write the output: {0}")] + Output(#[source] io::Error), +} + +impl CommandError { + pub(crate) fn from_tool_text(text: &str) -> Self { + match text.split_once(": ") { + Some((code, message)) if TOOL_CODES.contains(&code) => Self::Tool { + code: code.to_owned(), + message: message.to_owned(), + }, + _ => Self::Tool { + code: "unknown".to_owned(), + message: text.to_owned(), + }, + } + } + + pub(crate) fn code(&self) -> &str { + match self { + Self::Usage(_) => "cli.usage", + Self::OpensInApp => "cli.opens_in_app", + Self::NotRunning => "cli.not_running", + Self::ServerOff => "cli.server_off", + Self::NoToken => "cli.no_token", + Self::NotAnswering { .. } => "cli.not_answering", + Self::TokenRefused { .. } => "cli.token_refused", + Self::NotTablePro { .. } => "cli.not_tablepro", + Self::Transport(_) => "cli.transport", + Self::NoAnswer { .. } => "cli.no_answer", + Self::AnswerTooLarge { .. } => "cli.answer_too_large", + Self::Tool { code, .. } => code, + Self::UnknownConnection(_) => "cli.unknown_connection", + Self::AmbiguousConnection { .. } => "cli.ambiguous_connection", + Self::NotOpen(_) => "cli.not_open", + Self::OpenTimedOut(_) => "cli.open_timed_out", + Self::Answer(_) => "cli.unreadable_answer", + Self::Host(error) => error.code(), + Self::Runtime(_) => "cli.runtime", + Self::Output(_) => "cli.output", + } + } + + pub(crate) fn exit(&self) -> Exit { + match self { + Self::Usage(_) | Self::OpensInApp | Self::UnknownConnection(_) | Self::AmbiguousConnection { .. } => { + Exit::Usage + } + Self::ServerOff | Self::TokenRefused { .. } => Exit::Refused, + Self::NoAnswer { .. } => Exit::TimedOut, + Self::Tool { code, .. } => match code.as_str() { + "invalid_argument" => Exit::Usage, + "denied" | "user_cancelled" => Exit::Refused, + "timeout" => Exit::TimedOut, + _ => Exit::Failure, + }, + Self::NotRunning + | Self::NoToken + | Self::NotAnswering { .. } + | Self::NotTablePro { .. } + | Self::Transport(_) + | Self::AnswerTooLarge { .. } + | Self::NotOpen(_) + | Self::OpenTimedOut(_) + | Self::Answer(_) + | Self::Host(_) + | Self::Runtime(_) + | Self::Output(_) => Exit::Failure, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_tool_code_maps_to_its_exit_status() { + let cases = [ + ("invalid_argument: send one statement per call", Exit::Usage), + ("denied: “Staging” lets MCP clients read only", Exit::Refused), + ( + "user_cancelled: the user declined the statement in TablePro", + Exit::Refused, + ), + ( + "timeout: the statement ran past 30 seconds and was stopped", + Exit::TimedOut, + ), + ("not_found: no table or view named x", Exit::Failure), + ("not_connected: “Staging” is not open in TablePro", Exit::Failure), + ("query_failed: syntax error", Exit::Failure), + ("unsupported: this engine cannot", Exit::Failure), + ("internal_failure: TablePro failed", Exit::Failure), + ("something without a code", Exit::Failure), + ]; + + for (text, exit) in cases { + assert_eq!(CommandError::from_tool_text(text).exit(), exit, "{text}"); + } + } + + #[test] + fn the_message_drops_a_known_code_and_keeps_an_unknown_one() { + assert_eq!( + CommandError::from_tool_text("denied: read only").to_string(), + "read only" + ); + assert_eq!(CommandError::from_tool_text("odd: text").to_string(), "odd: text"); + } + + #[test] + fn an_ambiguous_name_lists_every_id() { + let error = CommandError::AmbiguousConnection { + name: "Staging".to_owned(), + ids: vec!["a".to_owned(), "b".to_owned()], + }; + + assert_eq!(error.exit(), Exit::Usage); + assert_eq!( + error.to_string(), + "2 connections are named “Staging”. Pass one of their ids: a, b." + ); + } + + #[test] + fn settings_off_and_a_refused_token_are_refusals() { + assert_eq!(CommandError::ServerOff.exit(), Exit::Refused); + assert_eq!(CommandError::TokenRefused { port: 1 }.exit(), Exit::Refused); + assert_eq!(CommandError::NotRunning.exit(), Exit::Failure); + assert_eq!(CommandError::NotAnswering { port: 1 }.exit(), Exit::Failure); + } + + #[test] + fn a_server_that_stops_answering_times_out_and_a_foreign_one_fails() { + assert_eq!(CommandError::NoAnswer { seconds: 60 }.exit(), Exit::TimedOut); + assert_eq!( + CommandError::NoAnswer { seconds: 60 }.to_string(), + "TablePro did not answer within 60 seconds." + ); + assert_eq!( + CommandError::Host(HostError::ForeignServer { port: 1 }).exit(), + Exit::Failure + ); + } +} diff --git a/core/crates/cli/src/command_line.rs b/core/crates/cli/src/command_line.rs new file mode 100644 index 0000000000..9e71c822fe --- /dev/null +++ b/core/crates/cli/src/command_line.rs @@ -0,0 +1,348 @@ +use std::ffi::OsString; +use std::path::PathBuf; + +use clap::{Arg, ArgAction, Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum, ValueHint}; + +use crate::output::{ExportFormat, OutputFormat}; + +pub(crate) const SUBCOMMANDS: [&str; 10] = [ + "open", + "query", + "connections", + "databases", + "schemas", + "tables", + "describe", + "export", + "status", + "completion", +]; + +const ABOUT: &str = "Open databases in TablePro and work with them from the shell."; + +const AFTER_HELP: &str = "Examples: + tablepro \"postgresql://user@127.0.0.1:5432/app\" + tablepro schema.sql + tablepro query -c Staging \"SELECT id, email FROM users LIMIT 5\" + tablepro query -c Staging -f report.sql --format csv > report.csv + +Exit status: 0 done, 1 database or connection error, 2 usage error, 3 refused by a setting, 4 timed out."; + +const QUERY_ABOUT: &str = "Run one SQL statement through TablePro and print the rows. + +The SQL comes from the argument, from --file, or from standard input. Put SQL that starts with a dash after --: + tablepro query -c Staging -- \"-- weekly report + SELECT 1\" + +The connection has to be open in TablePro. Run in a terminal, the command opens it and waits; otherwise it stops. + +Exit status: 0 done, 1 database or connection error, 2 usage error, 3 refused by a setting, 4 timed out."; + +const CONNECTION_HELP: &str = "A saved connection's name or id"; +const DATABASE_HELP: &str = "A database TablePro has open beside the connection; the connection's own by default"; +const SCHEMA_HELP: &str = "The schema; the connection's default by default"; +const FORMAT_HELP: &str = "Output format; table on a terminal, csv otherwise"; +const FORMAT_NAME: &str = "FORMAT"; + +#[derive(Debug, Parser)] +#[command( + name = "tablepro", + bin_name = "tablepro", + about = ABOUT, + after_help = AFTER_HELP, + args_conflicts_with_subcommands = true, + disable_version_flag = true +)] +pub(crate) struct Cli { + #[command(subcommand)] + pub(crate) command: Option, + #[command(flatten)] + pub(crate) open: OpenArgs, +} + +#[derive(Debug, Subcommand)] +pub(crate) enum Command { + #[command(about = "Open connection URLs and database or SQL files in TablePro")] + Open(OpenArgs), + #[command(about = "Run one SQL statement and print the rows", long_about = QUERY_ABOUT)] + Query(QueryArgs), + #[command(about = "List the saved connections this command can use")] + Connections(FormatArgs), + #[command(about = "List a connection's databases")] + Databases(DatabasesArgs), + #[command(about = "List a connection's schemas")] + Schemas(SchemasArgs), + #[command(about = "List the tables and views in a schema")] + Tables(TablesArgs), + #[command(about = "Show a table's columns, indexes and foreign keys")] + Describe(DescribeArgs), + #[command(about = "Export tables as CSV, JSON or SQL INSERT statements")] + Export(ExportArgs), + #[command(about = "Show whether this command can reach TablePro")] + Status(FormatArgs), + #[command(about = "Print a shell completion script")] + Completion(CompletionArgs), +} + +#[derive(Debug, Args)] +pub(crate) struct OpenArgs { + #[arg( + value_name = "URL_OR_FILE", + value_hint = ValueHint::AnyPath, + help = "Connection URLs and database or SQL files to open" + )] + pub(crate) targets: Vec, + #[arg(short = 'n', long, help = "Open a new window")] + pub(crate) new_window: bool, +} + +#[derive(Debug, Args)] +pub(crate) struct FormatArgs { + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, +} + +#[derive(Debug, Args)] +pub(crate) struct QueryArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_name = "NAME", help = DATABASE_HELP)] + pub(crate) database: Option, + #[arg( + short = 'f', + long, + value_name = "PATH", + value_hint = ValueHint::FilePath, + conflicts_with = "sql", + help = "Read the SQL from a file, or - for standard input" + )] + pub(crate) file: Option, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, + #[arg(long, value_name = "ROWS", help = "Rows to return, 1 to 1000; 100 by default")] + pub(crate) limit: Option, + #[arg( + long, + value_name = "SECONDS", + help = "Seconds the statement may run, 1 to 300; 30 by default" + )] + pub(crate) timeout: Option, + #[arg(long, help = "Refuse any statement that is not a read")] + pub(crate) read_only: bool, + #[arg(value_name = "SQL", help = "The statement to run")] + pub(crate) sql: Option, +} + +#[derive(Debug, Args)] +pub(crate) struct DatabasesArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, +} + +#[derive(Debug, Args)] +pub(crate) struct SchemasArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_name = "NAME", help = DATABASE_HELP)] + pub(crate) database: Option, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, +} + +#[derive(Debug, Args)] +pub(crate) struct TablesArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_name = "NAME", help = DATABASE_HELP)] + pub(crate) database: Option, + #[arg(long, value_name = "NAME", help = SCHEMA_HELP)] + pub(crate) schema: Option, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, +} + +#[derive(Debug, Args)] +pub(crate) struct DescribeArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_name = "NAME", help = DATABASE_HELP)] + pub(crate) database: Option, + #[arg(long, value_name = "NAME", help = SCHEMA_HELP)] + pub(crate) schema: Option, + #[arg(long, conflicts_with = "format", help = "Print the CREATE TABLE statement instead")] + pub(crate) ddl: bool, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = FORMAT_HELP)] + pub(crate) format: Option, + #[arg(value_name = "TABLE", help = "The table or view, unquoted")] + pub(crate) table: String, +} + +#[derive(Debug, Args)] +pub(crate) struct ExportArgs { + #[arg(short = 'c', long, value_name = "CONNECTION", help = CONNECTION_HELP)] + pub(crate) connection: String, + #[arg(long, value_name = "NAME", help = DATABASE_HELP)] + pub(crate) database: Option, + #[arg(long, value_name = "NAME", help = SCHEMA_HELP)] + pub(crate) schema: Option, + #[arg(long, value_enum, ignore_case = true, value_name = FORMAT_NAME, help = "Export format")] + pub(crate) format: ExportFormat, + #[arg( + long, + value_name = "ROWS", + help = "Rows to export per table, 1 to 1000; 100 by default" + )] + pub(crate) limit: Option, + #[arg( + required = true, + value_name = "TABLE", + help = "The tables or views to export, unquoted" + )] + pub(crate) tables: Vec, +} + +#[derive(Debug, Args)] +pub(crate) struct CompletionArgs { + #[arg(value_enum, help = "The shell to print the script for")] + pub(crate) shell: Shell, +} + +// PowerShell comes with the Windows app, where clap writes it as well. +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] +pub(crate) enum Shell { + Bash, + Zsh, + Fish, +} + +impl Shell { + pub(crate) fn generator(self) -> clap_complete::aot::Shell { + match self { + Self::Bash => clap_complete::aot::Shell::Bash, + Self::Zsh => clap_complete::aot::Shell::Zsh, + Self::Fish => clap_complete::aot::Shell::Fish, + } + } +} + +// The version is the app's, which only the app knows. +pub(crate) fn command(version: &str) -> clap::Command { + Cli::command().version(version.to_owned()).arg( + Arg::new("version") + .long("version") + .action(ArgAction::Version) + .help("Print the version"), + ) +} + +pub(crate) fn parse(args: Vec, version: &str) -> Result { + let matches = command(version).try_get_matches_from(args)?; + Cli::from_arg_matches(&matches) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parsed(args: &[&str]) -> Result { + parse(args.iter().map(OsString::from).collect(), "1.2.3") + } + + #[test] + fn the_command_line_is_well_formed() { + command("1.2.3").debug_assert(); + } + + #[test] + fn every_listed_subcommand_exists() { + let command = command("1.2.3"); + let names: Vec<&str> = command + .get_subcommands() + .map(clap::Command::get_name) + .filter(|&name| name != "help") + .collect(); + + assert_eq!(names, SUBCOMMANDS); + } + + #[test] + fn a_word_that_is_no_command_is_a_target() { + let cli = parsed(&["tablepro", "app.db", "postgresql://h/db"]).expect("parsed"); + + assert!(cli.command.is_none()); + assert_eq!( + cli.open.targets, + vec![OsString::from("app.db"), OsString::from("postgresql://h/db")] + ); + } + + #[test] + fn the_format_reads_in_any_case() { + let cli = parsed(&["tablepro", "query", "-c", "Staging", "--format", "JSON", "SELECT 1"]).expect("parsed"); + + let Some(Command::Query(query)) = cli.command else { + unreachable!("query parses as query"); + }; + assert_eq!(query.format, Some(OutputFormat::Json)); + assert_eq!(query.sql.as_deref(), Some("SELECT 1")); + } + + #[test] + fn sql_that_starts_with_a_dash_goes_after_the_terminator() { + let refused = parsed(&["tablepro", "query", "-c", "Staging", "-- note\nSELECT 1"]); + let accepted = parsed(&["tablepro", "query", "-c", "Staging", "--", "-- note\nSELECT 1"]).expect("parsed"); + + assert!(refused.is_err()); + let Some(Command::Query(query)) = accepted.command else { + unreachable!("query parses as query"); + }; + assert_eq!(query.sql.as_deref(), Some("-- note\nSELECT 1")); + } + + #[test] + fn usage_errors_exit_two() { + let cases: [&[&str]; 9] = [ + &["tablepro", "query", "SELECT 1"], + &["tablepro", "query", "-c", "S", "--format", "xml", "SELECT 1"], + &["tablepro", "query", "-c", "S", "-f", "a.sql", "SELECT 1"], + &["tablepro", "query", "-c", "S", "--bogus", "SELECT 1"], + &["tablepro", "describe", "-c", "S", "--ddl", "--format", "json", "orders"], + &["tablepro", "export", "-c", "S", "orders"], + &["tablepro", "export", "-c", "S", "--format", "xlsx", "orders"], + &["tablepro", "completion", "powershell"], + &["tablepro", "help", "nosuch"], + ]; + + for args in cases { + let error = parsed(args).err(); + assert_eq!(error.map(|error| error.exit_code()), Some(2), "{args:?}"); + } + } + + #[test] + fn help_and_version_print_to_standard_output() { + for args in [ + &["tablepro", "--help"][..], + &["tablepro", "-h"], + &["tablepro", "help", "query"], + &["tablepro", "query", "--help"], + &["tablepro", "open", "--help"], + &["tablepro", "--version"], + ] { + let error = parsed(args).err(); + assert_eq!(error.as_ref().map(clap::Error::use_stderr), Some(false), "{args:?}"); + assert_eq!(error.map(|error| error.exit_code()), Some(0), "{args:?}"); + } + } + + #[test] + fn the_version_is_the_apps() { + let error = parsed(&["tablepro", "--version"]) + .err() + .map(|error| error.render().to_string()); + + assert_eq!(error.as_deref(), Some("tablepro 1.2.3\n")); + } +} diff --git a/core/crates/cli/src/commands/connection_lookup.rs b/core/crates/cli/src/commands/connection_lookup.rs new file mode 100644 index 0000000000..7739cffcf7 --- /dev/null +++ b/core/crates/cli/src/commands/connection_lookup.rs @@ -0,0 +1,197 @@ +use std::time::Duration; + +use serde::Serialize; + +use crate::answers::{ConnectAnswer, ConnectionList, ListedConnection, parse}; +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::console::Console; +use crate::tool_arguments::{ConnectionArguments, NoArguments}; + +const OPEN_DEADLINE: Duration = Duration::from_secs(300); +const POLL_INTERVAL: Duration = Duration::from_secs(1); + +pub(super) async fn list(client: &McpClient) -> Result, CommandError> { + let answer: ConnectionList = parse(&client.call("list_connections", &NoArguments {}).await?)?; + Ok(answer.connections) +} + +pub(super) struct Resolved { + connection: ListedConnection, + opened: bool, +} + +// Ids and names go through the one listing, so a connection hidden from clients reads as unknown either way. +pub(super) async fn resolve( + client: &McpClient, + wanted: &str, + console: &mut Console<'_>, +) -> Result { + let connections = list(client).await?; + let connection = find(&connections, wanted)?.clone(); + let opened = !connection.is_open; + if opened { + open(client, &connection, console).await?; + } + Ok(Resolved { connection, opened }) +} + +impl Resolved { + pub(super) fn id(&self) -> &str { + &self.connection.id + } + + pub(super) async fn call( + &self, + client: &McpClient, + console: &mut Console<'_>, + tool: &'static str, + arguments: &impl Serialize, + ) -> Result { + self.call_within(client, console, tool, arguments, client.deadlines().tool) + .await + } + + // The listing can call a connection open that closed since, so a tool that finds it closed gets one open and + // one retry. Once open, a not_connected answer is final: a database the window does not hold gives it too. + pub(super) async fn call_within( + &self, + client: &McpClient, + console: &mut Console<'_>, + tool: &'static str, + arguments: &impl Serialize, + deadline: Duration, + ) -> Result { + match client.call_within(tool, arguments, deadline).await { + Err(error) if error.code() == "not_connected" && !self.opened && console.terminal().stderr => { + open(client, &self.connection, console).await?; + client.call_within(tool, arguments, deadline).await + } + answer => answer, + } + } +} + +fn find<'a>(connections: &'a [ListedConnection], wanted: &str) -> Result<&'a ListedConnection, CommandError> { + let by_id: Vec<&ListedConnection> = connections + .iter() + .filter(|connection| connection.id.eq_ignore_ascii_case(wanted)) + .collect(); + let by_name: Vec<&ListedConnection> = connections + .iter() + .filter(|connection| connection.name == wanted) + .collect(); + let lowered = wanted.to_lowercase(); + let by_folded_name: Vec<&ListedConnection> = connections + .iter() + .filter(|connection| connection.name.to_lowercase() == lowered) + .collect(); + let matches = [by_id, by_name, by_folded_name] + .into_iter() + .find(|matches| !matches.is_empty()) + .unwrap_or_default(); + match matches.as_slice() { + [] => Err(CommandError::UnknownConnection(wanted.to_owned())), + [only] => Ok(*only), + many => Err(CommandError::AmbiguousConnection { + name: wanted.to_owned(), + ids: many.iter().map(|connection| connection.id.clone()).collect(), + }), + } +} + +// connect opens a window in TablePro, which a script running unattended must not do behind the user. +async fn open( + client: &McpClient, + connection: &ListedConnection, + console: &mut Console<'_>, +) -> Result<(), CommandError> { + if !console.terminal().stderr { + return Err(CommandError::NotOpen(connection.name.clone())); + } + console.note(&format!( + "Opening “{}” in TablePro. Answer TablePro there if it asks.", + connection.name + )); + // Bounded as a whole, so a connect call still running at the deadline does not stretch it. + tokio::time::timeout(OPEN_DEADLINE, wait_open(client, connection)) + .await + .unwrap_or_else(|_| Err(CommandError::OpenTimedOut(connection.name.clone()))) +} + +async fn wait_open(client: &McpClient, connection: &ListedConnection) -> Result<(), CommandError> { + let arguments = ConnectionArguments { + connection_id: &connection.id, + }; + loop { + let answer: ConnectAnswer = parse(&client.call("connect", &arguments).await?)?; + if answer.status == "open" { + return Ok(()); + } + tokio::time::sleep(POLL_INTERVAL).await; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn listed(id: &str, name: &str) -> ListedConnection { + ListedConnection { + id: id.to_owned(), + name: name.to_owned(), + kind: "postgresql".to_owned(), + is_open: true, + } + } + + fn found(connections: &[ListedConnection], wanted: &str) -> Result { + find(connections, wanted).map(|connection| connection.id.clone()) + } + + #[test] + fn an_id_wins_then_the_exact_name_then_the_name_in_any_case() { + let connections = [ + listed("8b1f0c2e-0000-4000-8000-000000000001", "Staging"), + listed("8b1f0c2e-0000-4000-8000-000000000002", "staging"), + listed("8b1f0c2e-0000-4000-8000-000000000003", "Prod"), + ]; + + assert_eq!( + found(&connections, "8B1F0C2E-0000-4000-8000-000000000003") + .ok() + .as_deref(), + Some("8b1f0c2e-0000-4000-8000-000000000003") + ); + assert_eq!( + found(&connections, "staging").ok().as_deref(), + Some("8b1f0c2e-0000-4000-8000-000000000002") + ); + assert_eq!( + found(&connections, "prod").ok().as_deref(), + Some("8b1f0c2e-0000-4000-8000-000000000003") + ); + } + + #[test] + fn a_name_no_listed_connection_has_is_unknown() { + let connections = [listed("a", "Staging")]; + + assert!(matches!( + found(&connections, "Blocked"), + Err(CommandError::UnknownConnection(_)) + )); + } + + #[test] + fn two_names_alike_in_any_case_are_ambiguous() { + let connections = [listed("a", "Staging"), listed("b", "STAGING")]; + + let refused = found(&connections, "staging"); + + assert!(matches!( + refused, + Err(CommandError::AmbiguousConnection { ref ids, .. }) if ids == &["a", "b"] + )); + } +} diff --git a/core/crates/cli/src/commands/describe.rs b/core/crates/cli/src/commands/describe.rs new file mode 100644 index 0000000000..d4aeabf330 --- /dev/null +++ b/core/crates/cli/src/commands/describe.rs @@ -0,0 +1,91 @@ +use super::connection_lookup::resolve; +use crate::answers::{TableDdl, TableDescription, parse}; +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::command_line::DescribeArgs; +use crate::console::Console; +use crate::host::Host; +use crate::output::{Cell, Table}; +use crate::tool_arguments::TableArguments; + +pub(super) async fn run( + arguments: &DescribeArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = TableArguments { + connection_id: connection.id(), + table: &arguments.table, + database: arguments.database.as_deref(), + schema: arguments.schema.as_deref(), + }; + if arguments.ddl { + let answer: TableDdl = parse(&connection.call(&client, console, "get_table_ddl", &request).await?)?; + let mut text = answer.ddl; + if !text.ends_with('\n') { + text.push('\n'); + } + return console.text(&text); + } + let answer: TableDescription = parse(&connection.call(&client, console, "describe_table", &request).await?)?; + console.tables(&tables(answer), arguments.format) +} + +// Three result sets, as a batch prints them: columns, indexes, foreign keys. +fn tables(description: TableDescription) -> [Table; 3] { + let mut columns = Table::new(&[ + "name", + "data_type", + "is_nullable", + "is_primary_key", + "is_auto_increment", + "is_generated", + "default_value", + "comment", + ]); + for column in description.columns { + columns.push(vec![ + Cell::text(column.name), + Cell::text(column.data_type), + Cell::literal(column.is_nullable), + Cell::literal(column.is_primary_key), + Cell::literal(column.is_auto_increment), + Cell::literal(column.is_generated), + Cell::optional(column.default_value), + Cell::optional(column.comment), + ]); + } + let mut indexes = Table::new(&["name", "columns", "is_unique", "is_primary", "predicate"]); + for index in description.indexes { + indexes.push(vec![ + Cell::text(index.name), + Cell::text(index.columns.join(", ")), + Cell::literal(index.is_unique), + Cell::literal(index.is_primary), + Cell::optional(index.predicate), + ]); + } + let mut foreign_keys = Table::new(&[ + "name", + "columns", + "referenced_schema", + "referenced_table", + "referenced_columns", + "on_delete", + "on_update", + ]); + for key in description.foreign_keys { + foreign_keys.push(vec![ + Cell::text(key.name), + Cell::text(key.columns.join(", ")), + Cell::optional(key.referenced_schema), + Cell::text(key.referenced_table), + Cell::text(key.referenced_columns.join(", ")), + Cell::optional(key.on_delete), + Cell::optional(key.on_update), + ]); + } + [columns, indexes, foreign_keys] +} diff --git a/core/crates/cli/src/commands/export.rs b/core/crates/cli/src/commands/export.rs new file mode 100644 index 0000000000..0915795ac3 --- /dev/null +++ b/core/crates/cli/src/commands/export.rs @@ -0,0 +1,118 @@ +use super::connection_lookup::resolve; +use crate::answers::{Export, ExportAnswer, parse}; +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::command_line::ExportArgs; +use crate::console::Console; +use crate::host::Host; +use crate::output::{ExportFormat, json_document}; +use crate::tool_arguments::ExportArguments; + +pub(super) async fn run( + arguments: &ExportArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = ExportArguments { + connection_id: connection.id(), + format: arguments.format, + tables: &arguments.tables, + database: arguments.database.as_deref(), + schema: arguments.schema.as_deref(), + max_rows: arguments.limit, + }; + let deadline = client.deadlines().tables(arguments.tables.len()); + let answer = connection + .call_within(&client, console, "export_data", &request, deadline) + .await?; + let answer: ExportAnswer = parse(&answer)?; + let text = joined(&answer.exports, arguments.format); + match arguments.format { + ExportFormat::Json => console.text(&json_document(&text))?, + _ => console.text(&text)?, + } + report(&answer, console); + Ok(()) +} + +fn report(answer: &ExportAnswer, console: &mut Console<'_>) { + for note in &answer.notes { + console.note(note); + } + for export in answer.exports.iter().filter(|export| export.truncated) { + console.note(&format!( + "Exported the first {} rows of “{}”. Pass --limit to change it.", + export.row_count, export.table + )); + } +} + +// Several JSON exports become one array of arrays, so the output stays one document, as a query's sets do. +fn joined(exports: &[Export], format: ExportFormat) -> String { + match (format, exports) { + (ExportFormat::Json, [_, _, ..]) => { + let arrays: Vec<&str> = exports.iter().map(|export| export.data.trim_end()).collect(); + format!("[\n{}\n]\n", arrays.join(",\n")) + } + _ => exports + .iter() + .map(|export| match export.data.ends_with('\n') { + true => export.data.clone(), + false => format!("{}\n", export.data), + }) + .collect::>() + .join("\n"), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::host::Terminal; + + fn export(table: &str, data: &str) -> Export { + Export { + table: table.to_owned(), + row_count: 1, + truncated: false, + data: data.to_owned(), + } + } + + #[test] + fn one_export_is_written_as_the_tool_made_it() { + assert_eq!(joined(&[export("a", "id\n1\n")], ExportFormat::Csv), "id\n1\n"); + assert_eq!(joined(&[export("a", "[]")], ExportFormat::Json), "[]\n"); + } + + #[test] + fn several_exports_stay_apart() { + let csv = [export("a", "id\n1\n"), export("b", "id\n2")]; + let json = [export("a", "[{\"id\":1}]\n"), export("b", "[]")]; + + assert_eq!(joined(&csv, ExportFormat::Csv), "id\n1\n\nid\n2\n"); + assert_eq!(joined(&json, ExportFormat::Json), "[\n[{\"id\":1}],\n[]\n]\n"); + } + + #[test] + fn the_servers_notes_go_to_standard_error() { + let answer: ExportAnswer = parse( + r#"{"format":"csv","exports":[{"table":"orders","row_count":2,"truncated":true,"data":"id\n1\n2\n"}], + "notes":["Some values could not be decoded by TablePro and are written as null."]}"#, + ) + .expect("an export document"); + let (mut out, mut err) = (Vec::new(), Vec::new()); + let mut console = Console::new(&mut out, &mut err, Terminal::default()); + + report(&answer, &mut console); + + assert!(out.is_empty()); + assert_eq!( + String::from_utf8(err).expect("UTF-8"), + "tablepro: Some values could not be decoded by TablePro and are written as null.\n\ + tablepro: Exported the first 2 rows of “orders”. Pass --limit to change it.\n" + ); + } +} diff --git a/core/crates/cli/src/commands/listing.rs b/core/crates/cli/src/commands/listing.rs new file mode 100644 index 0000000000..7098257132 --- /dev/null +++ b/core/crates/cli/src/commands/listing.rs @@ -0,0 +1,93 @@ +use super::connection_lookup::{list, resolve}; +use crate::answers::{DatabaseList, SchemaList, TableList, parse}; +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::command_line::{DatabasesArgs, FormatArgs, SchemasArgs, TablesArgs}; +use crate::console::Console; +use crate::host::Host; +use crate::output::{Cell, Table}; +use crate::tool_arguments::{ConnectionArguments, ScopeArguments}; + +// The name, id and type only, as on macOS: never a host, user or password. +pub(super) async fn connections( + arguments: &FormatArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let mut table = Table::new(&["name", "id", "type"]); + for connection in list(&client).await? { + table.push(vec![ + Cell::text(connection.name), + Cell::text(connection.id), + Cell::text(connection.kind), + ]); + } + console.tables(&[table], arguments.format) +} + +pub(super) async fn databases( + arguments: &DatabasesArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = ConnectionArguments { + connection_id: connection.id(), + }; + let answer: DatabaseList = parse(&connection.call(&client, console, "list_databases", &request).await?)?; + let mut table = Table::new(&["name", "is_current"]); + for database in answer.databases { + table.push(vec![Cell::text(database.name), Cell::literal(database.is_current)]); + } + console.tables(&[table], arguments.format) +} + +pub(super) async fn schemas( + arguments: &SchemasArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = ScopeArguments { + connection_id: connection.id(), + database: arguments.database.as_deref(), + schema: None, + }; + let answer: SchemaList = parse(&connection.call(&client, console, "list_schemas", &request).await?)?; + let mut table = Table::new(&["name", "is_default", "is_system"]); + for schema in answer.schemas { + table.push(vec![ + Cell::text(schema.name), + Cell::literal(schema.is_default), + Cell::literal(schema.is_system), + ]); + } + console.tables(&[table], arguments.format) +} + +pub(super) async fn tables( + arguments: &TablesArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = ScopeArguments { + connection_id: connection.id(), + database: arguments.database.as_deref(), + schema: arguments.schema.as_deref(), + }; + let answer: TableList = parse(&connection.call(&client, console, "list_tables", &request).await?)?; + let mut table = Table::new(&["name", "schema", "kind"]); + for listed in answer.tables { + table.push(vec![ + Cell::text(listed.name), + Cell::optional(listed.schema), + Cell::text(listed.kind), + ]); + } + console.tables(&[table], arguments.format) +} diff --git a/core/crates/cli/src/commands/mod.rs b/core/crates/cli/src/commands/mod.rs new file mode 100644 index 0000000000..88eb5f8ae0 --- /dev/null +++ b/core/crates/cli/src/commands/mod.rs @@ -0,0 +1,48 @@ +mod connection_lookup; +mod describe; +mod export; +mod listing; +mod query; +mod sql_source; +mod status; + +use crate::command_error::CommandError; +use crate::command_line::Command; +use crate::console::Console; +use crate::exit::Exit; +use crate::host::Host; + +// One request at a time, so a runtime on this thread is all a run needs. +pub(crate) fn execute( + command: Command, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Exit { + let runtime = match tokio::runtime::Builder::new_current_thread().enable_all().build() { + Ok(runtime) => runtime, + Err(error) => return console.fail(&CommandError::Runtime(error.to_string())), + }; + match runtime.block_on(dispatch(command, host, console)) { + Ok(exit) => exit, + Err(error) => console.fail(&error), + } +} + +async fn dispatch( + command: Command, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result { + let done = match command { + Command::Status(arguments) => return status::run(&arguments, host, console).await, + Command::Query(arguments) => query::run(&arguments, host, console).await, + Command::Connections(arguments) => listing::connections(&arguments, host, console).await, + Command::Databases(arguments) => listing::databases(&arguments, host, console).await, + Command::Schemas(arguments) => listing::schemas(&arguments, host, console).await, + Command::Tables(arguments) => listing::tables(&arguments, host, console).await, + Command::Describe(arguments) => describe::run(&arguments, host, console).await, + Command::Export(arguments) => export::run(&arguments, host, console).await, + Command::Open(_) | Command::Completion(_) => Err(CommandError::OpensInApp), + }; + done.map(|()| Exit::Success) +} diff --git a/core/crates/cli/src/commands/query.rs b/core/crates/cli/src/commands/query.rs new file mode 100644 index 0000000000..b00706d976 --- /dev/null +++ b/core/crates/cli/src/commands/query.rs @@ -0,0 +1,121 @@ +use super::connection_lookup::resolve; +use super::sql_source; +use crate::answers::{Results, parse}; +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::command_line::QueryArgs; +use crate::console::Console; +use crate::host::Host; +use crate::output::{OutputFormat, Table}; +use crate::tool_arguments::QueryArguments; + +pub(super) async fn run( + arguments: &QueryArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result<(), CommandError> { + // A usage error is reported before TablePro is asked anything. + let sql = sql_source::read(arguments, console.terminal())?; + let client = McpClient::connect(host).await?; + let connection = resolve(&client, &arguments.connection, console).await?; + let request = QueryArguments { + connection_id: connection.id(), + query: &sql, + database: arguments.database.as_deref(), + read_only: arguments.read_only, + max_rows: arguments.limit, + timeout_seconds: arguments.timeout, + }; + let deadline = client.deadlines().statement(arguments.timeout); + // A connection can drop after the server ran a write, so only a statement held to reading is sent twice. + let answer = match arguments.read_only { + true => { + connection + .call_within(&client, console, "execute_query", &request, deadline) + .await? + } + false => client.call_within("execute_query", &request, deadline).await?, + }; + let results: Results = parse(&answer)?; + write(&results, arguments.format, console) +} + +// Rows go to standard output; what the database or this command says about them goes to standard error. +fn write(results: &Results, format: Option, console: &mut Console<'_>) -> Result<(), CommandError> { + let tables: Vec = results.result_sets.iter().map(|set| set.to_table()).collect(); + console.tables(&tables, format)?; + for notice in &results.notices { + console.line(notice); + } + for note in results.notes() { + console.note(note); + } + for (set, table) in results.result_sets.iter().zip(&tables) { + if set.truncated { + let shown = match table.rows.len() { + 1 => "row".to_owned(), + count => format!("{count} rows"), + }; + console.note(&format!("Showing the first {shown}. Pass --limit to change it.")); + } + } + if tables.iter().all(|table| table.columns.is_empty()) + && let Some(affected) = results.rows_affected + { + console.line(&match affected { + 1 => "1 row affected.".to_owned(), + count => format!("{count} rows affected."), + }); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::host::Terminal; + + fn written(text: &str, terminal: Terminal) -> (String, String) { + let results: Results = parse(text).expect("a results document"); + let (mut out, mut err) = (Vec::new(), Vec::new()); + let mut console = Console::new(&mut out, &mut err, terminal); + write(&results, None, &mut console).expect("written"); + ( + String::from_utf8(out).expect("UTF-8"), + String::from_utf8(err).expect("UTF-8"), + ) + } + + #[test] + fn the_servers_notes_go_to_standard_error_after_the_rows() { + let text = r#"{"result_sets":[{"columns":[{"name":"id","type":"integer"}],"rows":[{"id":1}], + "row_count":1,"truncated":true}],"execution_time_ms":2, + "notes":["The statement left a transaction open, and it was rolled back when the call's connection closed.", + "Some values could not be decoded by TablePro and are shown as null.", + "A result set has more rows than the limit; ask for fewer with a WHERE clause or raise max_rows."]}"#; + + let (out, err) = written(text, Terminal::default()); + + assert_eq!(out, "id\n1\n"); + assert_eq!( + err, + "tablepro: The statement left a transaction open, and it was rolled back when the call's connection closed.\n\ + tablepro: Some values could not be decoded by TablePro and are shown as null.\n\ + tablepro: Showing the first row. Pass --limit to change it.\n" + ); + } + + #[test] + fn a_note_on_a_terminal_is_escaped() { + let text = + r#"{"result_sets":[],"rows_affected":0,"execution_time_ms":1,"notes":["bad \u001b]52;c;eA==\u0007"]}"#; + let terminal = Terminal { + stderr: true, + ..Terminal::default() + }; + + let (_, err) = written(text, terminal); + + assert_eq!(err, "tablepro: bad \\x1B]52;c;eA==\\x07\n0 rows affected.\n"); + } +} diff --git a/core/crates/cli/src/commands/sql_source.rs b/core/crates/cli/src/commands/sql_source.rs new file mode 100644 index 0000000000..db18cf00c3 --- /dev/null +++ b/core/crates/cli/src/commands/sql_source.rs @@ -0,0 +1,117 @@ +use std::io::Read; +use std::path::Path; + +use crate::command_error::CommandError; +use crate::command_line::QueryArgs; +use crate::host::Terminal; + +const BYTE_ORDER_MARK: char = '\u{feff}'; + +pub(super) fn read(arguments: &QueryArgs, terminal: Terminal) -> Result { + let text = match source(arguments, terminal)? { + Source::Argument(text) => text.to_owned(), + Source::File(path) => read_file(path)?, + Source::StandardInput => read_standard_input()?, + }; + let text = text.strip_prefix(BYTE_ORDER_MARK).map(str::to_owned).unwrap_or(text); + if text.trim().is_empty() { + return Err(CommandError::Usage("The SQL is empty.".to_owned())); + } + Ok(text) +} + +#[derive(Debug, PartialEq, Eq)] +enum Source<'a> { + Argument(&'a str), + File(&'a Path), + StandardInput, +} + +fn source(arguments: &QueryArgs, terminal: Terminal) -> Result, CommandError> { + match (&arguments.sql, &arguments.file) { + (Some(text), _) => Ok(Source::Argument(text)), + // An explicit - asks for standard input, so a terminal is read too, until end of file. + (None, Some(path)) if path.as_os_str() == "-" => Ok(Source::StandardInput), + (None, Some(path)) => Ok(Source::File(path)), + (None, None) if terminal.stdin => Err(CommandError::Usage( + "Pass the SQL as an argument, with --file, or on standard input.".to_owned(), + )), + (None, None) => Ok(Source::StandardInput), + } +} + +fn read_file(path: &Path) -> Result { + let bytes = + std::fs::read(path).map_err(|error| CommandError::Usage(format!("Cannot read {}: {error}", path.display())))?; + String::from_utf8(bytes).map_err(|_| CommandError::Usage(format!("{} is not UTF-8 text.", path.display()))) +} + +fn read_standard_input() -> Result { + let mut bytes = Vec::new(); + std::io::stdin() + .read_to_end(&mut bytes) + .map_err(|error| CommandError::Usage(format!("Cannot read standard input: {error}")))?; + String::from_utf8(bytes).map_err(|_| CommandError::Usage("Standard input is not UTF-8 text.".to_owned())) +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use super::*; + + fn query(sql: Option<&str>, file: Option<&str>) -> QueryArgs { + QueryArgs { + connection: "Staging".to_owned(), + database: None, + file: file.map(PathBuf::from), + format: None, + limit: None, + timeout: None, + read_only: false, + sql: sql.map(str::to_owned), + } + } + + fn terminal(stdin: bool) -> Terminal { + Terminal { + stdin, + ..Terminal::default() + } + } + + #[test] + fn a_dash_reads_standard_input_even_from_a_terminal() { + let arguments = query(None, Some("-")); + + assert_eq!(source(&arguments, terminal(true)).ok(), Some(Source::StandardInput)); + } + + #[test] + fn no_sql_reads_a_pipe_but_not_a_terminal() { + let arguments = query(None, None); + + assert_eq!(source(&arguments, terminal(false)).ok(), Some(Source::StandardInput)); + assert!(matches!( + source(&arguments, terminal(true)), + Err(CommandError::Usage(_)) + )); + } + + #[test] + fn a_file_is_read_without_its_byte_order_mark() { + let dir = tempfile::tempdir().expect("a directory"); + let file = dir.path().join("report.sql"); + std::fs::write(&file, "\u{feff}SELECT 1").expect("a file"); + let arguments = query(None, file.to_str()); + + assert_eq!(read(&arguments, terminal(true)).ok().as_deref(), Some("SELECT 1")); + } + + #[test] + fn empty_sql_or_a_missing_file_is_a_usage_error() { + for arguments in [query(Some(" \n"), None), query(None, Some("/nonexistent/report.sql"))] { + assert!(matches!(read(&arguments, terminal(true)), Err(CommandError::Usage(_)))); + } + } +} diff --git a/core/crates/cli/src/commands/status.rs b/core/crates/cli/src/commands/status.rs new file mode 100644 index 0000000000..f50e70c0bc --- /dev/null +++ b/core/crates/cli/src/commands/status.rs @@ -0,0 +1,114 @@ +use std::cell::OnceCell; +use std::net::SocketAddr; + +use crate::client::McpClient; +use crate::command_error::CommandError; +use crate::command_line::FormatArgs; +use crate::console::Console; +use crate::endpoint::endpoint; +use crate::exit::Exit; +use crate::host::{Host, HostError, Terminal}; +use crate::output::{Cell, Table}; + +// Reports what a command would meet, and exits as that command would; it never starts or changes anything. +pub(super) async fn run( + arguments: &FormatArgs, + host: &(impl Host + Clone + Send + Sync + 'static), + console: &mut Console<'_>, +) -> Result { + let reading = Reading::of(host); + let reached = match endpoint(&reading) { + Ok(reachable) => McpClient::connect_to(&reachable, &reading.version, host) + .await + .map(|client| client.server_version()), + Err(error) => Err(error), + }; + let mut table = Table::new(&[ + "version", + "running", + "server_enabled", + "port", + "token_found", + "server_version", + "ready", + ]); + table.push(vec![ + Cell::text(reading.version.clone()), + Cell::literal(reading.running), + reading + .enabled + .as_ref() + .map_or(Cell::Null, |&enabled| Cell::literal(enabled)), + reading.port.as_ref().map_or(Cell::Null, |&port| Cell::literal(port)), + match reading.token.get() { + Some(Ok(token)) => Cell::literal(token.is_some()), + _ => Cell::Null, + }, + Cell::optional(reached.as_ref().ok().cloned().flatten()), + Cell::literal(reached.is_ok()), + ]); + console.tables(&[table], arguments.format)?; + match reached { + Ok(_) => Ok(Exit::Success), + Err(error) => { + console.note(&error.to_string()); + Ok(error.exit()) + } + } +} + +// Each value is read once. The token is read only where a command would read it, so a stopped app or a server +// turned off never has the keyring ask to unlock. +struct Reading<'a> { + host: &'a dyn Host, + version: String, + running: bool, + enabled: Result, + port: Result, + token: OnceCell, HostError>>, + terminal: Terminal, +} + +impl<'a> Reading<'a> { + fn of(host: &'a dyn Host) -> Self { + Self { + host, + version: host.app_version(), + running: host.app_running(), + enabled: host.server_enabled(), + port: host.server_port(), + token: OnceCell::new(), + terminal: host.terminal(), + } + } +} + +impl Host for Reading<'_> { + fn app_version(&self) -> String { + self.version.clone() + } + + fn app_running(&self) -> bool { + self.running + } + + fn server_enabled(&self) -> Result { + self.enabled.clone() + } + + fn server_port(&self) -> Result { + self.port.clone() + } + + fn verify_peer(&self, peer: SocketAddr, local: SocketAddr) -> Result<(), HostError> { + self.host.verify_peer(peer, local) + } + + fn token(&self) -> Result, HostError> { + self.token.get_or_init(|| self.host.token()).clone() + } + + fn terminal(&self) -> Terminal { + self.terminal + } +} diff --git a/core/crates/cli/src/console.rs b/core/crates/cli/src/console.rs new file mode 100644 index 0000000000..d027b2b6a8 --- /dev/null +++ b/core/crates/cli/src/console.rs @@ -0,0 +1,85 @@ +use std::borrow::Cow; +use std::io::{self, Write}; + +use crate::command_error::CommandError; +use crate::exit::Exit; +use crate::host::Terminal; +use crate::output::{OutputFormat, Table, escape_lines, render}; + +pub(crate) struct Console<'a> { + out: &'a mut dyn Write, + err: &'a mut dyn Write, + terminal: Terminal, +} + +impl<'a> Console<'a> { + pub(crate) fn new(out: &'a mut dyn Write, err: &'a mut dyn Write, terminal: Terminal) -> Self { + Self { out, err, terminal } + } + + pub(crate) fn terminal(&self) -> Terminal { + self.terminal + } + + pub(crate) fn write_out(&mut self, bytes: &[u8]) -> Result<(), CommandError> { + self.out + .write_all(bytes) + .and_then(|()| self.out.flush()) + .map_err(CommandError::Output) + } + + pub(crate) fn text(&mut self, text: &str) -> Result<(), CommandError> { + let text = match self.terminal.stdout { + true => escape_lines(text), + false => Cow::Borrowed(text), + }; + self.write_out(text.as_bytes()) + } + + pub(crate) fn tables(&mut self, tables: &[Table], format: Option) -> Result<(), CommandError> { + let text = render(tables, OutputFormat::or_default(format, self.terminal)); + self.text(&text) + } + + pub(crate) fn note(&mut self, text: &str) { + self.line(&format!("tablepro: {text}")); + } + + // Server text can carry escape sequences, and a terminal would run them. + pub(crate) fn line(&mut self, text: &str) { + let text = match self.terminal.stderr { + true => escape_lines(text), + false => Cow::Borrowed(text), + }; + // Standard error is the last place to report to; a failed write has nowhere to go. + let _ = writeln!(self.err, "{text}"); + } + + pub(crate) fn fail(&mut self, error: &CommandError) -> Exit { + tracing::debug!(code = error.code(), "the command failed"); + // The reader stopped reading, as `| head` does; that is not the command failing. + if let CommandError::Output(cause) = error + && cause.kind() == io::ErrorKind::BrokenPipe + { + return Exit::Success; + } + self.note(&error.to_string()); + error.exit() + } + + pub(crate) fn parse_failed(&mut self, error: &clap::Error) -> Exit { + let text = error.render().to_string(); + if !error.use_stderr() { + return match self.write_out(text.as_bytes()) { + Ok(()) => Exit::Success, + Err(failure) => self.fail(&failure), + }; + } + let text = match self.terminal.stderr { + true => escape_lines(&text), + false => Cow::Borrowed(text.as_str()), + }; + let _ = self.err.write_all(text.as_bytes()); + Exit::Usage + } +} diff --git a/core/crates/cli/src/dispatch.rs b/core/crates/cli/src/dispatch.rs new file mode 100644 index 0000000000..01385fba0f --- /dev/null +++ b/core/crates/cli/src/dispatch.rs @@ -0,0 +1,173 @@ +use std::ffi::{OsStr, OsString}; +use std::path::Path; + +use crate::command_line::SUBCOMMANDS; +use crate::output::escape_lines; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Dispatch { + Command, + Gui(Vec), + // The whole line for standard error, without its line break. + Invalid(String), +} + +// The GUI keeps every argument it reads today: files, URLs, -n and GApplication's own options. +// Only a word this crate owns is taken from it. +pub fn classify(args: &[OsString]) -> Dispatch { + let Some(first) = args.get(1) else { + return Dispatch::Gui(args.to_vec()); + }; + match first.to_str() { + Some("-h" | "--help" | "--version" | "help") => Dispatch::Command, + Some("open") => { + let rest = args.get(2..).unwrap_or_default(); + if asks_for_help(rest) { + return Dispatch::Command; + } + checked(args.iter().take(1).chain(rest).cloned().collect()) + } + Some(word) if SUBCOMMANDS.contains(&word) => Dispatch::Command, + _ => checked(args.to_vec()), + } +} + +fn asks_for_help(args: &[OsString]) -> bool { + args.iter() + .take_while(|&arg| arg != "--") + .any(|arg| arg == "-h" || arg == "--help") +} + +// A word that is neither a link nor a file is most likely a mistyped command, never something to open. +fn checked(args: Vec) -> Dispatch { + let mut options_ended = false; + for arg in args.iter().skip(1) { + if !options_ended { + if arg == "--" { + options_ended = true; + continue; + } + if is_option(arg) { + continue; + } + } + if !has_scheme(arg) && !Path::new(arg).exists() { + return Dispatch::Invalid(format!( + "tablepro: No command or file named “{}”. Run tablepro --help to list the commands.", + escape_lines(&arg.to_string_lossy()) + )); + } + } + Dispatch::Gui(args) +} + +fn is_option(arg: &OsStr) -> bool { + let bytes = arg.as_encoded_bytes(); + bytes.len() > 1 && bytes.starts_with(b"-") +} + +// RFC 3986 scheme. One letter is left out: on Windows it is a drive, as in C:\data.db. +fn has_scheme(arg: &OsStr) -> bool { + let Some((scheme, _)) = arg.to_str().and_then(|text| text.split_once(':')) else { + return false; + }; + let mut characters = scheme.chars(); + scheme.len() > 1 + && characters.next().is_some_and(|first| first.is_ascii_alphabetic()) + && characters.all(|character| character.is_ascii_alphanumeric() || matches!(character, '+' | '-' | '.')) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn line(words: &[&str]) -> Vec { + words.iter().map(OsString::from).collect() + } + + fn classified(words: &[&str]) -> Dispatch { + classify(&line(words)) + } + + #[test] + fn a_command_word_goes_to_the_command_line() { + for words in [ + &["tablepro", "query", "-c", "S", "SELECT 1"][..], + &["tablepro", "connections"], + &["tablepro", "databases"], + &["tablepro", "schemas"], + &["tablepro", "tables"], + &["tablepro", "describe"], + &["tablepro", "export"], + &["tablepro", "status"], + &["tablepro", "completion", "zsh"], + &["tablepro", "help"], + &["tablepro", "-h"], + &["tablepro", "--help"], + &["tablepro", "--version"], + &["tablepro", "open", "--help"], + &["tablepro", "open", "-n", "-h"], + ] { + assert_eq!(classified(words), Dispatch::Command, "{words:?}"); + } + } + + #[test] + fn the_gui_gets_its_own_arguments_unchanged() { + for words in [ + &["tablepro"][..], + &["tablepro", "-n"], + &["tablepro", "--new-window"], + &["tablepro", "--gapplication-service"], + &["tablepro", "--help-all"], + &["tablepro", "--help-gapplication"], + &["tablepro", "postgresql://ada@db.example.com:5432/shop"], + &["tablepro", "mongodb+srv://cluster.example.com/app"], + &["tablepro", "jdbc:sqlserver://h:1433;databaseName=x"], + ] { + assert_eq!(classified(words), Dispatch::Gui(line(words)), "{words:?}"); + } + } + + #[test] + fn open_is_taken_off_and_files_pass_when_they_exist() { + let dir = tempfile::tempdir().expect("a directory"); + let file = dir.path().join("query"); + std::fs::write(&file, "").expect("a file"); + let path = file.to_str().expect("a UTF-8 path"); + + assert_eq!( + classified(&["tablepro", "open", "-n", path]), + Dispatch::Gui(line(&["tablepro", "-n", path])) + ); + assert_eq!( + classified(&["tablepro", path]), + Dispatch::Gui(line(&["tablepro", path])) + ); + assert_eq!(classified(&["tablepro", "open"]), Dispatch::Gui(line(&["tablepro"]))); + } + + #[test] + fn a_word_that_is_neither_link_nor_file_is_refused() { + for words in [ + &["tablepro", "querry"][..], + &["tablepro", "version"], + &["tablepro", "-n", "missing.db"], + &["tablepro", "open", "query"], + &["tablepro", "open", "--", "-missing.sql"], + &["tablepro", "C:missing"], + ] { + assert!(matches!(classified(words), Dispatch::Invalid(_)), "{words:?}"); + } + } + + #[test] + fn the_refusal_names_the_word() { + assert_eq!( + classified(&["tablepro", "querry"]), + Dispatch::Invalid( + "tablepro: No command or file named “querry”. Run tablepro --help to list the commands.".to_owned() + ) + ); + } +} diff --git a/core/crates/cli/src/endpoint.rs b/core/crates/cli/src/endpoint.rs new file mode 100644 index 0000000000..45a2cf81e2 --- /dev/null +++ b/core/crates/cli/src/endpoint.rs @@ -0,0 +1,20 @@ +use crate::command_error::CommandError; +use crate::host::Host; + +pub(crate) struct Endpoint { + pub(crate) port: u16, + pub(crate) token: String, +} + +// A stopped app or disabled server must not ask the keyring to unlock. +pub(crate) fn endpoint(host: &dyn Host) -> Result { + if !host.app_running() { + return Err(CommandError::NotRunning); + } + if !host.server_enabled()? { + return Err(CommandError::ServerOff); + } + let port = host.server_port()?; + let token = host.token()?.ok_or(CommandError::NoToken)?; + Ok(Endpoint { port, token }) +} diff --git a/core/crates/cli/src/exit.rs b/core/crates/cli/src/exit.rs new file mode 100644 index 0000000000..dc6d0ff813 --- /dev/null +++ b/core/crates/cli/src/exit.rs @@ -0,0 +1,39 @@ +use std::process::ExitCode; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum Exit { + Success = 0, + Failure = 1, + Usage = 2, + Refused = 3, + TimedOut = 4, +} + +impl Exit { + pub fn code(self) -> u8 { + self as u8 + } +} + +impl From for ExitCode { + fn from(exit: Exit) -> Self { + ExitCode::from(exit.code()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn each_status_has_its_documented_number() { + let codes: Vec = [Exit::Success, Exit::Failure, Exit::Usage, Exit::Refused, Exit::TimedOut] + .into_iter() + .map(Exit::code) + .collect(); + + assert_eq!(codes, vec![0, 1, 2, 3, 4]); + assert_eq!(ExitCode::from(Exit::Refused), ExitCode::from(3)); + } +} diff --git a/core/crates/cli/src/host.rs b/core/crates/cli/src/host.rs new file mode 100644 index 0000000000..01242a2b2b --- /dev/null +++ b/core/crates/cli/src/host.rs @@ -0,0 +1,60 @@ +use std::io::IsTerminal; +use std::net::SocketAddr; + +// Each app reads these its own way: Linux from GSettings, the keyring and the session bus. +pub trait Host { + fn app_version(&self) -> String; + fn app_running(&self) -> bool; + fn server_enabled(&self) -> Result; + fn server_port(&self) -> Result; + // Another user can bind the port before TablePro does, so the token goes only to a listener this one confirms. + fn verify_peer(&self, peer: SocketAddr, local: SocketAddr) -> Result<(), HostError>; + // Read only: a token made here would be one the running server never accepts. + fn token(&self) -> Result, HostError>; + fn terminal(&self) -> Terminal; +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Terminal { + pub stdin: bool, + pub stdout: bool, + pub stderr: bool, +} + +impl Terminal { + pub fn detect() -> Self { + Self { + stdin: std::io::stdin().is_terminal(), + stdout: std::io::stdout().is_terminal(), + stderr: std::io::stderr().is_terminal(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum HostError { + #[error("Could not read TablePro's settings: {reason}")] + Settings { reason: String }, + #[error("Could not read the MCP token from the keyring: {reason}")] + Keyring { reason: String }, + #[error( + "Another program holds port {port}, so the command did not send it TablePro's token. Quit that program, or pick another port in Preferences > MCP." + )] + ForeignServer { port: u16 }, + #[error("Nothing listens on port {port}.")] + NotListening { port: u16 }, + #[error("Could not check which program answers on port {port}: {reason}")] + Unverified { port: u16, reason: String }, +} + +impl HostError { + pub fn code(&self) -> &'static str { + match self { + Self::Settings { .. } => "cli.settings_unreadable", + Self::Keyring { .. } => "cli.keyring_unreadable", + Self::ForeignServer { .. } => "cli.foreign_server", + Self::NotListening { .. } => "cli.not_listening", + Self::Unverified { .. } => "cli.server_unverified", + } + } +} diff --git a/core/crates/cli/src/lib.rs b/core/crates/cli/src/lib.rs new file mode 100644 index 0000000000..dcfd577a37 --- /dev/null +++ b/core/crates/cli/src/lib.rs @@ -0,0 +1,19 @@ +mod answers; +mod client; +mod command_error; +mod command_line; +mod commands; +mod console; +mod dispatch; +mod endpoint; +mod exit; +mod host; +mod output; +mod peer_verification; +mod runner; +mod tool_arguments; + +pub use dispatch::{Dispatch, classify}; +pub use exit::Exit; +pub use host::{Host, HostError, Terminal}; +pub use runner::run; diff --git a/core/crates/cli/src/output/escape.rs b/core/crates/cli/src/output/escape.rs new file mode 100644 index 0000000000..8a157ef587 --- /dev/null +++ b/core/crates/cli/src/output/escape.rs @@ -0,0 +1,69 @@ +use std::borrow::Cow; +use std::fmt::Write as _; + +// A cell goes to a terminal, so a stored escape sequence is shown, never run. +pub(crate) fn escape_cell(text: &str) -> Cow<'_, str> { + if !text.chars().any(char::is_control) { + return Cow::Borrowed(text); + } + let mut escaped = String::with_capacity(text.len() + 8); + for character in text.chars() { + match character { + '\\' => escaped.push_str("\\\\"), + '\t' => escaped.push_str("\\t"), + '\n' => escaped.push_str("\\n"), + '\r' => escaped.push_str("\\r"), + other if other.is_control() => push_hex(&mut escaped, other), + other => escaped.push(other), + } + } + Cow::Owned(escaped) +} + +// Line breaks and tabs stay, so a database's message keeps the caret under the column it points at. +pub(crate) fn escape_lines(text: &str) -> Cow<'_, str> { + if !text.chars().any(runs_in_a_terminal) { + return Cow::Borrowed(text); + } + let mut escaped = String::with_capacity(text.len() + 8); + for character in text.chars() { + match character { + '\r' => escaped.push_str("\\r"), + other if runs_in_a_terminal(other) => push_hex(&mut escaped, other), + other => escaped.push(other), + } + } + Cow::Owned(escaped) +} + +fn runs_in_a_terminal(character: char) -> bool { + character.is_control() && character != '\n' && character != '\t' +} + +fn push_hex(into: &mut String, character: char) { + let _ = write!(into, "\\x{:02X}", u32::from(character)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_cell_shows_its_control_characters_as_escapes() { + assert_eq!(escape_cell("plain \\ text"), "plain \\ text"); + assert_eq!(escape_cell("a\tb\nc\r\u{1b}[2J\\"), "a\\tb\\nc\\r\\x1B[2J\\\\"); + assert_eq!(escape_cell("\u{9b}31m"), "\\x9B31m"); + } + + #[test] + fn a_message_keeps_its_lines_and_loses_its_sequences() { + assert_eq!( + escape_lines("syntax error\nLINE 1: SELEC\n\t^"), + "syntax error\nLINE 1: SELEC\n\t^" + ); + assert_eq!( + escape_lines("bad value \u{1b}]52;c;eA==\u{7}\r"), + "bad value \\x1B]52;c;eA==\\x07\\r" + ); + } +} diff --git a/core/crates/cli/src/output/format.rs b/core/crates/cli/src/output/format.rs new file mode 100644 index 0000000000..2a264c4ee8 --- /dev/null +++ b/core/crates/cli/src/output/format.rs @@ -0,0 +1,32 @@ +use clap::ValueEnum; +use serde::Serialize; + +use crate::host::Terminal; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] +pub(crate) enum OutputFormat { + Csv, + Tsv, + Json, + Jsonl, + Table, +} + +impl OutputFormat { + // A person reads a terminal; a pipe or a file is read by a program. + pub(crate) fn or_default(format: Option, terminal: Terminal) -> Self { + match (format, terminal.stdout) { + (Some(format), _) => format, + (None, true) => Self::Table, + (None, false) => Self::Csv, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Serialize)] +#[serde(rename_all = "snake_case")] +pub(crate) enum ExportFormat { + Csv, + Json, + Sql, +} diff --git a/core/crates/cli/src/output/mod.rs b/core/crates/cli/src/output/mod.rs new file mode 100644 index 0000000000..3f199ab86f --- /dev/null +++ b/core/crates/cli/src/output/mod.rs @@ -0,0 +1,9 @@ +mod escape; +mod format; +mod table; +mod writer; + +pub(crate) use escape::escape_lines; +pub(crate) use format::{ExportFormat, OutputFormat}; +pub(crate) use table::{Cell, Table}; +pub(crate) use writer::{json_document, render}; diff --git a/core/crates/cli/src/output/table.rs b/core/crates/cli/src/output/table.rs new file mode 100644 index 0000000000..9c21ae577d --- /dev/null +++ b/core/crates/cli/src/output/table.rs @@ -0,0 +1,83 @@ +use std::fmt::Display; + +use serde_json::value::RawValue; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum Cell { + Null, + Text(String), + // A number, a boolean or a nested document as the server wrote it, so no digit is lost on the way. + Json(String), +} + +impl Cell { + pub(crate) fn from_raw(raw: &RawValue) -> Self { + let text = raw.get().trim(); + match text.as_bytes().first() { + Some(b'n') => Self::Null, + Some(b'"') => serde_json::from_str(text).map_or_else(|_| Self::Json(text.to_owned()), Self::Text), + _ => Self::Json(text.to_owned()), + } + } + + pub(crate) fn text(value: impl Into) -> Self { + Self::Text(value.into()) + } + + pub(crate) fn optional(value: Option) -> Self { + value.map_or(Self::Null, Self::Text) + } + + pub(crate) fn literal(value: impl Display) -> Self { + Self::Json(value.to_string()) + } + + pub(crate) fn as_text(&self) -> Option<&str> { + match self { + Self::Null => None, + Self::Text(text) | Self::Json(text) => Some(text), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Table { + pub(crate) columns: Vec, + pub(crate) rows: Vec>, +} + +impl Table { + pub(crate) fn new(columns: &[&str]) -> Self { + Self { + columns: columns.iter().map(|&column| column.to_owned()).collect(), + rows: Vec::new(), + } + } + + pub(crate) fn push(&mut self, row: Vec) { + self.rows.push(row); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn cell(json: &str) -> Cell { + let raw: Box = serde_json::from_str(json).expect("JSON"); + Cell::from_raw(&raw) + } + + #[test] + fn a_value_keeps_the_text_the_server_sent() { + assert_eq!(cell("null"), Cell::Null); + assert_eq!(cell(r#""a \"b\"""#), Cell::text("a \"b\"")); + assert_eq!(cell("18446744073709551615"), Cell::literal("18446744073709551615")); + assert_eq!( + cell("12345678901234567890.12345678901234567890"), + Cell::literal("12345678901234567890.12345678901234567890") + ); + assert_eq!(cell("true"), Cell::literal(true)); + assert_eq!(cell(r#"{"k":[1,2]}"#), Cell::literal(r#"{"k":[1,2]}"#)); + } +} diff --git a/core/crates/cli/src/output/writer.rs b/core/crates/cli/src/output/writer.rs new file mode 100644 index 0000000000..72c8b08a6f --- /dev/null +++ b/core/crates/cli/src/output/writer.rs @@ -0,0 +1,434 @@ +// The formats are the macOS command's (CLIRowWriter.swift), so a script reads the same text on every platform. + +use std::borrow::Cow; +use std::collections::HashSet; +use std::fmt::Write as _; + +use unicode_width::UnicodeWidthStr; + +use super::escape::escape_cell; +use super::format::OutputFormat; +use super::table::{Cell, Table}; + +const NULL_MARKER: &str = "NULL"; + +static MISSING: Cell = Cell::Null; + +pub(crate) fn render(tables: &[Table], format: OutputFormat) -> String { + let printable: Vec<&Table> = tables.iter().filter(|table| !table.columns.is_empty()).collect(); + let mut text = String::new(); + match (printable.as_slice(), format) { + ([], _) => {} + ([table], _) => render_one(table, format, &mut text), + // One document either way: several result sets nest as one array each. + (many, OutputFormat::Json) => { + text.push_str("[\n"); + for (index, table) in many.iter().enumerate() { + json_array(table, " ", index + 1 == many.len(), &mut text); + } + text.push_str("]\n"); + } + (many, OutputFormat::Jsonl) => { + for table in many { + render_one(table, format, &mut text); + } + } + (many, _) => { + for (index, table) in many.iter().enumerate() { + if index > 0 { + text.push('\n'); + } + render_one(table, format, &mut text); + } + } + } + text +} + +fn render_one(table: &Table, format: OutputFormat, text: &mut String) { + match format { + OutputFormat::Csv => delimited(table, ',', csv_field, text), + OutputFormat::Tsv => delimited(table, '\t', tsv_field, text), + OutputFormat::Json => json_array(table, "", true, text), + OutputFormat::Jsonl => { + let keys = unique_keys(&table.columns); + for row in &table.rows { + json_object(&keys, row, text); + text.push('\n'); + } + } + OutputFormat::Table => grid(table, text), + } +} + +fn cells(row: &[Cell], count: usize) -> impl Iterator { + row.iter().chain(std::iter::repeat(&MISSING)).take(count) +} + +fn delimited(table: &Table, separator: char, field: fn(Option<&str>, &mut String), text: &mut String) { + for (index, column) in table.columns.iter().enumerate() { + if index > 0 { + text.push(separator); + } + field(Some(column), text); + } + text.push('\n'); + for row in &table.rows { + for (index, cell) in cells(row, table.columns.len()).enumerate() { + if index > 0 { + text.push(separator); + } + field(cell.as_text(), text); + } + text.push('\n'); + } +} + +// NULL is an empty field and an empty string is "", so the two stay apart. +fn csv_field(value: Option<&str>, text: &mut String) { + let Some(value) = value else { + return; + }; + let quoted = value.is_empty() + || value.chars().any(|character| { + matches!( + character, + ',' | '"' | '\n' | '\u{b}' | '\u{c}' | '\r' | '\u{85}' | '\u{2028}' | '\u{2029}' + ) + }); + if !quoted { + text.push_str(value); + return; + } + text.push('"'); + text.push_str(&value.replace('"', "\"\"")); + text.push('"'); +} + +fn tsv_field(value: Option<&str>, text: &mut String) { + let Some(value) = value else { + return; + }; + for character in value.chars() { + match character { + '\\' => text.push_str("\\\\"), + '\t' => text.push_str("\\t"), + '\n' => text.push_str("\\n"), + '\r' => text.push_str("\\r"), + other => text.push(other), + } + } +} + +fn json_array(table: &Table, indent: &str, last: bool, text: &mut String) { + let close = match last { + true => "]\n", + false => "],\n", + }; + text.push_str(indent); + if table.rows.is_empty() { + text.push('['); + text.push_str(close); + return; + } + let keys = unique_keys(&table.columns); + text.push_str("[\n"); + for (index, row) in table.rows.iter().enumerate() { + text.push_str(indent); + text.push_str(" "); + json_object(&keys, row, text); + text.push_str(match index + 1 == table.rows.len() { + true => "\n", + false => ",\n", + }); + } + text.push_str(indent); + text.push_str(close); +} + +fn json_object(keys: &[String], row: &[Cell], text: &mut String) { + text.push('{'); + for (index, (key, cell)) in keys.iter().zip(cells(row, keys.len())).enumerate() { + if index > 0 { + text.push(','); + } + json_string(key, text); + text.push(':'); + match cell { + Cell::Null => text.push_str("null"), + Cell::Text(value) => json_string(value, text), + Cell::Json(value) => text.push_str(&json_document(value)), + } + } + text.push('}'); +} + +fn json_string(value: &str, text: &mut String) { + text.push('"'); + for character in value.chars() { + match character { + '"' => text.push_str("\\\""), + '\\' => text.push_str("\\\\"), + other if other.is_control() => { + let _ = write!(text, "\\u{:04x}", u32::from(other)); + } + other => text.push(other), + } + } + text.push('"'); +} + +// JSON permits raw DEL and C1 in strings, but a terminal must never receive them. +pub(crate) fn json_document(value: &str) -> Cow<'_, str> { + if !value + .chars() + .any(|character| ('\u{7f}'..='\u{9f}').contains(&character)) + { + return Cow::Borrowed(value); + } + let mut text = String::with_capacity(value.len()); + for character in value.chars() { + if ('\u{7f}'..='\u{9f}').contains(&character) { + let _ = write!(text, "\\u{:04x}", u32::from(character)); + } else { + text.push(character); + } + } + Cow::Owned(text) +} + +// A join can name two columns alike, and an object keeps only one of two equal keys. +// The rule is the server's JSON export's (json_field_names), so a key reads the same in both. +fn unique_keys(columns: &[String]) -> Vec { + let mut reserved: HashSet = columns.iter().cloned().collect(); + let mut taken: HashSet = HashSet::with_capacity(columns.len()); + let mut keys = Vec::with_capacity(columns.len()); + for column in columns { + let mut key = column.clone(); + if !taken.insert(key.clone()) { + let mut suffix = 2; + loop { + let candidate = format!("{column}_{suffix}"); + if !reserved.contains(&candidate) && taken.insert(candidate.clone()) { + key = candidate; + break; + } + suffix += 1; + } + reserved.insert(key.clone()); + } + keys.push(key); + } + keys +} + +fn grid(table: &Table, text: &mut String) { + let header: Vec = table + .columns + .iter() + .map(|column| escape_cell(column).into_owned()) + .collect(); + let body: Vec> = table + .rows + .iter() + .map(|row| { + cells(row, header.len()) + .map(|cell| match cell.as_text() { + None => NULL_MARKER.to_owned(), + Some(value) => escape_cell(value).into_owned(), + }) + .collect() + }) + .collect(); + // Terminal cells, not characters: 日本語 takes six. + let mut widths: Vec = header.iter().map(|cell| cell.width()).collect(); + for row in &body { + for (width, cell) in widths.iter_mut().zip(row) { + *width = (*width).max(cell.width()); + } + } + grid_line(&header, &widths, text); + let rule: Vec = widths.iter().map(|&width| "-".repeat(width)).collect(); + text.push_str(&rule.join("-+-")); + text.push('\n'); + for row in &body { + grid_line(row, &widths, text); + } +} + +fn grid_line(cells: &[String], widths: &[usize], text: &mut String) { + for (index, (cell, &width)) in cells.iter().zip(widths).enumerate() { + if index > 0 { + text.push_str(" | "); + } + text.push_str(cell); + if index + 1 < cells.len() { + text.push_str(&" ".repeat(width.saturating_sub(cell.width()))); + } + } + text.push('\n'); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn people() -> Table { + let mut table = Table::new(&["id", "name", "note"]); + table.push(vec![Cell::literal(1), Cell::text("Ada"), Cell::Null]); + table.push(vec![Cell::literal(2), Cell::text("Grace, \"Amazing\""), Cell::text("")]); + table + } + + #[test] + fn csv_quotes_only_what_needs_it_and_keeps_null_apart_from_empty() { + assert_eq!( + render(&[people()], OutputFormat::Csv), + "id,name,note\n1,Ada,\n2,\"Grace, \"\"Amazing\"\"\",\"\"\n" + ); + } + + #[test] + fn csv_quotes_every_line_break() { + let mut table = Table::new(&["a"]); + table.push(vec![Cell::text("x\ny")]); + table.push(vec![Cell::text("x\u{2028}y")]); + + assert_eq!(render(&[table], OutputFormat::Csv), "a\n\"x\ny\"\n\"x\u{2028}y\"\n"); + } + + #[test] + fn tsv_escapes_tabs_line_breaks_and_backslashes() { + let mut table = Table::new(&["a", "b"]); + table.push(vec![Cell::text("x\ty\nz\r\\"), Cell::Null]); + + assert_eq!(render(&[table], OutputFormat::Tsv), "a\tb\nx\\ty\\nz\\r\\\\\t\n"); + } + + #[test] + fn json_is_one_array_of_objects_keeping_numbers_as_sent() { + let mut table = Table::new(&["n", "s"]); + table.push(vec![Cell::literal("18446744073709551615"), Cell::text("a\"\u{1}")]); + table.push(vec![Cell::Null, Cell::text("b")]); + + assert_eq!( + render(&[table], OutputFormat::Json), + "[\n {\"n\":18446744073709551615,\"s\":\"a\\\"\\u0001\"},\n {\"n\":null,\"s\":\"b\"}\n]\n" + ); + } + + #[test] + fn json_strings_escape_all_controls_and_preserve_other_unicode() { + for scalar in (0..=0x1f).chain(0x7f..=0x9f) { + let value = char::from_u32(scalar).expect("control scalar").to_string(); + let mut text = String::new(); + json_string(&value, &mut text); + assert_eq!(text, format!("\"\\u{scalar:04x}\"")); + assert_eq!(serde_json::from_str::(&text).expect("valid JSON"), value); + } + let mut text = String::new(); + json_string(" ~\u{a0}日本語😀", &mut text); + assert_eq!(text, "\" ~\u{a0}日本語😀\""); + } + + #[test] + fn nested_json_controls_keep_keys_values_and_number_precision() { + let value = "\u{7f}\u{85}\u{1b}"; + let mut table = Table::new(&[value]); + let nested = + format!("{{\"{value}\":[\"{value}\",12345678901234567890.1234567890]}}").replace('\u{1b}', "\\u001b"); + table.push(vec![Cell::literal(nested)]); + for format in [OutputFormat::Json, OutputFormat::Jsonl] { + let output = render(std::slice::from_ref(&table), format); + assert_eq!(super::super::escape_lines(&output), output); + let decoded: serde_json::Value = serde_json::from_str(&output).expect("valid JSON"); + let row = if format == OutputFormat::Json { + &decoded[0] + } else { + &decoded + }; + assert_eq!(row[value][value][0], value); + assert!(output.contains("12345678901234567890.1234567890")); + } + } + + #[test] + fn json_with_no_rows_is_an_empty_array() { + assert_eq!(render(&[Table::new(&["a"])], OutputFormat::Json), "[]\n"); + } + + #[test] + fn several_result_sets_nest_in_json_and_stack_elsewhere() { + let mut first = Table::new(&["a"]); + first.push(vec![Cell::literal(1)]); + let mut second = Table::new(&["b"]); + second.push(vec![Cell::literal(2)]); + let sets = [first, Table::new(&[]), second]; + + assert_eq!( + render(&sets, OutputFormat::Json), + "[\n [\n {\"a\":1}\n ],\n [\n {\"b\":2}\n ]\n]\n" + ); + assert_eq!(render(&sets, OutputFormat::Csv), "a\n1\n\nb\n2\n"); + assert_eq!(render(&sets, OutputFormat::Jsonl), "{\"a\":1}\n{\"b\":2}\n"); + } + + #[test] + fn repeated_column_names_get_numbered_keys() { + let columns: Vec = ["id", "id", "id_2"].iter().map(|&name| name.to_owned()).collect(); + + assert_eq!(unique_keys(&columns), vec!["id", "id_3", "id_2"]); + } + + #[test] + fn a_short_row_reads_as_null_and_a_long_one_is_cut() { + let mut table = Table::new(&["a", "b"]); + table.push(vec![Cell::literal(1)]); + table.push(vec![Cell::literal(1), Cell::literal(2), Cell::literal(3)]); + + assert_eq!( + render(&[table], OutputFormat::Jsonl), + "{\"a\":1,\"b\":null}\n{\"a\":1,\"b\":2}\n" + ); + } + + #[test] + fn the_table_pads_columns_and_shows_null() { + let mut table = Table::new(&["id", "name", "note"]); + table.push(vec![Cell::literal(1), Cell::text("Ada"), Cell::Null]); + table.push(vec![Cell::literal(2), Cell::text("Bo"), Cell::text("")]); + + let expected = [ + "id | name | note", + "---+------+-----", + "1 | Ada | NULL", + "2 | Bo | ", + ] + .join("\n") + + "\n"; + assert_eq!(render(&[table], OutputFormat::Table), expected); + } + + #[test] + fn the_table_lines_up_wide_characters_by_terminal_cells() { + let mut table = Table::new(&["name", "id"]); + table.push(vec![Cell::text("日本語"), Cell::literal(1)]); + table.push(vec![Cell::text("abc"), Cell::literal(2)]); + + assert_eq!( + render(&[table], OutputFormat::Table), + "name | id\n-------+---\n日本語 | 1\nabc | 2\n" + ); + } + + #[test] + fn the_table_escapes_control_characters() { + let mut table = Table::new(&["note"]); + table.push(vec![Cell::text("\u{1b}[2Jgone")]); + + assert_eq!( + render(&[table], OutputFormat::Table), + "note\n-----------\n\\x1B[2Jgone\n" + ); + } +} diff --git a/core/crates/cli/src/peer_verification.rs b/core/crates/cli/src/peer_verification.rs new file mode 100644 index 0000000000..4edda4e914 --- /dev/null +++ b/core/crates/cli/src/peer_verification.rs @@ -0,0 +1,41 @@ +use std::error::Error; +use std::time::Duration; + +use hyper_util::client::legacy::connect::{Connection, HttpInfo}; + +use crate::host::{Host, HostError}; + +type ConnectError = Box; + +pub(crate) fn http_client( + host: &(impl Host + Clone + Send + Sync + 'static), + port: u16, +) -> Result { + let host = host.clone(); + // A proxy or redirect could carry the token to a different server. + reqwest::Client::builder() + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(Duration::from_secs(10)) + .connector_layer(tower::util::MapResultLayer::new(move |connection| { + verify_connection(connection, &host, port) + })) + .build() +} + +fn verify_connection( + connection: Result, + host: &dyn Host, + port: u16, +) -> Result { + let connection = connection?; + let mut extras = http::Extensions::new(); + connection.connected().get_extras(&mut extras); + let addresses = extras.get::().ok_or_else(|| HostError::Unverified { + port, + reason: "the connection did not report its addresses".to_owned(), + })?; + // Returning the stream lets HTTP write the token, so refusal must drop this same stream first. + host.verify_peer(addresses.remote_addr(), addresses.local_addr())?; + Ok(connection) +} diff --git a/core/crates/cli/src/runner.rs b/core/crates/cli/src/runner.rs new file mode 100644 index 0000000000..10f49aeadc --- /dev/null +++ b/core/crates/cli/src/runner.rs @@ -0,0 +1,373 @@ +use std::ffi::OsString; +use std::io::Write; + +use clap_complete::aot::Generator; + +use crate::command_error::CommandError; +use crate::command_line::{self, Command, Shell}; +use crate::commands; +use crate::console::Console; +use crate::exit::Exit; +use crate::host::Host; + +pub fn run( + args: Vec, + host: &(impl Host + Clone + Send + Sync + 'static), + out: &mut dyn Write, + err: &mut dyn Write, +) -> Exit { + let mut console = Console::new(out, err, host.terminal()); + let version = host.app_version(); + let cli = match command_line::parse(args, &version) { + Ok(cli) => cli, + Err(error) => return console.parse_failed(&error), + }; + match cli.command { + None | Some(Command::Open(_)) => console.fail(&CommandError::OpensInApp), + Some(Command::Completion(arguments)) => completion(arguments.shell, &version, &mut console), + Some(command) => commands::execute(command, host, &mut console), + } +} + +fn completion(shell: Shell, version: &str, console: &mut Console<'_>) -> Exit { + let mut command = command_line::command(version); + command.build(); + let mut script = Vec::new(); + let written = shell + .generator() + .try_generate(&command, &mut script) + .map_err(CommandError::Output) + .and_then(|()| console.write_out(&script)); + match written { + Ok(()) => Exit::Success, + Err(error) => console.fail(&error), + } +} + +#[cfg(test)] +pub(crate) mod tests { + use std::io::Read; + use std::net::SocketAddr; + use std::sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering}, + }; + + use super::*; + use crate::host::{HostError, Terminal}; + + #[derive(Clone)] + pub(crate) struct Offline { + running: bool, + enabled: bool, + terminal: Terminal, + } + + impl Host for Offline { + fn app_version(&self) -> String { + "1.2.3".to_owned() + } + + fn app_running(&self) -> bool { + self.running + } + + fn server_enabled(&self) -> Result { + Ok(self.enabled) + } + + fn server_port(&self) -> Result { + Ok(1) + } + + fn verify_peer(&self, _peer: SocketAddr, _local: SocketAddr) -> Result<(), HostError> { + Ok(()) + } + + fn token(&self) -> Result, HostError> { + Ok(None) + } + + fn terminal(&self) -> Terminal { + self.terminal + } + } + + pub(crate) const STOPPED: Offline = Offline { + running: false, + enabled: true, + terminal: Terminal { + stdin: false, + stdout: false, + stderr: false, + }, + }; + + // Counts each keyring read, and can find its port held by another user's program. + #[derive(Clone)] + struct Counted { + running: bool, + enabled: bool, + ours: Arc, + peer_checks: Arc, + port: u16, + token: Option<&'static str>, + token_reads: Arc, + } + + impl Counted { + fn new(running: bool, enabled: bool, ours: bool, port: u16, token: Option<&'static str>) -> Self { + Self { + running, + enabled, + ours: Arc::new(AtomicBool::new(ours)), + peer_checks: Arc::new(AtomicUsize::new(0)), + port, + token, + token_reads: Arc::new(AtomicUsize::new(0)), + } + } + } + + impl Host for Counted { + fn app_version(&self) -> String { + "1.2.3".to_owned() + } + + fn app_running(&self) -> bool { + self.running + } + + fn server_enabled(&self) -> Result { + Ok(self.enabled) + } + + fn server_port(&self) -> Result { + Ok(self.port) + } + + fn verify_peer(&self, peer: SocketAddr, _local: SocketAddr) -> Result<(), HostError> { + self.peer_checks.fetch_add(1, Ordering::SeqCst); + match self.ours.load(Ordering::SeqCst) { + true => Ok(()), + false => Err(HostError::ForeignServer { port: peer.port() }), + } + } + + fn token(&self) -> Result, HostError> { + self.token_reads.fetch_add(1, Ordering::SeqCst); + Ok(self.token.map(str::to_owned)) + } + + fn terminal(&self) -> Terminal { + Terminal::default() + } + } + + fn invoke(host: &(impl Host + Clone + Send + Sync + 'static), words: &[&str]) -> (Exit, String, String) { + let (mut out, mut err) = (Vec::new(), Vec::new()); + let exit = run(words.iter().map(OsString::from).collect(), host, &mut out, &mut err); + ( + exit, + String::from_utf8(out).expect("UTF-8"), + String::from_utf8(err).expect("UTF-8"), + ) + } + + #[test] + fn help_and_version_print_to_standard_output() { + let (exit, out, err) = invoke(&STOPPED, &["tablepro", "--help"]); + assert_eq!(exit, Exit::Success); + assert!(out.contains("Usage: tablepro"), "{out}"); + assert!(out.contains("query"), "{out}"); + assert!(err.is_empty(), "{err}"); + + let (exit, out, _) = invoke(&STOPPED, &["tablepro", "--version"]); + assert_eq!((exit, out.as_str()), (Exit::Success, "tablepro 1.2.3\n")); + } + + #[test] + fn a_usage_error_goes_to_standard_error_with_status_two() { + let (exit, out, err) = invoke(&STOPPED, &["tablepro", "query", "--format", "xml", "SELECT 1"]); + + assert_eq!(exit, Exit::Usage); + assert!(out.is_empty(), "{out}"); + assert!(err.starts_with("error:"), "{err}"); + } + + #[test] + fn each_shell_gets_its_completion_script() { + for (shell, start) in [("zsh", "#compdef tablepro"), ("bash", "_tablepro()"), ("fish", "")] { + let (exit, out, _) = invoke(&STOPPED, &["tablepro", "completion", shell]); + assert_eq!(exit, Exit::Success, "{shell}"); + assert!(out.starts_with(start), "{shell}: {out}"); + assert!(out.contains("query"), "{shell}"); + } + } + + #[test] + fn a_stopped_app_or_a_server_turned_off_is_reported_before_any_request() { + let (exit, _, err) = invoke(&STOPPED, &["tablepro", "connections"]); + assert_eq!(exit, Exit::Failure); + assert_eq!( + err, + "tablepro: TablePro is not running. Start it, then run the command again.\n" + ); + + let off = Offline { + running: true, + enabled: false, + ..STOPPED + }; + let (exit, _, err) = invoke(&off, &["tablepro", "connections"]); + assert_eq!(exit, Exit::Refused); + assert!(err.contains("Enable MCP Server"), "{err}"); + + let started = Offline { enabled: true, ..off }; + let (exit, _, err) = invoke(&started, &["tablepro", "connections"]); + assert_eq!(exit, Exit::Failure); + assert!(err.contains("has not started"), "{err}"); + } + + #[test] + fn missing_sql_on_a_terminal_is_a_usage_error_before_the_app_is_asked() { + let typing = Offline { + terminal: Terminal { + stdin: true, + ..Terminal::default() + }, + ..STOPPED + }; + + let (exit, _, err) = invoke(&typing, &["tablepro", "query", "-c", "Staging"]); + + assert_eq!(exit, Exit::Usage); + assert!(err.contains("Pass the SQL"), "{err}"); + } + + #[test] + fn opening_is_left_to_the_app() { + let (exit, _, _) = invoke(&STOPPED, &["tablepro", "app.db"]); + + assert_eq!(exit, Exit::Usage); + } + + #[test] + fn status_reports_every_reading_and_exits_as_a_command_would() { + let (exit, out, err) = invoke(&STOPPED, &["tablepro", "status", "--format", "csv"]); + + assert_eq!(exit, Exit::Failure); + assert_eq!( + out, + "version,running,server_enabled,port,token_found,server_version,ready\n1.2.3,false,true,1,,,false\n" + ); + assert!(err.contains("not running"), "{err}"); + } + + #[test] + fn status_reads_the_token_only_when_a_command_would() { + for (host, reads, token_found) in [ + (Counted::new(false, true, true, 1, None), 0, ""), + (Counted::new(true, false, true, 1, None), 0, ""), + (Counted::new(true, true, false, 1, None), 1, "false"), + (Counted::new(true, true, true, 1, None), 1, "false"), + ] { + let (_, out, _) = invoke(&host, &["tablepro", "status", "--format", "csv"]); + + assert_eq!(host.token_reads.load(Ordering::SeqCst), reads, "{out}"); + let row = out.lines().nth(1).unwrap_or_default(); + assert_eq!(row.split(',').nth(4), Some(token_found), "{row}"); + } + } + + #[test] + fn a_port_another_program_holds_never_gets_the_token() { + let squatter = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); + squatter.set_nonblocking(true).expect("a non-blocking listener"); + let port = squatter.local_addr().expect("an address").port(); + let host = Counted::new(true, true, false, port, Some("tp_secret")); + + let (exit, _, err) = invoke(&host, &["tablepro", "connections"]); + + assert_eq!(exit, Exit::Failure); + assert_eq!( + err, + format!( + "tablepro: Another program holds port {port}, so the command did not send it TablePro's token. \ + Quit that program, or pick another port in Preferences > MCP.\n" + ) + ); + assert_eq!(host.token_reads.load(Ordering::SeqCst), 1); + let (mut stream, _) = squatter.accept().expect("the connection being verified"); + stream + .set_read_timeout(Some(std::time::Duration::from_secs(1))) + .expect("a deadline"); + let mut bytes = Vec::new(); + stream.read_to_end(&mut bytes).expect("a closed connection"); + assert!(bytes.is_empty(), "the refused peer received request bytes"); + } + + #[tokio::test] + async fn reused_connections_stay_verified_and_new_connections_are_checked_again() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a port"); + let address = listener.local_addr().expect("an address"); + let host = Counted::new(true, true, true, address.port(), Some("tp_secret")); + let http = crate::peer_verification::http_client(&host, address.port()).expect("a client"); + let server = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.expect("a verified connection"); + for close in [false, true] { + let mut request = Vec::new(); + while !request.ends_with(b"\r\n\r\n") { + request.push(stream.read_u8().await.expect("request headers")); + } + assert!( + String::from_utf8(request) + .expect("HTTP headers") + .contains("Bearer tp_secret") + ); + let response = if close { + "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" + } else { + "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" + }; + stream.write_all(response.as_bytes()).await.expect("a response"); + } + drop(stream); + let (mut refused, _) = listener.accept().await.expect("a new connection"); + let mut bytes = Vec::new(); + refused + .read_to_end(&mut bytes) + .await + .expect("the refused connection closes"); + assert!(bytes.is_empty(), "a new unverified connection received bytes"); + }); + let exchange = async { + for _ in 0..2 { + http.get(format!("http://{address}/mcp")) + .bearer_auth("tp_secret") + .send() + .await + .expect("a verified request") + .bytes() + .await + .expect("the response body"); + } + assert_eq!(host.peer_checks.load(Ordering::SeqCst), 1); + host.ours.store(false, Ordering::SeqCst); + assert!( + http.get(format!("http://{address}/mcp")) + .bearer_auth("tp_secret") + .send() + .await + .is_err() + ); + assert_eq!(host.peer_checks.load(Ordering::SeqCst), 2); + server.await.expect("the listener saw no refused bytes"); + }; + tokio::time::timeout(std::time::Duration::from_secs(5), exchange) + .await + .expect("bounded exchanges"); + } +} diff --git a/core/crates/cli/src/tool_arguments.rs b/core/crates/cli/src/tool_arguments.rs new file mode 100644 index 0000000000..714737151d --- /dev/null +++ b/core/crates/cli/src/tool_arguments.rs @@ -0,0 +1,110 @@ +// The tools refuse a field they do not know, so an option the user left out is left out here too. + +use serde::Serialize; + +use crate::output::ExportFormat; + +#[derive(Serialize)] +pub(crate) struct NoArguments {} + +#[derive(Serialize)] +pub(crate) struct ConnectionArguments<'a> { + pub(crate) connection_id: &'a str, +} + +#[derive(Serialize)] +pub(crate) struct ScopeArguments<'a> { + pub(crate) connection_id: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) database: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) schema: Option<&'a str>, +} + +#[derive(Serialize)] +pub(crate) struct TableArguments<'a> { + pub(crate) connection_id: &'a str, + pub(crate) table: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) database: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) schema: Option<&'a str>, +} + +#[derive(Serialize)] +pub(crate) struct QueryArguments<'a> { + pub(crate) connection_id: &'a str, + pub(crate) query: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) database: Option<&'a str>, + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub(crate) read_only: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) max_rows: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) timeout_seconds: Option, +} + +#[derive(Serialize)] +pub(crate) struct ExportArguments<'a> { + pub(crate) connection_id: &'a str, + pub(crate) format: ExportFormat, + pub(crate) tables: &'a [String], + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) database: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) schema: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) max_rows: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_option_left_out_is_not_sent() { + let bare = QueryArguments { + connection_id: "c", + query: "SELECT 1", + database: None, + read_only: false, + max_rows: None, + timeout_seconds: None, + }; + let full = QueryArguments { + database: Some("sales"), + read_only: true, + max_rows: Some(5), + timeout_seconds: Some(9), + ..bare + }; + + assert_eq!( + serde_json::to_string(&bare).expect("JSON"), + r#"{"connection_id":"c","query":"SELECT 1"}"# + ); + assert_eq!( + serde_json::to_string(&full).expect("JSON"), + r#"{"connection_id":"c","query":"SELECT 1","database":"sales","read_only":true,"max_rows":5,"timeout_seconds":9}"# + ); + } + + #[test] + fn an_export_names_its_format_as_the_tool_does() { + let tables = vec!["orders".to_owned()]; + let arguments = ExportArguments { + connection_id: "c", + format: ExportFormat::Sql, + tables: &tables, + database: None, + schema: Some("public"), + max_rows: None, + }; + + assert_eq!( + serde_json::to_string(&arguments).expect("JSON"), + r#"{"connection_id":"c","format":"sql","tables":["orders"],"schema":"public"}"# + ); + } +} diff --git a/core/crates/cli/tests/cli_over_mcp.rs b/core/crates/cli/tests/cli_over_mcp.rs new file mode 100644 index 0000000000..28327dac1e --- /dev/null +++ b/core/crates/cli/tests/cli_over_mcp.rs @@ -0,0 +1,721 @@ +// The command against the MCP server it talks to in the app, over real SQLite sessions. + +#![expect( + clippy::expect_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::collections::HashMap; +use std::ffi::OsString; +use std::path::Path; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; + +use async_trait::async_trait; +use secrecy::SecretString; +use tablepro_cli::{Exit, Host, HostError, Terminal}; +use tablepro_core::credentials::CredentialInteraction; +use tablepro_core::dialect::SqlDialect; +use tablepro_core::{ + ConnectEnvironment, ExternalAccess, FileEndpoint, LivenessPolicy, ResultEvent, ResultSink, RunOptions, + SafeModeLevel, SinkFlow, TlsConfig, UserSql, WorkspaceSettings, +}; +use tablepro_driver_sqlite::SqliteDriver; +use tablepro_mcp::{AccessToken, McpHost, McpServer, OpenOutcome, ToolContext, WriteApproval}; +use tablepro_session::runtime::Tasks; +use tablepro_session::{ + ConnectRequest, ConnectServices, ConnectionMetadata, Connector, DefaultDeadlines, DriverConnector, + NoStoredCredentials, RequestTarget, Session, SessionDeps, +}; +use tablepro_ssh::{SshRuntimeCell, SshServices}; +use tablepro_storage::query_history::NewEntry; +use tablepro_storage::{ConnectionStore, SavedConnection, StoragePaths}; +use tokio_util::sync::CancellationToken; +use uuid::Uuid; + +const TOKEN: &str = "tp_test_token"; + +#[derive(Default)] +struct App { + sessions: Mutex>>, + // What connect answers for a session that is listed but shut down: open, though every tool still finds it closed. + reopens: Mutex>>, + opens: AtomicUsize, + approves: AtomicBool, +} + +#[async_trait] +impl McpHost for App { + async fn session(&self, id: Uuid) -> Option> { + self.sessions.lock().expect("sessions").get(&id).cloned() + } + + async fn open_connections(&self) -> Vec { + self.sessions.lock().expect("sessions").keys().copied().collect() + } + + async fn open(&self, connection: SavedConnection) -> OpenOutcome { + self.opens.fetch_add(1, Ordering::SeqCst); + match self.reopens.lock().expect("reopens").get(&connection.id) { + Some(session) => OpenOutcome::Open(session.clone()), + None => OpenOutcome::Closed, + } + } + + async fn approve(&self, _approval: WriteApproval) -> bool { + self.approves.load(Ordering::SeqCst) + } + + async fn record(&self, _entry: NewEntry, _dialect: Arc) {} +} + +struct Ids { + readable: Uuid, + writable: Uuid, + blocked: Uuid, + closed: Uuid, + twin: Uuid, + other_twin: Uuid, + stale: Uuid, +} + +struct Harness { + _dir: tempfile::TempDir, + server: McpServer, + app: Arc, + ids: Ids, +} + +fn saved(id: Uuid, name: &str, file: &Path, access: ExternalAccess) -> SavedConnection { + SavedConnection { + id, + name: name.to_owned(), + driver_id: "sqlite".to_owned(), + host: String::new(), + port: 0, + database: file.display().to_string(), + username: String::new(), + tls: TlsConfig::disabled(), + safe_mode: SafeModeLevel::Off, + external_access: access, + ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, + auth_mode: tablepro_core::AuthMode::Password, + ssh: None, + last_opened_at: None, + color: None, + group_id: None, + order: 0, + tags: Vec::new(), + icon: None, + options: Default::default(), + } +} + +async fn open_session(dir: &Path, file: &Path, id: Uuid, create: bool) -> Arc { + let connector = Arc::new(DriverConnector::new( + ConnectServices { + environment: ConnectEnvironment::new("test"), + ssh: Arc::new(SshServices { + runtime: Arc::new(SshRuntimeCell::new(dir.join("runtime"))), + ssh_program: "ssh".into(), + ssh_keygen_program: "ssh-keygen".into(), + askpass_program: "/nonexistent/tablepro-askpass".into(), + config_file: None, + }), + liveness: LivenessPolicy::DESKTOP, + }, + Arc::new(DefaultDeadlines::new(LivenessPolicy::DESKTOP)), + )); + let endpoint = match create { + true => FileEndpoint::create_new(file.to_owned()), + false => FileEndpoint::open_existing(file.to_owned()), + } + .expect("a file endpoint"); + let request = ConnectRequest { + metadata: ConnectionMetadata { + id, + name: "shop".to_owned(), + driver_id: "sqlite".to_owned(), + }, + driver: Arc::new(SqliteDriver), + target: RequestTarget::File(endpoint), + workspace_settings: WorkspaceSettings::workspace(false), + safe_mode: SafeModeLevel::Off, + }; + let pending = connector + .establish(request, CredentialInteraction::Unattended, CancellationToken::new()) + .await + .expect("an open SQLite file"); + Session::start( + pending, + SessionDeps { + connector, + credentials: Arc::new(NoStoredCredentials), + deadlines: Arc::new(DefaultDeadlines::new(LivenessPolicy::DESKTOP)), + tasks: Tasks::new(tokio::runtime::Handle::current()), + prompter: None, + }, + ) +} + +struct Discard; + +#[async_trait] +impl ResultSink for Discard { + async fn accept(&mut self, _event: ResultEvent) -> SinkFlow { + SinkFlow::Continue + } +} + +async fn seed(session: &Session) { + let editor = session + .open_editor_session(Default::default(), &CancellationToken::new()) + .await + .expect("an editor session"); + for sql in [ + "CREATE TABLE orders (id INTEGER PRIMARY KEY, status TEXT NOT NULL, note TEXT)", + "INSERT INTO orders (status, note) VALUES ('open', NULL), ('open', ''), ('paid', 'a, \"b\"')", + ] { + let summary = editor + .run( + UserSql::new(sql), + RunOptions::new(CancellationToken::new()), + Box::new(Discard), + ) + .await + .expect("a run"); + assert!(summary.succeeded(), "{sql}: {:?}", summary.end); + } + editor.close().await; +} + +async fn harness() -> Harness { + let dir = tempfile::tempdir().expect("a temporary directory"); + let file = dir.path().join("shop.db"); + let ids = Ids { + readable: Uuid::new_v4(), + writable: Uuid::new_v4(), + blocked: Uuid::new_v4(), + closed: Uuid::new_v4(), + twin: Uuid::new_v4(), + other_twin: Uuid::new_v4(), + stale: Uuid::new_v4(), + }; + let writable = open_session(dir.path(), &file, ids.writable, true).await; + seed(&writable).await; + let readable = open_session(dir.path(), &file, ids.readable, false).await; + let blocked = open_session(dir.path(), &file, ids.blocked, false).await; + let stale = open_session(dir.path(), &file, ids.stale, false).await; + let _ = stale.begin_shutdown().await; + let app = Arc::new(App::default()); + app.sessions.lock().expect("sessions").extend([ + (ids.writable, writable), + (ids.readable, readable), + (ids.blocked, blocked), + (ids.stale, stale.clone()), + ]); + app.reopens.lock().expect("reopens").insert(ids.stale, stale); + let paths = StoragePaths::under(dir.path(), "tablepro-test", "app.tablepro.TablePro.Test"); + let store = ConnectionStore::new(&paths); + for connection in [ + saved(ids.readable, "Readable", &file, ExternalAccess::ReadOnly), + saved(ids.writable, "Writable", &file, ExternalAccess::ReadWrite), + saved(ids.blocked, "Blocked", &file, ExternalAccess::Blocked), + saved(ids.closed, "Closed", &file, ExternalAccess::ReadOnly), + saved(ids.twin, "Twin", &file, ExternalAccess::ReadOnly), + saved(ids.other_twin, "twin", &file, ExternalAccess::ReadOnly), + saved(ids.stale, "Stale", &file, ExternalAccess::ReadOnly), + ] { + store.upsert_blocking(connection).expect("a saved connection"); + } + let token = AccessToken::from_secret(SecretString::from(TOKEN.to_owned())).expect("a token"); + let context = ToolContext { + host: app.clone(), + connections: store, + }; + let tasks = Tasks::new(tokio::runtime::Handle::current()); + let server = McpServer::start(0, token, context, "0.0.0-test", &tasks) + .await + .expect("a listening server"); + Harness { + _dir: dir, + server, + app, + ids, + } +} + +#[derive(Clone)] +struct Machine { + port: u16, + token: Option, + running: bool, + enabled: bool, + terminal: Terminal, +} + +impl Machine { + fn of(harness: &Harness) -> Self { + Self { + port: harness.server.address().port(), + token: Some(TOKEN.to_owned()), + running: true, + enabled: true, + terminal: Terminal::default(), + } + } + + fn in_a_terminal(self) -> Self { + Self { + terminal: Terminal { + stdin: true, + stdout: true, + stderr: true, + }, + ..self + } + } +} + +impl Host for Machine { + fn app_version(&self) -> String { + "0.0.0-test".to_owned() + } + + fn app_running(&self) -> bool { + self.running + } + + fn server_enabled(&self) -> Result { + Ok(self.enabled) + } + + fn server_port(&self) -> Result { + Ok(self.port) + } + + // The port's owner is the host's to check, and the Linux host's own tests cover it. + fn verify_peer(&self, _peer: std::net::SocketAddr, _local: std::net::SocketAddr) -> Result<(), HostError> { + Ok(()) + } + + fn token(&self) -> Result, HostError> { + Ok(self.token.clone()) + } + + fn terminal(&self) -> Terminal { + self.terminal + } +} + +struct Ran { + exit: Exit, + out: String, + err: String, +} + +// The command builds a runtime of its own, which a thread already running one may not do. +async fn tablepro(machine: Machine, words: &[&str]) -> Ran { + let args: Vec = std::iter::once("tablepro") + .chain(words.iter().copied()) + .map(OsString::from) + .collect(); + tokio::task::spawn_blocking(move || { + let (mut out, mut err) = (Vec::new(), Vec::new()); + let exit = tablepro_cli::run(args, &machine, &mut out, &mut err); + Ran { + exit, + out: String::from_utf8(out).expect("UTF-8 output"), + err: String::from_utf8(err).expect("UTF-8 errors"), + } + }) + .await + .expect("the command ran") +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn connections_lists_what_clients_may_use() { + let harness = harness().await; + + let ran = tablepro(Machine::of(&harness), &["connections", "--format", "jsonl"]).await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + let readable = format!( + r#"{{"name":"Readable","id":"{}","type":"sqlite"}}"#, + harness.ids.readable + ); + assert!(ran.out.lines().any(|line| line == readable), "{}", ran.out); + assert!(!ran.out.contains("Blocked"), "{}", ran.out); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_query_into_a_pipe_prints_csv() { + let harness = harness().await; + + let ran = tablepro( + Machine::of(&harness), + &[ + "query", + "-c", + "Readable", + "SELECT id, status, note FROM orders ORDER BY id", + ], + ) + .await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert_eq!( + ran.out, + "id,status,note\n1,open,\n2,open,\"\"\n3,paid,\"a, \"\"b\"\"\"\n" + ); + assert!(ran.err.is_empty(), "{}", ran.err); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_query_on_a_terminal_prints_a_table_and_the_name_matches_in_any_case() { + let harness = harness().await; + + let ran = tablepro( + Machine::of(&harness).in_a_terminal(), + &["query", "-c", "readable", "SELECT id, status FROM orders WHERE id = 1"], + ) + .await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert_eq!(ran.out, "id | status\n---+-------\n1 | open\n"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_limit_cuts_the_rows_and_says_so() { + let harness = harness().await; + + let ran = tablepro( + Machine::of(&harness), + &[ + "query", + "-c", + "Readable", + "--limit", + "1", + "--format", + "json", + "SELECT id FROM orders ORDER BY id", + ], + ) + .await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert_eq!(ran.out, "[\n {\"id\":1}\n]\n"); + assert_eq!(ran.err, "tablepro: Showing the first row. Pass --limit to change it.\n"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_hidden_or_unknown_connection_is_a_usage_error_by_name_and_by_id() { + let harness = harness().await; + let blocked = harness.ids.blocked.to_string(); + + for connection in ["Blocked", blocked.as_str(), "Nowhere"] { + let ran = tablepro(Machine::of(&harness), &["query", "-c", connection, "SELECT 1"]).await; + assert_eq!(ran.exit, Exit::Usage, "{connection}: {}", ran.err); + assert!(ran.err.contains("No connection named"), "{}", ran.err); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn two_names_alike_ask_for_an_id() { + let harness = harness().await; + + let ran = tablepro(Machine::of(&harness), &["query", "-c", "TWIN", "SELECT 1"]).await; + + assert_eq!(ran.exit, Exit::Usage, "{}", ran.err); + assert!(ran.err.contains(&harness.ids.twin.to_string()), "{}", ran.err); + assert!(ran.err.contains(&harness.ids.other_twin.to_string()), "{}", ran.err); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_closed_connection_is_opened_only_from_a_terminal() { + let harness = harness().await; + + let script = tablepro(Machine::of(&harness), &["query", "-c", "Closed", "SELECT 1"]).await; + let person = tablepro( + Machine::of(&harness).in_a_terminal(), + &["query", "-c", "Closed", "SELECT 1"], + ) + .await; + + assert_eq!(script.exit, Exit::Failure, "{}", script.err); + assert!(script.err.contains("is not open in TablePro"), "{}", script.err); + // The test app never opens a connection, so the attempt fails as a refused password would. + assert_eq!(person.exit, Exit::Failure, "{}", person.err); + assert!( + person.err.starts_with("tablepro: Opening “Closed” in TablePro."), + "{}", + person.err + ); + assert!(person.err.contains("did not open"), "{}", person.err); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_tool_that_still_finds_the_connection_closed_after_connect_stops_after_one_retry() { + let harness = harness().await; + + let script = tablepro(Machine::of(&harness), &["tables", "-c", "Stale"]).await; + let opened_by_script = harness.app.opens.load(Ordering::SeqCst); + let person = tablepro(Machine::of(&harness).in_a_terminal(), &["tables", "-c", "Stale"]).await; + + assert_eq!(script.exit, Exit::Failure, "{}", script.err); + assert!(script.err.contains("call connect to open it"), "{}", script.err); + assert_eq!(opened_by_script, 0); + assert_eq!(person.exit, Exit::Failure, "{}", person.err); + assert!( + person.err.starts_with("tablepro: Opening “Stale” in TablePro."), + "{}", + person.err + ); + assert!(person.err.ends_with("call connect to open it\n"), "{}", person.err); + assert_eq!(harness.app.opens.load(Ordering::SeqCst), 1); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_statement_that_may_write_is_never_sent_twice() { + let harness = harness().await; + + let ran = tablepro( + Machine::of(&harness).in_a_terminal(), + &["query", "-c", "Stale", "SELECT 1"], + ) + .await; + + assert_eq!(ran.exit, Exit::Failure, "{}", ran.err); + assert!(ran.err.contains("call connect to open it"), "{}", ran.err); + assert_eq!(harness.app.opens.load(Ordering::SeqCst), 0); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn tool_failures_keep_their_exit_status() { + let harness = harness().await; + let cases: [(&[&str], Exit); 4] = [ + (&["query", "-c", "Readable", "SELECT 1; SELECT 2"], Exit::Usage), + (&["query", "-c", "Readable", "DELETE FROM orders"], Exit::Refused), + (&["query", "-c", "Writable", "DELETE FROM orders"], Exit::Refused), + (&["query", "-c", "Readable", "SELECT * FROM missing"], Exit::Failure), + ]; + + for (words, exit) in cases { + let ran = tablepro(Machine::of(&harness), words).await; + assert_eq!(ran.exit, exit, "{words:?}: {}", ran.err); + assert!(ran.err.starts_with("tablepro: "), "{}", ran.err); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn read_only_refuses_a_write_on_a_writable_connection() { + let harness = harness().await; + harness.app.approves.store(true, Ordering::SeqCst); + + let ran = tablepro( + Machine::of(&harness), + &["query", "-c", "Writable", "--read-only", "DELETE FROM orders"], + ) + .await; + + assert_eq!(ran.exit, Exit::Refused, "{}", ran.err); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_approved_write_says_how_many_rows_it_changed() { + let harness = harness().await; + harness.app.approves.store(true, Ordering::SeqCst); + + let ran = tablepro( + Machine::of(&harness), + &["query", "-c", "Writable", "UPDATE orders SET note = 'x' WHERE id = 1"], + ) + .await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert!(ran.out.is_empty(), "{}", ran.out); + assert_eq!(ran.err, "1 row affected.\n"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_changed_token_is_refused_and_a_closed_port_is_unreachable() { + let harness = harness().await; + let changed = Machine { + token: Some("tp_other".to_owned()), + ..Machine::of(&harness) + }; + let free = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); + let closed_port = free.local_addr().expect("an address").port(); + drop(free); + let silent = Machine { + port: closed_port, + ..Machine::of(&harness) + }; + + let refused = tablepro(changed, &["connections"]).await; + let unreachable = tablepro(silent, &["connections"]).await; + + assert_eq!(refused.exit, Exit::Refused, "{}", refused.err); + assert!(refused.err.contains("token changed"), "{}", refused.err); + assert_eq!(unreachable.exit, Exit::Failure, "{}", unreachable.err); + assert!(unreachable.err.contains("does not answer"), "{}", unreachable.err); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn tables_and_describe_read_the_schema() { + let harness = harness().await; + + let tables = tablepro(Machine::of(&harness), &["tables", "-c", "Readable"]).await; + let columns = tablepro(Machine::of(&harness), &["describe", "-c", "Readable", "orders"]).await; + let ddl = tablepro( + Machine::of(&harness), + &["describe", "-c", "Readable", "--ddl", "orders"], + ) + .await; + + assert_eq!(tables.exit, Exit::Success, "{}", tables.err); + assert!(tables.out.starts_with("name,schema,kind\n"), "{}", tables.out); + assert!(tables.out.contains("orders,"), "{}", tables.out); + assert_eq!(columns.exit, Exit::Success, "{}", columns.err); + assert!( + columns.out.starts_with("name,data_type,is_nullable,is_primary_key"), + "{}", + columns.out + ); + assert!(columns.out.contains("\nstatus,TEXT,false,false"), "{}", columns.out); + assert_eq!(ddl.exit, Exit::Success, "{}", ddl.err); + assert!(ddl.out.contains("CREATE TABLE"), "{}", ddl.out); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn export_writes_the_tables_data() { + let harness = harness().await; + + let ran = tablepro( + Machine::of(&harness), + &["export", "-c", "Readable", "--format", "csv", "orders"], + ) + .await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert!(ran.out.contains("paid"), "{}", ran.out); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn status_is_ready_when_a_command_would_run() { + let harness = harness().await; + + let ran = tablepro(Machine::of(&harness), &["status", "--format", "csv"]).await; + + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + assert_eq!( + ran.out.lines().nth(1), + Some( + format!( + "0.0.0-test,true,true,{},true,0.0.0-test,true", + harness.server.address().port() + ) + .as_str() + ) + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn exports_and_ddl_escape_terminal_controls_and_preserve_pipe_bytes() { + let harness = harness().await; + harness.app.approves.store(true, Ordering::SeqCst); + let text = "\u{1b}]52;c;eA==\u{7}\u{1b}[2J"; + let sql = format!("CREATE TABLE controls (value TEXT DEFAULT '{text}')"); + let created = tablepro(Machine::of(&harness), &["query", "-c", "Writable", &sql]).await; + assert_eq!(created.exit, Exit::Success, "{}", created.err); + let inserted = tablepro( + Machine::of(&harness), + &["query", "-c", "Writable", "INSERT INTO controls DEFAULT VALUES"], + ) + .await; + assert_eq!(inserted.exit, Exit::Success, "{}", inserted.err); + + for arguments in [ + vec!["export", "-c", "Readable", "--format", "csv", "controls"], + vec!["export", "-c", "Readable", "--format", "sql", "controls"], + vec!["describe", "-c", "Readable", "--ddl", "controls"], + ] { + let piped = tablepro(Machine::of(&harness), &arguments).await; + let terminal = tablepro(Machine::of(&harness).in_a_terminal(), &arguments).await; + assert_eq!(piped.exit, Exit::Success, "{}", piped.err); + assert_eq!(terminal.exit, Exit::Success, "{}", terminal.err); + assert!(piped.out.contains(text), "{:?}", piped.out); + assert_eq!( + terminal.out, + piped.out.replace('\u{1b}', "\\x1B").replace('\u{7}', "\\x07") + ); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn delimited_queries_escape_terminal_controls_and_preserve_pipe_bytes() { + let harness = harness().await; + for format in ["csv", "tsv"] { + let words = [ + "query", + "-c", + "Readable", + "--format", + format, + "SELECT char(27) || ']52;c;eA==' || char(7) || char(27) || '[2J' AS value", + ]; + let piped = tablepro(Machine::of(&harness), &words).await; + let terminal = tablepro(Machine::of(&harness).in_a_terminal(), &words).await; + assert_eq!(piped.exit, Exit::Success, "{}", piped.err); + assert_eq!(terminal.exit, Exit::Success, "{}", terminal.err); + assert_eq!(piped.out, "value\n\u{1b}]52;c;eA==\u{7}\u{1b}[2J\n"); + assert_eq!(terminal.out, "value\n\\x1B]52;c;eA==\\x07\\x1B[2J\n"); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn json_queries_round_trip_controls_on_terminals_and_in_pipes() { + let harness = harness().await; + let value = "\u{7f}\u{85}\u{1b}"; + for format in ["json", "jsonl"] { + let words = [ + "query", + "-c", + "Readable", + "--format", + format, + "SELECT char(127) || char(133) || char(27) AS value", + ]; + let piped = tablepro(Machine::of(&harness), &words).await; + let terminal = tablepro(Machine::of(&harness).in_a_terminal(), &words).await; + assert_eq!(piped.exit, Exit::Success, "{}", piped.err); + assert_eq!(terminal.exit, Exit::Success, "{}", terminal.err); + let decoded: serde_json::Value = serde_json::from_str(&terminal.out).expect("valid JSON"); + let row = if format == "json" { &decoded[0] } else { &decoded }; + assert_eq!(row["value"], value); + assert_eq!(terminal.out, piped.out); + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn json_exports_round_trip_controls_on_terminals_and_in_pipes() { + let harness = harness().await; + harness.app.approves.store(true, Ordering::SeqCst); + for sql in [ + "CREATE TABLE controls (value TEXT)", + "INSERT INTO controls VALUES (char(127) || char(133) || char(27))", + ] { + let ran = tablepro(Machine::of(&harness), &["query", "-c", "Writable", sql]).await; + assert_eq!(ran.exit, Exit::Success, "{}", ran.err); + } + let words = ["export", "-c", "Readable", "--format", "json", "controls"]; + let piped = tablepro(Machine::of(&harness), &words).await; + let terminal = tablepro(Machine::of(&harness).in_a_terminal(), &words).await; + assert_eq!(piped.exit, Exit::Success, "{}", piped.err); + assert_eq!(terminal.exit, Exit::Success, "{}", terminal.err); + let decoded: serde_json::Value = serde_json::from_str(&terminal.out).expect("valid JSON"); + assert_eq!(decoded[0]["value"], "\u{7f}\u{85}\u{1b}"); + assert_eq!(terminal.out, piped.out); +} diff --git a/core/crates/mcp/src/export_tools.rs b/core/crates/mcp/src/export_tools.rs new file mode 100644 index 0000000000..05a06671b4 --- /dev/null +++ b/core/crates/mcp/src/export_tools.rs @@ -0,0 +1,163 @@ +use std::sync::Arc; +use std::time::Duration; + +use serde::Serialize; +use tablepro_core::browse::{ExportRequest, build_export}; +use tablepro_core::column::ResultColumn; +use tablepro_core::export::{CsvOptions, render_csv, render_json, render_sql_insert}; +use tablepro_core::{ColumnInfo, ReadStatement, TableRef, Value}; +use tablepro_session::Session; +use tokio_util::sync::CancellationToken; + +use crate::browse_request::filter_set; +use crate::connection_tools::to_json; +use crate::query_tools::{DEFAULT_TIMEOUT_SECONDS, row_limit, stream, table_shape}; +use crate::tool_args::{ExportArgs, ExportFormat}; +use crate::tool_context::ToolContext; +use crate::tool_error::ToolError; + +// The answer is inline, so the tables a call names bound its size along with max_rows. +pub const MAX_EXPORT_TABLES: usize = 20; + +#[derive(Serialize)] +struct ExportDocument { + format: ExportFormat, + exports: Vec, + #[serde(skip_serializing_if = "Vec::is_empty")] + notes: Vec<&'static str>, +} + +#[derive(Serialize)] +struct TableExport { + table: String, + row_count: usize, + truncated: bool, + data: String, +} + +pub async fn export_data( + context: &ToolContext, + args: ExportArgs, + cancel: CancellationToken, +) -> Result { + let connection = context.connection(&args.connection_id)?; + let limit = row_limit(args.max_rows, "max_rows")?; + match args.tables.len() { + 0 => return Err(ToolError::InvalidArgument("tables names no table".to_owned())), + count if count > MAX_EXPORT_TABLES => { + return Err(ToolError::InvalidArgument(format!( + "tables names {count} tables; one call exports {MAX_EXPORT_TABLES} at most" + ))); + } + _ => {} + } + if args.tables.iter().any(String::is_empty) { + return Err(ToolError::InvalidArgument("tables holds an empty name".to_owned())); + } + let session = context + .held_session(&connection, args.database.as_deref()) + .await? + .session; + let mut exports = Vec::with_capacity(args.tables.len()); + let mut undecodable = false; + for name in args.tables { + let table = TableRef::new(args.schema.clone(), name); + let (export, lossy) = export_table(&session, table, args.format, limit, &cancel).await?; + exports.push(export); + undecodable |= lossy; + } + let notes = match undecodable { + true => vec!["Some values could not be decoded by TablePro and are written as null."], + false => Vec::new(), + }; + to_json(&ExportDocument { + format: args.format, + exports, + notes, + }) +} + +// The flag says a value the driver could not decode went out as null. +async fn export_table( + session: &Session, + table: TableRef, + format: ExportFormat, + limit: usize, + cancel: &CancellationToken, +) -> Result<(TableExport, bool), ToolError> { + let (columns, identity) = table_shape(session, &table).await?; + let catalog: Arc<[ColumnInfo]> = columns.into(); + let filter = filter_set(&[], None)?; + let read_table = table.clone(); + let read_catalog = catalog.clone(); + let mut collected = stream( + session, + limit, + Duration::from_secs(u64::from(DEFAULT_TIMEOUT_SECONDS)), + cancel, + Some(catalog.clone()), + move |lease| { + let request = ExportRequest { + table: &read_table, + columns: &read_catalog, + filter: &filter, + sort: &[], + identity: &identity, + }; + Ok(ReadStatement::Generated(build_export(&*lease.dialect(), &request)?)) + }, + ) + .await?; + // An empty table can end the read before it names its columns. + let (result_columns, rows, truncated) = match collected.sets.pop() { + Some(set) => (set.columns, set.rows, set.truncated), + None => ( + catalog.iter().map(ColumnInfo::result_column).collect(), + Vec::new(), + false, + ), + }; + let data = render(session, format, &table, &catalog, &result_columns, &rows)?; + let export = TableExport { + row_count: rows.len(), + truncated, + data, + table: table.name, + }; + Ok((export, collected.undecodable)) +} + +fn render( + session: &Session, + format: ExportFormat, + table: &TableRef, + catalog: &[ColumnInfo], + columns: &[ResultColumn], + rows: &[Vec], +) -> Result { + let encoded = match format { + ExportFormat::Csv => render_csv(columns, rows, &CsvOptions::default()), + ExportFormat::Json => render_json(columns, rows), + ExportFormat::Sql => { + let dialect = session + .current_dialect() + .ok_or_else(|| ToolError::NotConnected("the connection was closed in TablePro".to_owned()))?; + // The table export's own statements: a column the server computes is left out, and + // one it numbers keeps its value so other rows still point at it. + let generated = names(catalog, |column| column.is_generated); + let numbered = names(catalog, |column| column.is_auto_increment); + return Ok(render_sql_insert( + &*dialect, table, columns, &generated, &numbered, rows, + )); + } + }; + encoded.map_err(|error| ToolError::Internal(error.to_string())) +} + +fn names(catalog: &[ColumnInfo], keep: impl Fn(&ColumnInfo) -> bool) -> Vec { + catalog + .iter() + .filter(|column| keep(column)) + .map(|column| column.name.clone()) + .collect() +} diff --git a/core/crates/mcp/src/lib.rs b/core/crates/mcp/src/lib.rs index 6b873d64fd..5154d9f34f 100644 --- a/core/crates/mcp/src/lib.rs +++ b/core/crates/mcp/src/lib.rs @@ -8,6 +8,7 @@ mod access_token; mod browse_request; mod collected_results; mod connection_tools; +mod export_tools; mod mcp_host; mod mcp_server; mod query_tools; @@ -19,6 +20,7 @@ mod tool_error; mod tool_server; pub use access_token::{AccessToken, TokenError}; +pub use export_tools::MAX_EXPORT_TABLES; pub use mcp_host::{McpHost, OpenOutcome, WriteApproval}; pub use mcp_server::{MCP_PATH, McpServer, StartError}; pub use query_tools::{DEFAULT_ROWS, DEFAULT_TIMEOUT_SECONDS, MAX_QUERY_BYTES, MAX_ROWS, MAX_TIMEOUT_SECONDS}; diff --git a/core/crates/mcp/src/mcp_host.rs b/core/crates/mcp/src/mcp_host.rs index 21fa9e9977..369d6829e0 100644 --- a/core/crates/mcp/src/mcp_host.rs +++ b/core/crates/mcp/src/mcp_host.rs @@ -15,6 +15,11 @@ use uuid::Uuid; pub trait McpHost: Send + Sync + 'static { async fn session(&self, id: Uuid) -> Option>; + // Only a session the window already holds beside the connection: a sibling belongs to it (ADR 0030). + async fn session_in(&self, _id: Uuid, _database: &str) -> Option> { + None + } + async fn open_connections(&self) -> Vec; /// Open the saved connection in a window, as the picker does, and @@ -55,6 +60,8 @@ impl std::fmt::Debug for OpenOutcome { pub struct WriteApproval { pub connection_id: Uuid, pub connection_name: String, + // The question has to name a database that is not the connection's own. + pub database: Option, pub sql: String, /// Drops, empties or rewrites a whole table. pub destructive: bool, diff --git a/core/crates/mcp/src/query_tools.rs b/core/crates/mcp/src/query_tools.rs index bbdb656970..8250756f51 100644 --- a/core/crates/mcp/src/query_tools.rs +++ b/core/crates/mcp/src/query_tools.rs @@ -5,7 +5,7 @@ use serde::Serialize; use tablepro_core::browse::{DeclaredPrecisionSink, ExportRequest, build_count, build_export}; use tablepro_core::meta::{ObjectScope, RowIdentity}; use tablepro_core::sql_syntax::script::{LexicalSettings, ScriptPlan}; -use tablepro_core::sql_syntax::{SqlGrammar, changes_catalog, statement_access}; +use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, changes_catalog, statement_access}; use tablepro_core::{ CallOptions, ColumnInfo, EditorSessionOptions, ExternalGate, ExternalRefusal, ReadStatement, ResultSink, RowLimit, RunEnd, RunOptions, TableRef, TransactionState, UserSql, @@ -21,7 +21,7 @@ use crate::connection_tools::to_json; use crate::mcp_host::WriteApproval; use crate::schema_tools::no_such_table; use crate::tool_args::{BrowseArgs, CountArgs, QueryArgs}; -use crate::tool_context::ToolContext; +use crate::tool_context::{HeldSession, ToolContext}; use crate::tool_error::ToolError; pub const DEFAULT_ROWS: u32 = 100; @@ -47,13 +47,19 @@ pub async fn execute_query( args.query.len() ))); } - let session = context.session(&connection).await?; + let HeldSession { session, database } = context.held_session(&connection, args.database.as_deref()).await?; let dialect = session .current_dialect() .ok_or_else(|| ToolError::NotConnected(format!("“{}” has no connection right now", connection.name)))?; let grammar = dialect.grammar(); let text = single_batch(&args.query, grammar)?; let access = statement_access(&text, grammar); + // Before the gate, so a client that asked for a read never puts a write to the user. + if args.read_only && access != StatementAccess::Read { + return Err(ToolError::Denied( + "the call set read_only, and the statement can change the database".to_owned(), + )); + } let (ran, executed_at, elapsed) = match connection.external_access.gate(session.safe_mode(), access) { ExternalGate::Refuse(reason) => return Err(refusal(&connection, reason)), ExternalGate::Run => { @@ -65,6 +71,7 @@ pub async fn execute_query( let approval = WriteApproval { connection_id: connection.id, connection_name: connection.name.clone(), + database, sql: text.clone(), destructive, cancel: cancel.clone(), @@ -235,7 +242,7 @@ async fn run_write(session: &Session, text: &str, limit: usize, timeout: Duratio /// `limit` rows of each result set and stopping at the row past them. /// A table's rows pass through `precision`, so a decimal keeps the scale /// its column declares, as in an export. -async fn stream( +pub(crate) async fn stream( session: &Session, limit: usize, timeout: Duration, @@ -313,7 +320,10 @@ pub async fn browse_table( } /// The table's columns and how its rows are told apart, read together. -async fn table_shape(session: &Session, table: &TableRef) -> Result<(Vec, RowIdentity), ToolError> { +pub(crate) async fn table_shape( + session: &Session, + table: &TableRef, +) -> Result<(Vec, RowIdentity), ToolError> { let read = session .run(OperationKind::Metadata, |lease, call| { let table = table.clone(); @@ -362,7 +372,7 @@ pub async fn count_rows(context: &ToolContext, args: CountArgs) -> Result, name: &str) -> Result { +pub(crate) fn row_limit(asked: Option, name: &str) -> Result { match asked.unwrap_or(DEFAULT_ROWS) { rows @ 1..=MAX_ROWS => Ok(rows as usize), rows => Err(ToolError::InvalidArgument(format!( diff --git a/core/crates/mcp/src/schema_tools.rs b/core/crates/mcp/src/schema_tools.rs index 458a5d4f69..933436b0fc 100644 --- a/core/crates/mcp/src/schema_tools.rs +++ b/core/crates/mcp/src/schema_tools.rs @@ -1,12 +1,12 @@ use serde::Serialize; use tablepro_core::column::ColumnDefault; use tablepro_core::ddl::BuildDdlError; -use tablepro_core::meta::{IndexKey, ObjectKind, ObjectScope, RowCountEstimate}; -use tablepro_core::{ColumnInfo, ForeignKeyInfo, IndexInfo, TableRef}; +use tablepro_core::meta::{IndexKey, ObjectKind, ObjectScope, RowCountEstimate, SchemaInfo}; +use tablepro_core::{ColumnInfo, DriverError, ForeignKeyInfo, IndexInfo, TableRef}; use tablepro_session::{OperationError, OperationKind}; use crate::connection_tools::to_json; -use crate::tool_args::{ConnectionArgs, SchemaArgs, TableArgs}; +use crate::tool_args::{ConnectionArgs, DatabaseArgs, SchemaArgs, TableArgs}; use crate::tool_context::ToolContext; use crate::tool_error::ToolError; @@ -22,9 +22,12 @@ struct ListedSchema { is_system: bool, } -pub async fn list_schemas(context: &ToolContext, args: ConnectionArgs) -> Result { +pub async fn list_schemas(context: &ToolContext, args: DatabaseArgs) -> Result { let connection = context.connection(&args.connection_id)?; - let session = context.session(&connection).await?; + let session = context + .held_session(&connection, args.database.as_deref()) + .await? + .session; let schemas = session .schemas() .await @@ -41,6 +44,48 @@ pub async fn list_schemas(context: &ToolContext, args: ConnectionArgs) -> Result }) } +#[derive(Serialize)] +struct DatabaseList { + databases: Vec, +} + +#[derive(Serialize)] +struct ListedDatabase { + name: String, + is_current: bool, +} + +pub async fn list_databases(context: &ToolContext, args: ConnectionArgs) -> Result { + let connection = context.connection(&args.connection_id)?; + let session = context.session(&connection).await?; + let databases = session + .run(OperationKind::Metadata, |lease, call| async move { + Ok(lease.list_databases(call).await?) + }) + .await + .map_err(|error| match error { + OperationError::Driver(DriverError::Unsupported { .. }) => ToolError::Unsupported(format!( + "“{}” reaches one database and lists no others", + connection.name + )), + error => ToolError::from_operation(&error), + })?; + to_json(&database_list(&databases)) +} + +fn database_list(databases: &[SchemaInfo]) -> DatabaseList { + DatabaseList { + databases: databases + .iter() + .map(|database| ListedDatabase { + name: database.name.clone(), + // The driver marks the connection's own database as the default. + is_current: database.is_default, + }) + .collect(), + } +} + #[derive(Serialize)] struct TableList { tables: Vec, @@ -58,7 +103,10 @@ struct ListedTable { /// client just created is there; the read also refreshes the sidebar. pub async fn list_tables(context: &ToolContext, args: SchemaArgs) -> Result { let connection = context.connection(&args.connection_id)?; - let session = context.session(&connection).await?; + let session = context + .held_session(&connection, args.database.as_deref()) + .await? + .session; let scope = match args.schema { Some(schema) => ObjectScope::Schema(schema), None => ObjectScope::DefaultSchema, @@ -147,7 +195,10 @@ type TableParts = (Vec, Vec, Vec, RowCoun pub async fn describe_table(context: &ToolContext, args: TableArgs) -> Result { let connection = context.connection(&args.connection_id)?; - let session = context.session(&connection).await?; + let session = context + .held_session(&connection, args.database.as_deref()) + .await? + .session; let table = TableRef::new(args.schema, args.table); let read: Result = session .run(OperationKind::Metadata, |lease, call| { @@ -251,7 +302,10 @@ struct TableDdl { /// dialect. pub async fn get_table_ddl(context: &ToolContext, args: TableArgs) -> Result { let connection = context.connection(&args.connection_id)?; - let session = context.session(&connection).await?; + let session = context + .held_session(&connection, args.database.as_deref()) + .await? + .session; let table = TableRef::new(args.schema, args.table); let read = session .run(OperationKind::Metadata, |lease, call| { @@ -285,3 +339,29 @@ pub fn no_such_table(table: &TableRef) -> ToolError { None => format!("no table or view named {} in the default schema", table.name), }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_connections_own_database_is_the_current_one() { + let database = |name: &str, is_default: bool, is_system: bool| SchemaInfo { + name: name.to_owned(), + is_default, + is_system, + }; + + let listed = to_json(&database_list(&[ + database("analytics", false, false), + database("shop", true, false), + database("postgres", false, true), + ])) + .expect("JSON"); + + assert_eq!( + listed, + r#"{"databases":[{"name":"analytics","is_current":false},{"name":"shop","is_current":true},{"name":"postgres","is_current":false}]}"# + ); + } +} diff --git a/core/crates/mcp/src/tool_args.rs b/core/crates/mcp/src/tool_args.rs index 3fa950c09b..612f5ea736 100644 --- a/core/crates/mcp/src/tool_args.rs +++ b/core/crates/mcp/src/tool_args.rs @@ -7,7 +7,10 @@ )] use schemars::JsonSchema; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; + +const DATABASE: &str = "Another of the connection's databases, one TablePro already has open beside the connection \ +(Open Database… in its window). Omit it for the connection's own database."; #[derive(Debug, Deserialize, JsonSchema)] #[serde(deny_unknown_fields)] @@ -20,11 +23,22 @@ pub struct ConnectionArgs { pub connection_id: String, } +#[derive(Debug, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct DatabaseArgs { + #[schemars(description = "A connection id from list_connections.")] + pub connection_id: String, + #[schemars(description = DATABASE)] + pub database: Option, +} + #[derive(Debug, Deserialize, JsonSchema)] #[serde(deny_unknown_fields)] pub struct SchemaArgs { #[schemars(description = "A connection id from list_connections.")] pub connection_id: String, + #[schemars(description = DATABASE)] + pub database: Option, #[schemars(description = "The schema to list. Omit it for the connection's default schema.")] pub schema: Option, } @@ -36,6 +50,8 @@ pub struct TableArgs { pub connection_id: String, #[schemars(description = "The table or view name, unquoted.")] pub table: String, + #[schemars(description = DATABASE)] + pub database: Option, #[schemars(description = "The table's schema. Omit it for the connection's default schema.")] pub schema: Option, } @@ -86,12 +102,46 @@ pub struct QueryArgs { description = "One SQL statement, or one batch on SQL Server, in the connection's own dialect. 100 KB at most." )] pub query: String, + #[schemars(description = DATABASE)] + pub database: Option, + #[serde(default)] + #[schemars( + description = "Refuse the statement unless it only reads, whatever the connection's External Clients level. Defaults to false." + )] + pub read_only: bool, #[schemars(description = "Rows to keep per result set, 1 to 1000. Defaults to 100.")] pub max_rows: Option, #[schemars(description = "Seconds before the statement is stopped, 1 to 300. Defaults to 30.")] pub timeout_seconds: Option, } +#[derive(Debug, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ExportArgs { + #[schemars(description = "A connection id from list_connections.")] + pub connection_id: String, + #[schemars( + description = "csv: a header row, then one line per row. json: an array of objects. sql: one INSERT per row, in the connection's own dialect." + )] + pub format: ExportFormat, + #[schemars(description = "The tables or views to export, unquoted, 1 to 20.")] + pub tables: Vec, + #[schemars(description = DATABASE)] + pub database: Option, + #[schemars(description = "The tables' schema. Omit it for the connection's default schema.")] + pub schema: Option, + #[schemars(description = "Rows to export per table, 1 to 1000. Defaults to 100.")] + pub max_rows: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum ExportFormat { + Csv, + Json, + Sql, +} + #[derive(Debug, Deserialize, JsonSchema)] #[serde(deny_unknown_fields)] pub struct FilterArg { diff --git a/core/crates/mcp/src/tool_context.rs b/core/crates/mcp/src/tool_context.rs index 1753592b75..2e8c23c5b3 100644 --- a/core/crates/mcp/src/tool_context.rs +++ b/core/crates/mcp/src/tool_context.rs @@ -1,12 +1,19 @@ use std::sync::Arc; -use tablepro_session::Session; +use tablepro_core::dialect::DatabaseLayout; +use tablepro_session::{OperationKind, Session}; use tablepro_storage::{ConnectionList, ConnectionListState, ConnectionStore, SavedConnection}; use uuid::Uuid; use crate::mcp_host::McpHost; use crate::tool_error::ToolError; +pub(crate) struct HeldSession { + pub session: Arc, + // None for the connection's own database. + pub database: Option, +} + /// What every tool reads: the saved connections as the store holds them /// now, and the app. #[derive(Clone)] @@ -62,4 +69,102 @@ impl ToolContext { ))), } } + + // Never opens a database: one the user did not open in TablePro stays closed to clients. + pub(crate) async fn held_session( + &self, + connection: &SavedConnection, + database: Option<&str>, + ) -> Result { + let own = self.session(connection).await?; + let Some(database) = database else { + return Ok(HeldSession { + session: own, + database: None, + }); + }; + if database.is_empty() { + return Err(ToolError::InvalidArgument( + "database is empty; omit it for the connection's own database".to_owned(), + )); + } + if own.database() == Some(database) { + return Ok(HeldSession { + session: own, + database: None, + }); + } + if let Some(session) = self.host.session_in(connection.id, database).await + && !session.is_shut_down() + { + return Ok(HeldSession { + session, + database: Some(database.to_owned()), + }); + } + if own.database().is_none() && opened_in(&own, database).await { + return Ok(HeldSession { + session: own, + database: None, + }); + } + Err(not_held( + &connection.name, + database, + own.current_dialect().map(|dialect| dialect.database_layout()), + )) + } +} + +// A connection saved without a database opens the login's default, which only the server can name. +async fn opened_in(session: &Session, database: &str) -> bool { + session + .run(OperationKind::Metadata, |lease, call| async move { + Ok(lease.list_databases(call).await?) + }) + .await + .is_ok_and(|listed| listed.iter().any(|own| own.is_default && own.name == database)) +} + +fn not_held(connection: &str, database: &str, layout: Option) -> ToolError { + match layout { + Some(DatabaseLayout::Single) => ToolError::Unsupported(format!( + "“{connection}” reaches one database, so it has no “{database}”" + )), + Some(DatabaseLayout::AsSchemas) => ToolError::InvalidArgument(format!( + "on “{connection}” the database “{database}” is a schema of the connection: pass it as schema, or \ +qualify the names in the query" + )), + Some(DatabaseLayout::WithSchemas) | None => ToolError::NotConnected(format!( + "the database “{database}” of “{connection}” is not open in TablePro; the user can open it with \ +Open Database… on the connection in TablePro, then call again" + )), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_database_not_held_says_what_reaches_it_on_each_engine() { + let codes = [ + (Some(DatabaseLayout::WithSchemas), "not_connected"), + (None, "not_connected"), + (Some(DatabaseLayout::AsSchemas), "invalid_argument"), + (Some(DatabaseLayout::Single), "unsupported"), + ]; + + for (layout, code) in codes { + let refused = not_held("prod", "analytics", layout); + + assert_eq!(refused.code(), code, "{layout:?}"); + assert!(refused.message().contains("analytics"), "{}", refused.message()); + } + assert!( + not_held("prod", "analytics", Some(DatabaseLayout::WithSchemas)) + .message() + .contains("Open Database…") + ); + } } diff --git a/core/crates/mcp/src/tool_server.rs b/core/crates/mcp/src/tool_server.rs index 7910a3b529..ccb43177f5 100644 --- a/core/crates/mcp/src/tool_server.rs +++ b/core/crates/mcp/src/tool_server.rs @@ -10,17 +10,20 @@ use rmcp::{ErrorData, RoleServer, ServerHandler}; use schemars::JsonSchema; use serde::de::DeserializeOwned; -use crate::tool_args::{BrowseArgs, ConnectionArgs, CountArgs, NoArgs, QueryArgs, SchemaArgs, TableArgs}; +use crate::tool_args::{ + BrowseArgs, ConnectionArgs, CountArgs, DatabaseArgs, ExportArgs, NoArgs, QueryArgs, SchemaArgs, TableArgs, +}; use crate::tool_context::ToolContext; use crate::tool_error::ToolError; -use crate::{connection_tools, query_tools, schema_tools}; +use crate::{connection_tools, export_tools, query_tools, schema_tools}; const INSTRUCTIONS: &str = "TablePro reaches the database connections the user saved in it. \ Start with list_connections. A connection has to be open in a TablePro window: call connect for one whose \ is_open is false, and the user may have to answer TablePro first. Call describe_table before writing SQL \ against a table. execute_query runs one statement: a read runs read-only, and a write runs only where the \ connection's External Clients level allows it and after the user approves it in TablePro. A refusal is the \ -user's setting, not a passing failure, so do not retry it."; +user's setting, not a passing failure, so do not retry it. A tool that takes database reaches only a database \ +the user already opened in TablePro beside the connection."; /// The tools, as a stateless handler: every call reads the saved /// connections and the windows again. @@ -49,6 +52,7 @@ impl ToolServer { match request.name.as_ref() { "list_connections" => connection_tools::list_connections(tools, parse(arguments)?).await, "connect" => connection_tools::connect(tools, parse(arguments)?).await, + "list_databases" => schema_tools::list_databases(tools, parse(arguments)?).await, "list_schemas" => schema_tools::list_schemas(tools, parse(arguments)?).await, "list_tables" => schema_tools::list_tables(tools, parse(arguments)?).await, "describe_table" => schema_tools::describe_table(tools, parse(arguments)?).await, @@ -56,6 +60,7 @@ impl ToolServer { "browse_table" => query_tools::browse_table(tools, parse(arguments)?, cancel).await, "count_rows" => query_tools::count_rows(tools, parse(arguments)?).await, "execute_query" => query_tools::execute_query(tools, parse(arguments)?, cancel).await, + "export_data" => export_tools::export_data(tools, parse(arguments)?, cancel).await, other => Err(ToolError::NotFound(format!("there is no tool named {other}"))), } } @@ -128,9 +133,17 @@ first, such as for a password; then the status is connecting, and calling connec true, ), tool::( + "list_databases", + "List Databases", + "The server's databases the login can open, with the connection's own marked as current. An engine \ +that reaches one database, such as a file, answers unsupported.", + true, + ), + tool::( "list_schemas", "List Schemas", - "The schemas of an open connection, with the default one marked.", + "The schemas of an open connection, or of another of its databases open in TablePro, with the default \ +one marked.", true, ), tool::( @@ -170,9 +183,16 @@ for the engine.", "Run a Statement", "Run one SQL statement, or one batch on SQL Server. A read runs read-only. A write runs only on a \ connection whose External Clients level is Read & Write and whose Safe Mode is not Read-Only, and only after the \ -user approves it in TablePro.", +user approves it in TablePro. With read_only set, anything but a read is refused before it runs.", false, ), + tool::( + "export_data", + "Export Tables", + "Rows of one or more tables or views as CSV, JSON or INSERT statements, returned in the answer and \ +never written to a file. Each table keeps at most max_rows rows and says whether it had more.", + true, + ), ] } @@ -185,7 +205,7 @@ mod tests { let tools = catalog(); let names: Vec<&str> = tools.iter().map(|tool| tool.name.as_ref()).collect(); - assert_eq!(names.len(), 9); + assert_eq!(names.len(), 11); for tool in &tools { assert_eq!( tool.input_schema.get("type").and_then(serde_json::Value::as_str), @@ -204,10 +224,28 @@ mod tests { #[test] fn an_unknown_argument_is_refused() { let arguments: serde_json::Value = - serde_json::from_str(r#"{"connection_id": "x", "query": "SELECT 1", "database": "other"}"#).expect("JSON"); + serde_json::from_str(r#"{"connection_id": "x", "query": "SELECT 1", "schema": "other"}"#).expect("JSON"); let refused = parse::(arguments); assert!(matches!(refused, Err(ToolError::InvalidArgument(_)))); } + + #[test] + fn an_export_takes_only_its_three_formats() { + let arguments = |format: &str| -> serde_json::Value { + serde_json::from_str(&format!( + r#"{{"connection_id": "x", "format": "{format}", "tables": ["orders"]}}"# + )) + .expect("JSON") + }; + + for format in ["csv", "json", "sql"] { + assert!(parse::(arguments(format)).is_ok(), "{format}"); + } + assert!(matches!( + parse::(arguments("xlsx")), + Err(ToolError::InvalidArgument(_)) + )); + } } diff --git a/core/crates/mcp/tests/mcp_server.rs b/core/crates/mcp/tests/mcp_server.rs index 12d2357671..a87ec76e6f 100644 --- a/core/crates/mcp/tests/mcp_server.rs +++ b/core/crates/mcp/tests/mcp_server.rs @@ -54,6 +54,7 @@ fn tasks() -> Tasks { #[derive(Default)] struct TestHost { sessions: Mutex>>, + siblings: Mutex>>, approves: AtomicBool, asked: Mutex>, recorded: Mutex>, @@ -65,6 +66,14 @@ impl McpHost for TestHost { self.sessions.lock().expect("sessions").get(&id).cloned() } + async fn session_in(&self, id: Uuid, database: &str) -> Option> { + self.siblings + .lock() + .expect("siblings") + .get(&(id, database.to_owned())) + .cloned() + } + async fn open_connections(&self) -> Vec { self.sessions.lock().expect("sessions").keys().copied().collect() } @@ -184,14 +193,22 @@ impl ResultSink for Discard { } async fn seed(session: &Session) { + run_all( + session, + &[ + "CREATE TABLE orders (id INTEGER PRIMARY KEY, status TEXT NOT NULL, total NUMERIC)", + "INSERT INTO orders (status, total) VALUES ('open', 10), ('open', 20), ('paid', 30)", + ], + ) + .await; +} + +async fn run_all(session: &Session, statements: &[&str]) { let editor = session .open_editor_session(Default::default(), &CancellationToken::new()) .await .expect("an editor session"); - for sql in [ - "CREATE TABLE orders (id INTEGER PRIMARY KEY, status TEXT NOT NULL, total NUMERIC)", - "INSERT INTO orders (status, total) VALUES ('open', 10), ('open', 20), ('paid', 30)", - ] { + for &sql in statements { let summary = editor .run( UserSql::new(sql), @@ -350,6 +367,25 @@ fn parsed(text: &str) -> Json { serde_json::from_str(text).expect("a JSON answer") } +// Another database of Readable's and Writable's server, held by their windows as Open Database… leaves it. +async fn hold_archive(harness: &Harness) { + let dir = harness._dir.path(); + let file = dir.join("archive.db"); + let readable = open_session(dir, &file, harness.ids.readable, false, true).await; + run_all( + &readable, + &[ + "CREATE TABLE invoices (id INTEGER PRIMARY KEY, amount INTEGER NOT NULL)", + "INSERT INTO invoices (amount) VALUES (5), (7)", + ], + ) + .await; + let writable = open_session(dir, &file, harness.ids.writable, false, false).await; + let mut siblings = harness.host.siblings.lock().expect("siblings"); + siblings.insert((harness.ids.readable, "archive".to_owned()), readable); + siblings.insert((harness.ids.writable, "archive".to_owned()), writable); +} + async fn order_count(harness: &Harness) -> u64 { let (failed, text) = call( harness, @@ -426,8 +462,10 @@ async fn initialize_names_the_server_and_lists_every_tool() { .iter() .filter_map(|tool| tool["name"].as_str()) .collect(); - assert_eq!(names.len(), 9, "{names:?}"); - assert!(names.contains(&"execute_query")); + assert_eq!(names.len(), 11, "{names:?}"); + for tool in ["execute_query", "list_databases", "export_data"] { + assert!(names.contains(&tool), "{names:?}"); + } } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -835,3 +873,223 @@ async fn a_write_stops_when_its_connection_closes_in_tablepro() { assert!(text.starts_with("not_connected:"), "{text}"); assert_eq!(order_count(&harness).await, 3); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_engine_of_one_database_lists_none() { + let harness = harness().await; + + let (failed, text) = call( + &harness, + "list_databases", + json!({ "connection_id": harness.ids.readable.to_string() }), + ) + .await; + let (closed_failed, closed) = call( + &harness, + "list_databases", + json!({ "connection_id": harness.ids.closed.to_string() }), + ) + .await; + + assert!(failed); + assert!(text.starts_with("unsupported:"), "{text}"); + assert!(closed_failed); + assert!(closed.starts_with("not_connected:"), "{closed}"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_database_the_window_holds_answers_from_its_own_session() { + let harness = harness().await; + hold_archive(&harness).await; + let in_archive = |extra: &[(&str, &str)]| { + let mut arguments = json!({ "connection_id": harness.ids.readable.to_string(), "database": "archive" }); + for &(name, value) in extra { + arguments[name] = json!(value); + } + arguments + }; + + let (_, tables) = call(&harness, "list_tables", in_archive(&[])).await; + let (schemas_failed, schemas) = call(&harness, "list_schemas", in_archive(&[])).await; + let (_, described) = call(&harness, "describe_table", in_archive(&[("table", "invoices")])).await; + let (_, ddl) = call(&harness, "get_table_ddl", in_archive(&[("table", "invoices")])).await; + let (_, counted) = call( + &harness, + "execute_query", + in_archive(&[("query", "SELECT count(*) AS n FROM invoices")]), + ) + .await; + let (own_failed, _) = call( + &harness, + "describe_table", + json!({ "connection_id": harness.ids.readable.to_string(), "table": "invoices" }), + ) + .await; + + assert_eq!(parsed(&tables)["tables"][0]["name"], "invoices", "{tables}"); + assert!(!schemas_failed, "{schemas}"); + assert_eq!(parsed(&described)["columns"][1]["name"], "amount", "{described}"); + assert!( + parsed(&ddl)["ddl"].as_str().is_some_and(|ddl| ddl.contains("invoices")), + "{ddl}" + ); + assert_eq!( + parsed(&counted)["result_sets"][0]["rows"], + json!([{ "n": 2 }]), + "{counted}" + ); + assert!(own_failed, "the connection's own database has no invoices"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_database_the_window_does_not_hold_is_refused_and_never_opened() { + let harness = harness().await; + let id = harness.ids.readable.to_string(); + + let (failed, text) = call( + &harness, + "list_tables", + json!({ "connection_id": id, "database": "elsewhere" }), + ) + .await; + let (query_failed, query) = call( + &harness, + "execute_query", + json!({ "connection_id": id, "database": "elsewhere", "query": "SELECT 1" }), + ) + .await; + let (empty_failed, empty) = call(&harness, "list_schemas", json!({ "connection_id": id, "database": "" })).await; + + // SQLite reaches one database; a server of several answers not_connected (tool_context's tests). + assert!(failed); + assert!(text.starts_with("unsupported:"), "{text}"); + assert!(query_failed); + assert!(query.starts_with("unsupported:"), "{query}"); + assert!(empty_failed); + assert!(empty.starts_with("invalid_argument:"), "{empty}"); + assert!(harness.host.recorded.lock().expect("recorded").is_empty()); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn read_only_refuses_a_write_before_asking_even_at_read_and_write() { + let harness = harness().await; + harness.host.approves.store(true, Ordering::SeqCst); + let id = harness.ids.writable.to_string(); + + let (failed, text) = call( + &harness, + "execute_query", + json!({ "connection_id": id, "query": "DELETE FROM orders WHERE id = 1", "read_only": true }), + ) + .await; + let (read_failed, read) = call( + &harness, + "execute_query", + json!({ "connection_id": id, "query": "SELECT count(*) AS n FROM orders", "read_only": true }), + ) + .await; + + assert!(failed); + assert!(text.starts_with("denied:"), "{text}"); + assert!(harness.host.asked.lock().expect("asked").is_empty()); + assert!(!read_failed, "{read}"); + assert_eq!(parsed(&read)["result_sets"][0]["rows"], json!([{ "n": 3 }])); + assert_eq!(order_count(&harness).await, 3); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_write_in_another_database_names_it_to_the_user_and_runs_there() { + let harness = harness().await; + hold_archive(&harness).await; + harness.host.approves.store(true, Ordering::SeqCst); + + let (failed, text) = call( + &harness, + "execute_query", + json!({ + "connection_id": harness.ids.writable.to_string(), + "database": "archive", + "query": "DELETE FROM invoices WHERE id = 1", + }), + ) + .await; + + assert!(!failed, "{text}"); + assert_eq!(parsed(&text)["rows_affected"], 1); + let asked = harness.host.asked.lock().expect("asked").clone(); + assert_eq!(asked.len(), 1); + assert_eq!(asked[0].database.as_deref(), Some("archive")); + assert_eq!(order_count(&harness).await, 3); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_export_writes_each_format_under_the_row_cap() { + let harness = harness().await; + let export = |format: &str, max_rows: Option| { + let mut arguments = json!({ + "connection_id": harness.ids.readable.to_string(), + "format": format, + "tables": ["orders"], + }); + if let Some(rows) = max_rows { + arguments["max_rows"] = json!(rows); + } + call(&harness, "export_data", arguments) + }; + + let (csv_failed, csv) = export("csv", Some(2)).await; + let (_, json_text) = export("json", None).await; + let (_, sql) = export("sql", None).await; + + assert!(!csv_failed, "{csv}"); + let csv = parsed(&csv); + assert_eq!(csv["format"], "csv"); + let first = &csv["exports"][0]; + assert_eq!(first["table"], "orders"); + assert_eq!(first["row_count"], 2); + assert_eq!(first["truncated"], true); + let lines: Vec<&str> = first["data"].as_str().expect("CSV text").lines().collect(); + assert_eq!(lines.len(), 3, "{lines:?}"); + // The app's own CSV export neutralises formulas, which quotes every field. + assert_eq!(lines[0], r#""id","status","total""#); + assert!(lines[1].starts_with(r#""1","open","#), "{lines:?}"); + let json_export = &parsed(&json_text)["exports"][0]; + assert_eq!(json_export["truncated"], false); + let rows = parsed(json_export["data"].as_str().expect("JSON text")); + assert_eq!(rows.as_array().map(Vec::len), Some(3), "{rows}"); + assert_eq!(rows[2]["status"], "paid"); + let statements = parsed(&sql)["exports"][0]["data"] + .as_str() + .expect("SQL text") + .to_owned(); + assert_eq!(statements.matches("INSERT INTO").count(), 3, "{statements}"); + assert!(statements.contains("'paid'"), "{statements}"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_export_outside_its_limits_is_refused() { + let harness = harness().await; + let id = harness.ids.readable.to_string(); + let many: Vec = (0..=tablepro_mcp::MAX_EXPORT_TABLES).map(|n| format!("t{n}")).collect(); + + let refusals = [ + json!({ "connection_id": id, "format": "csv", "tables": ["orders"], "max_rows": 1001 }), + json!({ "connection_id": id, "format": "csv", "tables": [] }), + json!({ "connection_id": id, "format": "csv", "tables": many }), + json!({ "connection_id": id, "format": "xlsx", "tables": ["orders"] }), + json!({ "connection_id": id, "format": "csv", "tables": ["orders"], "path": "/tmp/out.csv" }), + ]; + for arguments in refusals { + let (failed, text) = call(&harness, "export_data", arguments.clone()).await; + + assert!(failed, "{arguments}"); + assert!(text.starts_with("invalid_argument:"), "{arguments}: {text}"); + } + let (missing_failed, missing) = call( + &harness, + "export_data", + json!({ "connection_id": id, "format": "json", "tables": ["orders", "nowhere"] }), + ) + .await; + assert!(missing_failed, "{missing}"); +} diff --git a/core/docs/decisions/0018-command-line.md b/core/docs/decisions/0018-command-line.md new file mode 100644 index 0000000000..4e342047f2 --- /dev/null +++ b/core/docs/decisions/0018-command-line.md @@ -0,0 +1,124 @@ +# 0018: One command line for the apps on the core + +- **Status**: Accepted +- **Date**: 2026-11-08 + +## Context + +The macOS app has a Swift command that uses its MCP server. Linux needs the same +shell commands, and Windows will need them too. Parsing, output and exit codes +should not depend on a GUI toolkit. Finding the running app and reading its token +still depend on the platform. + +## Decision + +Put the command line in `core/crates/cli` (`tablepro-cli`). It owns argument +classification and parsing, help, completion scripts, the MCP client, connection +lookup, request deadlines, output and exit codes. It has no GUI or platform crate +dependency. Database work stays in the app's MCP server, under its access checks, +write approval and row limits. + +Each app calls `classify` before GUI startup. It passes commands to `run`, with a +`Host` and output streams. Files, URLs and GUI options return to the app, with a +leading `open` removed. Unknown targets return an error. Linux startup, URL +handling and packaging are decided in [Linux 0045](../../../linux/docs/decisions/0045-command-line-and-connection-urls.md). + +The `Host` supplies the app version, whether the app is running, the MCP setting +and port, a token read, peer verification and terminal flags for all three standard +streams. Linux supplies these through its session bus, settings, keyring and +socket owner check. macOS keeps its Swift command and pairing flow. Windows must +supply its own host when it adopts the crate. + +### Command contract + +Keep the command surface and output formats aligned with the macOS command. +Authentication and opening connections remain platform choices. + +| Command | Work | +| --- | --- | +| `[URL\|FILE]...`, `open` | Hand targets to the app | +| `query -c ` | Run SQL from an argument, `--file` or standard input | +| `connections` | List connections visible to the client | +| `databases`, `schemas`, `tables` | List objects for a connection | +| `describe
[--ddl]` | Show structure or the CREATE statement | +| `export
... --format csv\|json\|sql` | Export through the server | +| `status` | Report readiness and try the MCP handshake when possible | +| `completion bash\|zsh\|fish`, `help`, `-h`, `--help`, `--version` | Work without contacting the app | + +Rows use CSV, TSV, JSON, JSONL or a text table. The default is a table when standard +output is a terminal, CSV otherwise. Writers preserve numeric text, distinguish +NULL from an empty string in CSV, and nest multiple JSON result sets in one +document. Notes go to standard error. Table cells escape control characters; +terminal diagnostics keep line breaks and tabs but escape other controls. + +The exit numbers match macOS. The crate maps failures as follows: + +| Status | Meaning | +| --- | --- | +| 0 | Success, including a broken output pipe | +| 1 | Database, transport, host or output failure; missing token; connection not open | +| 2 | Usage error, unknown or ambiguous connection, or `invalid_argument` | +| 3 | Server disabled, token refused, `denied` or `user_cancelled` | +| 4 | Request deadline or tool `timeout` | + +The five-minute wait for a connection to open returns 1 if it expires. Request +deadlines return 4. A query retries after `not_connected` only with `--read-only`, +since a write may already have run. + +### Parser and authentication + +Use clap's derive parser for help, usage errors and suggestions. Generate bash, +zsh and fish completions from that same definition with clap_complete. This keeps +parsing, help and completion together without maintaining each by hand. + +Linux reads the server's existing token and has no pairing step. Programs running +as that user can already read it from the keyring. Pairing would grant access the +command already has. Creating a token in the command would not update the running +server. A stopped app or disabled server is reported before reading the token, +including for `status`. + +Use rmcp's Streamable HTTP client at `127.0.0.1:/mcp`, with proxies and +redirects disabled. A reqwest connector layer calls `Host::verify_peer` on every +new TCP connection before HTTP can send the bearer token. Missing socket +addresses or a failed check close that connection. A separate probe would leave +a gap in which another process could take the port. Verified connections can be +reused. The later MCP handshake must also name the server `tablepro`. rmcp reads +at most 16 MiB of one answer; a larger one fails with a message to ask for fewer +rows. + +On Linux, verification checks the peer socket's owner against the current user. +It does not prove the process is TablePro. That user is the boundary because the +same user can read the token. + +Keep help and diagnostics written by the crate in English. The core has no +translation catalog, and shell output should not change with the desktop locale. +Host and command errors retain their `code()` values. + +## Consequences + +- Linux and Windows can share command behavior without sharing a GUI. macOS keeps + a separate Swift implementation, including `pair` and `unpair`. +- Windows needs an MCP server, argument dispatch and a `Host` for app state, + settings, Credential Manager, terminal detection and peer verification. The + existing Windows socket helper accepts only the current process. It cannot + verify a separate app process as written. PowerShell completion also remains + to be added. +- Messages currently name Linux settings such as Preferences > MCP. Another app + must account for its own settings names when adopting the crate. +- Commands remain subject to server limits. Query and export report truncation; + they do not fetch the rest of a result automatically. +- Connection lookup uses only the server's visible list, even for IDs. Optional + tool fields are omitted when unset, so older servers need not know them. + +## Alternatives considered + +- **A parser in each app on the core.** It repeats the command contract and its + tests. Platform differences fit in `Host` and the GUI handoff. +- **A hand-written Rust parser.** Help, suggestions and completions would need + separate code and could drift from the accepted arguments. +- **Direct database access or another IPC interface.** It duplicates work and + authorization already provided by MCP. +- **Verify the port once before connecting.** It checks a different socket from + the one that sends the token and misses later connections. +- **Generic output writers.** The command needs the macOS format, including CSV + empty-string quoting, table layout and one JSON document for multiple results. diff --git a/core/docs/decisions/README.md b/core/docs/decisions/README.md index cc8675ce3a..eec617ac43 100644 --- a/core/docs/decisions/README.md +++ b/core/docs/decisions/README.md @@ -25,3 +25,4 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [0015](0015-editing-a-query-result.md) | A query result edits when its statement reads one table and its driver traces the columns | Accepted | The parser accepts one row-preserving SELECT over one table; each driver sets `ResultColumn::origin` only for a direct column of a permanent base table, from the wire or a lookup on its own connection; the result maps to the catalog with its full primary key and saves through `plan_save`. | | [0016](0016-tablepro-accounts.md) | TablePro accounts live in the core, with features gated by the entitlement | Accepted | `tablepro-account` signs in through the browser with OAuth and a loopback redirect, registers the device, verifies the Ed25519 entitlement and gates features by its codes; each app passes its keyring, browser, machine ID and OAuth client in. | | [0017](0017-favourites.md) | Favourites keep folders and keywords on saved queries, and the tables and databases a user marks | Accepted | One migration in the history's database adds per-connection folders that nest, a keyword and an order on saved queries, and favourite tables and databases with the four macOS environments; stable error codes, no UUID ids, and a database an older build wrote is copied first. | +| [0018](0018-command-line.md) | One command line for the apps on the core | Accepted | `tablepro-cli` parses with clap, writes the macOS command's formats and exit status, and reaches the app's MCP server with the token the app's host reads; `classify` leaves files, links and GUI options to the app. | diff --git a/linux/build-aux/debian/copyright b/linux/build-aux/debian/copyright index ca9916a5d4..45e0a4ee97 100644 --- a/linux/build-aux/debian/copyright +++ b/linux/build-aux/debian/copyright @@ -4761,6 +4761,7 @@ License: Apache-2.0 Files: vendor/clap-4.6.7/* vendor/clap_builder-4.6.7/* + vendor/clap_complete-4.6.11/* vendor/clap_derive-4.6.7/* vendor/clap_lex-1.1.1/* Copyright: Individual contributors @@ -17337,7 +17338,7 @@ License: Apache-2.0 and ISC OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -Files: vendor/rmcp-3.6.0/* +Files: vendor/rmcp-3.9.0/* Copyright: 2024-2025 Model Context Protocol a Series of LF Projects, LLC. License: Apache-2.0 and Expat The MCP project is undergoing a licensing transition from the MIT License to the Apache License, Version 2.0 ("Apache-2.0"). All new code and specification contributions to the project are licensed under Apache-2.0. Documentation contributions (excluding specifications) are licensed under CC-BY-4.0. diff --git a/linux/build-aux/debian/rules b/linux/build-aux/debian/rules index aa3480bb38..df1fbf01b1 100755 --- a/linux/build-aux/debian/rules +++ b/linux/build-aux/debian/rules @@ -44,8 +44,13 @@ export DEB_CXXFLAGS_MAINT_APPEND = -g1 dh $@ --buildsystem=meson --sourcedirectory=linux --builddirectory=$(builddir) # Tests change Cargo's LTO profile; install the package binaries already built before them. +# Debian's zsh reads completions from vendor-completions, where its own packages put them, and +# has no site-functions under /usr/share. override_dh_auto_install: meson install -C $(builddir) --no-rebuild --destdir "$(CURDIR)/debian/tablepro" + mkdir -p debian/tablepro/usr/share/zsh/vendor-completions + mv debian/tablepro/usr/share/zsh/site-functions/_tablepro debian/tablepro/usr/share/zsh/vendor-completions/ + rmdir debian/tablepro/usr/share/zsh/site-functions # The upstream changelog is the Linux app's, not a file at the top. override_dh_installchangelogs: diff --git a/linux/build-aux/packaging/smoke-test-flatpak.sh b/linux/build-aux/packaging/smoke-test-flatpak.sh index 257fefe23a..b53ad260ed 100755 --- a/linux/build-aux/packaging/smoke-test-flatpak.sh +++ b/linux/build-aux/packaging/smoke-test-flatpak.sh @@ -75,11 +75,13 @@ in_app() { } check_app() { - help="$(dbus-run-session -- flatpak run "$1" --help)" + help="$(dbus-run-session -- flatpak run "$1" --help-all)" case "$help" in *--help-gapplication*) ;; - *) fail "$1 --help did not list the GApplication options: $help" ;; + *) fail "$1 --help-all did not list the GApplication options: $help" ;; esac + # The command line runs inside the sandbox, as Flatpak users reach it. + dbus-run-session -- flatpak run "$1" --version || fail "$1 --version failed" files="$(flatpak info --user --show-location "$1")/files" for lang in $languages; do [ -f "$files/share/locale/$lang/LC_MESSAGES/tablepro.mo" ] || fail "$1 has no $lang catalogue" diff --git a/linux/build-aux/packaging/smoke-test.sh b/linux/build-aux/packaging/smoke-test.sh index 3a839232b8..296be92085 100755 --- a/linux/build-aux/packaging/smoke-test.sh +++ b/linux/build-aux/packaging/smoke-test.sh @@ -19,6 +19,7 @@ fi case "$package" in *.deb) + zsh_completion=/usr/share/zsh/vendor-completions/_tablepro export DEBIAN_FRONTEND=noninteractive # The Ubuntu image skips every .mo dpkg unpacks; a desktop install keeps them. if [ -f /etc/dpkg/dpkg.cfg.d/excludes ]; then @@ -34,6 +35,7 @@ case "$package" in } ;; *.rpm) + zsh_completion=/usr/share/zsh/site-functions/_tablepro # The Fedora image installs en_US translations only; a desktop install takes all of them. rm -f /etc/rpm/macros.image-language-conf dnf install -y --setopt=install_weak_deps=False "$package" @@ -50,18 +52,28 @@ case "$package" in esac # Before anything else is installed, so the package's own dependencies are -# all it has. The binary opens its settings before it reads the command -# line, so this also proves the schema was installed and compiled. -help="$(tablepro --help)" +# all it has. The GUI opens its settings before GApplication reads the +# command line, so this also proves the schema was installed and compiled. +# --help is the command line's own, which runs before the GUI's setup. +help="$(tablepro --help-all)" case "$help" in *--help-gapplication*) ;; *) - echo "smoke-test.sh: tablepro --help did not list the GApplication options:" >&2 + echo "smoke-test.sh: tablepro --help-all did not list the GApplication options:" >&2 echo "$help" >&2 exit 1 ;; esac test -x /usr/libexec/tablepro-askpass +# The command line's help and version, with no display. +tablepro --help >/dev/null +tablepro --version +for completion in /usr/share/bash-completion/completions/tablepro /usr/share/fish/vendor_completions.d/tablepro.fish "$zsh_completion"; do + if ! grep -qF tablepro "$completion"; then + echo "smoke-test.sh: the package installed no completion at $completion" >&2 + exit 1 + fi +done install_tools desktop-file-validate /usr/share/applications/app.tablepro.TablePro.desktop @@ -71,6 +83,7 @@ glib-compile-schemas --strict --dry-run /usr/share/glib-2.0/schemas # glibc loads no catalogue under C or C.UTF-8, so LANGUAGE picks each # language under a real locale. The --new-window help text is the app's # own string, so finding its translation shows the app read the catalogue. +# GApplication lists it under --help-all. if [ -n "$linguas" ]; then languages="$(sed 's/#.*//' "$linguas")" else @@ -86,9 +99,9 @@ for lang in $languages; do echo "smoke-test.sh: the $lang catalogue does not translate \"Open a new window\"" >&2 exit 1 fi - translated="$(LC_ALL=en_US.UTF-8 LANGUAGE="$lang" tablepro --help)" + translated="$(LC_ALL=en_US.UTF-8 LANGUAGE="$lang" tablepro --help-all)" if ! printf '%s\n' "$translated" | grep -qF -- "$expected"; then - echo "smoke-test.sh: tablepro --help under $lang did not show \"$expected\":" >&2 + echo "smoke-test.sh: tablepro --help-all under $lang did not show \"$expected\":" >&2 echo "$translated" >&2 exit 1 fi diff --git a/linux/build-aux/rpm/tablepro.spec b/linux/build-aux/rpm/tablepro.spec index a4dccc8a64..a09fa168b2 100644 --- a/linux/build-aux/rpm/tablepro.spec +++ b/linux/build-aux/rpm/tablepro.spec @@ -216,6 +216,9 @@ export CARGO_BUILD_JOBS=%{_smp_build_ncpus} %doc linux/CHANGELOG.md %{_bindir}/tablepro %{_libexecdir}/tablepro-askpass +%{_datadir}/bash-completion/completions/tablepro +%{_datadir}/fish/vendor_completions.d/tablepro.fish +%{_datadir}/zsh/site-functions/_tablepro %{_datadir}/applications/app.tablepro.TablePro.desktop %{_datadir}/dbus-1/services/app.tablepro.TablePro.service %{_datadir}/glib-2.0/schemas/app.tablepro.TablePro.gschema.xml diff --git a/linux/build-aux/tests/test_debian_install.py b/linux/build-aux/tests/test_debian_install.py index 812cc4f531..964ca4926d 100644 --- a/linux/build-aux/tests/test_debian_install.py +++ b/linux/build-aux/tests/test_debian_install.py @@ -25,10 +25,12 @@ def test_install_preserves_built_binaries_without_running_builder(self): "command: ['sh', files('build.sh'), '@OUTPUT0@', '@OUTPUT1@'], " "install: true, install_dir: ['bin', 'libexec'])\n" "install_data('data.txt', install_dir: 'share/tablepro')\n" + "install_data('_tablepro', install_dir: 'share/zsh/site-functions')\n" ) builder = source / "build.sh" builder.write_text('printf app > "$1"\nprintf askpass > "$2"\n') (source / "data.txt").write_text("package data") + (source / "_tablepro").write_text("#compdef tablepro") triple = subprocess.check_output(["dpkg-architecture", "-qDEB_HOST_GNU_TYPE"], text=True).strip() build = root / f"obj-{triple}" subprocess.run(["meson", "setup", str(build), str(source), "--prefix=/usr"], check=True) @@ -41,6 +43,10 @@ def test_install_preserves_built_binaries_without_running_builder(self): self.assertEqual((staged / "bin" / "tablepro").read_text(), "app") self.assertEqual((staged / "libexec" / "tablepro-askpass").read_text(), "askpass") self.assertEqual((staged / "share" / "tablepro" / "data.txt").read_text(), "package data") + self.assertEqual( + (staged / "share" / "zsh" / "vendor-completions" / "_tablepro").read_text(), "#compdef tablepro" + ) + self.assertFalse((staged / "share" / "zsh" / "site-functions").exists()) if __name__ == "__main__": diff --git a/linux/changelog.d/4145.added.1.md b/linux/changelog.d/4145.added.1.md new file mode 100644 index 0000000000..4bf2656a8c --- /dev/null +++ b/linux/changelog.d/4145.added.1.md @@ -0,0 +1 @@ +Database links such as postgresql:// open in TablePro once you confirm them. diff --git a/linux/changelog.d/4145.added.md b/linux/changelog.d/4145.added.md new file mode 100644 index 0000000000..c2acbc97b6 --- /dev/null +++ b/linux/changelog.d/4145.added.md @@ -0,0 +1 @@ +A tablepro command runs queries, lists and describes tables, exports them and prints shell completions. diff --git a/linux/changelog.d/4145.fixed.1.md b/linux/changelog.d/4145.fixed.1.md new file mode 100644 index 0000000000..e8d7c0ed97 --- /dev/null +++ b/linux/changelog.d/4145.fixed.1.md @@ -0,0 +1 @@ +tablepro --new-window with files dropped the files when TablePro was already running. diff --git a/linux/changelog.d/4145.fixed.md b/linux/changelog.d/4145.fixed.md new file mode 100644 index 0000000000..a63a84f576 --- /dev/null +++ b/linux/changelog.d/4145.fixed.md @@ -0,0 +1 @@ +Opening a database link showed its password on a page saying the file was not on this computer. diff --git a/linux/changelog.d/4145.security.md b/linux/changelog.d/4145.security.md new file mode 100644 index 0000000000..850f8ca55a --- /dev/null +++ b/linux/changelog.d/4145.security.md @@ -0,0 +1 @@ +tablepro export printed control codes stored in the database to the terminal, where they could run. diff --git a/linux/crates/app/Cargo.toml b/linux/crates/app/Cargo.toml index 6600fedccc..62182ae25b 100644 --- a/linux/crates/app/Cargo.toml +++ b/linux/crates/app/Cargo.toml @@ -60,6 +60,7 @@ kerberos = ["tablepro-driver-mssql/kerberos"] [dependencies] tablepro-account.workspace = true tablepro-ai.workspace = true +tablepro-cli.workspace = true tablepro-core.workspace = true tablepro-driver-cassandra.workspace = true tablepro-driver-clickhouse.workspace = true diff --git a/linux/crates/app/meson.build b/linux/crates/app/meson.build index 357c99e956..4f5ecedf66 100644 --- a/linux/crates/app/meson.build +++ b/linux/crates/app/meson.build @@ -21,7 +21,7 @@ cargo_built = cargo_target_dir / cargo_profile_dir app_env = cargo_env app_env.set('TABLEPRO_METAINFO', metainfo_file.full_path()) -custom_target( +tablepro_binaries = custom_target( 'cargo-build', build_always_stale: true, build_by_default: true, diff --git a/linux/crates/app/src/application/imp.rs b/linux/crates/app/src/application/imp.rs index fd609deb55..f7df6b44a0 100644 --- a/linux/crates/app/src/application/imp.rs +++ b/linux/crates/app/src/application/imp.rs @@ -10,11 +10,14 @@ use libadwaita as adw; use libadwaita::subclass::prelude::*; use super::app_activity::Activity; +use super::open_files::Placement; use super::session_signals::SessionSignals; use super::{AppServices, WindowRegistry, install_app_actions, install_application_accelerators}; use crate::workspace::CloseDecision; pub(super) const NEW_WINDOW_OPTION: &str = "new-window"; +// GOption's G_OPTION_REMAINING: the files and links after the options. +pub(super) const FILES_OPTION: &str = ""; #[derive(Default)] pub struct TableProApplication { @@ -31,9 +34,9 @@ pub struct TableProApplication { /// The restore waiting on the record, which a quit approved /// meanwhile waits for too. pub(super) restoring: RefCell>>, - /// Files handed over while the record is read, opened after the - /// windows it restores. - pub(super) pending_files: RefCell>, + /// Files and links handed over while the record is read, opened + /// after the windows it restores. + pub(super) pending_files: RefCell, Placement)>>, /// What to say at launch, found before any window opened. pub(super) launch_notices: RefCell>, /// Answers the MCP server's questions about windows and sessions. @@ -79,6 +82,15 @@ impl ObjectImpl for TableProApplication { &crate::i18n::gettext("Open a new window"), None, ); + // GOption collects the files, so handle-local-options can send them with --new-window's hint. + self.obj().add_main_option( + FILES_OPTION, + glib::Char::from(b'\0'), + glib::OptionFlags::NONE, + glib::OptionArg::FilenameArray, + "", + Some(&crate::i18n::gettext("[FILE or URL…]")), + ); } } @@ -116,31 +128,31 @@ impl ApplicationImpl for TableProApplication { app.open_launch_windows(); } - /// The files from Open With in the file manager, or from the command - /// line. GApplication opens them instead of activating, so no picker - /// window comes with them. - fn open(&self, files: &[gio::File], _hint: &str) { + // GApplication opens them instead of activating, so no picker window comes with them. + fn open(&self, files: &[gio::File], hint: &str) { + let placement = Placement::from_hint(hint); match self.restore.get() { - RestoreState::Done => self.obj().open_files(files), + RestoreState::Done => self.obj().open_files(files, placement), // The record is read before a file's window adds to it, or // that write would replace the sessions still to restore. RestoreState::NotAttempted => { - self.pending_files.borrow_mut().extend_from_slice(files); + self.pending_files.borrow_mut().push((files.to_vec(), placement)); self.obj().restore_session(); } - RestoreState::Loading => self.pending_files.borrow_mut().extend_from_slice(files), + RestoreState::Loading => self.pending_files.borrow_mut().push((files.to_vec(), placement)), RestoreState::Quitting { .. } => { - self.pending_files.borrow_mut().extend_from_slice(files); + self.pending_files.borrow_mut().push((files.to_vec(), placement)); self.restore.set(RestoreState::Quitting { relaunched: true }); } } } - /// `--new-window` on a second launch opens the window in the running - /// instance and exits. On the first launch it falls through to - /// activate, which opens the one window either way. + // Opened here as GApplication would open them: a second launch then exits, and a first one runs + // while their windows hold it. On a first launch, --new-window alone falls through to activate. fn handle_local_options(&self, options: &glib::VariantDict) -> ControlFlow { - if !options.contains(NEW_WINDOW_OPTION) { + let new_window = options.contains(NEW_WINDOW_OPTION); + let files = launch_files(options); + if !new_window && files.is_empty() { return ControlFlow::Continue(()); } let app = self.obj(); @@ -148,6 +160,14 @@ impl ApplicationImpl for TableProApplication { tracing::error!(%error, "could not register with the session bus"); return ControlFlow::Break(glib::ExitCode::FAILURE); } + if !files.is_empty() { + let placement = match new_window { + true => Placement::NewWindow, + false => Placement::Reuse, + }; + app.open(&files, placement.hint()); + return ControlFlow::Break(glib::ExitCode::SUCCESS); + } if app.is_remote() { app.activate_action("new-window", None); return ControlFlow::Break(glib::ExitCode::SUCCESS); @@ -184,3 +204,17 @@ impl GtkApplicationImpl for TableProApplication { } impl AdwApplicationImpl for TableProApplication {} + +fn launch_files(options: &glib::VariantDict) -> Vec { + match options.lookup::>(FILES_OPTION) { + Ok(arguments) => arguments + .unwrap_or_default() + .iter() + .map(gio::File::for_commandline_arg) + .collect(), + Err(error) => { + tracing::warn!(%error, "the command line's files came in an unexpected form"); + Vec::new() + } + } +} diff --git a/linux/crates/app/src/application/mcp_bridge.rs b/linux/crates/app/src/application/mcp_bridge.rs index f4fd0e552f..b11c789126 100644 --- a/linux/crates/app/src/application/mcp_bridge.rs +++ b/linux/crates/app/src/application/mcp_bridge.rs @@ -15,6 +15,7 @@ use tokio::sync::oneshot; use uuid::Uuid; use super::{AppServices, TableProApplication, WindowLaunch}; +use crate::i18n::gettext_f; use crate::runtime::{InputSender, input_queue, pump_inputs}; use crate::services::history_service::HistoryService; use crate::ui::write_confirmation::{WriteConfirmation, confirm_until}; @@ -32,6 +33,11 @@ pub(crate) enum McpRequest { id: Uuid, reply: oneshot::Sender>>, }, + SessionIn { + id: Uuid, + database: String, + reply: oneshot::Sender>>, + }, OpenConnections { reply: oneshot::Sender>, }, @@ -65,6 +71,13 @@ impl McpHost for GtkMcpHost { self.ask(|reply| McpRequest::Session { id, reply }).await.flatten() } + async fn session_in(&self, id: Uuid, database: &str) -> Option> { + let database = database.to_owned(); + self.ask(|reply| McpRequest::SessionIn { id, database, reply }) + .await + .flatten() + } + async fn open_connections(&self) -> Vec { self.ask(|reply| McpRequest::OpenConnections { reply }) .await @@ -131,6 +144,10 @@ fn answer(app: &TableProApplication, request: McpRequest) { McpRequest::Session { id, reply } => { let _ = reply.send(services.sessions.session(&SessionKey::Saved(id))); } + // Only a database the window opened beside the connection; a client never opens one. + McpRequest::SessionIn { id, database, reply } => { + let _ = reply.send(services.sessions.session(&SessionKey::SavedIn { id, database })); + } McpRequest::OpenConnections { reply } => { let open = services .sessions @@ -234,14 +251,30 @@ fn approve( approval: WriteApproval, mut reply: oneshot::Sender, ) { - let key = SessionKey::Saved(approval.connection_id); + let (key, connection) = match &approval.database { + Some(database) => ( + SessionKey::SavedIn { + id: approval.connection_id, + database: database.clone(), + }, + // Translators: a connection and another of its server's databases, open beside it. + gettext_f( + "{connection} · {database}", + &[("connection", &approval.connection_name), ("database", database)], + ), + ), + None => ( + SessionKey::Saved(approval.connection_id), + approval.connection_name.clone(), + ), + }; let owner = services.sessions.window_for(&key); let window = owner.and_then(|id| app.window_by_id(id)); let (Some(owner), Some(window)) = (owner, window) else { let _ = reply.send(false); return; }; - let confirmation = WriteConfirmation::external(&approval.connection_name, &approval.sql, approval.destructive); + let confirmation = WriteConfirmation::external(&connection, &approval.sql, approval.destructive); // The question comes over the connection it is about, which may be // behind another session of the window. app.present_session(owner, &key); diff --git a/linux/crates/app/src/application/mod.rs b/linux/crates/app/src/application/mod.rs index 0aa248ac88..629804da7b 100644 --- a/linux/crates/app/src/application/mod.rs +++ b/linux/crates/app/src/application/mod.rs @@ -18,6 +18,7 @@ mod mcp_bridge; #[cfg(test)] mod metainfo_release; mod open_files; +mod open_links; mod quit_sequence; mod runtime_versions; mod session_signals; @@ -49,6 +50,7 @@ pub use accelerators::{ pub use app_actions::install_app_actions; pub use app_services::{AppServices, AppServicesParts}; pub use app_window_host::AppWindowHost; +pub use open_files::Placement; pub use quit_sequence::QuitSequence; pub use shortcut_listing::ShortcutListing; pub use shortcut_owner::ShortcutOwner; @@ -184,8 +186,8 @@ impl TableProApplication { /// filed under its window as soon as the window has an id, so the /// record never groups them under a window that is gone. fn finish_restore(&self, windows: Vec>) { - let files = self.imp().pending_files.take(); - if windows.is_empty() && files.is_empty() { + let pending = self.imp().pending_files.take(); + if windows.is_empty() && pending.is_empty() { self.open_window(WindowLaunch::Picker); } let open_sessions = self.services().map(|services| services.open_sessions().clone()); @@ -197,7 +199,9 @@ impl TableProApplication { open_sessions.assign(&keys, window.id()); } } - self.open_files(&files); + for (files, placement) in pending { + self.open_files(&files, placement); + } } /// Ask every window to close, in turn. While one is asking, another diff --git a/linux/crates/app/src/application/open_files.rs b/linux/crates/app/src/application/open_files.rs index 850b3faa4f..a9c3a1e4f8 100644 --- a/linux/crates/app/src/application/open_files.rs +++ b/linux/crates/app/src/application/open_files.rs @@ -3,24 +3,49 @@ use gtk4::prelude::*; use tablepro_session::SessionKey; use super::{TableProApplication, WindowLaunch}; +use crate::services::connection_link; use crate::services::file_resolution::{FileTarget, ResolvedFile, resolve_file_blocking}; use crate::ui::{ScriptInput, SessionWindow, SessionWindowInput}; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Placement { + Reuse, + // A database a window already has still comes forward there: one session has one window. + NewWindow, +} + +impl Placement { + // The open hint a second launch with --new-window sends. + pub const NEW_WINDOW_HINT: &'static str = "new-window"; + + pub fn from_hint(hint: &str) -> Self { + match hint { + Self::NEW_WINDOW_HINT => Self::NewWindow, + _ => Self::Reuse, + } + } + + pub fn hint(self) -> &'static str { + match self { + Self::Reuse => "", + Self::NewWindow => Self::NEW_WINDOW_HINT, + } + } +} + impl TableProApplication { - /// Open each file the file manager or the command line hands over. - /// A database opens in the window that has it open already, in the - /// active window while it only shows the picker, or in a new window. - /// A SQL script opens in the last window used, or in a new one. - pub fn open_files(&self, files: &[gio::File]) { + pub fn open_files(&self, files: &[gio::File], placement: Placement) { for file in files { - self.open_file(file.clone()); + // A link names no file, and file resolution would show its text, password and all. + match connection_link::link_text(file) { + Some(link) => self.open_link(&link, placement), + None => self.open_file(file.clone(), placement), + } } } - /// Finding the window that has the file open means canonicalizing - /// its path and matching it against the saved connections, which - /// read the disk, so the file is resolved on the spawner first. - fn open_file(&self, file: gio::File) { + // Matching the file to a window reads the disk, so it resolves on the blocking pool first. + fn open_file(&self, file: gio::File, placement: Placement) { let Some(services) = self.services() else { tracing::warn!("no file opened: the application has no services"); return; @@ -42,25 +67,21 @@ impl TableProApplication { Some(ResolvedFile { uri, target: Ok(FileTarget::Script), - }) => app.place_script(gio::File::for_uri(&uri)), - resolved => app.place_file(file, resolved.as_ref().and_then(session_key)), + }) => app.place_script(gio::File::for_uri(&uri), placement), + resolved => app.place_file(file, resolved.as_ref().and_then(session_key), placement), } } drop(hold); }); } - fn place_file(&self, file: gio::File, key: Option) { + fn place_file(&self, file: gio::File, key: Option, placement: Placement) { // The owner may show another of its sessions in front. let owner = key.and_then(|key| Some((self.services()?.sessions.window_for(&key)?, key))); if owner.is_some_and(|(owner, key)| self.present_session(owner, &key)) { return; } - let idle = self - .active_window() - .and_downcast::() - .filter(SessionWindow::is_idle); - match idle { + match self.idle_window(placement) { Some(window) => { window.send(SessionWindowInput::OpenFile(file)); window.present(); @@ -71,18 +92,30 @@ impl TableProApplication { } } + pub(super) fn idle_window(&self, placement: Placement) -> Option { + if placement == Placement::NewWindow { + return None; + } + self.active_window() + .and_downcast::() + .filter(SessionWindow::is_idle) + } + // A second tab on one file would have two editors saving over each // other, so a tab already bound to it comes forward instead. - fn place_script(&self, file: gio::File) { + fn place_script(&self, file: gio::File, placement: Placement) { + if SessionWindow::focus_tab_bound_to(self, &file) { + return; + } + if placement == Placement::NewWindow { + return self.route_script(Vec::new(), file); + } // GTK lists the windows most recently focused first. let windows: Vec = self .windows() .into_iter() .filter_map(|window| window.downcast::().ok()) .collect(); - if SessionWindow::focus_tab_bound_to(self, &file) { - return; - } // While any window settles, a tab bound to the file may still appear, so a picker asks rather // than opening a second one beside it. if windows.iter().any(SessionWindow::settling) { diff --git a/linux/crates/app/src/application/open_links.rs b/linux/crates/app/src/application/open_links.rs new file mode 100644 index 0000000000..68fde5e89a --- /dev/null +++ b/linux/crates/app/src/application/open_links.rs @@ -0,0 +1,128 @@ +use gtk4::glib; +use gtk4::prelude::*; +use libadwaita as adw; +use libadwaita::prelude::*; +use tablepro_core::connection_url::{self, ConnectionUrl}; +use tablepro_session::SessionKey; +use tablepro_storage::SavedConnection; + +use super::open_files::Placement; +use super::{TableProApplication, WindowLaunch}; +use crate::services::connection_link::{LinkTarget, saved_for_link_blocking}; +use crate::ui::{SessionWindow, SessionWindowInput}; + +pub(super) const CANCEL: &str = "cancel"; +pub(super) const OPEN: &str = "open"; + +impl TableProApplication { + // A link never connects on its own, and its text, which may hold a password, is never logged or shown. + pub(super) fn open_link(&self, text: &str, placement: Placement) { + let Some(services) = self.services() else { + tracing::warn!("no link opened: the application has no services"); + return; + }; + let url = match connection_url::parse(text) { + Ok(url) => url, + Err(error) => { + tracing::warn!(code = error.code(), "could not read a connection link"); + if let Some(window) = self.link_window(placement) { + let message = crate::ui::error_text::connection_url_message(&error); + window.send(SessionWindowInput::ShowToast(message)); + window.present(); + } + return; + } + }; + let Some(driver) = services.drivers.get(url.driver_id) else { + // The connect dialog says no driver here opens it. + return self.connect_from_link(url, placement); + }; + let target = LinkTarget::new(&url, driver.capabilities(), &glib::user_name().to_string_lossy()); + let connections = services.storage.connections().clone(); + let finding = services + .tasks + .spawn_blocking_task(move || saved_for_link_blocking(&target, &connections)); + // An instance D-Bus started for this link would otherwise exit + // before its window opens. + let hold = self.hold(); + let app = self.downgrade(); + glib::spawn_future_local(async move { + let saved = finding.await.ok().flatten(); + if let Some(app) = app.upgrade() { + match saved { + Some(saved) => app.confirm_saved_link(saved, placement).await, + None => app.connect_from_link(url, placement), + } + } + drop(hold); + }); + } + + fn connect_from_link(&self, url: ConnectionUrl, placement: Placement) { + let Some(window) = self.link_window(placement) else { + tracing::warn!("no window to open a connection link in"); + return; + }; + window.send(SessionWindowInput::ConnectFromLink(Box::new(url))); + window.present(); + } + + async fn confirm_saved_link(&self, saved: SavedConnection, placement: Placement) { + // A launch for the link has no window yet, and the question needs one under it. + let (parent, opened_for_link) = match self.active_window().and_downcast::() { + Some(window) => (window, false), + None => match self.open_window(WindowLaunch::Picker).and_downcast::() { + Some(window) => (window, true), + None => return, + }, + }; + let dialog = saved_link_dialog(&saved.name); + let answer = crate::workspace::respond(&dialog, parent.upcast_ref()).await; + if answer != OPEN { + return; + } + let key = SessionKey::Saved(saved.id); + if let Some(owner) = self.services().and_then(|services| services.sessions.window_for(&key)) + && self.present_session(owner, &key) + { + return; + } + let window = match opened_for_link && parent.is_idle() { + true => Some(parent), + false => self.idle_window(placement), + }; + match window { + Some(window) => { + window.send(SessionWindowInput::OpenSaved(Box::new(saved))); + window.present(); + } + None => { + self.open_window(WindowLaunch::Connect(Box::new(saved))); + } + } + } + + fn link_window(&self, placement: Placement) -> Option { + self.idle_window(placement) + .or_else(|| self.open_window(WindowLaunch::Picker).and_downcast::()) + } +} + +// Cancel is the default and the close response: a link can come from any web page. +pub(super) fn saved_link_dialog(name: &str) -> adw::AlertDialog { + let dialog = adw::AlertDialog::new( + Some(&crate::i18n::gettext_f("Open “{connection}”?", &[("connection", name)])), + Some(&crate::i18n::gettext( + "A link asks TablePro to open this saved connection. Open it only if you trust where the link came from.", + )), + ); + // A connection name is never markup. + dialog.set_heading_use_markup(false); + dialog.set_body_use_markup(false); + dialog.add_response(CANCEL, &crate::i18n::gettext("_Cancel")); + dialog.add_response(OPEN, &crate::i18n::gettext("_Open")); + dialog.set_response_appearance(OPEN, adw::ResponseAppearance::Suggested); + dialog.set_default_response(Some(CANCEL)); + dialog.set_close_response(CANCEL); + dialog +} diff --git a/linux/crates/app/src/application/tests.rs b/linux/crates/app/src/application/tests.rs index 0b9c56b182..aef0fdecd0 100644 --- a/linux/crates/app/src/application/tests.rs +++ b/linux/crates/app/src/application/tests.rs @@ -37,6 +37,10 @@ impl Drop for Backing { } fn fake_services() -> (AppServices, Backing) { + fake_services_with(Vec::new()) +} + +fn fake_services_with(more: Vec) -> (AppServices, Backing) { adw::init().expect("libadwaita initialises under the test backend"); crate::register_test_resources(); if let Some(settings) = gtk4::Settings::default() { @@ -58,6 +62,9 @@ fn fake_services() -> (AppServices, Backing) { &FakeDriver::FILE_CAPABILITIES, script.clone(), ))); + for driver in more { + drivers.register(Arc::new(driver)); + } let services = AppServices::new(AppServicesParts { drivers, connector: Arc::new(FakeConnector::new([]).always(FakeEstablish::Connect(script))), @@ -103,6 +110,14 @@ impl TestApp { test } + fn started_with_drivers(name: &str, more: Vec) -> Self { + let (services, backing) = fake_services_with(more); + let app = TableProApplication::new(services); + app.set_application_id(Some(&format!("{}.Test.{name}", crate::config::APP_ID))); + app.register(gio::Cancellable::NONE).expect("register the application"); + Self { app, _backing: backing } + } + fn window(&self) -> adw::ApplicationWindow { self.app.open_window(WindowLaunch::Picker).expect("a window") } @@ -276,6 +291,89 @@ fn handle_local_options_without_new_window_does_not_register() { assert!(!test.app.is_registered()); } +// GOption collects the files after the options, so handle-local-options can send them on with its hint. +#[gtk4::test] +fn the_files_after_the_options_reach_handle_local_options() { + let test = TestApp::new("ParseFiles"); + let seen = Rc::new(std::cell::RefCell::new(None)); + let sink = seen.clone(); + test.app.connect_handle_local_options(move |_, options| { + let files = options.lookup::>(imp::FILES_OPTION); + sink.replace(Some((options.contains("new-window"), files.ok().flatten()))); + ControlFlow::Break(glib::ExitCode::SUCCESS) + }); + + let code = test.app.run_with_args(&[ + "tablepro", + "--new-window", + "/tmp/shop.db", + "postgresql://ada@db.example.com/shop", + ]); + + assert_eq!(code, glib::ExitCode::SUCCESS); + let (new_window, files) = seen.take().expect("handle-local-options ran"); + assert!(new_window); + assert_eq!( + files, + Some(vec![ + "/tmp/shop.db".into(), + "postgresql://ada@db.example.com/shop".into() + ]) + ); +} + +// A second `--new-window FILE` once opened an empty window and dropped the files. +#[gtk4::test] +fn new_window_with_files_opens_them_and_leaves_the_launch() { + let test = TestApp::new("NewWindowFiles"); + let key = test.database("shop.db"); + let path = test._backing.dir.path().join("shop.db"); + let options = glib::VariantDict::new(None); + options.insert_value("new-window", &true.to_variant()); + options.insert_value(imp::FILES_OPTION, &vec![path.into_os_string()].to_variant()); + + let flow = test.app.imp().handle_local_options(&options); + + assert!(matches!(flow, ControlFlow::Break(glib::ExitCode::SUCCESS)), "{flow:?}"); + test.wait_for_sessions(std::slice::from_ref(&key)); +} + +#[gtk4::test] +fn a_file_with_the_new_window_hint_opens_beside_an_idle_picker() { + let test = TestApp::started("NewWindowHint"); + test.app.activate(); + wait_until(WAIT, || test.app.windows().len() == 1).expect("the picker"); + let picker = test.app.active_window().expect("the picker"); + let key = test.database("shop.db"); + let SessionKey::File(uri) = &key else { + unreachable!("a database file opens as a file key"); + }; + + test.app.open(&[gio::File::for_uri(uri)], Placement::NEW_WINDOW_HINT); + + test.wait_for_sessions(std::slice::from_ref(&key)); + assert_eq!(test.app.windows().len(), 2, "the file did not get a window of its own"); + assert_ne!(test.window_for(&key).upcast::(), picker); +} + +#[gtk4::test] +fn a_file_without_a_hint_opens_in_the_idle_picker() { + let test = TestApp::started("ReuseHint"); + test.app.activate(); + wait_until(WAIT, || test.app.windows().len() == 1).expect("the picker"); + let picker = test.app.active_window().expect("the picker"); + let key = test.database("shop.db"); + let SessionKey::File(uri) = &key else { + unreachable!("a database file opens as a file key"); + }; + + test.app.open(&[gio::File::for_uri(uri)], ""); + + test.wait_for_sessions(std::slice::from_ref(&key)); + assert_eq!(test.app.windows().len(), 1); + assert_eq!(test.window_for(&key).upcast::(), picker); +} + #[gtk4::test] fn startup_installs_the_app_actions_and_their_accels() { let test = TestApp::started("Actions"); @@ -1139,6 +1237,7 @@ mod mcp { WriteApproval { connection_id: id, connection_name: "Shop".to_owned(), + database: None, sql: "DELETE FROM orders WHERE id = 1".to_owned(), destructive: false, cancel, @@ -1359,3 +1458,218 @@ mod mcp { assert_eq!(listed["connections"][0]["is_open"], false); } } + +mod links { + use tablepro_core::{AiPolicy, AuthMode, ExternalAccess, SafeModeLevel, TlsConfig}; + use tablepro_storage::SavedConnection; + use uuid::Uuid; + + use super::*; + use crate::application::open_links::{CANCEL, OPEN}; + use crate::ui::SessionWindow; + + // Fakes under SQLite's and PostgreSQL's ids, so sqlite:// and postgresql:// links find a driver. + fn started(name: &str) -> TestApp { + let script = ConnectionScript::default(); + let test = TestApp::started_with_drivers( + name, + vec![ + FakeDriver::connecting(&FakeDriver::FILE_CAPABILITIES, script.clone()).with_id("sqlite"), + FakeDriver::connecting(&FakeDriver::NETWORK_CAPABILITIES, script).with_id("postgres"), + ], + ); + test.app.activate(); + wait_until(WAIT, || test.app.windows().len() == 1).expect("the picker"); + test + } + + fn picker(test: &TestApp) -> SessionWindow { + test.app + .active_window() + .and_downcast::() + .expect("the picker") + } + + fn saved_sqlite(test: &TestApp, name: &str) -> (SavedConnection, String) { + let path = test._backing.dir.path().join(name); + std::fs::write(&path, b"").expect("create the database"); + let saved = SavedConnection { + id: Uuid::new_v4(), + name: "Shop".to_owned(), + driver_id: "sqlite".to_owned(), + host: String::new(), + port: 0, + database: path.display().to_string(), + username: String::new(), + tls: TlsConfig::disabled(), + safe_mode: SafeModeLevel::Off, + external_access: ExternalAccess::default(), + ai_policy: AiPolicy::default(), + query_timeout_secs: None, + auth_mode: AuthMode::Password, + ssh: None, + last_opened_at: None, + color: None, + group_id: None, + order: 0, + tags: Vec::new(), + icon: None, + options: Default::default(), + }; + test.services() + .storage + .connections() + .upsert_blocking(saved.clone()) + .expect("the saved connection"); + (saved, format!("sqlite://{}", path.display())) + } + + fn link(text: &str) -> gio::File { + crate::services::connection_link::register_link_schemes().expect("register the connection links"); + gio::File::for_uri(text) + } + + #[gtk4::test] + fn a_link_to_a_saved_connection_asks_and_open_connects_it() { + let test = started("LinkSavedOpen"); + let (saved, text) = saved_sqlite(&test, "shop.db"); + let window = picker(&test); + + test.app.open(&[link(&text)], ""); + + wait_until(WAIT, || crate::workspace::visible_alert(&window).is_some()).expect("the question"); + let alert = crate::workspace::visible_alert(&window).expect("the question"); + assert!( + alert.heading().is_some_and(|heading| heading.contains("Shop")), + "{:?}", + alert.heading() + ); + assert_eq!(alert.default_response().as_deref(), Some(CANCEL)); + assert_eq!(alert.close_response().as_str(), CANCEL); + assert!(crate::workspace::answer(&window, OPEN)); + let key = SessionKey::Saved(saved.id); + let services = test.services(); + wait_until(WAIT, || services.sessions.window_for(&key).is_some()).expect("the saved connection to open"); + assert_eq!(test.app.windows().len(), 1, "the link opened another window"); + } + + #[gtk4::test] + fn cancel_on_a_link_opens_nothing() { + let test = started("LinkSavedCancel"); + let (saved, text) = saved_sqlite(&test, "shop.db"); + let window = picker(&test); + + test.app.open(&[link(&text)], ""); + + wait_until(WAIT, || crate::workspace::visible_alert(&window).is_some()).expect("the question"); + assert!(crate::workspace::answer(&window, CANCEL)); + let opened = wait_until(Duration::from_millis(500), || { + test.services() + .sessions + .window_for(&SessionKey::Saved(saved.id)) + .is_some() + }); + assert!(opened.is_err(), "Cancel opened the connection"); + assert_eq!(window.page_name(), "picker"); + } + + #[gtk4::test] + fn a_link_no_saved_connection_matches_fills_the_connect_dialog() { + let test = started("LinkUnsaved"); + let window = picker(&test); + + test.app.open( + &[link("postgresql://ada:s3cret@db.example.com:6543/shop?sslmode=require")], + "", + ); + + wait_until(WAIT, || window.connect_dialog().is_some()).expect("the connect dialog"); + let form = window.connect_dialog().expect("the connect dialog").form(); + assert_eq!(form.driver_id, "postgres"); + assert_eq!((form.host.as_str(), form.port), ("db.example.com", 6543)); + assert_eq!((form.database.as_str(), form.username.as_str()), ("shop", "ada")); + assert_eq!(secrecy::ExposeSecret::expose_secret(&form.password), "s3cret"); + assert!( + crate::workspace::visible_alert(&window).is_none(), + "a link to a new connection asked first" + ); + assert_eq!( + test.services().sessions.live_count(), + 0, + "the link connected on its own" + ); + } + + #[gtk4::test] + fn a_queued_link_reserves_its_picker_until_the_dialog_closes() { + let test = started("LinkReserved"); + let window = picker(&test); + let url = + tablepro_core::connection_url::parse("postgresql://ada@db.example.com/shop").expect("a connection URL"); + assert!(window.is_idle()); + + window.send(crate::ui::SessionWindowInput::ConnectFromLink(Box::new(url))); + + assert!(!window.is_idle(), "the queued link left its picker available"); + wait_until(WAIT, || window.connect_dialog().is_some()).expect("the connect dialog"); + assert!(!window.is_idle()); + window.connect_dialog().expect("the connect dialog").force_close(); + wait_until(WAIT, || window.is_idle()).expect("the picker to become available again"); + } + + #[gtk4::test] + fn two_unsaved_links_open_both_connect_dialogs() { + let test = started("LinkUnsavedPair"); + + test.app.open( + &[ + link("postgresql://ada:first-secret@first.example.com:6543/shop"), + link("postgresql://grace:second-secret@second.example.com:7654/stock"), + ], + "", + ); + + let forms = || { + test.app + .windows() + .into_iter() + .filter_map(|window| window.downcast::().ok()) + .filter_map(|window| window.connect_dialog()) + .map(|dialog| dialog.form()) + .collect::>() + }; + wait_until(WAIT, || forms().len() == 2).expect("a connect dialog for each link"); + let mut forms = forms(); + forms.sort_by(|first, second| first.host.cmp(&second.host)); + for (form, (host, port, database, username, password)) in forms.iter().zip([ + ("first.example.com", 6543, "shop", "ada", "first-secret"), + ("second.example.com", 7654, "stock", "grace", "second-secret"), + ]) { + assert_eq!(form.driver_id, "postgres"); + assert_eq!((form.host.as_str(), form.port), (host, port)); + assert_eq!((form.database.as_str(), form.username.as_str()), (database, username)); + assert_eq!(secrecy::ExposeSecret::expose_secret(&form.password), password); + } + assert_eq!(test.app.windows().len(), 2); + assert_eq!( + test.services().sessions.live_count(), + 0, + "the links connected on their own" + ); + } + + #[gtk4::test] + fn a_link_that_does_not_parse_says_why_and_opens_nothing() { + let test = started("LinkUnreadable"); + let window = picker(&test); + + test.app + .open(&[link("postgresql://ada:s3cret@db.example.com:99999/shop")], ""); + + crate::test_support::drain_main_context(); + let _ = wait_until(Duration::from_millis(300), || window.connect_dialog().is_some()); + assert!(window.connect_dialog().is_none()); + assert_eq!(test.app.windows().len(), 1); + assert_eq!(window.page_name(), "picker"); + } +} diff --git a/linux/crates/app/src/command_line/linux_host.rs b/linux/crates/app/src/command_line/linux_host.rs new file mode 100644 index 0000000000..fb5bda3463 --- /dev/null +++ b/linux/crates/app/src/command_line/linux_host.rs @@ -0,0 +1,118 @@ +use std::net::SocketAddr; + +use gio::prelude::*; +use secrecy::ExposeSecret; +use tablepro_cli::{Host, HostError, Terminal}; +use tablepro_core::credentials::{SecretItem, SecretItems}; +use tablepro_linux_platform::{AppSettings, SecretStore}; + +use crate::config; + +// NameHasOwner is answered by the bus itself, so this only bounds a bus that hangs. +const BUS_TIMEOUT_MS: i32 = 5_000; + +// Inside Flatpak this runs in the app's own sandbox, with its keyfile settings and portal-keyed keyring. +#[derive(Clone)] +pub struct LinuxHost; + +impl Host for LinuxHost { + fn app_version(&self) -> String { + config::VERSION.to_owned() + } + + // Asked of the bus: a call to TablePro's own name would D-Bus-activate it. + fn app_running(&self) -> bool { + let Ok(bus) = gio::bus_get_sync(gio::BusType::Session, gio::Cancellable::NONE) else { + return false; + }; + bus.call_sync( + Some("org.freedesktop.DBus"), + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "NameHasOwner", + Some(&(config::APP_ID,).to_variant()), + None, + gio::DBusCallFlags::NO_AUTO_START, + BUS_TIMEOUT_MS, + gio::Cancellable::NONE, + ) + .ok() + .and_then(|reply| reply.get::<(bool,)>()) + .is_some_and(|(owned,)| owned) + } + + fn server_enabled(&self) -> Result { + Ok(settings()?.mcp_server_enabled()) + } + + fn server_port(&self) -> Result { + Ok(settings()?.mcp_server_port()) + } + + // This user's is enough: the same user can already read the token from the keyring. + fn verify_peer(&self, peer: SocketAddr, local: SocketAddr) -> Result<(), HostError> { + let port = peer.port(); + let ours = tablepro_net::loopback_peer_is_ours(peer, local).map_err(|error| HostError::Unverified { + port, + reason: error.to_string(), + })?; + match ours { + true => Ok(()), + false => Err(HostError::ForeignServer { port }), + } + } + + // Only read: a token made here would be one the running server never accepts. + fn token(&self) -> Result, HostError> { + // A thread of its own, so the read never runs inside a runtime the caller may be blocking on. + std::thread::scope(|scope| scope.spawn(read_token).join()).unwrap_or_else(|_| { + Err(HostError::Keyring { + reason: "the keyring read stopped unexpectedly".to_owned(), + }) + }) + } + + fn terminal(&self) -> Terminal { + Terminal::detect() + } +} + +fn settings() -> Result { + AppSettings::open(config::APP_ID).map_err(|error| HostError::Settings { + reason: error.to_string(), + }) +} + +fn read_token() -> Result, HostError> { + let keyring = |reason: String| HostError::Keyring { reason }; + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|error| keyring(error.to_string()))?; + let store = SecretStore::new(config::secret_schema()); + let token = runtime + .block_on(store.load_item(SecretItem::McpToken)) + .map_err(|error| keyring(error.to_string()))?; + Ok(token.map(|token| token.expose_secret().to_owned())) +} + +#[cfg(test)] +mod tests { + use std::net::{Ipv4Addr, TcpListener, TcpStream}; + + use super::*; + + #[test] + fn a_listener_this_process_owns_is_verified() { + let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).expect("a free port"); + let stream = TcpStream::connect(listener.local_addr().expect("an address")).expect("a connection"); + + assert_eq!( + LinuxHost.verify_peer( + stream.peer_addr().expect("the peer"), + stream.local_addr().expect("the local address") + ), + Ok(()) + ); + } +} diff --git a/linux/crates/app/src/command_line/mod.rs b/linux/crates/app/src/command_line/mod.rs new file mode 100644 index 0000000000..be1061d40d --- /dev/null +++ b/linux/crates/app/src/command_line/mod.rs @@ -0,0 +1,19 @@ +mod linux_host; + +use std::ffi::OsString; +use std::io::Write; +use std::process::ExitCode; + +pub use linux_host::LinuxHost; + +pub fn run(args: Vec) -> ExitCode { + let host = LinuxHost; + let (stdout, stderr) = (std::io::stdout(), std::io::stderr()); + tablepro_cli::run(args, &host, &mut stdout.lock(), &mut stderr.lock()).into() +} + +pub fn refuse(message: &str) -> ExitCode { + // Nothing is left to report a failed write to. + let _ = writeln!(std::io::stderr().lock(), "{message}"); + tablepro_cli::Exit::Usage.into() +} diff --git a/linux/crates/app/src/lib.rs b/linux/crates/app/src/lib.rs index 8f5ea649a1..b76158bab6 100644 --- a/linux/crates/app/src/lib.rs +++ b/linux/crates/app/src/lib.rs @@ -1,3 +1,4 @@ +use std::ffi::OsString; use std::rc::Rc; use std::sync::Arc; @@ -7,6 +8,7 @@ use tablepro_core::DriverRegistry; use tablepro_linux_platform::AppSettings; pub mod application; +pub mod command_line; pub mod config; pub mod crash_report; pub mod i18n; @@ -17,6 +19,8 @@ mod services; pub mod storage; // Public for the save tests that run a tracker against a real server. pub use services::{change_tracker, tab_tracker_handle, tracker_edit}; +// Public for the desktop entry's test, which claims each of them. +pub use services::connection_link::LINK_SCHEMES; #[cfg(test)] mod test_support; mod ui; @@ -32,13 +36,15 @@ pub enum StartupError { Runtime(#[source] std::io::Error), #[error("could not register the embedded resources: {0}")] Resources(#[source] glib::Error), + #[error("could not register connection links: {0}")] + ConnectionLinks(#[source] glib::BoolError), #[error("could not open the settings schema: {0}")] Settings(#[from] tablepro_linux_platform::SettingsError), } /// Start the application and return its exit code once the last window /// and the last background close are gone. -pub fn run() -> Result { +pub fn run(args: Vec) -> Result { // SAFETY: nothing above spawns a thread; the tokio runtime and GTK start later. let translations = unsafe { i18n::init() }; logging::init(config::profile())?; @@ -55,8 +61,9 @@ pub fn run() -> Result { let drivers = build_registry(); tracing::info!(drivers = drivers.len(), "starting tablepro"); + services::connection_link::register_link_schemes().map_err(StartupError::ConnectionLinks)?; let application = TableProApplication::new(AppServices::production(drivers, settings, paths, runtime.tasks())); - let code = gio::prelude::ApplicationExtManual::run(&application); + let code = gio::prelude::ApplicationExtManual::run_with_args_os(&application, &args); drop(application); // The main loop has returned, so nothing is producing work any diff --git a/linux/crates/app/src/main.rs b/linux/crates/app/src/main.rs index 9e7afed97e..4affe416d1 100644 --- a/linux/crates/app/src/main.rs +++ b/linux/crates/app/src/main.rs @@ -1,17 +1,31 @@ +use std::ffi::OsString; +use std::process::ExitCode; + use tablepro_app::StartupError; +use tablepro_cli::Dispatch; + +fn main() -> ExitCode { + let args: Vec = std::env::args_os().collect(); + // Before any of the GUI's setup, so a command runs with no display and no installed schema. + match tablepro_cli::classify(&args) { + Dispatch::Command => tablepro_app::command_line::run(args), + Dispatch::Invalid(message) => tablepro_app::command_line::refuse(&message), + Dispatch::Gui(args) => gui(args), + } +} -fn main() -> glib::ExitCode { - match tablepro_app::run() { - Ok(code) => code, +fn gui(args: Vec) -> ExitCode { + match tablepro_app::run(args) { + Ok(code) => code.into(), Err(StartupError::Logging(error)) => { // Nothing is journalling yet, so this is the one place that // has to reach the session log by another route. glib::g_critical!("tablepro", "{error}"); - glib::ExitCode::FAILURE + ExitCode::FAILURE } Err(error) => { tracing::error!(%error, "TablePro could not start"); - glib::ExitCode::FAILURE + ExitCode::FAILURE } } } diff --git a/linux/crates/app/src/services/connection_link.rs b/linux/crates/app/src/services/connection_link.rs new file mode 100644 index 0000000000..871307c515 --- /dev/null +++ b/linux/crates/app/src/services/connection_link.rs @@ -0,0 +1,350 @@ +use gio::prelude::*; +use tablepro_core::connection_url::ConnectionUrl; +use tablepro_core::{DriverCapabilities, is_local_socket_host}; +use tablepro_storage::{ConnectionStore, SavedConnection}; + +// The schemes tablepro_core::connection_url::parse reads; the desktop entry claims each one. +pub const LINK_SCHEMES: [&str; 22] = [ + "cassandra", + "ch", + "clickhouse", + "cql", + "duckdb", + "mariadb", + "mongodb", + "mongodb+srv", + "mssql", + "mysql", + "mysqlx", + "postgres", + "postgresql", + "redis", + "rediss", + "scylla", + "scylladb", + "sqlite", + "sqlite3", + "sqlserver", + "valkey", + "valkeys", +]; + +// A JDBC URL wraps one of those schemes. It comes from a command line, never from a desktop link. +const JDBC_SCHEME: &str = "jdbc"; + +// Read from the text: a GFile a VFS rewrote can report no scheme at all. +pub fn is_link(uri: &str) -> bool { + glib::Uri::peek_scheme(uri).is_some_and(|scheme| scheme == JDBC_SCHEME || LINK_SCHEMES.contains(&scheme.as_str())) +} + +pub fn link_text(file: &gio::File) -> Option { + let uri = file.uri(); + is_link(&uri).then(|| uri.to_string()) +} + +// A failed gvfs daemon probe can release the local singleton, so the link callbacks own their backend. +fn link_vfs() -> Result { + let _default = gio::Vfs::default(); + let extension = gio::IOExtensionPoint::lookup(gio::VFS_EXTENSION_POINT_NAME) + .and_then(|point| point.extension_by_name("local")) + .ok_or_else(|| glib::bool_error!("GIO has no local VFS implementation"))?; + glib::Object::with_type(extension.type_()) + .downcast::() + .map_err(|_| glib::bool_error!("GIO's local implementation is not a VFS")) +} + +// gvfs reads these into a mount spec that drops the password and all but one MongoDB host. +pub fn register_link_schemes() -> Result<(), glib::BoolError> { + static REGISTERED: std::sync::OnceLock> = std::sync::OnceLock::new(); + REGISTERED + .get_or_init(|| { + let vfs = gio::Vfs::default(); + // Backend construction registers a scheme and cannot run under a lookup callback's scheme lock. + let local = link_vfs()?; + for scheme in LINK_SCHEMES.into_iter().chain([JDBC_SCHEME]) { + let uri_vfs = local.clone(); + let parse_vfs = local.clone(); + let registered = vfs.register_uri_scheme( + scheme, + Some(Box::new(move |_, uri| Some(uri_vfs.file_for_uri(uri)))), + Some(Box::new(move |_, name| { + is_link(name).then(|| parse_vfs.file_for_uri(name)) + })), + ); + if !registered { + return Err(glib::bool_error!( + "GIO already has a handler for a connection link scheme" + )); + } + } + Ok(()) + }) + .clone() +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LinkTarget { + pub driver_id: String, + pub host: String, + pub port: u16, + pub database: String, + pub username: String, +} + +impl LinkTarget { + // Filled as the connect dialog fills its form, so a link matches the connection that dialog saves. + pub fn new(url: &ConnectionUrl, caps: &DriverCapabilities, desktop_user: &str) -> Self { + if caps.is_file_based() { + return Self { + driver_id: url.driver_id.to_owned(), + host: String::new(), + port: 0, + database: url.database.clone(), + username: String::new(), + }; + } + let host = match url.host.as_str() { + "" => caps + .local_socket() + .and_then(|socket| socket.defaults().first()) + .map(|path| (*path).to_owned()) + .unwrap_or_default(), + host => host.to_owned(), + }; + let username = match url.username.is_empty() && is_local_socket_host(&host) { + true => desktop_user.to_owned(), + false => url.username.clone(), + }; + Self { + driver_id: url.driver_id.to_owned(), + port: url.port.or(caps.default_port()).unwrap_or(0), + database: url.database.clone(), + host, + username, + } + } + + pub fn matches(&self, saved: &SavedConnection) -> bool { + saved.driver_id == self.driver_id + && same_host(&saved.host, &self.host) + && saved.port == self.port + && saved.database == self.database + && saved.username == self.username + } +} + +// A host name has no case; a socket path does. +fn same_host(saved: &str, wanted: &str) -> bool { + match is_local_socket_host(wanted) { + true => saved == wanted, + false => saved.eq_ignore_ascii_case(wanted), + } +} + +pub fn saved_for_link<'a>(target: &LinkTarget, saved: &'a [SavedConnection]) -> Option<&'a SavedConnection> { + saved.iter().find(|connection| target.matches(connection)) +} + +// The store, not the picker's list, which may still be loading when a launch for the link gets here. +pub fn saved_for_link_blocking(target: &LinkTarget, connections: &ConnectionStore) -> Option { + match connections.load_blocking() { + Ok(saved) => saved_for_link(target, &saved).cloned(), + Err(error) => { + tracing::warn!(%error, "the saved connections could not be read to match a link against"); + None + } + } +} + +pub fn without_password(uri: &str) -> String { + let Some((scheme, rest)) = uri.split_once("://") else { + return uri.to_owned(); + }; + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + let (authority, tail) = rest.split_at(end); + let Some((userinfo, host)) = authority.rsplit_once('@') else { + return uri.to_owned(); + }; + let user = userinfo.split_once(':').map_or(userinfo, |(user, _)| user); + format!("{scheme}://{user}@{host}{tail}") +} + +#[cfg(test)] +mod tests { + use tablepro_core::connection_url::{ConnectionUrlError, parse}; + use tablepro_core::{AiPolicy, AuthMode, ExternalAccess, SafeModeLevel, TlsConfig}; + use tablepro_test_support::FakeDriver; + use uuid::Uuid; + + use super::*; + + fn saved(driver_id: &str, host: &str, port: u16, database: &str, username: &str) -> SavedConnection { + SavedConnection { + id: Uuid::new_v4(), + name: "Shop".to_owned(), + driver_id: driver_id.to_owned(), + host: host.to_owned(), + port, + database: database.to_owned(), + username: username.to_owned(), + tls: TlsConfig::disabled(), + safe_mode: SafeModeLevel::Off, + external_access: ExternalAccess::default(), + ai_policy: AiPolicy::default(), + query_timeout_secs: None, + auth_mode: AuthMode::Password, + ssh: None, + last_opened_at: None, + color: None, + group_id: None, + order: 0, + tags: Vec::new(), + icon: None, + options: Default::default(), + } + } + + fn target(url: &str, caps: &DriverCapabilities) -> LinkTarget { + LinkTarget::new(&parse(url).expect("a connection URL"), caps, "ada") + } + + #[test] + fn every_link_scheme_is_one_the_parser_reads() { + for scheme in LINK_SCHEMES { + let parsed = parse(&format!("{scheme}://db.example.com/0")); + assert!( + !matches!(parsed, Err(ConnectionUrlError::UnknownScheme { .. })), + "{scheme}: {parsed:?}" + ); + } + } + + #[test] + fn a_scheme_the_parser_does_not_read_is_no_link() { + assert!(matches!( + parse("ftp://db.example.com/shop"), + Err(ConnectionUrlError::UnknownScheme { .. }) + )); + assert!(!is_link("ftp://db.example.com/shop")); + assert!(!is_link("file:///home/ada/shop.db")); + assert!(!is_link("/home/ada/shop.db")); + } + + #[test] + fn links_are_told_by_their_scheme_whatever_its_case() { + assert!(is_link("postgresql://ada@db.example.com/shop")); + assert!(is_link("PostgreSQL://ada@db.example.com/shop")); + assert!(is_link("mongodb+srv://ada@cluster.example.com/shop")); + assert!(is_link("sqlite:/home/ada/shop.db")); + assert!(is_link("jdbc:sqlserver://db.example.com;databaseName=shop")); + } + + #[test] + fn a_link_matches_the_saved_connection_that_goes_where_it_goes() { + let caps = &FakeDriver::NETWORK_CAPABILITIES; + let shop = saved("postgres", "db.example.com", 5432, "shop", "ada"); + let list = [ + saved("postgres", "db.example.com", 5432, "stock", "ada"), + saved("postgres", "db.example.com", 5432, "shop", "bob"), + saved("postgres", "db.example.com", 6543, "shop", "ada"), + shop.clone(), + ]; + + let found = saved_for_link(&target("postgresql://ada:secret@DB.example.com:5432/shop", caps), &list); + + assert_eq!(found.map(|connection| connection.id), Some(shop.id)); + } + + #[test] + fn a_link_without_a_port_takes_the_engine_default() { + let caps = &FakeDriver::NETWORK_CAPABILITIES; + let list = [saved("postgres", "db.example.com", 1, "shop", "ada")]; + + assert!(saved_for_link(&target("postgresql://ada@db.example.com/shop", caps), &list).is_some()); + } + + #[test] + fn another_engine_never_matches() { + let caps = &FakeDriver::NETWORK_CAPABILITIES; + let list = [saved("mysql", "db.example.com", 5432, "shop", "ada")]; + + let link = target("postgresql://ada@db.example.com:5432/shop", caps); + + assert_eq!(link.driver_id, "postgres"); + assert!(saved_for_link(&link, &list).is_none()); + } + + #[test] + fn a_file_link_matches_the_saved_file() { + let caps = &FakeDriver::FILE_CAPABILITIES; + let list = [saved("sqlite", "", 0, "/home/ada/shop.db", "")]; + + assert!(saved_for_link(&target("sqlite:///home/ada/shop.db", caps), &list).is_some()); + assert!(saved_for_link(&target("sqlite:///home/ada/stock.db", caps), &list).is_none()); + } + + #[test] + fn the_password_is_taken_out_of_a_uri() { + assert_eq!( + without_password("sftp://ada:s3cret@files.example.com/shop.db"), + "sftp://ada@files.example.com/shop.db" + ); + assert_eq!( + without_password("postgresql://ada:p@ss@db.example.com:5432/shop?sslmode=require"), + "postgresql://ada@db.example.com:5432/shop?sslmode=require" + ); + assert_eq!( + without_password("redis://:pw@localhost:6379/0"), + "redis://@localhost:6379/0" + ); + assert_eq!(without_password("file:///home/ada/shop.db"), "file:///home/ada/shop.db"); + assert_eq!(without_password("https://example.com/a@b"), "https://example.com/a@b"); + } + + #[test] + fn a_registered_link_keeps_its_password_and_every_host() { + register_link_schemes().expect("register the connection links"); + + for link in [ + "postgresql://ada:s3cr%40t@db.example.com:5432/app?sslmode=require", + "mongodb://u:p@h1:27017,h2:27017,h3:27017/db?replicaSet=rs0", + "redis://:pw@localhost:6379/0", + ] { + assert_eq!(gio::File::for_uri(link).uri().as_str(), link); + assert_eq!(gio::File::for_commandline_arg(link).uri().as_str(), link); + } + } + + #[test] + fn registered_links_keep_their_text_on_other_threads() { + register_link_schemes().expect("register the connection links"); + let workers: Vec<_> = (0..4) + .map(|_| { + std::thread::spawn(|| { + register_link_schemes().expect("reuse the connection links"); + for scheme in LINK_SCHEMES.into_iter().chain([JDBC_SCHEME]) { + let link = format!("{scheme}://ada:p%40ss@first:1234,second:5678/shop?key=value"); + assert_eq!(gio::File::for_uri(&link).uri().as_str(), link); + assert_eq!(gio::File::for_parse_name(&link).uri().as_str(), link); + } + }) + }) + .collect(); + for worker in workers { + worker.join().expect("link lookups on a worker"); + } + } + + #[test] + fn parse_names_still_work_once_the_links_are_registered() { + register_link_schemes().expect("register the connection links"); + + let file = gio::File::for_parse_name("/home/ada/shop.db"); + + assert_eq!(file.path().as_deref(), Some(std::path::Path::new("/home/ada/shop.db"))); + assert_eq!( + gio::File::for_parse_name("postgresql://ada@h/db").uri().as_str(), + "postgresql://ada@h/db" + ); + } +} diff --git a/linux/crates/app/src/services/mod.rs b/linux/crates/app/src/services/mod.rs index f641fba034..43992b67cf 100644 --- a/linux/crates/app/src/services/mod.rs +++ b/linux/crates/app/src/services/mod.rs @@ -7,6 +7,7 @@ pub mod change_tracker; pub mod change_tracker_registry; pub mod column_relayout; pub mod connection_deletion; +pub mod connection_link; pub(crate) mod connection_search; pub mod deletion_error; pub mod deletion_report; diff --git a/linux/crates/app/src/ui/connect_dialog/mod.rs b/linux/crates/app/src/ui/connect_dialog/mod.rs index c8a1098636..830c8d6826 100644 --- a/linux/crates/app/src/ui/connect_dialog/mod.rs +++ b/linux/crates/app/src/ui/connect_dialog/mod.rs @@ -25,6 +25,7 @@ use gtk4::subclass::prelude::*; use libadwaita as adw; use libadwaita::prelude::*; use secrecy::SecretString; +use tablepro_core::connection_url::ConnectionUrl; use tablepro_core::credentials::CredentialInteraction; use tablepro_core::{ AiPolicy, AuthMode, ConfigError, DatabaseDriver, DriverCapabilities, DriverRegistry, ExternalAccess, LocalSocket, @@ -396,9 +397,20 @@ impl ConnectDialog { } } + // The link's text never reaches the URL row, so its password stays in the Password row alone. + pub fn fill_from_link(&self, url: ConnectionUrl) { + if let Err(message) = self.fill_from_parsed(url) { + self.show_toast(&message, None); + } + } + fn fill_from_url(&self, text: &str) -> Result<(), String> { let url = tablepro_core::connection_url::parse(text) .map_err(|error| crate::ui::error_text::connection_url_message(&error))?; + self.fill_from_parsed(url) + } + + fn fill_from_parsed(&self, url: ConnectionUrl) -> Result<(), String> { let index = self .imp() .drivers diff --git a/linux/crates/app/src/ui/session_window/connections.rs b/linux/crates/app/src/ui/session_window/connections.rs index 920d443960..5101608445 100644 --- a/linux/crates/app/src/ui/session_window/connections.rs +++ b/linux/crates/app/src/ui/session_window/connections.rs @@ -8,6 +8,7 @@ use libadwaita as adw; use libadwaita::prelude::*; use tablepro_core::SafeModeLevel; +use tablepro_core::connection_url::ConnectionUrl; use tablepro_session::connect::StoreConnectionWrites; use tablepro_session::{ConnectSource, PendingSave, RequestTarget, SessionKey}; use tablepro_storage::{ @@ -65,6 +66,21 @@ impl SessionWindow { mode: ConnectDialogMode, prefill: Option, focus: Option, + ) { + self.present_dialog(mode, prefill, focus, None); + } + + pub(super) fn on_connect_from_link(&self, url: Box) { + self.imp().link_queued.set(false); + self.present_dialog(ConnectDialogMode::Create, None, None, Some(url)); + } + + fn present_dialog( + &self, + mode: ConnectDialogMode, + prefill: Option, + focus: Option, + link: Option>, ) { let Some(state) = self.state() else { return; @@ -85,7 +101,7 @@ impl SessionWindow { }); if let Some(window) = window.upgrade() { window.imp().dialog_opening.set(false); - window.show_connect_dialog(mode, prefill, focus, environment); + window.show_connect_dialog(mode, prefill, focus, link, environment); } }); } @@ -95,6 +111,7 @@ impl SessionWindow { mode: ConnectDialogMode, prefill: Option, focus: Option, + link: Option>, ssh: SshEnvironment, ) { let (Some(state), Some(input)) = (self.state(), self.input()) else { @@ -128,6 +145,9 @@ impl SessionWindow { dialog.connect_saved(move |_, id| saved.deliver(SessionWindowInput::ConnectionSaved(id))); dialog.connect_closed(move |_| input.deliver(SessionWindowInput::DialogClosed)); dialog.present(Some(self)); + if let Some(url) = link { + dialog.fill_from_link(*url); + } if let Some(field) = focus { dialog.reveal_field(field); } diff --git a/linux/crates/app/src/ui/session_window/imp.rs b/linux/crates/app/src/ui/session_window/imp.rs index 0a5cdcfb47..2de9293473 100644 --- a/linux/crates/app/src/ui/session_window/imp.rs +++ b/linux/crates/app/src/ui/session_window/imp.rs @@ -91,6 +91,7 @@ pub struct SessionWindow { /// The connect dialog was asked for and opens once the SSH keys are /// looked for. pub(super) dialog_opening: Cell, + pub(super) link_queued: Cell, pub(super) shut_down: Cell, pub(super) launch_notices: RefCell>, pub(super) unsaved_secrets: crate::ui::UnsavedSecretToasts, diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index d708c587a9..20c71d4b2e 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -71,6 +71,7 @@ use self::session_window_command::{AttemptError, ConnectFailure, Connected, Sess pub(crate) use self::session_window_input::{LibraryEdit, MovedTab, ScriptInput, SessionWindowInput}; use crate::application::{AppServices, WindowHost, WindowLaunch}; use crate::runtime::{DeliverInput, InputSender}; +use crate::services::connection_link::without_password; use crate::services::secret_save_report::{ReportingCredentials, SecretSaveOutcome}; use crate::services::session_context::SessionContext; use crate::ui::connect_dialog::{ConnectDialogMode, ConnectSubmission}; @@ -228,6 +229,11 @@ impl SessionWindow { .map(|picker| (picker.search_bar(), picker.search_entry())) } + #[cfg(test)] + pub(crate) fn connect_dialog(&self) -> Option { + self.imp().dialog.borrow().clone() + } + #[cfg(test)] pub(crate) fn manage_tags(&self) -> Option> { self.imp().manage_tags.borrow().clone() @@ -240,6 +246,10 @@ impl SessionWindow { pub(crate) fn send(&self, message: SessionWindowInput) { if let Some(input) = self.imp().input.get() { + // Routing must see the reservation before the input queue runs. + if matches!(&message, SessionWindowInput::ConnectFromLink(_)) { + self.imp().link_queued.set(true); + } input.deliver(message); } } @@ -251,6 +261,7 @@ impl SessionWindow { self.page() == SessionPage::Picker && !imp.opening_file.get() && !imp.dialog_opening.get() + && !imp.link_queued.get() && imp.dialog.try_borrow().is_ok_and(|dialog| dialog.is_none()) } @@ -446,9 +457,8 @@ impl SessionWindow { SessionWindowInput::Restore(key) => self.on_restore(key), SessionWindowInput::RestoreSaved { id, saved } => self.on_restore_saved(id, saved.map(|saved| *saved)), SessionWindowInput::RestoreUnread(key) => self.end_restore(&key), - SessionWindowInput::OpenConnectDialog => { - self.present_connect_dialog(ConnectDialogMode::Create, None, None); - } + SessionWindowInput::OpenConnectDialog => self.present_connect_dialog(ConnectDialogMode::Create, None, None), + SessionWindowInput::ConnectFromLink(url) => self.on_connect_from_link(url), SessionWindowInput::ConnectFromDialog(submission) => self.on_connect_from_dialog(*submission), SessionWindowInput::DialogClosed => drop(self.imp().dialog.take()), SessionWindowInput::CancelConnect => self.on_cancel_connect(), @@ -1163,7 +1173,7 @@ impl SessionWindow { self.on_open_script(gio::File::for_uri(&uri)); } Err(error) => { - tracing::warn!(%error, %uri, "could not open the file"); + tracing::warn!(%error, uri = %without_password(&uri), "could not open the file"); self.forget_unclaimed(&arrived); let message = file_failure_message(&uri, &error); if !matches!(self.page(), SessionPage::Picker | SessionPage::Failed) { @@ -2118,7 +2128,7 @@ enum ShownPage { } fn file_failure_message(uri: &str, error: &FileResolveError) -> String { - let name = gio::File::for_uri(uri).parse_name().to_string(); + let name = without_password(&gio::File::for_uri(uri).parse_name()); match error { FileResolveError::NotLocal => crate::i18n::gettext_f( "{file} is not on this computer. Copy it to a local folder to open it.", diff --git a/linux/crates/app/src/ui/session_window/session_window_input.rs b/linux/crates/app/src/ui/session_window/session_window_input.rs index bfc3cb8603..39a3df0897 100644 --- a/linux/crates/app/src/ui/session_window/session_window_input.rs +++ b/linux/crates/app/src/ui/session_window/session_window_input.rs @@ -1,6 +1,7 @@ use gtk4::glib; use libadwaita as adw; use tablepro_core::SafeModeLevel; +use tablepro_core::connection_url::ConnectionUrl; use tablepro_core::credentials::SecretKind; use tablepro_core::ddl::DropDatabaseOptions; use tablepro_session::SessionKey; @@ -42,6 +43,8 @@ pub(crate) enum SessionWindowInput { saved: Option>, }, OpenConnectDialog, + /// A connection link no saved connection matches, for the connect dialog to show. + ConnectFromLink(Box), /// The connect dialog's Connect: connect, then save the entry. ConnectFromDialog(Box), DialogClosed, diff --git a/linux/crates/app/tests/application_uniqueness.rs b/linux/crates/app/tests/application_uniqueness.rs index 6cb2e6d0c9..36141a8c5c 100644 --- a/linux/crates/app/tests/application_uniqueness.rs +++ b/linux/crates/app/tests/application_uniqueness.rs @@ -1,5 +1,5 @@ -//! One TablePro per session: a second launch forwards its activation to -//! the running one over D-Bus and exits. +//! One TablePro per session: a second launch forwards its activation, or +//! its files with `--new-window`, to the running one over D-Bus and exits. #![expect( clippy::print_stderr, @@ -103,6 +103,29 @@ fn with_primary(sandbox: &Sandbox, address: &str, connection: &gio::DBusConnecti if windows != 2 { return Err(format!("{windows} windows after one plain launch and one --new-window")); } + new_window_with_a_file(sandbox, address, connection) +} + +// A second `--new-window FILE` once opened an empty window and dropped the file. +fn new_window_with_a_file(sandbox: &Sandbox, address: &str, connection: &gio::DBusConnection) -> Result<(), String> { + let database = sandbox.root().join("shop.sqlite3"); + std::fs::write(&database, b"").map_err(|error| format!("could not create {}: {error}", database.display()))?; + let database = database.to_string_lossy().into_owned(); + + let status = launch(sandbox, address, &["--new-window", database.as_str()])?; + if !status.success() { + return Err(format!("a launch with --new-window and a file exited with {status}")); + } + + wait_until("a window on the file", || { + window_ids(connection) + .iter() + .any(|window| shows_workspace(connection, window)) + })?; + let windows = window_count(connection); + if windows != 3 { + return Err(format!("{windows} windows after --new-window with a file, not 3")); + } Ok(()) } @@ -166,6 +189,50 @@ fn window_count(connection: &gio::DBusConnection) -> usize { .map_or(0, |(xml,)| xml.matches("()) + else { + return Vec::new(); + }; + xml.split(" bool { + connection + .call_sync( + Some(APP_ID), + &format!("{}/window/{window}", object_path()), + "org.gtk.Actions", + "Describe", + Some(&("disconnect",).to_variant()), + None, + gio::DBusCallFlags::NONE, + CALL_TIMEOUT_MS, + gio::Cancellable::NONE, + ) + .ok() + .and_then(|reply| reply.child_value(0).child_value(0).get::()) + .unwrap_or(false) +} + /// GApplication's object path: the id with dots as slashes. fn object_path() -> String { format!("/{}", APP_ID.replace('.', "/").replace('-', "_")) diff --git a/linux/crates/app/tests/desktop_entry.rs b/linux/crates/app/tests/desktop_entry.rs index b115540120..99326646fa 100644 --- a/linux/crates/app/tests/desktop_entry.rs +++ b/linux/crates/app/tests/desktop_entry.rs @@ -1,5 +1,5 @@ -//! The desktop entry offers TablePro for SQLite files and SQL scripts -//! and hands them over the way the application expects. +//! The desktop entry offers TablePro for SQLite files, SQL scripts and +//! connection links, and hands them over the way the application expects. use std::path::Path; @@ -45,6 +45,23 @@ fn sql_scripts_are_offered_to_tablepro() { ); } +// xdg-open and browsers hand a postgresql:// link to the app that claims x-scheme-handler/postgresql. +#[test] +fn connection_links_are_offered_to_tablepro() { + let entry = entry().expect("the desktop entry parses"); + + let types = list(&entry, MAIN, "MimeType").expect("a MimeType list"); + let mut claimed: Vec<&str> = types + .iter() + .filter_map(|kind| kind.strip_prefix("x-scheme-handler/")) + .collect(); + claimed.sort_unstable(); + let mut expected = tablepro_app::LINK_SCHEMES.to_vec(); + expected.sort_unstable(); + + assert_eq!(claimed, expected); +} + #[test] fn files_reach_the_running_application() { let entry = entry().expect("the desktop entry parses"); diff --git a/linux/crates/app/tests/kill_during_save.rs b/linux/crates/app/tests/kill_during_save.rs index db2a1bba4f..98bdc5af3d 100644 --- a/linux/crates/app/tests/kill_during_save.rs +++ b/linux/crates/app/tests/kill_during_save.rs @@ -71,7 +71,7 @@ fn app(markers: PathBuf) -> ExitCode { std::process::exit(1); } }); - match tablepro_app::run() { + match tablepro_app::run(std::env::args_os().collect()) { Ok(code) => code.into(), Err(error) => { eprintln!("TablePro did not start: {error}"); diff --git a/linux/crates/app/tests/open_files.rs b/linux/crates/app/tests/open_files.rs index 1c653f1293..5f22c41258 100644 --- a/linux/crates/app/tests/open_files.rs +++ b/linux/crates/app/tests/open_files.rs @@ -1,6 +1,8 @@ //! A database file handed over D-Bus, the way Files' Open With does, //! starts TablePro and opens a window on it. The same file again brings -//! that window forward; another file opens a second window. +//! that window forward; another file opens a second window. A connection +//! link opens a window too, and its password never reaches the log. A +//! command such as `status` never starts TablePro. #![expect( clippy::print_stderr, @@ -9,7 +11,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::process::ExitCode; +use std::process::{Command, ExitCode, Stdio}; use std::time::{Duration, Instant}; use gio::prelude::*; @@ -22,6 +24,8 @@ const APP_ID: &str = tablepro_app::config::APP_ID; const WAIT: Duration = Duration::from_secs(30); /// The first Open waits for D-Bus to start the service. const CALL_TIMEOUT_MS: i32 = 30_000; +// It must never reach the log. +const LINK_PASSWORD: &str = "link-s3cret"; fn main() -> ExitCode { match run() { @@ -61,15 +65,19 @@ fn opens(sandbox: &Sandbox, address: &str) -> Result<(), String> { let first = sandbox.database("first.sqlite3")?; let second = sandbox.database("second.sqlite3")?; - let outcome = open_twice_then_another(&connection, &first, &second); + let outcome = a_command_never_starts_tablepro(sandbox, address, &connection) + .and_then(|()| open_twice_then_another(&connection, &first, &second)) + .and_then(|()| a_link_opens_a_window_and_keeps_its_password(sandbox, &connection)); if name_has_owner(&connection) { - let quitting = quit(&connection) - .and_then(|()| wait_until("TablePro to exit after app.quit", || !name_has_owner(&connection))); + let quitting = quit_all_windows(&connection); if let (Ok(()), Err(message)) = (&outcome, quitting) { return Err(message); } } - outcome + outcome.map_err(|message| { + let log = std::fs::read_to_string(&sandbox.log).unwrap_or_default(); + format!("{message}\nTablePro's log:\n{log}") + }) } fn open_twice_then_another(connection: &gio::DBusConnection, first: &str, second: &str) -> Result<(), String> { @@ -94,6 +102,54 @@ fn open_twice_then_another(connection: &gio::DBusConnection, first: &str, second wait_until("a window on the second file", || window_ids(connection).len() == 2) } +// A call to TablePro's own name would have D-Bus start it through the service file. +fn a_command_never_starts_tablepro( + sandbox: &Sandbox, + address: &str, + connection: &gio::DBusConnection, +) -> Result<(), String> { + let output = Command::new(env!("CARGO_BIN_EXE_tablepro")) + .arg("status") + .envs(sandbox.environment.iter().cloned()) + .env("DBUS_SESSION_BUS_ADDRESS", address) + .stdin(Stdio::null()) + .output() + .map_err(|error| format!("could not run tablepro status: {error}"))?; + + if output.status.success() { + return Err(format!( + "tablepro status said TablePro is ready when it is not running:\n{}", + String::from_utf8_lossy(&output.stdout) + )); + } + std::thread::sleep(Duration::from_millis(500)); + if name_has_owner(connection) { + return Err("tablepro status started TablePro".to_owned()); + } + Ok(()) +} + +// Both windows show a database, so the link gets a window of its own for its connect dialog. +fn a_link_opens_a_window_and_keeps_its_password( + sandbox: &Sandbox, + connection: &gio::DBusConnection, +) -> Result<(), String> { + let before = window_ids(connection).len(); + open( + connection, + &format!("postgresql://ada:{LINK_PASSWORD}@db.invalid:5432/shop?sslmode=require"), + )?; + wait_until("a window for the link", || window_ids(connection).len() == before + 1)?; + // The old path logged the URL as a file it could not open, after the window appeared. + std::thread::sleep(Duration::from_millis(500)); + let log = std::fs::read_to_string(&sandbox.log) + .map_err(|error| format!("could not read {}: {error}", sandbox.log.display()))?; + if log.contains(LINK_PASSWORD) { + return Err(format!("TablePro logged the link's password:\n{log}")); + } + Ok(()) +} + /// What Files sends for Open With on a D-Bus activatable application. fn open(connection: &gio::DBusConnection, uri: &str) -> Result<(), String> { let platform_data: HashMap = HashMap::new(); @@ -135,6 +191,22 @@ fn quit(connection: &gio::DBusConnection) -> Result<(), String> { .map_err(|error| format!("app.quit over D-Bus failed: {error}")) } +fn quit_all_windows(connection: &gio::DBusConnection) -> Result<(), String> { + let deadline = Instant::now() + WAIT; + let mut refused = None; + while name_has_owner(connection) { + if Instant::now() >= deadline { + let reason = refused.map(|error| format!(": {error}")).unwrap_or_default(); + return Err(format!("timed out waiting for TablePro to exit after app.quit{reason}")); + } + // A link's modal dialog takes the first quit, so its window needs another once the dialog closes. + // An exiting GApplication drops its actions before its name, so a quit sent then fails. + refused = quit(connection).err(); + std::thread::sleep(Duration::from_millis(50)); + } + Ok(()) +} + fn name_has_owner(connection: &gio::DBusConnection) -> bool { connection .call_sync( @@ -220,6 +292,9 @@ fn wait_until(what: &str, mut ready: impl FnMut() -> bool) -> Result<(), String> struct Sandbox { root: PathBuf, services: PathBuf, + environment: Vec<(String, PathBuf)>, + // TablePro logs to stderr. + log: PathBuf, } impl Sandbox { @@ -251,10 +326,14 @@ impl Sandbox { } } let services = dir("services")?; - write_service(&services, &environment)?; + let log = root.join("tablepro.log"); + std::fs::write(&log, b"").map_err(|error| format!("could not create {}: {error}", log.display()))?; + write_service(&services, &environment, &log)?; Ok(Self { root: root.to_path_buf(), services, + environment, + log, }) } @@ -268,13 +347,16 @@ impl Sandbox { /// The D-Bus service file that starts this build's binary, with the /// sandbox set in its Exec line rather than in this process. -fn write_service(dir: &Path, environment: &[(String, PathBuf)]) -> Result<(), String> { +fn write_service(dir: &Path, environment: &[(String, PathBuf)], log: &Path) -> Result<(), String> { let mut exec = vec!["/usr/bin/env".to_owned()]; for (name, value) in environment { exec.push(quoted(&format!("{name}={}", value.display()))?); } + exec.push("/bin/sh".to_owned()); + exec.push("-c".to_owned()); + exec.push(quoted(r#"exec "$0" --gapplication-service 2>>"$1""#)?); exec.push(quoted(env!("CARGO_BIN_EXE_tablepro"))?); - exec.push("--gapplication-service".to_owned()); + exec.push(quoted(&log.display().to_string())?); let service = format!("[D-BUS Service]\nName={APP_ID}\nExec={}\n", exec.join(" ")); let path = dir.join(format!("{APP_ID}.service")); std::fs::write(&path, service).map_err(|error| format!("could not write {}: {error}", path.display())) diff --git a/linux/crates/app/tests/startup.rs b/linux/crates/app/tests/startup.rs index e9b19b9d3c..eb59cbd0e0 100644 --- a/linux/crates/app/tests/startup.rs +++ b/linux/crates/app/tests/startup.rs @@ -27,16 +27,17 @@ fn main() -> ExitCode { if the_binary_has_no_posthog_key().is_err() { return ExitCode::FAILURE; } - if help_lists_the_gapplication_options(&schemas, &sandbox).is_err() { - return ExitCode::FAILURE; - } - if version_prints_the_version(&schemas, &sandbox).is_err() { - return ExitCode::FAILURE; - } - if a_missing_schema_is_reported(&sandbox).is_err() { - return ExitCode::FAILURE; + let cases = [ + help_all_lists_the_gapplication_options(&schemas, &sandbox), + version_prints_the_version_with_no_display_or_schema(&sandbox), + commands_need_no_display_or_schema(&sandbox), + an_unknown_word_is_a_usage_error(&sandbox), + a_missing_schema_is_reported(&sandbox), + ]; + match cases.iter().all(Result::is_ok) { + true => ExitCode::SUCCESS, + false => ExitCode::FAILURE, } - ExitCode::SUCCESS } /// The directories a run is confined to. @@ -163,31 +164,85 @@ fn the_binary_has_no_posthog_key() -> Result<(), ()> { /// `--help-gapplication` appears only once GApplication has parsed the /// command line, which happens after `run()` registered the embedded /// GResource. A missing or malformed gresource aborts before that. -fn help_lists_the_gapplication_options(schemas: &Path, sandbox: &Sandbox<'_>) -> Result<(), ()> { +fn help_all_lists_the_gapplication_options(schemas: &Path, sandbox: &Sandbox<'_>) -> Result<(), ()> { let output = run( - tablepro(sandbox).arg("--help").env("GSETTINGS_SCHEMA_DIR", schemas), - "--help", + tablepro(sandbox).arg("--help-all").env("GSETTINGS_SCHEMA_DIR", schemas), + "--help-all", )?; if !output.status.success() { - eprintln!("tablepro --help exited with {}:\n{}", output.status, combined(&output)); + eprintln!( + "tablepro --help-all exited with {}:\n{}", + output.status, + combined(&output) + ); return Err(()); } let stdout = String::from_utf8_lossy(&output.stdout); - if !stdout.contains("--help-gapplication") { - eprintln!("tablepro --help did not list the GApplication options:\n{stdout}"); + for option in ["--help-gapplication", "--new-window"] { + if !stdout.contains(option) { + eprintln!("tablepro --help-all did not list {option}:\n{stdout}"); + return Err(()); + } + } + Ok(()) +} + +// Without a display GTK cannot start and without the schema the GUI refuses to, so a run that succeeds here +// opened no window. +fn headless(sandbox: &Sandbox<'_>) -> Command { + let mut command = tablepro(sandbox); + command + .env("GSETTINGS_SCHEMA_DIR", sandbox.empty) + .env_remove("DISPLAY") + .env_remove("WAYLAND_DISPLAY") + .env_remove("DBUS_SESSION_BUS_ADDRESS"); + command +} + +fn commands_need_no_display_or_schema(sandbox: &Sandbox<'_>) -> Result<(), ()> { + let cases: [(&[&str], &str); 7] = [ + (&["--help"], "completion"), + (&["-h"], "completion"), + (&["help"], "completion"), + (&["help", "query"], "--connection"), + (&["completion", "bash"], "tablepro"), + (&["completion", "zsh"], "tablepro"), + (&["completion", "fish"], "tablepro"), + ]; + for (arguments, expected) in cases { + let what = arguments.join(" "); + let output = run(headless(sandbox).args(arguments), &what)?; + let stdout = String::from_utf8_lossy(&output.stdout); + if !output.status.success() || !stdout.contains(expected) { + eprintln!( + "tablepro {what} exited with {} without {expected} on stdout:\n{}", + output.status, + combined(&output) + ); + return Err(()); + } + } + Ok(()) +} + +// It once opened a window saying the file could not be found. +fn an_unknown_word_is_a_usage_error(sandbox: &Sandbox<'_>) -> Result<(), ()> { + let output = run(headless(sandbox).arg("qeury"), "an unknown word")?; + + if output.status.code() != Some(2) || output.stderr.is_empty() { + eprintln!( + "tablepro qeury exited with {}, not 2 with a message:\n{}", + output.status, + combined(&output) + ); return Err(()); } Ok(()) } -/// `--version` prints the version and exits rather than opening a -/// window. -fn version_prints_the_version(schemas: &Path, sandbox: &Sandbox<'_>) -> Result<(), ()> { - let output = run( - tablepro(sandbox).arg("--version").env("GSETTINGS_SCHEMA_DIR", schemas), - "--version", - )?; +fn version_prints_the_version_with_no_display_or_schema(sandbox: &Sandbox<'_>) -> Result<(), ()> { + let output = run(headless(sandbox).arg("--version"), "--version")?; if !output.status.success() { eprintln!( diff --git a/linux/data/app.tablepro.TablePro.desktop.in.in b/linux/data/app.tablepro.TablePro.desktop.in.in index 2830f5f5f5..f8c22c3e79 100644 --- a/linux/data/app.tablepro.TablePro.desktop.in.in +++ b/linux/data/app.tablepro.TablePro.desktop.in.in @@ -9,7 +9,7 @@ Icon=@app-id@ Terminal=false Type=Application Categories=Development;Database; -MimeType=application/vnd.sqlite3;application/sql; +MimeType=application/vnd.sqlite3;application/sql;x-scheme-handler/cassandra;x-scheme-handler/ch;x-scheme-handler/clickhouse;x-scheme-handler/cql;x-scheme-handler/duckdb;x-scheme-handler/mariadb;x-scheme-handler/mongodb;x-scheme-handler/mongodb+srv;x-scheme-handler/mssql;x-scheme-handler/mysql;x-scheme-handler/mysqlx;x-scheme-handler/postgres;x-scheme-handler/postgresql;x-scheme-handler/redis;x-scheme-handler/rediss;x-scheme-handler/scylla;x-scheme-handler/scylladb;x-scheme-handler/sqlite;x-scheme-handler/sqlite3;x-scheme-handler/sqlserver;x-scheme-handler/valkey;x-scheme-handler/valkeys; DBusActivatable=true StartupNotify=true X-GNOME-UsesNotifications=true diff --git a/linux/data/completions/meson.build b/linux/data/completions/meson.build new file mode 100644 index 0000000000..da7cfab412 --- /dev/null +++ b/linux/data/completions/meson.build @@ -0,0 +1,22 @@ +# The built binary prints each script, so it always matches the binary's +# commands. Flatpak exports no completions, and its users type +# `flatpak run app.tablepro.TablePro`, which a tablepro completion does not +# match. The directories are the ones bash-completion, zsh and fish read on +# Fedora; debian/rules moves the zsh one to Debian's vendor-completions. +if not get_option('flatpak') + completions = { + 'bash': ['tablepro', get_option('datadir') / 'bash-completion' / 'completions'], + 'zsh': ['_tablepro', get_option('datadir') / 'zsh' / 'site-functions'], + 'fish': ['tablepro.fish', get_option('datadir') / 'fish' / 'vendor_completions.d'], + } + foreach shell, place : completions + custom_target( + 'completion-' + shell, + output: place[0], + command: [tablepro_binaries[0], 'completion', shell], + capture: true, + install: true, + install_dir: place[1], + ) + endforeach +endif diff --git a/linux/data/licenses/THIRD-PARTY-NOTICE.md b/linux/data/licenses/THIRD-PARTY-NOTICE.md index 5388810773..4bb4edb6d8 100644 --- a/linux/data/licenses/THIRD-PARTY-NOTICE.md +++ b/linux/data/licenses/THIRD-PARTY-NOTICE.md @@ -1287,7 +1287,7 @@ Used by: Used by: -- rmcp 3.6.0 +- rmcp 3.9.0 ~~~~text Apache License @@ -8112,7 +8112,7 @@ DEALINGS IN THE SOFTWARE. Used by: -- rmcp 3.6.0 +- rmcp 3.9.0 ~~~~text Copyright (c) 2024-2025 Model Context Protocol a Series of LF Projects, LLC. @@ -8210,6 +8210,12 @@ DEALINGS IN THE SOFTWARE. Used by: +- anstyle 1.0.14 +- clap 4.6.7 +- clap_builder 4.6.7 +- clap_complete 4.6.11 +- clap_derive 4.6.7 +- clap_lex 1.1.1 - env_filter 2.0.0 - env_logger 0.11.11 - toml_datetime 1.1.1+spec-1.1.0 diff --git a/linux/data/licenses/third-party.json b/linux/data/licenses/third-party.json index cef205f7b9..9c338790ed 100644 --- a/linux/data/licenses/third-party.json +++ b/linux/data/licenses/third-party.json @@ -65,7 +65,7 @@ "name": "Apache License 2.0", "text": " Apache License\n Version 2.0, January 2004\n http://www.apache.org/licenses/\n\n TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n 1. Definitions.\n\n \"License\" shall mean the terms and conditions for use, reproduction,\n and distribution as defined by Sections 1 through 9 of this document.\n\n \"Licensor\" shall mean the copyright owner or entity authorized by\n the copyright owner that is granting the License.\n\n \"Legal Entity\" shall mean the union of the acting entity and all\n other entities that control, are controlled by, or are under common\n control with that entity. For the purposes of this definition,\n \"control\" means (i) the power, direct or indirect, to cause the\n direction or management of such entity, whether by contract or\n otherwise, or (ii) ownership of fifty percent (50%) or more of the\n outstanding shares, or (iii) beneficial ownership of such entity.\n\n \"You\" (or \"Your\") shall mean an individual or Legal Entity\n exercising permissions granted by this License.\n\n \"Source\" form shall mean the preferred form for making modifications,\n including but not limited to software source code, documentation\n source, and configuration files.\n\n \"Object\" form shall mean any form resulting from mechanical\n transformation or translation of a Source form, including but\n not limited to compiled object code, generated documentation,\n and conversions to other media types.\n\n \"Work\" shall mean the work of authorship, whether in Source or\n Object form, made available under the License, as indicated by a\n copyright notice that is included in or attached to the work\n (an example is provided in the Appendix below).\n\n \"Derivative Works\" shall mean any work, whether in Source or Object\n form, that is based on (or derived from) the Work and for which the\n editorial revisions, annotations, elaborations, or other modifications\n represent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) to the interfaces of,\n the Work and Derivative Works thereof.\n\n \"Contribution\" shall mean any work of authorship, including\n the original version of the Work and any modifications or additions\n to that Work or Derivative Works thereof, that is intentionally\n submitted to the Licensor for inclusion in the Work by the copyright\n owner or by an individual or Legal Entity authorized to submit on behalf\n of the copyright owner. For the purposes of this definition, \"submitted\"\n means any form of electronic, verbal, or written communication sent\n to the Licensor or its representatives, including but not limited to\n communication on electronic mailing lists, source code control systems,\n and issue tracking systems that are managed by, or on behalf of, the\n Licensor for the purpose of discussing and improving the Work, but\n excluding communication that is conspicuously marked or otherwise\n designated in writing by the copyright owner as \"Not a Contribution.\"\n\n \"Contributor\" shall mean Licensor and any individual or Legal Entity\n on behalf of whom a Contribution has been received by Licensor and\n subsequently incorporated within the Work.\n\n 2. Grant of Copyright License. Subject to the terms and conditions of\n this License, each Contributor hereby grants to You a perpetual,\n worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n copyright license to reproduce, prepare Derivative Works of,\n publicly display, publicly perform, sublicense, and distribute the\n Work and such Derivative Works in Source or Object form.\n\n 3. Grant of Patent License. Subject to the terms and conditions of\n this License, each Contributor hereby grants to You a perpetual,\n worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n (except as stated in this section) patent license to make, have made,\n use, offer to sell, sell, import, and otherwise transfer the Work,\n where such license applies only to those patent claims licensable\n by such Contributor that are necessarily infringed by their\n Contribution(s) alone or by combination of their Contribution(s)\n with the Work to which such Contribution(s) was submitted. If You\n institute patent litigation against any entity (including a\n cross-claim or counterclaim in a lawsuit) alleging that the Work\n or a Contribution incorporated within the Work constitutes direct\n or contributory patent infringement, then any patent licenses\n granted to You under this License for that Work shall terminate\n as of the date such litigation is filed.\n\n 4. Redistribution. You may reproduce and distribute copies of the\n Work or Derivative Works thereof in any medium, with or without\n modifications, and in Source or Object form, provided that You\n meet the following conditions:\n\n (a) You must give any other recipients of the Work or\n Derivative Works a copy of this License; and\n\n (b) You must cause any modified files to carry prominent notices\n stating that You changed the files; and\n\n (c) You must retain, in the Source form of any Derivative Works\n that You distribute, all copyright, patent, trademark, and\n attribution notices from the Source form of the Work,\n excluding those notices that do not pertain to any part of\n the Derivative Works; and\n\n (d) If the Work includes a \"NOTICE\" text file as part of its\n distribution, then any Derivative Works that You distribute must\n include a readable copy of the attribution notices contained\n within such NOTICE file, excluding those notices that do not\n pertain to any part of the Derivative Works, in at least one\n of the following places: within a NOTICE text file distributed\n as part of the Derivative Works; within the Source form or\n documentation, if provided along with the Derivative Works; or,\n within a display generated by the Derivative Works, if and\n wherever such third-party notices normally appear. The contents\n of the NOTICE file are for informational purposes only and\n do not modify the License. You may add Your own attribution\n notices within Derivative Works that You distribute, alongside\n or as an addendum to the NOTICE text from the Work, provided\n that such additional attribution notices cannot be construed\n as modifying the License.\n\n You may add Your own copyright statement to Your modifications and\n may provide additional or different license terms and conditions\n for use, reproduction, or distribution of Your modifications, or\n for any such Derivative Works as a whole, provided Your use,\n reproduction, and distribution of the Work otherwise complies with\n the conditions stated in this License.\n\n 5. Submission of Contributions. Unless You explicitly state otherwise,\n any Contribution intentionally submitted for inclusion in the Work\n by You to the Licensor shall be under the terms and conditions of\n this License, without any additional terms or conditions.\n Notwithstanding the above, nothing herein shall supersede or modify\n the terms of any separate license agreement you may have executed\n with Licensor regarding such Contributions.\n\n 6. Trademarks. This License does not grant permission to use the trade\n names, trademarks, service marks, or product names of the Licensor,\n except as required for reasonable and customary use in describing the\n origin of the Work and reproducing the content of the NOTICE file.\n\n 7. Disclaimer of Warranty. Unless required by applicable law or\n agreed to in writing, Licensor provides the Work (and each\n Contributor provides its Contributions) on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n implied, including, without limitation, any warranties or conditions\n of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n PARTICULAR PURPOSE. You are solely responsible for determining the\n appropriateness of using or redistributing the Work and assume any\n risks associated with Your exercise of permissions under this License.\n\n 8. Limitation of Liability. In no event and under no legal theory,\n whether in tort (including negligence), contract, or otherwise,\n unless required by applicable law (such as deliberate and grossly\n negligent acts) or agreed to in writing, shall any Contributor be\n liable to You for damages, including any direct, indirect, special,\n incidental, or consequential damages of any character arising as a\n result of this License or out of the use or inability to use the\n Work (including but not limited to damages for loss of goodwill,\n work stoppage, computer failure or malfunction, or any and all\n other commercial damages or losses), even if such Contributor\n has been advised of the possibility of such damages.\n\n 9. Accepting Warranty or Additional Liability. While redistributing\n the Work or Derivative Works thereof, You may choose to offer,\n and charge a fee for, acceptance of support, warranty, indemnity,\n or other liability obligations and/or rights consistent with this\n License. However, in accepting such obligations, You may act only\n on Your own behalf and on Your sole responsibility, not on behalf\n of any other Contributor, and only if You agree to indemnify,\n defend, and hold each Contributor harmless for any liability\n incurred by, or claims asserted against, such Contributor by reason\n of your accepting any such warranty or additional liability.\n\n END OF TERMS AND CONDITIONS", "used_by": [ - { "crate": { "name": "rmcp", "version": "3.6.0" } } + { "crate": { "name": "rmcp", "version": "3.9.0" } } ] }, { @@ -1250,7 +1250,7 @@ "name": "MIT License", "text": "Copyright (c) 2024-2025 Model Context Protocol a Series of LF Projects, LLC.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.", "used_by": [ - { "crate": { "name": "rmcp", "version": "3.6.0" } } + { "crate": { "name": "rmcp", "version": "3.9.0" } } ] }, { @@ -1274,6 +1274,12 @@ "name": "MIT License", "text": "Copyright (c) Individual contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", "used_by": [ + { "crate": { "name": "anstyle", "version": "1.0.14" } }, + { "crate": { "name": "clap", "version": "4.6.7" } }, + { "crate": { "name": "clap_builder", "version": "4.6.7" } }, + { "crate": { "name": "clap_complete", "version": "4.6.11" } }, + { "crate": { "name": "clap_derive", "version": "4.6.7" } }, + { "crate": { "name": "clap_lex", "version": "1.1.1" } }, { "crate": { "name": "env_filter", "version": "2.0.0" } }, { "crate": { "name": "env_logger", "version": "0.11.11" } }, { "crate": { "name": "toml_datetime", "version": "1.1.1+spec-1.1.0" } }, @@ -2633,6 +2639,7 @@ { "name": "ahash", "version": "0.8.12", "license": "MIT OR Apache-2.0" }, { "name": "aho-corasick", "version": "1.1.5", "license": "Unlicense OR MIT" }, { "name": "allocator-api2", "version": "0.2.21", "license": "MIT OR Apache-2.0" }, + { "name": "anstyle", "version": "1.0.14", "license": "MIT OR Apache-2.0" }, { "name": "arc-swap", "version": "1.9.3", "license": "MIT OR Apache-2.0" }, { "name": "arcstr", "version": "1.2.0", "license": "Apache-2.0 OR MIT OR Zlib" }, { "name": "arrow", "version": "60.1.0", "license": "Apache-2.0" }, @@ -2687,6 +2694,11 @@ { "name": "chacha20", "version": "0.10.2", "license": "MIT OR Apache-2.0" }, { "name": "chrono", "version": "0.4.45", "license": "MIT OR Apache-2.0" }, { "name": "cipher", "version": "0.4.4", "license": "MIT OR Apache-2.0" }, + { "name": "clap", "version": "4.6.7", "license": "MIT OR Apache-2.0" }, + { "name": "clap_builder", "version": "4.6.7", "license": "MIT OR Apache-2.0" }, + { "name": "clap_complete", "version": "4.6.11", "license": "MIT OR Apache-2.0" }, + { "name": "clap_derive", "version": "4.6.7", "license": "MIT OR Apache-2.0" }, + { "name": "clap_lex", "version": "1.1.1", "license": "MIT OR Apache-2.0" }, { "name": "clickhouse", "version": "0.15.2", "license": "MIT OR Apache-2.0" }, { "name": "clickhouse-macros", "version": "0.3.0", "license": "MIT OR Apache-2.0" }, { "name": "clickhouse-types", "version": "0.1.3", "license": "MIT OR Apache-2.0" }, @@ -2990,7 +3002,7 @@ { "name": "regress", "version": "0.12.0", "license": "MIT OR Apache-2.0" }, { "name": "reqwest", "version": "0.13.5", "license": "MIT OR Apache-2.0" }, { "name": "resolv-conf", "version": "0.7.6", "license": "MIT OR Apache-2.0" }, - { "name": "rmcp", "version": "3.6.0", "license": "Apache-2.0 AND MIT" }, + { "name": "rmcp", "version": "3.9.0", "license": "Apache-2.0 AND MIT" }, { "name": "rmp", "version": "0.8.15", "license": "MIT" }, { "name": "rmp-serde", "version": "1.3.1", "license": "MIT" }, { "name": "roxmltree", "version": "0.20.0", "license": "MIT OR Apache-2.0" }, diff --git a/linux/data/tablepro.1 b/linux/data/tablepro.1 index 9899d2ff42..e65ac49237 100644 --- a/linux/data/tablepro.1 +++ b/linux/data/tablepro.1 @@ -1,90 +1,340 @@ .TH TABLEPRO 1 2026-11-07 "TablePro 0.1.0-beta.3" "User Commands" .SH NAME -tablepro \- work with your databases +tablepro \- open databases and query saved connections from a shell .SH SYNOPSIS -.B tablepro -.RI [ OPTION ...] -.RI [ FILE ...] +.SY tablepro +.OP \-n +.RI [ FILE " | " URL " ...]" +.YS +.SY "tablepro open" +.OP \-n +.RI [ FILE " | " URL " ...]" +.YS +.SY "tablepro query" +.BI \-c " CONNECTION" +.OP \-\-database NAME +.OP \-\-format FORMAT +.OP \-\-limit ROWS +.OP \-\-timeout SECONDS +.OP \-\-read\-only +.RI [ SQL " |" +.BI \-f " FILE" +.RB ] +.YS +.SY "tablepro connections" +.OP \-\-format FORMAT +.YS +.SY "tablepro databases" +.BI \-c " CONNECTION" +.OP \-\-format FORMAT +.YS +.SY "tablepro schemas" +.BI \-c " CONNECTION" +.OP \-\-database NAME +.OP \-\-format FORMAT +.YS +.SY "tablepro tables" +.BI \-c " CONNECTION" +.OP \-\-database NAME +.OP \-\-schema NAME +.OP \-\-format FORMAT +.YS +.SY "tablepro describe" +.BI \-c " CONNECTION" +.OP \-\-database NAME +.OP \-\-schema NAME +.RB [ \-\-ddl " | " \-\-format " FORMAT ]" +.I TABLE +.YS +.SY "tablepro export" +.BI \-c " CONNECTION" +.BI \-\-format " FORMAT" +.OP \-\-database NAME +.OP \-\-schema NAME +.OP \-\-limit ROWS +.IR TABLE ... +.YS +.SY "tablepro status" +.OP \-\-format FORMAT +.YS +.SY "tablepro completion" +.BR bash " | " zsh " | " fish +.YS +.SY "tablepro help" +.RI [ COMMAND ] +.YS +.SY tablepro +.BR \-\-help " | " \-\-version +.YS .SH DESCRIPTION -TablePro connects to PostgreSQL, MySQL, SQLite, DuckDB, SQL Server, ClickHouse, -Cassandra, ScyllaDB, Redis and MongoDB. -It browses tables, edits rows in place, runs SQL with syntax highlighting, -and reaches servers behind an SSH bastion through the system OpenSSH client. -.PP -Each -.I FILE -is a SQLite or DuckDB database, opened in its own window, -or a SQL file, opened in an editor tab of the connection in front. -A SQL file never runs by itself; with no connection open, the window asks for one. -A running TablePro opens the files itself and the new launch exits. +TablePro browses and edits PostgreSQL, MySQL, MariaDB, SQLite, DuckDB, +SQL Server, ClickHouse, Cassandra, ScyllaDB, Redis and MongoDB databases. +.PP +Run without a command to open the graphical app. +Files and connection URLs open there, in the running instance when one exists. +Queries, schema listings and exports print in the terminal. +.PP +The Linux packages install +.BR tablepro . +For Flatpak, replace it with +.B flatpak run app.tablepro.TablePro +in each invocation. +Run the command from the same installation as the app. +.SH COMMANDS +Commands that use the database require the app to be running with +.B Preferences > MCP > Enable MCP Server +on. +They read the server port from the app's settings and its token from the keyring. +They never start the app or create a token. +There is no pairing command. +.PP +A command opens a closed connection only when standard error is a terminal. +Answer any prompt in the app; the command waits up to five minutes. +For unattended scripts, open the connection first. +.TP +.B open +Open files and links, or the app with no targets. +SQL files open in an editor without running. +SQLite and DuckDB files open database sessions. +A link matching a saved connection asks before opening it; +other links fill the connect dialog and wait for Connect. +Local links also require consent. +.TP +.B query +Run one statement, or one SQL Server batch. +Read UTF-8 SQL from the argument, a file, or standard input. +With no argument or file, standard input must be a pipe or redirected file. +Use +.B \-f \- +to read from a terminal until end of file. +SQL text is limited to 100 KiB. +.TP +.B connections +List visible saved connections by name, id and type. +.TP +.B databases +List the connection's databases. +An engine without database listing returns an error. +.TP +.B schemas +List schemas, including default and system flags. +.TP +.B tables +List tables and views in a schema. +.TP +.B describe +Print columns, indexes and foreign keys as separate result sets. +Use +.B \-\-ddl +for the CREATE statements instead. +.TP +.B export +Print data from one to 20 tables or views. +Choose CSV, JSON or SQL INSERT statements with the required +.B \-\-format +option. +Names are unquoted SQL identifiers; use shell quotes for spaces and +.B \-\-schema +for the schema. +Redirect standard output to save a file. +.TP +.B status +Report the app version, running state, MCP setting and port, +token availability, server version and readiness. +Does not start the app or open a connection. +.TP +.B completion +Print a completion script for bash, zsh or fish. +The .deb and .rpm packages install these scripts; Flatpak does not. +Connection names are not looked up for completion. +.TP +.B help +Show all commands, or the options of one command. .SH OPTIONS +Options apply to the commands shown in SYNOPSIS. .TP -.BR \-n ", " \-\-new\-window -Open a new window, in the running TablePro when there is one. +.BR \-c ", " \-\-connection " CONNECTION" +Select a saved connection by name or id. +Use an id if several connections have the same name. .TP -.B \-\-version -Print the version and exit. +.BI \-\-database " NAME" +Use a database already open beside the connection through Open Database. +The default is the connection's own database. +.TP +.BI \-\-schema " NAME" +Use a schema instead of the connection's default. +This option is not accepted by query; qualify names in the SQL instead. +.TP +.BR \-f ", " \-\-file " FILE" +Read SQL from FILE, or standard input when FILE is a single dash. +Cannot be combined with a SQL argument. +.TP +.BI \-\-format " FORMAT" +For query, listings, describe and status: csv, tsv, json, jsonl or table. +The default is table on a terminal and csv otherwise. +For export: csv, json or sql, with no default. +Format names accept any letter case. +.TP +.BI \-\-limit " ROWS" +Return 1 to 1,000 rows, with 100 as the default. +For query the limit applies per result set; for export, per table. +Truncation is reported on standard error. +The limit does not bound how many rows a write changes. +.TP +.BI \-\-timeout " SECONDS" +Allow 1 to 300 seconds for the statement, with 30 as the default. +Time spent waiting for write approval is not part of this timeout. +.TP +.B \-\-read\-only +Refuse any statement that is not a read, before asking for write approval. +.TP +.B \-\-ddl +Print CREATE statements instead of a table description. +Cannot be combined with +.BR \-\-format . +.TP +.BR \-n ", " \-\-new\-window +Open a new window, with any supplied files or URLs. .TP .BR \-h ", " \-\-help -Show the options and exit. +Print help and exit. +.TP +.B \-\-version +Print the app version and exit. .TP .B \-\-help\-all -Show every option, including the GApplication ones, and exit. -.SH ENVIRONMENT +Show the GUI and GApplication options. .TP -.B RUST_LOG -Which messages are logged to standard error, as a tracing filter such as -.BR info " or " tablepro=debug . +.B \-\- +End options. +Place SQL or file names beginning with a dash after this marker. +.SH OUTPUT +Rows go to standard output; errors, notices and truncation messages go to standard error. +CSV has a header, an empty field for NULL and two quotes for an empty string. +TSV escapes tabs, newlines, carriage returns and backslashes. +JSON is an array of objects; JSONL writes one object per line. +The table format aligns columns and displays control characters as escapes. +.PP +Several result sets become an array of arrays in JSON, +consecutive objects in JSONL, or blocks separated by a blank line otherwise. +Several JSON table exports also form an array of arrays. +.SH ACCESS +The connection's External Clients setting controls access. +Blocked connections are omitted from listings and cannot be selected. +Read Only is the default and refuses writes. +Read & Write still requires approval for every write in the connection's window. +Safe Mode Read-Only refuses writes regardless of external access. +A transaction left open by a write is rolled back when that call ends. +.SH CONNECTION URLS +The desktop registers these schemes: +postgresql, postgres, mysql, mariadb, mysqlx, sqlserver, mssql, +clickhouse, ch, cassandra, cql, scylla, scylladb, redis, rediss, +valkey, valkeys, mongodb, mongodb+srv, sqlite, sqlite3 and duckdb. +A jdbc wrapper around a supported URL also works from the command line. +.PP +A link never connects without consent. +For a saved connection, click Open; Enter and Escape cancel. +For other links, review the connect dialog and click Connect. +A successful connection from that dialog is saved. +.SH EXIT STATUS .TP -.B TABLEPRO_LOG_FORMAT -.B json -logs one JSON object per line. +.B 0 +Success, including help, version and completion output. +A pipe closed by its reader also exits 0. +.TP +.B 1 +Database, connection or output error. +Also used when the app is stopped, a script finds a closed connection, +or a connection does not open within the wait period. +.TP +.B 2 +Invalid usage, SQL input or option value; unknown or ambiguous connection; +missing file. +.TP +.B 3 +Access refused, MCP server disabled, token rejected, or approval declined. +.TP +.B 4 +Query or server request timed out. .SH FILES .TP .I $XDG_CONFIG_HOME/tablepro/connections.json -The saved connections: hosts, ports, user names and SSH settings. -Passwords are not in it; they are in the Secret Service keyring. +Saved connections. Passwords and the MCP token live in the keyring. +.TP +.I $XDG_CONFIG_HOME/tablepro/column_layouts.json +Column layout settings. .TP -.I $XDG_CONFIG_HOME/tablepro/column_widths.json -.TQ .I $XDG_CONFIG_HOME/tablepro/filter_settings.json -Column widths and filters, kept per table. +Saved table filters. .TP .I $XDG_STATE_HOME/tablepro/history.db -The query history and the saved queries, in a SQLite database. +Query history and saved queries. .TP .I $XDG_STATE_HOME/tablepro/workspaces/ -The tabs of each connection, and which connections were open at the last quit. +Saved tabs and open connections. .TP .I $XDG_DATA_HOME/tablepro/drafts/ -The text of each SQL editor tab. +SQL editor drafts. .TP .I $XDG_STATE_HOME/tablepro/crashes/ -A report of each crash. -.TP -.I $XDG_RUNTIME_DIR/app/app.tablepro.TablePro/ssh/ -The control sockets of the open SSH tunnels. +Crash reports from the graphical app. .PP -When unset, -.B XDG_CONFIG_HOME -is +Preferences use the GSettings schema +.BR app.tablepro.TablePro . +Flatpak uses its own settings and keyring storage. +.SH ENVIRONMENT +.TP +.BR XDG_CONFIG_HOME ", " XDG_STATE_HOME ", " XDG_DATA_HOME +Base directories for application files. +Defaults are .IR ~/.config , -.B XDG_STATE_HOME -is .I ~/.local/state and -.B XDG_DATA_HOME -is -.IR ~/.local/share . -Preferences are kept in GSettings, under the schema -.BR app.tablepro.TablePro . -.SH BUGS -Report bugs at https://github.com/TableProApp/TablePro/issues. -Include the text that Copy Text puts on the clipboard in -About TablePro > Troubleshooting > Debugging Information. +.IR ~/.local/share , +respectively. +.TP +.B RUST_LOG +Log filter for the graphical app, such as +.BR tablepro=debug . +Does not control query output. +.TP +.B TABLEPRO_LOG_FORMAT +Graphical app log format: text by default, or json. +.SH EXAMPLES +Open a database file: +.PP +.EX +tablepro app.sqlite3 +.EE +.PP +Open a file named like a command: +.PP +.EX +tablepro open ./query +.EE +.PP +List connections and run a report: +.PP +.EX +tablepro connections \-\-format json +tablepro query \-c Staging \-f report.sql \-\-format csv > report.csv +.EE +.PP +Read SQL from standard input under Flatpak: +.PP +.EX +flatpak run app.tablepro.TablePro query \-c Staging < report.sql +.EE +.PP +Inspect and export a table: +.PP +.EX +tablepro describe \-c Staging \-\-schema public \-\-ddl orders +tablepro export \-c Staging \-\-format sql \-\-limit 1000 orders > orders.sql +.EE .SH SEE ALSO -.BR ssh (1), -.BR ssh_config (5), -.BR gsettings (1) +.BR flatpak (1), +.BR gsettings (1), +.BR ssh (1) .PP -https://docs.tablepro.app +https://docs.tablepro.app/linux/command-line diff --git a/linux/docs/decisions/0005-supply-chain-policy.md b/linux/docs/decisions/0005-supply-chain-policy.md index cb3bea9ba7..e8d659d0a5 100644 --- a/linux/docs/decisions/0005-supply-chain-policy.md +++ b/linux/docs/decisions/0005-supply-chain-policy.md @@ -72,3 +72,9 @@ sqlx takes MySQL CA certificates only as PEM bytes and has no hook for a rustls 3. **Entitlement checks around jsonwebtoken** (`entitlement.rs`): jsonwebtoken cannot parse a header whose `alg` it does not know, such as `none`, so `alg`, `typ` and `kid` are read from the header first; and it reads `exp` and `nbf` against the system clock, so `mid`, `nbf` and `exp` are checked against the caller's time after it verified the signature and `aud`. tablepro-cloud's shared vectors cover every check. **Watch list**: oauth2 accepting an http revocation URL on a loopback or test host; jsonwebtoken validating against a caller's time. Each one deletes its piece. + +### Command-line output formats (`tablepro-cli`) + +The `tablepro` command prints rows as csv, tsv, json, jsonl and a padded table, from the JSON values the MCP server sends ([core ADR 0018](../../../core/docs/decisions/0018-command-line.md)). The `csv` crate quotes per writer, not per field, so it cannot write NULL as an empty field and an empty string as `""` in one record, which the format promises on every platform. `tablepro_core::export` renders typed driver values, which the command never holds. `unicode-width` measures the table's columns. + +**Watch list**: a `csv` writer option that quotes an empty field. diff --git a/linux/docs/decisions/0022-ai-assistant-and-mcp-server.md b/linux/docs/decisions/0022-ai-assistant-and-mcp-server.md index 9ed4ca22ff..5414d15dc0 100644 --- a/linux/docs/decisions/0022-ai-assistant-and-mcp-server.md +++ b/linux/docs/decisions/0022-ai-assistant-and-mcp-server.md @@ -1,6 +1,6 @@ # 0022: An AI assistant and a local MCP server -- **Status**: Accepted; the level of connections saved before it existed superseded by 0033 +- **Status**: Accepted; the level of connections saved before it existed superseded by 0033; the token's readers amended by 0045 - **Date**: 2026-10-23 ## Context diff --git a/linux/docs/decisions/0045-command-line-and-connection-urls.md b/linux/docs/decisions/0045-command-line-and-connection-urls.md new file mode 100644 index 0000000000..1e144e1301 --- /dev/null +++ b/linux/docs/decisions/0045-command-line-and-connection-urls.md @@ -0,0 +1,75 @@ +# 0045: The tablepro command line and connection URLs + +- **Status**: Accepted +- **Date**: 2026-11-08 + +## Context + +The executable started only the GTK app. GApplication treated command names as files, +and help needed the app's settings schema and startup work. Shell scripts had no way to +query a saved connection through the running app. + +Connection URLs also reached file handling. A failure could display or log the URL, +including a password. GIO could rewrite a URL before the app received it, dropping the +password or hosts from a MongoDB seed list. The desktop registered no database schemes. + +Flatpak exposes one default command. The shared [command crate](../../../core/docs/decisions/0018-command-line.md) +and the existing [MCP server](0022-ai-assistant-and-mcp-server.md) supply the command surface +and database access without another executable. + +## Decision + +Dispatch arguments in `main` before GUI setup. Pass recognized commands, help and version +to `tablepro-cli` with a Linux host. Pass files, URLs and GUI options to GApplication. +Strip an explicit `open` before that handoff. Reject a target that is neither an existing +file nor a URL with exit status 2. + +The Linux host checks the session bus with `NameHasOwner`, reads the MCP setting and port, +and reads `SecretItem::McpToken` from the app's keyring. It never starts the app or creates +a token. Before sending the token, it checks that the loopback listener belongs to the +same user. This adds a reader of the token described in 0022, without changing its owner +or lifetime. Flatpak runs the command inside the app's sandbox. + +A command may open a saved connection only when standard error is a terminal. The user +answers the app's prompts, and the command waits up to five minutes. A script without that +terminal requires an open connection. All calls pass through the MCP access checks, +write approval and row limits. A database override selects only an already open database. + +Register each supported database URL scheme in the desktop entry and with GIO's VFS. +The VFS callbacks preserve the original URL text. Accept `jdbc:` wrappers from the command +line without registering them as desktop links. + +Route links before file resolution. Match saved connections by driver, host, port, +database and user. Ask before opening a match, with Cancel as the default and close +response. Otherwise fill the connect dialog and wait for Connect. Save only after a +successful connection. Never log or display the full URL; put its password only in the +password field. Local links require the same consent. + +## Consequences + +- Help, version and completion run without a display, driver startup or settings schema. + Database commands need the running app, its settings and its keyring. +- `--help` describes the shell commands. `--help-all` keeps GApplication's translated + options. The shared command help is English. +- A file named like a command needs `open` or a path such as `./query`. With no arguments, + the executable still opens the GUI. +- `--new-window` carries supplied files and URLs to a new window in the running app. +- The .deb and .rpm packages install bash, zsh and fish completions generated by the built + executable. Flatpak installs none; its invocation is `flatpak run app.tablepro.TablePro`. +- Native and Flatpak commands read their own installation's settings and token. There is + no pairing step, token copy in a shell config, or handshake file. +- The command's output and exit statuses are shared with macOS where their behavior + matches. Linux keeps its own authentication, connection opening and MCP limits. + +## Alternatives + +- **Dispatch in `local_command_line`.** That hook runs after the app builds its runtime, + settings and drivers. Help would still depend on GUI startup. +- **Ship another executable.** Packages would need another name, and Flatpak users would + need `--command` to select it. +- **Start the app for database commands.** Activation opens windows and races session + restore. A bare activation also expires after the inactivity timeout. +- **Read URLs through the default VFS handling.** Some schemes lose credentials or hosts + before the app can parse them. +- **Remember trusted links.** That needs a stored grant and controls to remove it. + Keep explicit consent in the saved-connection prompt or connect dialog. diff --git a/linux/docs/decisions/README.md b/linux/docs/decisions/README.md index a54277ebd7..815eb3bfc0 100644 --- a/linux/docs/decisions/README.md +++ b/linux/docs/decisions/README.md @@ -69,7 +69,7 @@ A fact the repository keeps elsewhere, such as a workflow trigger, a branch, a v | [0019](0019-ssh-key-file-identities-only.md) | A chosen SSH key file limits the keys ssh offers | Superseded by 0020 | A key file comes with `IdentitiesOnly=yes`, so the agent's other keys cannot use up `MaxAuthTries` first. | | [0020](0020-pin-only-a-key-file-the-agent-lacks.md) | Pin only a key file the agent does not hold | Accepted | `IdentitiesOnly=yes` only for a key file the agent lacks, so an agent-held certificate or encrypted PEM key still signs. | | [0021](0021-engines-without-sql.md) | Engines without SQL keep the driver contract and say what they hold | Accepted; amended by 0026 | A key-value engine states `DataModel::KeyValue`, lists keys with `scan_keys` and reads them with `read_key`, has its own `SqlGrammar` variant, and is held read-only by the driver's check where the server cannot. | -| [0022](0022-ai-assistant-and-mcp-server.md) | An AI assistant and a local MCP server | Accepted; the level of connections saved before it existed superseded by 0033 | An MCP server on `127.0.0.1`, off until turned on, with a token and a per-connection External Clients level, every client write approved in the app; an AI assistant with no provider until the user adds one and a per-connection AI policy. | +| [0022](0022-ai-assistant-and-mcp-server.md) | An AI assistant and a local MCP server | Accepted; the level of connections saved before it existed superseded by 0033; the token's readers amended by 0045 | An MCP server on `127.0.0.1`, off until turned on, with a token and a per-connection External Clients level, every client write approved in the app; an AI assistant with no provider until the user adds one and a per-connection AI policy. | | [0023](0023-loopback-relay-for-cql.md) | A loopback relay for the CQL client | Accepted; the Linux-only relay amended by core 0008 | Each scylla session dials a 127.0.0.1 relay that serves only this user and carries every connection over `Transport::open`, so CQL reaches a server through an SSH forward. | | [0024](0024-multi-cursor-on-text-marks.md) | Multiple cursors on text marks, in the app | Accepted | The SQL editor's view, a GtkSourceView subclass, keeps extra cursors as GtkTextMark pairs, runs GTK's own commands once per cursor and repeats committed text from `end-user-action`, so one undo takes back every cursor's edit. | | [0025](0025-bundled-duckdb.md) | DuckDB built from the duckdb crate's bundled sources | Accepted; the crate version superseded by 0031 | No PPA or COPR target but Ubuntu 26.10 ships libduckdb, so DuckDB 1.5.6 is compiled from the crate on every channel, with no extension fetched or loaded on its own. | @@ -92,6 +92,7 @@ A fact the repository keeps elsewhere, such as a workflow trigger, a branch, a v | [0042](0042-account-page-lists-this-platforms-features.md) | Preferences > Account lists only the features TablePro for Linux has | Accepted | Features lists only the codes whose platform flag is on and hides when none is; the licensing links move to a License group; the copy speaks of the plan and the license, never of a feature missing on Linux. | | [0043](0043-a-tag-publishes-its-dry-run.md) | A tag publishes what its commit's dry run built | Accepted | A tag's run takes the tarballs, template, source packages, packages and unsigned Flatpak repositories from the green dry run of its commit, re-signs only the Flatpak, checks the rest against the dry run's SHA256SUMS, and fails before publishing when there is no such run. | | [0044](0044-pinned-row-numbers.md) | Row numbers share the grid’s scrolling and input | Accepted | A passive gutter shares the main grid’s selection and vertical adjustment inside one scroller; native input stays in the main view. | +| [0045](0045-command-line-and-connection-urls.md) | The tablepro command line and connection URLs | Accepted | One binary: `main()` runs a command word through `tablepro-cli` before the GUI starts, everything else reaches GApplication; the desktop entry claims the core parser's URL schemes, registered with the VFS so gvfs keeps the password; a link opens a saved connection after an alert or fills the connect dialog; the command line only reads the MCP token. | 0011 is not used. It was reserved for the DDL decision, which was recorded as 0010, and docs cite ADRs by number, so the later ones keep theirs. diff --git a/linux/meson.build b/linux/meson.build index 041ba7eda3..2674ab8fdd 100644 --- a/linux/meson.build +++ b/linux/meson.build @@ -83,6 +83,7 @@ cargo_env.set('TABLEPRO_POSTHOG_KEY', get_option('posthog_key')) subdir('data') subdir('po') subdir('crates/app') +subdir('data/completions') subdir('build-aux') # `meson devenv` runs cargo against the build tree, where the askpass diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 464cf46144..cd9bb4f617 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -15,6 +15,7 @@ crates/app/src/application/mcp_bridge.rs crates/app/src/application/metainfo_release.rs crates/app/src/application/mod.rs crates/app/src/application/open_files.rs +crates/app/src/application/open_links.rs crates/app/src/application/quit_sequence.rs crates/app/src/application/runtime_versions.rs crates/app/src/application/session_signals.rs @@ -26,6 +27,8 @@ crates/app/src/application/third_party_licenses.rs crates/app/src/application/window_host.rs crates/app/src/application/window_launch.rs crates/app/src/application/window_registry.rs +crates/app/src/command_line/linux_host.rs +crates/app/src/command_line/mod.rs crates/app/src/config.rs crates/app/src/crash_report.rs crates/app/src/i18n.rs @@ -67,6 +70,7 @@ crates/app/src/services/change_tracker.rs crates/app/src/services/change_tracker_registry.rs crates/app/src/services/column_relayout.rs crates/app/src/services/connection_deletion.rs +crates/app/src/services/connection_link.rs crates/app/src/services/connection_search.rs crates/app/src/services/deletion_error.rs crates/app/src/services/deletion_report.rs diff --git a/linux/po/README.md b/linux/po/README.md index 162d0aa6cc..de64ccd44c 100644 --- a/linux/po/README.md +++ b/linux/po/README.md @@ -61,7 +61,7 @@ Start from the template CI builds, so you do not need gettext 1.0: or newer, as in CI's Translations job, that fails an entry whose translation drops or misspells a `{name}` placeholder; - the .deb and .rpm install it, and their smoke test starts - `tablepro --help` under it and looks for its translation of + `tablepro --help-all` under it and looks for its translation of "Open a new window". 4. Compile and install it. The package build does this on its own. To