microsandbox-server 0.2.6

`microsandbox-server` implements the sandbox server responsible for orchestrating sandboxes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Model Context Protocol (MCP) implementation for microsandbox server.
//!
//! This module implements MCP endpoints served at the `/mcp` endpoint.
//! MCP is essentially JSON-RPC with specific method names and schemas.
//!
//! The module provides:
//! - MCP server initialization and capabilities
//! - Tool definitions for sandbox operations
//! - Prompt templates for common sandbox tasks
//! - Integration with existing sandbox management functions

use serde_json::json;
use tracing::{debug, error};

use crate::{
    error::ServerError,
    handler::{
        forward_rpc_to_portal, sandbox_get_metrics_impl, sandbox_start_impl, sandbox_stop_impl,
    },
    payload::{
        JsonRpcError, JsonRpcRequest, JsonRpcResponse, JsonRpcResponseOrNotification,
        ProcessedNotification, SandboxMetricsGetParams, SandboxStartParams, SandboxStopParams,
        JSONRPC_VERSION,
    },
    state::AppState,
    ServerResult,
};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

/// MCP protocol version
const MCP_PROTOCOL_VERSION: &str = "2024-11-05";

/// Server information
const SERVER_NAME: &str = "microsandbox-server";
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");

//--------------------------------------------------------------------------------------------------
// Functions: Handlers
//--------------------------------------------------------------------------------------------------

/// Handle MCP initialize request
pub async fn handle_mcp_initialize(
    _state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponse> {
    debug!("Handling MCP initialize request");

    let result = json!({
        "protocolVersion": MCP_PROTOCOL_VERSION,
        "capabilities": {
            "tools": {
                "listChanged": false
            },
            "prompts": {
                "listChanged": false
            }
        },
        "serverInfo": {
            "name": SERVER_NAME,
            "version": SERVER_VERSION
        }
    });

    Ok(JsonRpcResponse::success(result, request.id))
}

/// Handle MCP list tools request
pub async fn handle_mcp_list_tools(
    _state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponse> {
    debug!("Handling MCP list tools request");

    let tools = json!({
        "tools": [
            {
                "name": "sandbox_start",
                "description": "Start a new sandbox with specified configuration. This creates an isolated environment for code execution. IMPORTANT: Always stop the sandbox when done to prevent it from running indefinitely and consuming resources. SUPPORTED IMAGES: Only 'microsandbox/python' (for Python code) and 'microsandbox/node' (for Node.js code) are currently supported.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "sandbox": {
                            "type": "string",
                            "description": "Name of the sandbox to start"
                        },
                        "namespace": {
                            "type": "string",
                            "description": "Namespace for the sandbox"
                        },
                        "config": {
                            "type": "object",
                            "description": "Sandbox configuration",
                            "properties": {
                                "image": {
                                    "type": "string",
                                    "description": "Docker image to use. Only 'microsandbox/python' and 'microsandbox/node' are supported.",
                                    "enum": ["microsandbox/python", "microsandbox/node"]
                                },
                                "memory": {
                                    "type": "integer",
                                    "description": "Memory limit in MiB"
                                },
                                "cpus": {
                                    "type": "integer",
                                    "description": "Number of CPUs"
                                },
                                "volumes": {
                                    "type": "array",
                                    "items": {"type": "string"},
                                    "description": "Volume mounts"
                                },
                                "ports": {
                                    "type": "array",
                                    "items": {"type": "string"},
                                    "description": "Port mappings"
                                },
                                "envs": {
                                    "type": "array",
                                    "items": {"type": "string"},
                                    "description": "Environment variables"
                                }
                            }
                        }
                    },
                    "required": ["sandbox", "namespace"]
                }
            },
            {
                "name": "sandbox_stop",
                "description": "Stop a running sandbox and clean up its resources. CRITICAL: Always call this when you're finished with a sandbox to prevent resource leaks and indefinite running. Failing to stop sandboxes will cause them to consume system resources unnecessarily.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "sandbox": {
                            "type": "string",
                            "description": "Name of the sandbox to stop"
                        },
                        "namespace": {
                            "type": "string",
                            "description": "Namespace of the sandbox"
                        }
                    },
                    "required": ["sandbox", "namespace"]
                }
            },
            {
                "name": "sandbox_run_code",
                "description": "Execute code in a running sandbox. PREREQUISITES: The target sandbox must be started first using sandbox_start - this will fail if the sandbox is not running. TIMING: Code execution is synchronous and may take time depending on complexity. Long-running code will block until completion or timeout.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "sandbox": {
                            "type": "string",
                            "description": "Name of the sandbox (must be already started)"
                        },
                        "namespace": {
                            "type": "string",
                            "description": "Namespace of the sandbox"
                        },
                        "code": {
                            "type": "string",
                            "description": "Code to execute"
                        },
                        "language": {
                            "type": "string",
                            "description": "Programming language (e.g., 'python', 'nodejs')"
                        }
                    },
                    "required": ["sandbox", "namespace", "code", "language"]
                }
            },
            {
                "name": "sandbox_run_command",
                "description": "Execute a command in a running sandbox. PREREQUISITES: The target sandbox must be started first using sandbox_start - this will fail if the sandbox is not running. TIMING: Command execution is synchronous and may take time depending on the command complexity. Long-running commands will block until completion or timeout.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "sandbox": {
                            "type": "string",
                            "description": "Name of the sandbox (must be already started)"
                        },
                        "namespace": {
                            "type": "string",
                            "description": "Namespace of the sandbox"
                        },
                        "command": {
                            "type": "string",
                            "description": "Command to execute"
                        },
                        "args": {
                            "type": "array",
                            "items": {"type": "string"},
                            "description": "Command arguments"
                        }
                    },
                    "required": ["sandbox", "namespace", "command"]
                }
            },
            {
                "name": "sandbox_get_metrics",
                "description": "Get metrics and status for sandboxes including CPU usage, memory consumption, and running state. This tool can check the status of any sandbox regardless of whether it's running or not",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "sandbox": {
                            "type": "string",
                            "description": "Optional specific sandbox name to get metrics for"
                        },
                        "namespace": {
                            "type": "string",
                            "description": "Namespace to query (use '*' for all namespaces)"
                        }
                    },
                    "required": ["namespace"]
                }
            }
        ]
    });

    Ok(JsonRpcResponse::success(tools, request.id))
}

