mcpField
B
bolyra

bolyra

本地服务
github.com/bolyra安全04 天前更新

关于 bolyra

bolyra 是用于 MCP 服务的身份验证中间件和客户端辅助库,适合需要按权限保护敏感工具的开发者。它验证绑定人类与 Agent 的证明,并支持委托链和开发模式模拟证明。

主要功能

  1. 01验证工具调用证明包
  2. 02按工具权限位授权
  3. 03支持 stdio 与 HTTP 中间件
  4. 04支持委托链和权限收窄
  5. 05提供客户端证明附加辅助函数

适用场景

  1. 01为 MCP 文件工具添加调用授权
  2. 02在 stdio 或 HTTP 服务中验证 Agent
  3. 03开发阶段使用模拟证明测试授权逻辑

MCP 相关

为 MCP 服务的 tools/call 添加身份验证;发现请求不受验证。客户端辅助函数可将证明附加到 stdio 元数据或 HTTP Authorization 请求头。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"bolyra": {
"command": "npx",
"args": [
"-y",
"@bolyra/mcp"
]
}
}
}
启动命令npx -y @bolyra/mcp

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npm@bolyra/mcp0.6.4

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

@bolyra/mcp

为 MCP 工具调用设置访问门控,确保只有获授权的 Agent 才能调用敏感工具。

快速开始(60 秒)

npm install @bolyra/mcp @bolyra/sdk @modelcontextprotocol/sdk
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { withBolyraAuthStdio } from '@bolyra/mcp';

const server = new McpServer({ name: 'my-server', version: '1.0.0' });

server.tool('read_file', { path: { type: 'string' } }, async (args) => ({
  content: [{ type: 'text', text: `Reading ${args.path}` }],
}));

withBolyraAuthStdio(server.server, {
  devMode: true,
  toolPolicy: {
    // 1n = READ_DATA, 2n = WRITE_DATA (BigInt — add 'n' suffix)
    read_file: 1n,
  },
});

就这么简单。现在,每个 tools/call 都需要有效的 Bolyra 证明包。

开发模式(完整示例)

开发模式使用模拟证明——无需电路制品、无需可信设置,即可立即启动。可用它在接入真实 ZKP 验证前构建和测试服务。

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { withBolyraAuthStdio } from '@bolyra/mcp';

// Server
const server = new McpServer({ name: 'my-server', version: '1.0.0' });

server.tool('read_file', { path: { type: 'string' } }, async (args) => ({
  content: [{ type: 'text', text: 'file contents...' }],
}));

withBolyraAuthStdio(server.server, {
  devMode: true,
  toolPolicy: {
    // 1n = READ_DATA, 2n = WRITE_DATA (BigInt — add 'n' suffix)
    read_file: 1n,
    write_file: 2n,
  },
});

await server.connect(new StdioServerTransport());

客户端:

import { createDevIdentities, attachBolyraProof } from '@bolyra/sdk';

const { human, agent } = await createDevIdentities();
const auth = await attachBolyraProof(human, agent, { devMode: true });

// stdio
await client.callTool({ name: 'read_file', arguments: { path: '/tmp/x' }, _meta: auth.meta });
// HTTP
await fetch('/mcp', { headers: { ...auth.headers }, ... });

工作原理

每个 tools/call 请求都必须携带一个证明包:一对 Groth16 证明(一个来自人类的电路,一个来自 Agent 的电路),并绑定到共享的会话随机数。

服务端:

  1. 从 params._meta.bolyra(stdio)或 Authorization: Bolyra <base64> 标头(HTTP)中提取证明包。
  2. 验证握手——包括两项证明、随机数时效性和分数下限。
  3. 根据 Agent 的有效位掩码检查工具的权限策略。
  4. 将 BolyraAuthContext 附加到请求,供下游处理程序使用。
  5. 如果任何步骤失败,则返回 MCP 错误。

发现请求(initialize、tools/list)无需身份验证即可通过。

