Skip to content

Commit d76fa32

Browse files
authored
feat: add Model Context Protocol (MCP) implementation (#235)
- Add MCP module with JSON-RPC based protocol implementation - Implement MCP server initialization and capabilities - Add tool definitions for sandbox operations (start, stop, metrics, etc.) - Add prompt templates for common sandbox tasks - Integrate with existing sandbox management functions - Make some handler functions public for MCP integration
1 parent 5d93370 commit d76fa32

File tree

4 files changed

+630
-21
lines changed

4 files changed

+630
-21
lines changed

Cargo.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

microsandbox-server/lib/handler.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tracing::{debug, trace, warn};
3030

3131
use crate::{
3232
error::ServerError,
33-
middleware,
33+
mcp, middleware,
3434
payload::{
3535
JsonRpcError, JsonRpcRequest, JsonRpcResponse, RegularMessageResponse,
3636
SandboxMetricsGetParams, SandboxStartParams, SandboxStopParams, JSONRPC_VERSION,
@@ -81,6 +81,24 @@ pub async fn json_rpc_handler(
8181
let method = request.method.as_str();
8282
let id = request.id.clone();
8383

84+
// Check if this is an MCP method first
85+
if mcp::is_mcp_method(method) {
86+
match mcp::handle_mcp_method(state, request).await {
87+
Ok(response) => return Ok((StatusCode::OK, Json(response))),
88+
Err(e) => {
89+
let error = JsonRpcError {
90+
code: -32603,
91+
message: format!("MCP method error: {}", e),
92+
data: None,
93+
};
94+
return Ok((
95+
StatusCode::INTERNAL_SERVER_ERROR,
96+
Json(JsonRpcResponse::error(error, id)),
97+
));
98+
}
99+
}
100+
}
101+
84102
match method {
85103
// Server specific methods
86104
"sandbox.start" => {
@@ -159,7 +177,7 @@ pub async fn json_rpc_handler(
159177
}
160178

161179
/// Forwards the JSON-RPC request to the portal service
162-
async fn forward_rpc_to_portal(
180+
pub async fn forward_rpc_to_portal(
163181
state: AppState,
164182
request: JsonRpcRequest,
165183
) -> ServerResult<(StatusCode, Json<JsonRpcResponse>)> {
@@ -296,7 +314,10 @@ async fn forward_rpc_to_portal(
296314
}
297315

298316
/// Implementation for starting a sandbox
299-
async fn sandbox_start_impl(state: AppState, params: SandboxStartParams) -> ServerResult<String> {
317+
pub async fn sandbox_start_impl(
318+
state: AppState,
319+
params: SandboxStartParams,
320+
) -> ServerResult<String> {
300321
// Validate sandbox name and namespace
301322
validate_sandbox_name(&params.sandbox)?;
302323
validate_namespace(&params.namespace)?;
@@ -704,7 +725,7 @@ async fn poll_sandbox_until_running(
704725
}
705726

706727
/// Implementation for stopping a sandbox
707-
async fn sandbox_stop_impl(state: AppState, params: SandboxStopParams) -> ServerResult<String> {
728+
pub async fn sandbox_stop_impl(state: AppState, params: SandboxStopParams) -> ServerResult<String> {
708729
// Validate sandbox name and namespace
709730
validate_sandbox_name(&params.sandbox)?;
710731
validate_namespace(&params.namespace)?;
@@ -764,7 +785,7 @@ async fn sandbox_stop_impl(state: AppState, params: SandboxStopParams) -> Server
764785
}
765786

766787
/// Implementation for sandbox metrics
767-
async fn sandbox_get_metrics_impl(
788+
pub async fn sandbox_get_metrics_impl(
768789
state: AppState,
769790
params: SandboxMetricsGetParams,
770791
) -> ServerResult<SandboxStatusResponse> {

microsandbox-server/lib/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod config;
1010
pub mod error;
1111
pub mod handler;
1212
pub mod management;
13+
pub mod mcp;
1314
pub mod middleware;
1415
pub mod payload;
1516
pub mod port;
@@ -20,6 +21,7 @@ pub use config::*;
2021
pub use error::*;
2122
pub use handler::*;
2223
pub use management::*;
24+
pub use mcp::*;
2325
pub use middleware::*;
2426
pub use payload::*;
2527
pub use route::*;

0 commit comments

Comments
 (0)