count_chars
Count characters or bytes in text with options to include or exclude whitespace for accurate text length measurement and analysis.
Instructions
Count the length of input text.
- mode="chars": Unicode code points count (Python len).
- mode="bytes": UTF-8 encoded byte length.
- include_whitespace: when False, whitespace characters are filtered out before counting.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| include_whitespace | No | ||
| mode | No | chars | |
| text | Yes |
Implementation Reference
- src/mcp_charcount/server.py:11-38 (handler)The @mcp.tool() decorated function implementing the core logic of the 'count_chars' tool, handling text counting in chars or bytes mode with optional whitespace exclusion.@mcp.tool() def count_chars( text: str, mode: Mode = "chars", include_whitespace: bool = True, ) -> Dict[str, Any]: """ Count the length of input text. - mode="chars": Unicode code points count (Python len). - mode="bytes": UTF-8 encoded byte length. - include_whitespace: when False, whitespace characters are filtered out before counting. """ s = text if include_whitespace else "".join(ch for ch in text if not ch.isspace()) if mode == "bytes": count = len(s.encode("utf-8")) elif mode == "chars": count = len(s) else: raise ValueError("Unsupported mode. Use 'chars' or 'bytes'.") return { "count": count, "mode": mode, "include_whitespace": include_whitespace, }
- src/mcp_charcount/server.py:8-16 (schema)Type definitions including Mode Literal type and function parameters with type hints, used by FastMCP to generate input schema.Mode = Literal["chars", "bytes"] @mcp.tool() def count_chars( text: str, mode: Mode = "chars", include_whitespace: bool = True, ) -> Dict[str, Any]:
- src/mcp_charcount/server.py:11-11 (registration)The decorator that registers the count_chars function as an MCP tool on the FastMCP server instance.@mcp.tool()