Add KV metadata MCP tools#117
Open
jm-merchan wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds KV v2 secret metadata support to the MCP Vault server by introducing new tools to read and write metadata (including custom metadata), registering them with the server, and adding a namespace helper and initial tests.
Changes:
- Register new
read_secret_metadataandwrite_secret_metadatatools in tool initialization - Implement KV v2 metadata read/write handlers (with optional namespace override)
- Add tests for reading secret metadata
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/tools/tools.go | Registers new KV metadata tools with the MCP server |
| pkg/tools/kv/read_secret_metadata.go | Implements KV v2 metadata read tool and handler |
| pkg/tools/kv/write_secret_metadata.go | Implements KV v2 metadata write tool and handler |
| pkg/tools/kv/read_secret_metadata_test.go | Adds tests for the metadata read handler |
| pkg/tools/kv/namespace.go | Adds helper to optionally apply Vault namespaces |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+79
to
+85
| m, exists := mounts[mount+"/"] | ||
| if !exists { | ||
| return mcp.NewToolResultError(fmt.Sprintf("mount path '%s' does not exist. Use 'create_mount' with the type kv2 to create the mount.", mount)), nil | ||
| } | ||
| if m.Type != "kv" || m.Options["version"] != "2" { | ||
| return mcp.NewToolResultError(fmt.Sprintf("mount path '%s' is not KV v2. Metadata is only available on KV v2 mounts.", mount)), nil | ||
| } |
Comment on lines
+87
to
+88
| fullPath := fmt.Sprintf("%s/metadata/%s", mount, strings.TrimPrefix(path, "/")) | ||
| secret, err := vault.Logical().Read(fullPath) |
Comment on lines
+102
to
+108
| jsonData, err := json.Marshal(secret.Data) | ||
| if err != nil { | ||
| logger.WithError(err).Error("Failed to marshal secret metadata to JSON") | ||
| return mcp.NewToolResultError(fmt.Sprintf("Error marshaling JSON: %v", err)), nil | ||
| } | ||
|
|
||
| return mcp.NewToolResultText(string(jsonData)), nil |
Comment on lines
+53
to
+54
| func writeSecretMetadataHandler(ctx context.Context, req mcp.CallToolRequest, logger *log.Logger) (*mcp.CallToolResult, error) { | ||
| logger.Debug("Handling write_secret_metadata request") |
Comment on lines
+38
to
+41
| mcp.WithString("custom_metadata", | ||
| mcp.Required(), | ||
| mcp.Description("JSON object with custom metadata key-value pairs to set (e.g. '{\"owner\":\"team-admin\",\"email\":\"admin@example.com\",\"app\":\"my-app\"}')."), | ||
| ), |
Comment on lines
+71
to
+74
| customMetadataRaw, ok := args["custom_metadata"].(string) | ||
| if !ok || strings.TrimSpace(customMetadataRaw) == "" { | ||
| return mcp.NewToolResultError("Missing or invalid 'custom_metadata' parameter (must be a JSON object string)"), nil | ||
| } |
Comment on lines
+127
to
+128
| out, _ := json.MarshalIndent(result, "", " ") | ||
| return mcp.NewToolResultText(string(out)), nil |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds two new MCP tools for KV v2 metadata operations: reading secret metadata and writing custom metadata.
Changes
read_secret_metadatawrite_secret_metadatapkg/tools/tools.goWhy
KV v2 metadata is needed for inventory, age-based analysis, and ownership workflows, but is not available through the current supported MCP toolset.
Validation
go test ./pkg/tools/kv/...go test ./pkg/tools/...