get-kv
Retrieve specific values from the Consul MCP Server's key-value (KV) store by specifying the desired key, enabling quick access to stored data.
Instructions
Get a value from the KV store
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | No | Key to get from the KV store |
Implementation Reference
- src/tools/consulTools.ts:358-371 (handler)The handler function that executes the get-kv tool logic: retrieves the KV value using consul.kv.get(key), handles errors, formats with formatKVPair, and returns formatted text.async ({ key }) => { try { const data = await consul.kv.get(key); if (!data) { return { content: [{ type: "text", text: `No value found for key: ${key}` }] }; } const kvText = formatKVPair(data); return { content: [{ type: "text", text: kvText }] }; } catch (error) { console.error("Error getting KV:", error); return { content: [{ type: "text", text: `Error getting value for key: ${key}` }] }; } }
- src/tools/consulTools.ts:352-372 (registration)Registration of the get-kv tool via server.tool within registerKVStore function, including description, input schema, and handler.server.tool( "get-kv", "Get a value from the KV store", { key: z.string().default("").describe("Key to get from the KV store"), }, async ({ key }) => { try { const data = await consul.kv.get(key); if (!data) { return { content: [{ type: "text", text: `No value found for key: ${key}` }] }; } const kvText = formatKVPair(data); return { content: [{ type: "text", text: kvText }] }; } catch (error) { console.error("Error getting KV:", error); return { content: [{ type: "text", text: `Error getting value for key: ${key}` }] }; } } );
- src/tools/consulTools.ts:355-356 (schema)Zod input schema for the get-kv tool defining the 'key' parameter.{ key: z.string().default("").describe("Key to get from the KV store"),
- src/utils/formatter.ts:41-60 (helper)Helper function formatKVPair used in the get-kv handler to format the retrieved KV pair, decoding base64-encoded value.export function formatKVPair(pair: KVPair): string { // Decode base64 value if it exists let value = "No value"; if (pair.Value !== null && pair.Value !== undefined) { try { // Consul stores values as base64 encoded strings value = atob(pair.Value); } catch (e) { value = pair.Value; } } return [ `Key: ${pair.Key || "Unknown"}`, `Value: ${value}`, `Flags: ${pair.Flags || 0}`, `Last Modified Index: ${pair.ModifyIndex || "Unknown"}`, "---", ].join("\n"); }
- src/server.ts:41-41 (registration)Invocation of registerKVStore which registers the get-kv tool (among others) on the MCP server.registerKVStore(server, consul);