|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { sendSessionMessage } from "../exec/gateway.js"; |
| 4 | +import { destroySession } from "../exec/session.js"; |
| 5 | + |
| 6 | + |
| 7 | +export function createMcpServer(): McpServer { |
| 8 | + const server = new McpServer({ |
| 9 | + name: "firecracker-sandbox", |
| 10 | + version: "1.0.0", |
| 11 | + }); |
| 12 | + |
| 13 | + server.tool( |
| 14 | + "execute", |
| 15 | + "Execute a command inside an isolated Firecracker microVM. " + |
| 16 | + "The workspace persists across calls within the same sessionId.", |
| 17 | + { |
| 18 | + sessionId: z.string().describe("Session identifier for workspace persistence"), |
| 19 | + command: z.string().describe("Command to run: node, python3, bash, etc."), |
| 20 | + args: z.array(z.string()).optional().describe("Command arguments"), |
| 21 | + cwd: z.string().optional().describe("Working directory relative to /workspace"), |
| 22 | + timeout: z.number().optional().describe("Timeout in milliseconds (default 30000)"), |
| 23 | + }, |
| 24 | + async ({ sessionId, command, args, cwd, timeout }) => { |
| 25 | + const parts: string[] = []; |
| 26 | + |
| 27 | + const result = await sendSessionMessage( |
| 28 | + sessionId, |
| 29 | + { type: "execute", command, args, cwd, timeout }, |
| 30 | + (chunk) => { |
| 31 | + parts.push(`[${chunk.stream}] ${chunk.data}`); |
| 32 | + }, |
| 33 | + ); |
| 34 | + |
| 35 | + const exitCode = result.data?.exitCode ?? -1; |
| 36 | + parts.push(`\n--- exit code: ${exitCode} ---`); |
| 37 | + |
| 38 | + return { |
| 39 | + content: [{ type: "text", text: parts.join("") }], |
| 40 | + isError: exitCode !== 0, |
| 41 | + }; |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + server.tool( |
| 46 | + "write_file", |
| 47 | + "Write a file to the session workspace.", |
| 48 | + { |
| 49 | + sessionId: z.string(), |
| 50 | + path: z.string().describe("File path relative to /workspace"), |
| 51 | + content: z.string().describe("File content (will be base64-encoded automatically)"), |
| 52 | + }, |
| 53 | + async ({ sessionId, path, content }) => { |
| 54 | + const encoded = Buffer.from(content).toString("base64"); |
| 55 | + const result = await sendSessionMessage(sessionId, { |
| 56 | + type: "write_file", path, content: encoded, |
| 57 | + }); |
| 58 | + return { |
| 59 | + content: [{ type: "text", text: `Wrote ${result.data?.bytesWritten} bytes to ${path}` }], |
| 60 | + }; |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + server.tool( |
| 65 | + "read_file", |
| 66 | + "Read a file from the session workspace.", |
| 67 | + { |
| 68 | + sessionId: z.string(), |
| 69 | + path: z.string().describe("File path relative to /workspace"), |
| 70 | + }, |
| 71 | + async ({ sessionId, path }) => { |
| 72 | + const result = await sendSessionMessage(sessionId, { |
| 73 | + type: "read_file", path, |
| 74 | + }); |
| 75 | + const content = Buffer.from(result.data?.content || "", "base64").toString("utf-8"); |
| 76 | + return { |
| 77 | + content: [{ type: "text", text: content }], |
| 78 | + }; |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + server.tool( |
| 83 | + "list_files", |
| 84 | + "List files in the session workspace.", |
| 85 | + { |
| 86 | + sessionId: z.string(), |
| 87 | + path: z.string().optional().describe("Directory path relative to /workspace"), |
| 88 | + recursive: z.boolean().optional().describe("List recursively"), |
| 89 | + }, |
| 90 | + async ({ sessionId, path, recursive }) => { |
| 91 | + const result = await sendSessionMessage(sessionId, { |
| 92 | + type: "list_files", path, recursive, |
| 93 | + }); |
| 94 | + const listing = (result.data?.files || []) |
| 95 | + .map((f: any) => `${f.type === "dir" ? "📁" : "📄"} ${f.path} (${f.size}b)`) |
| 96 | + .join("\n"); |
| 97 | + return { |
| 98 | + content: [{ type: "text", text: listing || "(empty)" }], |
| 99 | + }; |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + server.tool( |
| 104 | + "reset_session", |
| 105 | + "Destroy a session and its VM. The workspace is lost.", |
| 106 | + { sessionId: z.string() }, |
| 107 | + async ({ sessionId }) => { |
| 108 | + const destroyed = await destroySession(sessionId); |
| 109 | + return { |
| 110 | + content: [{ |
| 111 | + type: "text", |
| 112 | + text: destroyed ? "Session destroyed." : "No active session found.", |
| 113 | + }], |
| 114 | + }; |
| 115 | + } |
| 116 | + ); |
| 117 | + |
| 118 | + return server; |
| 119 | +} |
0 commit comments