Skip to content

Commit f5cd4c2

Browse files
committed
refactor
1 parent 9d6b180 commit f5cd4c2

File tree

9 files changed

+409
-613
lines changed

9 files changed

+409
-613
lines changed

CONTEXT.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# CodeBench MCP - JavaScript Executor
22

3-
An MCP server that provides JavaScript execution capabilities with Node.js-like APIs.
3+
An MCP server that provides JavaScript execution capabilities with ski runtime.
44

55
## Tool
66

77
### executeJS
8-
- Execute JavaScript code with full Node.js-like environment
9-
- Includes: console, fs, http, fetch (with promises), timers (setTimeout/setInterval), process, and require
8+
- Execute JavaScript code with ski runtime environment
9+
- Includes: console (built-in), http, fetch, timers, buffer, crypto, and other ski modules
1010
- Parameters: `code` (required): JavaScript code to execute
1111
- Modules are configurable via CLI flags
1212

1313
## CLI Usage
14-
- `codebench-mcp` - Run with all modules enabled
15-
- `codebench-mcp --enabled-modules console,fs,timers` - Enable only specific modules
16-
- `codebench-mcp --disabled-modules http,fetch` - Disable specific modules
17-
- Available modules: console, fs, http, fetch, timers, process, require
14+
- `codebench-mcp` - Run with default modules enabled (http, fetch, timers, buffer, crypto)
15+
- `codebench-mcp --enabled-modules http,fetch` - Enable only specific modules
16+
- `codebench-mcp --disabled-modules timers` - Disable specific modules
17+
- Available modules: http (import serve from 'ski/http/server'), fetch (global), timers (global), buffer (global), cache (import cache from 'ski/cache'), crypto (import crypto from 'ski/crypto'), dom, encoding (global), ext, html, signal (global), stream (global), url (global)
1818

1919
## Build/Test Commands
2020
- `go build ./...` - Build all packages
@@ -39,4 +39,10 @@ An MCP server that provides JavaScript execution capabilities with Node.js-like
3939

4040
## Project Info
4141
- Go version: 1.23.10
42-
- Module: github.com/mark3labs/codebench-mcp
42+
- Module: github.com/mark3labs/codebench-mcp
43+
44+
## Known Limitations
45+
- No fs or process modules - not available in ski runtime
46+
- Module access varies: some modules are global (fetch, timers, buffer, encoding, signal, stream, url), others require imports (http: 'ski/http/server', cache: 'ski/cache', crypto: 'ski/crypto')
47+
- Each execution creates a fresh VM instance for isolation
48+
- Module filtering configuration exists but actual runtime filtering not fully implemented

README.md

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# JavaScript Executor MCP Server
22

3-
This MCP server provides JavaScript execution capabilities with a Node.js-like environment.
3+
This MCP server provides JavaScript execution capabilities with ski runtime.
44

55
## Features
66

77
The `executeJS` tool provides:
88

9-
- **Console API**: `console.log()`, `console.error()`, `console.warn()`
10-
- **File System**: `fs.readFileSync()`, `fs.writeFileSync()`, `fs.existsSync()`
9+
- **Console API**: `console.log()`, `console.error()`, `console.warn()` (built-in)
1110
- **HTTP Server**: `http.createServer()` with request/response handling
12-
- **Fetch API**: `fetch()` with Promise support for HTTP requests
13-
- **Timers**: `setTimeout()`, `clearTimeout()`, `setInterval()`, `clearInterval()`
14-
- **Process**: `process.argv`, `process.cwd()`, `process.exit()`, `process.env`
15-
- **Module System**: `require()` for loading JavaScript modules
11+
- **HTTP Client**: `http.request()` for HTTP requests
12+
- **Fetch API**: Modern `fetch()` with Request, Response, Headers, FormData
13+
- **Timers**: `setTimeout()`, `setInterval()`, `clearTimeout()`, `clearInterval()`
14+
- **Buffer**: Buffer, Blob, File APIs for binary data handling
15+
- **Crypto**: Cryptographic functions (hashing, encryption, HMAC)
16+
- **Additional modules**: cache, dom, encoding, ext, html, signal, stream, url
1617

