Skip to contents

Registers the current R session so that Claude can connect to it, and optionally lets Claude run R code in it. Then ask Claude to "connect to my R session".

Usage

natmcp_session(run_r = FALSE, quiet = FALSE)

Arguments

run_r

If TRUE, allow Claude to run R code in this session. Needs the evaluate package.

quiet

If TRUE, suppress the instructions printed on success.

Value

Invisibly, TRUE.

Details

This wraps mcptools::mcp_session(): the session listens in the background, leaving the console free. Claude lists registered sessions with the list_r_sessions tool and connects with select_r_session; tool calls then run in this session.

The server always offers a natmcp_run_r tool (see natmcp_mcp_server()), but it only runs code in a session where run_r = TRUE was set here; any other session, or the server's own background process, refuses. Code runs in the global environment via btw::btw_tool_run_r(), so objects Claude creates appear in your workspace. Your Claude app asks you to approve each tool call unless you have told it to always allow the tool. Call again with run_r = FALSE to revoke.

On macOS, sessions and the server meet in a socket directory that mcptools derives from TMPDIR, which can differ between apps. natmcp pins it (via the MCPTOOLS_SOCKET_DIR environment variable, unless you have set it) to your per-user temp directory in both, and warns if that was not possible.

To register every interactive session automatically, add natmcp::natmcp_session(quiet = TRUE) to your ~/.Rprofile (or let natmcp_setup() do it).

Examples

if (FALSE) { # \dontrun{
# Claude can use this session for look-ups
natmcp_session()
# ... and can also run R code here
natmcp_session(run_r = TRUE)
} # }