容器化 MCP Server!

大家好!我是韩老师。

本文是 MCP 系列文章的第五篇,之前的四篇是:

写在最前:容器化 MCP Server,有用。但是,你不一定需要。

WHY

如果你在开发一个 local MCP Server,并且有以下的任何一种情况:

  • 需要安装多个 toolchain,才能运行 local MCP Server
  • 用于开发 local MCP Server 的语言,没有像 npx 或者 uv 那样一键运行程序的工具

那么,容器化 MCP Server,对你的用户是有用的。

反之,如果你已经用主流的 Node.js 或者 Python 来开发 local MCP Server,并且没有其他额外的依赖。

那么,你也许并不需要容器化。

WHAT

local MCP Server 其实就是个 Node.js/Python/PHP/Go/Java/... 开发的 Console App 而已,通过 stdin/stdout 与 MCP Client 交互,没有什么特别的地方。

所以,一般来说,你只需要一个 Dockerfile 即可。

HOW

既然是容器化一个普通的 Console App,那么,一切就变得很简单了。

以下是 Code Runner MCP Server 的 Dockerfile :

复制代码
 1 ## Stage 1: Builder
 2 FROM node:lts-alpine AS builder
 3 
 4 # Set working directory
 5 WORKDIR /app
 6 
 7 # Copy all files into the container
 8 COPY . .
 9 
10 # Install dependencies without running scripts
11 RUN npm install --ignore-scripts
12 
13 # Build the TypeScript source code
14 RUN npm run build
15 
16 ## Stage 2: Runtime
17 FROM node:lts-alpine
18 
19 WORKDIR /app
20 
21 # Install Python and other programming languages
22 RUN apk add --no-cache \
23     python3 \
24     go \
25     php \
26     ruby
27 
28 # Copy only the necessary files from the builder stage
29 COPY --from=builder /app/dist ./dist
30 COPY package*.json ./
31 
32 # Install only production dependencies
33 RUN npm install --production --ignore-scripts
34 
35 # Use a non-root user for security (optional)
36 RUN adduser -D mcpuser
37 USER mcpuser
38 
39 # Set the entrypoint command
40 CMD ["node", "./dist/index.js"]

这,就是一个标准的 multi-stage builds 的 Dockerfile。

由于 Code Runner MCP Server 需要支持多种编程语言的运行,我在 Dockerfile 里面,预先安装了几个常用的编程语言的解释器/编译器。

这样,用户在使用的时候,唯一需要安装的,就是 Docker 而已:

复制代码
 1 {
 2   "mcp": {
 3     "inputs": [],
 4     "servers": {
 5       "mcp-server-code-runner": {
 6         "command": "docker",
 7         "args": [
 8           "run",
 9           "--rm",
10           "-i",
11           "formulahendry/mcp-server-code-runner"
12         ]
13       }
14     }
15   }
16 }

完整的代码,可以参考 Code Runner MCP Server 的 repo,完全开源:

https://github.com/formulahendry/mcp-server-code-runner

相关推荐
浩风祭月10 小时前
Agent Plugins 1.0怎么把一套技能同时带进VS Code和Copilot CLI?
ai编程·vs code·github copilot·mcp·copilot cli·agent plugins
benchmark_cc14 小时前
Claude Code + MCP + QuantDash:打造全自动量化研究流水线的终极指南
人工智能·后端·爬虫·算法·claude·mcp·quantdash
枫彩14 小时前
WorkBuddy 做A股自动复盘实战:数据校验、工具调用与定时报告
大数据·网络·数据库·a股·mcp·workbuddy
Lambert2811 天前
MCP 史上最大升级:握手没了、会话没了,Java 生态的六个坑
ai编程·mcp
会周易的程序员1 天前
软件接入大模型实现 Agent —— 从原理到 C++ 落地完全指南
c++·人工智能·物联网·架构·agent·工业协议·mcp
梦想blog2 天前
Claude Code + Chrome MCP:让浏览器自动化测试更简单
前端·chrome·自动化·mcp
xrlfreedom2 天前
大厂 MCP 面试实录:基于 JSON Schema 与 RAG 的调用异常排查方案设计
json schema·mcp·rag 知识库
逸Y 仙X2 天前
MCP(模型控制协议)完全指南:从核心概念到实战开发
python·大模型·llm·ai编程·mcp
Behavior2 天前
Claude Code + chrome-devtools MCP:安装部署与实战全记录
claude·mcp
阿里云云原生2 天前
从 MCP 协议演进看网关架构变革:Higress v2.2.4 实践与验证报告
mcp