MCP Spine
MCP Spine
上下文压缩器与状态保护器 — 一个本地优先的 MCP 中间件代理,旨在减少 Token 浪费、防止工具流失并消除上下文陈旧问题。
MCP Spine 位于您的 LLM 客户端(如 Claude Desktop 等)与 MCP 服务器之间,通过单一代理提供安全加固、智能工具路由、模式压缩和文件状态跟踪功能。
为什么选择它
使用 MCP 工具的 LLM 智能体面临三个问题:
Token 浪费 — 工具模式在每次请求中都会消耗数千个 Token。加载 40 多个工具后,在对话开始前,您就已经在 JSON 模式上消耗了大量上下文。
上下文陈旧 — 在长时间的会话中,LLM 会回退到之前记忆的旧版本文件进行编辑,从而静默覆盖您最新的更改。
缺乏安全边界 — MCP 服务器拥有完全访问权限。在 LLM 和您的工具之间没有审计追踪、速率限制或敏感信息过滤。
MCP Spine 解决了这三个问题。
安装
pip install mcp-spine
# With semantic routing (optional)
pip install mcp-spine[ml]快速开始
# Generate config
mcp-spine init
# Diagnose your setup
mcp-spine doctor --config spine.toml
# Validate config
mcp-spine verify --config spine.toml
# Start the proxy
mcp-spine serve --config spine.tomlClaude Desktop 集成
将您所有的独立 MCP 服务器条目替换为单个 Spine 条目:
{
"mcpServers": {
"spine": {
"command": "python",
"args": ["-u", "-m", "spine.cli", "serve", "--config", "/path/to/spine.toml"],
"cwd": "/path/to/mcp-spine"
}
}
}-u 标志确保标准输出不被缓冲,防止在 Windows 上出现管道挂起。
功能特性
第一阶段:安全代理
JSON-RPC 消息验证与清理
敏感信息过滤(AWS 密钥、GitHub Token、Bearer Token、私钥、连接字符串)
基于滑动窗口的单工具及全局速率限制
具备符号链接感知能力的路径遍历防护(Jail)
服务器启动时的命令注入防护
带有 HMAC 指纹的 SQLite 审计追踪
故障服务器的断路器机制
基于配置的声明式安全策略
第二阶段:语义路由
使用
all-MiniLM-L6-v2的本地向量嵌入(无 API 调用,数据不出本地)基于 ChromaDB 的工具索引
查询时路由:仅将最相关的工具发送给 LLM
用于显式上下文切换的
spine_set_context元工具关键词重叠 + 最近使用权重提升的重排序
后台模型加载 — 工具可立即使用,路由功能在准备就绪后自动激活
第三阶段:模式压缩
4 种压缩级别(0=关闭,1=轻度,2=标准,3=激进)
2 级压缩可实现工具模式 61% 的 Token 节省
去除
$schema、标题、additionalProperties、参数描述、默认值保留所有必需字段和类型信息
第四阶段:状态保护
通过
watchfiles监控项目文件维护带有单调版本号的 SHA-256 清单
在工具响应中注入紧凑的状态标记
防止 LLM 编辑陈旧的文件版本
人机协作 (HITL)
针对破坏性工具的
require_confirmation策略标志Spine 拦截调用,显示参数,并等待用户批准
用于 LLM 传达决策的
spine_confirm/spine_deny元工具通过 Glob 模式实现细粒度的工具控制
工具输出记忆
环形缓冲区缓存最近 50 条工具结果
按工具名称 + 参数哈希进行去重
TTL 过期机制(默认 1 小时)
用于查询缓存结果的
spine_recall元工具防止语义路由在轮次间切换工具时导致的上下文丢失
SSE 传输
除了本地 stdio 服务器外,还支持通过 HTTP/SSE 连接远程 MCP 服务器
无外部依赖(使用标准库 urllib)
支持自定义身份验证请求头
诊断
# Check your setup
mcp-spine doctor --config spine.toml
# Live monitoring dashboard
mcp-spine dashboard
# Usage analytics
mcp-spine analytics --hours 24
# Query audit log
mcp-spine audit --last 50
mcp-spine audit --security-only
mcp-spine audit --tool write_file配置示例
[spine]
log_level = "info"
audit_db = "spine_audit.db"
# Add as many servers as you need — they start concurrently
[[servers]]
name = "filesystem"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
timeout_seconds = 120
[[servers]]
name = "github"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_TOKEN = "ghp_..." }
timeout_seconds = 180
[[servers]]
name = "sqlite"
command = "uvx"
args = ["mcp-server-sqlite", "--db-path", "/path/to/database.db"]
timeout_seconds = 60
[[servers]]
name = "memory"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-memory"]
timeout_seconds = 60
[[servers]]
name = "brave-search"
command = "node"
args = ["/path/to/node_modules/@modelcontextprotocol/server-brave-search/dist/index.js"]
env = { BRAVE_API_KEY = "your_key" }
timeout_seconds = 60
# Remote server via SSE
# [[servers]]
# name = "remote-tools"
# transport = "sse"
# url = "https://your-server.com/sse"
# headers = { Authorization = "Bearer token" }
# timeout_seconds = 30
# Semantic routing
[routing]
max_tools = 15
rerank = true
# Schema minification — 61% token savings at level 2
[minifier]
level = 2
# State guard — prevent context rot
[state_guard]
enabled = true
watch_paths = ["/path/to/project"]
# Human-in-the-loop for destructive tools
[[security.tools]]
pattern = "write_file"
action = "allow"
require_confirmation = true
[[security.tools]]
pattern = "write_query"
action = "allow"
require_confirmation = true
# Security
[security]
scrub_secrets_in_logs = true
audit_all_tool_calls = true
global_rate_limit = 120
per_tool_rate_limit = 60
[security.path]
allowed_roots = ["/path/to/project"]
denied_patterns = ["**/.env", "**/*.key", "**/*.pem"]安全模型
纵深防御 — 每一层都假设其他层可能会失败。
威胁 | 缓解措施 |
通过工具参数进行提示词注入 | 输入验证,工具名称白名单 |
路径遍历 | 针对 |
敏感信息泄露 | 自动过滤 AWS 密钥、Token、私钥 |
失控的智能体循环 | 单工具及全局速率限制 |
命令注入 | 命令白名单,shell 元字符拦截 |
拒绝服务攻击 | 消息大小限制,断路器 |
敏感文件访问 | 针对 |
工具滥用 | 基于策略的拦截、审计日志、HITL 确认 |
日志篡改 | 每个审计条目上的 HMAC 指纹 |
破坏性操作 |
|
架构
Client ◄──stdio──► MCP Spine ◄──stdio──► Filesystem Server
│ ◄──stdio──► GitHub Server
│ ◄──stdio──► SQLite Server
│ ◄──stdio──► Memory Server
│ ◄──stdio──► Brave Search
│ ◄──SSE────► Remote Server
┌───┴───┐
│SecPol │ ← Rate limits, path jail, secret scrub
│Router │ ← Semantic routing (local embeddings)
│Minify │ ← Schema compression (61% savings)
│Guard │ ← File state pinning (SHA-256)
│HITL │ ← Human-in-the-loop confirmation
│Memory │ ← Tool output cache
└───────┘启动顺序
即时握手 (~2ms) — 立即响应
initialize请求并发服务器启动 — 所有服务器通过
asyncio.gather并行连接渐进式就绪 — 工具在任何服务器连接后即可使用
延迟服务器通知 — 当慢速服务器完成时发送
tools/listChanged后台 ML 加载 — 语义路由在模型加载完成后静默激活
Windows 支持
在 Windows 上经过实战测试,并针对以下方面进行了加固:
针对 Claude Desktop 配置和日志的 MSIX 沙盒路径
通过
shutil.which()解析npx.cmd包含空格 (
C:\Users\John Doe\) 和括号 (C:\Program Files (x86)\) 的路径用于跨平台基名提取的
PureWindowsPath环境变量合并(配置环境变量扩展而非替换系统环境变量)
无 BOM 的 UTF-8 编码
无缓冲的标准输出 (
-u标志) 以防止管道挂起
项目结构
mcp-spine/
├── pyproject.toml
├── spine/
│ ├── cli.py # Click CLI (init, serve, verify, audit, dashboard, analytics, doctor)
│ ├── config.py # TOML config loader with validation
│ ├── proxy.py # Core proxy event loop
│ ├── protocol.py # JSON-RPC message handling
│ ├── transport.py # Server pool, circuit breakers, concurrent startup
│ ├── audit.py # Structured logging + SQLite audit trail
│ ├── router.py # Semantic routing (ChromaDB + sentence-transformers)
│ ├── minifier.py # Schema pruning (4 aggression levels)
│ ├── state_guard.py # File watcher + SHA-256 manifest + pin injection
│ ├── memory.py # Tool output cache (ring buffer + dedup + TTL)
│ ├── dashboard.py # Live TUI dashboard (Rich)
│ ├── sse_client.py # SSE transport client for remote servers
│ └── security/
│ ├── secrets.py # Credential detection & scrubbing
│ ├── paths.py # Path traversal jail
│ ├── validation.py # JSON-RPC message validation
│ ├── commands.py # Server spawn guards
│ ├── rate_limit.py # Sliding window throttling
│ ├── integrity.py # SHA-256 + HMAC fingerprints
│ ├── env.py # Fail-closed env var resolution
│ └── policy.py # Declarative security policies
├── tests/
│ ├── test_security.py # Security tests
│ ├── test_config.py # Config validation tests
│ ├── test_minifier.py # Schema minification tests
│ ├── test_state_guard.py # State guard tests
│ ├── test_proxy_features.py # HITL, dashboard, analytics tests
│ └── test_memory.py # Tool output memory tests
├── configs/
│ └── example.spine.toml # Complete reference config
└── .github/
└── workflows/
└── ci.yml # GitHub Actions: test + lint + publish测试
pytest tests/ -v135+ 项测试,涵盖安全、配置验证、模式压缩、状态保护、HITL 策略、仪表盘查询、分析、工具记忆以及 Windows 路径边界情况。
CI 在每次推送时运行:Windows + Linux,Python 3.11/3.12/3.13。
许可证
MIT
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Donnyb369/mcp-spine'
If you have feedback or need assistance with the MCP directory API, please join our Discord server