Skip to main content
Glama

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
NameRequiredDescriptionDefault
include_whitespaceNo
modeNochars
textYes

Implementation Reference

  • 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, }
  • 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]:
  • The decorator that registers the count_chars function as an MCP tool on the FastMCP server instance.
    @mcp.tool()

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/TORIFUKUKaiou/mcp-charcount'

If you have feedback or need assistance with the MCP directory API, please join our Discord server