plcsharp/PLCSharp.Mcp/ProjectTools.cs

79 lines
7.9 KiB
C#

using System.ComponentModel;
using ModelContextProtocol.Server;
using System.Text.Json.Nodes;
namespace PLCSharp.Mcp;
[McpServerToolType]
internal sealed class ProjectTools(ProjectGateway gateway)
{
[McpServerTool(Name = "preview_object_edit"), Description("Preview create, update or delete for Recipes, Workflows, Variables, SystemVariables, AxisPoints, RobotPoints, Matrices, RobotMatrices, InterpolationGroups, VisionFunctions, ImageDatas, LocalImageDatas, CanvasConfigs, CustomControls or hardware (Controllers, Axes, DI, DQ, Cylinders, Robots, Connects, Cameras). Use persisted JSON column names. Create generates an ID; pass RecipeID for scoped objects and RobotID for robot points/matrices and FuncID for local images. Workflow Code uses __WORKFLOW_ID__ for the generated class name suffix. Deletion previews list owned records and affected files. Update/delete require id. Objects in properties merge recursively, arrays replace. Review workflow/config references, dynamic references and compile results before apply_changes. Use preview_undo to reverse.")]
public string PreviewObjectEdit(string action, string kind, string id = null, JsonObject properties = null) =>
gateway.Call("previewObjectEdit", new() { ["action"] = action, ["kind"] = kind, ["id"] = id, ["properties"] = properties?.DeepClone() });
[McpServerTool(Name = "preview_vision_function_rename"), Description("Preview renaming a vision function by ID. Compile rewritten recipe workflows and list dynamic or uncertain references before applying.")]
public string PreviewVisionFunctionRename(string id, string newName) =>
gateway.Call("previewVisionRename", new() { ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_vision_asset_rename"), Description("Preview a Cameras, ImageDatas or LocalImageDatas rename with scoped vision steps, compiled workflow edits and manual references.")]
public string PreviewVisionAssetRename(string kind, string id, string newName) =>
gateway.Call("previewVisionAssetRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_object_rename"), Description("Preview a rename of any named project object. Use the kind and ID from list_objects. For VisionLocalVariables, ControlMeasurements, InterpolationSegments or InterpolationPoints pass the parent object ID and memberName, plus segmentName for a point. Review configuration, compiled scripts and manual references before applying.")]
public string PreviewObjectRename(string kind, string id, string newName, string memberName = null, string segmentName = null) =>
gateway.Call("previewObjectRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName, ["memberName"] = memberName, ["segmentName"] = segmentName });
[McpServerTool(Name = "preview_device_rename"), Description("Preview renaming a Cylinders, Robots or Connects ID. Updates the hardware definition and bound lookups across recipes, compiles affected scripts and lists manual references.")]
public string PreviewDeviceRename(string kind, string id, string newName) =>
gateway.Call("previewDeviceRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_io_rename"), Description("Preview renaming a DI or DQ channel across the project, including cylinder bindings and compiled workflow references.")]
public string PreviewIoRename(string kind, string id, string newName) =>
gateway.Call("previewIoRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_axis_rename"), Description("Preview a project-wide motion axis rename by ID, including point and interpolation configuration, compiled workflow edits and dynamic references.")]
public string PreviewAxisRename(string id, string newName) =>
gateway.Call("previewAxisRename", new() { ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_point_rename"), Description("Preview renaming an AxisPoints or RobotPoints ID, including bound workflow and matrix references. Review uncertain and dynamic hits before applying.")]
public string PreviewPointRename(string kind, string id, string newName) =>
gateway.Call("previewPointRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_workflow_rename"), Description("Preview renaming a workflow by ID in its recipe. Compile rewritten callers and list dynamic or uncertain references for review.")]
public string PreviewWorkflowRename(string id, string newName) => gateway.Call("previewWorkflowRename", new() { ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_variable_rename"), Description("Preview renaming a recipe user variable by ID. Compile rewritten workflows, and report dynamic or uncertain references for review. Apply the returned draft ID after review.")]
public string PreviewVariableRename(string id, string newName) => gateway.Call("previewRename", new() { ["id"] = id, ["newName"] = newName });
[McpServerTool(Name = "preview_undo"), Description("Preview reversal of a saved variable, axis, vision function, point or workflow rename from this session. Apply the new draft ID to confirm undo.")]
public string PreviewUndo(string draftId) => gateway.Call("previewUndo", new() { ["draftId"] = draftId });
[McpServerTool(Name = "preview_workflow_script"), Description("Preview replacement C# source for a workflow ID. Returns a line diff, Roslyn diagnostics, entry-point validation and draft ID. No code runs. Apply a valid draft with apply_changes; a local WPF confirmation is required.")]
public string PreviewWorkflowScript(string id, string code) =>
gateway.Call("previewScript", new() { ["id"] = id, ["code"] = code });
[McpServerTool(Name = "preview_changes"), Description("Preview a comment change to one recipe, variable or workflow. Returns a draft ID and before/after values. No file is written.")]
public string PreviewChanges(string kind, string id, string comment) =>
gateway.Call("preview", new() { ["kind"] = kind, ["id"] = id, ["comment"] = comment });
[McpServerTool(Name = "apply_changes"), Description("Save an existing preview using its draft ID. Requires write-enabled launch. An open editor requests user confirmation.")]
public string ApplyChanges(string draftId) => gateway.Call("apply", new() { ["draftId"] = draftId });
[McpServerTool(Name = "get_change_status", ReadOnly = true), Description("Inspect a pending, rejected or saved draft.")]
public string GetChangeStatus(string draftId) => gateway.Call("status", new() { ["draftId"] = draftId });
[McpServerTool(Name = "find_references", ReadOnly = true), Description("Find workflow references to a named object or nested member. For members supply the parent ID, memberName and, for interpolation points, segmentName.")]
public string FindReferences(string kind, string id, string memberName = null, string segmentName = null) =>
gateway.Call("references", new() { ["kind"] = kind, ["id"] = id, ["memberName"] = memberName, ["segmentName"] = segmentName });
[McpServerTool(Name = "get_project_summary", ReadOnly = true), Description("Read the bound PLCSharp project, default recipe and available object kinds.")]
public string Summary() => gateway.Call("summary", new());
[McpServerTool(Name = "list_objects", ReadOnly = true), Description("List one object kind from the summary, optionally scoped to a recipe. Pages start at offset zero.")]
public string ListObjects(string kind, string recipeId = null, int offset = 0, int limit = 50) =>
gateway.Call("list", new() { ["kind"] = kind, ["recipeId"] = recipeId, ["offset"] = offset, ["limit"] = limit });
[McpServerTool(Name = "get_object", ReadOnly = true), Description("Read one project object by kind and stable ID, including source for workflows.")]
public string GetObject(string kind, string id) => gateway.Call("get", new() { ["kind"] = kind, ["id"] = id });
}