内容
[](https://github.com/agenticpoa/apoa)
# @apoa/mcp
APOA 授权,用于 [MCP](https://modelcontextprotocol.io) 服务器。每次工具调用的范围限定、委托链、审计追踪。
MCP 使用 OAuth 2.1 进行连接级身份验证(谁可以连接)。此软件包添加了操作级身份验证(连接后可以执行的操作)。
```
MCP 客户端 --> @apoa/mcp --> MCP 服务器
(授权每次工具调用)
```
## 两种模式
### 中间件(适用于您构建的服务器)
```typescript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { withAPOA } from '@apoa/mcp';
const server = new Server({ name: 'my-server', version: '1.0.0' }, { capabilities: { tools: {} } });
withAPOA(server, {
key: publicKey,
mappings: {
read_file: 'filesystem:files:read',
write_file: 'filesystem:files:write',
list_dir: 'filesystem:dirs:list',
},
});
```
三行代码。每次工具调用都针对传递的 `_meta.apoa_token` 中的 APOA令牌进行授权。没有令牌,则无访问权限。
### 代理(适用于您无法修改的第三方服务器)
```bash
npx apoa-mcp --config gateway.config.json
```
位于客户端和服务器之间,拦截每次 `tools/call`,授权,剥离令牌,转发。
## 工作原理
1. MCP 客户端在每次工具调用时,在 `_meta.apoa_token` 中传递 APOA 令牌
2. 工具名称映射到 APOA `service:scope` 对
3. 验证令牌(签名、过期、吊销、重放)
4. SDK 的 `authorize()` 检查范围、约束和规则
5. 如果授权,则运行工具。如果不授权,则客户端收到拒绝和原因。
令牌在到达工具处理程序之前被剥离 -- 工具永远不会看到它。
## 传递令牌
MCP 客户端在每次工具调用的 `_meta` 字段中包含 APOA 令牌:
```typescript
import { APOA, generateKeyPair } from '@apoa/core';
const keys = await generateKeyPair();
const apoa = new APOA({ privateKey: keys.privateKey });
const grant = await apoa.tokens.createGrant({
principal: 'did:apoa:alice',
agent: 'did:apoa:research-agent',
service: 'filesystem',
scopes: ['files:read'],
expiresIn: '1h',
});
```
将 `grant.raw` 作为 `_meta.apoa_token` 传递:
```json
{
"method": "tools/call",
"params": {
"name": "read_file",
"arguments": {
"path": "/tmp/test.txt",
"_meta": {
"apoa_token": "<grant.raw>"
}
}
}
}
```
在 MCP 服务器端,使用匹配的公钥配置 `withAPOA()`:
```typescript
withAPOA(server, {
key: keys.publicKey,
mappings: {
read_file: 'filesystem:files:read',
},
});
```
## 工具映射
**简单格式**(每个工具一个字符串):
```typescript
withAPOA(server, {
key: publicKey,
mappings: {
read_file: 'filesystem:files:read',
write_file: 'filesystem:files:write',
},
});
```
**自动映射**(无需配置):
```typescript
withAPOA(server, { key: publicKey });
// read_file -> read_file:call, write_file -> write_file:call, 等
```
**条件映射**(参数感知):
```json
{
"toolMappings": [
{ "tool": "write_file", "service": "filesystem", "scope": "files:write:sandbox",
"when": { "path": { "startsWith": "/tmp" } }, "priority": 10 },
{ "tool": "write_file", "service": "filesystem", "scope": "files:write" }
]
}
```
## apoa.check(干预运行工具)
启用它以注入一个调试工具,检查授权而无需执行:
```typescript
withAPOA(server, { key: publicKey, enableCheckTool: true });
```
代理或人类可以调用 `apoa.check({ tool: 'write_file', path: '/home/data' })` 以查看操作是否被授权,映射到哪个范围,以及哪个规则将被触发。
## 本产品为MCP添加的功能
| 功能 | MCP 原生支持 | @apoa/mcp |
|---|---|---|
| 连接级身份验证(OAuth 2.1) | 是 | 不适用(互补) |
| 每次工具调用的授权 | 否(SEP-1880 关闭为 NOT_PLANNED) | 是 |
| 具有衰减的委托链 | 否 | 是(通过 @apoa/core) |
| 硬/软规则引擎 | 否 | 是 |
| 每个操作的约束检查 | 否 | 是 |
| 级联吊销 | 否 | 是 |
| 审计追踪 | 否(路线图“pre-RFC”) | 是(哈希链,防篡改) |
| 重放保护 | 否 | 是(基于 JTI) |
## 持久化存储
在生产环境中,使用默认的内存存储以外的 SQLite 存储:
```typescript
import { SqliteRevocationStore, SqliteAuditStore, SqliteReplayStore } from '@apoa/mcp';
withAPOA(server, {
key: publicKey,
revocationStore: new SqliteRevocationStore('./auth.db'),
auditStore: new SqliteAuditStore('./auth.db'),
replayStore: new SqliteReplayStore('./auth.db'),
});
```
审计存储使用 SHA-256 哈希链进行防篡改。
## 开发
```bash
pnpm install
pnpm test # 运行测试
pnpm run typecheck # 类型检查
pnpm run build # 构建
```
## 许可证
Apache-2.0
## APOA 标准的一部分
- [APOA 规范](https://github.com/agenticpoa/apoa/blob/main/SPEC.md)
- [@apoa/core](https://github.com/agenticpoa/apoa/tree/main/sdks/typescript) (TypeScript SDK)
- [apoa](https://pypi.org/project/apoa/) (Python SDK)
连接信息
猜你喜欢
rea
用于编码代理的逆向工程:一个 CLI 和 MCP 服务器,用于检查任何应用程序,了解其功能,并按照您的方式构建它们。
everything-claude-code
完整的 Claude Code 配置集合 - agents、skills、hooks、commands、rules、MCPs。来自 Anthropic...
MarkItDown MCP
MarkItDown-MCP是一个轻量级服务器,用于将URI转换为Markdown。
cc-switch
跨平台的Claude Code、Codex和Gemini CLI一体化助手。
Agent-Reach
赋予你的AI Agent观察整个互联网的眼睛。读取并搜索...
servers
# 模型上下文协议服务器