MCP Docker Server

MCP Docker Server

Enables secure Docker command execution from isolated environments like containers through MCP protocol. Provides tools for managing Docker containers, images, and Docker Compose services with security validation and async operation support.

Category
访问服务器

README

MCP Docker Server

A Python-based MCP (Model Context Protocol) server that provides secure Docker command execution for language models or other clients running in isolated environments like containers.

Overview

This MCP server runs on a host system and provides access to its Docker daemon via the MCP protocol. It implements security filtering and command validation to ensure safe operation, only allowing docker and docker-compose commands to be executed.

Features

  • Docker Command Execution: Executes docker and docker-compose commands on the host.
  • Docker Compose Support: Handles both legacy docker-compose and modern docker compose syntax.
  • Security Validation: An explicit allowlist restricts executable commands to docker and docker-compose only.
  • Async I/O: Built with asyncio for non-blocking command execution.
  • Multiple Transports: Supports stdio, sse, and streamable-http MCP transports.
  • Rich Toolset: Provides specific tools for common operations like listing containers, images, and checking system info.
  • Error Handling: Returns detailed error messages for failed commands.

Architecture

┌─────────────────┐    MCP Protocol     ┌──────────────┐
│ Client          │ ─────────────────► │ MCP Server   │
│ (e.g. Container)│                     │ (Host)       │
│                 │ ◄─────────────────  │              │
│ - Calls Tools   │    Command Results  │ - Docker CLI │
└─────────────────┘                     │ - Security   │
                                        └──────────────┘
                                               │
                                               ▼
                                        ┌──────────────┐
                                        │ Docker Daemon│
                                        │ (Host)       │
                                        └──────────────┘

Installation

  1. Python: Requires Python 3.12 or newer.
  2. Docker: Docker must be installed and the daemon must be running on the host system.
  3. Dependencies: Install the required Python packages using uv or pip from pyproject.toml.
    # Using uv
    uv pip install -r requirements.txt
    
    # Or directly from the pyproject.toml
    uv pip install .
    

Usage

Starting the Server

The server can be started with different MCP transports.

# Start with the default stdio transport
uv run main.py

# Start with the Streamable HTTP transport
uv run main.py streamable-http

# Start with the SSE (Server-Sent Events) transport
uv run main.py sse

Environment Variables

Configure the server's network binding with these environment variables.

  • FASTMCP_HOST: Server bind address (default: 0.0.0.0 for container access).
  • FASTMCP_PORT: Server port (default: 3000).

You can create a .env file to manage these variables:

FASTMCP_HOST=0.0.0.0
FASTMCP_PORT=3000

Then run the server with:

# Example with a custom .env file
uv run --env-file=.env main.py streamable-http

MCP Protocol

The server uses the mcp library to expose tools. The available transports are stdio, sse, and streamable-http.

Available Methods (Tools)

The following tools are exposed by the server:

  • execute_docker_command(command: str, working_directory: str = None) -> str

    • Description: Executes a general Docker or Docker Compose command. The command is validated against an allowlist.
    • Example: session.call_tool("execute_docker_command", {"command": "docker ps -a"})
  • docker_system_info() -> str

    • Description: Retrieves Docker version and system disk usage information.
    • Example: session.call_tool("docker_system_info", {})
  • list_containers(all: bool = False) -> str

    • Description: Lists Docker containers.
    • Args: all (boolean) - If true, shows all containers (including stopped ones). Defaults to False.
    • Example: session.call_tool("list_containers", {"all": True})
  • list_images(all: bool = False) -> str

    • Description: Lists Docker images.
    • Args: all (boolean) - If true, shows all images (including intermediate ones). Defaults to False.
    • Example: session.call_tool("list_images", {})
  • docker_compose_status(working_directory: str = None) -> str

    • Description: Gets the status of services defined in a docker-compose.yml file.
    • Args: working_directory (string) - The path to the directory containing the compose file.
    • Example: session.call_tool("docker_compose_status", {"working_directory": "/path/to/project"})

Security Features

Command Validation

The server uses an allowlist-based approach for security. The is_allowed_command method in main.py ensures that only commands beginning with docker or docker-compose (including docker compose) are processed. All other commands are rejected.

Error Handling

If a command fails to execute, the server captures stdout, stderr, and the exit code, returning a detailed error message to the client. The execute_command function contains a try...except block to handle exceptions during subprocess execution.

Troubleshooting

Common Issues

  1. Docker Not Available:

    • Ensure Docker is installed: docker --version
    • Check that the Docker daemon is running: docker info
    • Verify your user has permissions to access the Docker socket.
  2. Port Already in Use:

    • If you see an error like address already in use, the port (default 3000) is occupied.
    • Change the port using the FASTMCP_PORT environment variable.
    • Find the process using the port: lsof -i :3000 or netstat -tulnp | grep 3000.
  3. Permission Denied (Docker Socket):

    • If you get a "permission denied" error when running Docker commands, add your user to the docker group: sudo usermod -aG docker $USER.
    • You will need to start a new shell session for this change to take effect.
  4. Connection Refused:

    • Verify the MCP server is running and listening on the correct host and port.
    • Check for firewall rules that might be blocking the connection.
    • Ensure your client configuration matches the server's FASTMCP_HOST and FASTMCP_PORT.

Development

Project Structure

/workspace/mcp_docker/
├── .python-version
├── main.py           # Main server implementation
├── pyproject.toml    # Project metadata and dependencies
├── README.md         # This documentation
└── test/
    └── test_client.py # Example client for testing

Testing

Run the example test client to connect to a running server, list its tools, and execute a sample command.

# Make sure the server is running in another terminal
# Then run the client
uv run test/test_client.py

License

This project is open-source and available for modification and distribution.

Support

For issues and questions:

  1. Check the Troubleshooting section.
  2. Verify your Docker installation and permissions.
  3. Review the server logs for error details.
  4. Test connectivity with simple commands first (e.g., docker_system_info).

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选