-
Notifications
You must be signed in to change notification settings - Fork 1
Add a download_manifests tool to our agent harness mcp server #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JohnBlackwell
wants to merge
2
commits into
main
Choose a base branch
from
john/prod-4782-add-a-download_manifests-tool-to-our-agent-harness-mcp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| package tool | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/mark3labs/mcp-go/server" | ||
|
|
||
| "github.com/pluralsh/deployment-operator/pkg/agentrun-harness/environment" | ||
| console "github.com/pluralsh/deployment-operator/pkg/client" | ||
| "github.com/pluralsh/deployment-operator/pkg/manifests" | ||
| ) | ||
|
|
||
| // manifestsSubdir is the top-level directory (relative to the agent harness | ||
| // working directory) under which manifests for individual services are | ||
| // extracted. The actual files live in | ||
| // "<workingDir>/<manifestsSubdir>/<handle>-<service>/". | ||
| const manifestsSubdir = "manifests" | ||
|
|
||
| // safeNamePattern matches characters that are safe to use in a directory name | ||
| // derived from a cluster handle / service name. | ||
| var safeNamePattern = regexp.MustCompile(`[^a-zA-Z0-9._-]+`) | ||
|
|
||
| func (in *DownloadManifests) Install(s *server.MCPServer) { | ||
| s.AddTool( | ||
| mcp.NewTool( | ||
| in.id.String(), | ||
| mcp.WithDescription(in.description), | ||
| mcp.WithString("cluster", | ||
| mcp.Required(), | ||
| mcp.Description("Handle of the Plural cluster the service is deployed to (e.g. 'mgmt' or 'prod-eu-1')"), | ||
| ), | ||
| mcp.WithString("service", | ||
| mcp.Required(), | ||
| mcp.Description("Name of the Plural service whose rendered manifests should be downloaded"), | ||
| ), | ||
| ), | ||
| in.handler, | ||
| ) | ||
| } | ||
|
|
||
| func (in *DownloadManifests) handler(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| if err := in.fromRequest(request); err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("could not handle download manifests request: %v", err)), nil | ||
| } | ||
|
|
||
| svc, err := in.client.GetServiceDeploymentByHandle(in.ClusterHandle, in.ServiceName) | ||
| if err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to look up service %q on cluster %q: %v", in.ServiceName, in.ClusterHandle, err)), nil | ||
| } | ||
| if svc.Tarball == nil || *svc.Tarball == "" { | ||
| return mcp.NewToolResultError(fmt.Sprintf("service %q on cluster %q does not yet have a rendered tarball available", in.ServiceName, in.ClusterHandle)), nil | ||
| } | ||
|
|
||
| _, token := in.client.GetCredentials() | ||
| if token == "" { | ||
| return mcp.NewToolResultError("Plural Console credentials are not configured for the MCP server"), nil | ||
| } | ||
|
|
||
| baseDir, err := resolveManifestsBaseDir() | ||
| if err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to resolve manifests base directory: %v", err)), nil | ||
| } | ||
|
|
||
| targetDir := filepath.Join(baseDir, manifestsSubdir, sanitizeSegment(in.ClusterHandle)+"-"+sanitizeSegment(in.ServiceName)) | ||
|
|
||
| if err := os.RemoveAll(targetDir); err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to clear target directory %q: %v", targetDir, err)), nil | ||
| } | ||
| if err := os.MkdirAll(targetDir, 0o755); err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to create target directory %q: %v", targetDir, err)), nil | ||
| } | ||
|
|
||
| reader, _, err := manifests.GetReader(*svc.Tarball, token) | ||
| if err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to download service tarball: %v", err)), nil | ||
| } | ||
| defer reader.Close() | ||
|
|
||
| if err := manifests.Untar(targetDir, reader); err != nil { | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to extract service tarball into %q: %v", targetDir, err)), nil | ||
| } | ||
|
|
||
| return mcp.NewToolResultJSON(struct { | ||
| Success bool `json:"success"` | ||
| Message string `json:"message"` | ||
| Cluster string `json:"cluster"` | ||
| Service string `json:"service"` | ||
| ServiceID string `json:"serviceId"` | ||
| Directory string `json:"directory"` | ||
| Instructions string `json:"instructions"` | ||
| }{ | ||
| Success: true, | ||
| Message: fmt.Sprintf("downloaded manifests for service %q on cluster %q", in.ServiceName, in.ClusterHandle), | ||
| Cluster: in.ClusterHandle, | ||
| Service: in.ServiceName, | ||
| ServiceID: svc.ID, | ||
| Directory: targetDir, | ||
| Instructions: fmt.Sprintf( | ||
| "The rendered Kubernetes manifests for the service have been written to %q. "+ | ||
| "Use Read/Glob/Grep against this directory to inspect the actual resources Plural is applying "+ | ||
| "(including resources rendered from external Helm charts) instead of guessing via web searches.", | ||
| targetDir, | ||
| ), | ||
| }) | ||
| } | ||
|
|
||
| func (in *DownloadManifests) fromRequest(request mcp.CallToolRequest) (err error) { | ||
| if in.ClusterHandle, err = request.RequireString("cluster"); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if in.ServiceName, err = request.RequireString("service"); err != nil { | ||
| return | ||
| } | ||
|
|
||
| in.ClusterHandle = strings.TrimSpace(in.ClusterHandle) | ||
| in.ServiceName = strings.TrimSpace(in.ServiceName) | ||
|
|
||
| if in.ClusterHandle == "" { | ||
| return fmt.Errorf("cluster handle must not be empty") | ||
| } | ||
| if in.ServiceName == "" { | ||
| return fmt.Errorf("service name must not be empty") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // resolveManifestsBaseDir picks the directory under which the | ||
| // "manifests/<handle>-<service>" tree should live. We prefer the parent of | ||
| // the cloned repository (the agent harness working directory) so that | ||
| // downloaded manifests are kept side-by-side with the repo without polluting | ||
| // the git working tree. | ||
| func resolveManifestsBaseDir() (string, error) { | ||
| if cfg, err := environment.Load(); err == nil && cfg != nil && cfg.Dir != "" { | ||
| return filepath.Dir(cfg.Dir), nil | ||
| } | ||
|
|
||
| return os.Getwd() | ||
| } | ||
|
|
||
| // sanitizeSegment normalises a value coming from outside the harness into | ||
| // something safe to use as a directory name segment. | ||
| func sanitizeSegment(s string) string { | ||
| s = strings.TrimSpace(s) | ||
| s = safeNamePattern.ReplaceAllString(s, "-") | ||
| s = strings.Trim(s, "-") | ||
| if s == "" { | ||
| return "service" | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func NewDownloadManifests(client console.Client, agentRunID string) Tool { | ||
| return &DownloadManifests{ | ||
| ConsoleTool: ConsoleTool{ | ||
| id: DownloadManifestsTool, | ||
| description: "Downloads the fully rendered Kubernetes manifests for a Plural service " + | ||
| "and writes them to a dedicated '<handle>-<name>' subdirectory under '" + manifestsSubdir + "/' " + | ||
| "next to the cloned repository. Use this whenever you need to understand what Plural " + | ||
| "is actually applying for a service - including resources rendered from external Helm " + | ||
| "charts or the Plural gitops layout - instead of guessing via web searches. After it " + | ||
| "returns, inspect the listed directory with Read/Glob/Grep.", | ||
| client: client, | ||
| agentRunID: agentRunID, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fromRequestwritesin.ClusterHandleandin.ServiceNamedirectly onto the*DownloadManifestsreceiver, which is a single instance shared across all concurrent MCP tool calls. Two simultaneous invocations can interleave: Request A setsClusterHandle = "prod", Request B overwrites it withClusterHandle = "dev", then Request A proceeds to callGetServiceDeploymentByHandle("dev", ...)— fetching and extracting the wrong service's manifests, and potentially writing them to the wrongtargetDir.The fix is to keep
clusterHandleandserviceNameas function-local variables inhandlerinstead of struct fields.