Skip to content

Commit 3278c2f

Browse files
committed
linting
1 parent 7f0ae1c commit 3278c2f

File tree

15 files changed

+157
-217
lines changed

15 files changed

+157
-217
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Container MCP Server
2+
3+
This is a simple MCP-based interface for a sandboxed development environment.
4+
5+
## Local dev
6+
7+
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.
8+
9+
Do the following from within the sandbox-container app:
10+
11+
1. Copy the `.dev.vars.example` file to a new `.dev.vars` file.
12+
2. Get the Cloudflare client id and secret from a team member and add them to the `.dev.vars` file.
13+
3. Run `pnpm i` then `pnpm dev` to start the MCP server.
14+
4. Run `pnpx @modelcontextprotocol/inspector` to start the MCP inspector client.
15+
5. Open the inspector client in your browser and connect to the server via `http://localhost:8976/sse`.
16+
17+
Note: Temporary files created through files tool calls are stored in the workdir folder of this app.
18+
19+
## Deploying
20+
21+
1. Make sure the docker daemon is running
22+
23+
2. Disable WARP and run
24+
25+
```
26+
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/14387504770/npm-package-wrangler-8740 deploy
27+
```
28+
29+
3. Add to your Claude config. If using with Claude, you'll need to disable WARP:
30+
31+
```
32+
{
33+
"mcpServers": {
34+
"container": {
35+
"command": "npx",
36+
"args": [
37+
"mcp-remote",
38+
// this is my deployed instance
39+
"https://container-starter-2.cmsparks.workers.dev/sse"
40+
]
41+
}
42+
}
43+
}
44+
```

apps/sandbox-container/README.md

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,49 @@
1-
# Container MCP Server
1+
# Cloudflare Container Sandbox MCP Server
22

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

5-
## Local dev
5+
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.
66

7-
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.
7+
## Tools
88

9-
Do the following from within the sandbox-container app:
9+
| **Category** | **Tool** | **Description** |
10+
| ----------------- | -------------------------- | ----------------------------------------------------------------------------- |
11+
| **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.|
12+
| | `container_ping` | Ping a container for connectivity |
13+
| **Filesystem** | `container_file_write` | Write to a file |
14+
| | `container_files_list` | List all files in the work directory |
15+
| | `container_file_read` | Read the contents of a single file or directory |
16+
| | `container_file_delete` | Delete a single file or directory |
17+
| **Execution** | `container_exec` | Run a command in the shell |
1018

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

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

19-
## Deploying
22+
### Prompt Examples
2023

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

23-
2. Disable WARP and run
28+
## Access the remote MCP server from from any MCP Client
2429

25-
```
26-
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/14387504770/npm-package-wrangler-8740 deploy
27-
```
30+
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/)).
2831

29-
3. Add to your Claude config. If using with Claude, you'll need to disable WARP:
32+
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.
3033

31-
```
34+
Replace the content with the following configuration:
35+
36+
```json
3237
{
33-
"mcpServers": {
34-
"container": {
35-
"command": "npx",
36-
"args": [
37-
"mcp-remote",
38-
// this is my deployed instance
39-
"https://container-starter-2.cmsparks.workers.dev/sse"
40-
]
41-
}
42-
}
38+
"mcpServers": {
39+
"cloudflare": {
40+
"command": "npx",
41+
"args": ["mcp-remote", "https://containers.mcp.cloudflare.com/sse"]
42+
}
43+
}
4344
}
4445
```
4546

46-
## Tools
47-
48-
- `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).
49-
- `container_ping`: Ping a container for connectivity
50-
- `container_exec`: Run a command in the shell
51-
- `container_file_write`: Write to a file
52-
- `container_files_list`: List all files in the work directory
53-
- `container_file_read`: Read the contents of a single file or directory
54-
- `container_file_delete`: Delete a single file or directory
55-
56-
## Resources
57-
58-
TODO
59-
60-
Tried implementing these, but MCP clients don't support resources well at all.
61-
62-
## Prompts
63-
64-
TODO
65-
66-
## Container support
47+
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.
6748

68-
The container currently runs python and node. It's connected to the internet and LLMs can install whatever packages.
49+
Interested in contributing, and running this server locally? See [CONTRIBUTING.md](CONTRIBUTING.md) to get started.

