Skip to content

Z0shua/omcp_r

 
 

Repository files navigation

OMCP R Sandbox Server

A secure, MCP-compliant R code execution environment specialized for OMOP CDM and DARWIN EU® analytics. This server implements the Model Context Protocol (MCP) for safe, isolated, and stateful R session management.

🌟 Key Features

  • OMOP/DARWIN Specialized: Pre-configured with OHDSI HADES and DARWIN R packages (CDMConnector, DatabaseConnector, SqlRender, etc.) and Java 17.
  • Persistent R Sessions: Maintain state across multiple code executions using Docker-based Rserve containers.
  • Robust Output Capturing: Captures stdout and stderr (e.g., print(), summary(), and OHDSI logs) and returns them alongside execution results.
  • Persistent Workspaces: Support for host bind-mounts to preserve files across session lifecycles.
  • File Management: Built-in tools to upload cohort definitions, list sandbox files, and retrieve analysis results.
  • Enterprise-Grade Security:
    • Docker isolation with non-root user (UID 1000).
    • Read-only root filesystem with limited writable tmpfs.
    • Dropped Linux capabilities and no privilege escalation.
    • Network isolation (configurable) and resource limits (CPU/Memory).
  • Standardized Connectivity: Ready for PostgreSQL/OMOP CDM databases with auto-proxying for localhost connections to host.docker.internal.

🛠️ MCP Tools

The server exposes the following tools to any MCP client:

Tool Description
create_session Start a new persistent R session (Docker container).
execute_in_session Run R code in a session. State persists and output is captured.
list_sessions List all active R sessions and their metadata.
close_session Safely stop and remove an R session.
list_session_files List files in the session's workspace.
read_session_file Read the content of a file (e.g., analysis results).
write_session_file Upload a file to the sandbox (e.g., JSON cohort).
install_package Install R packages dynamically from CRAN or GitHub.

🚀 Quick Start

Prerequisites

  • Docker and Docker Compose
  • Python 3.10+
  • uv (recommended for Python dependency management)

1. Installation

git clone https://github.com/Z0shua/omcp_r.git
cd omcp_r
uv pip install -e .

2. Environment Setup

Copy sample.env to .env and configure your database and Docker settings:

DOCKER_IMAGE=omcp-r-sandbox:latest
SANDBOX_TIMEOUT=300
DB_HOST=your-postgres-host
DB_NAME=cdm_database
# ... see sample.env for all options

3. Build & Run

Use Docker Compose to build the specialized R sandbox image and start the MCP server:

docker-compose up --build

📖 Documentation

For detailed guides, please refer to the docs/ directory:

⚖️ License

MIT

About

R sandbox. An MCP server that allows agents to execute R code in a secure, sandboxed environment.

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 95.4%
  • Dockerfile 4.6%