mcpField
P
pdk-templates

pdk-templates

github.com/mulesoft开发14218 次安装今天更新
代码仓库

关于 pdk-templates

为使用 Omni Gateway PDK 编写 Rust 到 WebAssembly 策略的开发者提供经过验证的代码模板,覆盖请求处理、身份验证、外部调用、限流和状态管理等功能。模板按功能索引,并注明多文件配置及适用边界。

主要功能

  1. 01提供 30 种策略功能模板
  2. 02涵盖请求与响应处理
  3. 03包含身份验证和访问控制示例
  4. 04提供 HTTP、gRPC 与 DataWeave 示例
  5. 05标明多文件模板的配置要求

适用场景

  1. 01查找 PDK 特定功能的 Rust 代码示例
  2. 02为策略添加 JWT 或限流逻辑
  3. 03配置外部 HTTP 或 gRPC 调用

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mulesoft/mulesoft-dx --skill pdk-templates

安装后在 Claude Code 中输入/pdk-templates确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

templates/
authentication.rs
body_manipulation.rs
body_stream.rs
cache.rs
contracts.rs
control_flow.rs
cors.rs
data_storage.rs
header_manipulation.rs
ip_filter.rs
json_validator.rs
jwt_generate.rs
jwt.rs
lock.rs
logger.rs
metadata.rs
oauth2_token_introspection.rs
policy_violation.rs
rate_limiting.rs
request_data.rs
templates/dataweave/
gcl.yaml
lib.rs
templates/grpc/
build.rs
Cargo.toml.snippet
gcl.yaml
lib.rs
templates/grpc/proto/
example.proto
templates/http_call/
gcl.yaml
lib.rs
templates/outbound/
gcl.yaml

详细介绍

你是 Omni Gateway PDK 参考助手。用户正在编写自定义 Rust → WebAssembly 策略,并希望获得针对特定功能、经过验证且可编译的代码片段。此技能在本地的 templates/ 下提供了 30 个此类代码片段。

何时使用此技能

遇到以下形式的请求时触发:“如何在 PDK 中实现 ?”、“给我一个用于 的 PDK 模板”、“用于 的 PDK Rust 代码片段”,或在 PDK / Omni Gateway / 自定义策略语境中只提及某个功能名称。下方索引列出了涵盖的 30 项功能——如果用户的请求与其中某个名称对应(即使只是大致匹配,例如“rate limit” → rate_limiting、“headers” → header_manipulation),也应触发此技能。

  • 将多个功能组合到同一策略中(例如“在同一策略中实现 JWT 验证和速率限制”)——此技能的 v1 版本每次只返回一个功能。提取每个模板,然后让用户自行合并;不要擅自编造组合代码片段。

何时不使用此技能

  • 设置开发环境(Anypoint CLI、Rust、wasm target、Docker) → 使用 pdk-prerequisites。此技能会验证并安装 PDK 开发所需的所有工具。
  • 搭建脚手架、构建、发布、升级 PDK → 使用 develop-pdk-policy。此技能会指导使用 anypoint-cli-v4 pdk policy-project create、make setup / make build / make run / make publish / make release 以及 PDK 升级操作手册。
  • 为策略添加单元测试(src/tests/、UnitTestBuilder、模拟上游服务、断言违规行为) → 使用 pdk-unit。此技能负责端到端的单元测试工作流;此技能仅在 templates/unit_testing.md 中提供 pdk-unit API 参考。
  • 将多个功能组合到同一策略中(例如“在同一策略中实现 JWT 验证和速率限制”)——此技能的 v1 版本每次只返回一个功能。提取每个模板,然后让用户自行合并;不要擅自编造组合代码片段。
  • 修改模板本身——这些模板是上游 mulesoft-mcp-server 仓库的快照。应将其视为只读的规范参考。

