Terminal MCP Server

by weidwonder
Verified

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Enables cloning the terminal-mcp-server repository to install and build the server

  • Enables command execution on Linux hosts, both locally and remotely via SSH

  • Supports using tmux for running long-running commands in persistent sessions on local or remote hosts

终端 MCP 服务器

中文詳細

终端 MCP 服务器是一个模型上下文协议 (MCP) 服务器,允许在本地或远程主机上执行命令。它为 AI 模型和其他应用程序提供了一个简单而强大的接口,可以在本地机器上或通过 SSH 在远程主机上执行系统命令。

特征

  • 本地命令执行:直接在本地机器上执行命令
  • 远程命令执行:通过 SSH 在远程主机上执行命令
  • 会话持久性:支持在指定时间内重复使用相同终端环境的持久会话(默认 20 分钟)
  • 环境变量:为命令设置自定义环境变量
  • 多种连接方式:通过 stdio 或 SSE(服务器发送事件)连接

安装

通过 Smithery 安装

要通过Smithery自动为 Claude Desktop 安装终端 mcp 服务器:

npx -y @smithery/cli install @weidwonder/terminal-mcp-server --client claude

手动安装

# Clone the repository git clone https://github.com/weidwonder/terminal-mcp-server.git cd terminal-mcp-server # Install dependencies npm install # Build the project npm run build

用法

启动服务器

# Start the server using stdio (default mode) npm start # Or run the built file directly node build/index.js

以 SSE 模式启动服务器

SSE(服务器发送事件)模式允许您通过 HTTP 远程连接到服务器。

# Start the server in SSE mode npm run start:sse # Or run the built file directly with SSE flag node build/index.js --sse

您可以使用以下命令行选项自定义 SSE 服务器:

选项描述默认
--port-p监听的端口8080
--endpoint-e端点路径/sse
--host-h绑定到的主机本地主机

自定义选项的示例:

# Start SSE server on port 3000, endpoint /mcp, and bind to all interfaces node build/index.js --sse --port 3000 --endpoint /mcp --host 0.0.0.0

这将启动服务器并监听http://0.0.0.0:3000/mcp上的 SSE 连接。

使用 MCP Inspector 进行测试

# Start the MCP Inspector tool npm run inspector

execut_command工具

execute_command 工具是终端 MCP 服务器提供的核心功能,用于在本地或远程主机上执行命令。

参数

范围类型必需的描述
命令细绳是的要执行的命令
主持人细绳要连接的远程主机。如果未提供,则命令将在本地执行。
用户名细绳指定主机时必填SSH 连接的用户名
会议细绳会话名称,默认为“default”。相同的会话名称将重复使用相同的终端环境 20 分钟
环境目的环境变量,默认为空对象

示例

本地执行命令

{ "command": "ls -la", "session": "my-local-session", "env": { "NODE_ENV": "development" } }

在远程主机上执行命令

{ "host": "example.com", "username": "user", "command": "ls -la", "session": "my-remote-session", "env": { "NODE_ENV": "production" } }

使用 AI 助手进行配置

使用 Roo 代码进行配置

  1. 打开 VSCode 并安装 Roo Code 扩展
  2. 打开 Roo Code 设置文件: ~/Library/Application Support/Code/User/globalStorage/rooveterinaryinc.roo-cline/settings/cline_mcp_settings.json
  3. 添加以下配置:

对于 stdio 模式(本地连接)

{ "mcpServers": { "terminal-mcp": { "command": "node", "args": ["/path/to/terminal-mcp-server/build/index.js"], "env": {} } } }

对于 SSE 模式(远程连接)

{ "mcpServers": { "terminal-mcp-sse": { "url": "http://localhost:8080/sse", "headers": {} } } }

如果您已自定义,请将localhost:8080/sse替换为您的实际服务器地址、端口和端点。

使用 Cline 配置

  1. 打开 Cline 设置文件: ~/.cline/config.json
  2. 添加以下配置:

对于 stdio 模式(本地连接)

{ "mcpServers": { "terminal-mcp": { "command": "node", "args": ["/path/to/terminal-mcp-server/build/index.js"], "env": {} } } }

对于 SSE 模式(远程连接)

{ "mcpServers": { "terminal-mcp-sse": { "url": "http://localhost:8080/sse", "headers": {} } } }

使用 Claude Desktop 进行配置

  1. 打开 Claude Desktop 设置文件: ~/Library/Application Support/Claude/claude_desktop_config.json
  2. 添加以下配置:

对于 stdio 模式(本地连接)

{ "mcpServers": { "terminal-mcp": { "command": "node", "args": ["/path/to/terminal-mcp-server/build/index.js"], "env": {} } } }

对于 SSE 模式(远程连接)

{ "mcpServers": { "terminal-mcp-sse": { "url": "http://localhost:8080/sse", "headers": {} } } }

最佳实践

命令执行

  • 在运行命令之前,最好确定系统类型(Mac、Linux 等)
  • 使用完整路径来避免与路径相关的问题
  • 对于需要维护环境的命令序列,使用&&连接多个命令
  • 对于长时间运行的命令,请考虑使用nohupscreen / tmux

SSH 连接

  • 确保设置了基于 SSH 密钥的身份验证
  • 如果连接失败,请检查密钥文件是否存在(默认路径: ~/.ssh/id_rsa
  • 确保远程主机上正在运行 SSH 服务

会话管理

  • 使用 session 参数来维护相关命令之间的环境
  • 对于需要特定环境的操作,使用相同的会话名称
  • 请注意,会话将在 20 分钟不活动后自动关闭

错误处理

  • 命令执行结果包括stdout和stderr
  • 检查 stderr 以确定命令是否执行成功
  • 对于复杂的操作,添加验证步骤以确保成功

重要提示

  • 对于远程命令执行,必须提前设置基于 SSH 密钥的身份验证
  • 对于本地命令执行,命令将在启动服务器的用户上下文中运行
  • 会话超时为20分钟,超过20分钟后连接将自动关闭
ID: wfkm7d2k2u