@@ -9,6 +9,7 @@ The `executeJS` tool provides:
9
9
- ** Console API** : ` console.log() ` , ` console.error() ` , ` console.warn() `
10
10
- ** File System** : ` fs.readFileSync() ` , ` fs.writeFileSync() ` , ` fs.existsSync() `
11
11
- ** HTTP Server** : ` http.createServer() ` with request/response handling
12
+ - ** Fetch API** : ` fetch() ` with Promise support for HTTP requests
12
13
- ** Timers** : ` setTimeout() ` , ` clearTimeout() ` , ` setInterval() ` , ` clearInterval() `
13
14
- ** Process** : ` process.argv ` , ` process.cwd() ` , ` process.exit() ` , ` process.env `
14
15
- ** Module System** : ` require() ` for loading JavaScript modules
@@ -31,6 +32,30 @@ go install github.com/mark3labs/codebench-mcp@latest
31
32
codebench-mcp
32
33
```
33
34
35
+ #### With module configuration
36
+
37
+ ``` bash
38
+ # Enable only specific modules
39
+ codebench-mcp --enabled-modules console,fs,timers
40
+
41
+ # Disable specific modules (enable all others)
42
+ codebench-mcp --disabled-modules http,fetch
43
+
44
+ # Show help
45
+ codebench-mcp --help
46
+ ```
47
+
48
+ ** 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.
58
+
34
59
#### As a library in your Go project
35
60
36
61
``` go
@@ -57,6 +82,76 @@ func main() {
57
82
}
58
83
```
59
84
85
+ #### Using InProcessTransport
86
+
87
+ ``` go
88
+ package main
89
+
90
+ import (
91
+ " context"
92
+ " log"
93
+
94
+ " github.com/mark3labs/codebench-mcp/jsserver"
95
+ " github.com/mark3labs/mcp-go/client"
96
+ " github.com/mark3labs/mcp-go/mcp"
97
+ )
98
+
99
+ func main () {
100
+ // Create the JS server
101
+ jsServer , err := jsserver.NewJSServer ()
102
+ if err != nil {
103
+ log.Fatalf (" Failed to create server: %v " , err)
104
+ }
105
+
106
+ // Create an in-process client
107
+ mcpClient , err := client.NewInProcessClient (jsServer)
108
+ if err != nil {
109
+ log.Fatalf (" Failed to create client: %v " , err)
110
+ }
111
+ defer mcpClient.Close ()
112
+
113
+ // Start the client
114
+ if err := mcpClient.Start (context.Background ()); err != nil {
115
+ log.Fatalf (" Failed to start client: %v " , err)
116
+ }
117
+
118
+ // Initialize the client
119
+ initRequest := mcp.InitializeRequest {}
120
+ initRequest.Params .ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
121
+ initRequest.Params .ClientInfo = mcp.Implementation {
122
+ Name: " my-app" ,
123
+ Version: " 1.0.0" ,
124
+ }
125
+ _, err = mcpClient.Initialize (context.Background (), initRequest)
126
+ if err != nil {
127
+ log.Fatalf (" Failed to initialize: %v " , err)
128
+ }
129
+
130
+ // Execute JavaScript code
131
+ callRequest := mcp.CallToolRequest {}
132
+ callRequest.Params .Name = " executeJS"
133
+ callRequest.Params .Arguments = map [string ]any{
134
+ " code" : `
135
+ console.log("Hello from JavaScript!");
136
+ const result = Math.sqrt(16);
137
+ console.log("Square root of 16 is:", result);
138
+ result;
139
+ ` ,
140
+ }
141
+
142
+ result , err := mcpClient.CallTool (context.Background (), callRequest)
143
+ if err != nil {
144
+ log.Fatalf (" Failed to call tool: %v " , err)
145
+ }
146
+
147
+ if result.IsError {
148
+ log.Printf (" JavaScript execution error: %v " , result.Content )
149
+ } else {
150
+ log.Printf (" JavaScript execution result: %v " , result.Content )
151
+ }
152
+ }
153
+ ```
154
+
60
155
### Usage with Model Context Protocol
61
156
62
157
To integrate this server with apps that support MCP:
@@ -119,11 +214,40 @@ fs.writeFileSync("test.txt", "Hello from JS!");
119
214
const content = fs .readFileSync (" test.txt" );
120
215
console .log (" File content:" , content);
121
216
122
- // HTTP server
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
123
227
const server = http .createServer ((req , res ) => {
124
- res .end (" Hello from HTTP server!" );
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
+ });
238
+
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" );
125
245
});
126
- server .listen (8080 );
246
+
247
+ // Server management
248
+ setTimeout (() => {
249
+ server .close (); // Gracefully shutdown server
250
+ }, 10000 );
127
251
128
252
// Timers
129
253
setTimeout (() => {
0 commit comments