1718
## Getting Started
1819

@@ -36,25 +37,33 @@ codebench-mcp
3637

3738
```bash
3839
# Enable only specific modules
39-
codebench-mcp --enabled-modules console,fs,timers
40+
codebench-mcp --enabled-modules http,fetch
4041

4142
# Disable specific modules (enable all others)
42-
codebench-mcp --disabled-modules http,fetch
43+
codebench-mcp --disabled-modules timers
4344

4445
# Show help
4546
codebench-mcp --help
4647
```
4748

4849
**Available modules:**
49-
- `console` - Console logging (console.log, console.error, console.warn)
50-
- `fs` - File system operations (fs.readFileSync, fs.writeFileSync, fs.existsSync)
51-
- `http` - HTTP server creation (http.createServer)
52-
- `fetch` - HTTP client requests (fetch API with promises)
53-
- `timers` - Timer functions (setTimeout, setInterval, clearTimeout, clearInterval)
54-
- `process` - Process information (process.argv, process.cwd, process.env, process.exit)
55-
- `require` - Module loading system
56-
57-
**Note:** The `executeJS` tool description dynamically updates to show only the enabled modules and includes detailed information about what each module provides. This helps users understand exactly what JavaScript APIs are available in the simplified VM environment.
50+
- `http` - HTTP server creation and client requests (import serve from 'ski/http/server')
51+
- `fetch` - Modern fetch API with Request, Response, Headers, FormData (available globally)
52+
- `timers` - setTimeout, setInterval, clearTimeout, clearInterval (available globally)
53+
- `buffer` - Buffer, Blob, File APIs for binary data handling (available globally)
54+
- `cache` - In-memory caching with TTL support (import cache from 'ski/cache')
55+
- `crypto` - Cryptographic functions (hashing, encryption, HMAC) (import crypto from 'ski/crypto')
56+
- `dom` - DOM Event and EventTarget APIs
57+
- `encoding` - TextEncoder, TextDecoder for text encoding/decoding (available globally)
58+
- `ext` - Extended context and utility functions
59+
- `html` - HTML parsing and manipulation
60+
- `signal` - AbortController and AbortSignal for cancellation (available globally)
61+
- `stream` - ReadableStream and streaming APIs (available globally)
62+
- `url` - URL and URLSearchParams APIs (available globally)
63+
64+
**Default modules:** `http`, `fetch`, `timers`, `buffer`, `crypto`
65+
66+
**Note:** The `executeJS` tool description dynamically updates to show only the enabled modules and includes detailed information about what each module provides.
5867

5968
#### As a library in your Go project
6069

@@ -200,7 +209,7 @@ To integrate the Docker image with apps that support MCP:
200209

201210
### executeJS
202211

203-
Execute JavaScript code with full Node.js-like environment.
212+
Execute JavaScript code with ski runtime environment.
204213

