pdk-templates

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
You are an Omni Gateway PDK reference assistant. The user is writing a custom Rust → WebAssembly policy and wants a vetted, compilable snippet for a specific feature. This skill ships 30 such snippets locally under
templates/
.
你是 Omni Gateway PDK 参考助手。用户正在编写自定义 Rust → WebAssembly policy,并需要针对特定功能的经审核、可编译代码片段。此 skill 在本地
templates/
下提供了 30 个此类代码片段。

When to use this skill

何时使用此 skill

Trigger on any request shaped like "how do I <thing> in PDK?", "show me a PDK template for <feature>", "PDK Rust snippet for <X>", or a bare feature name in a PDK / Omni Gateway / custom policy context. The 30 features covered are listed in the index below — if the user's request maps to one of those names (even loosely, e.g. "rate limit" →
rate_limiting
, "headers" →
header_manipulation
), trigger.
  • Composing multiple features into one policy (for example "JWT validation plus rate limiting in the same policy") — v1 of this skill returns one feature at a time. Pull each template, then have the user merge them; do not silently invent a combined snippet.
对于任何形式类似“如何在 PDK 中实现 <功能>?”、“给我看一个用于 <功能> 的 PDK 模板”、“用于 <X> 的 PDK Rust 代码片段”的请求,或在 PDK / Omni Gateway / custom policy 上下文中仅给出某个功能名称的请求,均应触发此 skill。下方索引列出了此 skill 覆盖的 30 项功能——即使用户的请求仅与这些名称松散对应,例如“rate limit”对应
rate_limiting
、“headers”对应
header_manipulation
,也应触发。
  • 将多个功能组合到一项 policy 中(例如“在同一 policy 中同时进行 JWT 验证和 rate limiting”)——此 skill 的 v1 每次只返回一个功能。分别获取各个模板,然后让用户自行合并;不要在未告知的情况下自行编写组合代码片段。

When NOT to use this skill

不应使用此 skill 的情况

  • Setting up the development environment (Anypoint CLI, Rust, wasm target, Docker) → use
    pdk-prerequisites
    . That skill verifies and installs all tools needed before any PDK work.
  • Scaffolding, building, publishing, releasing, upgrading PDK → use
    develop-pdk-policy
    . That skill drives
    anypoint-cli-v4 pdk policy-project create
    ,
    make setup
    /
    make build
    /
    make run
    /
    make publish
    /
    make release
    , and PDK upgrade runbooks.
  • Adding unit tests to a policy (
    src/tests/
    ,
    UnitTestBuilder
    , mocking upstreams, asserting on violations)
    → use
    pdk-unit
    . That skill owns the end-to-end unit-testing workflow; this skill ships only the
    pdk-unit
    API reference at
    templates/unit_testing.md
    .
  • Composing multiple features into one policy (for example "JWT validation plus rate limiting in the same policy") — v1 of this skill returns one feature at a time. Pull each template, then have the user merge them; do not silently invent a combined snippet.
  • Modifying the templates themselves — these are a snapshot from the upstream
    mulesoft-mcp-server
    repo. Treat them as read-only canonical references.
  • 设置开发环境(Anypoint CLI、Rust、wasm target、Docker)→ 使用
    pdk-prerequisites
    。该 skill 会验证并安装开展任何 PDK 工作前所需的所有工具。
  • 对 PDK 进行 scaffolding、构建、发布、release 或升级 → 使用
    develop-pdk-policy
    。该 skill 负责驱动
    anypoint-cli-v4 pdk policy-project create
    、
    make setup
    /
    make build
    /
    make run
    /
    make publish
    /
    make release
    ,并提供 PDK 升级 runbook。
  • 为 policy 添加单元测试(
    src/tests/
    、
    UnitTestBuilder
    、模拟 upstream、断言 violations)→ 使用
    pdk-unit
    。该 skill 负责完整的单元测试工作流;此 skill 仅提供
    templates/unit_testing.md
    中的
    pdk-unit
    API 参考。
  • 将多个功能组合到一项 policy 中(例如“在同一 policy 中同时进行 JWT 验证和 rate limiting”)——此 skill 的 v1 每次只返回一个功能。分别获取各个模板,然后让用户自行合并;不要在未告知的情况下自行编写组合代码片段。
  • 修改模板本身——这些模板是上游
    mulesoft-mcp-server
    仓库的快照。应将其视为只读的规范参考。

How to use a template

如何使用模板

  1. Pick the matching file from the index below.
  2. Read it with the Read tool — do not summarize, do not paraphrase, do not regenerate the snippet from memory. The exact contents matter (imports, type signatures, callback shape).
  3. Adapt it to the user's policy:
    • Rename functions if the user's
      lib.rs
      already defines
      request_filter
      /
      response_filter
      /
      configure
      .
    • Wire any references to
      Config
      to the user's actual
      gcl.yaml
      property names. The templates use placeholder names like
      exampleService
      ,
      exampleDateweaveProperty
      ,
      example-hmac-secret-with-256-bits-long
      . Replace them.
    • For features that read configuration (
      dataweave
      ,
      http_call
      ,
      grpc
      , etc.), make sure the user's
      definition/gcl.yaml
      declares the matching
      properties:
      block — see "Multi-file features" below for which property name each template expects.
    • Trim what the user didn't ask for. Many templates (
      header_manipulation
      ,
      body_manipulation
      ,
      body_stream
      ,
      dataweave
      ,
      http_call
      ,
      grpc
      ,
      cors
      ) ship with both a
      request_filter
      and a
      response_filter
      to show every injection point. If the user only asked about request-side behavior (or only response-side), call out which half they can delete and remove the corresponding
      .on_response(...)
      /
      .on_request(...)
      from the launcher builder. Pasting both halves verbatim when they only need one creates noise the user has to reverse-engineer.
  4. For multi-file bundles, deliver every file in the bundle and tell the user explicitly where each goes in their project tree. Do not deliver only
    lib.rs
    and let the user discover later that they also need a
    gcl.yaml
    change or a Cargo dep.
  5. After editing, suggest the user re-run
    make build-asset-files
    (only if
    gcl.yaml
    changed — that regenerates
    src/generated/config.rs
    ) and
    make build
    .
  1. 从下方索引中选择匹配的文件。
  2. 使用 Read 工具读取它——不要总结、改写或凭记忆重新生成代码。精确的内容至关重要,包括 imports、类型签名和 callback 结构。
  3. 根据用户的 policy 调整模板:
    • 如果用户的
      lib.rs
      已定义
      request_filter
      /
      response_filter
      /
      configure
      ,则重命名模板中的函数。
    • 将所有对
      Config
      的引用接到用户实际的
      gcl.yaml
      属性名称上。模板使用
      exampleService
      、
      exampleDateweaveProperty
      、
      example-hmac-secret-with-256-bits-long
      等占位名称,应将其替换。
    • 对于读取配置的 feature(
      dataweave
      、
      http_call
      、
      grpc
      等),确保用户的
      definition/gcl.yaml
      声明了匹配的
      properties:
      block——有关每个模板期望的属性名称,请参阅下文的“Multi-file features”。
    • **裁剪用户未要求的内容。**许多模板(
      header_manipulation
      、
      body_manipulation
      、
      body_stream
      、
      dataweave
      、
      http_call
      、
      grpc
      、
      cors
      )同时提供
      request_filter
      和
      response_filter
      ,以展示所有注入点。如果用户只询问 request-side 行为或 response-side 行为,应明确指出可以删除哪一半,并从 launcher builder 中移除相应的
      .on_response(...)
      或
      .on_request(...)
      。当用户只需要其中一半时原样粘贴两部分,会产生需要用户自行梳理的冗余内容。
  4. 对于 multi-file bundle,交付其中的每个文件,并明确说明每个文件在用户项目目录中的位置。不要只交付
    lib.rs
    ,让用户之后自行发现还需要修改
    gcl.yaml
    或添加 Cargo dependency。
  5. 编辑完成后,建议用户重新运行
    make build-asset-files
    (仅当
    gcl.yaml
    发生变化时需要运行,它会重新生成
    src/generated/config.rs
    )和
    make build
    。

Template index

模板索引

Request / response handling

请求/响应处理

FeatureTemplateSummary
header_manipulation
templates/header_manipulation.rs
Read, set, add, remove, and bulk-replace request and response headers.
body_manipulation
templates/body_manipulation.rs
Read and replace request/response bodies.
body_stream
templates/body_stream.rs
Stream request/response bodies in chunks.
request_data
templates/request_data.rs
Read method, scheme, host, path, query, and authority from a request.
control_flow
templates/control_flow.rs
Short-circuit a request with a synthetic response (
Flow::Break
).
FeatureTemplateSummary
header_manipulation
templates/header_manipulation.rs
读取、设置、添加、删除及批量替换请求和响应 headers。
body_manipulation
templates/body_manipulation.rs
读取并替换请求/响应 body。
body_stream
templates/body_stream.rs
以分块方式流式处理请求/响应 body。
request_data
templates/request_data.rs
从请求中读取 method、scheme、host、path、query 和 authority。
control_flow
templates/control_flow.rs
使用合成响应(
Flow::Break
)提前终止请求。

