Skip to main content
Glama
README.md
# ๐Ÿงฉ Terminal MCP

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Version](https://img.shields.io/badge/version-2.1.0-blue.svg)](https://github.com/yourusername/terminal-mcp)
[![Node.js](https://img.shields.io/badge/node-%3E%3D16.0.0-brightgreen.svg)](https://nodejs.org/)
[![Contributions Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg)](CONTRIBUTING.md)

A production-grade Model Context Protocol (MCP) server with **PTY-native execution** designed specifically for **Cursor IDE**. Provides a unified interactive terminal for Cursor's AI assistant and humans, enabling seamless command execution in shared terminal environments.

## โœจ Features

- **PTY-native execution** - Commands run directly in PTY terminals (single source of truth)
- **Multi-project support** - On-demand PTY per project root with shared state
- **Real environment** - AWS SSO, exports, credentials all work correctly
- **Shared visibility** - You and AI assistants see the exact same terminal
- **HTTP transport** - Deploy anywhere, access from any machine
- **WebSocket support** - Real-time terminal attachment and monitoring
- **Context-aware execution** - Enhanced tools that provide terminal context before execution
- **Daemon mode** - Run as a background service with CLI management
- **Graceful shutdown** - Proper cleanup of PTY processes and connections

## ๐Ÿ“‹ Prerequisites

- Node.js (v16 or higher)
- A Unix-like system (macOS, Linux) with PTY support

## ๐Ÿš€ Quick Start

### Installation

```bash
npm install -g terminal-mcp
```

Or clone and install locally:

```bash
git clone https://github.com/yourusername/terminal-mcp.git
cd terminal-mcp
npm install
```

### Basic Usage

1. Start the daemon:

```bash
npm run daemon
# Or run in background:
npm run daemon:bg
```

2. Configure Cursor MCP settings (**Cursor โ†’ Settings โ†’ Features โ†’ Model Context Protocol**):

```json
{
  "mcpServers": {
    "pty-terminal": {
      "url": "http://localhost:3333/mcp"
    }
  }
}
```

3. Restart Cursor

### CLI Commands

```bash
# Start daemon
npm run daemon

# Start daemon in background
npm run daemon:bg

# Check status
npm run status

# List active terminals
npm run list

# Stop daemon
npm run stop
```

## ๐Ÿ—๏ธ Architecture

```
Cursor/AI Assistant
  โ”‚
  โ–ผ
MCP HTTP Server (Express)
  โ”‚
  โ”œโ”€ HTTP Transport (JSON + SSE)
  โ”œโ”€ WebSocket Transport (real-time)
  โ”‚
  โ–ผ
PTY Manager
  โ”‚
  โ”œโ”€ On-demand PTY creation per project root
  โ”œโ”€ Shared PTY state (env vars, cwd, auth tokens)
  โ”œโ”€ Terminal Event Journal (output history)
  โ”‚
  โ–ผ
PTY Processes (node-pty)
  โ”‚
  โ–ผ
Real Shell Environment
```

**Key Components:**
- **PTY Manager**: Manages multiple PTY instances, one per project root
- **Terminal Event Journal**: Captures and stores terminal output history
- **MCP Tools**: `execute`, `execute2`, `interrupt`, `list_terminals`, `read_terminal`
- **HTTP Server**: Express-based server with MCP transport and WebSocket support

## ๐Ÿ“ How It Works

1. MCP server starts and creates a default PTY for the default project root
2. AI assistants send commands via the MCP `execute` or `execute2` tool
3. Commands are executed in the appropriate PTY (based on project root)
4. Output is captured via PTY event listeners and stored in the event journal
5. Results are returned to the AI assistant via MCP
6. Humans can attach to terminals via WebSocket or CLI to monitor activity

## ๐ŸŽฏ Usage Examples

### For AI Assistants (Automatic)

AI assistants automatically use this terminal for all commands. Just ask:

- "Run tests"
- "Install dependencies"
- "Start the server"
- "Check the logs"
- "Build the project"

### For Humans (Manual Terminal Access)

Attach to a terminal via WebSocket:

```bash
# Using the CLI
mcp-terminal attach

# Or connect directly via WebSocket
wscat -c ws://localhost:3333/attach?projectRoot=/path/to/project
```

## ๐Ÿ”ง Configuration

### Environment Variables

- `PORT`: Server port (default: 3333)
- `PROJECT_ROOT`: Default project root path (default: home directory)
- `SHELL`: Shell to use for PTY (default: `/bin/zsh`)

### Project-Specific Terminals

The server automatically creates separate PTY instances for different project roots. Specify the project root when calling MCP tools:

```json
{
  "command": "npm test",
  "projectRoot": "/path/to/my/project"
}
```

## ๐Ÿš€ Deployment

### Docker

```bash
docker build -t terminal-mcp .
docker run -p 3333:3333 \
  -e PROJECT_ROOT=/app \
  -e PORT=3333 \
  terminal-mcp
```

### Cloud Platforms

Works on:
- **AWS EC2** / **GCP VM** / **DigitalOcean**
- **Cloud Run** / **Fly.io** / **Render**
- **Docker Compose** / **Kubernetes**

All support HTTP + SSE transport.

## ๐Ÿ”’ Security

**Important Security Considerations:**

- โš ๏ธ **No authentication by default** - Add authentication for production use
- โš ๏ธ **No rate limiting** - Implement rate limiting for public deployments
- โš ๏ธ **Full command execution access** - Commands run with the same privileges as the daemon user

**For production, add:**
- API key header authentication
- IP allowlist
- Command denylist/filtering
- Read-only mode toggle
- TLS/HTTPS encryption

See [SECURITY.md](SECURITY.md) for more information.

## ๐Ÿงช Development

### Setup

```bash
git clone https://github.com/yourusername/terminal-mcp.git
cd terminal-mcp
npm install
```

### Running Tests

```bash
npm test
```

### Code Quality

```bash
# Linting (if configured)
npm run lint

# Formatting (if configured)
npm run format
```

See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed development guidelines.

## ๐Ÿ“š API Reference

### MCP Tools

#### `execute`
Execute a command in the shared PTY terminal.

**Parameters:**
- `command` (required): Shell command to execute
- `projectRoot` (optional): Project root path

#### `execute2`
Context-aware execution that shows terminal history before executing.

**Parameters:**
- `command` (required): Shell command to execute
- `projectRoot` (optional): Project root path
- `contextChars` (optional): Maximum characters of context to show (default: 2000)

#### `interrupt`
Send Ctrl+C to interrupt the currently running command.

**Parameters:**
- `projectRoot` (optional): Project root path

#### `list_terminals`
List all active PTY terminals managed by the server.

#### `read_terminal`
Read terminal output history without executing any command.

**Parameters:**
- `projectRoot` (optional): Project root path
- `maxChars` (optional): Maximum characters to return (default: 8000)
- `mode` (optional): Read mode - `tail`, `since_last`, or `full` (default: `tail`)

### HTTP Endpoints

- `GET /health` - Health check endpoint
- `POST /mcp` - MCP protocol endpoint
- `GET /terminals` - List active terminals
- `WS /attach` - WebSocket endpoint for terminal attachment

## ๐Ÿ› ๏ธ Troubleshooting

### Terminal not found

The server automatically creates PTY instances on demand. If you encounter issues:

1. Check that the daemon is running: `npm run status`
2. Verify the project root path exists
3. Check server logs for errors

### Commands not executing

Make sure:
1. The MCP server is configured correctly in Cursor settings
2. The PROJECT_ROOT path is absolute and correct
3. You've restarted Cursor after configuration
4. The daemon is running and accessible

### Check active terminals

```bash
npm run list
# Or via HTTP:
curl http://localhost:3333/terminals
```

## ๐Ÿค Contributing

We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

- ๐Ÿ› Found a bug? [Open an issue](https://github.com/yourusername/terminal-mcp/issues)
- ๐Ÿ’ก Have an idea? [Start a discussion](https://github.com/yourusername/terminal-mcp/discussions)
- ๐Ÿ“ Want to contribute code? [Read the contributing guide](CONTRIBUTING.md)

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

- Built with [Model Context Protocol](https://modelcontextprotocol.io/)
- Uses [node-pty](https://github.com/microsoft/node-pty) for PTY support
- Inspired by the need for seamless AI-human terminal collaboration

## ๐Ÿ“ž Support

- ๐Ÿ“– [Documentation](docs/)
- ๐Ÿ’ฌ [Discussions](https://github.com/yourusername/terminal-mcp/discussions)
- ๐Ÿ› [Issue Tracker](https://github.com/yourusername/terminal-mcp/issues)
- ๐Ÿ”’ [Security Issues](SECURITY.md)

---

Made with โค๏ธ for the open source community