rlm_filter_context
Apply regex filtering to a context, keeping or removing lines that match a pattern, to create a new filtered subset for targeted analysis.
Instructions
Filter context using regex/string operations. Creates a new filtered context.
Args: name: Source context identifier output_name: Name for filtered context pattern: Regex pattern to match mode: 'keep' or 'remove' matching lines
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| output_name | Yes | ||
| pattern | Yes | ||
| mode | No | keep |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/rlm_mcp_server.py:1499-1546 (handler)The main handler for the rlm_filter_context tool. Loads a context by name, applies a regex pattern to filter lines in 'keep' or 'remove' mode, creates a new filtered context in memory and on disk, and returns metadata about the filtering operation.
@mcp.tool() async def rlm_filter_context( name: str, output_name: str, pattern: str, mode: str = "keep", ) -> dict: """Filter context using regex/string operations. Creates a new filtered context. Args: name: Source context identifier output_name: Name for filtered context pattern: Regex pattern to match mode: 'keep' or 'remove' matching lines """ error = _ensure_context_loaded(name) if error: return {"error": "context_not_found", "message": error} content = contexts[name]["content"] lines = content.split("\n") regex = re.compile(pattern) if mode == "keep": filtered = [line for line in lines if regex.search(line)] else: filtered = [line for line in lines if not regex.search(line)] new_content = "\n".join(filtered) meta = _context_summary( output_name, new_content, hash=_hash_content(new_content), source=name, filter_pattern=pattern, filter_mode=mode, chunks=None, ) contexts[output_name] = {"meta": meta, "content": new_content} _save_context_to_disk(output_name, new_content, meta) return { "status": "filtered", "name": output_name, "original_lines": len(lines), "filtered_lines": len(filtered), "length": len(new_content), } - src/rlm_mcp_server.py:1499-1513 (registration)Registration of rlm_filter_context as a FastMCP tool via the @mcp.tool() decorator on line 1499.
@mcp.tool() async def rlm_filter_context( name: str, output_name: str, pattern: str, mode: str = "keep", ) -> dict: """Filter context using regex/string operations. Creates a new filtered context. Args: name: Source context identifier output_name: Name for filtered context pattern: Regex pattern to match mode: 'keep' or 'remove' matching lines """ - src/rlm_mcp_server.py:1500-1546 (handler)The complete handler function for the rlm_filter_context tool (same as registration since it uses @mcp.tool decorator).
async def rlm_filter_context( name: str, output_name: str, pattern: str, mode: str = "keep", ) -> dict: """Filter context using regex/string operations. Creates a new filtered context. Args: name: Source context identifier output_name: Name for filtered context pattern: Regex pattern to match mode: 'keep' or 'remove' matching lines """ error = _ensure_context_loaded(name) if error: return {"error": "context_not_found", "message": error} content = contexts[name]["content"] lines = content.split("\n") regex = re.compile(pattern) if mode == "keep": filtered = [line for line in lines if regex.search(line)] else: filtered = [line for line in lines if not regex.search(line)] new_content = "\n".join(filtered) meta = _context_summary( output_name, new_content, hash=_hash_content(new_content), source=name, filter_pattern=pattern, filter_mode=mode, chunks=None, ) contexts[output_name] = {"meta": meta, "content": new_content} _save_context_to_disk(output_name, new_content, meta) return { "status": "filtered", "name": output_name, "original_lines": len(lines), "filtered_lines": len(filtered), "length": len(new_content), } - src/rlm_mcp_server.py:976-987 (helper)Helper function called by rlm_filter_context to ensure the source context is loaded into memory before filtering.
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" - src/rlm_mcp_server.py:990-998 (helper)Helper function used by rlm_filter_context to build metadata for the new filtered context.
def _context_summary(name: str, content: str, **extra: Any) -> dict: """Build a common context summary dict.""" summary = { "name": name, "length": len(content), "lines": content.count("\n") + 1, } summary.update(extra) return summary