Skip to main content
Glama

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
NameRequiredDescriptionDefault
keyNoKey to get from the KV store

Implementation Reference

  • 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}` }] };
      }
    }
  • 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}` }] };
        }
      }
    );
  • Zod input schema for the get-kv tool defining the 'key' parameter.
    {
      key: z.string().default("").describe("Key to get from the KV store"),
  • 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);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kocierik/consul-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server