Factory Intelligence MCP Server
Provides KPI tools for factory intelligence, including productivity, quality, downtime metrics, and alarm analysis via TimescaleDB.
README
Factory Intelligence MCP Server
This is a production-ready MCP (Model Context Protocol) server providing KPI tools for a Factory Intelligence dashboard. It communicates via the Stdio transport and leverages TimescaleDB for efficient time-series analysis, calculating Productivity, Quality, Downtime metrics, and diagnosing Alarms.
Features
- Productivity KPI (
get_productivity_kpi): Computes production efficiency against targets. - Quality KPI (
get_quality_kpi): Calculates Yield % and Defect Rate %. - Downtime KPI (
get_downtime_kpi): Analyzes machine availability based on production gaps. - KPI Summary (
get_kpi_summary): Bundles all metrics for high-level dashboards. - Downtime Alarms Analysis (
get_downtime_alarms_analysis): Correlates alarms with downtime periods to identify root causes.
Setup & Installation
Prerequisites
- Python 3.10+
uv(recommended) orpip- A running PostgreSQL/TimescaleDB instance with the factory schema.
1. Installation
git clone https://github.com/lvshrd/Factory-Intelligence-MCP-Server.git
cd Factory-Intelligence-MCP-Server
uv sync # Installs dependencies including mcp, psycopg2, python-dateutil
2. Configuration
The server requires a DATABASE_URL environment variable. You have two options:
Option A: .env file (Recommended for local dev)
Create a .env file in the Factory-Intelligence-MCP-Server directory:
DATABASE_URL="postgresql://username:password@localhost:5432/ProductionDB"
Option B: Environment Variable Injection
Pass the DATABASE_URL directly through your MCP client configuration (see below).
Integration Guide
1. Using with Claude Desktop / Cursor
You can configure this server in Claude Desktop or Cursor's MCP settings.
Add this to your claude_desktop_config.json (or Cursor's MCP settings):
{
"mcpServers": {
"factory-intelligence": {
"command": "uv",
"args": [
"--directory",
"/ABSOLUTE/PATH/TO/Factory-Intelligence-MCP-Server",
"run",
"server.py"
],
"env": {
"DATABASE_URL": "postgresql://username:password@localhost:5432/ProductionDB"
}
}
}
}
<img src="assets/mcp%20server%20loaded.png" alt="MCP Server Loaded" width="500" class="center"/>
2. Using with LangGraph / LangChain (Python)
To integrate this server programmatically using the official LangChain MCP client:
from langchain_mcp_adapters.client import MultiServerMCPClient
# Initialize client with Stdio transport
client = MultiServerMCPClient(
{
"factory-intelligence": {
"transport": "stdio",
"command": "uv",
"args": [
"--directory",
"/ABSOLUTE/PATH/TO/Factory-Intelligence-MCP-Server",
"run",
"server.py"
],
"env": {
"DATABASE_URL": "postgresql://username:password@localhost:5432/ProductionDB"
}
}
}
)
Tool Definitions & Schemas
All tools share a common input structure requiring start_time and end_time.
1. get_productivity_kpi
Computes productivity metrics based on total good and bad bottles produced versus a target.
- Inputs:
start_time(string, ISO 8601)end_time(string, ISO 8601)
- Outputs:
summary: Object containingvalue(ratio),total_production,good_count,bad_count.timeseries: Array of{ timestamp, value }objects.metadata: Info on data source and computation notes.
2. get_quality_kpi
Computes Quality (Yield %) and Defect Rate %.
- Inputs:
start_time,end_time(ISO 8601) - Outputs:
summary:yield_percentage,defect_rate_percentage.timeseries: Trend of Yield % over time.
3. get_downtime_kpi
Calculates uptime and downtime duration based on production gaps (zero production intervals).
- Inputs:
start_time,end_time(ISO 8601) - Outputs:
summary:uptime_seconds,downtime_seconds,availability_percentage.
4. get_kpi_summary
Bundles Productivity, Quality, and Downtime KPIs into a single response.
- Inputs:
start_time,end_time(ISO 8601) - Outputs:
productivity: Summary object from Tool 1.quality: Summary object from Tool 2.downtime: Summary object from Tool 3.
5. get_downtime_alarms_analysis
Identifies and ranks alarms that were active during inferred downtime periods.
- Inputs:
start_time,end_time(ISO 8601) - Outputs:
summary: Total downtime events and top alarm count.top_alarms: List of alarms withfrequencyandtotal_duration_during_downtime.downtime_events_sample: List of specific downtime windows (start,end,duration).
Example Tool Calls & Outputs
AI Agent Usage Example
Below is a demonstration of an AI agent (Cursor) calling the tools to analyze productivity and downtime root causes:
<img src="assets/screenshot.png" alt="Agent Usage Demo" width="300"/>
Request (Client -> Server)
Calling get_productivity_kpi for a single day:
{
"name": "get_productivity_kpi",
"arguments": {
"start_time": "2025-12-10T00:00:00Z",
"end_time": "2025-12-10T23:59:59Z"
}
}
Response (Server -> Client)
Note: The result field contains the actual tool payload.
{
"tool": "get_productivity_kpi",
"inputs": {
"start_time": "2025-12-10T00:00:00Z",
"end_time": "2025-12-10T23:59:59Z"
},
"result": {
"summary": {
"kpi_name": "Productivity",
"value": 0.2019,
"total_production": 54074.0,
"good_count": 53473.0,
"bad_count": 601.0,
"unit": "ratio"
},
"timeseries": [
{
"timestamp": "2025-12-10T00:00:00+00:00",
"value": 54074.0
}
],
"metadata": {
"data_source": "agg_counter_1hour",
"bucket_width": "1 day",
"computation_note": "Target based on max observed speed (11160 BPH)"
}
},
"status": "ok",
"errors": []
}
Engineering Design Notes
1. Why specific tables were used?
agg_counter_10sec_delta(The Source of Truth): Used for precise logic like Downtime Inference. Its delta-based structure allows us to accurately determine "zero production" intervals at a high resolution (10 seconds).agg_counter_1min/agg_counter_1hour(Performance): Used for KPI calculations over longer time ranges. Querying pre-aggregated data reduces the number of rows scanned by orders of magnitude (e.g., 1 year of 1-hour data is ~8,760 rows, vs ~3.1 million rows for 10-second data).agg_boolean_state_durations: Used for Alarm analysis because it natively stores state intervals (start,end,value), making overlap queries significantly easier than reconstructing states from raw timeseries events.
2. Assumptions Made
- Downtime Inference: We assume Zero Production = Downtime. Any 10-second bucket with
sum(delta) = 0is treated as a stop. - Target Production: Calculated dynamically using a "Design Speed" of 11,160 Bottles Per Hour. This rate was derived from analyzing the historical data to find the maximum observed production in a single 10-second interval (31 bottles), ensuring the productivity ratio is relative to the machine's demonstrated peak capacity.
- Alarm Correlation: We assume that if an alarm is active (
value=true) and its time interval overlaps with a downtime event, it is related to that downtime.
3. Performance Considerations
- Dynamic Aggregation Strategy: The system implements an intelligent router (
get_aggregation_strategy) that selects the optimal table based on query duration:< 10 mins->agg_counter_1min(High detail)< 30 mins->agg_counter_30min(Medium detail)< 12 hours->agg_counter_1hour(Balanced)> 12 hours->agg_counter_1hour(Aggregated to Daily buckets on-the-fly)
- SQL-Side Computation: Heavy logic is pushed to the database.
- Downtime: Instead of fetching millions of rows to Python, we use SQL CTEs and
COUNT(*) FILTERto calculate uptime/downtime seconds instantly. - Alarm Analysis: We use "Gaps and Islands" logic (using
ROW_NUMBER()) inside the database to merge continuous zero-production buckets into downtime events, preventing data explosion in the application layer.
- Downtime: Instead of fetching millions of rows to Python, we use SQL CTEs and
Testing
Run the included verification script to see all tools in action:
uv run test_kpi_service.py
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。