205214
**Parameters:**
206215
- `code` (required): JavaScript code to execute
@@ -209,56 +218,50 @@ Execute JavaScript code with full Node.js-like environment.
209218
```javascript
210219
console.log("Hello, World!");
211220

212-
// File operations
213-
fs.writeFileSync("test.txt", "Hello from JS!");
214-
const content = fs.readFileSync("test.txt");
215-
console.log("File content:", content);
216-
217-
// Fetch API with promises
218-
fetch("https://github.com/api/users/octocat")
219-
.then(response => response.json())
220-
.then(data => {
221-
console.log("User:", data.name);
222-
console.log("Public repos:", data.public_repos);
223-
})
224-
.catch(error => console.error("Fetch error:", error));
225-
226-
// HTTP server with configurable ports and callbacks
227-
const server = http.createServer((req, res) => {
228-
console.log(`${req.method} ${req.url}`);
229-
230-
res.setHeader("Content-Type", "application/json");
231-
res.writeHead(200);
232-
res.end(JSON.stringify({
233-
message: "Hello from HTTP server!",
234-
method: req.method,
235-
url: req.url
236-
}));
237-
});
221+
// Basic JavaScript execution
222+
const result = 2 + 3;
223+
console.log('Result:', result);
238224

239-
// Multiple ways to start the server:
240-
server.listen(3000); // Port only
241-
server.listen(3000, () => console.log("Started!")); // Port + callback
242-
server.listen(3000, "localhost"); // Port + host
243-
server.listen(3000, "localhost", () => { // Port + host + callback
244-
console.log("Server running on localhost:3000");
225+
// Fetch API (available globally when enabled)
226+
const response = await fetch('https://api.example.com/data');
227+
const data = await response.json();
228+
229+
// HTTP server (import required)
230+
import serve from 'ski/http/server';
231+
serve(8000, async (req) => {
232+
return new Response('Hello World');
245233
});
246234

247-
// Server management
248-
setTimeout(() => {
249-
server.close(); // Gracefully shutdown server
250-
}, 10000);
235+
// Cache operations (import required)
236+
import cache from 'ski/cache';
237+
cache.set('key', 'value');
238+
console.log(cache.get('key'));
239+
240+
// Crypto operations (import required)
241+
import crypto from 'ski/crypto';
242+
const hash = crypto.md5('hello').hex();
243+
console.log('MD5 hash:', hash);
251244

252-
// Timers
253-
setTimeout(() => {
254-
console.log("Timer executed!");
255-
}, 1000);
245+
// Timers (available globally)
246+
setTimeout(() => console.log('Hello after 1 second'), 1000);
256247

257-
// Process info
258-
console.log("Current directory:", process.cwd());
259-
console.log("Arguments:", process.argv);
248+
// Buffer operations (available globally)
249+
const buffer = Buffer.from('hello', 'utf8');
250+
console.log(buffer.toString('base64'));
251+
252+
// URL operations (available globally)
253+
const url = new URL('https://example.com/path?param=value');
254+
console.log('Host:', url.host);
255+
console.log('Pathname:', url.pathname);
260256
```
261257

258+
## Limitations
259+
260+
- **No fs or process modules** - File system and process APIs are not available in ski runtime
261+
- **Module access varies** - Some modules are global (fetch, http), others may need require()
262+
- **Each execution creates a fresh VM** - For isolation, each execution starts with a clean state
263+
- **Module filtering** - Configuration exists but actual runtime filtering not fully implemented
264+
262265
## Building
263266

264267
```bash

cmd/root.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,27 @@ var (
1919

2020
// Available modules
2121
var availableModules = []string{
22-
"console",
23-
"fs",
2422
"http",
23+
"fetch",
2524
"timers",
26-
"process",
27-
"require",
25+
"buffer",
26+
"cache",
27+
"crypto",
28+
"dom",
29+
"encoding",
30+
"ext",
31+
"html",
32+
"signal",
33+
"stream",
34+
"url",
2835
}
2936

3037
// rootCmd represents the base command when called without any subcommands
3138
var rootCmd = &cobra.Command{
3239
Use: "codebench-mcp",
3340
Short: "JavaScript Executor MCP Server",
3441
Long: `A Model Context Protocol (MCP) server that provides JavaScript execution capabilities
35-
with a Node.js-like environment including console, fs, http, timers, process, and require modules.`,
42+
with ski runtime including http, fetch, timers, buffer, crypto, and other modules.`,
3643
Run: func(cmd *cobra.Command, args []string) {
3744
// Validate module configuration
3845
if len(enabledModules) > 0 && len(disabledModules) > 0 {
@@ -64,8 +71,8 @@ with a Node.js-like environment including console, fs, http, timers, process, an
6471
}
6572
}
6673
} else {
67-
// Enable all modules by default
68-
modulesToEnable = availableModules
74+
// Enable default modules (same as NewJSHandler default)
75+
modulesToEnable = []string{"http", "fetch", "timers", "buffer", "crypto"}
6976
}
7077

