text_format
Format text into JSON, YAML, or remove extra whitespace with adjustable indentation.
Instructions
格式化文本内容,支持 JSON、YAML(需 pyyaml)、去除多余空白。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | 要格式化的文本 | |
| format | No | 格式类型: json / yaml / plain(去除多余空白) | plain |
| indent | No | 缩进空格数(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"], }, ), - src/onion_mcp_server/tools/text.py:117-128 (registration)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) - src/onion_mcp_server/server.py:54-55 (registration)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 - src/onion_mcp_server/server.py:39-46 (registration)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, ]