Identity and access control

身份验证与访问控制

FeatureTemplateSummary
authentication
templates/authentication.rs
Read and set the request's resolved authentication principal.
jwt
templates/jwt.rs
Validate a JWT signature and extract claims/headers.
jwt_generate
templates/jwt_generate.rs
Mint a new signed JWT from claims.
oauth2_token_introspection
templates/oauth2_token_introspection.rs
Introspect an OAuth2 access token (RFC 7662).
ip_filter
templates/ip_filter.rs
Allow/deny by client IP / CIDR.
contracts
templates/contracts.rs
Validate Anypoint API contracts (
client_id
/
client_secret
).
FeatureTemplateSummary
authentication
templates/authentication.rs
读取并设置请求中已解析的 authentication principal。
jwt
templates/jwt.rs
验证 JWT 签名并提取 claims/headers。
jwt_generate
templates/jwt_generate.rs
根据 claims 签发新的已签名 JWT。
oauth2_token_introspection
templates/oauth2_token_introspection.rs
对 OAuth2 access token 执行 introspection(RFC 7662)。
ip_filter
templates/ip_filter.rs
根据 client IP/CIDR 允许或拒绝请求。
contracts
templates/contracts.rs
验证 Anypoint API contracts(
client_id
/
client_secret
)。

External calls

外部调用

FeatureTemplateSummary
http_call
templates/http_call/
Outbound HTTP requests against a configured
service
(multi-file:
lib.rs
+
gcl.yaml
).
grpc
templates/grpc/
Outbound gRPC against a
service
with protobuf codegen (multi-file: 5 files).
dataweave
templates/dataweave/
Evaluate a DataWeave expression against
payload
,
attributes
,
vars
,
authentication
(multi-file:
lib.rs
+
gcl.yaml
).
outbound
templates/outbound/gcl.yaml
Marker
gcl.yaml
declaring an outbound-only policy (no Rust).
FeatureTemplateSummary
http_call
templates/http_call/
针对已配置的
service
发起出站 HTTP 请求(multi-file:
lib.rs
+
gcl.yaml
)。
grpc
templates/grpc/
针对
service
发起出站 gRPC 调用,并使用 protobuf codegen(multi-file:5 个文件)。
dataweave
templates/dataweave/
针对
payload
、
attributes
、
vars
、
authentication
计算 DataWeave expression(multi-file:
lib.rs
+
gcl.yaml
)。
outbound
templates/outbound/gcl.yaml
用于声明 outbound-only policy 的标记
gcl.yaml
(无 Rust 代码)。

Validation and transformation

验证与转换

FeatureTemplateSummary
json_validator
templates/json_validator.rs
Validate a JSON body against a JSON Schema.
xml_validator
templates/xml_validator.rs
Validate an XML body against an XSD.
FeatureTemplateSummary
json_validator
templates/json_validator.rs
根据 JSON Schema 验证 JSON body。
xml_validator
templates/xml_validator.rs
根据 XSD 验证 XML body。

Rate, spike, and concurrency

速率、峰值与并发控制

FeatureTemplateSummary
rate_limiting
templates/rate_limiting.rs
Token-bucket rate limiter with quota windows.
spike_control
templates/spike_control.rs
Spike arrest with queueing.
cache
templates/cache.rs
Per-policy in-memory cache for response or computed values.
lock
templates/lock.rs
Distributed advisory lock for a critical section.
FeatureTemplateSummary
rate_limiting
templates/rate_limiting.rs
带 quota window 的 token-bucket rate limiter。
spike_control
templates/spike_control.rs
带排队机制的 spike arrest。
cache
templates/cache.rs
用于缓存响应或计算值的 per-policy 内存缓存。
lock
templates/lock.rs
用于 critical section 的 distributed advisory lock。

Observability and meta

可观测性与元数据

