create_force_rule
Set a feature to a specific value for targeted environments using conditions. Use this tool to enforce feature behavior in specific scenarios, ideal for controlled rollouts or conditional overrides.
Instructions
Create a new force rule on an existing feature. If the existing feature isn't apparent, create a new feature using create_feature_flag first. A force rule sets a feature to a specific value for a specific environment based on a condition. For A/B tests and experiments, use create_experiment instead.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| condition | No | Applied to everyone by default. Write conditions in MongoDB-style query syntax. | |
| description | No | ||
| environments | Yes | ||
| featureId | Yes | The ID of the feature to create the rule on | |
| fileExtension | Yes | The extension of the current file. If it's unclear, ask the user. | |
| value | Yes | The type of the value should match the feature type |
Implementation Reference
- src/tools/features.ts:155-221 (handler)The handler function that implements the create_force_rule tool. It fetches default environments, constructs a payload with force rules for each environment, sends a POST request to update the feature flag, handles the response, and returns formatted content with GrowthBook link and code stub.async ({ featureId, description, condition, value, fileExtension }) => { try { // Fetch feature defaults first and surface to user const defaults = await getDefaults(apiKey, baseApiUrl); const defaultEnvironments = defaults.environments; const payload = { // Loop through the environments and create a rule for each one keyed by environment name environments: defaultEnvironments.reduce((acc, env) => { acc[env] = { enabled: false, rules: [ { type: "force", description, condition, value, }, ], }; return acc; }, {} as Record<string, { enabled: boolean; rules: Array<any> }>), }; const res = await fetch(`${baseApiUrl}/api/v1/features/${featureId}`, { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify(payload), }); await handleResNotOk(res); const data = await res.json(); const linkToGrowthBook = generateLinkToGrowthBook( appOrigin, "features", featureId ); const { docs, language, stub } = getDocsMetadata(fileExtension); const text = `This is the API response: ${JSON.stringify(data, null, 2)} Additionally, here is a template of what to show to the user: **✅ Your feature flag \`my-flag-name\` is ready!.** [View it in GrowthBook](${linkToGrowthBook}) **How to use it in your code:** (To the LLM agent. Use the text below to properly add the feature flag to the codebase. Also, surface the relevant docs/implemenation to the user) ${stub} **Want to learn more?** See the [GrowthBook ${language} docs](${docs}) `; return { content: [{ type: "text", text }], }; } catch (error) { throw new Error(`Error creating force rule: ${error}`); } }
- src/tools/features.ts:138-151 (schema)Input schema for the create_force_rule tool, defining parameters like featureId, description, fileExtension, condition, and value using Zod.{ featureId: featureFlagSchema.id, description: featureFlagSchema.description.optional().default(""), fileExtension: featureFlagSchema.fileExtension, condition: z .string() .describe( "Applied to everyone by default. Write conditions in MongoDB-style query syntax." ) .optional(), value: z .string() .describe("The type of the value should match the feature type"), },
- src/tools/features.ts:132-222 (registration)Registration of the create_force_rule tool using server.tool, including description, schema, hints, and handler reference./** * Tool: create_force_rule */ server.tool( "create_force_rule", "Create a new force rule on an existing feature. If the existing feature isn't apparent, create a new feature using create_feature_flag first. A force rule sets a feature to a specific value based on a condition. For A/B tests and experiments, use create_experiment instead.", { featureId: featureFlagSchema.id, description: featureFlagSchema.description.optional().default(""), fileExtension: featureFlagSchema.fileExtension, condition: z .string() .describe( "Applied to everyone by default. Write conditions in MongoDB-style query syntax." ) .optional(), value: z .string() .describe("The type of the value should match the feature type"), }, { readOnlyHint: false, }, async ({ featureId, description, condition, value, fileExtension }) => { try { // Fetch feature defaults first and surface to user const defaults = await getDefaults(apiKey, baseApiUrl); const defaultEnvironments = defaults.environments; const payload = { // Loop through the environments and create a rule for each one keyed by environment name environments: defaultEnvironments.reduce((acc, env) => { acc[env] = { enabled: false, rules: [ { type: "force", description, condition, value, }, ], }; return acc; }, {} as Record<string, { enabled: boolean; rules: Array<any> }>), }; const res = await fetch(`${baseApiUrl}/api/v1/features/${featureId}`, { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify(payload), }); await handleResNotOk(res); const data = await res.json(); const linkToGrowthBook = generateLinkToGrowthBook( appOrigin, "features", featureId ); const { docs, language, stub } = getDocsMetadata(fileExtension); const text = `This is the API response: ${JSON.stringify(data, null, 2)} Additionally, here is a template of what to show to the user: **✅ Your feature flag \`my-flag-name\` is ready!.** [View it in GrowthBook](${linkToGrowthBook}) **How to use it in your code:** (To the LLM agent. Use the text below to properly add the feature flag to the codebase. Also, surface the relevant docs/implemenation to the user) ${stub} **Want to learn more?** See the [GrowthBook ${language} docs](${docs}) `; return { content: [{ type: "text", text }], }; } catch (error) { throw new Error(`Error creating force rule: ${error}`); } } );