node.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > node.js > 容器化 MCP Server

教你如何实现容器化 MCP Server

作者:formulahendry

这篇文章主要介绍了如何将MCPServer容器化,以便于开发和运行,但并不适用于所有的开发环境

大家好!我是韩老师。

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

WHY

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

那么,容器化 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 :

## 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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文