Skip to main content
Glama
TORIFUKUKaiou

MCP Character Count

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

Implementation Reference

  • 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,
        }
  • Type alias defining the valid values for the 'mode' parameter in the count_chars tool schema.
    Mode = Literal["chars", "bytes"]
  • Decorator registering the count_chars function as an MCP tool with name derived from function name and schema from type hints.
    @mcp.tool()
Install Server

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