Navil Policy — MCP Tool Scoping and Cost Optimization
GitHub's MCP server exposes 90+ tools consuming 50,000+ tokens before your agent thinks about your question. At scale, MCP tool definitions can exceed model context limits entirely.
Navil Policy solves this by controlling which tools each agent sees in tools/list responses. A code review agent sees 3 tools instead of 90. That is a 94% reduction in schema tokens — cheaper inference, faster responses, and a smaller attack surface.
When to Use This Skill
- - User complains about token costs or context window bloat from MCP
- User has multiple MCP servers and agents are seeing too many tools
- User wants to restrict which tools specific agents can access
- User says "MCP is too expensive" or "my context is full of tool schemas"
- User asks about least privilege, tool scoping, or agent permissions
- User wants to auto-generate policies from observed agent behavior
How Tool Scoping Works
Navil's policy engine sits in the proxy layer (set up by navil-shield). When an agent requests tools/list, Navil filters the response based on the policy file before the agent ever sees it.
This means:
- - The agent doesn't know the filtered tools exist — they never enter the context window
- Token savings are immediate — fewer tool schemas = less context consumed
- Security improves — agents can't call tools they can't see
- No code changes — the policy file controls everything
Creating a Policy
Step 1: Check if Navil Shield is Active
CODEBLOCK0
If navil is not installed, install it first:
CODEBLOCK1
If MCP servers are not yet wrapped with navil shim, the policy engine cannot filter tool lists. Recommend installing navil-shield first.
Step 2: Observe Current Tool Usage
To see what tools are currently being exposed to agents:
CODEBLOCK2
Step 3: Generate a Starter Policy
Navil can auto-generate policies by watching how agents actually use tools:
CODEBLOCK3
This creates ~/.navil/policy.auto.yaml based on observed baselines. Review it, then copy rules you want to keep into ~/.navil/policy.yaml.
Step 4: Write Custom Policy
For manual policy creation, create ~/.navil/policy.yaml:
CODEBLOCK4
Step 5: Apply and Test
CODEBLOCK5
Step 6: Monitor Policy Decisions
View the live decision log to verify policies are working:
CODEBLOCK6
This shows pending auto-generated rules with confidence scores. Accept the ones that make sense.
Step 7: Rollback if Needed
CODEBLOCK7
This undoes auto-generated policy changes. Your manually written policy.yaml is never modified by the auto-generator.
Token Savings Calculator
Present this to the user when they ask about cost:
| Scenario | Tools Exposed | Approx Schema Tokens | With Navil Scoping | Savings |
|---|
| GitHub MCP (all tools) | 90+ | ~50,000 | 3 tools (~1,600) | 97% |
| Database MCP |
106 | ~54,600 | 8 tools (~4,100) |
92% |
| Full enterprise stack (5 servers) | 300+ | ~150,000+ | 20 tools (~10,000) |
93% |
At typical API pricing, scoping a heavy MCP setup saves $50-200/month in token costs alone.
Policy Templates
Navil ships with community templates for common MCP servers. Ask the user which servers they use, then suggest the appropriate template:
- - GitHub MCP: Read-only, code-review, full-access scopes
- Filesystem MCP: Read-only, workspace-scoped, full-access scopes
- kubectl MCP: View-only, namespace-scoped, admin scopes
Important Notes
- -
policy.yaml (manual rules) always takes precedence over policy.auto.yaml (generated rules) - Policies filter what agents see in
tools/list, separate from what they can call - The
default scope with allow: "*" ensures backward compatibility - Scoped responses are cached with 60s TTL in the Rust proxy — near-zero performance cost
Links
- - Policy documentation: https://github.com/navilai/navil#tool-scoping
- Community policy templates: https://github.com/navilai/navil/tree/main/policies
- Token cost guide: https://navil.ai/docs/token-costs
Navil Policy — MCP工具范围界定与成本优化
GitHub的MCP服务器在您的智能体思考问题之前,就会暴露90多个工具,消耗超过50,000个token。在规模化场景下,MCP工具定义可能完全超出模型上下文限制。
Navil Policy通过控制每个智能体在tools/list响应中看到哪些工具来解决这个问题。一个代码审查智能体只会看到3个工具,而不是90个。这减少了94%的模式token——推理成本更低,响应速度更快,攻击面更小。
何时使用此技能
- - 用户抱怨MCP带来的token成本或上下文窗口膨胀
- 用户拥有多个MCP服务器,智能体看到太多工具
- 用户想要限制特定智能体可以访问哪些工具
- 用户说MCP太贵了或我的上下文充满了工具模式
- 用户询问最小权限、工具范围界定或智能体权限
- 用户想要根据观察到的智能体行为自动生成策略
工具范围界定如何工作
Navil的策略引擎位于代理层(由navil-shield设置)。当智能体请求tools/list时,Navil在智能体看到响应之前根据策略文件过滤响应。
这意味着:
- - 智能体不知道被过滤工具的存在——它们永远不会进入上下文窗口
- 立即节省token——更少的工具模式 = 更少的上下文消耗
- 安全性提升——智能体无法调用它们看不到的工具
- 无需修改代码——策略文件控制一切
创建策略
步骤1:检查Navil Shield是否激活
bash
navil --version
如果未安装navil,请先安装:
bash
pip install navil --break-system-packages 2>/dev/null || pip install navil
如果MCP服务器尚未使用navil shim包装,策略引擎无法过滤工具列表。建议先安装navil-shield。
步骤2:观察当前工具使用情况
要查看当前向智能体暴露了哪些工具:
bash
navil policy check --tool * --agent default --action list
步骤3:生成初始策略
Navil可以通过观察智能体实际使用工具的方式自动生成策略:
bash
navil policy auto-generate
这将根据观察到的基线创建~/.navil/policy.auto.yaml。审查它,然后将您想要保留的规则复制到~/.navil/policy.yaml中。
步骤4:编写自定义策略
对于手动创建策略,创建~/.navil/policy.yaml:
yaml
示例:按工作流范围界定工具
scopes:
code-review:
allow:
- get
pullrequest
- list_files
- create
reviewcomment
description: 代码审查智能体只看到与PR相关的工具
deploy:
allow:
- create_deployment
- getdeploymentstatus
description: 部署智能体只看到部署工具
read-only:
allow:
- get_*
- list_*
- search_*
description: 只读智能体无法修改任何内容
default:
allow: *
description: 向后兼容——无限制访问
每个智能体的速率限制
rate_limits:
default:
requests
perminute: 60
deploy:
requests
perminute: 10
步骤5:应用并测试
bash
navil policy check --tool create_deployment --agent code-review --action call
预期:DENIED — code-review范围不包括部署工具
navil policy check --tool getpullrequest --agent code-review --action call
预期:ALLOWED
步骤6:监控策略决策
查看实时决策日志以验证策略是否正常工作:
bash
navil policy suggest
这将显示带有置信度分数的待处理自动生成规则。接受那些合理的规则。
步骤7:必要时回滚
bash
navil policy rollback
这将撤销自动生成的策略更改。您手动编写的policy.yaml永远不会被自动生成器修改。
Token节省计算器
当用户询问成本时,向他们展示以下内容:
| 场景 | 暴露的工具数 | 大约的模式Token数 | 使用Navil范围界定 | 节省 |
|---|
| GitHub MCP(所有工具) | 90+ | ~50,000 | 3个工具(~1,600) | 97% |
| 数据库MCP |
106 | ~54,600 | 8个工具(~4,100) |
92% |
| 完整企业栈(5个服务器) | 300+ | ~150,000+ | 20个工具(~10,000) |
93% |
按典型的API定价,对重型MCP设置进行范围界定,仅token成本每月就可节省50-200美元。
策略模板
Navil附带常见MCP服务器的社区模板。询问用户使用哪些服务器,然后建议相应的模板:
- - GitHub MCP:只读、代码审查、完全访问范围
- 文件系统MCP:只读、工作空间范围、完全访问范围
- kubectl MCP:仅查看、命名空间范围、管理员范围
重要说明
- - policy.yaml(手动规则)始终优先于policy.auto.yaml(生成规则)
- 策略过滤智能体在tools/list中看到的内容,与它们可以调用的内容分开
- 使用allow: *的default范围确保向后兼容
- 范围界定的响应在Rust代理中以60秒TTL缓存——性能成本接近零
链接
- - 策略文档:https://github.com/navilai/navil#tool-scoping
- 社区策略模板:https://github.com/navilai/navil/tree/main/policies
- Token成本指南:https://navil.ai/docs/token-costs