只读CLI工具访问服务器

一个安全加固的Model Context Protocol服务器,为AI代理提供对CLI工具的只读访问,适用于自动允许环境。

  • by readonly-mcp
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 readonly-mcp/core

使用说明

@只读mcp/core

加强安保 模型上下文协议 服务器,为AI代理提供对CLI工具的只读访问。专为自动允许环境而设计,在这种环境中,满排列表是防止快速注入的唯一安全边界。

为什么存在

大多数代理框架通过提示指令或通过管理模型看到的工具来限制工具访问。这些是软边界——一个提示注入,说服代理“使用bash”或“调用这个未列出的函数”完全绕过它们。限制存在于模型的解释中,而不是模型无法影响的代码中。

此服务器将边界移动到执行层。满列表是由MCP服务器进程在运行时强制执行的:如果命令没有明确允许,服务器会在生成任何进程之前拒绝它。再多的创意提示也无法改变什么 execFile 将执行。代理可以请求任何东西——服务器决定实际运行什么。

目标是使只读工具的自动允许安全。如果不在服务器上强制执行,自动允许MCP工具相当于给代理不受限制的shell访问权限,因为该工具的“只读”契约只与模型的合规性一样强。

安全模型

  • 允许名单,而非拒绝名单 --必须明确允许每个命令和子命令
  • 不 cwd 参数 --命令仅在服务器的工作目录中运行,防止代理读取任意目录中的文件
  • execFile,没有外壳 --防止壳元字符注射(;, &&, |, $(), `)即使参数包含不受信任的输入
  • 前缀标志匹配 — rejectBlockedFlags 使用前缀匹配来击败CLI标志缩写(例如,npm扩展 --reg 到 --registry).完全匹配是不可能的。
  • --registry 阻塞 --防止SSRF/数据泄露到攻击者控制的注册表
  • --ignore-scripts 注入 --深度防御npm阻止生命周期脚本。 --no-ignore-scripts 和 --ignore-scripts 还阻止用户提供参数,以防止最后获胜覆盖。
  • az devops invoke 国旗反对者 --未知标志被拒绝(允许列表,而非阻止列表),阻止缩写绕过
  • 资源限制 --10秒超时,最大2 MB缓冲区,防止命令失控
  • Windows支持 — bash -c 'exec "$@"' 包装蜜饯 execFile shell实用程序的保证

设计排除

命令/标志 原因
chezmoi cat-config 可能泄漏加密密钥、GPG收件人ID或其他敏感配置
chezmoi data 泄漏可能包含机密的模板变量
printenv / env 泄漏环境变量(API密钥、令牌)
rg 主机冗余 Grep 工具;允许搜索任意路径
cat, head, tail, bat, diff, delta 主机冗余 Read 工具;path args重复文件读取表面积
git diff --no-index 读取存储库外的任意文件
git --output / diff -o 将命令输出写入文件
gh variable get 返回可能包含内部URL、主机名或准敏感配置的纯文本值

工具

shell

只读shell实用程序: basename, date, dirname, eza, file, jq, ls, pwd, readlink, realpath, stat, wc, which, whoami

jq 阻止文件路径参数、文件读取标志(--slurpfile, --rawfile, -f, -L)、包含文件读取字符的组合短标志(例如。, -rf, -nf),连接 -L/path,以及 --flag=val 表单——改用stdin管道。

架构: { command: string, args?: string[] }

git

只读git命令: blame, branch, describe, diff, log, ls-files, ls-tree, merge-base, reflog, remote, rev-parse, shortlog, show, stash, status, worktree

附加限制:

  • --output 全局阻止(前缀匹配击败缩写)
  • -o 短旗被挡住了 diff 仅限(其他命令使用 -o 对于不相关的标志。, ls-files -o = --others)
  • --no-index 全局阻止(前缀匹配)
  • branch:破坏性标志被阻止(-D, -d, -m, --delete, --force等等)
  • stash:仅 list 和 show 子命令(-- 被视为终结者)
  • worktree:仅 list (突变: add, remove, move, prune等等)
  • reflog:仅 show, exists,以及裸调用(破坏性: delete, expire)
  • remote:仅列出裸照, get-url,以及 show -n (无网络I/O)

架构: { args: string[] }

gh

只读GitHub CLI命令: attestation verify, cache list, gist list/view, issue list/status/view, label list, pr checks/diff/list/status/view, project field-list/item-list/list/view, release list/view, repo list/view, ruleset check/list/view, run list/view, search code/commits/issues/prs/repos, secret list, status, variable list, workflow list/view

还支持 --version, --help, -h 作为独立的标志。

架构: { args: string[] }

az

只读Azure DevOps CLI命令: devops project/team/extension/service-endpoint/wiki, pipelines list/show/build/release/runs/pool/agent

devops invoke 仅限于针对构建调试端点的GET请求(build/builds, build/timeline, build/logs, build/changes, build/artifacts, build/leases, test/runs).未知标志被拒绝(允许列表,而非阻止列表)。

架构: { args: string[] }

npm

只读npm命令: audit, bin, explain, fund, ls, outdated, root, search, view

被阻止的标志: --fix, --registry, --no-ignore-scripts, --ignore-scripts. --ignore-scripts 总是由服务器重新注入。

架构: { args: string[] }

pnpm

只读pnpm命令: audit, bin, licenses list, list, outdated, root, search, store status, why

还支持 --version, --help, -h 作为独立的标志。

被阻止的标志: --fix, --registry

架构: { args: string[] }

chezmoi

只读chezmoi命令: diff, doctor, managed, source-path, status, target-path, verify

架构: { args: string[] }

acli

只读Atlassian CLI命令: jira board list, jira filter list, jira project list, jira sprint list, jira workitem comment list, jira workitem list/search/view

架构: { args: string[] }

安装

pnpm add -g github:readonly-mcp/core

克劳德代码

claude mcp add -s user readonly -- readonly-mcp

要自动批准所有只读工具,请添加到 ~/.claude/settings.json:

{
  "permissions": {
    "allow": [
      "mcp__readonly__*"
    ]
  }
}

VS代码/副本

添加到您的 settings.json:

{
  "mcp": {
    "servers": {
      "readonly": {
        "command": "readonly-mcp"
      }
    }
  }
}

发展

pnpm install   # first time only
pnpm test

项目结构

index.mjs           Server entry point
lib/
  allowlist.mjs     Allowlist matching and validation
  exec.mjs          Sandboxed command execution
tools/
  index.mjs         Tool registry
  acli.mjs          Atlassian CLI tool
  az.mjs            Azure DevOps CLI tool
  chezmoi.mjs       Chezmoi tool
  gh.mjs            GitHub CLI tool
  git.mjs           Git tool
  npm.mjs           npm tool
  pnpm.mjs          pnpm tool
  shell.mjs         Shell utilities tool
test/               Unit and integration tests (vitest)