-
Notifications
You must be signed in to change notification settings - Fork 27
Add 5 critical MCP tools: capabilities, back, key, gesture, batch #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| using System.ComponentModel; | ||||||
| using System.Text.Json; | ||||||
| using System.Text.Json.Nodes; | ||||||
| using ModelContextProtocol.Server; | ||||||
| using Microsoft.Maui.Cli.DevFlow.Mcp; | ||||||
|
|
||||||
| namespace Microsoft.Maui.Cli.DevFlow.Mcp.Tools; | ||||||
|
|
||||||
| [McpServerToolType] | ||||||
| public sealed class BatchTools | ||||||
| { | ||||||
| [McpServerTool(Name = "maui_batch"), Description(""" | ||||||
| Execute multiple UI actions atomically in a single request. Actions run sequentially. | ||||||
|
rmarinho marked this conversation as resolved.
Outdated
|
||||||
| The 'actionsJson' parameter must be a JSON array of action objects. | ||||||
|
Comment on lines
+12
to
+15
|
||||||
| Each action object must have an "action" field specifying the operation. | ||||||
|
|
||||||
| Supported actions and their fields: | ||||||
| - {"action":"tap", "elementId":"<id>"} | ||||||
| - {"action":"fill", "elementId":"<id>", "text":"<value>"} | ||||||
| - {"action":"clear", "elementId":"<id>"} | ||||||
| - {"action":"key", "key":"enter", "elementId":"<id>"} | ||||||
| - {"action":"focus", "elementId":"<id>"} | ||||||
| - {"action":"scroll", "elementId":"<id>", "deltaX":0, "deltaY":200} | ||||||
| - {"action":"gesture", "type":"swipe", "elementId":"<id>", "direction":"up"} | ||||||
| - {"action":"navigate", "route":"//page"} | ||||||
| - {"action":"back"} | ||||||
|
Comment on lines
+15
to
+27
|
||||||
|
|
||||||
| Example: [{"action":"fill","elementId":"entry1","text":"hello"},{"action":"tap","elementId":"btn1"}] | ||||||
| """)] | ||||||
| public static async Task<string> Batch( | ||||||
| McpAgentSession session, | ||||||
| [Description("JSON array of action objects (see tool description for schema)")] string actionsJson, | ||||||
| [Description("If true, continue executing remaining actions after a failure (default: false)")] bool continueOnError = false, | ||||||
| [Description("Agent HTTP port (optional if only one agent connected)")] int? agentPort = null) | ||||||
| { | ||||||
| JsonArray? parsed; | ||||||
| try | ||||||
| { | ||||||
| var node = JsonNode.Parse(actionsJson); | ||||||
| parsed = node?.AsArray(); | ||||||
| if (parsed == null) | ||||||
| return "Invalid input: 'actionsJson' must be a JSON array, not " + (node?.GetValueKind().ToString() ?? "null") + "."; | ||||||
|
rmarinho marked this conversation as resolved.
Outdated
|
||||||
| } | ||||||
| catch (JsonException ex) | ||||||
| { | ||||||
| return $"Invalid JSON in 'actionsJson': {ex.Message}"; | ||||||
| } | ||||||
|
|
||||||
| if (parsed.Count == 0) | ||||||
| return "Empty actions array — nothing to execute."; | ||||||
|
|
||||||
| var actions = new List<JsonObject>(); | ||||||
| for (int i = 0; i < parsed.Count; i++) | ||||||
| { | ||||||
| if (parsed[i] is not JsonObject obj) | ||||||
| return $"Invalid action at index {i}: expected a JSON object, got {parsed[i]?.GetValueKind().ToString() ?? "null"}."; | ||||||
|
|
||||||
| if (obj["action"] == null && obj["type"] == null) | ||||||
| return $"Invalid action at index {i}: must have an 'action' field (e.g., 'tap', 'fill', 'navigate')."; | ||||||
|
||||||
| return $"Invalid action at index {i}: must have an 'action' field (e.g., 'tap', 'fill', 'navigate')."; | |
| return $"Invalid action at index {i}: must have an 'action' or 'type' field (e.g., 'tap', 'fill', 'navigate')."; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,45 @@ public static async Task<string> Clear( | |
| : $"Failed to clear element '{elementId}'."; | ||
| } | ||
|
|
||
| [McpServerTool(Name = "maui_key"), Description("Send a key press to an element or the app. Supported keys for Entry/Editor/SearchBar: 'enter' (submit or newline), 'backspace' (delete last character). Use 'text' parameter to type characters. Other keys may have no effect depending on the element type.")] | ||
| public static async Task<string> Key( | ||
| McpAgentSession session, | ||
| [Description("Key to press: 'enter', 'return', 'backspace', 'delete'")] string key, | ||
| [Description("Target element ID (optional — uses focused element if omitted)")] string? elementId = null, | ||
| [Description("Text to type character by character into the element")] string? text = null, | ||
| [Description("Agent HTTP port (optional if only one agent connected)")] int? agentPort = null) | ||
| { | ||
| var agent = await session.GetAgentClientAsync(agentPort); | ||
| var success = await agent.KeyAsync(key, elementId, text); | ||
| return success | ||
| ? elementId is not null ? $"Sent key '{key}' to element '{elementId}'." : $"Sent key '{key}'." | ||
| : $"Failed to send key '{key}'. Element may not support keyboard input."; | ||
|
rmarinho marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| [McpServerTool(Name = "maui_gesture"), Description("Perform a touch gesture on the app. Supported gesture types: 'swipe' (requires direction), 'tap', 'longpress'. Use maui_tap for simple taps — this tool is for advanced gestures like swiping.")] | ||
| public static async Task<string> Gesture( | ||
| McpAgentSession session, | ||
| [Description("Gesture type: 'swipe', 'tap', or 'longpress'")] string type, | ||
| [Description("Target element ID (optional)")] string? elementId = null, | ||
|
Comment on lines
+67
to
+71
|
||
| [Description("Swipe direction: 'up', 'down', 'left', 'right' (required for swipe)")] string? direction = null, | ||
| [Description("Swipe distance in pixels (optional, uses default if omitted)")] double? distance = null, | ||
| [Description("Gesture duration in milliseconds (optional)")] int? durationMs = null, | ||
| [Description("Agent HTTP port (optional if only one agent connected)")] int? agentPort = null) | ||
|
Comment on lines
+72
to
+75
|
||
| { | ||
| var validTypes = new[] { "swipe", "tap", "longpress" }; | ||
| if (!validTypes.Contains(type, StringComparer.OrdinalIgnoreCase)) | ||
| return $"Unsupported gesture type '{type}'. Supported types: {string.Join(", ", validTypes)}."; | ||
|
|
||
| if (type.Equals("swipe", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(direction)) | ||
| return "Swipe gesture requires a 'direction' parameter ('up', 'down', 'left', 'right')."; | ||
|
|
||
| var agent = await session.GetAgentClientAsync(agentPort); | ||
| var success = await agent.GestureAsync(type, elementId, direction, distance, durationMs); | ||
| return success | ||
| ? elementId is not null ? $"Performed {type} gesture on element '{elementId}'." : $"Performed {type} gesture." | ||
| : $"Failed to perform {type} gesture."; | ||
|
rmarinho marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| [McpServerTool(Name = "maui_scroll"), Description("Scroll a ScrollView, CollectionView, or ListView. Supports delta-based scrolling, scrolling to an item index, or scrolling an element into view.")] | ||
| public static async Task<string> Scroll( | ||
| McpAgentSession session, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.