count_chars
Count characters or bytes in text with options to include or exclude whitespace. Use this tool for text analysis and length measurement.
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 |
|---|---|---|---|
| text | Yes | ||
| mode | No | chars | |
| include_whitespace | No |
Implementation Reference
- src/mcp_charcount/server.py:11-38 (handler)The core handler function for the 'count_chars' MCP tool, decorated with @mcp.tool() for automatic schema inference and registration. Implements logic to count characters or bytes, optionally excluding whitespace.@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-8 (schema)Type alias defining the valid values for the 'mode' parameter in the count_chars tool schema.Mode = Literal["chars", "bytes"]
- src/mcp_charcount/server.py:11-11 (registration)Decorator registering the count_chars function as an MCP tool with name derived from function name and schema from type hints.@mcp.tool()