Skip to contents

What securer does

securer runs R code in a separate R process that the operating system keeps in a sandbox. That code can call “tools”, which are functions you define. The tools run in your own R session, outside the sandbox, and the two processes pass messages over a Unix domain socket.

It’s built for LLM agents. The model writes R code, and the code can use the tools you give it, but it can’t reach the network or your files on its own.

Installation

pak::pak("ian-flores/securer")

Verify the installation:

library(securer)
execute_r("1 + 1")
#> [1] 2

Hello world

The simplest way to run code is execute_r(). It starts a session, runs the code, and shuts the session down again:

library(securer)

execute_r("1 + 1")
#> [1] 2

execute_r("paste('Hello from', R.version.string)")
#> [1] "Hello from R version 4.4.2 (2024-10-31)"

execute_r() uses the sandbox by default (sandbox = TRUE). Pass sandbox = FALSE to turn it off.

Defining tools

A tool is a function that sandboxed code can call but that runs in your session. Make one with securer_tool():

add_tool <- securer_tool(
  name = "add",
  description = "Add two numbers",
  fn = function(a, b) a + b,
  args = list(a = "numeric", b = "numeric")
)
add_tool

A tool has a name, which is what the sandboxed code calls it, and a description, which you can show to an LLM so it knows what the tool is for. fn is the function that actually runs, in your session. args lists the argument names and, optionally, the type each one must have.

Argument types

Type string Check function
"numeric" is.numeric()
"character" is.character()
"logical" is.logical()
"integer" is.integer()
"list" is.list()
"data.frame" is.data.frame()

If you leave out an argument’s type, securer doesn’t check it.

Using tools

Pass a list of tools to execute_r():

tools <- list(
  securer_tool("add", "Add two numbers",
    fn = function(a, b) a + b,
    args = list(a = "numeric", b = "numeric")),
  securer_tool("get_weather", "Get weather for a city",
    fn = function(city) list(temp = 72, condition = "sunny"),
    args = list(city = "character"))
)

execute_r('add(2, 3)', tools = tools)
#> [1] 5

execute_r('get_weather("Boston")', tools = tools)
#> $temp
#> [1] 72
#>
#> $condition
#> [1] "sunny"

Next steps