using System.ComponentModel; using ModelContextProtocol.Server; using System.Text.Json.Nodes; namespace PLCSharp.Mcp; [McpServerToolType] internal sealed class ProjectTools(ProjectGateway gateway) { [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_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 C# references to a Variables, SystemVariables, Workflows, VisionFunctions, AxisPoints or RobotPoints ID within its recipe, or an Axes, DI, DQ, Cylinders, Robots or Connects ID across recipes. Dynamic hits require human review.")] public string FindReferences(string kind, string id) => gateway.Call("references", new() { ["kind"] = kind, ["id"] = id }); [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 }); }