Skip to content

Commit 770809d

Browse files
authored
feat: export all chart schema & call tool function (#81)
* feat: export all chart schema & call tool function * chore: fix typo of english * refactor: chart schema should be an object * test: add test cases for utils
1 parent 3321c96 commit 770809d

29 files changed

+247
-142
lines changed

__tests__/api.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import * as API from "../src/sdk";
3+
4+
describe("sdk API", () => {
5+
it("callTool", () => {
6+
expect(API.callTool).instanceOf(Function);
7+
});
8+
9+
it("chart meta", () => {
10+
const { callTool, ...charts } = API;
11+
expect(Object.keys(charts)).toEqual([
12+
"area",
13+
"bar",
14+
"column",
15+
"fishbone-diagram",
16+
"flow-diagram",
17+
"histogram",
18+
"line",
19+
"mind-map",
20+
"network-graph",
21+
"pie",
22+
"radar",
23+
"scatter",
24+
"treemap",
25+
"word-cloud",
26+
"dual-axes",
27+
]);
28+
});
29+
30+
it("chart meta structure", () => {
31+
const { area } = API;
32+
expect(area.schema).toBeTypeOf("object");
33+
expect(area.tool).toBeTypeOf("object");
34+
expect(area.tool.name).toBeTypeOf("string");
35+
expect(area.tool.description).toBeTypeOf("string");
36+
expect(area.tool.inputSchema).toBeTypeOf("object");
37+
});
38+
});

__tests__/charts.spec.ts renamed to __tests__/charts/charts.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vitest";
2-
import * as actualCharts from "../src/charts";
3-
import * as expectedCharts from "./charts";
2+
import * as expectedCharts from ".";
3+
import * as actualCharts from "../../src/charts";
44

55
describe("charts schema check", () => {
66
// Get the chart names from the rightCharts module

__tests__/utils/validators.spec.ts renamed to __tests__/constant.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { describe, expect, it } from "vitest";
2-
import * as Charts from "../../src/charts";
3-
4-
const mindMapSchema = {
1+
export const MindMapSchema = {
52
type: "mind-map",
63
data: {
74
name: "剪映视频剪辑指南",
@@ -172,7 +169,7 @@ const mindMapSchema = {
172169
source: "mcp-server-chart",
173170
};
174171

175-
const flowDiagramSchema = {
172+
export const FlowDiagramSchema = {
176173
type: "network-graph",
177174
data: {
178175
nodes: [
@@ -266,25 +263,3 @@ const flowDiagramSchema = {
266263
height: 600,
267264
source: "mcp-server-chart",
268265
};
269-
270-
describe("valid", () => {
271-
// Valid mind map bad case
272-
it("should valid schema for mind-map chart", () => {
273-
const chartType = "mind-map";
274-
expect(() => {
275-
const schema = Charts[chartType].schema;
276-
schema.safeParse(mindMapSchema);
277-
}).toThrow("Invalid parameters: node name '文字动画' is not unique.");
278-
});
279-
280-
// Valid flow diagram bad case
281-
it("should valid schema for flow diagram chart", () => {
282-
const chartType = "flow-diagram";
283-
expect(() => {
284-
const schema = Charts[chartType].schema;
285-
schema.safeParse(flowDiagramSchema);
286-
}).toThrow(
287-
"Invalid parameters: edge pair 'KnowledgeBase-Model' is not unique.",
288-
);
289-
});
290-
});

__tests__/utils/env.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { getVisRequestServer } from "../../src/utils/env";
3+
4+
describe("env", () => {
5+
it("default vis request server", () => {
6+
expect(getVisRequestServer()).toBe(
7+
"https://antv-studio.alipay.com/api/gpt-vis",
8+
);
9+
});
10+
11+
it("modify vis request server by env", () => {
12+
process.env.VIS_REQUEST_SERVER = "https://example.com/api/gpt-vis";
13+
expect(getVisRequestServer()).toBe("https://example.com/api/gpt-vis");
14+
});
15+
16+
afterEach(() => {
17+
process.env.VIS_REQUEST_SERVER =
18+
"https://antv-studio.alipay.com/api/gpt-vis";
19+
});
20+
});

__tests__/utils/schema.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { z } from "zod";
3+
import { zodToJsonSchema } from "../../src/utils/schema";
4+
5+
describe("schema", () => {
6+
it("default vis request server", () => {
7+
expect(
8+
zodToJsonSchema({
9+
a: z.number(),
10+
b: z.string(),
11+
c: z.boolean(),
12+
}),
13+
).toEqual({
14+
$schema: "http://json-schema.org/draft-07/schema#",
15+
properties: {
16+
a: {
17+
type: "number",
18+
},
19+
b: {
20+
type: "string",
21+
},
22+
c: {
23+
type: "boolean",
24+
},
25+
},
26+
required: ["a", "b", "c"],
27+
type: "object",
28+
});
29+
});
30+
});

__tests__/utils/validator.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from "vitest";
2+
import { z } from "zod";
3+
import * as Charts from "../../src/charts";
4+
import { FlowDiagramSchema, MindMapSchema } from "../constant";
5+
6+
describe("validator", () => {
7+
it("should valid schema for mind-map chart", () => {
8+
const chartType = "mind-map";
9+
expect(() => {
10+
const schema = Charts[chartType].schema;
11+
z.object(schema).safeParse(MindMapSchema);
12+
}).toThrow("Invalid parameters: node's name '文字动画' should be unique.");
13+
});
14+
15+
it("should valid schema for flow diagram chart", () => {
16+
const chartType = "flow-diagram";
17+
expect(() => {
18+
const schema = Charts[chartType].schema;
19+
z.object(schema).safeParse(FlowDiagramSchema);
20+
}).toThrow(
21+
"Invalid parameters: edge pair 'KnowledgeBase-Model' should be unique.",
22+
);
23+
});
24+
});

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{
22
"name": "@antv/mcp-server-chart",
33
"description": "A Model Context Protocol server for generating charts using AntV, This is a TypeScript-based MCP server that provides chart generation capabilities. It allows you to create various types of charts through MCP tools.",
4-
"version": "0.4.1",
4+
"version": "0.5.0",
55
"main": "build/index.js",
66
"type": "module",
7+
"exports": {
8+
"./sdk": "./build/sdk.js"
9+
},
710
"scripts": {
811
"prebuild": "rm -rf build/*",
912
"build": "tsc && tsc-alias -p tsconfig.json",

src/charts/area.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const data = z.object({
1717
});
1818

1919
// Area chart input schema
20-
const schema = z.object({
20+
const schema = {
2121
data: z
2222
.array(data)
2323
.describe("Data for area chart, such as, [{ time: '2018', value: 99.9 }].")
@@ -35,7 +35,7 @@ const schema = z.object({
3535
title: TitleSchema,
3636
axisXTitle: AxisXTitleSchema,
3737
axisYTitle: AxisYTitleSchema,
38-
});
38+
};
3939

4040
// Area chart tool descriptor
4141
const tool = {

src/charts/bar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const data = z.object({
1717
});
1818

1919
// Bar chart input schema
20-
const schema = z.object({
20+
const schema = {
2121
data: z
2222
.array(data)
2323
.describe(
@@ -44,7 +44,7 @@ const schema = z.object({
4444
title: TitleSchema,
4545
axisXTitle: AxisXTitleSchema,
4646
axisYTitle: AxisYTitleSchema,
47-
});
47+
};
4848

4949
// Bar chart tool descriptor
5050
const tool = {

src/charts/column.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const data = z.object({
1717
});
1818

1919
// Column chart input schema
20-
const schema = z.object({
20+
const schema = {
2121
data: z
2222
.array(data)
2323
.describe(
@@ -44,7 +44,7 @@ const schema = z.object({
4444
title: TitleSchema,
4545
axisXTitle: AxisXTitleSchema,
4646
axisYTitle: AxisYTitleSchema,
47-
});
47+
};
4848

4949
// Column chart tool descriptor
5050
const tool = {

0 commit comments

Comments
 (0)