foundry-sandbox-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@foundry-sandbox-mcp run all forge tests in the test folder"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Foundry Sandbox MCP Server
一个基于 Docker 的 Foundry 测试沙盒 MCP Server,允许 AI 在隔离的 Docker 容器中安全地运行 Foundry 测试命令。
功能特性
✅ 自动容器管理: 每次测试时自动创建新容器,测试完成后自动清理
✅ 全新测试环境: 每次测试都在全新的容器中运行,确保环境干净
✅ 多包管理器支持: 支持 forge、npm、yarn 三种包管理器
✅ 灵活的依赖格式: 支持数组格式(不带版本号)和对象格式(带版本号)
✅ 自动依赖安装: 根据依赖清单文件自动安装依赖
✅ Docker 缓存清理: 测试完成后自动清理 Docker system 缓存
✅ 环境一致性: 无论运行在 Mac、Windows 还是 Linux,行为完全一致
✅ 安全性: 所有操作在 Docker 容器中运行,与宿主机隔离
✅ 零污染: 所有依赖和缓存保留在容器内,测试完成后自动清理
Related MCP server: AgentExecMPC
前置要求
Docker 和 Docker Compose
Node.js 18+ 和 Yarn
Foundry 项目
快速开始
安装
克隆或下载项目
安装依赖:
yarn install构建项目:
yarn build配置 MCP 客户端
Claude Desktop 配置
编辑配置文件:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"foundry-sandbox": {
"command": "node",
"args": ["/absolute/path/to/foundry-mcp/dist/index.js"]
}
}
}Cursor 配置
{
"mcpServers": {
"foundry-sandbox": {
"command": "node",
"args": ["/absolute/path/to/foundry-mcp/dist/index.js"]
}
}
}配置说明:
/absolute/path/to/foundry-mcp: MCP Server 的安装路径(绝对路径)Docker 镜像会在首次使用时自动构建
开发模式
{
"mcpServers": {
"foundry-sandbox": {
"command": "yarn",
"args": ["dev"],
"cwd": "/absolute/path/to/foundry-mcp"
}
}
}可用工具
forge_test
在 Docker 容器中运行 forge test 命令。每次测试时创建新容器,测试完成后自动清理,确保全新环境。
参数:
projectRoot(必需): 项目根路径(绝对路径),用于 Docker 挂载。例如/path/to/projecttestFolderPath(必需): 测试合约文件夹路径(相对项目根路径)。例如test或test/unit。如果路径以.sol结尾,则直接使用该路径;否则会自动匹配该文件夹下的所有.t.sol文件dependenciesManifestPath(必需): 依赖项清单文件路径(相对项目根路径)。文件格式为 JSON 对象,例如dependencies.jsonextraArgs(可选): 额外的forge test参数数组enablePrune(可选): 是否在测试完成后执行docker system prune -f(默认为false,安全起见默认跳过)
工作流程:
创建新容器(使用唯一名称),挂载项目目录
根据依赖清单文件自动安装依赖(forge、npm、yarn)
在容器中运行
forge test命令返回测试结果
自动删除容器并清理 Docker system 缓存
依赖管理
依赖清单文件格式
依赖清单文件支持三种包管理器(forge、npm、yarn),每种包管理器支持两种格式:
格式说明
数组格式(不带版本号):
["package-name"]- 使用最新版本对象格式(带版本号):
{"package-name": "version"}- 指定版本
示例
{
"forge": ["foundry-rs/forge-std"],
"npm": {
"@openzeppelin/contracts": "^5.0.2",
"@openzeppelin/contracts-upgradeable": "^5.0.2"
},
"yarn": ["@chainlink/contracts"]
}详细说明
forge: 使用
forge install --no-git安装 Git 依赖数组格式:
["foundry-rs/forge-std"](使用最新版本)对象格式:
{"foundry-rs/forge-std": "v1.0.0"}(指定版本或 tag)
npm: 使用
npm install安装 npm 包数组格式:
["@openzeppelin/contracts"](使用最新版本)对象格式:
{"@openzeppelin/contracts": "^5.0.2"}(指定版本)
yarn: 使用
yarn add安装 yarn 包数组格式:
["@chainlink/contracts"](使用最新版本)对象格式:
{"@chainlink/contracts": "^1.0.0"}(指定版本)
注意事项
所有字段(forge、npm、yarn)都是可选的,但至少需要提供一个字段
每个字段可以独立选择使用数组或对象格式
支持混合格式(部分字段使用数组,部分字段使用对象)
版本号格式遵循各包管理器的标准格式
Docker 环境管理
自动容器管理
MCP Server 会自动管理 Docker 容器生命周期:
✅ 每次测试时创建新容器:使用唯一名称(基于时间戳),确保全新环境
✅ 自动挂载项目目录:将传入的项目路径挂载到容器的
/workspace目录✅ 测试完成后自动清理:删除容器并清理 Docker system 缓存
✅ 无需手动操作:完全自动化,无需手动创建或删除容器
Docker 镜像管理
重要:Docker 镜像 foundry-sandbox:latest 会在首次使用时自动构建。
如果镜像不存在,MCP 工具会自动:
检测 MCP 服务器路径(通过环境变量
FOUNDRY_MCP_PROJECT_PATH或自动查找)读取
src/docker/Dockerfile.foundry和src/docker/docker-compose.yml配置使用
docker-compose build自动构建 Docker 镜像
手动构建(可选):
# 使用 docker-compose(推荐)
cd /path/to/foundry-mcp
docker-compose -f src/docker/docker-compose.yml build foundry-sandbox
# 或使用 docker build
docker build -t foundry-sandbox:latest -f src/docker/Dockerfile.foundry .设置 MCP 服务器路径(可选,用于自动构建):
export FOUNDRY_MCP_PROJECT_PATH=/path/to/foundry-mcp注意:MCP 服务器目录必须同时包含 src/docker/Dockerfile.foundry 和 src/docker/docker-compose.yml 文件。
Docker 镜像内容
Docker 镜像基于 ghcr.io/foundry-rs/foundry:latest,并包含:
Foundry 工具集(forge, cast, anvil, chisel)
Node.js 20.x
npm
yarn
使用示例
运行所有测试
{
"name": "forge_test",
"arguments": {
"projectRoot": "/absolute/path/to/project",
"testFolderPath": "test",
"dependenciesManifestPath": "dependencies.json"
}
}运行特定测试文件
{
"name": "forge_test",
"arguments": {
"projectRoot": "/absolute/path/to/project",
"testFolderPath": "test/Counter.t.sol",
"dependenciesManifestPath": "dependencies.json"
}
}使用额外参数
{
"name": "forge_test",
"arguments": {
"projectRoot": "/absolute/path/to/project",
"testFolderPath": "test",
"dependenciesManifestPath": "dependencies.json",
"extraArgs": ["-vvv", "--gas-report"]
}
}工作原理
MCP Server 接收来自 AI 的工具调用请求
Docker Manager 创建新的 Docker 容器并挂载项目目录
依赖安装 根据依赖清单文件自动安装依赖(forge、npm、yarn)
Forge Tool 在容器中执行
forge test命令结果返回 命令输出(stdout/stderr)和退出码被捕获并返回给 AI
清理 自动删除容器并清理 Docker system 缓存
项目结构
foundry-mcp/
├── src/
│ ├── index.ts # MCP Server 主文件
│ ├── docker-manager.ts # Docker 容器管理
│ ├── docker/
│ │ ├── Dockerfile.foundry # Foundry Docker 镜像
│ │ └── docker-compose.yml # Docker Compose 配置
│ └── tools/
│ └── forge-tool.ts # Forge 工具实现
├── dist/ # 编译后的文件
├── dependencies.json # 依赖清单示例文件
├── package.json
├── tsconfig.json
└── README.md开发
开发模式
yarn dev构建
yarn build运行
yarn start故障排除
Docker 容器未找到
MCP Server 现在会自动创建容器。如果仍然失败:
检查 Docker 是否正在运行:
docker ps检查 Docker 镜像是否存在:
docker images | grep foundry-sandbox如果镜像不存在,MCP Server 会自动构建,或手动构建:
docker-compose -f src/docker/docker-compose.yml build foundry-sandboxDocker 未运行
确保 Docker Desktop 正在运行:
docker ps依赖安装失败
检查依赖清单文件格式是否正确
检查网络连接(依赖需要从网络下载)
查看 MCP Server 日志获取详细错误信息
权限问题
如果遇到权限问题,确保 Docker 有权限访问项目目录。
安全注意事项
所有操作在 Docker 容器中运行,与宿主机隔离
容器与宿主机通过卷挂载共享文件
测试完成后自动清理容器和缓存
建议在生产环境中使用只读卷挂载(如果需要)
许可证
MIT
Available Tools
1 toolforge_testA
在 Docker 容器中运行 forge test 命令。每次测试时创建新容器,测试完成后自动清理,确保全新环境。运行测试前会根据依赖清单文件自动安装依赖。
Inputs: projectRoot (required), testFolderPath (required), dependenciesManifestPath (required), extraArgs, +1 more.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 1 tool
With only a single tool, there is no possibility of confusion or overlap; the purpose is entirely distinct.
The single tool name 'forge_test' follows a consistent verb_noun pattern, which is as consistent as possible with one tool.
The server has only 1 tool, which is borderline for a typical MCP server; more tools would be expected for a sandbox environment.
The server only provides a test runner, missing essential operations like compilation or deployment, leaving significant gaps for a Foundry sandbox.
Related MCP Connectors
MCP server that lets your AI assistant build and deploy apps, hosting each one at a live URL.
MCP server for building and testing AI agents with multi-model experimentation and insights.
Cloud-hosted MCP server for durable AI memory
Secure Linux sandboxes for AI agents, as MCP tools.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceAn MCP server to create secure code sandbox environment for executing code within Docker containers.327MIT
- FlicenseAqualityDmaintenanceA FastMCP server that enables AI agents to safely execute shell commands, run code in Python/Node.js/Go, and install packages, all packaged in Docker for secure deployment.32-
- AlicenseBqualityDmaintenanceAn MCP server that provides AI agents with a full Ubuntu desktop environment inside Docker, enabling them to perform complex computer tasks like browsing, coding, testing, and GUI automation.3611MIT
- AlicenseAqualityDmaintenanceMCP server that gives AI assistants a secure gVisor-isolated sandbox to run Python or JavaScript code, returning stdout, stderr, and exit codes without local access.137 npmMIT