Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/bash-mcp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@capsule-run/bash-mcp",
"version": "0.1.0",
"description": "MCP server exposing sandboxed bash made for agents",
"type": "module",
"main": "./src/index.ts",
"types": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"bin": {
"bash-mcp": "./src/index.ts"
},
"scripts": {
"start": "node --import tsx/esm src/index.ts",
"dev": "tsx watch src/index.ts",
"build": "tsup src/index.ts --format esm --dts"
},
"license": "Apache-2.0",
"packageManager": "pnpm@10.21.0",
"devDependencies": {
"tsup": "^8.0.0",
"tsx": "^4.0.0"
},
"dependencies": {
"@capsule-run/bash": "workspace:*",
"@capsule-run/bash-types": "workspace:*",
"@capsule-run/bash-wasm": "workspace:*",
"@modelcontextprotocol/sdk": "^1.10.2",
"zod": "^3.25.0"
}
}
106 changes: 106 additions & 0 deletions packages/bash-mcp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { Bash } from "@capsule-run/bash";
import { WasmRuntime } from "@capsule-run/bash-wasm";


const sessions = new Map<string, Bash>();

function getSession(sessionId: string): Bash {
if (!sessions.has(sessionId)) {
sessions.set(sessionId, new Bash({ runtime: new WasmRuntime(), hostWorkspace: `.capsule/sessions/${sessionId}` }));
}
return sessions.get(sessionId)!;
}


const server = new McpServer({
name: "@capsule-run/bash-mcp",
version: "0.1.0",
});

server.registerTool(
"run",
{
description: "Run a bash command inside the sandboxed Capsule environment. Returns stdout, stderr, exit code, filesystem diff, and current shell state (cwd + env).",
inputSchema: {
command: z.string().describe("The bash command to execute."),
session_id: z
.string()
.optional()
.default("default")
.describe(
"Identifier for the shell session. Commands within the same session share cwd, env, and filesystem state. Defaults to 'default'."
),
},
},
async ({ command, session_id }) => {
const bash = getSession(session_id ?? "default");
const result = await bash.run(command);

const text = JSON.stringify(
{
stdout: result.stdout,
stderr: result.stderr,
exitCode: result.exitCode,
diff: result.diff ?? null,
state: result.state ?? null,
},
null,
2
);

return {
content: [{ type: "text", text }],
isError: result.exitCode !== 0,
};
}
);

server.registerTool(
"reset",
{
description: "Reset the sandboxed filesystem and shell state (cwd, env vars) for a session back to their initial values.",
inputSchema: {
session_id: z
.string()
.optional()
.default("default")
.describe("The session to reset. Defaults to 'default'."),
},
},
async ({ session_id }) => {
const bash = getSession(session_id ?? "default");
bash.reset();

return {
content: [
{
type: "text",
text: JSON.stringify({ ok: true, session_id: session_id ?? "default" }),
},
],
};
}
);

server.registerTool(
"sessions",
{
description: "List all active shell sessions.",
},
async () => {
return {
content: [
{
type: "text",
text: JSON.stringify({ sessions: Array.from(sessions.keys()) }),
},
],
};
}
);

const transport = new StdioServerTransport();
await server.connect(transport);
8 changes: 8 additions & 0 deletions packages/bash-mcp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
Loading
Loading