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
| Name | Required | Description | Default |
|---|---|---|---|
| datacenters | No | Datacenters to query | |
| name | No | Name of the prepared query | |
| nearestN | No | Number of nearest nodes to return | |
| service | No | Service to query |
Implementation Reference
- src/tools/consulTools.ts:597-615 (handler)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}` }] }; } }
- src/tools/consulTools.ts:591-596 (schema)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"), },
- src/tools/consulTools.ts:588-616 (registration)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}` }] }; } } );
- src/tools/consulTools.ts:42-52 (schema)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[]; }; }; }