Skip to main content
Glama

clear_graph

Remove GraphRAG data from Paperlib MCP to manage storage and refresh knowledge graphs. Delete data for specific documents or perform complete cleanup when needed.

Instructions

清理 GraphRAG 数据

清理指定文档或全部的 GraphRAG 数据。

Args: doc_id: 文档 ID(清理单个文档) clear_all: 是否清理全部(危险操作)

Returns: 清理结果,包含删除的记录数

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
doc_idNo
clear_allNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function for the 'clear_graph' tool. It deletes GraphRAG-related data from the database for a specific document (if doc_id provided) or all data (if clear_all=True). Uses ClearGraphOut model for response and handles errors.
    @mcp.tool()
    def clear_graph(doc_id: str | None = None, clear_all: bool = False) -> dict[str, Any]:
        """清理 GraphRAG 数据
        
        清理指定文档或全部的 GraphRAG 数据。
        
        Args:
            doc_id: 文档 ID(清理单个文档)
            clear_all: 是否清理全部(危险操作)
            
        Returns:
            清理结果,包含删除的记录数
        """
        try:
            if not doc_id and not clear_all:
                return ClearGraphOut(
                    ok=False,
                    error=MCPErrorModel(code="VALIDATION_ERROR", message="Must provide doc_id or set clear_all=True"),
                ).model_dump()
            
            deleted_counts = {}
            
            with get_db() as conn:
                with conn.cursor() as cur:
                    if clear_all:
                        # 按顺序清理(先清理依赖表)
                        cur.execute("DELETE FROM community_summaries")
                        deleted_counts["community_summaries"] = cur.rowcount
                        
                        cur.execute("DELETE FROM community_members")
                        deleted_counts["community_members"] = cur.rowcount
                        
                        cur.execute("DELETE FROM communities")
                        deleted_counts["communities"] = cur.rowcount
                        
                        cur.execute("DELETE FROM claims")
                        deleted_counts["claims"] = cur.rowcount
                        
                        cur.execute("DELETE FROM relations")
                        deleted_counts["relations"] = cur.rowcount
                        
                        cur.execute("DELETE FROM mentions")
                        deleted_counts["mentions"] = cur.rowcount
                        
                        cur.execute("DELETE FROM entity_aliases")
                        deleted_counts["entity_aliases"] = cur.rowcount
                        
                        cur.execute("DELETE FROM entities")
                        deleted_counts["entities"] = cur.rowcount
                        
                        cur.execute("DELETE FROM entity_merge_log")
                        deleted_counts["entity_merge_log"] = cur.rowcount
                    else:
                        # 清理单个文档
                        # 删除 claims
                        cur.execute("DELETE FROM claims WHERE doc_id = %s", (doc_id,))
                        deleted_counts["claims"] = cur.rowcount
                        
                        # 删除 mentions
                        cur.execute("DELETE FROM mentions WHERE doc_id = %s", (doc_id,))
                        deleted_counts["mentions"] = cur.rowcount
                        
                        # 删除 relations(通过 evidence)
                        cur.execute(
                            "DELETE FROM relations WHERE evidence->>'doc_id' = %s",
                            (doc_id,)
                        )
                        deleted_counts["relations"] = cur.rowcount
                        
                        # 可选:删除 Paper entity
                        # cur.execute(
                        #     "DELETE FROM entities WHERE type = 'Paper' AND canonical_key = %s",
                        #     (doc_id,)
                        # )
                        # deleted_counts["paper_entity"] = cur.rowcount
            
            return ClearGraphOut(
                ok=True,
                deleted_counts=deleted_counts,
            ).model_dump()
            
        except Exception as e:
            return ClearGraphOut(
                ok=False,
                error=MCPErrorModel(code="DB_CONN_ERROR", message=str(e)),
            ).model_dump()
  • Pydantic schemas for clear_graph tool: ClearGraphIn defines optional doc_id and clear_all parameters; ClearGraphOut defines response with success flag, deleted counts, and optional error.
    class ClearGraphIn(BaseModel):
        """clear_graph 输入"""
        doc_id: Optional[str] = None
        clear_all: bool = False
    
    
    class ClearGraphOut(BaseModel):
        """clear_graph 输出"""
        ok: bool
        deleted_counts: Optional[dict[str, int]] = None
        error: Optional[MCPErrorModel] = None
  • The registration point where register_graph_maintenance_tools is called on the MCP instance, which in turn defines and registers the clear_graph tool using @mcp.tool() decorator.
    register_graph_maintenance_tools(mcp)
  • The function that defines and registers all graph maintenance tools, including clear_graph, by using @mcp.tool() decorators inside it.
    def register_graph_maintenance_tools(mcp: FastMCP) -> None:
        """注册 GraphRAG 维护工具"""
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden. It discloses that clear_all is a '危险操作' (dangerous operation), which is valuable behavioral context about potential data loss. However, it doesn't mention permissions required, whether the operation is reversible, rate limits, or what happens to related data structures. The description adds some behavioral insight but leaves significant gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized with clear sections: purpose statement, parameter explanations, and return value description. It's front-loaded with the main purpose, though the Args/Returns formatting could be more integrated. Every sentence adds value, with no redundant information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a destructive tool with no annotations, the description provides good coverage: clear purpose, parameter semantics, warning about dangerous operations, and return value information. The presence of an output schema means the description doesn't need to detail return values. However, it could better address permissions, reversibility, and relationships to sibling tools.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description provides essential semantic context for both parameters: doc_id is for cleaning a single document, and clear_all is a dangerous operation for cleaning everything. This compensates well for the lack of schema descriptions, though it doesn't specify format requirements for doc_id or clarify the mutual exclusivity of the parameters.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '清理 GraphRAG 数据' (clean GraphRAG data) with options for cleaning specific documents or all data. It uses a specific verb ('清理' - clean/clear) and resource ('GraphRAG 数据'), though it doesn't explicitly differentiate from sibling tools like 'delete_document' or 'rechunk_document'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through the parameter descriptions: 'doc_id: 文档 ID(清理单个文档)' and 'clear_all: 是否清理全部(危险操作)'. It suggests using doc_id for single document cleanup and warns that clear_all is dangerous, but doesn't provide explicit guidance on when to choose this tool over alternatives like 'delete_document' or 'rechunk_document'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/h-lu/paperlib-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server