MCX-模块化代码执行
███╗ ███╗ ██████╗██╗ ██╗
████╗ ████║██╔════╝╚██╗██╔╝
██╔████╔██║██║ ╚███╔╝
██║╚██╔╝██║██║ ██╔██╗
██║ ╚═╝ ██║╚██████╗██╔╝ ██╗
╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝MCP服务器,允许AI代理执行代码,而不是直接调用工具。
基于 Anthropic的代码执行文章.
问题
传统的MCP有两个低效之处:
- 工具定义过载 -加载所有工具定义洪泛上下文
- 中间结果膨胀 -每个API响应都通过模型
解决方案
代理不直接调用工具 编写代码 它在沙盒中运行:
const invoices = await api.getInvoices({ limit: 100 });
return {
count: invoices.length,
total: sum(invoices, 'amount'),
byStatus: count(invoices, 'status')
};
// Returns ~50 tokens instead of 50,000结果:代币减少98% 通过过滤执行环境内的数据。
安装
# Install globally with bun
bun add -g @papicandela/mcx-cli
# Initialize global directory (~/.mcx/)
mcx init需要Bun: MCX在运行时使用Bun。 安装Bun 如果你还没有。
快速开始
# 1. Initialize global MCX directory
mcx init
# 2. Generate adapters from API docs
mcx gen ./api-docs.md -n myapi
# 3. Add credentials to ~/.mcx/.env
# 4. Start server
mcx serve目录结构
~/.mcx/
├── adapters/ # Your adapters
│ ├── supabase.ts # Supabase Management API
│ ├── chrome-devtools.ts # Chrome DevTools Protocol
│ └── myapi.ts # Generated from OpenAPI
├── skills/ # Reusable skills
├── mcx.config.ts # Auto-loads all adapters
├── .env # API credentials
└── package.json # Dependencies
# Runtime (created automatically)
~/.mcx/
├── logs/ # Server logs (mcx logs to view)
└── .cache/ # FTS5 search indexClaude代码集成
添加到您的克劳德代码设置(~/.claude.json 或项目的 .mcp.json):
{
"mcpServers": {
"mcx": {
"command": "mcx",
"args": ["serve"]
}
}
}就是这样!MCX自动使用 ~/.mcx/ 用于配置和适配器。
克劳德代码挂钩(可选)
将本机工具重定向到MCX替代品以获得更好的性能:
~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{ "matcher": "Grep", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-redirect.js" }] },
{ "matcher": "Glob", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-redirect.js" }] },
{ "matcher": "Edit", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-redirect.js" }] },
{ "matcher": "Write", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-redirect.js" }] },
{ "matcher": "Read", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-read-check.js" }] },
{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "bun ~/.claude/hooks/mcx-bash-check.js" }] }
]
}
}| 原生工具 | MCX替代品 | 优势 |
|---|---|---|
Glob | mcx_find | 怪异排名、git状态、邻近度提升 |
Grep | mcx_grep | SIMD加速模糊搜索 |
Edit | mcx_edit | 无“先读”要求,CRLF规范化 |
Write | mcx_write | 无“先读”要求 |
Read (>50KB) | mcx_file({ storeAs }) | 文件保留在沙盒中,节省99%的令牌 |
Bash cat/grep/find | mcx_file/mcx_grep/mcx_find | 将shell命令重定向到MCX |
看 Hooks集成 用于钩子脚本。
主要特点
| 特性 | 描述 |
|---|---|
| 延迟加载 | 适配器来自 ~/.mcx/adapters/ 元数据在启动时扫描,首次使用时完全加载 |
| 域名提示 | 按域(支付、数据库、电子邮件等)分组的适配器,以提高可发现性 |
| 静音自动校正 | executeSql 自动更正为 execute_sql (camelCase到snake_case) |
| 上下文效率 | 过滤发生在沙盒中,模型只看到结果 |
| 可变持久性 | 将结果存储为 $invoices, $customers 供以后使用 |
| 自动压缩 | 过期变量(>5分钟,>1KB)自动压缩以保存上下文 |
| FTS5搜索 | 自动索引大输出,搜索 intent 参数 |
| FFF集成 | 快速文件查找器 -SIMD加速模糊搜索,内容恢复 |
| 后台任务 | mcx_spawn 对于长时间运行的操作,请与 mcx_tasks |
| 批量操作 | mcx_batch 用于一次呼叫中的多个操作 |
| 文件处理 | mcx_file 使用处理本地文件 $file 注射 |
| 大文件处理 | 大于50KB的文件通过以下方式留在沙盒中 storeAs,使用助手查询 |
| 文件编辑 | mcx_edit (字符串/行模式), mcx_write -无“先读”要求 |
| 文件查询辅助工具 | around(), lines(), block(), grep(), outline() 用于存储文件 |
| URL获取 | mcx_fetch 具有HTML到markdown转换(24小时TTL缓存) |
| 控制流 | 循环、条件语句、重试作为本机代码运行 |
| 隐私 | 中间数据留在沙箱中 |
| 安全 | 网络隔离、路径穿越保护、防止环境注入 |
MCP工具
| 工具 | 说明 |
|---|---|
mcx_execute | 使用适配器访问执行代码,自动存储为 $result |
mcx_search | 3种模式:规格探索、FTS5搜索、适配器/方法搜索 |
mcx_batch | 一次调用中的多次执行/搜索(绕过限制) |
mcx_file | 使用处理本地文件 $file 注射或仅储存模式 storeAs |
mcx_edit | 编辑文件(字符串模式或行模式)-绕过本机Edit的读取要求 |
mcx_write | 创建/覆盖文件-绕过本机Write的读取要求 |
mcx_fetch | 使用HTML获取URL以进行标记和自动索引(24小时缓存) |
mcx_find | 具有新鲜度+接近度排名的快速模糊文件搜索 |
mcx_grep | SIMD加速了跨文件的内容搜索 |
mcx_related | 通过导入/导出分析查找相关文件 |
mcx_tree | 在不加载完整内容的情况下导航大型JSON结果 |
mcx_spawn | 在后台运行代码,立即返回任务ID |
mcx_tasks | 列出/检查背景任务及其结果 |
mcx_list | 列出可用的适配器和技能 |
mcx_stats | 会话统计(索引内容、变量、网络) |
mcx_doctor | 运行诊断程序(Bun、SQLite、适配器、沙盒、FFF) |
mcx_upgrade | 获取最新版本的自升级命令 |
mcx_run_skill | 运行已注册的技能 |
CLI命令
| 命令 | 描述 |
|---|---|
mcx serve | 启动MCP服务器(默认) |
mcx gen | 根据OpenAPI规范生成适配器(使用TUI) |
mcx init | 初始化全局 ~/.mcx/ 目录 |
mcx update | 更新CLI和全局安装 |
mcx list | 列出可用的适配器和技能 |
mcx run | 直接运行技能 |
mcx logs | 查看服务器日志 |
看 CLI文档 了解详情。
包括适配器
| 适配器 | 方法 | 描述 |
|---|---|---|
supabase | 24 | 上级管理API(项目、表、函数、机密) |
chrome-devtools | 25 | Chrome DevTools协议(屏幕截图、导航、DOM) |
从OpenAPI文档生成自己的适配器:
mcx gen ./api-docs.md -n myapi内置助手
沙盒中可用的功能:
pick(data, ['id', 'name']) // Extract fields
first(data, 5) // First N items
sum(data, 'amount') // Sum numeric field
count(data, 'status') // Count by field
table(data, 10) // Markdown table
// Async helpers
await poll(fn, { interval: 2000, maxIterations: 5 }) // Poll until done
await waitFor(fn, { timeout: 30000 }) // Wait for condition文件查询辅助工具
使用时 mcx_file({ path, storeAs }) 要将文件加载到沙盒中,请执行以下操作:
// Load file without returning content to context (99% token savings)
mcx_file({ path: "src/large-file.ts", storeAs: "src" })
// Then query with helpers:
around($src, 150, 10) // 10 lines around line 150
lines($src, 100, 120) // Get lines 100-120 (1-indexed, inclusive)
block($src, 150) // Extract code block by indentation
grep($src, "TODO", 3) // Search with 3 lines context
outline($src) // Extract function/class signatures文档
发展
git clone https://github.com/schizoidcock/mcx
cd mcx
bun install
bun run build许可证
麻省理工学院
