Skip to content

Add KV metadata MCP tools#117

Open
jm-merchan wants to merge 1 commit into
hashicorp:mainfrom
jm-merchan:feat/mcp-kv-metadata-tools
Open

Add KV metadata MCP tools#117
jm-merchan wants to merge 1 commit into
hashicorp:mainfrom
jm-merchan:feat/mcp-kv-metadata-tools

Conversation

@jm-merchan

Copy link
Copy Markdown

Summary

Adds two new MCP tools for KV v2 metadata operations: reading secret metadata and writing custom metadata.

Changes

  • add read_secret_metadata
  • add write_secret_metadata
  • register both tools in pkg/tools/tools.go
  • add focused tests for metadata reads
  • include namespace override support for metadata calls

Why

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/...

Copilot AI review requested due to automatic review settings June 12, 2026 09:55
@jm-merchan jm-merchan requested a review from a team as a code owner June 12, 2026 09:55

Copilot AI left a comment

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.

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_metadata and write_secret_metadata tools 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
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.

2 participants