-
Notifications
You must be signed in to change notification settings - Fork 7
feat: WASM bindings for cedar-policy-mcp-schema-generator #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomjwxf
wants to merge
1
commit into
cedar-policy:main
Choose a base branch
from
tomjwxf:feat/wasm-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| [package] | ||
| name = "cedar-policy-mcp-schema-generator-wasm" | ||
| description = "WASM bindings for cedar-policy-mcp-schema-generator, exposing SchemaGenerator to JavaScript/TypeScript." | ||
| version = "0.1.0" | ||
|
|
||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| categories.workspace = true | ||
| keywords.workspace = true | ||
| homepage.workspace = true | ||
| repository.workspace = true | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| cedar-policy-mcp-schema-generator = { path = "../cedar-policy-mcp-schema-generator" } | ||
| mcp-tools-sdk = { path = "../mcp-tools-sdk" } | ||
| wasm-bindgen = "0.2" | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_json = "1.0" | ||
|
|
||
| [dev-dependencies] | ||
| wasm-bindgen-test = "0.3" | ||
victornicolet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| serde_json = "1.0" | ||
|
|
||
| [lints] | ||
| workspace = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # cedar-policy-mcp-schema-generator-wasm | ||
|
|
||
| WASM bindings for [cedar-policy-mcp-schema-generator](../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 | ||
|
|
||
| ```javascript | ||
| 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?)` | ||
|
|
||
| | Parameter | Type | Description | | ||
| |-----------|------|-------------| | ||
| | `schemaStub` | `string` | Cedar schema stub with `@mcp_principal` and `@mcp_resource` annotations | | ||
| | `toolsJson` | `string` | MCP tool descriptions as JSON (the `tools` array from `tools/list`) | | ||
| | `configJson` | `string?` | Optional configuration as JSON | | ||
|
|
||
| **Returns:** JSON string with fields: | ||
|
|
||
| | Field | Type | Description | | ||
| |-------|------|-------------| | ||
| | `schema` | `string \| null` | Generated Cedar schema as `.cedarschema` text | | ||
| | `schemaJson` | `string \| null` | Generated schema as JSON (for `isAuthorized()`) | | ||
| | `error` | `string \| null` | Error message if generation failed | | ||
| | `isOk` | `boolean` | Whether generation succeeded | | ||
|
|
||
| ### Configuration | ||
|
|
||
| ```json | ||
| { | ||
| "includeOutputs": false, | ||
| "objectsAsRecords": false, | ||
| "eraseAnnotations": true, | ||
| "flattenNamespaces": false, | ||
| "numbersAsDecimal": false | ||
| } | ||
| ``` | ||
|
|
||
| | Option | Default | Description | | ||
| |--------|---------|-------------| | ||
| | `includeOutputs` | `false` | Include tool output schemas in actions | | ||
| | `objectsAsRecords` | `false` | Use records instead of entities for objects without `additionalProperties` | | ||
| | `eraseAnnotations` | `true` | Remove `@mcp_*` annotations from output | | ||
| | `flattenNamespaces` | `false` | Flatten all types into a single namespace | | ||
| | `numbersAsDecimal` | `false` | Encode JSON `number` as Cedar `Decimal` instead of `Long` | | ||
|
|
||
| ## Building | ||
|
|
||
| ```bash | ||
| # 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 | ||
| ``` | ||
|
|
||
| ## 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for updating cedar-policy-core to 4.9.1 after the upstream changes. You might want to also bump other upstream dependency changes for CI to pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomjwxf I think this is the last outstanding item; the diff still shows many dependencies downgraded relative to mainline in Cargo.toml.