Pkgdiet

io.github.om-tajne由 GitHub 帳號 om-tajne 發布v2.0.1更新於 Oct 30, 2026

Dependency policy and MCP server to stop AI coding agents from installing risky npm packages.

已驗證STDIO僅桌面開發者工具安全與監控

概覽

AI 產生的概覽

本機 MCP 伺服器,依儲存庫相依性政策檢查 npm 套件,在 AI 代理安裝前回傳 ALLOW、WARN 或 BLOCK 結論。

可以做什麼
  • 依本機政策評估單一 npm 套件,回傳附證據的 ALLOW、WARN 或 BLOCK 結構化結論
  • 一次呼叫批次檢查最多 50 個相依套件
  • 為被封鎖或不建議的套件查詢精選的現代、更輕量替代方案
  • 讀取目前儲存庫的相依性政策規則與門檻
  • 詳細解釋某個結論,包括評分、維護訊號與診斷代碼
適用情境
當 AI 編碼代理撰寫 JavaScript 或 TypeScript、可能提出不存在、已棄用、有漏洞或仿冒名稱的 npm 套件時適用,可用統一政策攔阻安裝。也適合希望以代理鉤子和 CI 關卡執行同一套規則的團隊。
執行需求
透過 npm 套件 pkgdiet 以本機 stdio 程序執行,需要 Node.js 20 或更新版本。專案中需有 .pkgdietrc.json 政策檔定義規則。漏洞與來源驗證需連線 npm 註冊表與公開的 OSV 公告 API,除非以 PKGDIET_NO_NETWORK 啟用離線模式。不需帳號或 API 金鑰。
安裝前請注意
MCP 工具據稱是唯讀的,但初始化指令會寫入大量檔案到儲存庫,包括代理設定、鉤子腳本與 GitHub Actions 工作流程,且鉤子可攔阻安裝指令。選用環境變數控制離線模式、遙測與自訂註冊表位址。除非啟用離線模式,套件中介資料會傳送到公開的 npm 註冊表與 OSV API。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 Pkgdiet,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

🥗 PkgDiet

Dependency policy for AI-assisted JavaScript and TypeScript development.
Put your node_modules on a diet.

[npm version] [npm downloads] [Audited by PkgDiet]
[CI Status] [Node.js] [TypeScript] [GitHub Issues] [PRs Welcome] [License]
[Powered by MCP] [Glama MCP Server] [Listed on mcpservers.org]

[GitHub stars]

The Problem: AI coding agents (Claude Code, Codex, Cursor, Windsurf, Cline) propose nonexistent, deprecated, vulnerable, or typosquatted packages. There is no built-in mechanism to stop them from running the install.

The Solution: PkgDiet intercepts every npm install / pnpm add / yarn add command your AI agent generates — using native agent hook APIs — and blocks unapproved installs before they execute.


🔒 Live Proof

Run this to see PkgDiet block real risky AI-generated dependencies in your terminal:

bash
npx pkgdiet demo

Example output:

text
🔒 PkgDiet — Live AI Dependency Enforcement Demo   Simulating what happens when an AI agent tries to install these packages.
🟡  WARNING — moment   Context:   AI suggested this for date formatting   Score:     100/100   Reason:    Efficiency Flag: Better alternatives exist for moment.   Use this:  dayjs, date-fns
🔴  BLOCKED — request   Context:   AI suggested this for HTTP requests   Score:     15/100   Reason:    Health score 15 is below minimum allowed (60).   Use this:  undici, native fetch   CVEs:      GHSA-p8p7-x288-28g6
   🔒 PkgDiet blocked this risky AI-generated dependency before install.
🔴  BLOCKED — node-uuid   Context:   AI suggested this for UUID generation   Score:     15/100   Reason:    Health score 15 is below minimum allowed (60).   Use this:  crypto.randomUUID(), uuid
   🔒 PkgDiet blocked this risky AI-generated dependency before install.
🟡  WARNING — lodash   Context:   AI suggested this for array utilities   Score:     80/100   Reason:    Efficiency Flag: Better alternatives exist for lodash.   Use this:  lodash-es, native JS
📊 Demo Summary: 2 blocked, 2 warned   These checks run automatically before every npm install in your AI agent.   Setup: npx pkgdiet init