/// Handle MCP list prompts request
pub async fn handle_mcp_list_prompts(
    _state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponse> {
    debug!("Handling MCP list prompts request");

    let prompts = json!({
        "prompts": [
            {
                "name": "create_python_sandbox",
                "description": "Create a Python development sandbox",
                "arguments": [
                    {
                        "name": "sandbox_name",
                        "description": "Name for the new sandbox",
                        "required": true
                    },
                    {
                        "name": "namespace",
                        "description": "Namespace for the sandbox",
                        "required": true
                    }
                ]
            },
            {
                "name": "create_node_sandbox",
                "description": "Create a Node.js development sandbox",
                "arguments": [
                    {
                        "name": "sandbox_name",
                        "description": "Name for the new sandbox",
                        "required": true
                    },
                    {
                        "name": "namespace",
                        "description": "Namespace for the sandbox",
                        "required": true
                    }
                ]
            }
        ]
    });

    Ok(JsonRpcResponse::success(prompts, request.id))
}

/// Handle MCP get prompt request
pub async fn handle_mcp_get_prompt(
    _state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponse> {
    debug!("Handling MCP get prompt request");

    let params = request.params.as_object().ok_or_else(|| {
        ServerError::ValidationError(crate::error::ValidationError::InvalidInput(
            "Request parameters must be an object".to_string(),
        ))
    })?;

    let prompt_name = params.get("name").and_then(|v| v.as_str()).ok_or_else(|| {
        ServerError::ValidationError(crate::error::ValidationError::InvalidInput(
            "Missing required 'name' parameter".to_string(),
        ))
    })?;

    let arguments = params.get("arguments").and_then(|v| v.as_object());

    let result = match prompt_name {
        "create_python_sandbox" => {
            let sandbox_name = arguments
                .and_then(|args| args.get("sandbox_name"))
                .and_then(|v| v.as_str())
                .unwrap_or("python-sandbox");
            let namespace = arguments
                .and_then(|args| args.get("namespace"))
                .and_then(|v| v.as_str())
                .unwrap_or("default");

            json!({
                "description": "Create a Python development sandbox",
                "messages": [
                    {
                        "role": "user",
                        "content": {
                            "type": "text",
                            "text": format!(
                                "Create a Python sandbox named '{}' in namespace '{}' using the sandbox_start tool with the following configuration:\n\n\
                                - Image: microsandbox/python\n\
                                - Memory: 512 MiB\n\
                                - CPUs: 1\n\
                                - Working directory: /workspace\n\n\
                                This will set up a Python development environment ready for code execution.",
                                sandbox_name, namespace
                            )
                        }
                    }
                ]
            })
        }
        "create_node_sandbox" => {
            let sandbox_name = arguments
                .and_then(|args| args.get("sandbox_name"))
                .and_then(|v| v.as_str())
                .unwrap_or("node-sandbox");
            let namespace = arguments
                .and_then(|args| args.get("namespace"))
                .and_then(|v| v.as_str())
                .unwrap_or("default");

            json!({
                "description": "Create a Node.js development sandbox",
                "messages": [
                    {
                        "role": "user",
                        "content": {
                            "type": "text",
                            "text": format!(
                                "Create a Node.js sandbox named '{}' in namespace '{}' using the sandbox_start tool with the following configuration:\n\n\
                                - Image: microsandbox/node\n\
                                - Memory: 512 MiB\n\
                                - CPUs: 1\n\
                                - Working directory: /workspace\n\n\
                                This will set up a Node.js development environment ready for JavaScript execution.",
                                sandbox_name, namespace
                            )
                        }
                    }
                ]
            })
        }
        _ => {
            return Err(ServerError::NotFound(format!(
                "Prompt '{}' not found",
                prompt_name
            )));
        }
    };

    Ok(JsonRpcResponse::success(result, request.id))
}

/// Handle MCP call tool request
pub async fn handle_mcp_call_tool(
    state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponse> {
    debug!("Handling MCP call tool request");

    let params = request.params.as_object().ok_or_else(|| {
        ServerError::ValidationError(crate::error::ValidationError::InvalidInput(
            "Request parameters must be an object".to_string(),
        ))
    })?;

    let tool_name = params.get("name").and_then(|v| v.as_str()).ok_or_else(|| {
        ServerError::ValidationError(crate::error::ValidationError::InvalidInput(
            "Missing required 'name' parameter".to_string(),
        ))
    })?;

    let arguments = params.get("arguments").ok_or_else(|| {
        ServerError::ValidationError(crate::error::ValidationError::InvalidInput(
            "Missing required 'arguments' parameter".to_string(),
        ))
    })?;

    // Convert MCP tool calls to our internal JSON-RPC calls
    let internal_method = match tool_name {
        "sandbox_start" => "sandbox.start",
        "sandbox_stop" => "sandbox.stop",
        "sandbox_run_code" => "sandbox.repl.run",
        "sandbox_run_command" => "sandbox.command.run",
        "sandbox_get_metrics" => "sandbox.metrics.get",
        _ => {
            return Err(ServerError::NotFound(format!(
                "Tool '{}' not found",
                tool_name
            )));
        }
    };

    // Create internal JSON-RPC request
    let internal_request = JsonRpcRequest {
        jsonrpc: JSONRPC_VERSION.to_string(),
        method: internal_method.to_string(),
        params: arguments.clone(),
        id: request.id.clone(),
    };

    // Handle the request using our existing infrastructure
    let internal_response = if matches!(internal_method, "sandbox.repl.run" | "sandbox.command.run")
    {
        // These need to be forwarded to the portal
        match forward_rpc_to_portal(state, internal_request).await {
            Ok((_, json_response)) => json_response.0,
            Err(e) => {
                error!("Failed to forward request to portal: {}", e);
                return Ok(JsonRpcResponse::error(
                    JsonRpcError {
                        code: -32603,
                        message: format!("Internal error: {}", e),
                        data: None,
                    },
                    request.id,
                ));
            }
        }
    } else {
        // These are handled locally - call the handler functions directly
        match internal_method {
            "sandbox.start" => {
                let params: SandboxStartParams = serde_json::from_value(arguments.clone())
                    .map_err(|e| {
                        return JsonRpcResponse::error(
                            JsonRpcError {
                                code: -32602,
                                message: format!("Invalid parameters: {}", e),
                                data: None,
                            },
                            request.id.clone(),
                        );
                    })
                    .unwrap();

                match sandbox_start_impl(state, params).await {
                    Ok(result) => JsonRpcResponse::success(json!(result), request.id.clone()),
                    Err(e) => JsonRpcResponse::error(
                        JsonRpcError {
                            code: -32603,
                            message: format!("Sandbox start failed: {}", e),
                            data: None,
                        },
                        request.id.clone(),
                    ),
                }
            }
            "sandbox.stop" => {
                let params: SandboxStopParams = serde_json::from_value(arguments.clone())
                    .map_err(|e| {
                        return JsonRpcResponse::error(
                            JsonRpcError {
                                code: -32602,
                                message: format!("Invalid parameters: {}", e),
                                data: None,
                            },
                            request.id.clone(),
                        );
                    })
                    .unwrap();

                match sandbox_stop_impl(state, params).await {
                    Ok(result) => JsonRpcResponse::success(json!(result), request.id.clone()),
                    Err(e) => JsonRpcResponse::error(
                        JsonRpcError {
                            code: -32603,
                            message: format!("Sandbox stop failed: {}", e),
                            data: None,
                        },
                        request.id.clone(),
                    ),
                }
            }
            "sandbox.metrics.get" => {
                let params: SandboxMetricsGetParams = serde_json::from_value(arguments.clone())
                    .map_err(|e| {
                        return JsonRpcResponse::error(
                            JsonRpcError {
                                code: -32602,
                                message: format!("Invalid parameters: {}", e),
                                data: None,
                            },
                            request.id.clone(),
                        );
                    })
                    .unwrap();

                match sandbox_get_metrics_impl(state, params).await {
                    Ok(result) => JsonRpcResponse::success(json!(result), request.id.clone()),
                    Err(e) => JsonRpcResponse::error(
                        JsonRpcError {
                            code: -32603,
                            message: format!("Get metrics failed: {}", e),
                            data: None,
                        },
                        request.id.clone(),
                    ),
                }
            }
            _ => JsonRpcResponse::error(
                JsonRpcError {
                    code: -32601,
                    message: format!("Method not found: {}", internal_method),
                    data: None,
                },
                request.id.clone(),
            ),
        }
    };

    // Convert the response to MCP format
    let mcp_result = if let Some(result) = internal_response.result {
        json!({
            "content": [
                {
                    "type": "text",
                    "text": serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string())
                }
            ]
        })
    } else if let Some(error) = internal_response.error {
        json!({
            "content": [
                {
                    "type": "text",
                    "text": format!("Error: {}", error.message)
                }
            ],
            "isError": true
        })
    } else {
        json!({
            "content": [
                {
                    "type": "text",
                    "text": "No result returned"
                }
            ]
        })
    };

    Ok(JsonRpcResponse::success(mcp_result, request.id))
}

/// Handle MCP notifications/initialized request
pub async fn handle_mcp_notifications_initialized(
    _state: AppState,
    _request: JsonRpcRequest,
) -> ServerResult<ProcessedNotification> {
    debug!("Handling MCP notifications/initialized");

    // This is a notification - no response is expected
    // The client is indicating it has finished initialization
    Ok(ProcessedNotification::processed())
}

/// Handle MCP methods
pub async fn handle_mcp_method(
    state: AppState,
    request: JsonRpcRequest,
) -> ServerResult<JsonRpcResponseOrNotification> {
    match request.method.as_str() {
        "initialize" => {
            let response = handle_mcp_initialize(state, request).await?;
            Ok(JsonRpcResponseOrNotification::response(response))
        }
        "tools/list" => {
            let response = handle_mcp_list_tools(state, request).await?;
            Ok(JsonRpcResponseOrNotification::response(response))
        }
        "tools/call" => {
            let response = handle_mcp_call_tool(state, request).await?;
            Ok(JsonRpcResponseOrNotification::response(response))
        }
        "prompts/list" => {
            let response = handle_mcp_list_prompts(state, request).await?;
            Ok(JsonRpcResponseOrNotification::response(response))
        }
        "prompts/get" => {
            let response = handle_mcp_get_prompt(state, request).await?;
            Ok(JsonRpcResponseOrNotification::response(response))
        }
        "notifications/initialized" => {
            let notification = handle_mcp_notifications_initialized(state, request).await?;
            Ok(JsonRpcResponseOrNotification::notification(notification))
        }
        _ => Err(ServerError::NotFound(format!(
            "MCP method '{}' not found",
            request.method
        ))),
    }
}