using System.ComponentModel; using ModelContextProtocol.Server; using System.Text.Json.Nodes; namespace PLCSharp.Mcp; [McpServerToolType] internal sealed class ProjectTools(ProjectGateway gateway) { [McpServerTool(Name = "preview_batch", ReadOnly = true), Description("Use one batch for all edits requested together, even across files. Preview only; no editor dialog or project writes. Pass ordered operations with action create/update/delete/rename, kind, id, properties or newName (and optional memberName/segmentName). To edit script source update Workflows with properties.Code. Hardware, recipe and point names are protected. For dependencies give a create an alias, then use $alias as id or RecipeID/RobotID/FuncID. Returns references, diagnostics and one draft ID. Review once with apply_changes; preview_undo reverses the whole saved batch. For analysis only use read/preview tools and stop before apply_changes.")] public string PreviewBatch(JsonArray operations) => gateway.Call("previewBatch", new() { ["operations"] = operations.DeepClone() }); [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("Rename a workflow, vision function, image, variable or homepage control. Existing hardware, recipe and point names are protected. 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_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 an entire saved batch, object edit or rename from this session, without a dialog. 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; confirm the displayed draft in the Agent before saving.")] 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. Present this exact draft, impact and compile results to the user in the Agent first. Only pass confirmed=true after user approval of this draft. Pass false to reject. The editor never prompts. Requires --allow-write.")] public string ApplyChanges(string draftId, bool confirmed) => gateway.Call("apply", new() { ["draftId"] = draftId, ["confirmed"] = confirmed }); [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_workflow_script_api", ReadOnly = true), Description("Consult before authoring workflow C#. Returns this project runtime build's script entry contract, API signatures, call conditions, wait/completion and failure semantics, and compilable examples. Omit topic for all, or pass vision, workflow, flow, lookup, a method name, or Type.Method. For edits pass workflowId to substitute its actual class name in examples.")] public string WorkflowScriptApi(string topic = null, string workflowId = null) => gateway.Call("scriptApi", new() { ["topic"] = topic, ["workflowId"] = workflowId }); [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 }); }