Skip to main content
Glama
onion-ai

onion-mcp-server

Official
by onion-ai

text_format

Format text into JSON, YAML, or remove extra whitespace with adjustable indentation.

Instructions

格式化文本内容,支持 JSON、YAML(需 pyyaml)、去除多余空白。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes要格式化的文本
formatNo格式类型: json / yaml / plain(去除多余空白)plain
indentNo缩进空格数(json/yaml 有效,默认 2)

Implementation Reference

  • The _text_format function is the handler that executes the 'text_format' tool logic. It accepts 'text', 'format' (json/yaml/plain), and 'indent' parameters, formatting text accordingly.
    def _text_format(args: dict) -> list[types.TextContent]:
        text   = args["text"]
        fmt    = args.get("format", "plain")
        indent = int(args.get("indent", 2))
    
        if fmt == "json":
            try:
                parsed = json.loads(text)
                result = json.dumps(parsed, ensure_ascii=False, indent=indent)
                return [types.TextContent(type="text", text=f"```json\n{result}\n```")]
            except json.JSONDecodeError as e:
                return [types.TextContent(type="text", text=f"❌ JSON 解析失败: {e}")]
    
        if fmt == "yaml":
            try:
                import yaml
                parsed = yaml.safe_load(text)
                result = yaml.dump(parsed, allow_unicode=True,
                                   default_flow_style=False, indent=indent)
                return [types.TextContent(type="text", text=f"```yaml\n{result}\n```")]
            except ImportError:
                return [types.TextContent(type="text",
                    text="❌ 需要安装 pyyaml: pip install pyyaml")]
            except Exception as e:
                return [types.TextContent(type="text", text=f"❌ YAML 解析失败: {e}")]
    
        # plain: 去除多余空白
        lines  = [line.strip() for line in text.splitlines()]
        result = "\n".join(line for line in lines if line)
        return [types.TextContent(type="text", text=result)]
  • The inputSchema for the 'text_format' tool defining its name, description, input parameters (text required, format with enum json/yaml/plain, indent integer) and defaults.
    TEXT_TOOLS: list[types.Tool] = [
        types.Tool(
            name="text_format",
            description="格式化文本内容,支持 JSON、YAML(需 pyyaml)、去除多余空白。",
            inputSchema={
                "type": "object",
                "properties": {
                    "text":   {"type": "string", "description": "要格式化的文本"},
                    "format": {
                        "type":    "string",
                        "description": "格式类型: json / yaml / plain(去除多余空白)",
                        "enum":    ["json", "yaml", "plain"],
                        "default": "plain",
                    },
                    "indent": {
                        "type":    "integer",
                        "description": "缩进空格数(json/yaml 有效,默认 2)",
                        "default": 2,
                    },
                },
                "required": ["text"],
            },
        ),
  • The handle_text function routes tool calls by name, dispatching 'text_format' to the _text_format handler.
    async def handle_text(name: str, arguments: dict) -> list[types.TextContent]:
        handlers = {
            "text_format":   _text_format,
            "text_diff":     _text_diff,
            "text_template": _text_template,
            "text_count":    _text_count,
            "text_clean":    _text_clean,
        }
        fn = handlers.get(name)
        if fn is None:
            raise ValueError(f"未知 text 工具: {name}")
        return fn(arguments)
  • Registration of all text tools (including 'text_format') into the server's routing table _HANDLERS, mapping tool names to the handle_text dispatcher.
    for _t in TEXT_TOOLS:   
        _HANDLERS[_t.name] = handle_text
  • ALL_TOOLS aggregation: TEXT_TOOLS (including 'text_format') are bundled into the server's complete tool list for exposure via list_tools().
    ALL_TOOLS: list[types.Tool] = [
        *AI_TOOLS,
        *CODE_TOOLS,
        *TEXT_TOOLS,
        *DATA_TOOLS,
        *WEB_TOOLS,
        *SYSTEM_TOOLS,
    ]
Behavior3/5

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

No annotations, so description should disclose behaviors. It states YAML requires pyyaml, but does not mention error handling, what happens on invalid input, or whether the tool mutates input or returns new string. Minimal disclosure.

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?

Single, front-loaded sentence with no wasted words. All information is relevant and efficiently presented.

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 low complexity and 3 parameters, the description is adequate for basic use. However, it does not explain return format, error scenarios, or behavior for invalid input. Could be more complete.

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 covers all 3 parameters with descriptions. The description adds context about the three formats and pyyaml dependency, but does not add significant new meaning beyond the schema (e.g., format enumeration values are listed, indent default stated).

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 it formats text content, supports JSON, YAML, and plain (removing whitespace). It specifies verb '格式化' (format) and resource '文本内容' (text content), and distinguishes from siblings like text_clean.

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

Usage Guidelines4/5

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

Provides clear context for when to use: for formatting text into JSON, YAML, or removing whitespace. Mentions pyyaml dependency for YAML. Does not explicitly state when not to use, but context is sufficient.

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