close_pdf
Release system resources by closing a loaded PDF file after completing redaction operations, freeing memory and preventing file lock issues.
Instructions
Close a loaded PDF and free its resources.
Args: pdf_path: Path to the PDF file to close ctx: MCP context for logging
Returns: Confirmation message
Raises: ToolError: If the PDF is not loaded
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pdf_path | Yes | Path to the PDF file to close |
Implementation Reference
- src/redact_mcp/server.py:380-421 (handler)The handler function for the 'close_pdf' tool. It closes the specified loaded PDF document, removes it from the in-memory store, clears associated redaction tracking, and logs the action. The function signature provides input validation schema via Annotated types. The @mcp.tool decorator registers it as an MCP tool.@mcp.tool async def close_pdf( pdf_path: Annotated[str, Field(description="Path to the PDF file to close")], ctx: Context = None ) -> str: """Close a loaded PDF and free its resources. Args: pdf_path: Path to the PDF file to close ctx: MCP context for logging Returns: Confirmation message Raises: ToolError: If the PDF is not loaded """ try: path = Path(pdf_path).resolve() path_str = str(path) if path_str not in _loaded_pdfs: raise ToolError(f"PDF not loaded: {path}") # Close the document _loaded_pdfs[path_str].close() del _loaded_pdfs[path_str] # Also clear redaction tracking for this PDF if path_str in _applied_redactions: del _applied_redactions[path_str] await ctx.info(f"Closed PDF: {path}") return f"Successfully closed PDF: {path}" except ToolError: raise except Exception as e: await ctx.error(f"Failed to close PDF: {str(e)}") raise ToolError(f"Failed to close PDF: {str(e)}")