Skip to main content
Glama
growthbook

GrowthBook MCP Server

Official
by growthbook

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
NameRequiredDescriptionDefault
conditionNoApplied to everyone by default. Write conditions in MongoDB-style query syntax.
descriptionNo
environmentsYes
featureIdYesThe ID of the feature to create the rule on
fileExtensionYesThe extension of the current file. If it's unclear, ask the user.
valueYesThe type of the value should match the feature type

Implementation Reference

  • 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}`); } }
  • 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"), },
  • 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}`); } } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/growthbook/growthbook-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server