Skip to content

Add MCP blueprint management tools#6

Closed
hookdump wants to merge 30 commits into
mainfrom
xdev-2316-crud-for-blueprints
Closed

Add MCP blueprint management tools#6
hookdump wants to merge 30 commits into
mainfrom
xdev-2316-crud-for-blueprints

Conversation

@hookdump

@hookdump hookdump commented May 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a full read + create + granular-write tool surface to the @churnkey/mcp server so agents can inspect, create, edit, publish, and manage cancel-flow blueprints and segments through a Data API key. Every edit is granular (the agent never resends the whole nested steps array), draft edits are kept separate from live-impacting actions, and the API audit-logs every configuration change.

This branch is rebased onto current origin/main and includes support for the new cancel-flow REBATE offer type. update_blueprint_offer accepts offerType: "REBATE" plus rebate config fields: amountType, customAmount, percentAmount, mbgWindowDays, and invoiceScope.

Backed by companion API PR churnkey/churnkey-api#836.

Tools

Read

  • list_blueprints - current flow inventory: the default org flow plus every non-deleted segment flow, each with status, published, hasUnpublishedChanges, editable/published blueprint IDs, and compact draft/published metadata.
  • get_blueprint - the full blueprint including the steps array and stable guid values for targeted edits.
  • list_segments - segment metadata in priority order, including priority, enabled, and audience filter rules.
  • list_segment_attributes - the targetable audience-filter attributes for cancel flow segments (builtIn = the cancel-flow palette with each attribute's valueType/operands, plus the org's custom attributes). update_segment_filter rejects built-ins scoped to other products (e.g. CUSTOMER_HAS_PHONE) so MCP-set audiences stay editable in the dashboard. Fixed-enum attributes (BILLING_INTERVAL, SUBSCRIPTION_STATUS, SUBSCRIPTION_DISCOUNT) also return their allowed values ({value,label}, provider-scoped); update_segment_filter validates against them, requiring the exact dashboard value (e.g. MONTH, not month) so segments render correctly in the builder.

Create

  • create_blueprint (confirm: "create_blueprint") - creates the default org-level draft only when the org does not already have one. Supports template: "empty" | "BASIC" | "B2B" | "MERGEFIELDS" plus optional name/branding/languages.
  • create_segment_flow (confirm: "create_segment_flow") - creates a cancel-flow segment and a linked editable blueprint draft. This is the preferred repeatable path for new flows and smoke tests.
  • archive_segment (confirm: "archive_segment") - soft-deletes a segment, useful for cleaning up disposable test flows.

Blueprint draft edits - granular, draft-only

  • update_blueprint_draft - top-level fields (name, brandImage, primaryColor, translatedLanguages).
  • update_blueprint_step - one step's copy + behavioral config by stepGuid or stepIndex.
  • update_blueprint_offer - an offer's type + functional config, addressable on an offer step, survey choice, or structured follow-up option. Supports discount, pause, trial extension, redirect, plan change, contact, and rebate. If the target (step/choice/option) has no offer yet, passing offerType seeds one with default config (mirrors the dashboard's add-offer); editing an offer-less target without offerType is rejected.
  • edit_survey_structure - add/remove/reorder survey choices and configure a choice's follow-up.
  • add_blueprint_step / remove_blueprint_step - add a step at a canonical place or remove one by stepGuid.

Publish & live segment config - confirm-gated

  • publish_blueprint (confirm: "publish")
  • reorder_segments (confirm: "reorder_segments")
  • set_segment_enabled (confirm: "set_segment_enabled")
  • update_segment_filter (confirm: "update_segment_filter") — whole-array replace; a live (enabled + published) segment also requires confirmLiveChange: true since the change takes effect immediately.

Design principles

  • Granular, not full-payload. Each edit sends only the targeted fields, so the agent never reconstructs the full steps array with translations.
  • Draft vs. live. Blueprint edits mutate the unlocked working copy only; publish_blueprint is the live gate. Segment creation/priority/enabled/filter/archive mutate live segment config and require confirmation.
  • Creation mirrors builder quirks. The dashboard creates a blank blueprint and then prepopulates survey/offer structure client-side. The Data API/MCP path now supports server-side templates so newly created flows are useful immediately.
  • Enabled state is orthogonal to publish. Publishing a segment flow never changes its enabled state (mirrors the dashboard) — segments default to enabled at creation, and set_segment_enabled toggles targeting independently.
  • Disposable smoke path. The smoke script creates a disabled segment flow, mutates its draft, verifies persistence, changes one disposable offer to REBATE, then archives the segment instead of touching the org's primary draft.

API dependency

Depends on companion API PR: https://github.com/churnkey/churnkey-api/pull/836

Linear

Related PRs

Validation

  • pnpm --filter @churnkey/mcp exec vitest run tests/tools/blueprints.test.ts tests/tools/segments.test.ts - 23 tests passed.
  • pnpm --filter @churnkey/mcp typecheck - passed.
  • pnpm --filter @churnkey/mcp build - passed.
  • git diff --check - clean.

Smoke note

Live MCP smoke passed against local API http://localhost:3000/v1 using the test Data API credentials for app 22w36slh6.

  • node packages/mcp/scripts/smoke.mjs - 14 checks passed.
  • node packages/mcp/scripts/smoke.mjs --mutate - 24 checks passed.
  • The mutating smoke created a disabled disposable segment flow, patched survey structure, updated pause config, switched an offer to REBATE, verified the rebate config via re-read, and archived the segment.

hookdump and others added 14 commits June 3, 2026 16:33
The Data API returns error bodies as plain text, but the client only read
a JSON `message` field, so every validation/authorization failure showed as
a generic "Churnkey API error <status>". Relay the server message verbatim
and add clearer 403/404 fallbacks.

