Get Element Context
get_contextRetrieve a Markdown element with its surrounding context to understand document structure and relationships between sections.
Instructions
Returns the target element along with its immediate neighbors (before and after).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| path | Yes | Path to the element (e.g., 'Intro > paragraph 1') |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| after | No | Element after target | |
| before | No | Element before target | |
| target | No | The target element |
Implementation Reference
- Core logic that retrieves the target element by path and constructs context with preceding and following sibling elements.
def get_context(self, path: str) -> Dict[str, Any]: """Get element and its neighbors""" target = self.find_by_path(path) if not target: return {"error": "Not found"} siblings = target.parent.children if target.parent else self.elements idx = siblings.index(target) context = { "current": {"path": target.path, "content": target.content}, "before": None, "after": None, } if idx > 0: context["before"] = { "path": siblings[idx - 1].path, "content": siblings[idx - 1].content[:200], } if idx < len(siblings) - 1: context["after"] = { "path": siblings[idx + 1].path, "content": siblings[idx + 1].content[:200], } return context - EditTool instance method that caches/loads the Document and calls its get_context method.
async def get_context(self, file_path: str, path: str) -> Dict[str, Any]: doc = self.get_doc(file_path) return doc.get_context(path) - Top-level async handler function (get_element_context) imported and called by server.py for the 'get_context' tool.
async def get_element_context(file_path: str, path: str): return await _instance.get_context(file_path, path) - JSON schema definition for input (file_path, path) and output (target, before, after objects) of the get_context tool.
Tool( name="get_context", title="Get Element Context", description="Returns the target element along with its immediate neighbors (before and after).", inputSchema={ "type": "object", "properties": { "file_path": {"type": "string", "examples": ["./document.md"]}, "path": { "type": "string", "description": "Path to the element (e.g., 'Intro > paragraph 1')", "examples": ["Introduction > paragraph 2", "Conclusion"], }, }, "required": ["file_path", "path"], "additionalProperties": False, }, outputSchema={ "type": "object", "properties": { "target": {"type": "object", "description": "The target element"}, "before": { "type": "object", "description": "Element before target", }, "after": {"type": "object", "description": "Element after target"}, }, }, ), - src/markdown_editor/server.py:575-581 (registration)Registration and dispatch logic in the MCP server's call_tool handler that routes 'get_context' calls to the get_element_context function.
elif name == "get_context": res = await get_element_context(file_path, arguments["path"]) return CallToolResult( content=[TextContent(type="text", text=json.dumps(res, ensure_ascii=False, indent=2))], structuredContent=res, isError="error" in res, )