Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

How to build an MCP Tool.

Scripts → Tutorials → Tutorial | Concept | How-to Guide | Reference


This Tutorial Teaches you to:

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.

Prerequisites:

Scripts & Data Enrichment Tags configured for production metrics

Table of Contents
maxLevel2
minLevel2
indent10px
excludeTutorial
stylenone


Step 1:

Configure Historian

Create the MCP script

  1. Navigate to

Historian → Tables
  • Create table: ProductionData
  • Add tags:
    • Tag.ProductionRate
    • Tag.Efficiency
    • Tag.QualityScore
    • Tag.DowntimeMinutes
  • Set scan rate: 60 seconds
  • Enable storage
    1. Scripts → Classes

    2. Click in the create a New Class button

    3. In Create new Code, select MCPTool

    4. Click OK

    Step 2: Edit the Script

    In the Script code, you can have multiple methods and they follow this format:

    Code Block
    [McpServerTool, Description("<This is the question>")]
    public string <MethodName> (
    [Description("<Description of the parameter>")] <Parameters>)
    {
    	<Logic>
    
        <Return>
    }

    Example:

    Code Block
    [McpServerTool, Description("Performs concatenation of two input text values.")]
    public string Concat(
    	[Description("The first text value to be used in the operation.")] string parameter1,
    	[Description("The second text value to be used in the operation, concatenated after the first.")] string parameter2)
    {
    	return parameter1 + parameter2;
    }



    The logic can process the data and return it as a string, so the AI will receive it.

    Step 3: Configure Claude AI

    1. Have Claude AI Desktop downloaded

    2. Go in Settings → Developer → Edit Config and select the ”claude_desktop_config.json”

    3. This .json should have the following content:

      Code Block
      {
        "mcpServers": {
          "<SolutionName>": {
            "command": "<ProductPath>\\fx-10\\net8.0\\TMCPServerStdio\\TMCPServerStdio.exe",
            "args": [ "/host:127.0.0.1", "/port:<port>" ],
            "transport": "stdio"
          }
        }
      }


    4. In the Developer setting it shoud show “running” and when you open a new chat in “Search and Tools” you will see the name of your solution there.

    Step 4: Query in Claude AI

    You can query any method in a Claude chat. e.g: “What is the tag value?” and it returns the value requested.

    Besides the method you created in the script, you can ask general information about the solution, like:

    • Get tag historian

    • Get alarm online

    • Get value

    Step 2: Create KPI Calculator Class

    1. Go to Scripts → Classes
    2. Create class: KPICalculator
    3. Configure as standard Methods class

    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";
    }

    Step 3: Create MCP Tool Class

    1. Create new class: ProductionMCPTool
    2. Select MCP Tool type
    3. Add decorated methods:

    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";
    }

    Step 4: Test MCP Tool

    1. Start runtime
    2. Verify KPIs calculating
    3. Check historian data collection
    4. Test with AI assistant:
      • "What are the current production KPIs?"
      • "Show me production history for last 8 hours"
      • "Analyze the production trend"

    Next Steps

  • Advanced MCP Tools → Complex multi-tool scenarios
  • ML.NET Models → Regression and classification
  • Edge Computing → Deploy to field devices


    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.


    In this section...

    Page Tree
    root@parentspaces93DRAF