npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@riant/docker-bash-mcp

v1.0.0

Published

MCP for interactive Docker container sessions with persistent shell access

Downloads

9

Readme

Docker Bash MCP

一个用于与 Docker 容器进行交互式会话的 MCP (Model Context Protocol) 服务器。

为什么需要这个 MCP?

现有的 Docker MCP 工具大多只能执行一次性命令(如 docker exec ... -c "command"),每次执行都是独立的会话,无法保持状态。

本 MCP 的核心优势:

  • 持久会话 - 创建会话后可以持续交互,工作目录、环境变量等状态都会保持
  • 像人工操作 - AI 可以像真人一样进入容器,执行一系列命令,然后退出
  • 简单直接 - 5 个工具,逻辑清晰,易于使用

安装与配置

方式一:使用 npx(推荐)

无需手动 clone 和安装依赖,直接在 MCP 客户端配置:

{
  "mcpServers": {
    "docker-bash": {
      "command": "npx",
      "args": ["-y", "@riant/docker-bash-mcp"]
    }
  }
}

npx 会自动下载包并安装依赖,开箱即用。

方式二:手动安装

git clone https://github.com/Riant/docker-bash-mcp.git
cd docker-bash-mcp
npm install

配置:

{
  "mcpServers": {
    "docker-bash": {
      "command": "node",
      "args": ["/path/to/docker-bash-mcp/index.js"]
    }
  }
}

工具列表

1. docker_create_session

创建与 Docker 容器的交互会话。

参数: | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | container_id | string | 是 | 容器 ID 或名称 | | shell | string | 否 | 使用的 shell,默认 /bin/bash | | user | string | 否 | 以指定用户运行 |

返回:

{
  "success": true,
  "session": {
    "sessionId": "xxx-xxx-xxx",
    "containerId": "my-container",
    "shell": "/bin/bash"
  }
}

2. docker_execute

在会话中执行命令。命令会在会话的当前工作目录下执行。

参数: | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | session_id | string | 是 | 会话 ID | | command | string | 是 | 要执行的命令 |

3. docker_read_output

读取命令执行的输出。

参数: | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | session_id | string | 是 | 会话 ID | | clear | boolean | 否 | 读取后清空缓冲区,默认 true |

返回:

{
  "output": "命令输出内容",
  "lastActivity": "2024-01-01T00:00:00.000Z",
  "exited": false
}

4. docker_close_session

关闭并销毁会话。

参数: | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | session_id | string | 是 | 会话 ID |

5. docker_list_sessions

列出所有活跃的会话。

使用示例

AI: 我来帮你操作容器

// 1. 创建会话
docker_create_session({ container_id: "my-container" })
// 返回: { sessionId: "abc-123" }

// 2. 切换目录
docker_execute({ session_id: "abc-123", command: "cd /home/app" })
docker_read_output({ session_id: "abc-123" })

// 3. 查看文件(工作目录保持在 /home/app)
docker_execute({ session_id: "abc-123", command: "ls -la" })
docker_read_output({ session_id: "abc-123" })

// 4. 查看 Node 版本
docker_execute({ session_id: "abc-123", command: "node -v" })
docker_read_output({ session_id: "abc-123" })
// 输出: v20.18.1

// 5. 完成任务,关闭会话
docker_close_session({ session_id: "abc-123" })

技术实现

  • 使用 node-pty 创建伪终端 (PTY) 进程
  • 通过 docker exec -it 进入容器的交互式 shell
  • 会话状态通过 PTY 进程保持

依赖

  • Node.js >= 16
  • Docker
  • @modelcontextprotocol/sdk
  • node-pty
  • uuid

License

MIT