Skip to content

Sdk migration - refactor tools and client into packages and add list_terraform_orgs tool - #493

Open
MulyaP wants to merge 5 commits into
hashicorp:mainfrom
MulyaP:sdk-migration
Open

Sdk migration - refactor tools and client into packages and add list_terraform_orgs tool#493
MulyaP wants to merge 5 commits into
hashicorp:mainfrom
MulyaP:sdk-migration

Conversation

@MulyaP

@MulyaP MulyaP commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

This PR adds list_terraform_org tool using the official go-sdk for MCP and refactors the official sdk code into packages like in mark2labs/mcp-go code.

PCI review checklist

  • I have documented a clear reason for, and description of, the change I am making.

  • If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

  • If applicable, I've documented the impact of any changes to security controls.

    Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.

@MulyaP
MulyaP requested a review from a team as a code owner August 24, 2026 13:57
Comment on lines +33 to +34
trueVal := true
falseVal := false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is gross and no doubt we're gonna need to do it in a bunch of places. Instead, we could make a helper func for this like:

func ptr[T any](v T) *T {
    return &v
}

@MulyaP MulyaP Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agree. Made a utils folder and utils.go file and added the helper function there.

"github.com/hashicorp/terraform-mcp-server/pkg/mcp-official/client"
)

type OrganizationSummary struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With the new code let's be consistent and put the godoc comments above every struct definition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed and added

PageSize int `json:"pageSize,omitempty" jsonschema:"Results per page for pagination (min 1, max 100)"`
}

func ListTerraformOrganizationsTool() *mcp.Tool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Something that's missing here is injecting the logger dependency.

It gets passed in at the RegisterTools() call here: https://github.com/hashicorp/terraform-mcp-server/blob/main/pkg/mcp-official/tools.go#L9

But then it just gets swallowed, and the code here defaults to using log from the import.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I looked into this and simply passing logger into the handler func argument in mcp.AddTool will not work because of how of its designed. One solution that works here is to make the handler a closure function which looks something like this:

func ListTerraformOrganizationsFunc(logger *log.Logger) func(ctx context.Context, req *mcp.CallToolRequest, input ListOrganizationsArguments) (*mcp.CallToolResult, *OrganizationSummaryList, error) {
	return func(ctx context.Context, req *mcp.CallToolRequest, input ListOrganizationsArguments) (*mcp.CallToolResult, *OrganizationSummaryList, error) {
		log.Info("ListTerraformOrganizations for official mcp go-sdk called.. dkfhgbdf")
	}
}

In my opinion, this makes it unnecessarily convoluted and importing log seems like a good alternative. I will try to find other methods with which we can achieve this in a simpler way.

Comment thread pkg/mcp-official/tools/tfe/workspace.go Outdated

func ListWorkspacesFunc(ctx context.Context, request *mcp.CallToolRequest, input ListWorkspacesArguments) (*mcp.CallToolResult, *WorkspaceSummaryList, error) {
log.Info("ListWorkspaces for official mcp go-dk called..")
log.Info("ListWorkspaces for official mcp go-sdk called..")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This log message isn't very useful, there is already a tool call logger middleware that's going to get ported across: https://github.com/Marin2409/terraform-mcp-server/blob/main/pkg/client/tool_logging_middleware.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, I should remove it. It was just there for me to know which tool endpoint was called (official or mark3labs) when testing it.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants