data_convert
Convert data between JSON, CSV, YAML, and TOML formats by specifying the source and target formats.
Instructions
在 JSON、CSV、YAML、TOML 格式之间互相转换。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | 源数据文本 | |
| from_format | Yes | 源格式 | |
| to_format | Yes | 目标格式 |
Implementation Reference
- The _data_convert function that executes the actual data format conversion logic. Parses source data from JSON/CSV/YAML/TOML and serializes to the target format.
def _data_convert(args: dict) -> list[types.TextContent]: text = args["text"] from_format = args["from_format"] to_format = args["to_format"] if from_format == to_format: return [types.TextContent(type="text", text=text)] # 解析源数据 try: if from_format == "json": data = json.loads(text) elif from_format == "csv": reader = csv.DictReader(io.StringIO(text)) data = list(reader) elif from_format == "yaml": import yaml data = yaml.safe_load(text) elif from_format == "toml": import tomllib data = tomllib.loads(text) else: return [types.TextContent(type="text", text=f"❌ 不支持的源格式: {from_format}")] except Exception as e: return [types.TextContent(type="text", text=f"❌ 解析 {from_format} 失败: {e}")] # 序列化目标格式 try: if to_format == "json": result = json.dumps(data, ensure_ascii=False, indent=2) return [types.TextContent(type="text", text=f"```json\n{result}\n```")] elif to_format == "csv": if not isinstance(data, list): data = [data] buf = io.StringIO() writer = csv.DictWriter(buf, fieldnames=list(data[0].keys())) writer.writeheader() writer.writerows(data) return [types.TextContent(type="text", text=buf.getvalue())] elif to_format == "yaml": import yaml result = yaml.dump(data, allow_unicode=True, default_flow_style=False) return [types.TextContent(type="text", text=f"```yaml\n{result}\n```")] elif to_format == "toml": try: import tomli_w if not isinstance(data, dict): data = {"data": data} result = tomli_w.dumps(data) except ImportError: return [types.TextContent(type="text", text="❌ 需要安装 tomli-w: pip install tomli-w")] return [types.TextContent(type="text", text=f"```toml\n{result}\n```")] else: return [types.TextContent(type="text", text=f"❌ 不支持的目标格式: {to_format}")] except Exception as e: return [types.TextContent(type="text", text=f"❌ 转换为 {to_format} 失败: {e}")] - The Tool registration for data_convert defining name, description, and inputSchema with text, from_format, and to_format fields.
types.Tool( name="data_convert", description="在 JSON、CSV、YAML、TOML 格式之间互相转换。", inputSchema={ "type": "object", "properties": { "text": {"type": "string", "description": "源数据文本"}, "from_format": { "type": "string", "enum": ["json", "csv", "yaml", "toml"], "description": "源格式", }, "to_format": { "type": "string", "enum": ["json", "csv", "yaml", "toml"], "description": "目标格式", }, }, "required": ["text", "from_format", "to_format"], }, ), - src/onion_mcp_server/tools/data.py:99-109 (registration)The handle_data dispatcher function that maps tool names to handler functions, registered in the _HANDLERS dict in server.py.
async def handle_data(name: str, arguments: dict) -> list[types.TextContent]: handlers = { "data_json_query": _json_query, "data_csv_analyze": _csv_analyze, "data_table_format": _table_format, "data_convert": _data_convert, } fn = handlers.get(name) if fn is None: raise ValueError(f"未知 data 工具: {name}") return fn(arguments) - src/onion_mcp_server/server.py:56-61 (registration)Registration of the data_convert tool in the server's handler routing table via _HANDLERS[_t.name] = handle_data.
for _t in DATA_TOOLS: _HANDLERS[_t.name] = handle_data for _t in WEB_TOOLS: _HANDLERS[_t.name] = handle_web for _t in SYSTEM_TOOLS: _HANDLERS[_t.name] = handle_system