教你如何實現容器化 MCP Server
大家好!我是韓老師。
寫在最前:容器化 MCP Server,有用。但是,你不一定需要。
WHY
如果你在開發(fā)一個 local MCP Server,并且有以下的任何一種情況:
- 需要安裝多個 toolchain,才能運行 local MCP Server
- 用于開發(fā) local MCP Server 的語言,沒有像 npx 或者 uv 那樣一鍵運行程序的工具
那么,容器化 MCP Server,對你的用戶是有用的。
反之,如果你已經用主流的 Node.js 或者 Python 來開發(fā) local MCP Server,并且沒有其他額外的依賴。
那么,你也許并不需要容器化。
WHAT
local MCP Server 其實就是個 Node.js/Python/PHP/Go/Java/... 開發(fā)的 Console App 而已,通過 stdin/stdout 與 MCP Client 交互,沒有什么特別的地方。
所以,一般來說,你只需要一個 Dockerfile 即可。
HOW
既然是容器化一個普通的 Console App,那么,一切就變得很簡單了。
以下是 Code Runner MCP Server 的 Dockerfile :
## Stage 1: Builder FROM node:lts-alpine AS builder # Set working directory WORKDIR /app # Copy all files into the container COPY . . # Install dependencies without running scripts RUN npm install --ignore-scripts # Build the TypeScript source code RUN npm run build ## Stage 2: Runtime FROM node:lts-alpine WORKDIR /app # Install Python and other programming languages RUN apk add --no-cache \ python3 \ go \ php \ ruby # Copy only the necessary files from the builder stage COPY --from=builder /app/dist ./dist COPY package*.json ./ # Install only production dependencies RUN npm install --production --ignore-scripts # Use a non-root user for security (optional) RUN adduser -D mcpuser USER mcpuser # Set the entrypoint command CMD ["node", "./dist/index.js"]
這,就是一個標準的 multi-stage builds 的 Dockerfile。
由于 Code Runner MCP Server 需要支持多種編程語言的運行,我在 Dockerfile 里面,預先安裝了幾個常用的編程語言的解釋器/編譯器。
這樣,用戶在使用的時候,唯一需要安裝的,就是 Docker 而已:
{ "mcp": { "inputs": [], "servers": { "mcp-server-code-runner": { "command": "docker", "args": [ "run", "--rm", "-i", "formulahendry/mcp-server-code-runner" ] } } } }
完整的代碼,可以參考 Code Runner MCP Server 的 repo,完全開源:
https://github.com/formulahendry/mcp-server-code-runner
到此這篇關于教你如何實現容器化 MCP Server的文章就介紹到這了,更多相關容器化 MCP Server內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
node.js中的querystring.stringify方法使用說明
這篇文章主要介紹了node.js中的querystring.stringify方法使用說明,本文介紹了querystring.stringify的方法說明、語法、接收參數、使用實例和實現源碼,需要的朋友可以參考下2014-12-12