find_nth_char
Locate the position of the nth occurrence of a specific character within text. Returns the index or -1 if not found.
Instructions
Find index of nth occurrence of a character. Returns -1 if not found.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| char | Yes | ||
| n | No |
Implementation Reference
- char_index_mcp/server.py:31-49 (handler)The handler function for the 'find_nth_char' tool, including decorator for registration and inline schema via Annotated types. Implements iteration over text to count and locate the nth occurrence of the specified character.@mcp.tool() def find_nth_char( text: Annotated[str, "Text to search in"], char: Annotated[str, "Single character to find"], n: Annotated[int, "Which occurrence to find (1-based)"] = 1 ) -> int: """Find index of nth occurrence of a character. Returns -1 if not found.""" if len(char) != 1: raise ValueError("char must be a single character") if n < 1: raise ValueError("n must be >= 1") count = 0 for i, c in enumerate(text): if c == char: count += 1 if count == n: return i return -1