Contract/doc alignment with the dashboard:
- list_blueprints: document the new hasUnpublishedChanges flag and that
  status is a coarse subset of the dashboard badges (not a 1:1 mirror)
- get_blueprint: document the step/offer/survey-choice guid response shape
- list_segments: document audience filter rules, priority, enabled, and
  that A/B variant segments appear as separate entries
- update_blueprint_draft: brandImage validation now mirrors the server
  (path ends in png/jpg/jpeg/gif/webp, or images.churnkey.co); reject
  empty-string step/choice guids instead of silently treating as absent
- dsr_delete: document the deleted/reasonForRejection outcome
- README: scope the live/test mode note (sessions + DSR only; config is
  shared and recoveries are not mode-partitioned); drop the phantom
  invoiceMonth breakdown from the changelog

Tests: add client error-handling cases and brandImage validation cases;
add scripts/smoke.mjs for end-to-end checks against a local churnkey-api.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
New tools so an agent can edit a flow without resending the whole steps array:

- update_blueprint_offer: change an offer's type + functional config
  (discount/pause/trial/redirect/plan-change), addressable on an offer step,
  a survey choice, or a structured follow-up option via guids.
- edit_survey_structure: add/remove/reorder survey choices and configure a
  choice's follow-up (freeform / structured / freeform-structured / none).
- add_blueprint_step / remove_blueprint_step: add a step at a canonical place
  (server builds the base step) or remove one by stepGuid.
- update_blueprint_step gained survey behavior (randomize, followupRequired,
  minLength) and freeform/confirm config.
- set_segment_enabled and update_segment_filter for segment config.

Draft blueprint edits are draft-only and not confirm-gated (publish is the
live gate; destructive ones carry destructiveHint). Segment edits act on live
config so they require a confirm literal. Adds routing/validation tests and an
observed reversible write round-trip to scripts/smoke.mjs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@hookdump hookdump force-pushed the xdev-2316-crud-for-blueprints branch from 9a67918 to 03da3d7 Compare June 3, 2026 19:38
@hookdump hookdump marked this pull request as ready for review June 4, 2026 14:09
@hookdump

hookdump commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Ready for review. Depends on API PR https://github.com/churnkey/churnkey-api/pull/836 and is documented by https://github.com/churnkey/churnkey-docs/pull/106.

Validation now includes live MCP smoke against the local API: read-only smoke passed 14 checks, mutating smoke passed 24 checks, including disposable segment-flow create/edit/REBATE/archive.

@hookdump hookdump requested a review from jordancardwell June 4, 2026 14:39
hookdump and others added 12 commits June 7, 2026 22:52
…filter

The attribute field only gave a couple of examples and pointed to
list_segments, which is useless when no segment has a populated filter
yet. Enumerate the built-in targeting attributes grouped by value type
and applicable operands so agents can discover what is targetable from
the tool schema alone. Custom org attributes remain accepted (attribute
stays a free string).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Exposes the Data API segment attribute catalog (built-in + custom) so
agents can discover targetable audience-filter attributes and their valid
operands without an already-populated segment. Also corrects the inline
attribute catalog in update_segment_filter to match the evaluator: drops
the non-functional PAYMENT_DECLINE_REASON/TYPE, adds FREEFORM_SENTIMENT,
and fixes PRICE/INVOICE_AMOUNT_DUE units (major currency units, not cents).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
publish_blueprint no longer promises to auto-enable a segment on first
publish (matches the dashboard). Update publish_blueprint and
create_segment_flow descriptions, README, and tests; segments are enabled
by default at creation and set_segment_enabled toggles targeting.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
update_segment_filter no longer requires disable/edit/re-enable for a live
(enabled + published) segment. It edits directly, gated by a new
confirmLiveChange: true acknowledgment that the API enforces for live
segments. Updates schema, descriptions, README, CHANGELOG, and tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
list_segment_attributes and update_segment_filter's inline catalog now
describe only the cancel-flow attribute palette, and note that built-ins
scoped to other products (e.g. CUSTOMER_HAS_PHONE, INVOICE_AMOUNT_DUE) are
rejected. Mirrors the dashboard so MCP-set audiences stay editable there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
list_segment_attributes exposes allowed values for fixed-enum attributes;
update_segment_filter's catalog notes the exact dashboard value is required
(e.g. MONTH not month) and that the set can be provider-specific.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Built-in attributes derive their type automatically; the API strips type on
them. Sending type on a built-in made the dashboard render a duplicate value
input. Update the filterRule type description and CHANGELOG.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
update_blueprint_offer adds an offer to a target that has none when offerType
is provided (seeded with default config), mirroring the dashboard. Update tool
description, README, and CHANGELOG.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
hookdump and others added 4 commits June 8, 2026 03:00
Numbers/dates use GTE/LTE/BETWEEN/NOT_BETWEEN; text/boolean use INCLUDES/
NOT_INCLUDES. Remove GT/LT from the operand enum and update the inline catalog,
operand/value descriptions, and list_segment_attributes guidance. The API
rejects operands outside an attribute's value-type set.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
create_segment_flow already defaults enabled:true (matching the dashboard),
but the schema/description didn't steer agents away from disabling. Strengthen
the segment.enabled field description and the tool description to say: omit (or
pass true) unless the user explicitly wants a paused flow.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Note in the autoOptimize field + update_blueprint_offer description that
adaptive discounts require Churnkey Intelligence and the API rejects them (403)
without access, with upgrade guidance. CHANGELOG updated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@hookdump

Copy link
Copy Markdown
Contributor Author

Superseded by #25 — [MCP reslice 2/2] @churnkey/mcp tool definitions (this tool is included there). Closing in favor of the reslice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant