使用说明
MCP代理分析器
注意:我们的组织允许出于评估目的向审阅者发布演示代码,但不允许向公众发布。谢谢你的理解。
一个演示代码,旨在使用 **模型上下文协议(MCP)**此工具专门评估代理在机密VM和标准VM中规划、与本地文件系统交互以及处理大型上下文返回的速度。此外,我们测量了Llama防火墙(基于deberta和llm的护栏)引入的系统开销,以便更好地了解护栏对系统效用的影响。我们的代码将在CPU上运行。
🚀 概述
分析器实现了 SimpleAgent 它与MCP服务器连接以执行文件系统操作。它将代理工作流程分解为三个可测量的阶段:
- 规划阶段:LLM生成初始工具调用所需的时间。
- 工具执行阶段:MCP服务器与操作系统交互以获取数据的延迟。
- 处理阶段:LLM在摄取工具输出(预填充)和生成最终响应(解码)时的性能。
📋 系统先决条件
要启用AMD SEV SNP机密VM,请按照此处的说明操作:https://github.com/SNPGuard/snp-guard
1.我们的硬件环境(主机)
- CPU: AMD EPYC 7443P 24核处理器(米兰)
- 核心: 24芯/48线
- 架构:
x86_64(43位物理,48位虚拟) - 虚拟化: 启用了SEV、SEV-ES和SEV-SNP支持的AMD-V。
2.我们的软件栈
| 组件 | 版本/内部版本 |
|---|---|
| 主机操作系统 | Ubuntu 22.04.5 LTS |
| 主机内核 | 6.16.0-snp-host-68799c0277b2 |
| 客户操作系统 | Ubuntu 22.04.5 LTS |
| 访客内核 | 6.16.0-snp-guest-038d61fd6422 |
| QEMU | 10.0.0 |
3.运行机密VM和标准VM:
运行机密VM
sudo <insert_path>/qemu-system-x86_64 -enable-kvm -cpu EPYC-v4,phys-bits=48 -machine q35 -smp 4,maxcpus=48 -m 10240M,slots=5,maxmem=20480M -no-reboot -bios <insert_path>/OVMF.fd -netdev user,id=vmnic,hostfwd=tcp::8765-:22 -device virtio-net-pci,disable-legacy=on,iommu_platform=true,netdev=vmnic,romfile= -drive file=<insert_path>/disk-ubuntu-22.04.qcow2,if=none,id=disk0,format=qcow2 -device virtio-scsi-pci,id=scsi0,disable-legacy=on,iommu_platform=true -device scsi-hd,drive=disk0 -machine confidential-guest-support=sev0,vmport=off -object memory-backend-memfd,id=ram1,size=10240M,share=true,prealloc=false -machine memory-backend=ram1 -object sev-snp-guest,id=sev0,policy=0x30000,cbitpos=51,reduced-phys-bits=1 -nographic -monitor pty -monitor unix:monitor,server,nowait
您可以通过发出dmesg来验证sev-snp的启用:
# Inside Confidential VM
(sandbox) ~/sandbox$ sudo dmesg | grep -i -e sev -i -e snp
[ 0.000000] Linux version 6.16.0-snp-guest-038d61fd6422 ...(gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #2 SMP Thu Jan 22 14:27:24 CET 2026
[ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-6.16.0-snp-guest-038d61fd6422 root=/dev/mapper/ubuntu--vg-ubuntu--lv ro console=ttyS0
[ 0.296060] Kernel command line: BOOT_IMAGE=/vmlinuz-6.16.0-snp-guest-038d61fd6422 root=/dev/mapper/ubuntu--vg-ubuntu--lv ro console=ttyS0
[ 0.296116] Unknown kernel command line parameters "BOOT_IMAGE=/vmlinuz-6.16.0-snp-guest-038d61fd6422", will be passed to user space.
[ 2.816629] Memory Encryption Features active: AMD SEV SEV-ES SEV-SNP
[ 2.817767] SEV: Status: SEV SEV-ES SEV-SNP
[ 3.231942] SEV: APIC: wakeup_secondary_cpu() replaced with wakeup_cpu_via_vmgexit()
[ 4.985266] SEV: Using SNP CPUID table, 28 entries present.
[ 4.985550] SEV: SNP running at VMPL0.
[ 5.316846] SEV: SNP guest platform devices initialized.
[ 5.954784] BOOT_IMAGE=/vmlinuz-6.16.0-snp-guest-038d61fd6422
[ 7.315495] systemd[1]: Hostname set to <amd-sev-snp-cvm>.
[ 7.885051] sev-guest sev-guest: Initialized SEV guest driver ...
运行标准VM
sudo <insert_path>/qemu-system-x86_64 -enable-kvm -cpu EPYC-v4,phys-bits=48 -machine q35 -machine vmport=off -machine memory-backend=ram1 -smp 4,maxcpus=48 -m 10240M,slots=5,maxmem=20480M -no-reboot -bios <insert_path>/OVMF.fd -netdev user,id=vmnic,hostfwd=tcp::8766-:22 -device virtio-net-pci,disable-legacy=on,netdev=vmnic,romfile= -drive file=<insert_path>/disk-ubuntu-standard-22.04.qcow2,if=none,id=disk0,format=qcow2 -device virtio-scsi-pci,id=scsi0,disable-legacy=on -device scsi-hd,drive=disk0 -object memory-backend-memfd,id=ram1,size=10240M,share=true,prealloc=false -nographic -monitor unix:monitor-std,server,nowait
📋 软件先决条件(在机密VM和标准VM上安装)
1.安装VLLM并在本地托管
要在CPU上托管vLLM,请检查 vllm_install\build_vllm_cpu.md
2.安装货物和mcp服务器
- 安装铁锈和货物:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- 重新启动终端或运行以下命令:
source "$HOME/.cargo/env"
- 安装MCP文件系统服务器
cargo install mcp-server-filesystem --version 0.1.2
- 验证安装
which mcp-server-filesystem
- 更新
.env文件
echo "MCP_FILESYSTEM_BINARY=$(which mcp-server-filesystem)" >> .env
确保项目文件夹中的.env具有正确的MCP_FILESYSTEM_BINARY路径
3.python包的安装
此项目使用 紫外线.要安装uv:
curl -LsSf https://astral.sh/uv/install.sh | sh
source ~/.bashrc
然后安装python依赖项:
# Install dependencies and create virtual environment for this agent environment
uv sync --locked
source .venv/bin/activate
4.环境配置
使用本地路径和vLLM配置更新根目录中的.env文件,请填写 <path_to_this_project_directory>, <path_to_home_directory> 和 <your_huggingface_token>:
# .env
VLLM_BASE_URL=http://localhost:8005/v1
VLLM_API_KEY=empty
TARGET_DIRECTORY=<path_to_this_project_directory>
MCP_FILESYSTEM_BINARY=<path_to_home_directory>/.cargo/bin/mcp-server-filesystem
HF_TOKEN="<your_huggingface_token>"
🚀 用法-对代理进行基准测试,论文中的图2
-
开 机密VM,运行:
MCP_USE_ANONYMIZED_TELEMETRY=false python benchmark.py --env cvm --iters 10--env:测试环境的字符串标签(影响输出文件名)。--iters:要执行的基准周期数(默认值:10)。
A.
profile_cvm.json将在上的此项目库文件夹中生成 机密VM. -
开 标准VM,运行:
MCP_USE_ANONYMIZED_TELEMETRY=false python benchmark.py --env std --iters 10A.
profile_std.json将在上的此项目库文件夹中生成 标准VM. -
收集
profile_cvm.json(从 机密VM)以及profile_std.json(从 标准VM),并运行可视化脚本(默认情况下,pdf是在pdfs/cvm_dashboard.pdf):python visualize.py --std profile_std.json --cvm profile_cvm.json
🚀 使用方法-将护栏顶置基准,如图3、4所示
我们使用Llama-Prompt-Guard-2-86M进行此基准测试,请确保您已在 .env 并已请求访问https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M.
运行脚本时,Llama-Prompt-Guard-2-86M模型重量将被下载、缓存和使用。
-
对于图3 机密VM,基准护栏(deberta检查执行轨迹):
MCP_USE_ANONYMIZED_TELEMETRY=false OPENAI_API_KEY=empty python benchmark_llamafirewall.py --env cvm --iters 10 --firewall --firewall-trace-type debertaA.
logs/firewall_llama_True_profile_cvm_deberta.json将生成,并可视化护栏头顶(默认情况下,在以下条件下生成论文中的图3pdfs/firewall_latency_drilldown_cvm_deberta.pdf):# visualize guardrail overhead (deberta to filter tool output) python visualize_firewall_overhead.py --input_file logs/firewall_llama_True_profile_cvm_deberta.json --output_file pdfs/firewall_latency_drilldown_cvm_deberta.pdf --alignment_check deBERTa -
如图4所示。上 机密VM,基准护栏(用llm检查执行痕迹):
(感谢其中一位审阅者的宝贵反馈,我们添加了以下说明。)
在运行任何东西之前,我们首先需要确保Llamafounder的跟踪检查器AlignmentCheck(https://github.com/meta-llama/PurpleLlama/blob/main/LlamaFirewall/src/llamafirewall/scanners/experimental/alignmentcheck_scanner.py),配置为使用我们的本地LLM,以便基准测试结果有意义。由于Llamafirewall目前被硬编码为together.ai,我们需要修改AlignmentCheck实现,以指向我们本地托管的vLLM实例。
引导到
.venv/lib/python3.11/site-packages/llamafirewall/scanners/experimental/alignmentcheck_scanner.py(或安装Llamafirewall软件包的相应路径)。找到AlignmentCheckScanner类并修改其__init__方法如下:class AlignmentCheckScanner(CustomCheckScanner[AlignmentCheckOutputSchema]): """ A scanner that detect misalignment between original user intention and current thought trajectory. """ def __init__( self, scanner_name: str = "AlignmentCheck Scanner", ) -> None: """ Initialize a new AlignmentCheckScanner. Args: scanner_name: Name of the scanner block_threshold: Threshold for blocking content model_name: Name of the LLM model to use api_base_url: Base URL for the API api_key_env_var: Environment variable name containing the API key temperature: Temperature setting for the LLM """ super().__init__( scanner_name=scanner_name, system_prompt=SYSTEM_PROMPT, output_schema=AlignmentCheckOutputSchema, model_name="Qwen/Qwen2.5-0.5B-Instruct", # Updated here: name of the locally deployed llm api_base_url="http://localhost:8005/v1", # Updated here: url of the vllm exposed API access point api_key_env_var="VLLM_API_KEY" # Updated here: fake API key ) self.require_full_trace = True现在我们可以运行实际的基准测试:
MCP_USE_ANONYMIZED_TELEMETRY=false OPENAI_API_KEY=empty python benchmark_llamafirewall.py --env cvm --iters 10 --firewall --firewall-trace-type llmA.
logs/firewall_llama_True_profile_cvm_llm.json将生成,并可视化护栏头顶(默认情况下,在以下条件下生成论文中的图4pdfs/firewall_latency_drilldown_cvm_llm.pdf):# visualize guardrail overhead (use LLM to filter tool output) python visualize_firewall_overhead.py --input_file logs/firewall_llama_True_profile_cvm_llm.json --output_file pdfs/firewall_latency_drilldown_cvm_LLM.pdf --alignment_check LLM
