Skip to content

Commit 43774a5

Browse files
authored
Fix get_apply_logs tool hanging when run is in pending state (#468)
1 parent 522bf55 commit 43774a5

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ FEATURES
88
* [New Tool] `get_project` Fetches detailed information about a Terraform project by its ID. Requires `project_id`.
99
* [New Tool] `create_team` Creates a new team in a Terraform Cloud/Enterprise organization. Requires `terraform_org_name` and `team_name`; optional `visibility` ("secret" or "organization"). [427](https://github.com/hashicorp/terraform-mcp-server/pull/427)
1010

11+
FIXES
12+
13+
* `get_apply_logs` now checks the apply status before attempting to stream logs. If the apply is not yet in a terminal state (`finished`, `errored`, `canceled`), the tool returns an informative message instead of timing out.
14+
1115
# 1.2.0
1216

1317
FEATURES

‎pkg/tools/tfe/get_apply_logs.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ package tools
55

66
import (
77
"context"
8+
"fmt"
89
"io"
10+
"slices"
911

12+
tfe "github.com/hashicorp/go-tfe"
1013
"github.com/hashicorp/terraform-mcp-server/pkg/client"
1114
"github.com/mark3labs/mcp-go/mcp"
1215
"github.com/mark3labs/mcp-go/server"
@@ -43,6 +46,24 @@ func getApplyLogsHandler(ctx context.Context, request mcp.CallToolRequest, logge
4346
return ToolError(logger, "failed to get Terraform client", err)
4447
}
4548

49+
apply, err := tfeClient.Applies.Read(ctx, applyID)
50+
if err != nil {
51+
return ToolErrorf(logger, "apply not found: %s", applyID)
52+
}
53+
54+
terminalStatuses := []tfe.ApplyStatus{
55+
tfe.ApplyErrored,
56+
tfe.ApplyFinished,
57+
tfe.ApplyCanceled,
58+
}
59+
60+
if !slices.Contains(terminalStatuses, apply.Status) {
61+
return mcp.NewToolResultText(fmt.Sprintf(
62+
"Apply %s is currently in status %q. Wait for the status to change to a terminal state (finished, errored, canceled) before calling again.",
63+
applyID, apply.Status,
64+
)), nil
65+
}
66+
4667
logReader, err := tfeClient.Applies.Logs(ctx, applyID)
4768
if err != nil {
4869
return ToolErrorf(logger, "failed to retrieve apply logs: %s", applyID)

0 commit comments

Comments
 (0)