Skip to content

Conversation

@vravind1
Copy link
Member

Fixes #228

  • Adds support for reading TFE tokens from ~/.terraform.d/credentials.tfrc.json
  • Users who have run terraform login can now use the MCP server without setting TFE_TOKEN environment
    variable
  • Maintains backward compatibility - env var still takes precedence when set

New credential resolution chain:
HTTP Header → Environment Variable → credentials.tfrc.json → Error

Changes

File Description
pkg/client/credentials.go New file - reads and parses credentials.tfrc.json
pkg/client/credentials_test.go Unit tests for credential reading
pkg/client/tfe_client.go Added fallback to credentials file (3 lines)

Test plan

  • Unit tests pass (go test ./pkg/client/...)
  • All existing tests pass (go test ./...)
  • Manual testing with Claude Desktop - successfully queried HCP Terraform workspaces using token from
    credentials file

Read TFE token from ~/.terraform.d/credentials.tfrc.json as fallback
when TFE_TOKEN environment variable is not set. This allows users who
have run `terraform login` to use the MCP server without additional
configuration.

Priority chain: HTTP Header → Environment Variable → credentials.tfrc.json

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@vravind1 vravind1 requested a review from a team as a code owner January 26, 2026 03:31
@github-actions
Copy link

Changelog Warning

Please add a changelog entry to CHANGELOG.md for this change. If you believe this change does not need a changelog entry, please add the 'no-changelog-needed' label.

@hashicorp-cla-app
Copy link

hashicorp-cla-app bot commented Jan 26, 2026

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


1 out of 2 committers have signed the CLA.

  • vravind1
  • Vishnu Ravindra

Vishnu Ravindra seems not to be a GitHub user.
You need a GitHub account to be able to sign the CLA.
If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

Copy link
Contributor

@jrhouston jrhouston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Vishnu 😄

Looks good, just a couple of test/error handling comments.

Comment on lines 20 to 23
{"standard https", "https://app.terraform.io", "app.terraform.io"},
{"with port", "https://tfe.example.com:8443", "tfe.example.com"},
{"with path", "https://app.terraform.io/api/v2", "app.terraform.io"},
{"http scheme", "http://localhost:8080", "localhost"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH – since extractHostname is a private function that just adds some sugar on top of url.Parse, I don't see much value in these tests. They are just re-testing the url.Parse() function which already has a comprehensive suite of tests that look exactly like this. The logic we've introduced here is returning "" for empty input or invalid URL.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've trimmed the test cases down to only the edge cases


// ReadCredentialsFile reads the Terraform CLI credentials file and returns
// the token for the specified hostname. Returns empty string if not found.
func ReadCredentialsFile(hostname string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but the function fails silently, and there's a few real failure modes we'd probably want to at least warn the user about in the logs:

  1. The path / file simply doesn't exist
  2. We tried to read the file but don't have permissions to do so.
  3. We read the file successfully but couldn't parse it.
  4. We read and parse the file successfully but the hostname we've configured doesn't exist in the file.

Right now if I try to use the credentials file but misconfigure it in some way, the server boots up OK but now I have a task of trying to figure out why it doesn't like my credentials file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alternate way to pass TFE/TFC variables to MCP service

2 participants