nesnilnehc/ai-cortex/consume-nats-message
Claude Code and agent skill by nesnilnehc/ai-cortex
- community
Checking that the SKILL.md file is still in the repository.
Drain pending NATS messages from a producer contract via NATS MCP tools. Discovers the available NATS tool capabilities, selects exact-subject or wildcard mode from .cortex/nats.yaml, applies Tolerant Reader semantics, executes ack/nak/term decisions, and returns aggregated stats.
Install
npx vskill@latest install nesnilnehc/ai-cortex/consume-nats-messageSKILL.md
The same SKILL.md was published earlier as nesnilnehc/ai-cortex/consume-nats-message.
Skill: Consume NATS Message
Purpose
Run one bounded NATS JetStream pull-consume inside the consumer repository: read the local configuration and the producer contract, fetch the pending messages, parse each one under Tolerant Reader semantics and call the business handler, then execute the right ack / nak / term + DLQ decision.
The default is drain-style: keep fetching until the queue is drained, the message cap is reached, or consecutive empty fetches reach the idle threshold.
Authoritative references
This Skill orchestrates a single consume action and nothing more; it does not redefine the NATS contract rules.
| Topic | Authoritative source | |
|---|---|---|
| subject / headers / payload / Tolerant Reader / DLQ / IaC boundary | specs/nats-messaging.md | |
contract_version / CHANGELOG / *-contract.md structure | specs/cross-team-contract.md | |
| Skill / Spec / Protocol / Rule boundaries | docs/architecture/terminology.md | |
| External links and raw-content policy | AGENTS.md |
Where this Skill conflicts with the assets above, follow the repository authority order AGENTS.md > specs/ > protocols/ > rules/ > docs/; the Skill body keeps the execution orchestration only.
Preconditions
- The current working directory is the consumer repo.
- The consumer repo carries
.cortex/nats.yaml, or the user allows a temporary configuration to be filled in during this interaction. - Some NATS MCP server is connected. Tool names need not be fixed, but they must map onto the fetch / ack / nak / term / publish-DLQ capabilities.
- The producer contract comes preferably from the local vendored snapshot; an external HTTP/HTTPS contract or official document may be read only where the current context explicitly permits external fetching.
Input
The user may supply any combination of:
producer: the producer name or service source.event: the event name.subject: an explicit NATS subject.contract_path: the local contract path, recommended to carry@versionsemantics.max_messages/batch_size/fetch_timeout/idle_threshold: override the defaults in.cortex/nats.yaml.
When producer / event / subject is missing, infer it from .cortex/nats.yaml and the vendored contracts first; ask the user only when that still leaves it ambiguous.
Behavior
0. Announce
Tell the user:
I am using the consume-nats-message skill to drain a batch of pending messages.
1. Read the local context
- Read
.cortex/nats.yaml. - Extract:
broker_urlservice_sourcedurable_name_prefixdlq_handler_dirvendor_contracts_dirconsume_defaultsconsume_subjects(optional)consume_pattern(optional)
- Override
consume_defaultswith the user's explicit parameters. - Where the configuration is missing, ask for the minimum necessary fields, and remind the user to commit the configuration into the repo afterwards.
2. Discover and map the NATS MCP tools
Do not assume the MCP tool names are fixed. Inspect the NATS-related tools the current runtime exposes and build a capability map first:
| Capability | Use | If missing | |
|---|---|---|---|
list_streams_or_subjects | List the broker topology during Bootstrap | Bootstrap cannot run; stop and name the missing capability | |
peek_message | Read-only sampling during Bootstrap; no ack, no durable offset movement | Stop where the contract is missing; a real drain cannot start | |
attach_or_pull_consumer | Reuse an existing durable and fetch a batch | Stop, and prompt the user to check the MCP configuration | |
ack | Acknowledge a successful or duplicate message | Stop; consuming safely is not possible | |
nak | Redeliver a retryable failure | Stop; consuming safely is not possible | |
term | Stop redelivery for an unrecoverable failure | Stop; failed messages cannot be isolated safely | |
publish | Send the DLQ copy | The term path is unusable; stop, or degrade to term-only on the user's confirmation |
Where a tool name differs from the example name, call by capability rather than by name. Where no tool-discovery capability exists, match manually against the current tool list; stop and ask the user about the configuration when it cannot be confirmed.
3. Choose the consume mode
| Condition | Mode | Behavior | |
|---|---|---|---|
.cortex/nats.yaml sets consume_pattern | wildcard | Build one <durable_name_prefix>-wildcard durable and match contracts dynamically against each message's actual subject | |
consume_pattern unset, but subject / producer + event / consume_subjects present | exact-subject | Reuse one exact durable per subject | |
| Neither is enough to settle it | discovery | List the local contracts and the visible subjects, and let the user choose |
consume_pattern and consume_subjects are mutually exclusive; once consume_pattern is present, this invocation must ignore consume_subjects, to avoid the same message being handled independently by several durables.
4. Locate or bootstrap the contract
exact-subject mode
Locate the contract in this order:
<vendor_contracts_dir>/<producer>/<event>-contract.md- The local
contract_paththe user supplied - The local contract that
.cortex/nats.yamlorconsume_subjectspoints at - Still missing → enter Bootstrap
Read the contract frontmatter and body, extracting at least contract_version, status, subject, the required headers, the payload fields, the DLQ subject, max_deliver, and backoff. Field semantics are governed by specs/nats-messaging.md and by the contract itself.
wildcard mode
Do not lock onto a single contract before the consumer is built. As each message arrives, run two pre-filters first; hitting either one leads straight to ack, skipping contract resolution, de-duplication, decoding, and the business handler (steps 2-7). Only when neither hits does processing carry on:
- Self-produced message:
headers['X-Source']exists and equalsservice_source(that is, this service emitted the message itself) →ackstraight away; do not resolve the contract, do not generate a draft, do not enter the business handler.
Why: a wildcard subject pattern matches on prefix only and carries no direction information. When producer and consumer share one naming prefix (both sides agreeing that zentao.omnireview.> carries events in both directions, say), this service's own outbound messages also match its wildcard filter and are delivered back to it. That is not an "unknown contract" but an echo, and running it through the unknown-subject flow would generate surplus draft contract files for the service's own messages.
A missing X-Source is not judged a self-produced message (no equality comparison against undefined/None) — let it through to the next filter and to real contract resolution; a message genuinely missing that required header is judged unrecoverable at step 3, "decode headers", and goes to term + DLQ, instead of being quietly swallowed by this pre-filter or making the read itself throw and break the whole batch.
- DLQ copy self-loop: the message carries an
X-DLQ-Original-Subjectheader (that is, the message is itself a DLQ copy this servicepublished earlier — see the list of headers a DLQ copy always carries under "10. Ack decisions") →ackstraight away; do not resolve the contract.
Why: the copy that term + publish DLQ produces lands on the subject <original-subject>.dlq, which matches the wildcard prefix just as well and reaches the same wildcard consumer again. A DLQ copy is the end point of a failure archive, not a business event awaiting processing; it needs no further round of contract resolution or a second DLQ, and should not trigger one. Judge it by the self-produced X-DLQ-Original-Subject header rather than by a bare .dlq suffix string match — the latter would misread a normal business subject that happens to end in .dlq as a loop, silently discarding real business messages.
When neither pre-filter hits, match msg.subject exactly against the subject: in the frontmatter of <vendor_contracts_dir>/<producer>/*.md:
- Hit with
status: active: carry on with normal processing. - Hit with
status: draft: do not enter the business handler; take the awaiting-confirmation path. - No hit: generate an N=1 draft contract for that subject, then take the awaiting-confirmation path.
5. Bootstrap a draft contract
Runs only when the contract is missing. Bootstrap is a read-only probing phase:
- No ack.
- No nak.
- No term.
- No creating or advancing of a real durable offset.
- No change to the authoritative producer contract.
Steps:
- Use the NATS MCP topology/peek capabilities to list streams / subjects / message counts.
- Choose the target subject; where it cannot be pinned down, ask the user.
- Peek the most recent N samples, N=5 by default; N=1 when a wildcard misses.
- Infer the headers and the payload fields:
- With N>1, a field present 100% of the time may be marked required, and one below 100% optional.
- With N=1, a field that shows up must not be judged required automatically; requiredness is marked
to be confirmedthroughout. - QoS / max_deliver / DLQ cannot be inferred reliably; mark them
TBD.
- Write the local draft contract:
<vendor_contracts_dir>/<producer>/<event>-contract.md. - The frontmatter must carry:
Branches after Bootstrap:
| Scenario | Default handling | |
|---|---|---|
| A new draft in exact-subject mode | Show the draft summary and ask the user whether to continue into a real drain; by default it does not continue | |
| wildcard mode meets an unknown subject | Generate the draft, then term + DLQ the current message, annotate it awaiting contract confirmation, and do not break the batch | |
| wildcard mode hits a draft | term + DLQ the current message, annotate it awaiting contract confirmation, and do not break the batch |
Only where the user explicitly confirms "continue the real drain on the draft" may exact-subject mode enter real processing under a draft contract; otherwise stop and wait for the producer owner to confirm the contract.
6. Version and status checks
exact-subject mode:
- Read the contract's
contract_versionand<vendor_contracts_dir>/<producer>/.lock. - Where
.lockis missing, write the current<contract-name>@<version>and treat it as the first alignment. - MINOR / PATCH drift: flag the risk, then carry on.
- MAJOR drift or a breaking subject change: stop, and prompt for an upgrade of the vendored contract.
status: draft: the user's confirmation must be obtained before any message is formally acked.
wildcard mode:
- No per-message SemVer drift check.
- Only a contract with
status: activemay enter the business handler. - A
draftor unknown contract always takes the awaiting-confirmation path.
7. Create or reuse the pull consumer
Honor the IaC boundary in specs/nats-messaging.md: the Skill creates no stream and no durable resources.
exact-subject mode:
- durable name:
<durable_name_prefix>-<event-slug> - filter subject: the contract subject
- Where the durable does not exist, stop and point at the IaC owner
wildcard mode:
- durable name:
<durable_name_prefix>-wildcard - filter subject:
consume_pattern - ack policy:
explicit ack_wait/max_delivertake the durable-level defaults; they cannot be forced per subject- Where the durable does not exist, stop and point at the IaC owner
8. The drain loop
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/consume-nats-message/SKILL.md
Popularity
- Repository stars
- 7
- Repository forks
- 3
- 7-day trend score
- 15
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/nesnilnehc/ai-cortex/consume-nats-message)