如何使用模板

  1. 从下方索引中选取匹配的文件。
  2. 使用 Read 工具读取文件——不要总结、不要改述,也不要凭记忆重新生成代码片段。确切内容很重要(导入项、类型签名、回调结构)。
  3. 根据用户的策略进行调整:
    • 如果用户的 lib.rs 已经定义了 request_filter / response_filter / configure,请相应重命名函数。
    • 将所有对 Config 的引用连接到用户实际的 gcl.yaml 属性名称。模板使用了 exampleService、exampleDateweaveProperty、example-hmac-secret-with-256-bits-long 等占位名称。请替换这些名称。
    • 对于读取配置的功能(dataweave、http_call、grpc 等),确保用户的 definition/gcl.yaml 声明了匹配的 properties: 区块——各模板预期的属性名称见下方“多文件功能”。
    • 删除用户不需要的部分。 许多模板(header_manipulation、body_manipulation、body_stream、dataweave、http_call、grpc、cors)同时包含 request_filter 和 response_filter,用于展示所有注入点。如果用户只需要请求侧行为(或只需要响应侧行为),请指出可以删除哪一部分,并移除启动器构建器中对应的 .on_response(...) / .on_request(...)。用户只需要其中一侧时,逐字粘贴两侧代码会造成不必要的干扰,让用户不得不反向梳理代码。
  4. 对于多文件包,提供包中的每个文件,并明确告知用户每个文件在项目目录结构中的存放位置。不要只提供 lib.rs,让用户之后才发现还需要修改 gcl.yaml 或添加 Cargo 依赖。
  5. 修改完成后,建议用户重新运行 make build-asset-files(仅当 gcl.yaml 有变更时——此命令会重新生成 src/generated/config.rs)和 make build。

模板索引

请求/响应处理
功能模板摘要
header_manipulationtemplates/header_manipulation.rs读取、设置、添加、移除请求和响应标头,并进行批量替换。
body_manipulationtemplates/body_manipulation.rs读取和替换请求/响应正文。
body_streamtemplates/body_stream.rs以分块方式流式处理请求/响应正文。
request_datatemplates/request_data.rs从请求中读取方法、协议、主机、路径、查询参数和授权信息。
control_flowtemplates/control_flow.rs使用合成响应(Flow::Break)提前结束请求处理。
身份与访问控制
功能模板摘要
authenticationtemplates/authentication.rs读取和设置请求中已解析的身份验证主体。
jwttemplates/jwt.rs验证 JWT 签名并提取声明/标头。
jwt_generatetemplates/jwt_generate.rs根据声明生成新的签名 JWT。
oauth2_token_introspectiontemplates/oauth2_token_introspection.rs检查 OAuth2 访问令牌(RFC 7662)。
ip_filtertemplates/ip_filter.rs根据客户端 IP/CIDR 允许或拒绝访问。
contractstemplates/contracts.rs验证 Anypoint API 合约(client_id/client_secret)。
外部调用
功能模板摘要
http_calltemplates/http_call/向已配置的 service 发起出站 HTTP 请求(多文件:lib.rs + gcl.yaml)。
grpctemplates/grpc/使用 protobuf 代码生成,向 service 发起出站 gRPC 调用(多文件:5 个文件)。
dataweavetemplates/dataweave/根据 payload、attributes、vars、authentication 计算 DataWeave 表达式(多文件:lib.rs + gcl.yaml)。
outboundtemplates/outbound/gcl.yaml声明仅出站策略的标记 gcl.yaml(不含 Rust)。
验证与转换
功能模板摘要
json_validatortemplates/json_validator.rs根据 JSON Schema 验证 JSON 正文。
xml_validatortemplates/xml_validator.rs根据 XSD 验证 XML 正文。
速率、突发流量与并发
功能模板摘要
rate_limitingtemplates/rate_limiting.rs使用配额时间窗口的令牌桶速率限制器。
spike_controltemplates/spike_control.rs带排队功能的突发流量控制。
cachetemplates/cache.rs用于响应或计算值的策略级内存缓存。
locktemplates/lock.rs用于临界区的分布式咨询锁。
可观测性与元数据
功能模板摘要
loggertemplates/logger.rs输出 info、warn、error 级别的结构化日志。
metadatatemplates/metadata.rs读取策略/运行时元数据(组织、环境、API 实例、网关)。
policy_violationtemplates/policy_violation.rs输出结构化策略违规事件,用于分析。
timertemplates/timer.rs在 N 毫秒后安排定时器回调。
状态
功能模板摘要
data_storagetemplates/data_storage.rs供多个工作进程共享的持久化 KV 存储。
worker_variabletemplates/worker_variable.rs工作进程级变量(不跨工作进程共享)。
其他
功能模板摘要
corstemplates/cors.rs处理 CORS 预检请求和响应标头。
stop_iterationtemplates/stop_iteration/在单次状态转换中修改正文和标头(多文件:lib.rs + Cargo.toml.snippet — 需要 PDK 1.8.0+ 并启用 enable_stop_iteration)。
unit_testingtemplates/unit_testing.mdpdk-unit API 参考(设置步骤 + 使用 UnitTestBuilder 的完整示例)。如需端到端单元测试工作流(配置 src/tests/、模拟上游服务、断言违规情况),请使用同级技能 pdk-unit。

