Skip to main content
Glama
severity1

terraform-cloud-mcp

create_state_version

Create a new state version and set it as current for a Terraform Cloud workspace, enabling migration from Terraform Community edition to HCP Terraform.

Instructions

Create a state version in a workspace.

Creates a new state version and sets it as the current state version for the given workspace. The workspace must be locked by the user creating a state version. This is most useful for migrating existing state from Terraform Community edition into a new HCP Terraform workspace.

API endpoint: POST /workspaces/:workspace_id/state-versions

Args: workspace_id: The ID of the workspace (format: "ws-xxxxxxxx") serial: The serial number of this state instance md5: An MD5 hash of the raw state version params: Additional state version parameters (optional): - state: Base64 encoded raw state file - lineage: Lineage of the state version - json_state: Base64 encoded JSON state - json_state_outputs: Base64 encoded JSON state outputs - run_id: The ID of the run to associate with the state version

Returns: The created state version data including download URLs and status information

See: docs/tools/state_versions.md for reference documentation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
workspace_idYes
serialYes
md5Yes
paramsNo

Implementation Reference

  • The core handler function that executes the logic for creating a state version by constructing the API payload and making a POST request to the Terraform Cloud API.
    async def create_state_version(
        workspace_id: str,
        serial: int,
        md5: str,
        params: Optional[StateVersionParams] = None,
    ) -> APIResponse:
        """Create a state version in a workspace.
    
        Creates a new state version and sets it as the current state version for the
        given workspace. The workspace must be locked by the user creating a state version.
        This is most useful for migrating existing state from Terraform Community edition
        into a new HCP Terraform workspace.
    
        API endpoint: POST /workspaces/:workspace_id/state-versions
    
        Args:
            workspace_id: The ID of the workspace (format: "ws-xxxxxxxx")
            serial: The serial number of this state instance
            md5: An MD5 hash of the raw state version
            params: Additional state version parameters (optional):
                - state: Base64 encoded raw state file
                - lineage: Lineage of the state version
                - json_state: Base64 encoded JSON state
                - json_state_outputs: Base64 encoded JSON state outputs
                - run_id: The ID of the run to associate with the state version
    
        Returns:
            The created state version data including download URLs and status information
    
        See:
            docs/tools/state_versions.md for reference documentation
        """
        # Extract parameters from params object
        param_dict = params.model_dump(exclude_none=True, by_alias=False) if params else {}
    
        # Add required parameters
        param_dict["serial"] = serial
        param_dict["md5"] = md5
    
        # Create request using Pydantic model
        request_params = StateVersionCreateRequest(workspace_id=workspace_id, **param_dict)
    
        # Create API payload using utility function
        payload = create_api_payload(
            resource_type="state-versions",
            model=request_params,
            exclude_fields={"workspace_id"},
        )
    
        # Add relationship if run_id is provided
        if param_dict.get("run_id"):
            payload = add_relationship(
                payload=payload,
                relation_name="run",
                resource_type="runs",
                resource_id=param_dict["run_id"],
            )
    
        # Make API request
        return await api_request(
            f"workspaces/{request_params.workspace_id}/state-versions",
            method="POST",
            data=payload,
        )
  • Pydantic model providing input validation and structure for the state version creation request, used directly in the handler.
    class StateVersionCreateRequest(APIRequest):
        """Request model for creating a state version.
    
        Validates and structures the request according to the Terraform Cloud API
        requirements for creating state versions.
    
        Reference: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/state-versions#create-a-state-version
    
        See:
            docs/models/state_versions.md for reference
        """
    
        workspace_id: str = Field(
            ...,
            description="The ID of the workspace to create a state version in",
            pattern=r"^ws-[a-zA-Z0-9]{16}$",  # Standard workspace ID pattern
        )
        serial: int = Field(
            ...,
            description="The serial of the state version",
            ge=0,
        )
        md5: str = Field(
            ...,
            description="An MD5 hash of the raw state version",
            pattern=r"^[a-fA-F0-9]{32}$",  # MD5 hash pattern
        )
        state: Optional[str] = Field(
            None,
            description="Base64 encoded raw state file",
        )
        lineage: Optional[str] = Field(
            None,
            description="Lineage of the state version",
        )
        json_state: Optional[str] = Field(
            None,
            alias="json-state",
            description='Base64 encoded json state, as expressed by "terraform show -json"',
        )
        json_state_outputs: Optional[str] = Field(
            None,
            alias="json-state-outputs",
            description='Base64 encoded output values as represented by "terraform show -json"',
        )
        run_id: Optional[str] = Field(
            None,
            description="The ID of the run to associate with the state version",
            pattern=r"^run-[a-zA-Z0-9]{16}$",  # Standard run ID pattern
        )
  • Pydantic model for optional parameters passed to the create_state_version handler, excluding routing fields like workspace_id.
    class StateVersionParams(APIRequest):
        """Parameters for state version operations without routing fields.
    
        This model provides all optional parameters for creating state versions,
        reusing field definitions from StateVersionCreateRequest. It separates configuration
        parameters from routing information.
    
        Reference: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/state-versions
    
        See:
            docs/models/state_versions.md for reference
        """
    
        serial: Optional[int] = Field(
            None,
            description="The serial of the state version",
            ge=0,
        )
        md5: Optional[str] = Field(
            None,
            description="An MD5 hash of the raw state version",
            pattern=r"^[a-fA-F0-9]{32}$",  # MD5 hash pattern
        )
        state: Optional[str] = Field(
            None,
            description="Base64 encoded raw state file",
        )
        lineage: Optional[str] = Field(
            None,
            description="Lineage of the state version",
        )
        json_state: Optional[str] = Field(
            None,
            alias="json-state",
            description='Base64 encoded json state, as expressed by "terraform show -json"',
        )
        json_state_outputs: Optional[str] = Field(
            None,
            alias="json-state-outputs",
            description='Base64 encoded output values as represented by "terraform show -json"',
        )
        run_id: Optional[str] = Field(
            None,
            description="The ID of the run to associate with the state version",
            pattern=r"^run-[a-zA-Z0-9]{16}$",  # Standard run ID pattern
        )
  • Registers the create_state_version handler as an MCP tool with write permissions configuration.
    mcp.tool(**write_tool_config)(state_versions.create_state_version)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations only indicate this is not read-only (readOnlyHint: false). The description adds valuable behavioral context beyond this: it specifies a prerequisite (workspace must be locked), describes the effect (sets as current state version), mentions the API endpoint, and references external documentation. No contradiction with annotations exists.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (purpose, prerequisites, usage context, API endpoint, args, returns, see also). While comprehensive, some sentences could be more concise (e.g., the migration context sentence is somewhat lengthy). Overall, it's appropriately sized for a complex tool.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with no output schema and 0% schema description coverage, the description provides excellent coverage: purpose, prerequisites, usage context, parameter details, return value description, and documentation references. The main gap is the lack of explicit error conditions or rate limit information, but it's otherwise very complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description carries the full burden of explaining parameters. It provides detailed explanations for all 4 parameters: workspace_id format, serial purpose, md5 purpose, and a comprehensive breakdown of optional params with their meanings and formats. This fully compensates for the schema gap.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Create a state version'), target resource ('in a workspace'), and key effect ('sets it as the current state version'). It distinguishes this from sibling tools like 'get_state_version' or 'list_state_versions' by emphasizing creation rather than retrieval.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit context for when to use this tool ('most useful for migrating existing state from Terraform Community edition into a new HCP Terraform workspace') and mentions a prerequisite ('workspace must be locked by the user'). However, it doesn't explicitly state when NOT to use it or name specific alternative tools for similar operations.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/severity1/terraform-cloud-mcp'

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