Skip to contents

Stores a credential for a model provider (for example "openrouter") or for an MCP server ("mcp:<server>"). With method = "auto", providers and MCP servers that support OAuth get the authorization-code flow with PKCE (S256): a page on 127.0.0.1 receives the redirect when 'httpuv' and 'later' are installed; otherwise you paste the address your browser ends on. A login inside a tool also uses hidden paste entry because loopback callbacks cannot run recursively. Key entry requires a UI with hidden input; it never falls back to visible typing. Hidden entry works in RStudio and Positron (through 'rstudioapi') and in terminal R on Unix; where the UI has none (Windows Rgui or Rterm, R.app, Jupyter, Emacs ESS), load keys with gptr_env() instead, or log in once elsewhere with a remembering scope. The host prompt shows the destination and storage before entry. For key entry, leave it blank (in a terminal, Ctrl-D counts as blank) or type /skip to skip; type /cancel, dismiss or interrupt (Ctrl-C) it to cancel. The initial OAuth review offers Skip and Cancel. Secret answers are not returned to the model. scope = "session" keeps credentials in this R process until logout or exit; it does not export an environment variable or create a credential file. For assisted setup, use this scope. The default scope = "user" remembers credentials in auth.json (mode 0600 on Unix; on Windows, protected by your user profile's access control list), or its existing keyring reference. scope = "keyring" explicitly uses the native OS credential backend through the optional 'keyring' package, and writes only a reference to auth.json. File and environment keyring backends are refused for this scope. Hidden input is not encryption. Provider credentials are bound to the configured endpoint; MCP credentials are bound to the reviewed server. Changed destinations require login again. An MCP tool that needs sign-in fails with an error naming this function; it never opens a browser by itself.

Usage

gptr_login(
  provider,
  method = c("auto", "oauth", "key"),
  scope = c("user", "session", "keyring"),
  slot = NULL
)

gptr_logout(provider)

Arguments

provider

A provider id such as "openrouter", or "mcp:<server>" for an MCP server.

method

"auto" (default), "oauth" (the browser flow only) or "key" (key entry).

scope

"user" (default) to remember, "session" for this R process only, or "keyring" to remember through a native OS credential backend.

slot

For a stdio MCP server, the name of a declared secret environment entry such as "SERVICE_API_TOKEN". The credential is passed only to that server's child environment, bound to its configuration as written and its resolved executable (an edit of either or an update of the executable asks for login again; changing R's working directory or an environment variable does not). Leave NULL for providers and HTTP MCP servers.

Value

gptr_login() returns TRUE invisibly on success, or FALSE invisibly with a status attribute of "skipped" or "cancelled", and says which in a message that names the store used; no credential value is returned or shown. gptr_logout() removes the stored credential and forgets the access token held in memory, and returns TRUE invisibly when it removed something, else FALSE.

Examples

if (interactive()) {
  gptr_login("openrouter")
  gptr_logout("openrouter")
}