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

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

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