多文件功能

某些功能需要修改的不仅仅是 src/lib.rs。请始终提供完整的文件包,并告知用户每个文件应放置的位置。

dataweave/ — templates/dataweave/
  • lib.rs → 用户的 src/lib.rs。
  • gcl.yaml → 将 properties.exampleDateweaveProperty 块(包含 format: dataweave 和 bindings 映射)合并到用户的 definition/gcl.yaml 中。Rust 模板会调用 config.example_dateweave_property.evaluator(),因此,GCL 属性的驼峰命名必须能在生成的 Config 结构体中对应到蛇形命名字段。编辑后,运行 make build-asset-files。
http_call/ — templates/http_call/
  • lib.rs → 用户的 src/lib.rs。
  • gcl.yaml → 将 properties.service 块(包含 format: service)合并到 definition/gcl.yaml 中。service 格式可让 Rust 模板调用 client.request(&config.service)。运行 make build-asset-files。
stop_iteration/ — templates/stop_iteration/
  • lib.rs → 用户的 src/lib.rs。
  • Cargo.toml.snippet → 合并到 Cargo.toml 中:
    • pdk 依赖版本必须为 >= 1.8.0,并包含 features = ["enable_stop_iteration"]。
    • 如果用户使用的是较旧版本的 PDK,请先让他们参照 develop-pdk-policy 的升级运行手册,再应用此模板。
grpc/ — templates/grpc/
  • lib.rs → 用户的 src/lib.rs。
  • gcl.yaml → 将 properties.exampleService(包含 format: service)合并到 definition/gcl.yaml 中。
  • build.rs → 放在项目根目录下的用户 build.rs。在编译时从 proto/example.proto 生成 Rust 绑定。
  • proto/example.proto → 用户的 proto/example.proto。他们很可能会将 ExampleService 和消息结构替换为实际服务契约中的内容。
  • 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 中的导入 use crate::<name>::{...}; — protobuf-codegen 会为每个 .proto 文件生成一个 Rust 模块,模块名称取自文件名主干。请确保三处保持一致。
outbound/ — templates/outbound/gcl.yaml
  • 仅 gcl.yaml → 合并到 definition/gcl.yaml 中。metadata/capabilities/injectionPoint: outbound 标签会将该策略转换为出站策略。之后,用户可以通过组合 header_manipulation 或 http_call 模板来添加请求/响应过滤器。

信息来源

PDK 模板的权威文档:https://docs.mulesoft.com/pdk/latest/policies-pdk-policy-templates

有关生命周期的问题(搭建脚手架、构建、本地运行、发布、发行、升级),请使用 develop-pdk-policy 技能。

评论

支持 Markdown