Authoring a ruleset
Write an API Governance ruleset with your own AI agent and the P4A MCP server, validate it as you go, then save it as a workspace ruleset and deploy it.
Overview
P4A doesn't run an AI model. Your own agent (Claude Code, Cursor, or any MCP client connected to the P4A MCP server) writes the ruleset, and P4A gives it the dialect rules and a validator so the result is correct. The loop is:
- Ask the agent to use the
author_rulesetprompt and describe the rule in plain English. - The agent writes the YAML and calls
validate_ruleset. - It fixes whatever the validator reports and repeats until the result is
valid: true. - It saves the result as a workspace ruleset with
save_ruleset_draft. - After you confirm, it deploys the workspace ruleset to your own Exchange with
deploy_ruleset_draft.
You don't need a repository for any of this, and the workspace ruleset opens in the portal's guided editor too. A repository is only needed to list the ruleset in the public catalog: see Step 5.
Before you start
- Connect your agent to P4A. See Connecting to the MCP server.
- Have a workspace to save workspace rulesets in. Your personal workspace works. See Using workspaces.
- For the final, authoritative check you also need a registered Anypoint connection. See Connecting your Anypoint ORG.
Step 1: describe the rule
Start the author_ruleset prompt and put your rule in the optional rule argument. The prompt gives your agent the Validation Profile dialect, the three severities, rule and constraint examples, and the common pitfalls, so you don't have to explain the format. A good request names the thing being checked, the condition, and how serious a miss is:
Every operation needs a description, and every API needs a title. A missing title blocks conformance. A missing description is only a warning.
The agent produces a profile like this:
#%Validation Profile 1.0
profile: API basics
description: Minimum documentation rules for REST APIs.
violation:
- api-has-title
warning:
- operations-have-descriptions
validations:
api-has-title:
message: The API must have a title.
targetClass: apiContract.WebAPI
propertyConstraints:
core.name:
minCount: 1
operations-have-descriptions:
message: Every operation should have a description.
targetClass: apiContract.Operation
propertyConstraints:
core.description:
minCount: 1Severity decides the outcome. A rule under violation makes an API non-conformant, warning is worth fixing but doesn't block, and info is advice only.
Step 2: start from an existing ruleset (optional)
If you want to adapt an existing rule, ask the agent to call fork_mulesoft_ruleset with a MuleSoft ruleset's slug, or fork_ruleset with a community ruleset's id. You can find both on the Rulesets page or with search_rulesets. In the portal, Customize this ruleset on a ruleset's page shows the same files, and Save as workspace ruleset copies the YAML into a workspace ruleset you can edit and deploy. The tool returns:
- the ruleset's Validation Profile YAML
- a suggested
exchange.jsonwith a new asset ID, a(custom)name, and version1.0.0 - the files you'd commit if you later submit it to the catalog
- the next steps
Your agent can edit the YAML and save it as a workspace ruleset, as in Step 4. Your fork needs its own asset ID, because you can't publish over the original. See Creating Custom Rulesets by Modifying Published Rulesets.
Step 3: validate until it passes
validate_ruleset takes the YAML and returns valid, a list of errors with a 1-based line where it can tell, the profile name, and the rule names with counts per severity. The same check is available over REST as POST /api/rulesets/validate, and in the portal as Validate a ruleset on the Rulesets page, where you paste the YAML.
There are two levels:
| Quick check | Authoritative check | |
|---|---|---|
| How to ask | The default (authoritative: false) | authoritative: true, plus connectionId and workspaceId |
| What it does | Checks the file's structure: dialect header, profile name, severity lists, and that every listed rule is defined | Runs the same validator Anypoint uses, with your connection |
| Speed | Instant | Waits up to about 30 seconds |
| Limits | None beyond normal API limits | Counts against a daily limit, and is unavailable while deployments are paused |
If the authoritative check is still running when the wait ends, the result is pending: true with a jobId. Call validate_ruleset again with only that jobId (no YAML) to fetch the result.
A ruleset that fails the authoritative check isn't an error in P4A. You get valid: false and the validator's messages, so your agent can fix the rule and try again.
The quick check looks at the profile's structure. It doesn't run your rules against a real API. Use the authoritative check, or test locally with the API Governance CLI, before you rely on a rule. See Validating and Publishing Custom Rulesets.
Step 4: save and deploy a workspace ruleset
Ask the agent to save the ruleset with save_ruleset_draft, passing a workspaceId, a name and the yaml. Every active member of that workspace can open the workspace ruleset, in the editor or over MCP. To update it later, the agent passes the draftId and the expectedRevision it last read. If someone else saved in between, the save is refused with the current revision, so nobody's changes are lost.
To publish, the agent calls deploy_ruleset_draft with the draftId, the expectedRevision, an Anypoint connectionId and the targetOrganizationIds. Your client asks you to confirm first. Leave out version and P4A picks the next one. There's no review step, because the ruleset only goes to your own Exchange. See Deploying a workspace ruleset.
list_ruleset_drafts, get_ruleset_draft and delete_ruleset_draft cover the rest. The MCP tools reference lists every argument.
Step 5: submit to the catalog (optional)
A workspace ruleset never reaches the public catalog. To share the ruleset there, commit it as ruleset.yaml in a public repository, with the exchange.json if you have one, and ask the agent to call submit_ruleset with the repository URL. P4A re-checks the repository before accepting the submission, then it follows the normal review path. The submission guide lists every check and how to fix a failure.
Tips for better rules
- Keep one concern per rule, and give each a clear
message. The message is what an API author sees when the rule fails. - Start a new rule as a
warningand promote it toviolationonce teams have had time to fix their APIs. - Validate after every change. Small rulesets are easier for the agent to repair than a large one.