apps/sandbox-container/container/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ app.post('/exec', zValidator('json', ExecParams), (c) => {
139139
const execParams = c.req.valid('json')
140140
const proc = exec(execParams.args)
141141
return streamText(c, async (stream) => {
142-
return new Promise(async (resolve, reject) => {
142+
return new Promise((resolve, reject) => {
143143
if (proc.stdout) {
144144
// Stream data from stdout
145145
proc.stdout.on('data', async (data) => {
146146
await stream.write(data.toString())
147147
})
148148
} else {
149-
await stream.write('WARNING: no stdout stream for process')
149+
void stream.write('WARNING: no stdout stream for process')
150150
}
151151

152152
if (execParams.streamStderr) {
@@ -155,15 +155,15 @@ app.post('/exec', zValidator('json', ExecParams), (c) => {
155155
await stream.write(data.toString())
156156
})
157157
} else {
158-
await stream.write('WARNING: no stderr stream for process')
158+
void stream.write('WARNING: no stderr stream for process')
159159
}
160160
}
161161

162162
// Handle process exit
163163
proc.on('exit', async (code) => {
164164
await stream.write(`Process exited with code: ${code}`)
165165
if (code === 0) {
166-
stream.close()
166+
await stream.close()
167167
resolve()
168168
} else {
169169
console.error(`Process exited with code ${code}`)

apps/sandbox-container/evals/exec.eval.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { assert, expect } from 'vitest'
1+
import { expect } from 'vitest'
22
import { describeEval } from 'vitest-evals'
3-
import { z } from 'zod'
43

54
import { checkFactuality } from '@repo/eval-tools/src/scorers'
65
import { eachModel } from '@repo/eval-tools/src/test-models'

apps/sandbox-container/evals/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { jsonSchemaToZod } from '@n8n/json-schema-to-zod'
22
import { MCPClientManager } from 'agents/mcp/client'
3-
import { LanguageModelV1, streamText, StreamTextResult, tool, ToolCallPart, ToolSet } from 'ai'
3+
import { streamText, tool } from 'ai'
44
import { z } from 'zod'
55

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

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

59-
for await (const part of res.fullStream) {
60+
// consume the stream
61+
// eslint-disable-next-line no-empty
62+
for await (const _ of res.fullStream) {
6063
}
6164

6265
// convert into an LLM readable result so our factuality checker can validate tool calls
6366
let messagesWithTools = ''
64-
let toolCalls: ToolCallPart[] = []
67+
const toolCalls: ToolCallPart[] = []
6568
const messages = (await res.response).messages
6669
for (const message of messages) {
6770
console.log(message.content)

apps/sandbox-container/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2-
"name": "containers-starter",
2+
"name": "containers-mcp",
33
"version": "0.0.1",
44
"private": true,
55
"type": "module",
66
"scripts": {
77
"check:types": "run-tsc",
8+
"check:lint": "run-eslint-workers",
89
"deploy": "wrangler deploy",
910
"dev": "concurrently \"tsx container/index.ts\" \"wrangler dev --var \"ENVIRONMENT:dev\"\"",
1011
"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)",

apps/sandbox-container/server/containerHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export async function startAndWaitForPort(
2828

2929
// force DO to keep track of running state
3030
monitor = container.monitor()
31-
monitor.then(() => console.log('Container exited'))
31+
void monitor.then(() => console.log('Container exited'))
3232
}
3333

3434
const conn = await port.connect(`10.0.0.1:${portToAwait}`)
35-
conn.close()
35+
await conn.close()
3636
console.log('Connected')
3737
return true
3838
} catch (err: any) {

apps/sandbox-container/server/containerManager.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { DurableObject } from 'cloudflare:workers'
22

3-
import type { Env } from './context'
4-
import { ContainerEvent } from './metrics'
3+
import { getEnv } from '@repo/mcp-common/src/env'
54
import { MetricsTracker } from '@repo/mcp-observability'
65

6+
import { ContainerEvent } from './metrics'
7+
8+
import type { Env } from './context'
9+
10+
const env = getEnv<Env>()
711
export class ContainerManager extends DurableObject<Env> {
8-
metrics = new MetricsTracker(this.env.MCP_METRICS, {
9-
name: this.env.MCP_SERVER_NAME,
10-
version: this.env.MCP_SERVER_VERSION
12+
metrics = new MetricsTracker(env.MCP_METRICS, {
13+
name: env.MCP_SERVER_NAME,
14+
version: env.MCP_SERVER_VERSION,
1115
})
1216

1317
constructor(
@@ -51,9 +55,11 @@ export class ContainerManager extends DurableObject<Env> {
5155
activeIds.push(c)
5256
}
5357

54-
this.metrics.logEvent(new ContainerEvent({
55-
active: activeIds.length
56-
}))
58+
this.metrics.logEvent(
59+
new ContainerEvent({
60+
active: activeIds.length,
61+
})
62+
)
5763

5864
return activeIds
5965
}

apps/sandbox-container/server/containerMcp.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { stripProtocolFromFilePath } from './utils'
99
import type { Env } from './context'
1010
import type { Props, UserContainer } from '.'
1111

12-
export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
12+
export class ContainerMcpAgent extends McpAgent<Env, never, Props> {
1313
_server: CloudflareMCPServer | undefined
1414
set server(server: CloudflareMCPServer) {
1515
this._server = server
@@ -57,7 +57,7 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
5757
const userInBlocklist = await this.env.USER_BLOCKLIST.get(this.props.user.id)
5858
if (userInBlocklist) {
5959
return {
60-
content: [{ type: 'text', text: "Blocked from intializing container." }],
60+
content: [{ type: 'text', text: 'Blocked from intializing container.' }],
6161
}
6262
}
6363
return {
@@ -66,16 +66,20 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
6666
}
6767
)
6868

69-
this.server.tool('container_ping', `Ping the container for liveliness. Use this tool to check if the container is running.`, {}, async ({}) => {
70-
return {
71-
content: [{ type: 'text', text: await this.userContainer.container_ping() }],
69+
this.server.tool(
70+
'container_ping',
71+
`Ping the container for liveliness. Use this tool to check if the container is running.`,
72+
async () => {
73+
return {
74+
content: [{ type: 'text', text: await this.userContainer.container_ping() }],
75+
}
7276
}
73-
})
77+
)
7478
this.server.tool(
7579
'container_exec',
7680
`Run a command in a container and return the results from stdout.
7781
If necessary, set a timeout. To debug, stream back standard error.
78-
If you\'re using python, ALWAYS use python3 alongside pip3`,
82+
If you're using python, ALWAYS use python3 alongside pip3`,
7983
{ args: ExecParams },
8084
async ({ args }) => {
8185
return {
@@ -106,22 +110,26 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
106110
}
107111
}
108112
)
109-
this.server.tool('container_files_list', 'List working directory file tree. This just reads the contents of the current working directory', {}, async ({}) => {
110-
// Begin workaround using container read rather than ls:
111-
const readFile = await this.userContainer.container_file_read('.')
112-
return {
113-
content: [
114-
{
115-
type: 'resource',
116-
resource: {
117-
text: readFile.type === "text" ? readFile.textOutput : readFile.base64Output,
118-
uri: `file://`,
119-
mimeType: readFile.mimeType,
113+
this.server.tool(
114+
'container_files_list',
115+
'List working directory file tree. This just reads the contents of the current working directory',
116+
async () => {
117+
// Begin workaround using container read rather than ls:
118+
const readFile = await this.userContainer.container_file_read('.')
119+
return {
120+
content: [
121+
{
122+
type: 'resource',
123+
resource: {
124+
text: readFile.type === 'text' ? readFile.textOutput : readFile.base64Output,
125+
uri: `file://`,
126+
mimeType: readFile.mimeType,
127+
},
120128
},
121-
},
122-
],
129+
],
130+
}
123131
}
124-
})
132+
)
125133
this.server.tool(
126134
'container_file_read',
127135
'Read a specific file or directory. Use this tool if you would like to read files or display them to the user. This allow you to get a displayable image for the user if there is an image file.',
@@ -135,7 +143,7 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
135143
{
136144
type: 'resource',
137145
resource: {
138-
text: readFile.type === "text" ? readFile.textOutput : readFile.base64Output,
146+
text: readFile.type === 'text' ? readFile.textOutput : readFile.base64Output,
139147
uri: `file://${path}`,
140148
mimeType: readFile.mimeType,
141149
},

apps/sandbox-container/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { MetricsTracker } from '@repo/mcp-observability'
1212

1313
import { ContainerManager } from './containerManager'
1414
import { ContainerMcpAgent } from './containerMcp'
15+
import { UserContainer } from './userContainer'
1516

1617
import type { McpAgent } from 'agents/mcp'
1718
import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler'
1819
import type { Env } from './context'
19-
import { UserContainer } from './userContainer'
2020

2121
export { ContainerManager, ContainerMcpAgent, UserContainer }
2222

0 commit comments

Comments
 (0)