FeatureTemplateSummary
logger
templates/logger.rs
Emit structured logs at
info
,
warn
,
error
.
metadata
templates/metadata.rs
Read policy/runtime metadata (org, env, API instance, gateway).
policy_violation
templates/policy_violation.rs
Emit a structured policy-violation event for analytics.
timer
templates/timer.rs
Schedule a timer callback after N milliseconds.
FeatureTemplateSummary
logger
templates/logger.rs
以
info
、
warn
、
error
级别发出结构化日志。
metadata
templates/metadata.rs
读取 policy/runtime metadata(org、env、API instance、gateway)。
policy_violation
templates/policy_violation.rs
为 analytics 发出结构化 policy-violation event。
timer
templates/timer.rs
在 N 毫秒后调度 timer callback。

State

状态

FeatureTemplateSummary
data_storage
templates/data_storage.rs
Persistent KV store shared across workers.
worker_variable
templates/worker_variable.rs
Per-worker variable (no cross-worker sharing).
FeatureTemplateSummary
data_storage
templates/data_storage.rs
跨 workers 共享的 persistent KV store。
worker_variable
templates/worker_variable.rs
Per-worker variable(不跨 workers 共享)。

Other

其他

FeatureTemplateSummary
cors
templates/cors.rs
Handle CORS preflight and response headers.
stop_iteration
templates/stop_iteration/
Mutate body and headers in a single state transition (multi-file:
lib.rs
+
Cargo.toml.snippet
— requires PDK 1.8.0+ with
enable_stop_iteration
).
unit_testing
templates/unit_testing.md
pdk-unit
API reference (setup + worked example using
UnitTestBuilder
). For the end-to-end unit-testing workflow — wiring
src/tests/
, mocking upstreams, asserting on violations — use the sibling skill
pdk-unit
.
FeatureTemplateSummary
cors
templates/cors.rs
处理 CORS preflight 和 response headers。
stop_iteration
templates/stop_iteration/
在一次 state transition 中修改 body 和 headers(multi-file:
lib.rs
+
Cargo.toml.snippet
——需要 PDK 1.8.0+ 并启用
enable_stop_iteration
)。
unit_testing
templates/unit_testing.md
pdk-unit
API 参考(包含 setup 和使用
UnitTestBuilder
的完整示例)。如需完整的单元测试工作流,包括配置
src/tests/
、模拟 upstream 和断言 violations,请使用同级 skill
pdk-unit
。

Multi-file features

Multi-file features

Some features need more than just a
src/lib.rs
change. Always deliver the whole bundle and tell the user which file goes where.
某些 feature 不仅需要修改
src/lib.rs
。始终交付完整的 bundle,并明确说明每个文件应放置的位置。

dataweave/
—
templates/dataweave/

dataweave/
—
templates/dataweave/

  • lib.rs
    → user's
    src/lib.rs
    .
  • gcl.yaml
    → merge the
    properties.exampleDateweaveProperty
    block (with
    format: dataweave
    and the
    bindings
    map) into the user's
    definition/gcl.yaml
    . The Rust template calls
    config.example_dateweave_property.evaluator()
    , so the GCL property name in camelCase must produce that snake_case field on the generated
    Config
    struct. After editing, run
    make build-asset-files
    .
  • lib.rs
    → 用户的
    src/lib.rs
    。
  • gcl.yaml
    → 将
    properties.exampleDateweaveProperty
    block(包含
    format: dataweave
    和
    bindings
    map)合并到用户的
    definition/gcl.yaml
    中。Rust 模板调用
    config.example_dateweave_property.evaluator()
    ,因此 GCL 属性名必须使用 camelCase,并在生成的
    Config
    struct 中产生对应的 snake_case 字段。编辑后运行
    make build-asset-files
    。

http_call/
—
templates/http_call/

http_call/
—
templates/http_call/

  • lib.rs
    → user's
    src/lib.rs
    .
  • gcl.yaml
    → merge the
    properties.service
    block (with
    format: service
    ) into
    definition/gcl.yaml
    . The
    service
    format is what unlocks
    client.request(&config.service)
    in the Rust template. Run
    make build-asset-files
    .
  • lib.rs
    → 用户的
    src/lib.rs
    。
  • gcl.yaml
    → 将
    properties.service
    block(包含
    format: service
    )合并到
    definition/gcl.yaml
    中。
    service
    format 可在 Rust 模板中启用
    client.request(&config.service)
    。运行
    make build-asset-files
    。

stop_iteration/
—
templates/stop_iteration/