For machine-readable CI proof:

bash
npx pkgdiet demo --json

🚀 One-Command Setup

bash
npx pkgdiet init

This single command creates:

FilePurpose
.pkgdietrc.jsonRepository dependency policy
.github/workflows/pkgdiet.ymlGitHub Actions CI gate
.cursor/mcp.jsonCursor MCP server
.cursor/rules/pkgdiet.mdcCursor always-applied enforcement rule
.cursorrulesCursor advisory rule (legacy)
.windsurfrulesWindsurf advisory rule
cline_mcp_settings.jsonCline MCP server
.github/mcp.jsonGitHub Copilot MCP
CLAUDE.mdClaude Code advisory rule
.claude/settings.jsonClaude Code PreToolUse hook registration
.claude/hooks/pkgdiet-install-guard.shClaude Code bash install guard (exit code 2)
.codex/hooks.jsonCodex PreToolUse hook registration
.codex/hooks/pkgdiet-install-guard.mjsCodex Node.js install guard

🛡️ Enforcement by Agent

PkgDiet uses each agent's native hook API to block installs — not just advisory rules:

AgentEnforcement MechanismWhat it blocks
Claude CodePreToolUse bash hook (.claude/hooks/)npm/pnpm/yarn install before execution
Codex (OpenAI)PreToolUse Node.js hook (.codex/hooks/)npm/pnpm/yarn install before execution
CursorMDC alwaysApply rule + MCP check_dependencyPrevents AI from writing install commands
Windsurf.windsurfrules (advisory rules only)Guides AI to avoid prohibited packages
ClineMCP check_dependencyBlocks before install via tool call
GitHub CopilotMCP check_dependencyBlocks before install via tool call
AntigravityNative MCP server (.gemini/antigravity/mcp/)Contextual tool evaluations
CI (GitHub Actions)pkgdiet ci gateBlocks PR merge if any new package fails policy

How PreToolUse hooks work: When Claude Code or Codex generates a Bash tool call containing npm install, the registered hook script runs first. If PkgDiet returns a non-zero exit code (exit code 2 for Claude Code), the agent sees the denial reason and aborts the install — the command never runs.

Claude Code Plugin

PkgDiet is packaged as a Claude Code plugin featuring bundled MCP tools, slash commands (/pkgdiet:check, /pkgdiet:audit), skill definitions, and PreToolUse execution hooks:

bash
/plugin install github:om-tajne/pkgdiet

(Or /plugin install pkgdiet@claude-plugins-official once listed in the official directory)


🔄 The 3-Phase Policy Loop

PkgDiet uses a shared core engine (@pkgdiet/core) so all interfaces apply the same evaluation logic.

  1. Repository Policy: A single .pkgdietrc.json file dictates what is allowed, warned, or blocked for your project.
  2. Agent Hook (Enforce): Native PreToolUse hooks for Claude Code and Codex intercept install commands before execution. MCP check_dependency instructs other agents before they write the command.
  3. CI Gate (Fallback): npx pkgdiet ci runs in GitHub Actions, diffs package.json against the base commit, and fails the PR if any added package violates policy.

🛠️ CLI Commands

CommandDescription
auditAudit existing dependencies for policy, health, size, and unused-package signals
check <pkgs...>Evaluate npm packages against this repository's dependency policy (ALLOW, WARN, BLOCK)
demoDemonstrate local policy evaluation against simulated risky package installs
mcpStart the local MCP JSON-RPC server over stdio for MCP-compatible AI coding agents
ciEnforce policy for dependency changes introduced by this branch (lockfile-diff-based)
initSet up PkgDiet in this project — creates policy, CI workflow, and all AI agent configs
setupInteractive wizard for creating a starter .pkgdietrc.json policy
agent-setupConfigure PkgDiet for AI coding agents and supported install guards
alternativesBrowse the PkgDiet alternatives dataset
driftScan project for dependency health drift over time
prGenerate a reviewer-ready pull request template for adding PkgDiet to your repository
policy-checkValidate the repository's .pkgdietrc.json policy
cacheManage the local PkgDiet registry cache

⚙️ Configuration (.pkgdietrc.json)

