YubiKey MCP服务器
通过模型上下文协议为AI代理提供硬件支持的加密安全
______________________________________________________________________
这是什么?
一 MCP(模型上下文协议)服务器 它允许AI助手与YubiKey硬件安全令牌进行交互。将其视为AI工具(如Claude、GitHub Copilot或任何兼容MCP的客户端)和YubiKey设备之间的桥梁。
30秒概述
如果没有这个项目:
- AI助手无法与硬件安全设备交互
- 您可以手动使用命令行工具来管理YubiKeys
- 安全操作无法通过人工智能实现自动化
对于这个项目:
- AI助手可以通过自然对话发现和管理您的YubiKeys
- 通过语音/文本生成OpenPGP密钥、配置应用程序和设置安全策略
- 启用或禁用NFC,需要触摸操作,管理PIN码-所有这些都是通过AI实现的
- 您的YubiKey可以访问AI工作流程,同时保持硬件安全
- 未来:签署文档、管理OTP、使用PIV证书等
______________________________________________________________________
什么是MCP?
模型上下文协议(MCP) 就像人工智能助手的USB端口一样,它是人工智能工具连接外部服务和数据的标准方式。
就像USB可以让你将任何设备插入任何计算机一样,MCP可以让你把任何工具(比如你的YubiKey)插入任何支持该协议的AI助手。
MCP有三个主要概念:
- 工具 -AI可以调用的函数(例如,“列出YubiKeys”、“生成OTP”)
- 资源 -AI可以读取的数据(例如,可用证书、设备信息)
- 提示 -常见工作流的预构建模板
为什么使用MCP而不是直接CLI访问?
好问题! AI代理 *可以* 技术使用 ykman 命令直接。以下是MCP更好的原因:
无MCP(直接命令行界面)
# AI must figure out commands, parse text output, handle errors
ykman list
ykman --device 12345 oath accounts code "GitHub:user"问题:
- ❌ 无安全 -代理具有完全的shell访问权限(可以运行任何命令)
- ❌ 无法发现 -代理不知道有哪些操作可用
- ❌ 容易出错 -必须解析文本输出,猜测命令语法
- ❌ 特定于平台 -每种AI工具的不同实现
使用MCP
# AI discovers and calls structured functions
list_yubikeys() → {"status": "success", "devices": [...]}
generate_otp(service="GitHub", account="user") → {"code": "123456"}优点:
- ✅ 安全和沙盒 -代理只能执行您明确允许的操作
- ✅ 可发现性 -AI会自动看到可用的工具及其参数
- ✅ 结构化数据 -JSON响应而不是解析文本
- ✅ 工作流整合 -一个工具=完整的工作流程(例如。,
sign_document处理证书选择→ 签署→ 验证) - ✅ 跨平台 -同样的工具也适用于Claude Code、VS Code、Rider和web应用程序
- ✅ 情境感知 -参考资料显示了YubiKey在运行命令之前可以做什么
现实世界类比: MCP就像提供一个带有文档端点的REST API,而不是提供根外壳访问并说“搞清楚”。两者都可以完成任务,但MCP更安全、更清晰、更容易正确使用。
______________________________________________________________________
快速开始
先决条件
- Python 3.10+ 安装
- 紫外线 包管理器(或pip)
- YubiKey 硬件(测试可选,实际操作所需)
- yubikey经理 (
ykman)已安装:
pip install yubikey-manager安装与测试
- 克隆并导航到hello world示例:
cd src/hello-world- 安装依赖项:
uv sync- MCP检验员测试:
uv run mcp dev server.py这将打开一个web界面,您可以在其中交互式地测试工具。
与AI工具集成
此MCP服务器可与多个AI平台配合使用:
克劳德代码(CLI)
配置文件: .mcp.json 在项目根中
{
"mcpServers": {
"yubikey-hello-world": {
"command": "uv",
"args": ["--directory", "/path/to/yubikit-mcp/src/hello-world", "run", "server.py"]
}
}
}VS代码+GitHub副本
配置文件: .vscode/mcp.json
{
"servers": {
"yubikey-hello-world": {
"type": "stdio",
"command": "uv",
"args": ["--directory", "/path/to/yubikit-mcp/src/hello-world", "run", "server.py"]
}
}
}JetBrains Rider/IntelliJ
首选 Settings → Tools → AI Assistant → Model Context Protocol (MCP) 并添加:
{
"mcpServers": {
"yubikey-hello-world": {
"command": "uv",
"args": ["--directory", "/path/to/yubikit-mcp/src/hello-world", "run", "server.py"]
}
}
}看 .vscode/Interesting file paths.md 查看详细的配置位置。
______________________________________________________________________
运作原理
架构概述
┌─────────────────┐ ┌──────────────────┐ ┌─────────────┐
│ AI Assistant │ ◄─MCP──►│ YubiKey MCP │ ◄─CLI──►│ YubiKey │
│ (Claude/Copilot)│ │ Server (Python) │ │ Hardware │
└─────────────────┘ └──────────────────┘ └─────────────┘- AI助手 (Claude Code、VS Code、Rider)发送MCP请求
- MCP服务器 (此项目)将请求转换为
ykman命令 - 伊克曼 与YubiKey硬件通信
- 结果通过链流回人工智能
当前实施情况
这 src/hello-world/server.py 提供了一套按功能组织的全面工具:
🔍 设备发现和信息
hello_yubikey-检查是否安装了ykman并显示版本list_yubikeys-列出所有已连接的YubiKey设备及其详细信息get_yubikey_info-获取详细的固件版本、外形尺寸和USB接口信息list_yubikey_applications-查看通过USB和NFC启用了哪些应用程序(OATH、PIV、FIDO2等)
⚙️ 设备配置
configure_yubikey_applications-通过USB或NFC传输启用或禁用应用程序(OATH、PIV、FIDO2、OTP、OpenPGP等)
🔐 OpenPGP(电子邮件和文件加密)
get_openpgp_info-查看OpenPGP应用程序状态、PIN重试计数器和密钥槽信息generate_openpgp_key-直接在YubiKey上生成RSA密钥对,用于电子邮件签名和加密set_openpgp_touch_policy-签名、加密或身份验证操作需要物理接触set_openpgp_pin_retries-配置锁定前允许多少次错误的PIN尝试
示例用法
配置AI助手后,您可以就YubiKey操作进行自然对话:
示例1:设备发现
You: "Can you list my connected YubiKeys?"
AI: Found 1 YubiKey(s):
- YubiKey 5 NFC (5.2.7) [OTP+FIDO+CCID] Serial: 16021303示例2:配置应用程序
You: "I want to enable OATH and PIV over NFC on my YubiKey"
AI: Successfully enabled OATH, PIV over NFC on YubiKey (Serial: 16021303)示例3:OpenPGP密钥生成
You: "Generate an OpenPGP key for john@example.com with name John Doe"
AI: Successfully generated rsa2048 OpenPGP key pair for John Doe
Key generation took 87 seconds. Your private keys never left the YubiKey!示例4:安全强化
You: "Require touch for all OpenPGP signing operations"
AI: Successfully set touch policy for SIG key to 'on'. You'll now need to
physically touch your YubiKey every time you sign an email or file.示例5:完成工作流-加密奶奶的秘密Cookie配方 🍪
You: "Encrypt grandma's secret cookie recipe"
AI: Let me help you encrypt that precious family recipe! First, let me check
your YubiKey setup...
✓ Found YubiKey 5 NFC (Serial: 16021303)
✓ Checking OpenPGP application status...
Hmm, I don't see an OpenPGP key on your YubiKey yet. Would you like me to
generate one? This will create a hardware-backed encryption key that never
leaves your YubiKey.
You: "Yes, generate one for me"
AI: Perfect! I'll generate a key for you. What's your name and email?
You: "Jane Doe, jane@familyrecipes.com"
AI: Great! Generating RSA-2048 key pair on your YubiKey...
⚠️ This will take about 90 seconds. Please don't remove your YubiKey!
🔑 Key generation in progress...
💡 You may need to touch your YubiKey if prompted...
✅ Success! Generated OpenPGP key for Jane Doe
Now encrypting "grandmas-secret-cookies.txt" to your public key...
✓ File encrypted → grandmas-secret-cookies.txt.gpg
🎉 Your recipe is now protected by military-grade hardware encryption!
Only your YubiKey can decrypt it. Even if someone steals your computer,
they can't read it without physically having your YubiKey.
Want me to test decryption to make sure it works?
You: "Yes, decrypt it!"
AI: Decrypting with your YubiKey...
👆 Please touch your YubiKey to authorize decryption...
✅ Decrypted successfully! Here's the secret:
"Grandma's Chocolate Chip Cookies 🍪
- 2 cups butter (must be room temperature!)
- 1 cup love
- 3 cups chocolate chips (Grandma always adds extra)
- SECRET INGREDIENT: A pinch of cinnamon ✨"
Your recipe is safe! The encrypted file can only be opened with your
YubiKey, even if you store it in the cloud or email it to yourself.______________________________________________________________________
项目结构
yubikit-mcp/
├── .mcp.json # Claude Code MCP config
├── .vscode/
│ ├── mcp.json # VS Code/Copilot MCP config
│ └── Interesting file paths.md # Config reference guide
├── src/
│ ├── hello-world/
│ │ ├── server.py # MCP server implementation
│ │ ├── pyproject.toml # Python dependencies
│ │ └── README.md # Detailed usage guide
│ └── mcp-store/ # Separate project: MCP Marketplace
│ └── README.md # See marketplace docs
├── CLAUDE.md # Developer guide for AI assistants
└── README.md # This file______________________________________________________________________
技术深度潜水
MCP服务器实现
服务器使用 FastMCP,一个用于构建MCP服务器的高级Python框架:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("yubikey-hello-world")
@mcp.tool()
async def list_yubikeys() -> dict:
"""List all connected YubiKeys"""
result = subprocess.run(["ykman", "list"], ...)
return {"status": "success", "devices": [...]}关键实施细节:
- 运输: 用于本地AI助手的stdio(标准输入/输出)
- 后端: 用途
ykmanCLI通过子流程 - 协议: 基于stdio的JSON-RPC 2.0
- 登录中: 使用Python
logging模块(从不print()-破坏stdio)
多设备处理
当多个YubiKey连接时,工具接受可选 device_serial 参数:
# Auto-select if only one device
devices = get_devices()
if len(devices) == 1:
use_device(devices[0])
else:
# Prompt user or return list
return {"error": "Multiple devices found", "devices": devices}安全考虑
- hello world中没有身份验证 -仅为基本示例
- 未来: OAuth 2.1+JWT生产验证
- 硬件绑定: 使用YubiKey证明将操作绑定到特定设备
- 审核日志记录: 跟踪所有安全操作
______________________________________________________________________
路线图和未来工具
✅ 已实施
- 设备管理:列出设备,获取详细信息,配置应用程序
- OpenPGP运营:密钥生成、触摸策略、PIN配置
- 多设备支持:当连接多个YubiKeys时,自动设备选择提示
🚧 在开发中
这些面向工作流的工具计划在未来发布:
1.PIV(智能卡和文件安全)
sign_document-使用PIV证书签署PDF、合同、报告encrypt_document-硬件支持的文档加密verify_document_signature-验证文件真实性generate_piv_certificate-为身份创建X.509证书authenticate_to_service-使用PIV进行API身份验证
2.OATH(双因素身份验证)
generate_otp_for_service-获取特定服务的TOTP/HOTP代码setup_totp_account-将新的2FA帐户添加到YubiKeylist_oath_accounts-查看所有存储的OATH凭据backup_oath_credentials-TOTP机密的安全备份
3.FIDO2/WWebAuthn
register_fido2_credential-为网站注册无密码登录list_fido2_credentials-查看已注册的FIDO2凭据delete_fido2_credential-删除特定凭据
4.代理人对代理人担保
sign_message-对AI代理消息进行加密签名verify_agent_signature-验证其他代理的签名encrypt_agent_communication-安全的代理到代理通道establish_secure_channel-设置受信任的连接
5.企业与合规
audit_credential_usage-跟踪所有加密操作rotate_expiring_credentials-自动证书轮换generate_attestation-证明操作发生在正版YubiKey硬件上
______________________________________________________________________
YubiKey协议支持
该项目可以支持YubiKey的所有功能:
- 宣誓 -TOTP/HOTP用于双因素身份验证
- 粒子图像测速 -智能卡、证书、文件签名
- OpenPGP -电子邮件签名、文件加密
- FIDO2/Waauthn -无密码身份验证
- YubiOTP -质询响应,静态密码
______________________________________________________________________
相关项目
此存储库还包含 MCP市场 -MCPB扩展的发现和货币化平台(如YubiKey MCP服务器)。
YubiKey MCP服务器可以打包为 .mcpb 捆绑并发布到市场上以进行更广泛的分发。
______________________________________________________________________
资源和文件
- MCP文件 -MCP协议官方文件
- FastMCP Python SDK -本项目中使用的框架
- yubikey经理 -后端CLI工具
- CLAUDE.md -Claude代码贡献者开发指南
- src/helloworld/README.md -详细使用方法和故障排除
______________________________________________________________________
贡献
这是一个早期项目!欢迎捐款:
- 添加新工具 以下模式
CLAUDE.md - 提高安全性 -添加身份验证、审核日志记录
- 支持更多协议 -PIV、OATH、FIDO2工作流程
- 文档 -改进指南和示例
______________________________________________________________________
许可证
\[在此处指定您的许可证\]
支持
- 问题:
- 讨论:
- MCP社区: MCP故障
______________________________________________________________________
内置❤️ 使用模型上下文协议
*将硬件支持的安全性引入AI代理工作流程*
