|
1 | | -import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js"; |
2 | | -import { z } from "zod"; |
3 | | -import * as Charts from "../charts"; |
4 | | -import { generateChartUrl } from "./generate"; |
5 | | -import { ValidateError } from "./validator"; |
6 | | - |
7 | | -// Chart type mapping |
8 | | -const CHART_TYPE_MAP = { |
9 | | - generate_area_chart: "area", |
10 | | - generate_bar_chart: "bar", |
11 | | - generate_boxplot_chart: "boxplot", |
12 | | - generate_column_chart: "column", |
13 | | - generate_dual_axes_chart: "dual-axes", |
14 | | - generate_fishbone_diagram: "fishbone-diagram", |
15 | | - generate_flow_diagram: "flow-diagram", |
16 | | - generate_funnel_chart: "funnel", |
17 | | - generate_histogram_chart: "histogram", |
18 | | - generate_line_chart: "line", |
19 | | - generate_liquid_chart: "liquid", |
20 | | - generate_mind_map: "mind-map", |
21 | | - generate_network_graph: "network-graph", |
22 | | - generate_organization_chart: "organization-chart", |
23 | | - generate_pie_chart: "pie", |
24 | | - generate_radar_chart: "radar", |
25 | | - generate_sankey_chart: "sankey", |
26 | | - generate_scatter_chart: "scatter", |
27 | | - generate_treemap_chart: "treemap", |
28 | | - generate_venn_chart: "venn", |
29 | | - generate_violin_chart: "violin", |
30 | | - generate_word_cloud_chart: "word-cloud", |
31 | | -} as const; |
32 | | - |
33 | | -/** |
34 | | - * Call a tool to generate a chart based on the provided name and arguments. |
35 | | - * @param tool The name of the tool to call, e.g., "generate_area_chart". |
36 | | - * @param args The arguments for the tool, which should match the expected schema for the chart type. |
37 | | - * @returns |
38 | | - */ |
39 | | -export async function callTool(tool: string, args: object = {}) { |
40 | | - const chartType = CHART_TYPE_MAP[tool as keyof typeof CHART_TYPE_MAP]; |
41 | | - |
42 | | - if (!chartType) { |
43 | | - throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${tool}.`); |
44 | | - } |
45 | | - |
46 | | - try { |
47 | | - // Validate input using Zod before sending to API. |
48 | | - // Select the appropriate schema based on the chart type. |
49 | | - const schema = Charts[chartType].schema; |
50 | | - |
51 | | - if (schema) { |
52 | | - // Use safeParse instead of parse and try-catch. |
53 | | - const result = z.object(schema).safeParse(args); |
54 | | - if (!result.success) { |
55 | | - throw new McpError( |
56 | | - ErrorCode.InvalidParams, |
57 | | - `Invalid parameters: ${result.error.message}`, |
58 | | - ); |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - const url = await generateChartUrl(chartType, args); |
63 | | - |
64 | | - return { |
65 | | - content: [ |
66 | | - { |
67 | | - type: "text", |
68 | | - text: url, |
69 | | - }, |
70 | | - ], |
71 | | - }; |
72 | | - // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
73 | | - } catch (error: any) { |
74 | | - if (error instanceof McpError) throw error; |
75 | | - if (error instanceof ValidateError) |
76 | | - throw new McpError(ErrorCode.InvalidParams, error.message); |
77 | | - throw new McpError( |
78 | | - ErrorCode.InternalError, |
79 | | - `Failed to generate chart: ${error?.message || "Unknown error."}`, |
80 | | - ); |
81 | | - } |
82 | | -} |
| 1 | +import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js"; |
| 2 | +import { z } from "zod"; |
| 3 | +import * as Charts from "../charts"; |
| 4 | +import { getStrategy } from "./GenerateStrategy"; |
| 5 | +import { getVisGenerateStrategy } from "./env"; |
| 6 | +import { ValidateError } from "./validator"; |
| 7 | + |
| 8 | +// Chart type mapping |
| 9 | +const CHART_TYPE_MAP = { |
| 10 | + generate_area_chart: "area", |
| 11 | + generate_bar_chart: "bar", |
| 12 | + generate_boxplot_chart: "boxplot", |
| 13 | + generate_column_chart: "column", |
| 14 | + generate_dual_axes_chart: "dual-axes", |
| 15 | + generate_fishbone_diagram: "fishbone-diagram", |
| 16 | + generate_flow_diagram: "flow-diagram", |
| 17 | + generate_funnel_chart: "funnel", |
| 18 | + generate_histogram_chart: "histogram", |
| 19 | + generate_line_chart: "line", |
| 20 | + generate_liquid_chart: "liquid", |
| 21 | + generate_mind_map: "mind-map", |
| 22 | + generate_network_graph: "network-graph", |
| 23 | + generate_organization_chart: "organization-chart", |
| 24 | + generate_pie_chart: "pie", |
| 25 | + generate_radar_chart: "radar", |
| 26 | + generate_sankey_chart: "sankey", |
| 27 | + generate_scatter_chart: "scatter", |
| 28 | + generate_treemap_chart: "treemap", |
| 29 | + generate_venn_chart: "venn", |
| 30 | + generate_violin_chart: "violin", |
| 31 | + generate_word_cloud_chart: "word-cloud", |
| 32 | +} as const; |
| 33 | + |
| 34 | +/** |
| 35 | + * Call a tool to generate a chart based on the provided name and arguments. |
| 36 | + * @param tool The name of the tool to call, e.g., "generate_area_chart". |
| 37 | + * @param args The arguments for the tool, which should match the expected schema for the chart type. |
| 38 | + * @returns |
| 39 | + */ |
| 40 | +export async function callTool(tool: string, args: object = {}) { |
| 41 | + const chartType = CHART_TYPE_MAP[tool as keyof typeof CHART_TYPE_MAP]; |
| 42 | + |
| 43 | + if (!chartType) { |
| 44 | + throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${tool}.`); |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + // Validate input using Zod before sending to API. |
| 49 | + // Select the appropriate schema based on the chart type. |
| 50 | + const schema = Charts[chartType].schema; |
| 51 | + |
| 52 | + if (schema) { |
| 53 | + // Use safeParse instead of parse and try-catch. |
| 54 | + const result = z.object(schema).safeParse(args); |
| 55 | + if (!result.success) { |
| 56 | + throw new McpError( |
| 57 | + ErrorCode.InvalidParams, |
| 58 | + `Invalid parameters: ${result.error.message}`, |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const generateStrategy = getStrategy(getVisGenerateStrategy()); |
| 64 | + const url = await generateStrategy.generate(chartType, args); |
| 65 | + |
| 66 | + return { |
| 67 | + content: [ |
| 68 | + { |
| 69 | + type: "text", |
| 70 | + text: url, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }; |
| 74 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 75 | + } catch (error: any) { |
| 76 | + if (error instanceof McpError) throw error; |
| 77 | + if (error instanceof ValidateError) |
| 78 | + throw new McpError(ErrorCode.InvalidParams, error.message); |
| 79 | + throw new McpError( |
| 80 | + ErrorCode.InternalError, |
| 81 | + `Failed to generate chart: ${error?.message || "Unknown error."}`, |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments