import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { BirstClient } from "../../client/birstClient.js";
import { z } from "zod";
// Response types
interface BqlMetadata {
bql: string;
columnDescriptions?: string[];
}
interface GenerateBqlResponse {
bqlMetadata?: BqlMetadata;
traces?: string[];
}
interface GenerateChartResponse {
generateBqlResponse?: GenerateBqlResponse;
chartType?: string;
tokenUsage?: string;
}
const inputSchema = z.object({
query: z.string().describe(
"Natural language description of the chart you want (e.g., 'Create a bar chart showing sales by region')"
),
spaceId: z.string().describe("The Birst space ID"),
});
export function registerGenerateChart(server: McpServer, client: BirstClient): void {
server.tool(
"birst_generate_chart",
"Generate a chart specification from natural language. Returns the recommended chart type and the BQL query to populate it.",
inputSchema.shape,
async (args) => {
const { query, spaceId } = inputSchema.parse(args);
const response = await client.genai<GenerateChartResponse>("/generate-chart/", {
method: "POST",
body: {
request: query,
spaceId,
},
});
const bqlResponse = response.generateBqlResponse;
const bql = bqlResponse?.bqlMetadata?.bql || "";
const columnDescriptions = bqlResponse?.bqlMetadata?.columnDescriptions || [];
return {
content: [
{
type: "text" as const,
text: JSON.stringify(
{
success: true,
chartType: response.chartType,
bql,
columnDescriptions,
tokenUsage: response.tokenUsage,
},
null,
2
),
},
],
};
}
);
}