教程

AI 副驾驶:TIA Portal 的 MCP 工具

让 AI 代理查看您的项目、了解您的变量并操控您的 PLC — 通过 31 个专用工具。

T
T-IA Connect 团队
15 分钟阅读
更新于 2026 年 3 月 11 日

为什么 MCP 改变一切

经典 AI 助手(ChatGPT、Copilot)在工业自动化中存在一个重大问题:上下文幻觉。它们生成语法正确的 SCL 代码,但使用不存在的变量、错误的数据类型,或忽略您的硬件配置。借助 T-IA Connect 内置的 MCP 服务器,AI 可以查看您的项目、读取您的标签表、检查现有块 — 并真正在 TIA Portal 中创建或修改代码。

前提条件

  • T-IA Connect 已安装并运行
  • 已打开 TIA Portal 项目(V16–V21)
  • 兼容 MCP 的 AI 客户端(Claude Desktop 或任何 MCP 客户端)

设置:将您的 AI 连接到 TIA Portal

在 Claude Desktop 配置文件(claude_desktop_config.json)中将 T-IA Connect 添加为 MCP 服务器:

claude_desktop_config.json
{
  "mcpServers": {
    "tia-connect": {
      "command": "C:\\Program Files\\FeelAutomCorp\\TiaConnect\\TiaPortalApi.McpBridge.exe",
      "args": [],
      "env": {
        "TIA_CONNECT_URL": "http://localhost:9000",
        "TIA_CONNECT_API_KEY": "your-api-key"
      }
    }
  }
}

McpBridge.exe 充当 stdio 到 HTTP 的桥梁。它将 AI 请求转发到 T-IA Connect API 服务器,该服务器通过 Openness 与 TIA Portal 通信。

示例 1:检查项目状态

最简单的工具 — 验证与 TIA Portal 的连接并查看当前打开的项目。

MCP Tool: get_project_status
{
  "name": "get_project_status",
  "arguments": {}
}
Response
{
  "connected": true,
  "projectName": "WaterTreatment_V19",
  "projectPath": "C:\TIA_Projects\WaterTreatment_V19",
  "tiaVersion": "V19",
  "modified": false
}

示例 2:打开项目

直接从 AI 打开 TIA Portal 项目文件。您还可以使用 list_project_files 扫描磁盘上的可用项目。

MCP Tool: open_project
{
  "name": "open_project",
  "arguments": {
    "path": "C:\TIA_Projects\WaterTreatment_V19\WaterTreatment_V19.ap19"
  }
}
Response
{
  "success": true,
  "projectName": "WaterTreatment_V19",
  "version": "V19"
}

示例 3:列出设备

查看当前项目中的所有 PLC、HMI 和其他设备。

MCP Tool: list_devices
{
  "name": "list_devices",
  "arguments": {}
}
Response
[
  {
    "name": "PLC_1",
    "type": "CPU 1516-3 PN/DP",
    "category": "PLC"
  },
  {
    "name": "HMI_1",
    "type": "TP1500 Comfort",
    "category": "HMI"
  }
]

示例 4:列出和读取块

浏览 PLC 中的所有块(FB、FC、DB、OB),然后读取任意块的完整代码和接口。

MCP Tool: list_blocks → get_block_details
// Step 1: List all blocks
{ "name": "list_blocks", "arguments": { "deviceName": "PLC_1" } }

// Step 2: Read a specific block
{ "name": "get_block_details", "arguments": {
    "deviceName": "PLC_1",
    "blockName": "FB_MotorControl"
  }
}
Response
// list_blocks response
[
  { "name": "Main [OB1]", "type": "OB", "language": "LAD", "number": 1 },
  { "name": "FB_MotorControl", "type": "FB", "language": "SCL", "number": 1 },
  { "name": "DB_Config", "type": "DB", "language": "DB", "number": 1 }
]

