用于安全执行本地代码的MCP服务器
是的,有一百万台MCP服务器用于沙盒代码执行。但这个是我的。
有两种模式。
- 直接在本地计算机或本地(受信任的)网络上运行二进制文件。然后在不以root身份运行的docker容器中执行代码。
- 在直接运行代码的隔离docker容器中运行MCP。当你的整个AI工具都被停靠时,这是理想的。
直接在本地机器上运行二进制文件,并启动docker镜像
如果您在本地运行类似Claude Code的程序并希望执行沙盒代码,则此选项最有意义。
构建图像
确保在运行mcp之前构建docker镜像:
docker build -t python-no-root -f docker/python.Dockerfile ./docker
docker build -t rust-no-root -f docker/rust.Dockerfile ./docker
docker build -t js-no-root -f docker/javascript.Dockerfile ./docker
测试图像:
docker run -it -v $(pwd):/app -e TYPE=default -w /app python-no-root python ./integration-tests/helloworld.py
docker run -it -v $(pwd):/app -w /app rust-no-root cargo run
docker run -it -v $(pwd):/app -e TYPE=default -w /app js-no-root node ./integration-tests/helloworld.js
运行MCP
cargo run
在隔离容器中运行MCP
如果您将代码编译工具作为更大部署的一部分运行(例如,使用docker compose),则此选项最有意义。请注意,在此模式下,MCP服务器将允许所有主机:请确保设置防火墙以限制访问。
塑造形象
docker build -t main-docker -f docker/Dockerfile ./运行MCP
docker run -p 8000:8000 \
-v $(pwd):/app/code_location \
--add-host=host.docker.internal:host-gateway \
main-docker访问MCP
npx @modelcontextprotocol/inspector http://127.0.0.1:8000/mcp --cli --method tools/list运行Python:
npx @modelcontextprotocol/inspector \
--cli http://127.0.0.1:8000/mcp \
--method tools/call --tool-name run_python \
--tool-arg command=python \
--tool-arg args=helloworld.py \
--tool-arg project_dir=integration-tests运行Javascript:
npx @modelcontextprotocol/inspector \
--cli http://127.0.0.1:8000/mcp \
--method tools/call --tool-name run_javascript \
--tool-arg command=node \
--tool-arg args=helloworld.js \
--tool-arg project_dir=integration-tests运行Rust:
npx @modelcontextprotocol/inspector \
--cli http://127.0.0.1:8000/mcp \
--method tools/call --tool-name run_rust \
--tool-arg command=cargo \
--tool-arg args=test \
--tool-arg project_dir=.将MCP添加到克劳德代码(Ollama)
ollama launch claude -- mcp add --transport http run_code http://127.0.0.1:8000/mcp
添加到本机Claude代码
claude mcp add --transport http run_code http://127.0.0.1:8000/mcp
