from typing import Literal, Dict, Any
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("mcp-charcount")
Mode = Literal["chars", "bytes"]
@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,
}
"""MCP server definition.
Run via:
uv run src/mcp_charcount/server.py
"""
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')