Back to Skills

containerize-mcp-server

pjt222
Updated 2 days ago
4 views
17
2
17
View on GitHub
Metaaimcpdesign

About

This skill enables developers to containerize R-based MCP servers using Docker, eliminating the need for local R installations. It covers mcptools integration, transport configuration (stdio/HTTP), and connecting Claude Code to the containerized server. Use it for reproducible deployments, running alongside other containerized services, or distributing MCP servers to other developers.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/containerize-mcp-server

Copy and paste this command in Claude Code to install this skill

Documentation

容器化 MCP 伺服器

將 R MCP 伺服器包入 Docker 容器以便可攜部署。

適用時機

  • 部署 R MCP 伺服器而無需本地 R 裝
  • 建可重現之 MCP 伺服器環境
  • 令 MCP 伺服器與他容器化服務並行
  • 分發 MCP 伺服器予他開發者

輸入

  • 必要:R MCP 伺服器實作(以 mcptools 或自訂)
  • 必要:Docker 已裝且運行
  • 選擇性:伺服器需之額外 R 包
  • 選擇性:傳輸模式(stdio 或 HTTP)

步驟

步驟一:建 MCP 伺服器之 Dockerfile

FROM rocker/r-ver:4.5.0

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev \
    libssl-dev \
    libxml2-dev \
    libgit2-dev \
    libssh2-1-dev \
    git \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Install R packages
RUN R -e "install.packages(c( \
    'remotes', \
    'ellmer' \
    ), repos='https://cloud.r-project.org/')"

# Install mcptools
RUN R -e "remotes::install_github('posit-dev/mcptools')"

# Set working directory
WORKDIR /workspace

# Expose MCP server ports
EXPOSE 3000 3001 3002

# Environment variables
ENV R_LIBS_USER=/workspace/renv/library
ENV RENV_PATHS_CACHE=/workspace/renv/cache

# Default: start MCP server
CMD ["R", "-e", "mcptools::mcp_server()"]

預期: 項目根有 Dockerfilerocker/r-ver 基底鏡、系統依賴、mcptools 之裝、MCP 伺服器為預命令。

失敗時: 驗基底鏡標合你之 R 版本。若 remotes::install_github 敗,查 gitlibgit2-dev 於系統依賴層中。

步驟二:建 docker-compose.yml

version: '3.8'

services:
  mcp-server:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: r-mcp-server
    image: r-mcp-server:latest

    volumes:
      - /path/to/projects:/workspace
      - renv-cache:/workspace/renv/cache

    stdin_open: true
    tty: true

    network_mode: "host"

    environment:
      - TERM=xterm-256color
      - R_LIBS_USER=/workspace/renv/library

    restart: unless-stopped

volumes:
  renv-cache:
    driver: local

network_mode: "host" 確 MCP 伺服器 port 於 localhost 可達。

預期: 項目根有 docker-compose.yml 含 MCP 伺服器服務、項目檔與 renv 快取之 volume 掛、為 stdio 傳輸而啟之 stdin_open/tty

失敗時: 若 volume 路徑無效,將 /path/to/projects 調為實項目目錄。Windows/WSL 用 /mnt/c/.../mnt/d/... 路徑。

步驟三:建並啟

docker compose build
docker compose up -d

預期: 容器啟含 MCP 伺服器運行。

失敗時:docker compose logs mcp-server 查日誌。常見:

  • 缺 R 包:加至 Dockerfile RUN install 步
  • Port 已用:換 port 或止衝突之服務

步驟四:將 Claude Code 連至容器

stdio 傳輸(容器須保運行含 stdin):

claude mcp add r-mcp-docker stdio "docker" "exec" "-i" "r-mcp-server" "R" "-e" "mcptools::mcp_server()"

HTTP 傳輸(若 MCP 伺服器支):

{
  "mcpServers": {
    "r-mcp-docker": {
      "type": "http",
      "url": "http://localhost:3000/mcp"
    }
  }
}

預期: Claude Code 之 MCP 配置含 r-mcp-docker 伺服器項,claude mcp list 顯新伺服器。

失敗時: stdio 則確容器名合(r-mcp-server)且容器以 docker ps 顯運行。HTTP 則驗 port 暴且以 curl http://localhost:3000/mcp 可達。

步驟五:驗連接

# Check container is running
docker ps | grep mcp-server

# Test R session inside container
docker exec -it r-mcp-server R -e "sessionInfo()"

# Verify mcptools is available
docker exec -it r-mcp-server R -e "library(mcptools)"

預期: docker psr-mcp-server 容器運行,sessionInfo() 返預期 R 版本,library(mcptools) 無誤載入。

失敗時: 若容器不運,以 docker compose logs mcp-server 查啟動錯。若 mcptools 載失,重建鏡以確包正確裝。

步驟六:加自訂 MCP 工具

加項目特定 MCP 工具,掛 R 腳本:

volumes:
  - ./mcp-tools:/mcp-tools

並於 CMD 中載:

CMD ["R", "-e", "source('/mcp-tools/custom_tools.R'); mcptools::mcp_server()"]

預期: 自訂 R 腳本於容器內 /mcp-tools/ 可達,MCP 伺服器啟時連預設工具載之。

失敗時:docker exec -it r-mcp-server ls /mcp-tools/ 驗 volume 掛路徑正確。若腳本 source 敗,查自訂工具中缺之包依賴。

驗證

  • 容器建而無誤
  • MCP 伺服器於容器內啟
  • Claude Code 可連至容器化伺服器
  • MCP 工具正確回應請求
  • 容器淨重啟
  • Volume 掛允存取項目檔

常見陷阱

  • stdin/tty 要:MCP stdio 傳輸需 stdin_open: truetty: true
  • 網路隔離:預 Docker 網路或防 localhost 存取。用 network_mode: "host" 或暴指定 port
  • 包版本:釘 mcptools 於特定提交以求可重現
  • 大鏡:mcptools + 依賴可甚大。生產慮多階段建
  • Windows Docker 路徑:Windows + WSL 下行 Docker Desktop 時路徑映射異

相關技能

  • create-r-dockerfile - R 之基底 Dockerfile 模式
  • setup-docker-compose - compose 配置細節
  • configure-mcp-server - 無 Docker 之 MCP 伺服器配置
  • troubleshoot-mcp-connection - 診 MCP 連通問題

GitHub Repository

pjt222/agent-almanac
Path: i18n/wenyan-lite/skills/containerize-mcp-server
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

polymarket

Meta

This skill enables developers to build applications with the Polymarket prediction markets platform, including API integration for trading and market data. It also provides real-time data streaming via WebSocket to monitor live trades and market activity. Use it for implementing trading strategies or creating tools that process live market updates.

View skill

creating-opencode-plugins

Meta

This skill helps developers create OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It provides the plugin structure, event API specifications, and implementation patterns for JavaScript/TypeScript modules. Use it when you need to intercept, monitor, or extend the OpenCode AI assistant's lifecycle with custom event-driven logic.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill