import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { BirstClient } from "../../client/birstClient.js";
import { z } from "zod";
interface ValidateQueryResponse {
valid: boolean;
}
const inputSchema = z.object({
bql: z.string().describe("The BQL query to validate"),
spaceId: z.string().describe("The Birst space ID (logical ID)"),
product: z.string().default("BIRST").describe("Product type (default: BIRST)"),
connectionId: z.string().optional().describe("Optional connection ID within the space"),
});
export function registerValidateQuery(server: McpServer, client: BirstClient): void {
server.tool(
"birst_validate_query",
"Validate BQL syntax before execution. Use this to check if a query is valid without running it.",
inputSchema.shape,
async (args) => {
const { bql, spaceId, product, connectionId } = inputSchema.parse(args);
const requestBody: Record<string, unknown> = {
query: bql,
};
if (connectionId) {
requestBody.connectionId = connectionId;
}
const response = await client.icw<ValidateQueryResponse>("/query/validate/", {
method: "PUT",
body: requestBody,
queryParams: {
product,
appId: spaceId,
},
});
return {
content: [
{
type: "text" as const,
text: JSON.stringify(
{
valid: response.valid,
query: bql,
},
null,
2
),
},
],
};
}
);
}