内容
# MCP 凭证服务器
🔐 **通过模型上下文协议为 AI 代理提供安全的凭证管理**
[](https://github.com/hsuehic/mcp-credentials-server/actions/workflows/ci.yml)
[](https://github.com/hsuehic/mcp-credentials-server/actions/workflows/release.yml)
[](https://badge.fury.io/js/mcp-credentials-server)
[](https://opensource.org/licenses/MIT)
[](https://nodejs.org/)
---
## 🚀 快速开始
### 安装
```bash
# 全局安装软件包
npm install -g mcp-credentials-server
# 或克隆并从源代码构建
git clone https://github.com/hsuehic/mcp-credentials-server.git
cd mcp-credentials-server
npm install
npm run build
```
### 游标配置
1. **打开游标设置** (`Cmd+,` 在 Mac 上,`Ctrl+,` 在 Windows/Linux 上)
2. **在设置中搜索"MCP"**
3. **将以下内容添加到您的MCP服务器配置中**:
#### ⚡ **选项 A:零配置(最简单)**
```json
{
"mcpServers": {
"mcp-credentials-server": {
"command": "mcp-credentials-server"
}
}
}
```
**✨ 功能:**
- 🔐 自动生成和保存加密密钥
- 🚀 生产优化的默认值(调试关闭)
- 📁 自动创建 `~/.mcp-credentials/` 存储
- 🔑 加密密钥保存到 `~/.mcp-credentials/.encryption-key`
- 🛡️ 安全的文件权限(仅限用户访问)
#### 🚀 **选项 B:全局安装(手动密钥)**
```json
{
"mcpServers": {
"mcp-credentials-server": {
"command": "mcp-credentials-server",
"env": {
"MCP_ENCRYPTION_KEY": "您的32位加密密钥",
"NODE_ENV": "production"
}
}
}
}
```
#### 📦 **选项 C:使用 npx 进行本地安装**
```json
{
"mcpServers": {
"mcp-credentials-server": {
"command": "npx",
"args": ["mcp-credentials-server"],
"env": {
"MCP_ENCRYPTION_KEY": "您的32位加密密钥",
"NODE_ENV": "production"
}
}
}
}
```
#### 🛠️ **选项 D:开发模式(从源代码)**
```json
{
"mcpServers": {
"mcp-credentials-server": {
"command": "node",
"args": ["/path/to/mcp-credentials-server/dist/server/index.js"],
"env": {
"MCP_ENCRYPTION_KEY": "您的32位加密密钥",
"MCP_DEBUG": "true",
"NODE_ENV": "development"
}
}
}
}
```
#### ⚙️ **环境变量解释**
| 变量 | 零配置 | 生产值 | 开发值 | 描述 |
| --- | --- | --- | --- | --- |
| `MCP_ENCRYPTION_KEY` | **自动生成** | `您的32位密钥` | `您的32位密钥` | 32位加密密钥(如果未设置则自动生成) |
| `NODE_ENV` | *未设置* | `production` | `development` | 控制调试输出和性能 |
| `MCP_DEBUG` | `false`(默认) | `false`(默认) | `true` | 启用详细日志记录(可以覆盖任何模式) |
| `MCP_STORE_PATH` | `~/.mcp-credentials` | 自定义路径 | 自定义路径 | 凭证存储目录 |
| `MCP_PORT` | `3000`(默认) | `3000`(默认) | `3000`(默认) | 服务器端口(通常MCP不需要) |
| `MCP_HOST` | `localhost`(默认) | `localhost`(默认) | `localhost`(默认) | 服务器主机(通常MCP不需要) |
4. **[可选] 生成加密密钥**(仅当不使用零配置时):
```bash
# 生成安全的32位加密密钥
node -e "console.log(require('crypto').randomBytes(16).toString('hex'));"
# 或使用openssl
openssl rand -hex 16
# 示例输出:a1b2c3d4e5f6789012345678901234ab
```
5. **完全重启Cursor** 以加载MCP服务器
6. **验证配置**(可选):
```bash
# 测试零配置设置
mcp-credentials-server
# 首次运行时,您将看到: "🔑 生成新的加密密钥并保存到: ~/.mcp-credentials/.encryption-key"
# 使用自定义密钥进行测试
NODE_ENV=production MCP_ENCRYPTION_KEY=您的密钥 mcp-credentials-server
# 您应该看到: "🚀 MCP 凭证服务器成功启动"
# 按 Ctrl+C 停止测试
```
### 🔧 **配置提示**
#### **🌟 推荐:零配置**
- ⚡ **最简单的设置** - 只需指定命令,不需要环境变量
- 🔐 **自动安全** - 自动生成和保存加密密钥
- 🚀 **生产就绪** - 默认优化,调试关闭
- 🛡️ **默认安全** - 密钥文件具有适当的权限(600)
- 📁 **自动设置** - 自动创建存储目录
#### **🔧 自定义配置**
- 🎛️ 在需要特定控制时使用环境变量
- 🔑 设置 `MCP_ENCRYPTION_KEY` 以跨系统使用一致的密钥
- 🏭 设置 `NODE_ENV=production` 以明确设置为生产模式
- 🛠️ 设置 `NODE_ENV=development` 以进行开发并启用调试日志
- 📂 设置 `MCP_STORE_PATH` 以自定义存储位置
#### **🐛 开发和调试**
- 🛠️ 使用 `NODE_ENV=development` 以获取详细日志
- 🔍 使用 `MCP_DEBUG=true` 以进行额外调试(覆盖NODE_ENV)
- 📝 调试模式在启动时显示配置详细信息
- 🔧 在开发服务器本身时使用本地路径
#### **⚠️ 故障排除**
- 🔍 **服务器未启动**:检查 `~/.mcp-credentials/` 的文件权限
- 🔍 **过于详细**:使用零配置(无环境变量)或 `NODE_ENV=production`
- 🔍 **需要更多日志**:添加 `MCP_DEBUG=true` 到任何配置
- 🔍 **密钥问题**:删除 `~/.mcp-credentials/.encryption-key` 以重新生成
### 用例
配置完成后,您可以询问Cursor:
```
"设置我的LinkedIn凭证并启用2FA"
"安全存储我的数据库连接详细信息"
"为GitHub生成2FA代码"
"列出所有已配置的服务"
```
### ⚠️ 重要免责声明
**请自行承担风险**:该软件处理敏感的凭证信息。虽然我们实施了行业标准的AES-256加密和安全最佳实践,但用户负责:
- 保持其加密密钥的安全
- 定期备份凭证数据
- 确保符合其组织的安全策略
- 了解任何处理凭证的AI系统都存在固有风险
**隐私声明**:所有凭证都存储在本地机器上。除非您明确发起AI操作,否则不会将任何数据传输到外部服务器。
---
## 🔐 技术概述
专为AI Agent设计的安全凭证管理系统,通过MCP(模型上下文协议)提供标准化的凭证获取和2FA处理。
## ✨ 核心特性
- 🤖 **AI Agent友好**:专为AI Agent设计的MCP协议支持
- 🔒 **军用级安全**:AES-256加密存储,文件权限保护
- 🔐 **智能2FA处理**:自动TOTP生成、备用代码管理、应用密码支持
- 🌐 **多平台支持**:LinkedIn、GitHub、数据库等主流服务
- 📊 **完整审计**:详细的操作日志和访问记录
- 🔄 **自动备份**:配置文件自动备份和恢复
- 🚀 **开箱即用**:简单配置,立即可用
## 🎯 解决的核心问题
### 1. AI Agent凭证管理难题
**问题**:AI Agent需要访问各种服务(LinkedIn、数据库、API等),但凭证管理复杂且不安全。
**解决方案**:提供统一的MCP接口,AI Agent通过标准化的function calls获取凭证。
```typescript
// AI Agent简单调用
const credentials = await mcpCall('get_credentials', {
service: 'linkedin',
include2FA: true, // 自动处理2FA
});
// 自动获得登录信息和2FA代码
console.log(credentials.data.email); // 登录邮箱
console.log(credentials.twoFactorCode); // 自动生成的2FA代码
```
### 2. 2FA自动化处理挑战
**问题**:大多数服务都要求2FA,传统自动化方案无法处理。
**解决方案**:智能2FA处理策略,支持多种自动化方式:
#### 🔐 TOTP自动生成
```typescript
// 存储TOTP secret,自动生成验证码
{
service: 'linkedin',
twoFactor: {
type: '2fa_totp',
totpSecret: 'JBSWY3DPEHPK3PXP', // 扫码获得的secret
isActive: true
}
}
// AI Agent自动获得6位验证码
const code = await mcpCall('generate_2fa', { service: 'linkedin' });
// 返回: { code: '123456', timeRemaining: 25 }
```
#### 📝 备用代码管理
```typescript
// 预存储备用验证码
{
twoFactor: {
type: '2fa_backup',
backupCodes: ['12345678', '23456789', '34567890'],
isActive: true
}
}
// 自动使用并移除已用代码
const result = await mcpCall('generate_2fa', { service: 'github' });
// 返回: { code: '12345678', codesRemaining: 2 }
```
#### 🔑 应用专用密码
```typescript
// 某些服务支持应用专用密码(如GitHub)
{
twoFactor: {
type: '2fa_app_password',
appPassword: 'ghp_xxxxxxxxxxxxxxxxxxxx',
isActive: true
}
}
// AI Agent直接使用应用密码,无需2FA
```
#### 💾 会话持久化
```typescript
// 保持长期有效的登录会话
{
twoFactor: {
type: '2fa_session_persist',
sessionCookies: ['session=xxx', 'token=yyy'],
isActive: true
}
}
```
## 🚀 快速开始
### 安装
```bash
npm install @xiaoweihsueh/mcp-credentials-server
```
### 基础配置
```typescript
import { MCPCredentialsServer } from '@xiaoweihsueh/mcp-credentials-server';
const server = new MCPCredentialsServer({
port: 3000,
storage: {
storePath: '~/.mcp-credentials',
encryptionKey: process.env.MCP_ENCRYPTION_KEY,
},
supported2FA: ['auto_totp', 'backup_codes', 'app_password'],
debug: true,
});
await server.start();
```
### 设置服务凭证
```typescript
// 设置LinkedIn凭证(带2FA)
await mcpCall('set_credentials', {
service: 'linkedin',
credentials: {
email: 'your@email.com',
password: 'your-linkedin-password',
twoFactor: {
type: '2fa_totp',
totpSecret: 'JBSWY3DPEHPK3PXP2Q4B7Z8A9C1D2E3F', // 从LinkedIn 2FA设置获得
isActive: true,
},
},
}
// 设置GitHub凭证(应用密码)
await mcpCall('set_credentials', {
service: 'github',
credentials: {
token: 'ghp_xxxxxxxxxxxxxxxxxxxx',
username: 'your-username',
twoFactor: {
type: '2fa_app_password',
appPassword: 'ghp_xxxxxxxxxxxxxxxxxxxx',
isActive: true,
},
},
});
```
## 🛠️ MCP Functions
### 1. get_credentials
获取服务凭证,自动处理2FA```typescript
const result = await mcpCall('get_credentials', {
service: 'linkedin',
profile: 'work', // 可选,默认'default'
include2FA: true // 可选,是否生成2FA代码
}
// 返回
{
success: true,
data: {
service: 'linkedin',
email: 'your@email.com',
password: 'your-linkedin-password',
twoFactor: { ... }
},
twoFactorCode: '123456', // 自动生成的2FA代码
timestamp: ${2025-10-10T12:00:00.000Z}
}
```
### 2. set_credentials
设置或更新服务凭证
```typescript
await mcpCall('set_credentials', {
service: 'postgres',
credentials: {
host: 'localhost',
port: 5432,
username: 'admin',
password: 'secret',
database: 'mydb',
},
profile: 'production',
});
```
### 3. generate_2fa
单独生成2FA代码
```typescript
const result = await mcpCall('generate_2fa', {
service: 'linkedin',
profile: 'work'
});
// 返回
{
success: true,
code: '123456',
codesRemaining: 5, // 如果使用备用代码
nextCodeIn: 25, // TOTP下次更新秒数
timestamp: '2025-10-10T12:00:00.000Z'
}
```
### 4. validate_credentials
验证凭证配置完整性
```typescript
const result = await mcpCall('validate_credentials', {
service: 'linkedin'
});
// 返回
{
success: true,
valid: true,
details: {
requiredFields: ['email', 'password'],
missingFields: [],
has2FA: true,
lastUpdated: '2025-10-10T12:00:00.000Z'
}
}
```
### 5. list_services
列出所有配置的服务
```typescript
const result = await mcpCall('list_services', {
profile: 'work'
});
// 返回
{
services: ['linkedin', 'github', 'postgres'],
count: 3,
timestamp: '2025-10-10T12:00:00.000Z'
}
```
## 🔐 2FA 配置指南
### LinkedIn 2FA 设置 - 完整步骤指南
#### 步骤 1: 在 LinkedIn 上启用 2FA
1. **访问 LinkedIn 安全设置**:
- 登录 [LinkedIn.com](https://linkedin.com) 到您的网页浏览器
- 点击您的 **个人资料图片** → **"设置和隐私"**
- 导航到左侧边栏中的 **"登录和安全"**
- 找到 **"两步验证"** 部分
2. **开始 2FA 设置**:
- 点击 **"启用"** (或 **"更改"** 如果已启用)
- LinkedIn 将要求您首先验证您的身份
- 选择 **"身份验证器应用"** 作为您的首选方法
#### 步骤 2: 获取 TOTP 密钥
当 LinkedIn 显示二维码设置屏幕时:
1. **查找手动设置选项**:
- 查找并点击 **"无法扫描?"** 或 **"手动输入代码"** 链接
- 这将显示文本版本的密钥
2. **复制密钥**:
```
示例:JBSWY3DPEHPK3PXP2Q4B7Z8A9C1D2E3F
```
**替代方法** (如果手动选项不可见):
- 右键单击二维码 → **"复制图像"**
- 使用在线二维码解码器或二维码扫描器应用程序
- 查找 URL:`otpauth://totp/LinkedIn:you@example.com?secret=JBSWY3DPEHPK3PXP&issuer=LinkedIn`
- 提取 `secret=` 参数值
#### 步骤 3: 使用 MCP 服务器配置
要求Cursor设置您的LinkedIn凭证:
```
设置我的LinkedIn凭证,启用TOTP 2FA:
- 邮箱:your-email@example.com
- 密码:your-linkedin-password
- TOTP 密钥:JBSWY3DPEHPK3PXP2Q4B7Z8A9C1D2E3F
```
**或使用此格式表示凭证对象**:
```typescript
{
service: 'linkedin',
email: 'your-email@example.com',
password: 'your-linkedin-password',
twoFactor: {
type: '2fa_totp',
totpSecret: 'JBSWY3DPEHPK3PXP2Q4B7Z8A9C1D2E3F',
isActive: true
}
}
```
#### 步骤 4: 完成 LinkedIn 设置
1. **测试MCP服务器**:要求Cursor `"为LinkedIn生成2FA代码"`
2. **输入生成的代码** 到LinkedIn完成设置
3. **保存备份代码** LinkedIn提供的备份代码作为备用
4. **点击"完成"** 完成设置
#### 步骤 5: 验证一切正常
测试您的完整设置:
```
获取我的LinkedIn凭证和2FA代码
```
**预期响应**:
```json
{
"success": true,
"data": {
"email": "your-email@example.com",
"password": "your-linkedin-password",
"twoFactor": { "type": "2fa_totp", "isActive": true }
},
"twoFactorCode": "123456",
"timestamp": "2025-10-10T12:00:00.000Z"
}
```
#### 🔒 安全最佳实践
- **保持TOTP密钥安全** - 将其视为密码
- **存储LinkedIn的备份代码** 在安全位置(不在MCP服务器中)
- **测试前测试** - 在脚本中使用之前验证设置是否有效
- **考虑单独帐户** 用于自动化以保护您的主LinkedIn个人资料
- **了解LinkedIn的条款** 有关自动访问
#### 🚨 常见问题
1. **"无效代码" 错误**:
- 检查系统时间是否同步
- 验证密钥是否正确复制
- 尝试生成新代码(每30秒更换)
2. **二维码不显示手动选项**:
- 尝试不同的浏览器(Chrome,Firefox,Safari)
- 暂时禁用广告拦截器
- 使用移动LinkedIn应用,有时会有不同的选项
3. **LinkedIn安全警报**:
- LinkedIn可能会标记自动登录尝试
- 在同一浏览器/设备上进行初始设置和自动化
- 考虑将您的IP地址添加到LinkedIn的受信任设备列表中
### GitHub 应用密码
1. **生成个人访问令牌**
- GitHub → 设置 → 开发者设置 → 个人访问令牌
- 生成新的令牌,选择所需的权限
2. **配置应用密码模式**
```typescript
{
token: 'ghp_xxxxxxxxxxxxxxxxxxxx',
twoFactor: {
type: '2fa_app_password',
appPassword: 'ghp_xxxxxxxxxxxxxxxxxxxx',
isActive: true
}
}
```
### 备用代码策略
1. **收集备用代码**
- 在每个服务的2FA设置中下载备用代码
- 通常为8-10位数字
2. **配置备用代码池**
```typescript
{
twoFactor: {
type: '2fa_backup',
backupCodes: [
'12345678',
'23456789',
'34567890'
],
isActive: true
}
}
```
## 🤖 AI Agent 集成示例
### 自动发布LinkedIn帖子
```typescript
class LinkedInAgent {
async postUpdate(message: string) {
// 1. 获取凭证和2FA代码
const creds = await mcpCall('get_credentials', {
service: 'linkedin',
include2FA: true,
});
// 2. 自动登录(集成Chrome DevTools MCP)
await browser.navigate('https://linkedin.com/login');
await browser.fill('#username', creds.data.email);
await browser.fill('#password', creds.data.password);
await browser.click('button[type="submit"]');
// 3. 处理2FA(如果需要)
if (creds.twoFactorCode) {
await browser.fill('input[name="pin"]', creds.twoFactorCode);
await browser.click('button[type="submit"]');
}
// 4. 发布帖子
await browser.click('[data-test-id="share-box-trigger"]');
await browser.fill('[data-test-id="share-box-text-area"]', message);
await browser.click('[data-test-id="share-button"]');
console.log('✅ LinkedIn帖子发布成功!');
}
}
```
### 自动数据库查询
```typescript
class DatabaseAgent {
async queryData(sql: string) {
// 获取数据库凭证
const creds = await mcpCall('get_credentials', {
service: 'postgres',
profile: 'production',
});
// 连接数据库
const client = new pg.Client({
host: creds.data.host,
port: creds.data.port,
user: creds.data.username,
password: creds.data.password,
database: creds.data.database,
ssl: creds.data.ssl,
});
await client.connect();
const result = await client.query(sql);
await client.end();
return result.rows;
}
}
```
### 智能工作流编排
```typescript
class ProjectWorkflow {
async publishWeeklyReport() {
// 1. 从数据库获取项目数据
const projectData = await this.databaseAgent.queryData(`
SELECT * FROM project_metrics
WHERE date >= CURRENT_DATE - INTERVAL 7 DAYS
`);
// 2. 生成报告内容
const report = this.generateReport(projectData);
// 3. 发布到LinkedIn
await this.linkedinAgent.postUpdate(report);
// 4. 同步到GitHub Issues
await this.githubAgent.createIssue('Weekly Report', report);
console.log('✅ 周报发布工作流完成!');
}
}
```
## 🛡️ 安全特性
### 加密存储
- **AES-256-GCM**:行业标准加密算法
- **随机盐值**:每个配置文件单独加密
- **文件权限**:配置文件仅限当前用户访问(600)
### 访问控制
```typescript
// 代理上下文控制
const agentContext = {
agentId: 'marketing-bot',
permissions: ['linkedin', 'twitter'], // 只能访问社交媒体
sessionId: 'session-xxx',
};
```
### 审计日志
```typescript
// 完整的操作审计
{
id: 'audit-xxx',
timestamp: '2025-10-10T12:00:00.000Z',
action: 'get_credentials',
service: 'linkedin',
agentContext: { agentId: 'marketing-bot' },
success: true,
metadata: { profile: 'work' }
}
```
### 备份恢复
```typescript
// 自动备份配置
{
backupEnabled: true,
backupInterval: 60, // 60分钟
retention: 10 // 保留10个备份
}
```
## 📁 项目结构
```
mcp-credentials-server/
├── src/
│ ├── server/ # MCP Server核心
│ │ └── mcp-server.ts
│ ├── auth/ # 2FA处理
│ │ └── 2fa-handler.ts
│ ├── storage/ # 存储层
│ │ └── credentials-storage.ts
│ └── types/ # 类型定义
│ └── index.ts
├── examples/ # 使用示例
│ └── ai-agent-usage.ts
├── docs/ # 文档
└── README.md
```
## 🔧 配置选项
```typescript
interface MCPServerConfig {
port?: number; // 服务端口,默认3000
host?: string; // 服务地址,默认localhost
storage: {
storePath?: string; // 存储路径,默认~/.mcp-credentials
encryptionKey?: string; // 加密密钥
backupEnabled?: boolean; // 是否启用备份
backupInterval?: number; // 备份间隔(分钟)
};
supported2FA?: string[]; // 支持的2FA策略
sessionTimeout?: number; // 会话超时(分钟)
debug?: boolean; // 调试模式
}
```
## 🚨 故障排除
### 常见问题
1. **2FA代码无效**
```bash
# 检查系统时间是否正确
ntpdate -s time.nist.gov
# 验证TOTP secret是否正确
node -e "
const handler = require('./dist/auth/2fa-handler');
console.log(handler.generateTOTP('YOUR_SECRET'));
"
```
2. **凭据获取失败**
```typescript
// 验证凭据是否存在
const validation = await mcpCall('validate_credentials', {
service: 'linkedin',
});
console.log(validation.details);
```
3. **权限错误**
```bash
# 检查配置文件权限
ls -la ~/.mcp-credentials/profiles/
# 修复权限
chmod 600 ~/.mcp-credentials/profiles/*.enc
```
## 🤝 与其他MCP工具集成
### Chrome DevTools MCP
```typescript
// 结合浏览器自动化
const browser = new ChromeDevToolsMCP();
const credentials = new MCPCredentialsServer();
// 自动化登录流程
async function autoLogin(service: string) {
const creds = await credentials.getCredentials(service);
await browser.navigate(getLoginURL(service));
await browser.fill('#username', creds.data.email);
await browser.fill('#password', creds.data.password);
if (creds.twoFactorCode) {
await browser.fill('#totp', creds.twoFactorCode);
}
await browser.click('#login-button');
}
```
### Confluence MCP
```typescript
// 自动发布文档
async function publishToConfluence(title: string, content: string) {
const creds = await mcpCall('get_credentials', {
service: 'confluence',
});
await confluenceMCP.authenticate(creds.data);
await confluenceMCP.createPage(title, content);
}
```
## 📊 监控和指标
### 服务状态
```typescript
const status = server.getStatus();
console.log(status);
// {
// version: '1.0.0',
// uptime: 3600,
// stats: {
// auditLogsCount: 150,
// lastActivity: '2025-10-10T12:00:00.000Z'
// }
// }
```
### 存储统计
```typescript
const stats = await storage.getStats();
console.log(stats);
// {
// profiles: 3,
// totalServices: 12,
// storageSize: 4096,
// backupCount: 7
// }
```
## 🛣️ 发展路线图
- [ ] **v1.1**: Web UI管理界面
- [ ] **v1.2**: 云端同步支持
- [ ] **v1.3**: 更多2FA方式(硬件密钥、生物识别)
- [ ] **v1.4**: 企业级权限管理
- [ ] **v1.5**: API密钥轮换自动化
## 📝 许可证
MIT License - 查看 [LICENSE](LICENSE) 文件了解详细信息。
## 🙏 致谢
- 感谢MCP协议的设计团队
- 感谢iTrade项目的测试和反馈
- 感谢所有贡献者的支持
---
**核心价值**: 让AI Agent能够安全、便捷地访问需要认证的服务,真正实现"AI驱动的全自动化"。
**作者**: Xiaowei Hsueh (<xiaoweihsueh@gmail.com>)
**项目地址**: <https://github.com/hsuehic/mcp-credentials-server>
**技术支持**: 欢迎提交Issues和Pull Requests
MCP 配置
以下是此 MCP Server 的配置信息,可直接复制到 Cursor 或其他 MCP 客户端中使用。
mcp.json
连接信息
猜你喜欢
暂无推荐服务器