infracost_cloud_create_tagging_policy
Create tagging policies in Infracost Cloud to validate resource tags in pull requests, ensuring compliance and preventing untagged deployments.
Instructions
Create a new tagging policy in Infracost Cloud for tag validation in pull requests. Requires INFRACOST_SERVICE_TOKEN environment variable.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| orgSlug | No | Organization slug from Infracost Cloud (defaults to INFRACOST_ORG env var) | |
| name | Yes | Name for the tagging policy | |
| message | No | The message to display in the PR comment | |
| prComment | No | Whether to add a comment to the PR | |
| blockPr | No | Whether to block the PR | |
| tags | Yes | Array of tag definitions | |
| filters | No | Filters to limit scope of the policy |
Implementation Reference
- src/tools.ts:585-610 (handler)Handler function in InfracostTools class that validates input, checks configuration, delegates to the API client for creating the tagging policy, and formats the response.
async handleCreateTaggingPolicy(args: z.infer<typeof CreateTaggingPolicySchema>) { if (!this.cloudApiClient) { throw new Error('INFRACOST_SERVICE_TOKEN is not configured for Infracost Cloud API operations'); } const orgSlug = args.orgSlug || this.config.orgSlug; if (!orgSlug) { throw new Error('Organization slug is required. Provide it via orgSlug parameter or set INFRACOST_ORG environment variable'); } const { orgSlug: _, ...policyData } = args; const result = await this.cloudApiClient.createTaggingPolicy(orgSlug, policyData); if (!result.success) { throw new Error(result.error || 'Create tagging policy request failed'); } return { content: [ { type: 'text', text: result.output || 'Tagging policy created successfully', }, ], }; } - src/tools.ts:168-207 (schema)Zod input schema defining parameters for creating a tagging policy, including name, tags, filters, and optional orgSlug.
export const CreateTaggingPolicySchema = z.object({ orgSlug: z.string().optional().describe('Organization slug from Infracost Cloud (defaults to INFRACOST_ORG env var)'), name: z.string().describe('Name for the tagging policy'), message: z.string().optional().describe('The message to display in the PR comment'), prComment: z.boolean().optional().describe('Whether to add a comment to the PR'), blockPr: z.boolean().optional().describe('Whether to block the PR'), tags: z .array( z.object({ key: z.string().describe('Tag key name'), mandatory: z.boolean().describe('Whether the tag is required'), valueType: z.enum(['ANY', 'LIST', 'REGEX']).describe('Value type: ANY (any value), LIST (predefined values), or REGEX (regex pattern)'), allowedValues: z.array(z.string()).optional().describe('List of allowed values (for LIST type)'), allowedRegex: z.string().optional().describe('Regex pattern for allowed values (for REGEX type)'), message: z.string().optional().describe('Optional message to display if the tag is missing/invalid'), }) ) .describe('Array of tag definitions'), filters: z .object({ repos: z.object({ include: z.array(z.string()).optional(), exclude: z.array(z.string()).optional(), }).optional().describe('Repository filters'), projects: z.object({ include: z.array(z.string()).optional(), exclude: z.array(z.string()).optional(), }).optional().describe('Project filters'), baseBranches: z.object({ include: z.array(z.string()).optional(), exclude: z.array(z.string()).optional(), }).optional().describe('Base branch filters'), resources: z.object({ include: z.array(z.string()).optional(), exclude: z.array(z.string()).optional(), }).optional().describe('Resource filters'), }) .optional() .describe('Filters to limit scope of the policy'), }); - src/api.ts:97-135 (helper)Core API client method that performs the HTTP POST request to the Infracost Cloud API endpoint to create a new tagging policy.
async createTaggingPolicy( orgSlug: string, request: CreateTaggingPolicyRequest ): Promise<CommandResult> { try { const response = await fetch( `${INFRACOST_CLOUD_API_BASE}/orgs/${orgSlug}/tagging-policies`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.serviceToken}`, }, body: JSON.stringify({ data: { type: 'tagging-policies', attributes: request } }), } ); if (!response.ok) { const errorText = await response.text(); return { success: false, error: `API request failed with status ${response.status}: ${errorText}`, }; } const data = await response.json(); return { success: true, output: JSON.stringify(data, null, 2), data, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error occurred', }; } } - src/index.ts:739-742 (registration)Registration and dispatching of the tool in the MCP server's CallToolRequestHandler switch statement.
case 'infracost_cloud_create_tagging_policy': { const validatedArgs = CreateTaggingPolicySchema.parse(args); return await tools.handleCreateTaggingPolicy(validatedArgs); }