提供通过node-pty实现的完整终端模拟支持的交互式shell会话管理的MCP服务器。
交互式Shell MCP(模型上下文协议)服务器使LLMs能够创建和管理交互式shell会话。它提供了持久的shell环境,在其中可以顺序执行命令并保持状态,类似于人类使用终端的方式。
start_shell_session启动一个新的PTY shell,并返回一个唯一的会话ID。
{ sessionId: string }send_shell_input将输入写入PTY,并自动处理换行符。
sessionId (string):shell的会话IDinput (string):要发送到shell的输入read_shell_output从PTY进程返回输出,支持两种模式:
流模式(默认):返回自上次读取以来的缓冲输出,并清除缓冲区
快照模式:返回当前终端屏幕状态而不清除(适用于top、htop、airodump-ng等应用)
输入:
sessionId (string):shell的会话IDmode (string, 可选):输出模式 - "streaming"(默认)或"snapshot"maxBytes (number, 可选):要返回的最大字节数(默认:100KB,最大:1MB)snapshotSize (number, 可选):要捕获的快照缓冲区大小(默认:50KB)输出:
{
"output": "string",
1. "metadata": {
"mode": "streaming|snapshot",
"totalBytesReceived": number,
"truncated": boolean,
"originalSize": number,
"isSnapshot": boolean,
"snapshotTime": number
}
}
end_shell_session关闭PTY并清理资源。
sessionId (string):要关闭的shell的会话IDnpm install
npm run build
要将此MCP服务器与Claude Desktop或VS Code一起使用,请在您的MCP设置文件中添加以下配置:
在macOS上添加到~/Library/Application Support/Claude/claude_desktop_config.json,或在Windows上添加到%APPDATA%\Claude\claude_desktop_config.json:
{
"mcpServers": {
"Interactive Shell MCP": {
"command": "node",
"args": [
"/path/to/interactive-shell-mcp/dist/server.js"
]
}
}
}
添加到~/.cursor/mcp.json:
{
"mcpServers": {
"Interactive Shell MCP": {
"command": "node",
"args": [
"/path/to/interactive-shell-mcp/dist/server.js"
]
}
}
}
请将/path/to/interactive-shell-mcp替换为您实际安装的路径。
注意:下面的示例演示了LLM如何与这个MCP服务器交互。这些不是可以直接运行的JavaScript代码,而是展示了预期的工具调用模式。
当处理产生大量输出或持续刷新屏幕的命令(如airodump-ng、htop、top)时,使用快照模式:
// 示例展示LLM如何调用这些工具:
// 启动一个会话
const { sessionId } = await start_shell_session();
// 运行airodump-ng
await send_shell_input(sessionId, "sudo airodump-ng wlan0mon");
// 在快照模式下读取输出以获取当前屏幕状态
const result = await read_shell_output(sessionId, {
mode: "snapshot"
});
对于生成流式输出的普通命令:
// 示例展示LLM如何调用这些工具:
// 使用默认的流模式
const output = await read_shell_output(sessionId);
// 或者明确设置非常大输出的大小限制
const output = await read_shell_output(sessionId, {
maxBytes: 50000 // 只返回最后50KB
});
为了独立运行服务器进行调试:
npm start
这将在标准I/O上启动服务器,主要用于测试安装和调试问题。
MIT </中文翻译>