Skip to main content
Glama

find_nth_substring

Locate the starting position of the nth occurrence of a substring within text. Returns -1 if the substring does not appear the specified number of times.

Instructions

Find starting index of nth occurrence of a substring. Returns -1 if not found.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
substringYes
nNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Implements the find_nth_substring tool logic using a while loop with str.find() to locate the starting index of the nth occurrence of the substring, handling overlaps by advancing start by 1.
    def find_nth_substring(
        text: Annotated[str, "Text to search in"],
        substring: Annotated[str, "Substring to find"],
        n: Annotated[int, "Which occurrence to find (1-based)"] = 1
    ) -> int:
        """Find starting index of nth occurrence of a substring. Returns -1 if not found."""
        if not substring:
            raise ValueError("substring cannot be empty")
        if n < 1:
            raise ValueError("n must be >= 1")
        
        count = 0
        start = 0
        
        while True:
            index = text.find(substring, start)
            if index == -1:
                return -1
            
            count += 1
            if count == n:
                return index
            
            start = index + 1
  • The @mcp.tool() decorator from FastMCP registers the find_nth_substring function as an MCP tool.
    @mcp.tool()
  • The function signature with Annotated type hints defines the input schema: text (str), substring (str), n (int, default 1) and output int.
    def find_nth_substring(
        text: Annotated[str, "Text to search in"],
        substring: Annotated[str, "Substring to find"],
        n: Annotated[int, "Which occurrence to find (1-based)"] = 1
    ) -> int:
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It clearly describes the core behavior (finding starting index) and edge case handling (returns -1 if not found), which is good. However, it doesn't mention performance characteristics, case sensitivity, encoding considerations, or what happens with overlapping matches - leaving some behavioral aspects unspecified.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise - just two short sentences that communicate the essential information with zero waste. It's front-loaded with the core purpose and follows with the important edge case behavior. Every word earns its place in this minimal but complete description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (string search with occurrence counting), no annotations, and the existence of an output schema (which handles return value documentation), the description is reasonably complete. It covers the core functionality and error case, though it could benefit from mentioning case sensitivity or match behavior. For a utility function with output schema support, this provides adequate context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description must compensate for the lack of parameter documentation in the schema. While it doesn't explicitly name parameters, it clearly explains what the tool does with 'text', 'substring', and 'n' through its functional description. The mention of 'nth occurrence' and 'returns -1 if not found' provides semantic context for all three parameters, though it doesn't detail the default value for 'n' or parameter constraints.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Find starting index of nth occurrence of a substring') and distinguishes it from siblings like 'find_all_substring_indices' (which finds all occurrences) and 'find_nth_char' (which works on characters rather than substrings). It uses precise technical language that defines exactly what the tool does.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'find_all_substring_indices' for finding all occurrences or 'find_nth_char' for character-based searches. There's no context about when this specific nth-occurrence search is preferable to other substring tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/agent-hanju/char-index-mcp'

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