@cedar-policy/mcp-schema-generator-wasm

中文文档
0.7.0发布于 2026/10/22Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 2.6万

WASM bindings for cedar-policy-mcp-schema-generator, exposing SchemaGenerator to JavaScript/TypeScript.

原始使用文档
文档目录9 节
  1. Usage
  2. API
  3. generateSchema(schemaStub, toolsJson, configJson?)
  4. Configuration
  5. generateRequest(schemaStub, toolsJson, inputJson, principal…
  6. Building
  7. npm Release
  8. Relationship to the Rust Generator
  9. License

cedar-policy-mcp-schema-generator-wasm

WASM bindings for cedar-policy-mcp-schema-generator, exposing SchemaGenerator to JavaScript and TypeScript via wasm-bindgen.

This enables Node.js and browser environments to generate Cedar authorization schemas from MCP tool descriptions with the exact same behavior as the Rust implementation, including correct handling of:

  • JSON number as Long or Decimal (configurable)
  • additionalProperties as Cedar tagged entities
  • Namespaced type deduplication for nested objects

Usage

Install the npm package:

npm install @cedar-policy/mcp-schema-generator-wasm
const { generateSchema } = require('@cedar-policy/mcp-schema-generator-wasm');

const stub = `
namespace MyServer {
    @mcp_principal
    entity User;
    @mcp_resource
    entity McpServer;
    action "call_tool" appliesTo {
        principal: [User],
        resource: [McpServer]
    };
}
`;

const tools = JSON.stringify([
  {
    name: 'read_file',
    description: 'Read a file from disk',
    inputSchema: {
      type: 'object',
      properties: { path: { type: 'string' } },
      required: ['path'],
    },
  },
]);

const result = JSON.parse(generateSchema(stub, tools));

if (result.isOk) {
  console.log(result.schema);     // Human-readable .cedarschema
  console.log(result.schemaJson); // JSON for Cedar WASM isAuthorized()
} else {
  console.error(result.error);
}

API

generateSchema(schemaStub, toolsJson, configJson?)

ParameterTypeDescription
schemaStubstringCedar schema stub with @mcp_principal and @mcp_resource annotations
toolsJsonstringMCP tool descriptions as JSON (the tools array from tools/list)
configJsonstring?Optional configuration as JSON

Returns: JSON string with fields:

FieldTypeDescription
schemastring | nullGenerated Cedar schema as .cedarschema text
schemaJsonstring | nullGenerated schema as JSON (for isAuthorized())
errorstring | nullError message if generation failed
isOkbooleanWhether generation succeeded

Configuration

{
  "includeOutputs": false,
  "objectsAsRecords": false,
  "eraseAnnotations": true,
  "flattenNamespaces": false,
  "numbersAsDecimal": false,
  "deduplicateEntityTypes": false
}
OptionDefaultDescription
includeOutputsfalseInclude tool output schemas in actions
objectsAsRecordsfalseUse records instead of entities for objects without additionalProperties
eraseAnnotationstrueRemove @mcp_* annotations from output
flattenNamespacesfalseFlatten all types into a single namespace
numbersAsDecimalfalseEncode JSON number as Cedar Decimal instead of Long
deduplicateEntityTypesfalseConsolidate identical entity types across tools into the lowest common ancestor namespace

generateRequest(schemaStub, toolsJson, inputJson, principalType, principalId, resourceType, resourceId, configJson?)

Generates Cedar authorization request components from an MCP tool call.

ParameterTypeDescription
schemaStubstringCedar schema stub (same as generateSchema)
toolsJsonstringMCP tool descriptions as JSON
inputJsonstringMCP tool call input ({"params": {"tool": "...", "args": {...}}})
principalTypestringCedar entity type for the principal (e.g., "User")
principalIdstringPrincipal identifier (e.g., "alice")
resourceTypestringCedar entity type for the resource (e.g., "McpServer")
resourceIdstringResource identifier (e.g., "my-server")
configJsonstring?Optional configuration as JSON

Returns: JSON string with fields:

FieldTypeDescription
principalstring | nullPrincipal EntityUID (e.g., MyServer::User::"alice")
actionstring | nullAction EntityUID (e.g., MyServer::Action::"read_file")
resourcestring | nullResource EntityUID (e.g., MyServer::McpServer::"my-server")
entitiesJsonstring | nullEntities as a JSON array string
errorstring | nullError message if generation failed
isOkbooleanWhether generation succeeded

Example:

const { generateRequest } = require('@cedar-policy/mcp-schema-generator-wasm');

const stub = `
namespace MyServer {
    @mcp_principal
    entity User;
    @mcp_resource
    entity McpServer;
    action "call_tool" appliesTo {
        principal: [User],
        resource: [McpServer]
    };
}
`;

const tools = JSON.stringify([
  {
    name: 'read_file',
    description: 'Read a file from disk',
    inputSchema: {
      type: 'object',
      properties: { path: { type: 'string' } },
      required: ['path'],
    },
  },
]);

const input = JSON.stringify({
  params: { tool: 'read_file', args: { path: '/etc/config.yaml' } },
});

const result = JSON.parse(
  generateRequest(stub, tools, input, 'User', 'alice', 'McpServer', 'my-server')
);

if (result.isOk) {
  console.log(result.principal);    // MyServer::User::"alice"
  console.log(result.action);       // MyServer::Action::"read_file"
  console.log(result.resource);     // MyServer::McpServer::"my-server"
  console.log(result.entitiesJson); // JSON array of entities for isAuthorized()
} else {
  console.error(result.error);
}

Building

# Install wasm-pack
cargo install wasm-pack

# Build for Node.js
wasm-pack build --target nodejs --scope cedar-policy

# Build for browsers
wasm-pack build --target web --scope cedar-policy

npm Release

The npm package name is:

@cedar-policy/mcp-schema-generator-wasm

The Rust crate name remains cedar-policy-mcp-schema-generator-wasm. During release, the generated wasm-pack package metadata is normalized so the npm package uses the shorter name above while preserving the Cargo.toml version.

Releases are performed manually through the Publish MCP schema generator WASM to npm GitHub Actions workflow. The workflow:

  1. validates that it is triggered from main;
  2. validates a tag of the form cedar-policy-mcp-schema-generator-wasm-v<MAJOR>.<MINOR>.<PATCH>;
  3. checks that the tag version matches this crate's Cargo.toml;
  4. runs wasm-pack build --target nodejs --scope cedar-policy;
  5. normalizes the generated npm package metadata;
  6. runs npm pack --dry-run; and
  7. computes the correct npm dist-tag; and
  8. publishes with npm publish --access public --tag <tag> --provenance.

The workflow expects npm authentication to use npm trusted publishing through the repository's release environment. It does not require an NPM_TOKEN secret.

Relationship to the Rust Generator

This crate is a thin wasm-bindgen wrapper around the existing cedar-policy-mcp-schema-generator Rust crate. All schema generation logic, type mapping, and edge case handling is delegated to the Rust implementation. The WASM bindings add no independent logic.

License

Apache-2.0

版本与发布记录

@cedar-policy/mcp-schema-generator-wasm 共发布 3 个版本,首次发布于 2026/06/10;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.0latest2026/10/2217 天前
0.6.12026/09/241 个月前
0.6.02026/06/104 个月前

关于 @cedar-policy/mcp-schema-generator-wasm 的常见问题

@cedar-policy/mcp-schema-generator-wasm 是什么?

WASM bindings for cedar-policy-mcp-schema-generator, exposing SchemaGenerator to JavaScript/TypeScript.

如何安装 @cedar-policy/mcp-schema-generator-wasm?

在项目目录运行 npm install @cedar-policy/mcp-schema-generator-wasm,或使用 pnpm add / yarn add / bun add。

@cedar-policy/mcp-schema-generator-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cedar-policy/mcp-schema-generator-wasm 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cedar-policy/mcp-schema-generator-wasm 可以商用吗?

@cedar-policy/mcp-schema-generator-wasm 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cedar-policy/mcp-schema-generator-wasm 的下载量有多少?

根据 npm 官方下载统计,@cedar-policy/mcp-schema-generator-wasm 在 2026/10/10 – 2026/11/08 这 30 天内被下载 26,417 次,日均约 881 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cedar-policy/mcp-schema-generator-wasm 还在维护吗?

最新版本 0.7.0 发布于 2026/10/22(17 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。