This MCP (Model Context Protocol) server allows Claude to safely execute bash commands within a controlled environment. It provides security safeguards like command whitelisting, directory restrictions, and output sanitization while supporting both stateless and interactive command execution.
To install the MCP server, run the following commands:
npm install
npm run build
The configuration is stored in config/default.json
. You can customize various settings to control security and behavior.
Example configuration:
{
"allowedCommands": ["ls", "cat", "echo", "pwd"],
"allowedDirectories": ["/tmp", "/home"],
"session": {
"timeout": 300,
"maxActiveSessions": 5,
"defaultMode": "stateless"
},
"security": {
"validateCommandsStrictly": true,
"sanitizeOutput": true,
"maxOutputSize": 1048576,
"commandTimeout": 30
},
"logging": {
"level": "info",
"file": "logs/bash-mcp.log",
"maxSize": 10485760,
"maxFiles": 5
}
}
Key configuration options:
You can use Bash MCP either as a library in your code or as a standalone MCP server.
For one-off command execution:
import { executeCommand } from 'bash-mcp';
const result = await executeCommand('ls -la', { cwd: '/home/user' });
console.log(result.output);
For maintaining state across multiple commands:
import { initBashMCP } from 'bash-mcp';
const mcp = await initBashMCP();
// Create a session
const session = mcp.createSession('/home/user');
const sessionId = session.sessionId;
// Execute a command in the session
const result1 = await mcp.executeCommand('ls -la', { sessionId });
console.log(result1.output);
// Send input to the session
const result2 = await mcp.sendInput({ sessionId, input: 'echo "Hello, world!"' });
console.log(result2.output);
// Close the session when done
mcp.closeSession(sessionId);
To start the MCP server for use with Claude Desktop or other MCP clients:
# Start the TypeScript MCP server
npm run mcp
# Start the JavaScript MCP server
npm run mcp-js
# Start with MCP Inspector for debugging
npm run inspector
While using the MCP server, follow these security best practices:
The security features include:
These safeguards help ensure that AI assistants can only execute approved commands in designated locations.
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "bash-mcp" '{"command":"npm","args":["run","mcp"]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"bash-mcp": {
"command": "npm",
"args": [
"run",
"mcp"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"bash-mcp": {
"command": "npm",
"args": [
"run",
"mcp"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect