Connect an agent to a sandboxed MCP Console session
Source:R/mcp-console.R
mcp_console_connection.RdStarts an MCP Console server
for one agent and connects to it. MCP Console runs R, Python and DuckDB SQL
in an OS sandbox and keeps their state for the conversation, with optional
named sessions and recording notes. Each connection starts its own
server. Register connection$tools() on the agent so that the send tool
goes through its permissions, hooks and limits.
Usage
mcp_console_connection(
agent,
command = Sys.getenv("DEPUTY_MCP_CONSOLE_BIN"),
args = character(),
env = NULL,
dependencies = c("deny", "allow"),
project_config = FALSE,
timeout = 60,
startup_timeout = 60
)Arguments
- agent
The agent that owns the connection. Its working directory becomes the Console workspace, where the server starts, the sandbox's workspace is rooted, and MCP Console checks for project configuration.
- command
Path to an
mcp-consoleexecutable, version 0.4.0. Defaults to theDEPUTY_MCP_CONSOLE_BINenvironment variable. Deputy doesn't install the executable or search for it.- args
Extra arguments for
mcp-console serve. Only--writable-root PATHand sandbox-preserving-coverrides (sandbox.profile=workspace,sandbox.profile=read_only,sandbox.network=restricted/false,r.resolution=disabled/fixed/explicit,python.resolution=fixed/explicit,tool.parameters.requirements.enabled=false, and positive integersessions.max) are accepted. Anything else, including--no-sandboxandtool.description, is an error.- env
Optional named character vector of environment variables for the server. The server inherits only a few variables (such as
HOME,PATHand theR_LIBSfamily), so set others here, for exampleUV_CACHE_DIR,XDG_CACHE_HOME, orMCP_CONSOLE_HOME. Without them, dependency installs and fallback recordings write underHOME.- dependencies
"deny"(the default) or"allow". MCP Console can prepare packages that code needs using its separate resolver sandbox (which downloads packages from registries and writes caches). With"deny", Deputy enforces non-preparing settings on launch (r.resolution=disabled,r.packages=[],languages=[r], andtool.parameters.requirements.enabled=false), verifies thatrequirements,python, andsqlare not exposed, and refuses anyrequirementscall. Pass"allow"to use the full R, Python and SQL workbench with managed dependency preparation. See Details.- project_config
Set to
TRUEto start even though the workspace contains.agents/console/config.yaml. That file can widen the sandbox, so by default the connection refuses to start when it exists and passes--no-project-configtomcp-console serve. Review the file before you opt in.- timeout
Maximum seconds for one MCP request.
- startup_timeout
Maximum seconds for the client and server to start.
Value
A McpConnection. Call its $close() method when the conversation
ends.
Details
Supported versions
Requires MCP Console 0.4.0 and mcptools 1.0.2 or 1.0.3. The connection
checks the version with command --version before starting the server.
After starting, it checks the arguments of send and the sandbox
description MCP Console adds to it, and closes the connection unless the
server uses its native sandbox with restricted networking (read_only or
workspace profile with private temporary storage).
Permissions
send runs code, so it is treated like a shell tool: in "standard" mode
the agent's permissions need bash = TRUE. The server declares no tool
annotations, so the cautious defaults also require web = TRUE.
"readonly" and "plan" modes always deny send. A call that declares
requirements also needs install_packages = TRUE and a connection created
with dependencies = "allow".
By default in MCP Console 0.4.0, requirements is exposed, R resolves
missing packages automatically on library() / ::, and managed Python and
DuckDB prepare runtimes and extensions in Console's separate resolver
sandbox. With dependencies = "deny" (the default), Deputy passes trailing
-c overrides (r.resolution=disabled, r.packages=[], languages=[r],
tool.parameters.requirements.enabled=false) so neither automatic
resolution nor managed Python/SQL preparation can run, and closes the
connection if requirements, python, or sql remain exposed. Pass
dependencies = "allow" to enable the full R, Python and SQL workbench;
automatic resolution then remains part of the host's connection-level
decision and cannot be checked call by call.
Long-running calls
mcptools waits about 4 seconds for a reply, so each send waits at most
2.5 seconds: timeout_ms is capped at 2500, also when omitted. Longer work
keeps running: the call returns [running; poll with an empty send], and a
later send with no code collects the output. The tool description tells
the model this. Installs requested with requirements, restarts, and input
sent to a stopped worker have no time limit in MCP Console. If one takes
longer than the reply window, the connection closes with a
deputy_mcp_desynchronized error and the session is lost. That is why only
you can restart a session, with mcp_console_control(); the model's send
refuses it.
Closing and recordings
$close() asks MCP Console to shut down cleanly, then stops the process.
$cancel(), interrupting the agent during a call, and a lost reply stop the
server without asking; MCP Console's sandbox runner then stops its worker.
Processes that escape the runner are outside Deputy's control.
MCP Console records every call, note, result and plot, without redaction,
under .agents/console/sessions/<run-id>/ when .agents/console already
exists in the workspace, or otherwise under $MCP_CONSOLE_HOME/sessions/
(~/.agents/console/sessions/ by default). The directory is reported in
$status()$execution$recordings; deleting old recordings is up to you.
Examples
if (FALSE) { # \dontrun{
agent <- Agent$new(
chat = ellmer::chat("openai/gpt-6.2-sol"),
permissions = Permissions(bash = TRUE, web = TRUE)
)
console <- mcp_console_connection(
agent,
command = "/path/to/mcp-console",
dependencies = "allow"
)
agent$register_tools(console$tools())
# In a Shiny app: session$onSessionEnded(function() console$close())
console$close()
} # }