MCP Terminal Server

MCP Terminal Server

A lightweight FastAPI server that allows remote execution of shell commands on Windows, with real-time output streaming and security features like API key authentication and rate limiting.

Category
访问服务器

README

MCP Terminal Server

A lightweight FastAPI server that can run as a Windows service and exposes an endpoint to execute shell commands and stream their output.

Features

  • Run as a Windows service or in debug mode
  • Execute shell commands and stream their output in real-time
  • API key authentication
  • Rate limiting
  • Configurable via environment variables or configuration file
  • Robust error handling and logging
  • Automatic process restart on failure

Installation

Prerequisites

  • Windows operating system
  • Python 3.7 or higher
  • Administrator privileges (for service installation)

Install Steps

  1. Clone this repository or download the source code
  2. Open PowerShell as Administrator
  3. Navigate to the project directory
  4. Run the installation script:
.\install_service.ps1

This will:

  • Install required Python dependencies
  • Generate a random API key (or use the one you provide)
  • Create a configuration file
  • Install and start the Windows service

Installation Options

You can customize the installation with the following parameters:

.\install_service.ps1 -ApiKey "your-api-key" -Port 8080 -Host "127.0.0.1" -LogLevel "debug"

Available parameters:

  • -ApiKey: Custom API key for authentication
  • -Port: Port number for the server (default: 8000)
  • -Host: Host address to bind to (default: 0.0.0.0)
  • -LogLevel: Log level (debug, info, warning, error, critical)
  • -ConfigFile: Path to a custom configuration file

Configuration

The server can be configured through:

  1. Environment variables
  2. Configuration file (config.json)
  3. Command-line arguments during installation

Configuration File

A sample configuration file (config.json.sample) is provided. You can copy this to config.json and modify it:

{
    "host": "0.0.0.0",
    "port": 8000,
    "log_level": "info",
    "log_file": "mcp_terminal_server.log",
    "max_log_size_mb": 10,
    "log_backup_count": 5,
    "restart_on_failure": true,
    "max_restart_attempts": 5,
    "restart_delay_seconds": 10,
    "shutdown_timeout_seconds": 30,
    "working_directory": null
}

Environment Variables

You can also configure the server using environment variables:

  • MCP_TERMINAL_API_KEY: API key for authentication
  • MCP_HOST: Host address to bind to
  • MCP_PORT: Port number for the server
  • MCP_LOG_LEVEL: Log level
  • MCP_LOG_FILE: Path to log file
  • MCP_MAX_LOG_SIZE_MB: Maximum log file size in MB
  • MCP_LOG_BACKUP_COUNT: Number of log backup files to keep
  • MCP_RESTART_ON_FAILURE: Whether to restart the server on failure
  • MCP_MAX_RESTART_ATTEMPTS: Maximum number of restart attempts
  • MCP_RESTART_DELAY_SECONDS: Delay between restart attempts
  • MCP_SHUTDOWN_TIMEOUT_SECONDS: Timeout for graceful shutdown
  • MCP_WORKING_DIRECTORY: Working directory for the server

Service Management

Service Commands

The service can be managed using the following commands:

# Install the service
python service.py install

# Start the service
python service.py start

# Stop the service
python service.py stop

# Restart the service
python service.py restart

# Remove the service
python service.py remove

# Update the service configuration
python service.py update

# Run in debug mode (not as a service)
python service.py debug

Logs

Logs are stored in the configured log file (default: mcp_terminal_server.log). The logs include:

  • Service start/stop events
  • Command execution
  • Errors and warnings
  • Health check results

API Usage

Test Client

A robust test client is provided to interact with the server with the following features:

  • Command-line arguments for better usability
  • API key authentication support
  • Colored output for better readability
  • Support for saving output to a file
  • Robust error handling and logging
  • Interactive mode with command history
  • Server health check functionality

Basic Usage

# Run a command
python test_client.py -c "dir" -k "your-api-key"

# Check server health
python test_client.py --health

# Interactive mode
python test_client.py -k "your-api-key"

# Save output to a file
python test_client.py -c "systeminfo" --save-output output.txt

# Specify server URL
python test_client.py -s "http://example.com:8000" -c "dir"

# Enable verbose output
python test_client.py -v -c "dir"

# Show version
python test_client.py --version

Configuration File

The test client can be configured using a JSON configuration file. Create a client_config.json file based on the provided sample:

{
    "server_url": "http://localhost:8000",
    "api_key": "your-api-key-here",
    "timeout": 60,
    "verbose": false
}

Then use it with:

python test_client.py --config client_config.json

Environment Variables

The test client also supports configuration via environment variables:

  • MCP_TERMINAL_API_KEY: API key for authentication
  • MCP_TERMINAL_SERVER_URL: Server URL

API Endpoints

  • POST /run: Execute a command

    • Requires API key in X-API-Key header
    • Request body: {"command": "your command"}
    • Returns streaming response with command output
  • GET /health: Check server health

    • Returns {"status": "healthy", "active_sessions": 0}

Security Considerations

  • Always change the default API key in production
  • Restrict the host to 127.0.0.1 if the server should only be accessible locally
  • Consider using HTTPS in production
  • The server blocks potentially dangerous commands, but additional security measures may be needed

Troubleshooting

  • Check the log file for errors
  • Run in debug mode to see console output: python service.py debug
  • Verify the service is running: Get-Service -Name "MCP_Terminal_Server"
  • Check Windows Event Viewer for service-related errors

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选