Skip to content

Container server fixes #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/sandbox-container/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Container MCP Server

This is a simple MCP-based interface for a sandboxed development environment.

## Local dev

Cloudchamber local dev isn't implemented yet, so we are doing a bit of a hack to just run the server in your local environment. Because of this, testing the container(s) and container manager locally is not possible at this time.

Do the following from within the sandbox-container app:

1. Copy the `.dev.vars.example` file to a new `.dev.vars` file.
2. Get the Cloudflare client id and secret from a team member and add them to the `.dev.vars` file.
3. Run `pnpm i` then `pnpm dev` to start the MCP server.
4. Run `pnpx @modelcontextprotocol/inspector` to start the MCP inspector client.
5. Open the inspector client in your browser and connect to the server via `http://localhost:8976/sse`.

Note: Temporary files created through files tool calls are stored in the workdir folder of this app.

## Deploying

1. Make sure the docker daemon is running

2. Disable WARP and run

```
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/14387504770/npm-package-wrangler-8740 deploy
```

3. Add to your Claude config. If using with Claude, you'll need to disable WARP:

```
{
"mcpServers": {
"container": {
"command": "npx",
"args": [
"mcp-remote",
// this is my deployed instance
"https://container-starter-2.cmsparks.workers.dev/sse"
]
}
}
}
```
67 changes: 36 additions & 31 deletions apps/sandbox-container/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
###
# STAGE: BASE
###
FROM node:22 AS base

# Set non-interactive mode to avoid prompts during package installation
ARG DEBIAN_FRONTEND=noninteractive

# Setup pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

# Use bash for the shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Update and install useful CLI utilities
RUN apt-get update && apt-get install -y curl ca-certificates \
# Use Alpine as base for minimal size
FROM alpine:3.19 as base

