终端MCP服务器

一个MCP服务器,允许AI模型通过SSH在本地机器或远程主机上执行系统命令,支持持久会话和环境变量。

os-automationdeveloper-tools

746 查看 · 2026-07-07 更新

简介

一个MCP服务器,允许AI模型通过SSH在本地机器或远程主机上执行系统命令,支持持久会话和环境变量。

简介

一个MCP服务器,允许AI模型通过SSH在本地机器或远程主机上执行系统命令,支持持久会话和环境变量。

终端 MCP 服务器

smithery 徽章

中文文档

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

特性

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

安装

通过 Smithery 安装

要通过 Smithery 自动为 Claude Desktop 安装 terminal-mcp-server:

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(Server-Sent Events)模式允许您通过 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绑定的主机localhost

带自定义选项的示例:

# 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

execute_command 工具

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

参数

参数类型必需描述
command字符串要执行的命令
host字符串要连接的远程主机。如果未提供,则命令将在本地执行
username字符串当指定了 host 时必需SSH 连接的用户名
session字符串会话名称,默认为 "default"。相同的会话名称将在20分钟内重用相同的终端环境
env对象环境变量,默认为空对象

示例

本地执行命令

{ "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 Code 配置

  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 服务正在运行

会话管理

  • 使用会话参数来保持相关命令之间的环境
  • 对于需要特定环境的操作,使用相同的会话名称
  • 请注意,如果在 20 分钟内没有活动,会话将自动关闭

错误处理

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

重要注意事项

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

服务配置

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

来源