extract_between_markers
Extract text content between specified start and end markers, returning both the extracted text and its character position information for precise text analysis.
Instructions
Extract content between markers with positions. Returns dict with content and position info.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| start_marker | Yes | ||
| end_marker | Yes | ||
| occurrence | No |
Implementation Reference
- char_index_mcp/server.py:207-257 (handler)The complete handler function for the 'extract_between_markers' tool, including decorator for registration and inline schema via Annotated types. This implements the core logic to find and extract content between specified markers.@mcp.tool() def extract_between_markers( text: Annotated[str, "Text to search in"], start_marker: Annotated[str, "Opening marker"], end_marker: Annotated[str, "Closing marker"], occurrence: Annotated[int, "Which occurrence to extract (1-based)"] = 1 ) -> dict: """Extract content between markers with positions. Returns dict with content and position info.""" if not start_marker or not end_marker: raise ValueError("Markers cannot be empty") if occurrence < 1: raise ValueError("occurrence must be >= 1") count = 0 search_start = 0 while True: start_idx = text.find(start_marker, search_start) if start_idx == -1: return { "content": None, "content_start": None, "content_end": None, "full_start": None, "full_end": None } content_start = start_idx + len(start_marker) end_idx = text.find(end_marker, content_start) if end_idx == -1: return { "content": None, "content_start": None, "content_end": None, "full_start": None, "full_end": None } count += 1 if count == occurrence: return { "content": text[content_start:end_idx], "content_start": content_start, "content_end": end_idx, "full_start": start_idx, "full_end": end_idx + len(end_marker) } search_start = content_start