Skip to contents

Starts an MCP server inside this R session that offers r, read, edit and write to agents such as Codex, Claude Code, Claude Desktop or Cursor. It listens only on 127.0.0.1, requires a 192-bit bearer token, rejects foreign browser origins, and runs every call through your permission settings; R code evaluates in envir, where its objects persist. The calls belong to a dedicated gptr session whose permission mode is the one in effect when the server starts (stop and start the server to change it); like any new session, it becomes gptr_last(). Requests are answered while R waits at the console or inside a gptr call; one that needs your approval is refused with a note on how to allow it, because gptr never asks from a background callback. The token appears only in the snippets of $config (printed redacted; as.character() gives the text to paste; $config$codex holds the codex options in $args and the environment to start Codex with in $env) and in the environment of children gptr starts. One server runs per R process. Needs 'httpuv', 'later' and 'openssl'.

Usage

gptr_mcp_serve(
  tools = c("r", "read", "edit", "write"),
  envir = parent.frame(),
  port = NULL,
  stop = FALSE
)

Arguments

tools

The tools to serve: a subset of c("r", "read", "edit", "write").

envir

Where served R code evaluates and its objects persist.

port

A port, or NULL for a free one.

stop

TRUE to stop the server.

Value

A gptr_mcp_handle with $url, $port, $token_env, $config and $stop(); calling again returns the running handle; with stop = TRUE, invisible(NULL).

Details

If shutdown cannot be confirmed, access is revoked and the server is retained for retry. Call gptr_mcp_serve(stop = TRUE) again; new requests are refused until shutdown completes.

Examples

if (interactive() && rlang::is_installed(c("httpuv", "later", "openssl"))) {
  h = gptr_mcp_serve(envir = new.env())
  h$config$codex
  gptr_mcp_serve(stop = TRUE)
}