委托链(v=2 证明包)可跨多个跳转,将权限范围从根凭证逐级收窄至叶子 Agent。工具策略看到的有效位掩码始终属于叶子 Agent,即权限范围最受限的一方。

API 参考

服务端 — stdio
withBolyraAuthStdio(server: McpServer, config: BolyraMcpConfig): void

包装一个 McpServer 实例。必须在 server.connect(transport) 之前调用。

服务端 — HTTP
bolyraAuthMiddleware(config: BolyraMcpHttpConfig): express.RequestHandler

Express 中间件。应挂载在 MCP HTTP 处理程序之前。未通过身份验证的 tools/call 请求将被拒绝,并返回 HTTP 401。

客户端辅助函数
attachBolyraProof(
  human: HumanIdentity,
  agent: AgentCredential,
  options?: AttachProofOptions,
): Promise<BolyraClientAuth>

执行握手并返回 { headers, meta, bundle }。设置 options.devMode = true 可跳过真实证明流程,改为生成模拟证明包。

attachDelegatedBolyraProof(
  human: HumanIdentity,
  rootCred: AgentCredential,
  hops: DelegationHopSpec[],
  options?: AttachProofOptions,
): Promise<BolyraClientAuth>

与 attachBolyraProof 类似,但会遍历委托链并返回 v=2 证明包。

验证
verifyBundle(bundle: BolyraProofBundle, config: BolyraMcpConfig): Promise<BolyraAuthContext>

直接验证证明包——适用于自定义传输或离线验证。

checkToolPolicy(
  toolName: string,
  ctx: BolyraAuthContext,
  policy: ToolPermissionPolicy,
): { allowed: boolean; reason?: string }

检查 BolyraAuthContext 是否满足工具所需的位掩码。

开发身份(从 SDK 重新导出)
createDevIdentities(options?: DevIdentityOptions): Promise<DevIdentities>

返回固定种子的 { human, agent, operatorKey }——结果确定,无需电路制品。首次调用时会记录警告。切勿在生产环境中使用。

生产环境配置

将 devMode: true 替换为真实的 resolveCredential 解析器,并让 SDK 指向你的电路制品:

withBolyraAuthStdio(server, {
  resolveCredential: async (commitment) => myRegistry.get(commitment),
  toolPolicy: {
    // 1n = READ_DATA, 2n = WRITE_DATA (BigInt — add 'n' suffix)
    read_file: 1n,
    write_file: 2n,
  },
  sdkConfig: {
    circuitDir: '/path/to/circuits/build',
    rpcUrl: 'https://sepolia.base.org',
    registryAddress: '0x2781dF8b6381462d881C833Fb703d68c661c9577',
  },
});

完整配置接口:

interface BolyraMcpConfig {
  network?: string;          // DID network label (default: 'base-sepolia')
  minScore?: number;         // Minimum score floor 0–100 (default: 70)
  maxProofAge?: number;      // Nonce freshness window in seconds (default: 300)
  toolPolicy?: ToolPermissionPolicy;
  devMode?: boolean;         // Mock verification — dev/test only
  resolveCredential?: (commitment: string) => Promise<AgentCredential | null>;
  sdkConfig?: BolyraConfig;
}

HTTP 变体还会添加 authScheme?: string(默认值为 "Bolyra")。

传输指南

传输方式证明包位置备注
stdio(Claude Desktop、Cursor、Cline)params._meta.bolyraMCP 规范未定义 stdio 身份验证接口;_meta 是协议层唯一可用的字段
HTTP / SSE / Streamable-HTTPAuthorization: Bolyra <base64-bundle>根据 RFC 7235 定义的自定义身份验证方案;符合 OAuth 2.1 资源服务器模式

两种方式在服务端都会生成相同的 BolyraAuthContext。工具处理程序无需知道使用的是哪种传输方式。

示例

参见 examples/protected-file-server/,其中包含一个使用开发模式的完整 stdio 服务端和客户端。运行命令:

cd integrations/mcp
npm run example:protected-file-server

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。