json
{  "minHealthScore": 70,  "warnHealthScore": 80,  "blockDeprecated": true,  "blockKnownVulnerabilities": true,  "blockTyposquats": false,  "requirePinnedVersions": true,  "maxPackageSizeBytes": 15728640,  "failOn": "BLOCK",  "securityMode": "fail-open",  "blockedPackages": ["moment", "request"],  "internalNamePrefixes": [],  "environments": {    "ci": { "minHealthScore": 80, "failOn": "BLOCK", "securityMode": "fail-closed" },    "dev": { "minHealthScore": 60, "failOn": "BLOCK", "securityMode": "fail-open" },    "staging": { "minHealthScore": 70, "failOn": "BLOCK", "securityMode": "fail-open" }  }}

🤖 MCP Integration

PkgDiet acts as a local Model Context Protocol (MCP) server over stdio.

When your AI coding agent connects to PkgDiet, it gains access to 5 read-only tools:

  • check_dependency: Evaluates an npm package against your local .pkgdietrc.json policy and returns structured ALLOW, WARN, or BLOCK verdicts with evidence.
  • check_dependencies: Bounded batch evaluation of up to 50 dependencies in a single tool call.
  • suggest_alternative: Queries PkgDiet's curated dataset to find modern, lighter, and maintained alternatives for blocked packages.
  • get_policy: Retrieves the active repository dependency policy rules and thresholds.
  • explain_verdict: Detailed breakdown of package scoring, maintenance signals, and diagnostic codes.

Built-in Verification (all free, zero paid API keys):

  • OSV vulnerability database — checks known CVEs via the public OSV advisory API
  • npm registry provenance — verifies signed npm attestations
  • Typosquat detection — edit-distance checks against popular package namespaces
  • Lockfile pinning validation — enforces deterministic package resolution
  • Dependency confusion detection — blocks internal-prefix packages found on public registries

💻 Requirements & Runtime Controls

  • Node.js: Requires Node.js 20 or later (Node.js >=20).
  • PKGDIET_NO_NETWORK=1: Runs completely offline using cached metadata; never contacts the external registry.
  • PKGDIET_TELEMETRY_DISABLED=1: Disables local telemetry and metrics collection.
  • PKGDIET_REGISTRY_URL: Overrides the default npm registry with a custom or private enterprise registry endpoint.
  • .pkgdiet-cache.json: Local cache file storing validated package metadata and advisory results.
  • .pkgdiet-metrics.json: Local metrics file for tracking audit performance and check history.

📦 Monorepo Architecture

PackagePathPurpose
@pkgdiet/corepackages/coreDeterministic policy evaluation engine, OSV matcher, and cache manager
@pkgdiet/mcppackages/mcpModel Context Protocol server exposing check_dependency and alternatives
pkgdietpackages/cliUnified command-line interface, agent setup wizards, and CI gate
GitHub Actionaction.ymlReusable GitHub Action for pull request dependency gating

⚖️ Legal, Privacy & Trademark Notice

  • Advisory Tooling: PkgDiet is open-source software provided under the MIT License. It evaluates publicly available npm metadata and local project configurations according to user-defined policy. It provides advisory analysis only and does not constitute formal legal, cybersecurity, compliance, or procurement advice.
  • Nominative Fair Use & Trademarks: All third-party trademarks, product names, logos, and brands (including npm, GitHub, GitHub Copilot, Claude / Claude Code, Cursor, Windsurf, Cline, Codex / OpenAI, and Google) are the property of their respective owners. Their mention in this repository is strictly for integration identification and compatibility under nominative fair use. No affiliation, endorsement, sponsorship, or partnership is claimed or implied.
  • Local-First Privacy: PkgDiet operates locally via stdio and local caching. It does not inspect, collect, or transmit proprietary codebase source code or credentials. For full terms, see LEGAL.md.

🔗 Documentation & Support

來源:README.md,提交 64ffcc9

工具

0
SourceWeft 曾在隔離沙箱中以無憑證方式啟動該軟體包一次,以列出其工具,但未能完成:伺服器未能啟動。

版本歷史

2
  1. v2.0.1最新Oct 30, 2026
  2. v1.2.1Sep 16, 2026

評分

已滿足適用檢查項的 50%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分

    請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。

  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具15 分

    請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。

  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分