Skip to content

feat(cli): call the Flags REST API directly for flags create, toggle and target - #148

Merged
filipmyllari merged 2 commits into
mainfrom
flags-rest
Dec 3, 2026
Merged

filipmyllari merged 2 commits into
mainfrom
flags-rest

Conversation

@filipmyllari

@filipmyllari filipmyllari commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Moves the last flags commands off the MCP layer onto the Flags REST API (CGT-69), as part of the CLI's move to the Confidence REST APIs for CLI 2.0 (CGT-76). flags create, toggle and target --add were the last flag commands on the MCP. With flags resolve removed in #146, every flags command now calls REST.

Builds on the shared REST client from #147, which is merged. The warehouse (#149) and recordings (#150) commands are stacked on this PR and move off the MCP there.

What

All three commands go through the shared REST client with the user's token. Routes and edge exposure were checked against epx-flags-admin, konfidens-mcp and edge-config.

  • flags create: POST /v1/flags?flagId=….
    • Uses the MCP's defaults: a boolean enabled schema with disabled and enabled variants, unless --variant is given.
    • --client takes a display name, which is resolved through IAM GET /v1/clients (new features/iam, shared with recordings in feat(cli): call the Flags REST API directly for recordings targeting-key commands #150). A resource name (clients/<id>) is used as is.
    • Creating a flag that already exists now fails with the service's "already exists" error. The MCP quietly returned the existing flag instead.
  • flags toggle --on|--off: resolves the client, reads the flag, then sends PATCH …?updateMask=clients with the client added or removed. If the flag is already in that state, nothing is sent.
  • flags target --add / --from-file: reads the flag, checks every variant exists, then sends POST /v1/segments (allocated at proportion 1.0) followed by POST /v1/flags/{f}/rules.
    • The 10,000 buckets are split by percentage, and any remainder goes to a fallthrough assignment, as in the MCP. --targeting-key defaults to user_id.
    • If the rule fails after the segment was created, the error names the segment that was left behind.
    • Viewing rules (flags target <key>) prints the same output, now read through the same REST client as the writes.
  • Test mock: it now covers flags and iam. Routes are registered at runtime, so they take priority over the existing default flags handlers, and requests to these hosts are still recorded.

Breaking

The output changes from MCP text to JSON or a table, and creating a flag that already exists is now an error. This ships with the rest of the migration in CLI 2.0 (the release PR #143 is on hold).

Testing

  • pnpm qa passes. I broke each guard on purpose to confirm a test catches it: the no-op toggle, the missing-variant check, the fallthrough remainder, the client lookup, and reporting the leftover segment.
  • Checked live on a test account with two throwaway flags, both archived afterwards: create with defaults, and with variants, a client and a description; a duplicate create; toggle on, on again (nothing sent) and off; target --add enabled:80,disabled:20, which produced buckets 0–8000 and 8000–10000; viewing the rules; and an unknown variant.

🤖 Generated with Claude Code

…and target

flags create, toggle and target --add now call the regional Flags and IAM
APIs with the user's token instead of the MCP server, on the shared REST
resource client. Output is JSON or a table instead of MCP text, and
dry-run prints the exact request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines +16 to +37
const body = {
schema: { schema: { enabled: { boolSchema: {} } } },
variants:
variants.length > 0
? variants.map((variant) => ({ name: variantResourceName(key, variant), value: {} }))
: [
{ name: variantResourceName(key, 'disabled'), value: { enabled: false } },
{ name: variantResourceName(key, 'enabled'), value: { enabled: true } },
],
...(description ? { description } : {}),
};
const create = createOperation(FLAGS, body, { id: key });
if (client === undefined) return create;

const result = await createFlag(token, flagKey, {
description,
variants: variants.length > 0 ? variants : undefined,
client,
return {
...create,
local: { client },
run: async (token) => {
const resolved = await resolveClient(token, client);
if (!resolved.ok) return resolved;
return createOperation(FLAGS, { ...body, clients: [resolved.data] }, { id: key }).run(
token,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we decided to inline the network call builders in the command handler. It isn't blocking just seems weird that some of the commands now inline it and some don't 😄

(I'd prefer to extract all the networking from handlers but that's not mandatory)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it was inconsistent. Fixed in 5854f5e: create, toggle and target now build their requests in features/flags/api.ts (createFlagOperation, toggleFlagOperation, addRuleOperation, getFlagOperation), next to list/get/update/archive. The handlers only parse input and print output now.

Comment on lines +18 to +19
const BUCKET_COUNT = 10_000;
const DEFAULT_TARGETING_KEY = 'user_id';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make this configurable btw?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it fixed. The bucket count only sets how finely percentages are rounded: 10,000 buckets is 0.01% steps, the same as the MCP used. Users give percentages, so a different count wouldn't change what they can express in practice, and it's one more thing to get wrong. If someone needs finer splits we can add it later.

…ommand handlers

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

@bespoyasov bespoyasov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

P.S. Let's also update the code structure skill to ensure the agents keep network builders separated from the handlers later on?

@filipmyllari
filipmyllari merged commit 5fe7f5c into main Dec 3, 2026
6 checks passed
@filipmyllari
filipmyllari deleted the flags-rest branch December 3, 2026 13:57
@github-actions github-actions Bot mentioned this pull request Dec 3, 2026
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.

2 participants