download_state_file
Retrieve raw or JSON-formatted state file content for a specific Terraform state version using the state version ID. Supports integration with Terraform Cloud API for infrastructure management.
Instructions
Download the state file content.
Retrieves the raw state file or JSON formatted state file for a specific state version.
API endpoint: Uses the hosted URLs from GET /state-versions/:state_version_id
Args: state_version_id: The ID of the state version (format: "sv-xxxxxxxx") json_format: Whether to download the JSON formatted state (default: False)
Returns: The raw state file content or JSON formatted state content
See: docs/tools/state_versions.md for reference documentation
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| json_format | No | ||
| state_version_id | Yes |
Implementation Reference
- The main handler function for the 'download_state_file' tool. It validates the state_version_id, retrieves the state version details to get the appropriate download URL (hosted-state-download-url or hosted-json-state-download-url based on json_format), handles cases where URL is unavailable, and fetches the state file content using api_request.@handle_api_errors async def download_state_file( state_version_id: str, json_format: bool = False ) -> APIResponse: """Download the state file content. Retrieves the raw state file or JSON formatted state file for a specific state version. API endpoint: Uses the hosted URLs from GET /state-versions/:state_version_id Args: state_version_id: The ID of the state version (format: "sv-xxxxxxxx") json_format: Whether to download the JSON formatted state (default: False) Returns: The raw state file content or JSON formatted state content See: docs/tools/state_versions.md for reference documentation """ # Validate parameters params = StateVersionRequest(state_version_id=state_version_id) # First get state version details to get the download URL state_version = await api_request(f"state-versions/{params.state_version_id}") # Determine which URL to use based on format request url_attr = ( "hosted-json-state-download-url" if json_format else "hosted-state-download-url" ) download_url = state_version.get("data", {}).get("attributes", {}).get(url_attr) # Check if URL is available if not download_url: if json_format: return { "error": "JSON state download URL not available. This may be because the state was not created with Terraform 1.3+" } else: return {"error": "State download URL not available for this state version"} # Use the enhanced api_request to fetch state from the external URL return await api_request(download_url, external_url=True, accept_text=True)
- Pydantic model used for input validation of the state_version_id parameter in the download_state_file tool.class StateVersionRequest(APIRequest): """Request model for retrieving a state version. Used to validate the state version ID parameter for API requests. Reference: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/state-versions See: docs/models/state_versions.md for reference """ state_version_id: str = Field( ..., description="The ID of the state version to retrieve", pattern=r"^sv-[a-zA-Z0-9]{16}$", # Standard state version ID pattern )
- terraform_cloud_mcp/server.py:117-117 (registration)Registration of the 'download_state_file' tool in the MCP server using the mcp.tool() decorator.mcp.tool()(state_versions.download_state_file)