import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { ProfileProvisionDeps, CreatedAuthProfile } from '@agentproto/auth'; /** * Read-only DISCOVERY SCANNER — `auth_discover_credentials`. * * Probes the well-known local locations where the CLIs and gateways this host * knows about write their credentials (Claude Code's OAuth item, Codex/Gemini * login files, `~/.hermes/config.yaml`, and the provider API-key env vars) and * reports what it FINDS — so onboarding can offer to import a credential the * user already has, instead of making them paste it again. * * It is a scanner, not a resolver. Two invariants make it money-safe, and the * suite in `__tests__/credential-discovery.test.ts` is written against both: * * 1. NEVER return (or log) a discovered secret's VALUE. Every probe answers a * BOOLEAN "is a non-empty credential present here?" via {@link jsonFieldPresent} * / a presence check — the value is never bound into a result, only a * non-secret `hint` locator ("OPENROUTER_API_KEY in ~/.hermes/config.yaml"). * This mirrors `verifyLocalLoginPresent`'s discard-the-value discipline in * `claude-code-oauth-source.ts`, taken one step further: we never hold the * plaintext at all. * * 2. NEVER throw on a malformed/unreadable source. A single corrupt file must * not sink the whole scan — each probe is wrapped so a parse error / bad * permission becomes a per-source `warn`+skip (same "malformed → empty, not * fatal" spirit as `loadProviders`). A MISSING file is normal "not found", * not even a warning. * * The source recipes (Keychain service name, file paths, jsonPaths) mirror the * builtin provision recipes in * `@agentproto/secrets/provision/recipe` (`claudeCodeOauthRecipe`, * `codexRecipe`, `geminiRecipe`) so discovery and provisioning agree on where a * credential lives. * * PHASE 1 (this file) is pure discovery: it reports EVERYTHING it finds, with * provenance. Cross-referencing against already-imported profiles (the * "found-but-not-*imported*" filter, keyed on a future AuthProfile `origin` * field) is Phase 2 and is deliberately not done here. */ /** Where a discovered credential came from. Stable, non-secret provenance. */ type CredentialOrigin = "claude-code" | "hermes-config" | "env" | "codex" | "gemini"; /** One found-but-not-imported credential. Carries provenance and a non-secret * locator only — NEVER the credential value. */ interface DiscoveredCredential { /** Billing endpoint / vendor the credential is for (anthropic, openai, …). */ endpoint: string; /** Auth method the credential installs as. */ method: "oauth-bearer" | "api-key"; /** Where it was found. */ origin: CredentialOrigin; /** Non-secret human locator — e.g. "OPENROUTER_API_KEY in ~/.hermes/config.yaml". * Says WHERE the secret is, never WHAT it is. */ hint: string; } /** * Injectable I/O so the scanner is unit-testable without touching the real * home dir / Keychain. Defaults reach for the real host. */ interface CredentialDiscoveryDeps { /** Home directory (`~` expands to this). Default: `os.homedir()`. */ homeDir?: string; /** Process env to probe. Default: `process.env`. */ env?: Record; /** Read a file's text. MUST throw a Node-style error with `.code === "ENOENT"` * for a missing file (the default `readFileSync` does). Default: real fs. */ readFile?: (path: string) => string; /** Look up a macOS Keychain generic-password by service, returning its raw * value; MUST throw when the item is absent. Default: `security find-generic-password` * on darwin, always-throws elsewhere. */ keychainLookup?: (service: string) => string; /** Platform gate for the Keychain probe. Default: `process.platform`. */ platform?: NodeJS.Platform; /** Non-secret warning sink for a malformed/unreadable source. Default: no-op. */ warn?: (message: string) => void; } /** * Is there a non-empty STRING at `path` in this JSON text? Returns a boolean — * it deliberately NEVER returns the value, so a discovered secret can't leak * through it. Throws only if the text isn't parseable JSON (a malformed * source), which the caller turns into a warn+skip. */ declare function jsonFieldPresent(raw: string, path: string): boolean; /** * Extract a `KEY: ` value from `~/.hermes/config.yaml` text — a minimal, * dependency-free reader (no YAML parser pulled in): the first top-levelish * `key:` line, quotes stripped. Returns `undefined` when the key is absent or * its value is blank/empty-quotes. * * Used TWO ways: the discovery scanner only asks {@link yamlKeyPresent} * (boolean, never the value); the import path calls this directly to COPY the * value into the keychain. The value it returns is a SECRET — callers must * pass it straight to the credential store and never echo it. */ declare function readYamlKeyValue(raw: string, key: string): string | undefined; /** * Does `~/.hermes/config.yaml` carry a non-empty value for `key`? Presence * only — returns a boolean, NEVER the value (the discovery invariant). */ declare function yamlKeyPresent(raw: string, key: string): boolean; /** * Scan the known local credential locations and report what's present, with * provenance and a non-secret locator. Read-only, and safe against a corrupt * or unreadable source (per-source warn+skip). NEVER returns a secret value. */ declare function discoverCredentials(deps?: CredentialDiscoveryDeps): DiscoveredCredential[]; /** Raised when an import can't proceed (unknown origin/endpoint, or nothing to * import). Distinct type so the MCP surface can map it to a clean error. */ declare class CredentialImportError extends Error { constructor(message: string); } /** How a chosen `{ origin, endpoint }` becomes a profile. Discriminated: a * `source`-backed oauth-bearer, or an api-key COPIED from a located value. */ type ImportMaterialization = { method: "oauth-bearer"; endpoint: string; source: string; } | { method: "api-key"; endpoint: string; copy: { via: "env" | "hermes"; key: string; }; }; /** * Decide how to materialize a profile for a discovered `{ origin, endpoint }`. * PURE — no I/O, no secret. Throws {@link CredentialImportError} for an unknown * origin, or an endpoint that origin doesn't serve. The method is fixed by * origin here, which is what forbids a bearer↔api-key mix. */ declare function planCredentialImport(origin: string, endpoint: string): ImportMaterialization; interface ImportCredentialArgs { /** Discovery origin to import from. */ origin: string; /** Billing endpoint to import (must be one the origin serves). */ endpoint: string; /** Optional explicit profile id; defaults to `-`. */ id?: string; /** Optional human label. */ label?: string; } /** * Import a discovered credential into a named auth profile end-to-end. * * Guards against fabricating a profile: it re-runs discovery and refuses unless * the requested `{ origin, endpoint }` is ACTUALLY present right now — discovery * having once located a value is not license to import it. Then materializes via * the single validated create path ({@link createAuthProfile}), stamping * `origin` so the UI can badge it. Returns the non-secret {@link * CreatedAuthProfile} (fingerprint, never the credential). * * `provisionDeps` is the profile/credential storage (a real keychain in prod, a * `MemoryStore` in tests); `ioDeps` is the discovery I/O (env / files / * keychain-read), injectable for the same reason. */ declare function importDiscoveredCredential(args: ImportCredentialArgs, provisionDeps: ProfileProvisionDeps, ioDeps?: CredentialDiscoveryDeps): Promise; declare function registerCredentialDiscoveryTools(server: McpServer): void; export { type CredentialDiscoveryDeps, CredentialImportError, type CredentialOrigin, type DiscoveredCredential, type ImportCredentialArgs, type ImportMaterialization, discoverCredentials, importDiscoveredCredential, jsonFieldPresent, planCredentialImport, readYamlKeyValue, registerCredentialDiscoveryTools, yamlKeyPresent };