File size: 14,570 Bytes
7114af0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""Tool definitions for the Datawrapper MCP server."""

from mcp.types import Tool

from .config import CHART_CLASSES


async def list_tools() -> list[Tool]:
    """List available tools."""
    return [
        Tool(
            name="create_chart",
            description=(
                "⚠️ THIS IS THE DATAWRAPPER INTEGRATION ⚠️\n"
                "Use this MCP tool for ALL Datawrapper chart creation.\n\n"
                "DO NOT:\n"
                "❌ Install the 'datawrapper' Python package\n"
                "❌ Use the Datawrapper API directly\n"
                "❌ Import 'from datawrapper import ...'\n"
                "❌ Run pip install datawrapper\n\n"
                "This MCP server IS the complete Datawrapper integration. All Datawrapper operations "
                "should use the MCP tools provided by this server.\n\n"
                "---\n\n"
                "Create a Datawrapper chart with full control using Pydantic models. "
                "This allows you to specify all chart properties including title, description, "
                "visualization settings, axes, colors, and more. The chart_config should "
                "be a complete Pydantic model dict matching the schema for the chosen chart type.\n\n"
                "STYLING WORKFLOW:\n"
                "1. Use get_chart_schema to explore all available options for your chart type\n"
                "2. Refer to https://datawrapper.readthedocs.io/en/latest/ for detailed examples\n"
                "3. Build your chart_config with the desired styling properties\n\n"
                "Common styling patterns:\n"
                '- Colors: {"color_category": {"sales": "#1d81a2", "profit": "#15607a"}}\n'
                '- Line styling: {"lines": [{"column": "sales", "width": "style1", "interpolation": "curved"}]}\n'
                '- Axis ranges: {"custom_range_y": [0, 100], "custom_range_x": [2020, 2024]}\n'
                '- Grid formatting: {"y_grid_format": "0", "x_grid": "on", "y_grid": "on"}\n'
                '- Tooltips: {"tooltip_number_format": "00.00", "tooltip_x_format": "YYYY"}\n'
                '- Annotations: {"text_annotations": [{"x": "2023", "y": 50, "text": "Peak"}]}\n\n'
                "See the documentation for chart-type specific examples and advanced patterns.\n\n"
                'Example data format: [{"date": "2024-01", "value": 100}, {"date": "2024-02", "value": 150}]'
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "data": {
                        "type": ["string", "array", "object"],
                        "description": (
                            "Chart data. RECOMMENDED: Pass data inline as a list or dict.\n\n"
                            "PREFERRED FORMATS (use these first):\n\n"
                            "1. List of records (RECOMMENDED):\n"
                            '   [{"year": 2020, "sales": 100}, {"year": 2021, "sales": 150}]\n\n'
                            "2. Dict of arrays:\n"
                            '   {"year": [2020, 2021], "sales": [100, 150]}\n\n'
                            "3. JSON string of format 1 or 2:\n"
                            '   \'[{"year": 2020, "sales": 100}]\'\n\n'
                            "ALTERNATIVE (only for extremely large datasets where inline data is impractical):\n\n"
                            "4. File path to CSV or JSON:\n"
                            '   "/path/to/data.csv" or "/path/to/data.json"\n'
                            "   - Use only when inline data would be too large to pass directly\n"
                            "   - CSV files are read directly\n"
                            "   - JSON files must contain list of dicts or dict of arrays"
                        ),
                    },
                    "chart_type": {
                        "type": "string",
                        "enum": list(CHART_CLASSES.keys()),
                        "description": "Type of chart to create",
                    },
                    "chart_config": {
                        "type": "object",
                        "description": (
                            "Complete chart configuration as a Pydantic model dict. "
                            "Must match the schema for the chosen chart_type. "
                            "Use get_chart_schema to see the full schema."
                        ),
                    },
                },
                "required": ["data", "chart_type", "chart_config"],
            },
        ),
        Tool(
            name="get_chart_schema",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Get the Pydantic JSON schema for a specific chart type. This is your primary tool "
                "for discovering styling and configuration options.\n\n"
                "The schema shows:\n"
                "- All available properties and their types\n"
                "- Enum values (e.g., line widths, interpolation methods)\n"
                "- Default values\n"
                "- Detailed descriptions for each property\n\n"
                "WORKFLOW: Use this tool first to explore options, then refer to "
                "https://datawrapper.readthedocs.io/en/latest/ for detailed examples and patterns "
                "showing how to use these properties in practice."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_type": {
                        "type": "string",
                        "enum": list(CHART_CLASSES.keys()),
                        "description": "Chart type to get schema for",
                    },
                },
                "required": ["chart_type"],
            },
        ),
        Tool(
            name="publish_chart",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Publish a Datawrapper chart to make it publicly accessible. "
                "Returns the public URL of the published chart. "
                "IMPORTANT: Only use this tool when the user explicitly requests to publish the chart. "
                "Do not automatically publish charts after creation unless specifically asked."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_id": {
                        "type": "string",
                        "description": "ID of the chart to publish",
                    },
                },
                "required": ["chart_id"],
            },
        ),
        Tool(
            name="get_chart",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Get information about an existing Datawrapper chart, "
                "including its metadata, data, and public URL if published."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_id": {
                        "type": "string",
                        "description": "ID of the chart to retrieve",
                    },
                },
                "required": ["chart_id"],
            },
        ),
        Tool(
            name="update_chart",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Update an existing Datawrapper chart's data or configuration using Pydantic models. "
                "IMPORTANT: The chart_config must use high-level Pydantic fields only (title, intro, "
                "byline, source_name, source_url, etc.). Do NOT use low-level serialized structures "
                "like 'metadata', 'visualize', or other internal API fields.\n\n"
                "STYLING UPDATES:\n"
                "Use get_chart_schema to see available fields, then apply styling changes:\n"
                '- Colors: {"color_category": {"sales": "#ff0000"}}\n'
                '- Line properties: {"lines": [{"column": "sales", "width": "style2"}]}\n'
                '- Axis settings: {"custom_range_y": [0, 200], "y_grid_format": "0,0"}\n'
                '- Tooltips: {"tooltip_number_format": "0.0"}\n\n'
                "See https://datawrapper.readthedocs.io/en/latest/ for detailed examples. "
                "The provided config will be validated through Pydantic and merged with the existing "
                "chart configuration."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_id": {
                        "type": "string",
                        "description": "ID of the chart to update",
                    },
                    "data": {
                        "type": ["string", "array", "object"],
                        "description": (
                            "Chart data. RECOMMENDED: Pass data inline as a list or dict.\n\n"
                            "PREFERRED FORMATS (use these first):\n\n"
                            "1. List of records (RECOMMENDED):\n"
                            '   [{"year": 2020, "sales": 100}, {"year": 2021, "sales": 150}]\n\n'
                            "2. Dict of arrays:\n"
                            '   {"year": [2020, 2021], "sales": [100, 150]}\n\n'
                            "3. JSON string of format 1 or 2:\n"
                            '   \'[{"year": 2020, "sales": 100}]\'\n\n'
                            "ALTERNATIVE (only for extremely large datasets where inline data is impractical):\n\n"
                            "4. File path to CSV or JSON:\n"
                            '   "/path/to/data.csv" or "/path/to/data.json"\n'
                            "   - Use only when inline data would be too large to pass directly\n"
                            "   - CSV files are read directly\n"
                            "   - JSON files must contain list of dicts or dict of arrays"
                        ),
                    },
                    "chart_config": {
                        "type": "object",
                        "description": (
                            "Updated chart configuration using high-level Pydantic fields (optional). "
                            "Must use Pydantic model fields like 'title', 'intro', 'byline', etc. "
                            "Do NOT use raw API structures like 'metadata' or 'visualize'. "
                            "Use get_chart_schema to see valid fields. Will be validated and merged "
                            "with existing config."
                        ),
                    },
                },
                "required": ["chart_id"],
            },
        ),
        Tool(
            name="delete_chart",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Delete a Datawrapper chart permanently."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_id": {
                        "type": "string",
                        "description": "ID of the chart to delete",
                    },
                },
                "required": ["chart_id"],
            },
        ),
        Tool(
            name="export_chart_png",
            description=(
                "⚠️ DATAWRAPPER MCP TOOL ⚠️\n"
                "This is part of the Datawrapper MCP server integration.\n\n"
                "---\n\n"
                "Export a Datawrapper chart as PNG and display it inline. "
                "The chart must be created first using create_chart. "
                "Supports high-resolution output via the zoom parameter. "
                "IMPORTANT: Only use this tool when the user explicitly requests to see the chart image "
                "or export it as PNG. Do not automatically export charts after creation unless specifically asked."
            ),
            inputSchema={
                "type": "object",
                "properties": {
                    "chart_id": {
                        "type": "string",
                        "description": "ID of the chart to export",
                    },
                    "width": {
                        "type": "integer",
                        "description": "Width of the image in pixels (optional, uses chart width if not specified)",
                    },
                    "height": {
                        "type": "integer",
                        "description": "Height of the image in pixels (optional, uses chart height if not specified)",
                    },
                    "plain": {
                        "type": "boolean",
                        "description": "If true, exports only the visualization without header/footer (default: false)",
                        "default": False,
                    },
                    "zoom": {
                        "type": "integer",
                        "description": "Scale multiplier for resolution, e.g., 2 = 2x resolution (default: 2)",
                        "default": 2,
                    },
                    "transparent": {
                        "type": "boolean",
                        "description": "If true, exports with transparent background (default: false)",
                        "default": False,
                    },
                    "border_width": {
                        "type": "integer",
                        "description": "Margin around visualization in pixels (default: 0)",
                        "default": 0,
                    },
                    "border_color": {
                        "type": "string",
                        "description": "Color of the border, e.g., '#FFFFFF' (optional, uses chart background if not specified)",
                    },
                },
                "required": ["chart_id"],
            },
        ),
    ]