text_count
Count words, lines, characters, and paragraphs in any text to analyze its structure.
Instructions
统计文本的字数、行数、字符数、段落数等信息。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | 要统计的文本 |
Implementation Reference
- The handler function for the text_count tool. Counts total characters, lines, blank lines, paragraphs, Chinese characters, English words, and estimated tokens.
def _text_count(args: dict) -> list[types.TextContent]: text = args["text"] lines = text.splitlines() # 中英文字数统计 cn_chars = len(re.findall(r"[\u4e00-\u9fff]", text)) en_words = len(re.findall(r"\b[a-zA-Z]+\b", text)) blank = sum(1 for line in lines if not line.strip()) paras = len([p for p in text.split("\n\n") if p.strip()]) return [types.TextContent(type="text", text=( f"📊 文本统计\n\n" f"总字符数: {len(text)}\n" f"总行数: {len(lines)}\n" f"空行数: {blank}\n" f"段落数: {paras}\n" f"中文字符: {cn_chars}\n" f"英文单词: {en_words}\n" f"预估 Token: ~{len(text) // 4}" ))] - Schema definition for text_count tool: requires a single 'text' string parameter.
types.Tool( name="text_count", description="统计文本的字数、行数、字符数、段落数等信息。", inputSchema={ "type": "object", "properties": { "text": {"type": "string", "description": "要统计的文本"}, }, "required": ["text"], }, - src/onion_mcp_server/tools/text.py:117-128 (registration)The dispatch/handler registration that routes 'text_count' to the _text_count function.
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 in the main server: TEXT_TOOLS (including text_count) are routed through handle_text.
for _t in TEXT_TOOLS: _HANDLERS[_t.name] = handle_text