Skip to main content
Glama

extract_substrings

Extract specific text segments from strings using character index ranges, supporting negative indices and partial range specifications to retrieve precise substring data.

Instructions

Extract substrings by index ranges. Supports negative indices and omitting end. Returns list of {start, end, substring, length} dicts.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
rangesYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function for the 'extract_substrings' tool. It extracts multiple substrings from the input text based on provided ranges (with optional end indices and negative index support), returning a list of dictionaries containing the resolved start/end positions, the extracted substring, and its length.
    @mcp.tool()
    def extract_substrings(
        text: Annotated[str, "Text to extract from"],
        ranges: Annotated[list[dict], "List of ranges with 'start' (required) and 'end' (optional). Negative indices supported"]
    ) -> list[dict]:
        """Extract substrings by index ranges. Supports negative indices and omitting end. Returns list of {start, end, substring, length} dicts."""
        results = []
        text_len = len(text)
        
        for r in ranges:
            start = r["start"]
            end = r.get("end", None)
            
            # Normalize negative indices
            if start < 0:
                start = max(0, text_len + start)
            if end is not None and end < 0:
                end = max(0, text_len + end)
            
            # Extract substring
            substring = text[start:end]
            actual_end = end if end is not None else text_len
            
            results.append({
                "start": start,
                "end": actual_end,
                "substring": substring,
                "length": len(substring)
            })
        
        return results
Behavior3/5

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

With no annotations provided, the description carries full burden. It discloses key behavioral traits: supports negative indices and omitting end, and returns structured dicts. However, it doesn't cover error handling (e.g., invalid ranges), performance characteristics, or whether the operation is read-only/destructive (though extraction implies non-destructive).

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?

Extremely concise and well-structured: one sentence covering purpose, key features, and return format. Every word earns its place with zero redundancy. The information is front-loaded with the core functionality stated first.

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

Completeness3/5

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

Given 2 parameters with 0% schema coverage and an output schema exists, the description provides adequate basic information but has gaps. It explains the return format (which the output schema would detail), but doesn't fully compensate for the undocumented parameters or provide complete behavioral context for a string manipulation tool.

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

Parameters2/5

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

Schema description coverage is 0%, so the description must compensate. It mentions 'index ranges' which helps explain the 'ranges' parameter, but provides no details about the 'text' parameter or the structure/format of range objects. The description adds minimal value beyond what's implied by parameter names.

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

Purpose4/5

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

The description clearly states the tool's purpose: 'Extract substrings by index ranges' with specific functionality (supports negative indices and omitting end) and output format. It distinguishes from siblings like 'extract_between_markers' by focusing on index-based extraction rather than marker-based, but doesn't explicitly compare to all alternatives.

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?

No guidance on when to use this tool versus alternatives like 'extract_between_markers', 'find_regex_matches', or other sibling tools. The description mentions technical capabilities but provides no context about appropriate use cases or when other tools might be better suited.

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