export_relations_compact_v1
Export aggregated canonical relationships from academic literature to create compact knowledge graph views for analysis and management.
Instructions
导出紧凑的关系视图(按 canonical 关系聚合)。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| comm_id | No | ||
| pack_id | No |
Implementation Reference
- The main tool handler function that queries and exports compact canonical relations with evidence counts, filtered by community or pack.def export_relations_compact_v1( comm_id: int | None = None, pack_id: int | None = None, ) -> dict[str, Any]: """导出紧凑的关系视图(按 canonical 关系聚合)。""" try: where_clauses = [] params = [] if comm_id: where_clauses.append(""" EXISTS ( SELECT 1 FROM community_members cm WHERE cm.comm_id = %s AND (cm.entity_id = cr.subj_entity_id OR cm.entity_id = cr.obj_entity_id) ) """) params.append(comm_id) elif pack_id: where_clauses.append(""" EXISTS ( SELECT 1 FROM evidence_pack_items epi JOIN canonical_relation_evidence cre ON cre.chunk_id = epi.chunk_id WHERE epi.pack_id = %s AND cre.canon_rel_id = cr.canon_rel_id ) """) params.append(pack_id) where_sql = " WHERE " + " AND ".join(where_clauses) if where_clauses else "" sql = f""" SELECT cr.canon_rel_id, s.canonical_name as subj_name, cr.predicate_norm, o.canonical_name as obj_name, cr.qualifiers_norm, (SELECT COUNT(*) FROM canonical_relation_evidence cre WHERE cre.canon_rel_id = cr.canon_rel_id) as evidence_count FROM canonical_relations cr JOIN entities s ON s.entity_id = cr.subj_entity_id JOIN entities o ON o.entity_id = cr.obj_entity_id {where_sql} ORDER BY evidence_count DESC """ rows = query_all(sql, tuple(params)) return ExportRelationsCompactOut(relations=rows).model_dump() except Exception as e: return ExportRelationsCompactOut( error=MCPErrorModel(code="SYSTEM_ERROR", message=str(e)) ).model_dump()
- Pydantic input and output models for schema validation of the tool.class ExportRelationsCompactIn(BaseModel): """export_relations_compact_v1 输入""" comm_id: Optional[int] = None pack_id: Optional[int] = None class ExportRelationsCompactOut(BaseModel): """export_relations_compact_v1 输出""" relations: list[dict[str, Any]] = Field(default_factory=list) error: Optional[MCPErrorModel] = None
- src/paperlib_mcp/server.py:50-50 (registration)Registration of the graph_relation_canonicalize_tools module in the main MCP server, which includes the export_relations_compact_v1 tool.register_graph_relation_canonicalize_tools(mcp)