# Install necessary packages while minimizing layers
# We combine commands with && and clean cache in the same layer
# to reduce the image size
RUN apk update && \
apk add --no-cache \
# Core utilities
git \
htop \
vim \
curl \
wget \
net-tools \
build-essential \
nmap \
sudo \
lsb-release \
# Build essentials
build-base \
# Python and pip
python3 \
python3-pip \
python3-matplotlib \
python3-numpy \
python3-pandas \
&& apt-get clean
py3-pip \
# Node and npm
nodejs \
npm && \
# Clean up the cache to reduce image size
rm -rf /var/cache/apk/* && \
# Create symlink for python
ln -sf /usr/bin/python3 /usr/bin/python

# Install pnpm in a separate layer for better caching
RUN npm install -g pnpm && \
rm -rf /root/.npm

# Set up pnpm environment
ENV PNPM_HOME=/usr/local/bin
ENV PATH=$PNPM_HOME:$PATH

# Set working directory
WORKDIR /app

# Set environment variables
ENV PATH="/app/node_modules/.bin:${PATH}"

###
# STAGE: PRUNE - Generate a partial monorepo for the sandbox-container app. The output will be placed into a directory named "out"
Expand All @@ -48,7 +53,7 @@ FROM base AS installer
WORKDIR /app

COPY --from=prune /app/out/ .
RUN pnpm install --frozen-lockfile
RUN pnpm install --frozen-lockfile --only=production

WORKDIR /app/apps/sandbox-container

Expand Down
83 changes: 32 additions & 51 deletions apps/sandbox-container/README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,49 @@
# Container MCP Server
# Cloudflare Container Sandbox MCP Server

This is a simple MCP-based interface for a sandboxed development environment.
This is a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) server that supports remote MCP connections, with Cloudflare OAuth built-in.

## Local dev
It integrates tools for running a sandbox container with your MCP client. With this server you can allow your LLM to run arbitrary code, such as Node or Python, in a secure, sandboxed environment.

Cloudchamber local dev isn't implemented yet, so we are doing a bit of a hack to just run the server in your local environment. Because of this, testing the container(s) and container manager locally is not possible at this time.
## Tools

Do the following from within the sandbox-container app:
| **Category** | **Tool** | **Description** |
| ----------------- | -------------------------- | ----------------------------------------------------------------------------- |
| **Container Lifecycle** | `container_initialize` | (Re)start a container. Containers are intended to be ephemeral and don't save any state. Containers are only guaranteed to last ~10m.|
| | `container_ping` | Ping a container for connectivity |
| **Filesystem** | `container_file_write` | Write to a file |
| | `container_files_list` | List all files in the work directory |
| | `container_file_read` | Read the contents of a single file or directory |
| | `container_file_delete` | Delete a single file or directory |
| **Execution** | `container_exec` | Run a command in the shell |

1. Copy the `.dev.vars.example` file to a new `.dev.vars` file.
2. Get the Cloudflare client id and secret from a team member and add them to the `.dev.vars` file.
3. Run `pnpm i` then `pnpm dev` to start the MCP server.
4. Run `pnpx @modelcontextprotocol/inspector` to start the MCP inspector client.
5. Open the inspector client in your browser and connect to the server via `http://localhost:8976/sse`.
This MCP server is still a work in progress, and we plan to add more tools in the future.

Note: Temporary files created through files tool calls are stored in the workdir folder of this app.

## Deploying
### Prompt Examples

1. Make sure the docker daemon is running
- `Create a visualization using matplotlib. Run it in the container that you can start`
- `Clone and explore this github repo: [repo link]. Setup and run the tests in your development environment`
- `Analyze this data using Python`

2. Disable WARP and run
## Access the remote MCP server from from any MCP Client

```
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/14387504770/npm-package-wrangler-8740 deploy
```
If your MCP client has first class support for remote MCP servers, the client will provide a way to accept the server URL (`https://bindings.mcp.cloudflare.com`) directly within its interface (for example in [Cloudflare AI Playground](https://playground.ai.cloudflare.com/)).

3. Add to your Claude config. If using with Claude, you'll need to disable WARP:
If your client does not yet support remote MCP servers, you will need to set up its respective configuration file using [mcp-remote](https://www.npmjs.com/package/mcp-remote) to specify which servers your client can access.

```
Replace the content with the following configuration:

```json
{
"mcpServers": {
"container": {
"command": "npx",
"args": [
"mcp-remote",
// this is my deployed instance
"https://container-starter-2.cmsparks.workers.dev/sse"
]
}
}
"mcpServers": {
"cloudflare": {
"command": "npx",
"args": ["mcp-remote", "https://containers.mcp.cloudflare.com/sse"]
}
}
}
```

## Tools

- `container_initialize`: (Re)start a container. Containers are intended to be ephemeral and don't save any state. Containers are only guaranteed to last 10m (this is just because I have a max of like ~5 containers per account).
- `container_ping`: Ping a container for connectivity
- `container_exec`: Run a command in the shell
- `container_file_write`: Write to a file
- `container_files_list`: List all files in the work directory
- `container_file_read`: Read the contents of a single file or directory
- `container_file_delete`: Delete a single file or directory

## Resources

TODO

Tried implementing these, but MCP clients don't support resources well at all.

## Prompts

TODO

## Container support
Once you've set up your configuration file, restart MCP client and a browser window will open showing your OAuth login page. Proceed through the authentication flow to grant the client access to your MCP server. After you grant access, the tools will become available for you to use.

The container currently runs python and node. It's connected to the internet and LLMs can install whatever packages.
Interested in contributing, and running this server locally? See [CONTRIBUTING.md](CONTRIBUTING.md) to get started.
8 changes: 4 additions & 4 deletions apps/sandbox-container/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ app.post('/exec', zValidator('json', ExecParams), (c) => {
const execParams = c.req.valid('json')
const proc = exec(execParams.args)
return streamText(c, async (stream) => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve, reject) => {
if (proc.stdout) {
// Stream data from stdout
proc.stdout.on('data', async (data) => {
await stream.write(data.toString())
})
} else {
await stream.write('WARNING: no stdout stream for process')
void stream.write('WARNING: no stdout stream for process')
}

if (execParams.streamStderr) {
Expand All @@ -155,15 +155,15 @@ app.post('/exec', zValidator('json', ExecParams), (c) => {
await stream.write(data.toString())
})
} else {
await stream.write('WARNING: no stderr stream for process')
void stream.write('WARNING: no stderr stream for process')
}
}

// Handle process exit
proc.on('exit', async (code) => {
await stream.write(`Process exited with code: ${code}`)
if (code === 0) {
stream.close()
await stream.close()
resolve()
} else {
console.error(`Process exited with code ${code}`)
Expand Down
3 changes: 1 addition & 2 deletions apps/sandbox-container/evals/exec.eval.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert, expect } from 'vitest'
import { expect } from 'vitest'
import { describeEval } from 'vitest-evals'
import { z } from 'zod'

import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'
Expand Down
9 changes: 6 additions & 3 deletions apps/sandbox-container/evals/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { jsonSchemaToZod } from '@n8n/json-schema-to-zod'
import { MCPClientManager } from 'agents/mcp/client'
import { LanguageModelV1, streamText, StreamTextResult, tool, ToolCallPart, ToolSet } from 'ai'
import { streamText, tool } from 'ai'
import { z } from 'zod'

import type { JsonSchemaObject } from '@n8n/json-schema-to-zod'
import type { LanguageModelV1, StreamTextResult, ToolCallPart, ToolSet } from 'ai'

export async function initializeClient(): Promise<MCPClientManager> {
const clientManager = new MCPClientManager('test-client', '0.0.0')
Expand Down Expand Up @@ -56,12 +57,14 @@ export async function runTask(
maxSteps: 10,
})

for await (const part of res.fullStream) {
// consume the stream
// eslint-disable-next-line no-empty
for await (const _ of res.fullStream) {
}

// convert into an LLM readable result so our factuality checker can validate tool calls
let messagesWithTools = ''
let toolCalls: ToolCallPart[] = []
const toolCalls: ToolCallPart[] = []
const messages = (await res.response).messages
for (const message of messages) {
console.log(message.content)
Expand Down
7 changes: 4 additions & 3 deletions apps/sandbox-container/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "containers-starter",
"version": "0.0.0",
"name": "containers-mcp",
"version": "0.0.1",
"private": true,
"type": "module",
"scripts": {
"check:types": "run-tsc",
"check:lint": "run-eslint-workers",
"deploy": "wrangler deploy",
"dev": "concurrently \"tsx container/index.ts\" \"wrangler dev --var \"ENVIRONMENT:dev\"\"",
"build": "docker build -f Dockerfile ../../",
"build:container": "docker build --platform linux/amd64 --tag sandbox-container:$(git rev-parse --short HEAD) -f Dockerfile ../../ && wrangler containers push sandbox-container:$(git rev-parse --short HEAD)",
"start": "wrangler dev",
"start:container": "tsx container/index.ts",
"postinstall": "mkdir -p workdir",
Expand Down
6 changes: 3 additions & 3 deletions apps/sandbox-container/server/containerHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const MAX_CONTAINERS = 8
export const MAX_CONTAINERS = 50
export async function startAndWaitForPort(
environment: 'dev' | 'prod' | 'test',
container: Container | undefined,
Expand Down Expand Up @@ -28,11 +28,11 @@ export async function startAndWaitForPort(

// force DO to keep track of running state
monitor = container.monitor()
monitor.then(() => console.log('Container exited'))
void monitor.then(() => console.log('Container exited'))
}

const conn = await port.connect(`10.0.0.1:${portToAwait}`)
conn.close()
await conn.close()
console.log('Connected')
return true
} catch (err: any) {
Expand Down
25 changes: 22 additions & 3 deletions apps/sandbox-container/server/containerManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { DurableObject } from 'cloudflare:workers'

import { getEnv } from '@repo/mcp-common/src/env'
import { MetricsTracker } from '@repo/mcp-observability'

import { ContainerEvent } from './metrics'

import type { Env } from './context'

const env = getEnv<Env>()
export class ContainerManager extends DurableObject<Env> {
metrics = new MetricsTracker(env.MCP_METRICS, {
name: env.MCP_SERVER_NAME,
version: env.MCP_SERVER_VERSION,
})

constructor(
public ctx: DurableObjectState,
public env: Env
Expand All @@ -27,9 +38,10 @@ export class ContainerManager extends DurableObject<Env> {

console.log(id, time, now, now.valueOf() - time.valueOf())

if (now.valueOf() - time.valueOf() > 10 * 60 * 1000) {
const doId = this.env.CONTAINER_MCP_AGENT.idFromString(id)
const stub = this.env.CONTAINER_MCP_AGENT.get(doId)
// 15m timeout for container lifetime
if (now.valueOf() - time.valueOf() > 15 * 60 * 1000) {
const doId = this.env.USER_CONTAINER.idFromString(id)
const stub = this.env.USER_CONTAINER.get(doId)
await stub.destroyContainer()
await this.killContainer(id)
}
Expand All @@ -42,6 +54,13 @@ export class ContainerManager extends DurableObject<Env> {
for (const c of activeContainers.keys()) {
activeIds.push(c)
}

this.metrics.logEvent(
new ContainerEvent({
active: activeIds.length,
})
)

return activeIds
}
}
Expand Down
Loading
Loading