Skip to main content
Glama

create-prepared-query

Define prepared queries to efficiently retrieve data from Consul MCP Server, specifying services, nearest nodes, and datacenters for targeted results.

Instructions

Create a new prepared query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
datacentersNoDatacenters to query
nameNoName of the prepared query
nearestNNoNumber of nearest nodes to return
serviceNoService to query

Implementation Reference

  • The asynchronous handler function that implements the core logic of the 'create-prepared-query' tool by calling consul.query.create to create a prepared query in Consul and handling the response or errors.
    async ({ name, service, nearestN, datacenters }) => {
      try {
        // @ts-ignore - The Consul type definitions are incomplete
        const query = await consul.query.create({
          Name: name,
          Service: {
            Service: service,
            Failover: {
              NearestN: nearestN || 3,
              Datacenters: datacenters || [],
            },
          },
        }) as PreparedQuery;
        return { content: [{ type: "text", text: `Created prepared query: ${query.ID}` }] };
      } catch (error) {
        console.error("Error creating prepared query:", error);
        return { content: [{ type: "text", text: `Error creating prepared query: ${name}` }] };
      }
    }
  • Zod schema defining the input parameters for the 'create-prepared-query' tool, including name, service, nearestN, and datacenters.
    {
      name: z.string().default("").describe("Name of the prepared query"),
      service: z.string().default("").describe("Service to query"),
      nearestN: z.number().optional().describe("Number of nearest nodes to return"),
      datacenters: z.array(z.string()).optional().describe("Datacenters to query"),
    },
  • The server.tool call that registers the 'create-prepared-query' tool with its description, input schema, and handler function.
    server.tool(
      "create-prepared-query",
      "Create a new prepared query",
      {
        name: z.string().default("").describe("Name of the prepared query"),
        service: z.string().default("").describe("Service to query"),
        nearestN: z.number().optional().describe("Number of nearest nodes to return"),
        datacenters: z.array(z.string()).optional().describe("Datacenters to query"),
      },
      async ({ name, service, nearestN, datacenters }) => {
        try {
          // @ts-ignore - The Consul type definitions are incomplete
          const query = await consul.query.create({
            Name: name,
            Service: {
              Service: service,
              Failover: {
                NearestN: nearestN || 3,
                Datacenters: datacenters || [],
              },
            },
          }) as PreparedQuery;
          return { content: [{ type: "text", text: `Created prepared query: ${query.ID}` }] };
        } catch (error) {
          console.error("Error creating prepared query:", error);
          return { content: [{ type: "text", text: `Error creating prepared query: ${name}` }] };
        }
      }
    );
  • TypeScript interface defining the structure of a PreparedQuery response from Consul, used in the tool's handler.
    interface PreparedQuery {
      ID: string;
      Name: string;
      Service: {
        Service: string;
        Failover: {
          NearestN: number;
          Datacenters: string[];
        };
      };
    }

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