Starts an MCP server inside this R session that offers inspect, r, read, edit and
write to agents such as Codex, Claude Code, Claude Desktop, Cursor or Biorouter. 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. inspect runs R code only when gptr classifies it as read-only (risk level 0, see
gptr_risk()) and refuses anything else without running it, so a client may run it without
asking you; r runs whatever your permission settings allow. The calls belong to a dedicated
gptr session whose permission mode is mode, or the configured default when the server
starts, independently of a chat's explicit mode; like any new session, it becomes
gptr_last(). A running server keeps its original workspace and mode until stopped.
An r or inspect call that ran is recorded on that session (of a call that stopped part-way,
the expressions that ran; nothing in plan mode), so it persists with the session and
gptr_last() |> peter() continues after it. While gptr_doc() binds a document, the call is
also written there as a direct R line (# direct R (no model)), which gptr_export() keeps as
runnable R; an inspect call that changed no object is inspection and is left out.
Requests are answered at R's top-level idle prompt or during an outermost gptr wait. Serving
pauses while Peter's own prompt waits (in every console), while other R code runs and during a
nested tool wait. Where R has no idle prompt (Rscript, knitr, Quarto, or a Jupyter kernel
between cells), a request waits for the next gptr wait, such as peter$sh() or
gptr_wait(), and a client may time out (a server started there says so); serve from an
interactive R console instead.
In Peter, /mcp serve starts or reuses the server and returns to R's prompt; a new server
uses that chat's workspace and mode. Resume the chat with gptr_last() |> peter(). Calls that
need approval are refused with a note on what a person can do, because gptr never asks from a
background callback. In plan mode, r calls that only read run in a scratch environment above
the workspace (what they create is not kept) and every call that changes something is
refused. The token appears only in the snippets of $config, which show it
redacted, in notebooks and documents too (as.character() gives the text to paste;
$config$codex holds the codex options in $args and the environment to start Codex with
in $env). One server runs per R process. Needs 'httpuv', 'later' and 'openssl'; gptr's own
codex route does not use this server.
Usage
gptr_mcp_serve(
tools = c("inspect", "r", "read", "edit", "write"),
envir = parent.frame(),
port = NULL,
stop = FALSE,
mode = NULL,
advertise = FALSE
)Arguments
- tools
The tools to serve: a subset of
c("inspect", "r", "read", "edit", "write").- envir
Where served R code evaluates and its objects persist.
- port
A port, or
NULLfor a free one.- stop
TRUEto stop the server.- mode
The permission mode of the server's session:
"plan","manual","edits"or"auto";NULLfor the configured default,"auto"unless you set another, in which a client can create and change objects ("manual"lets it only read). A running server keeps its mode: another one is an error until the server is stopped.- advertise
TRUEto write the discovery record for local clients, or a label (a string) to show them;FALSEwrites none. Advertising a running server writes its record.
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
With advertise = TRUE, or a label, the server also writes a discovery record so local clients
(Biorouter's gptr Agent, for example) find this session without your copying its address and
token: serve-<pid>.json in file.path(tools::R_user_dir("gptr", "cache"), "mcp-serve"). It
holds the address, the token, R's process id, the working folder, the label (by default the
folder's name), the mode, the served tools and the R, gptr and front-end versions. On Unix it
is readable only by you (mode 0600 in a folder of mode 0700); on Windows your user profile's
permissions protect it. gptr removes it when the server stops, when gptr is unloaded and when
R exits, and removes the records of R processes that are gone when it next loads. Any program
running as you can read the record, as it can your other credential files: its path always
ends in R/gptr/mcp-serve/serve-<pid>.json, which a tool that keeps credential files from
agents can match, and gptr's permission gate treats reading it as reading a secret.
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)
# let a local client such as Biorouter find the session, and let it change objects
gptr_mcp_serve(envir = globalenv(), mode = "auto", advertise = TRUE)
gptr_mcp_serve(stop = TRUE)
}