7178
// Create server with module configuration

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ module github.com/mark3labs/codebench-mcp
33
go 1.23.10
44

55
require (
6-
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd
6+
github.com/grafana/sobek v0.0.0-20250312125646-01f8811babf6
77
github.com/mark3labs/mcp-go v0.31.0
8+
github.com/shiroyk/ski v0.0.0-20250408031354-4adc80b35df6
89
github.com/spf13/cobra v1.9.1
9-
github.com/stretchr/testify v1.9.0
10+
github.com/stretchr/testify v1.10.0
1011
)
1112

1213
require (
1314
github.com/davecgh/go-spew v1.1.1 // indirect
14-
github.com/dlclark/regexp2 v1.11.4 // indirect
15+
github.com/dlclark/regexp2 v1.11.5 // indirect
1516
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
16-
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 // indirect
17+
github.com/google/pprof v0.0.0-20250302191652-9094ed2288e7 // indirect
1718
github.com/google/uuid v1.6.0 // indirect
1819
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1920
github.com/pmezard/go-difflib v1.0.0 // indirect
2021
github.com/spf13/cast v1.7.1 // indirect
2122
github.com/spf13/pflag v1.0.6 // indirect
2223
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
23-
golang.org/x/text v0.20.0 // indirect
24+
golang.org/x/crypto v0.36.0 // indirect
25+
golang.org/x/net v0.37.0 // indirect
26+
golang.org/x/text v0.23.0 // indirect
2427
gopkg.in/yaml.v3 v3.0.1 // indirect
2528
)

go.sum

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
33
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
7-
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
8-
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd h1:QMSNEh9uQkDjyPwu/J541GgSH+4hw+0skJDIj9HJ3mE=
9-
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
6+
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
7+
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
108
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
119
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
1210
github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q=
1311
github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
1412
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
1513
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
16-
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 h1:sAGdeJj0bnMgUNVeUpp6AYlVdCt3/GdI3pGRqsNSQLs=
17-
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
14+
github.com/google/pprof v0.0.0-20250302191652-9094ed2288e7 h1:+J3r2e8+RsmN3vKfo75g0YSY61ms37qzPglu4p0sGro=
15+
github.com/google/pprof v0.0.0-20250302191652-9094ed2288e7/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
1816
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1917
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
18+
github.com/grafana/sobek v0.0.0-20250312125646-01f8811babf6 h1:1PRgCTRht1ETEzQKXUjhn0YPKgMnUOJxfIyjPRGNrUg=
19+
github.com/grafana/sobek v0.0.0-20250312125646-01f8811babf6/go.mod h1:FmcutBFPLiGgroH42I4/HBahv7GxVjODcVWFTw1ISes=
2020
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
2121
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2222
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -30,18 +30,24 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
3030
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3131
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
3232
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
33+
github.com/shiroyk/ski v0.0.0-20250408031354-4adc80b35df6 h1:MP/JX14gcF+xYACElKB+Dzr56oMW8xLO/r+2CKnsusc=
34+
github.com/shiroyk/ski v0.0.0-20250408031354-4adc80b35df6/go.mod h1:jCP1xHey89rnssFI9hlBSNOZ34LUROFMysGNkE5otlY=
3335
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
3436
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
3537
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
3638
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
3739
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
3840
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
39-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
40-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
41+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
42+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4143
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
4244
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
43-
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
44-
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
45+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
46+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
47+
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
48+
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
49+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
50+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
4551
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4652
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4753
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

0 commit comments

Comments
 (0)