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
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| ranges | Yes |
Implementation Reference
- char_index_mcp/server.py:278-309 (handler)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