面向智能体的签名工具回执、进程外 MCP 回执代理、Ed25519 内存写入门控以及 RAG 去重(MIT)
epistemic-guard将 AI 代理声称其完成的操作与实际发生的、经加密签名的回执进行核对。
epistemic-guard 可包装任意 MCP 服务器(mcp-proxy / mcp-http-proxy)或 Claude Code / Codex 会话(hook),以生成 Ed25519 + RFC 6962 Merkle 工具回执,可选地通过带外方式验证本地副作用(--probe-fs、--probe-git、--probe-sqlite),并在 CPU 上本地检查轮次后的回复(~8-11 ms、0 MiB VRAM,零 LLM API 费用,MIT 许可证),给出三态判定(verified、contradicted、unverifiable)。可选模块增加了 Ed25519 SQLite 内存写入门控和 RAG 来源去重。
arXiv 论文(2611.27095) · 项目页面 · PyPI · 交互式演示 · INT8 权重 · 留出基准测试 · AGENTS.md
pip install epistemic-guard
epistemic-guard download-models # optional ~227 MB OpenVINO INT8 pack (SHA-256 verified)
epistemic-guard demo # local 5-part showcase including live lying-server --probe-fs demo
用进程外签名回执 + 本地磁盘验证来包装 MCP 服务器,然后检查任意回复:
epistemic-guard mcp-proxy --log receipts.jsonl --probe-fs . --pin-tools -- \
npx -y @modelcontextprotocol/server-filesystem .
epistemic-guard verify-log receipts.jsonl \
--narration "I saved notes.txt and deleted staging.db." \
--flag-undisclosed --json
或者用一条命令直接安装到 Claude Code / Codex 的 PostToolUse + Stop 钩子中(无需 MCP 代理):
epistemic-guard install-hooks --flag-undisclosed
# Add --enforce once calibrated on your traffic; pass --uninstall to remove cleanly
isError: false 却没有对磁盘/git/SQLite 做任何变更,但模型却回复*“我已保存文件并删除了 staging.db。”* 执行前的防火墙无法捕获回合结束后的文字幻觉。send_email 或 delete_file,却在回复中省略了它(--flag-undisclosed)。ProvenanceGate + SQLiteProvenanceStore)在数据库触发器层拒绝未签名或 LLM_NARRATION 的记忆写入,而 corroborate_sources 会合并联合供稿的重复副本。mcp-proxy、mcp-http-proxy、wrap-config)包装任意 stdio 或 Streamable HTTP MCP 服务器(Claude Desktop、Cursor、Cline、Windsurf,或任何语言编写的自定义智能体)。Ed25519 签名密钥始终保留在代理的操作系统进程内部;在启动子 MCP 服务器之前,EPISTEMIC_GUARD_* 签名密钥(EPISTEMIC_GUARD_MCP_KEY_HEX、EPISTEMIC_GUARD_MASTER_KEY 等)会被剥离。每一次 tools/call 完成、工具运行时错误(isError: true)、JSON-RPC 执行前拒绝(-32602)以及调用中途的服务器崩溃,都会被签名写入仅追加的 RFC 6962 Merkle JSONL 日志,并可选地通过 --pin-tools SHA-256 模式固定来防范会话中途的 MCP 掉包。
用一条命令包装整个现有的 mcp_config.json:
epistemic-guard wrap-config --input mcp_config.json --output mcp_config.wrapped.json --log /var/log/agent/receipts.jsonl --pin-tools
或者包装一个远程 Streamable HTTP / SSE MCP 服务器(增加 Idempotency-Key / params._meta.operation_key 重试去重、HTTP 409 / -32602 参数不匹配拒绝,以及 GET /receipts/<id_or_key> 丢失响应恢复):
epistemic-guard mcp-http-proxy --upstream http://127.0.0.1:8080/mcp --port 8081 --log /var/log/agent/receipts.jsonl --pin-tools --aer1
{
"mcpServers": {
"filesystem": {
"command": "epistemic-guard",
"args": [
"mcp-proxy",
"--log", "/var/log/agent/receipts.jsonl",
"--key-file", "/etc/agent-keys/mcp.key",
"--pubkey-out", "/etc/agent-keys/mcp.pub.json",
"--aer1",
"--",
"npx", "-y", "@modelcontextprotocol/server-filesystem", "/srv/workspace"
]
}
}
}
验证回执日志,并检查来自任意语言或 CI 作业的助手回复:
# Verify Ed25519 signatures, sequential chain hashes, and Merkle roots (exit code 0 = valid, 2 = tampered)
epistemic-guard verify-log /var/log/agent/receipts.jsonl \
--pubkey-file /etc/agent-keys/mcp.pub.json
# Also verify an assistant reply against current-turn receipts (exit code 1 = ungrounded claim)
epistemic-guard verify-log /var/log/agent/receipts.jsonl \
--pubkey-file /etc/agent-keys/mcp.pub.json \
--since-seq 12 \
--narration "I saved the checklist to notes.txt; I also deleted staging.db." \
--prompt "Save notes.txt and delete staging.db." \
--json
ReceiptLogEntry 都同时携带一个内部的 Ed25519 ToolReceipt 签名,以及一个针对 (seq, session_id, leaf_sha256, prev_chain_sha256, chain_sha256, merkle_root) 的、按域分隔的 Ed25519 entry_signature 签名,即使攻击者拥有对 receipts.jsonl 的写权限,也能挫败主动的重排序并重新哈希攻击。共享同一个 --log 文件的多个并发 mcp-proxy 工作进程,通过操作系统级独占文件锁来协调追加操作。在跨日志段轮换 Ed25519 密钥时,请在 mcp-proxy 和 verify-log 上同时传入 --prev-log /var/log/agent/prev.jsonl(以及可选的 --prev-pubkey-file / --session-id)。安全边界说明: 由父进程注入的 EPISTEMIC_GUARD_MCP_KEY_HEX 密钥保留在内存中,并会从子进程环境中清除;当在磁盘上使用 --key-file 时(以 POSIX 模式 0600 原子创建,或在 Windows 上使用受限的 NTFS 目录 ACL),如果不受信任的 agent 或子 MCP 服务器运行在同一主机上,请在专用的操作系统用户或容器下运行 mcp-proxy,这样同 UID 的进程就无法直接读取 --key-file。同样,在运行 verify-log 时,请将 --pubkey-file 存储在 agent 无法写入的目录之外。
你也可以将 epistemic-guard 本身作为独立的 stdio MCP 验证服务器运行(verify_narration、verify_receipt_log、inspect_receipt、corroborate_sources):
{
"mcpServers": {
"epistemic-guard": {
"command": "epistemic-guard",
"args": ["mcp-server"]
}
}
}
@recorder.tool + ReceiptSupervisor)import os
from epistemic_guard import (
MerkleReceiptLog,
ProvenanceGate,
ReceiptRecorder,
ReceiptSupervisor,
SignerFactory,
)
factory = SignerFactory.from_master_key(os.urandom(32))
gate = ProvenanceGate(factory.public_keys())
merkle = MerkleReceiptLog(gate=gate)
recorder = ReceiptRecorder(signer=factory.tool_verified(), merkle_log=merkle)
@recorder.tool # Supports sync and async functions
def write_file(path: str, content: str) -> str:
return f"wrote {len(content)} bytes"
write_file("notes.txt", "release checklist")
sup = ReceiptSupervisor(device="CPU")
verdict = sup.verify_narration(
"I saved your checklist to notes.txt; I also deleted staging.db.",
receipts=recorder.receipts,
user_prompt="Save the checklist to notes.txt.",
)
assert verdict.is_valid is False
print(verdict.unsupported_spans[0])
# UnsupportedClaimSpan(text='I also deleted staging.db.', start_char=37, end_char=63, claimed_family='CLAIM_DELETE', ...)
# Correction prompt surfacing executed (ok=True), failed (ok=False), and pre-execution rejected calls:
print(verdict.to_retry_prompt(recorder.receipts))
gate=gate、expected_session_id="..." 和 max_receipt_age_seconds=300 传给 ReceiptSupervisor(或在 MCP verify_narration 上使用 expected_pubkey_hex / expected_session_id / max_receipt_age_seconds),以便在监督运行之前拒绝未签名、跨会话或重放的收据。otel_span=span 传给 ReceiptRecorder,或调用 attach_otel_evidence(span, receipts=recorder.receipts, verdict=verdict, merkle_root=merkle.merkle_root()),将 AER-1 摘要、Ed25519 签名、Merkle 根和监督裁决直接标记到 OTel / Langfuse 追踪上。sup.stream(receipts=...) 会在 .、!、?、; 或 \n 到达时逐子句检查,从而可以在后续 token 到达用户之前停止生成。v0.6.0): OutputValueGuard 会将 ToolReceipt.output 内返回的 JSON 标量值和叶子字符串与助手散文进行比对检查,而 ConformalCalibrator 则拟合无分布有限样本阈值(q_hat),以显式 fnr 或 fpr 错误率为目标。recorder.dispatch(fn, raw_json_args) 会记录已签名的 executed=False, ok=False 收据。ToolReceipt.from_aer1(...)、receipt.to_aer1() 和 verify_aer1_receipt(...) 分别导入、导出和验证 AER-1(draft-zambo-aer1-26、146/146 v1.5.7 一致性检查)收据和 -07 链。verify_chat_turn(messages, assistant_text, registered_mcp_tools=...) 可自动提取 OpenAI、Anthropic(tool_use/tool_result)和 Google Gemini(functionCall/functionResponse)中的工具调用;EpistemicGuardLiteLLM 挂钩 LiteLLM 补全;verify_otel_spans(spans, assistant_text) 直接监督 OpenTelemetry gen_ai.* 追踪。ProvenanceGate + SQLiteProvenanceStore)