rlm_chunk_context
Divide massive contexts into smaller chunks using configurable strategies (lines, chars, paragraphs). Returns chunk metadata to facilitate processing of datasets exceeding standard context limits.
Instructions
Chunk a loaded context by strategy. Returns chunk metadata, not full content.
Args: name: Context identifier strategy: Chunking strategy - 'lines', 'chars', or 'paragraphs' size: Chunk size (lines/chars depending on strategy)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| strategy | No | lines | |
| size | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/rlm_mcp_server.py:1455-1468 (registration)The tool 'rlm_chunk_context' is registered as a FastMCP tool using the @mcp.tool() decorator. Defines input parameters: name (str), strategy (str, default 'lines'), size (int, default 100). Delegates to _chunk_context_impl.
@mcp.tool() async def rlm_chunk_context( name: str, strategy: str = "lines", size: int = 100, ) -> dict: """Chunk a loaded context by strategy. Returns chunk metadata, not full content. Args: name: Context identifier strategy: Chunking strategy - 'lines', 'chars', or 'paragraphs' size: Chunk size (lines/chars depending on strategy) """ return await _chunk_context_impl(name, strategy, size) - src/rlm_mcp_server.py:1423-1452 (handler)The _chunk_context_impl function contains the actual implementation logic. It ensures the context is loaded, calls _chunk_content to split it, stores chunk metadata in memory and on disk, and returns chunk metadata.
async def _chunk_context_impl( name: str, strategy: str = "lines", size: int = 100, ) -> dict: """Implementation of context chunking.""" error = _ensure_context_loaded(name) if error: return {"error": "context_not_found", "message": error} content = contexts[name]["content"] chunks = _chunk_content(content, strategy, size) chunk_meta = [{"index": i, "length": len(chunk), "preview": chunk[:100]} for i, chunk in enumerate(chunks)] contexts[name]["meta"]["chunks"] = chunk_meta contexts[name]["chunks"] = chunks chunk_dir = CHUNKS_DIR / name chunk_dir.mkdir(exist_ok=True) for i, chunk in enumerate(chunks): (chunk_dir / f"{i}.txt").write_text(chunk) return { "status": "chunked", "name": name, "strategy": strategy, "chunk_count": len(chunks), "chunks": chunk_meta, } - src/rlm_mcp_server.py:1088-1098 (helper)The _chunk_content helper function splits content into chunks using the specified strategy: 'lines' (by line count), 'chars' (by character count), or 'paragraphs' (by paragraph count).
def _chunk_content(content: str, strategy: str, size: int) -> list[str]: """Chunk content using the specified strategy.""" if strategy == "lines": lines = content.split("\n") return ["\n".join(lines[i : i + size]) for i in range(0, len(lines), size)] elif strategy == "chars": return [content[i : i + size] for i in range(0, len(content), size)] elif strategy == "paragraphs": paragraphs = re.split(r"\n\s*\n", content) return ["\n\n".join(paragraphs[i : i + size]) for i in range(0, len(paragraphs), size)] return [] - src/rlm_mcp_server.py:976-987 (helper)The _ensure_context_loaded helper ensures the named context is available in memory, loading from disk if necessary.
def _ensure_context_loaded(name: str) -> Optional[str]: """Ensure context is loaded into memory. Returns error message if not found.""" if name in contexts: return None disk_context = _load_context_from_disk(name) if disk_context: content = disk_context.pop("content") contexts[name] = {"meta": disk_context, "content": content} return None return f"Context '{name}' not found"