How to build an MCP (Model Context Protocol) Tool that exposes production KPIs and historical data to AI models, enabling intelligent analysis of industrial processes.
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Tutorial style none
ProductionData
Tag.ProductionRate
Tag.Efficiency
Tag.QualityScore
Tag.DowntimeMinutes
KPICalculator
csharp
public double CalculateOEE(double availability, double performance, double quality)
{
return availability * performance * quality * 100;
}
public double GetAverageProduction(DateTime startTime, DateTime endTime)
{
// Calculate average production rate over period
double totalProduction = @Tag.TotalUnits;
double hours = (endTime - startTime).TotalHours;
return hours > 0 ? totalProduction / hours : 0;
}
public string GetProductionStatus()
{
if (@Tag.ProductionRate > 100)
return "High Performance";
else if (@Tag.ProductionRate > 80)
return "Normal";
else
return "Below Target";
}
ProductionMCPTool
csharp
[MCPMethod(Description = "Get current production KPIs")]
public object GetCurrentKPIs()
{
return new {
ProductionRate = @Tag.ProductionRate,
Efficiency = @Tag.Efficiency,
OEE = @Script.Class.KPICalculator.CalculateOEE(
@Tag.Availability, @Tag.Performance, @Tag.Quality),
Status = @Script.Class.KPICalculator.GetProductionStatus(),
Timestamp = DateTime.Now
};
}
[MCPMethod(Description = "Get production history for specified hours")]
public object GetProductionHistory(
[MCPParameter(Description = "Hours to look back")] int hours)
{
var endTime = DateTime.Now;
var startTime = endTime.AddHours(-hours);
// Query historian
var data = @Historian.Table.ProductionData.GetData(startTime, endTime);
return new {
Period = $"Last {hours} hours",
AverageRate = @Script.Class.KPICalculator.GetAverageProduction(startTime, endTime),
TotalUnits = @Tag.TotalUnits,
DataPoints = data.Rows.Count
};
}
[MCPMethod(Description = "Analyze production trend")]
public string AnalyzeProductionTrend(
[MCPParameter(Description = "Time period in hours")] int periodHours)
{
var current = @Tag.ProductionRate;
var average = @Script.Class.KPICalculator.GetAverageProduction(
DateTime.Now.AddHours(-periodHours), DateTime.Now);
if (current > average * 1.1)
return "Trending Up - Production improving";
else if (current < average * 0.9)
return "Trending Down - Requires attention";
else
return "Stable - Within normal range";
}
These tutorials provide simple, practical starting points for both MCP Tools and ML.NET integration, focusing on real industrial scenarios while keeping complexity minimal for learning purposes.
Page Tree | ||
---|---|---|
|