manufacturing-defect-mcp
README.md
# Explainable Manufacturing Defect Investigation Using Knowledge Graphs, Neo4j GDS, MCP, and Agentic AI
A manufacturing investigation platform that combines Computer Vision outputs, Knowledge Graphs, Graph Data Science (GDS), Model Context Protocol (MCP), and Local LLM Agents to support defect analysis and engineering investigations.
The system extends traditional defect detection by providing contextual information, graph analytics, graph-based machine learning, and natural language access to manufacturing knowledge.
---
# Overview
Traditional defect detection systems answer:
> Is the product defective?
Quality engineers typically require additional context:
* Which machine produced it?
* Which supplier provided the materials?
* Have similar defects occurred before?
* Are there recurring defect patterns?
* What actions should be investigated?
This project combines manufacturing telemetry, traceability data, graph analytics, and LLM reasoning to generate explainable investigation reports.
---
# System Architecture
```text
AI4I Telemetry Dataset
+
MVTec Transistor Dataset
+
Synthetic Traceability Data
↓
Data Integration Pipeline
↓
Manufacturing Dataset
↓
Neo4j Knowledge Graph
↓
Neo4j Graph Data Science
↓
FastAPI + MCP Server
↓
LLM Investigation Agent
↓
Investigation Report
```
---
# Core Components
## Knowledge Graph
Manufacturing entities are represented as graph nodes.
### Node Types
* ProductCase
* Machine
* Supplier
* Batch
* MaterialLot
* Operator
* Shift
* ProductionLine
* Defect
* FailureType
* Document
### Relationships
```cypher
(ProductCase)-[:PRODUCED_BY]->(Machine)
(ProductCase)-[:SUPPLIED_BY]->(Supplier)
(ProductCase)-[:BELONGS_TO_BATCH]->(Batch)
(ProductCase)-[:HAS_DEFECT]->(Defect)
(ProductCase)-[:EXHIBITS_FAILURE]->(FailureType)
(ProductCase)-[:WORKED_IN_SHIFT]->(Shift)
(ProductCase)-[:MENTIONED_IN]->(Document)
```
---
# Graph Analytics
Implemented using Neo4j Graph Data Science.
## PageRank Centrality
Identifies machines that are most influential within the manufacturing network.
Endpoint:
```http
GET /analytics/centrality
```
Example Insight:
```text
Machine M5 exhibits the highest centrality score.
```
---
## Louvain Community Detection
Groups machines and suppliers with similar manufacturing behavior.
Endpoint:
```http
GET /analytics/communities
```
Example Insight:
```text
Machines and suppliers within the same community
share similar defect patterns.
```
---
# Graph Machine Learning
## Node Classification
Uses Neo4j GDS Node Classification Pipeline.
Workflow:
```text
ProductCase Nodes
↓
FastRP Embeddings
↓
Logistic Regression
↓
Defect Classification
```
Training Endpoint:
```http
POST /ml/train
```
Prediction Endpoint:
```http
GET /ml/predict/{uid}
```
---
## FastRP Embeddings
Generates graph embeddings for ProductCase nodes.
Endpoint:
```http
POST /ml/embeddings
```
Embedding Size:
```text
64 dimensions
```
Stored as:
```cypher
pc.embedding
```
---
## Similar Case Retrieval
Uses cosine similarity between FastRP embeddings.
Example:
```cypher
MATCH (target:ProductCase)
MATCH (other:ProductCase)
WITH target, other,
gds.similarity.cosine(
target.embedding,
other.embedding
) AS similarity
```
Endpoint:
```http
GET /ml/similar/{uid}
```
Purpose:
* Historical defect comparison
* Similar case retrieval
* Context-aware investigations
---
# Text-to-Cypher
Natural language queries are converted into Cypher using a local LLM.
Example:
```text
Which machines have the highest defect rates?
```
Generated Query:
```cypher
MATCH ...
RETURN ...
```
Endpoint:
```http
POST /text-to-cypher
```
---
# Graph Builder
Documents can be ingested into the Knowledge Graph.
Supported Examples:
* Maintenance logs
* SOP documents
* Engineering reports
Endpoint:
```http
POST /ingest-document
```
Generated Knowledge:
```text
Machine
↓
MENTIONED_IN
↓
Document
```
---
# MCP Investigation Agent
The MCP server exposes graph operations as tools.
Examples:
* get_case_context
* find_similar_cases
* calculate_machine_centrality
* detect_defect_communities
* text_to_cypher
The LLM agent invokes these tools to collect evidence and generate investigation reports.
---
# Example Investigation Workflow
```text
UID000001
↓
Retrieve Context
↓
Find Similar Cases
↓
Analyze Graph Patterns
↓
Collect Evidence
↓
Generate Investigation Report
```
Example Report Sections:
* Case Summary
* Evidence
* Hypothesis
* Recommendations
---
# REST API
## Data
```http
POST /sync
```
---
## Investigation
```http
POST /investigate/{uid}
```
---
## Graph Analytics
```http
GET /analytics/centrality
GET /analytics/communities
```
---
## Graph Machine Learning
```http
POST /ml/train
POST /ml/embeddings
GET /ml/predict/{uid}
GET /ml/similar/{uid}
```
---
## Knowledge Graph Querying
```http
POST /text-to-cypher
```
---
## Graph Builder
```http
POST /ingest-document
```
---
# Technology Stack
* FastAPI
* Neo4j
* Neo4j Graph Data Science
* MCP
* Ollama
* Qwen 2.5
* Python
* Docker
* PatchCore
* OpenCV
---
# Project Goal
This project demonstrates how Knowledge Graphs, Graph Analytics, Graph Machine Learning, MCP, and LLM Agents can be combined to transform manufacturing defect detection into explainable manufacturing investigations.
This server cannot be deployed
Maintenance
ActivityInactive
ResponsivenessNo issues