Skip to main content
Glama
turambar928

MCP-based Knowledge Graph Construction System

by turambar928

build_knowledge_graph

Automatically processes text data to assess quality, enrich information, and generate structured knowledge graphs with interactive visualizations.

Instructions

全自动构建知识图谱:自动评估数据质量、补全知识、构建图谱并生成可视化

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes要处理的文本数据
output_fileNo可视化输出文件名(可选)knowledge_graph.html

Implementation Reference

  • The 'build_knowledge_graph_tool' function implementation in kg_server_enhanced.py, which serves as the handler for the 'build_knowledge_graph' MCP tool. It extracts entities and triples from the input text and saves a visualization.
    async def build_knowledge_graph_tool(arguments: dict[str, Any]) -> list[TextContent]:
        """
        构建知识图谱(不进行质量评估、知识补全或其他内容增强)
        """
        try:
            text = arguments.get("text", "")
            output_file = arguments.get("output_file", "knowledge_graph.html")
    
            if not text.strip():
                return [TextContent(
                    type="text",
                    text=json.dumps({
                        "success": False,
                        "error": "输入文本不能为空"
                    }, ensure_ascii=False, indent=2)
                )]
    
            start_time = time.time()
    
            # 直接构建知识图谱
            kg_result = await kg_builder.build_graph(text, use_llm=True)
    
            # 检查是否成功提取到实体和三元组
            if not kg_result["entities"] and not kg_result["triples"]:
                return [TextContent(
                    type="text",
                    text=json.dumps({
                        "success": False,
                        "error": "无法从输入文本中提取到有效的实体或关系",
                        "suggestion": "请尝试输入包含明确实体和关系的文本"
                    }, ensure_ascii=False, indent=2)
                )]
    
            # 生成可视化
            visualization_file = kg_visualizer.save_simple_visualization(
                kg_result["triples"],
                kg_result["entities"],
                kg_result["relations"],
                output_file
            )
    
            abs_path = os.path.abspath(visualization_file)
            visualization_url = f"file:///{abs_path.replace(os.sep, '/')}"
            http_url = f"http://localhost:8000/{visualization_file}"
            server_info = f"可手动启动HTTP服务器访问:在项目目录运行 'python -m http.server 8000',然后访问 {http_url}"
    
            processing_time = time.time() - start_time
    
            # 构建结果
            result = {
                "success": True,
                "input_text": text,
                "processing_time": round(processing_time, 3),
                "knowledge_graph": {
                    "entities_count": len(kg_result["entities"]),
                    "relations_count": len(kg_result["relations"]),
                    "triples_count": len(kg_result["triples"]),
                    "entities": kg_result["entities"],
                    "relations": kg_result["relations"]
                },
                "visualization": {
                    "file_path": visualization_file,
                    "file_url": visualization_url,
                    "http_url": http_url,
                    "server_info": server_info
                }
            }
    
            return [TextContent(
                type="text",
                text=json.dumps(result, ensure_ascii=False, indent=2)
            )]
  • Registration logic for 'build_knowledge_graph' within the server's tool handler dispatch logic in kg_server_enhanced.py.
    tools = [
        Tool(
            name="build_knowledge_graph",
            description="构建知识图谱:直接从文本提取实体与关系并生成可视化(不进行内容增强)",
            inputSchema={
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/turambar928/MCP_based_KG_construction'

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