MCP Terminal Server

by cwmoore-ai
MIT License

Integrations

  • Provides a lightweight server implementation that exposes an endpoint to execute shell commands and stream their output in real-time.

  • Leverages Python 3.7+ to provide a Windows service that can execute commands with robust error handling, logging, and automatic process restart capabilities.

  • Enables remote execution of shell commands with streaming output, allowing for running and monitoring command-line operations through an authenticated API.

MCP ターミナルサーバー

Windows サービスとして実行でき、シェル コマンドを実行してその出力をストリーミングするためのエンドポイントを公開する軽量の FastAPI サーバーです。

特徴

  • Windowsサービスとして実行するかデバッグモードで実行する
  • シェルコマンドを実行し、その出力をリアルタイムでストリーミングする
  • APIキー認証
  • レート制限
  • 環境変数または設定ファイルで設定可能
  • 堅牢なエラー処理とログ記録
  • 障害時の自動プロセス再起動

インストール

前提条件

  • Windows オペレーティング システム
  • Python 3.7以上
  • 管理者権限(サービスのインストール用)

インストール手順

  1. このリポジトリをクローンするか、ソースコードをダウンロードしてください
  2. PowerShellを管理者として開く
  3. プロジェクトディレクトリに移動する
  4. インストール スクリプトを実行します。
.\install_service.ps1

これにより、次のようになります。

  • 必要なPython依存関係をインストールする
  • ランダムな API キーを生成する(または提供したキーを使用する)
  • 設定ファイルを作成する
  • Windowsサービスをインストールして開始する

インストールオプション

次のパラメータを使用してインストールをカスタマイズできます。

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

利用可能なパラメータ:

  • -ApiKey : 認証用のカスタムAPIキー
  • -Port : サーバーのポート番号(デフォルト: 8000)
  • -Host : バインドするホストアドレス(デフォルト: 0.0.0.0)
  • -LogLevel : ログレベル (デバッグ、情報、警告、エラー、クリティカル)
  • -ConfigFile : カスタム構成ファイルへのパス

構成

サーバーは以下を通じて構成できます。

  1. 環境変数
  2. 設定ファイル (config.json)
  3. インストール時のコマンドライン引数

設定ファイル

サンプル設定ファイル( config.json.sample )が提供されています。これをconfig.jsonにコピーして変更することができます。

{ "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 }

環境変数

環境変数を使用してサーバーを構成することもできます。

  • MCP_TERMINAL_API_KEY : 認証用のAPIキー
  • MCP_HOST : バインドするホストアドレス
  • MCP_PORT : サーバーのポート番号
  • MCP_LOG_LEVEL : ログレベル
  • MCP_LOG_FILE : ログファイルへのパス
  • MCP_MAX_LOG_SIZE_MB : ログファイルの最大サイズ(MB)
  • MCP_LOG_BACKUP_COUNT : 保存するログバックアップファイルの数
  • MCP_RESTART_ON_FAILURE : 障害時にサーバーを再起動するかどうか
  • MCP_MAX_RESTART_ATTEMPTS : 再起動の最大試行回数
  • MCP_RESTART_DELAY_SECONDS : 再起動の試行間の遅延
  • MCP_SHUTDOWN_TIMEOUT_SECONDS : 正常なシャットダウンのタイムアウト
  • MCP_WORKING_DIRECTORY : サーバーの作業ディレクトリ

サービス管理

サービスコマンド

次のコマンドを使用してサービスを管理できます。

# 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

ログ

ログは設定されたログファイル(デフォルト: mcp_terminal_server.log )に保存されます。ログには以下が含まれます。

  • サービスの開始/停止イベント
  • コマンド実行
  • エラーと警告
  • 健康診断結果

APIの使用

テストクライアント

次の機能を使用してサーバーと対話するための堅牢なテスト クライアントが提供されます。

  • 使いやすさを向上させるコマンドライン引数
  • APIキー認証のサポート
  • 読みやすさを向上させるカラー出力
  • 出力をファイルに保存するサポート
  • 堅牢なエラー処理とログ記録
  • コマンド履歴付きの対話型モード
  • サーバーヘルスチェック機能
基本的な使い方
# 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
設定ファイル

テストクライアントはJSON設定ファイルを使用して設定できます。提供されているサンプルに基づいてclient_config.jsonファイルを作成してください。

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

次に、次のように使用します。

python test_client.py --config client_config.json
環境変数

テスト クライアントは、環境変数による構成もサポートしています。

  • MCP_TERMINAL_API_KEY : 認証用のAPIキー
  • MCP_TERMINAL_SERVER_URL : サーバーURL

APIエンドポイント

  • POST /run : コマンドを実行する
    • X-API-Keyヘッダーに API キーが必要です
    • リクエスト本文: {"command": "your command"}
    • コマンド出力を含むストリーミング応答を返します
  • GET /health : サーバーの健全性をチェックする
    • {"status": "healthy", "active_sessions": 0}を返します

セキュリティに関する考慮事項

  • 本番環境では常にデフォルトのAPIキーを変更する
  • サーバーにローカルからのみアクセスできるようにする場合は、ホストを127.0.0.1に制限します。
  • 本番環境ではHTTPSの使用を検討する
  • サーバーは潜在的に危険なコマンドをブロックしますが、追加のセキュリティ対策が必要になる場合があります。

トラブルシューティング

  • ログファイルでエラーを確認してください
  • コンソール出力を確認するには、デバッグモードで実行します: python service.py debug
  • サービスが実行されていることを確認します: Get-Service -Name "MCP_Terminal_Server"
  • Windowsイベントビューアでサービス関連のエラーを確認する

ライセンス

マサチューセッツ工科大学

-
security - not tested
A
license - permissive license
-
quality - not tested

リアルタイム出力ストリーミングと、API キー認証やレート制限などのセキュリティ機能を備え、Windows 上でシェル コマンドをリモート実行できる軽量の FastAPI サーバーです。

  1. 特徴
    1. インストール
      1. 前提条件
      2. インストール手順
      3. インストールオプション
    2. 構成
      1. 設定ファイル
      2. 環境変数
    3. サービス管理
      1. サービスコマンド
      2. ログ
    4. APIの使用
      1. テストクライアント
      2. APIエンドポイント
    5. セキュリティに関する考慮事項
      1. トラブルシューティング
        1. ライセンス

          Related MCP Servers

          • A
            security
            A
            license
            A
            quality
            A secure shell command execution server implementing the Model Context Protocol (MCP). This server allows remote execution of whitelisted shell commands with support for stdin input.
            Last updated -
            1
            68
            Python
            MIT License
            • Apple
          • A
            security
            F
            license
            A
            quality
            A CLI command execution server that enables running shell commands with structured output, providing detailed execution results including stdout, stderr, exit code, and execution duration.
            Last updated -
            2
            16
            TypeScript
            • Apple
          • -
            security
            A
            license
            -
            quality
            A Model Context Protocol server that enables remote execution of shell commands across different operating systems, automatically handling platform-specific differences between Windows and Unix-like systems.
            Last updated -
            JavaScript
            MIT License
            • Linux
          • -
            security
            A
            license
            -
            quality
            An MCP server that enables secure execution of shell commands across Windows, macOS, and Linux with built-in whitelisting and approval mechanisms for enhanced security.
            Last updated -
            13
            JavaScript
            MIT License
            • Linux
            • Apple

          View all related MCP servers

          ID: k4nfeggzg0