Skip to main content
Glama
onion-ai

onion-mcp-server

Official
by onion-ai

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

TableJSON Schema
NameRequiredDescriptionDefault
textYes源数据文本
from_formatYes源格式
to_formatYes目标格式

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"],
        },
    ),
  • 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)
  • 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
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full responsibility for behavioral disclosure. It only states the conversion action but does not detail important traits like validation behavior, error handling, or output format preservation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, short sentence that efficiently conveys the core functionality without extraneous words. It is well front-loaded and easy to parse.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the lack of output schema, the description does not explain the return format (e.g., string). For a conversion tool, additional context about output structure would enhance completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, and the description does not add meaningful information beyond what the schema already provides (parameter names, types, enums). The description simply lists the formats, which matches the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action (convert) and the specific formats (JSON, CSV, YAML, TOML). It distinguishes well from sibling tools that perform more specific data operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives. It does not mention any prerequisites, limitations, or cases where other sibling tools might be more appropriate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/onion-ai/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server