// get_block_details response
{
  "name": "FB_MotorControl",
  "type": "FB",
  "language": "SCL",
  "interface": {
    "input": [
      { "name": "Start", "type": "Bool" },
      { "name": "Stop", "type": "Bool" },
      { "name": "SpeedSetpoint", "type": "Real" }
    ],
    "output": [
      { "name": "Running", "type": "Bool" },
      { "name": "Speed", "type": "Real" }
    ]
  },
  "code": "#Running := #Start AND NOT #Stop;\nIF #Running THEN\n  #Speed := #SpeedSetpoint;\nEND_IF;"
}

示例 5:创建 SCL 块

最强大的工具 — 创建带有 SCL 代码、类型化变量和完整接口定义的功能块。AI 可以根据您用自然语言描述的需求生成此代码。

MCP Tool: create_scl_block
{
  "name": "create_scl_block",
  "arguments": {
    "deviceName": "PLC_1",
    "blockName": "FB_ValveControl",
    "blockType": "FB",
    "interface": {
      "input": [
        { "name": "Open", "type": "Bool" },
        { "name": "Close", "type": "Bool" },
        { "name": "Timeout", "type": "Time", "defaultValue": "T#5s" }
      ],
      "output": [
        { "name": "IsOpen", "type": "Bool" },
        { "name": "IsClosed", "type": "Bool" },
        { "name": "Error", "type": "Bool" }
      ]
    },
    "sclCode": "IF #Open AND NOT #Close THEN\n  #IsOpen := TRUE;\n  #IsClosed := FALSE;\nELSIF #Close THEN\n  #IsOpen := FALSE;\n  #IsClosed := TRUE;\nEND_IF;"
  }
}
Response
{
  "name": "FB_ValveControl",
  "type": "FB",
  "number": 2,
  "created": true
}

示例 6:管理标签

列出标签表、浏览变量,并创建具有正确地址和数据类型的新标签。

MCP Tools: list_tags → create_tag
// List existing tags
{ "name": "list_tags", "arguments": {
    "deviceName": "PLC_1",
    "tableName": "Motors"
  }
}

// Create a new tag
{ "name": "create_tag", "arguments": {
    "deviceName": "PLC_1",
    "tableName": "Motors",
    "name": "Motor2_Start",
    "dataType": "Bool",
    "logicalAddress": "%I0.2"
  }
}
Response
// list_tags response
[
  { "name": "Motor1_Start", "dataType": "Bool", "address": "%I0.0" },
  { "name": "Motor1_Stop", "dataType": "Bool", "address": "%I0.1" },
  { "name": "Motor1_Running", "dataType": "Bool", "address": "%Q0.0" }
]

// create_tag response
{
  "name": "Motor2_Start",
  "dataType": "Bool",
  "address": "%I0.2",
  "created": true
}

示例 7:编译

触发设备编译以验证生成的代码。错误和警告会返回,以便 AI 自动修复问题。

MCP Tool: compile_device
{
  "name": "compile_device",
  "arguments": { "deviceName": "PLC_1" }
}
Response
{
  "success": true,
  "errors": [],
  "warnings": [
    "FB_ValveControl: Variable 'Timeout' declared but not used"
  ],
  "warningCount": 1,
  "errorCount": 0
}

示例 8:搜索硬件目录

搜索西门子硬件目录并将设备添加到您的项目中 — 全部通过自然语言完成。

MCP Tools: search_hardware_catalog → add_device
// Search the catalog
{ "name": "search_hardware_catalog", "arguments": {
    "query": "CPU 1511"
  }
}

// Add the device
{ "name": "add_device", "arguments": {
    "deviceName": "PLC_2",
    "typeIdentifier": "OrderNumber:6ES7 511-1AK02-0AB0/V2.9"
  }
}
Response
// search_hardware_catalog response
[
  {
    "name": "CPU 1511C-1 PN",
    "typeIdentifier": "OrderNumber:6ES7 511-1CK01-0AB0/V2.9",
    "version": "V2.9"
  },
  {
    "name": "CPU 1511-1 PN",
    "typeIdentifier": "OrderNumber:6ES7 511-1AK02-0AB0/V2.9",
    "version": "V2.9"
  }
]

// add_device response
{
  "name": "PLC_2",
  "type": "CPU 1511-1 PN",
  "added": true
}

示例 9:HMI 画面