stop_iteration/
—
templates/stop_iteration/

  • lib.rs
    → user's
    src/lib.rs
    .
  • Cargo.toml.snippet
    → merge into
    Cargo.toml
    :
    • pdk
      dependency must be
      >= 1.8.0
      and carry
      features = ["enable_stop_iteration"]
      .
    • If the user is on an older PDK, send them to
      develop-pdk-policy
      's upgrade runbook before applying this template.
  • lib.rs
    → 用户的
    src/lib.rs
    。
  • Cargo.toml.snippet
    → 合并到
    Cargo.toml
    :
    • pdk
      dependency 的版本必须为
      >= 1.8.0
      ,并包含
      features = ["enable_stop_iteration"]
      。
    • 如果用户使用较旧版本的 PDK,应先让用户转到
      develop-pdk-policy
      的升级 runbook,然后再应用此模板。

grpc/
—
templates/grpc/

grpc/
—
templates/grpc/

  • lib.rs
    → user's
    src/lib.rs
    .
  • gcl.yaml
    → merge
    properties.exampleService
    (with
    format: service
    ) into
    definition/gcl.yaml
    .
  • build.rs
    → user's
    build.rs
    at the project root. Generates Rust bindings from
    proto/example.proto
    at compile time.
  • proto/example.proto
    → user's
    proto/example.proto
    . They will likely replace
    ExampleService
    and the message shapes with their actual service contract.
  • Cargo.toml.snippet
    → merge:
    • [dependencies]
      add
      protobuf = "3.5.0"
      .
    • [build-dependencies]
      add
      protobuf-codegen = "3.5.0"
      .
  • After all edits, run
    make build-asset-files
    then
    make build
    .
Filename consistency gotcha. If you rename
proto/example.proto
to something more meaningful (e.g.
proto/user.proto
for a
UserService
), three places must agree or the build breaks:
  1. The
    .proto
    file's path on disk.
  2. build.rs
    line
    .input("proto/<name>.proto")
    .
  3. The
    lib.rs
    import
    use crate::<name>::{...};
    —
    protobuf-codegen
    emits one Rust module per
    .proto
    file, named after the file stem. Keep all three in sync.
  • lib.rs
    → 用户的
    src/lib.rs
    。
  • gcl.yaml
    → 将
    properties.exampleService
    (包含
    format: service
    )合并到
    definition/gcl.yaml
    中。
  • build.rs
    → 用户项目根目录下的
    build.rs
    。在编译时从
    proto/example.proto
    生成 Rust bindings。
  • proto/example.proto
    → 用户的
    proto/example.proto
    。用户很可能需要将
    ExampleService
    和 message 结构替换为其实际的服务 contract。
  • Cargo.toml.snippet
    → 合并以下内容:
    • 在
      [dependencies]
      中添加
      protobuf = "3.5.0"
      。
    • 在
      [build-dependencies]
      中添加
      protobuf-codegen = "3.5.0"
      。
  • 完成所有编辑后,运行
    make build-asset-files
    ,然后运行
    make build
    。
**文件名一致性陷阱。**如果将
proto/example.proto
重命名为更有意义的名称,例如为
UserService
使用
proto/user.proto
,以下三处必须保持一致,否则构建会失败:
  1. .proto
    文件在磁盘上的路径。
  2. build.rs
    中的
    .input("proto/<name>.proto")
    这一行。
  3. lib.rs
    中的 import:
    use crate::<name>::{...};
    ——
    protobuf-codegen
    会为每个
    .proto
    文件生成一个 Rust module,module 名称以文件 stem 命名。请确保这三处始终同步。

outbound/
—
templates/outbound/gcl.yaml

outbound/
—
templates/outbound/gcl.yaml

  • gcl.yaml
    only → merge into
    definition/gcl.yaml
    . The
    metadata/capabilities/injectionPoint: outbound
    label converts the policy into an outbound policy. The user can then add request/response filters by composing with
    header_manipulation
    or
    http_call
    templates.
  • 仅需
    gcl.yaml
    → 合并到
    definition/gcl.yaml
    中。
    metadata/capabilities/injectionPoint: outbound
    标签会将该 policy 转换为 outbound policy。之后,用户可以通过组合
    header_manipulation
    或
    http_call
    模板来添加 request/response filters。

Source of truth

权威来源

For lifecycle questions (scaffold, build, run locally, publish, release, upgrade), use the
develop-pdk-policy
skill.
对于生命周期相关问题(scaffold、build、本地运行、publish、release、升级),请使用
develop-pdk-policy
skill。