列出、创建和检查 WinCC HMI 画面。获取完整的 XML 设计用于分析。

MCP Tools: list_hmi_screens → create_hmi_screen
// List screens
{ "name": "list_hmi_screens", "arguments": {
    "deviceName": "HMI_1"
  }
}

// Create a new screen
{ "name": "create_hmi_screen", "arguments": {
    "deviceName": "HMI_1",
    "screenName": "MotorOverview"
  }
}
Response
// list_hmi_screens response
[
  { "name": "MainScreen", "type": "Screen" },
  { "name": "AlarmView", "type": "Screen" }
]

// create_hmi_screen response
{
  "name": "MotorOverview",
  "created": true
}

示例 10:保存、归档和关闭

完整的项目生命周期管理 — 保存您的工作、创建备份存档并正确关闭。

MCP Tools: save → archive → close
{ "name": "save_project", "arguments": {} }

{ "name": "archive_project", "arguments": {
    "targetDirectory": "C:\TIA_Backups",
    "archiveName": "WaterTreatment_backup_2026-03-11"
  }
}

{ "name": "close_project", "arguments": {} }
Response
{ "saved": true }

{
  "archivePath": "C:\TIA_Backups\WaterTreatment_backup_2026-03-11.zap19",
  "size": "45.2 MB",
  "created": true
}

{ "closed": true }

完整工具参考(31 个工具)

以下是 T-IA Connect 中所有可用的 MCP 工具完整列表,按类别组织。

项目管理

ToolDescriptionParameters
get_project_statusGet current project status
list_project_filesScan disk for TIA projectspath?
open_projectOpen a .ap* project filepath
create_projectCreate an empty TIA projectpath, name
close_projectClose the active project
save_projectSave the current project
archive_projectCreate a .zap backuptargetDirectory, archiveName
retrieve_projectRestore from .zap backuparchivePath, targetDirectory

硬件与网络

ToolDescriptionParameters
list_devicesList all devices (PLC, HMI)
search_hardware_catalogSearch Siemens hardware catalogquery
add_deviceAdd a device to the projectdeviceName, typeIdentifier
delete_deviceRemove a devicedeviceName
rename_deviceRename a devicecurrentName, newName
configure_networkSet IP / subnet / gatewaydeviceName, ipAddress, subnetMask, gateway

编程

ToolDescriptionParameters
list_blocksList all blocks (FB, FC, DB, OB)deviceName
get_block_detailsRead block interface and codedeviceName, blockName
create_scl_blockCreate SCL block with interfacedeviceName, blockName, sclCode, blockType, interface
delete_blockDelete a blockdeviceName, blockName
rename_blockRename a blockdeviceName, currentName, newName
compile_deviceCompile device codedeviceName

HMI (WinCC)

ToolDescriptionParameters
list_hmi_screensList WinCC screensdeviceName
create_hmi_screenCreate an HMI screendeviceName, screenName
delete_hmi_screenDelete an HMI screendeviceName, screenName
get_hmi_screen_xmlGet screen XML designdeviceName, screenName

数据与变量

ToolDescriptionParameters
list_tag_tablesList tag tablesdeviceName
create_tag_tableCreate a tag tabledeviceName, tableName
delete_tag_tableDelete a tag tabledeviceName, tableName
list_tagsList variables in a tabledeviceName, tableName
create_tagCreate a variable (I, Q, M)deviceName, tableName, name, dataType, logicalAddress
delete_tagDelete a variabledeviceName, tableName, tagName

系统诊断

ToolDescriptionParameters
get_server_diagnosticsAPI & TIA Portal health check

附加:FDS 工具(设计规范)

T-IA Connect 还包含 5 个额外的 MCP 工具,用于 FDS(功能设计规范)模块。详情请参阅专用教程。

从 CSV 生成 SCL 代码指南

AI + PLC = 工程的未来

借助 31 个 MCP 工具,您的 AI 助手成为真正的工程伙伴 — 它读取您的项目、理解您的硬件,并编写能够实际编译的代码。不再有虚构的变量或盲目的代码生成。

开始使用 T-IA Connect,变革您的 TIA Portal 工作流程。