-
Notifications
You must be signed in to change notification settings - Fork 17
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 all commits
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,73 @@ | ||
| 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 in a single request. Actions run sequentially and are not transactional. | ||
| Earlier actions are applied even if a later action fails. | ||
| The 'actionsJson' parameter must be a JSON array of action objects. | ||
|
Comment on lines
+12
to
+15
|
||
| Each action object must have an "action" (or "type") 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
|
||
|
|
||
| Note: The backend accepts both "action" and "type" fields. For gesture actions, | ||
| use "action":"gesture" with a separate "type" field for the gesture kind. | ||
|
|
||
| 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. Each must have an 'action' or 'type' field (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); | ||
| if (node is not JsonArray array) | ||
| return "Invalid input: 'actionsJson' must be a JSON array, not " + (node?.GetValueKind().ToString() ?? "null") + "."; | ||
|
|
||
| parsed = array; | ||
| } | ||
| 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' or 'type' field (e.g., 'tap', 'fill', 'navigate')."; | ||
|
|
||
| actions.Add(obj); | ||
| } | ||
|
|
||
| var agent = await session.GetAgentClientAsync(agentPort); | ||
| var result = await agent.BatchAsync(actions, continueOnError); | ||
| return CliJson.SerializeUntyped(result, indented: false); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,6 +47,61 @@ public static async Task<string> Clear( | |||||||||||||||||
| : $"Failed to clear element '{elementId}'."; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| [McpServerTool(Name = "maui_key"), Description("Send a key press to an element. Supported keys for Entry/Editor/SearchBar: 'enter' (submit or newline), 'backspace' (delete last character). Use 'text' parameter to type characters. For reliable behavior, provide an element ID; omitting it may have no effect depending on the agent/platform implementation.")] | ||||||||||||||||||
| public static async Task<string> Key( | ||||||||||||||||||
| McpAgentSession session, | ||||||||||||||||||
| [Description("Key to press: 'enter', 'return', 'backspace', 'delete'")] string key, | ||||||||||||||||||
| [Description("Target element ID. Optional, but omitting it may result in no action; provide an element ID for reliable behavior.")] string? elementId = null, | ||||||||||||||||||
|
Comment on lines
+50
to
+54
|
||||||||||||||||||
| [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}' without a target element; it may have had no effect." | ||||||||||||||||||
| : $"Failed to send key '{key}'. The target element may not support keyboard input, or no target element was provided."; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| [McpServerTool(Name = "maui_gesture"), Description("Perform a touch gesture on the app. Supported gesture types: 'swipe' (requires direction), 'tap', 'longpress', and 'long-press'. 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', 'longpress', or 'long-press'")] string type, | ||||||||||||||||||
|
Comment on lines
+67
to
+70
|
||||||||||||||||||
| [McpServerTool(Name = "maui_gesture"), Description("Perform a touch gesture on the app. Supported gesture types: 'swipe' (requires direction), 'tap', 'longpress', and 'long-press'. 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', 'longpress', or 'long-press'")] string type, | |
| [McpServerTool(Name = "maui_gesture"), Description("Perform a touch gesture on the app. Supported gesture types: 'swipe' (requires direction) and 'tap'. 'longpress' and 'long-press' are accepted for compatibility but currently use tap behavior rather than performing a true long-press interaction. Use maui_tap for simple taps — this tool is primarily for advanced gestures like swiping.")] | |
| public static async Task<string> Gesture( | |
| McpAgentSession session, | |
| [Description("Gesture type: 'swipe' or 'tap'. 'longpress' and 'long-press' are accepted aliases but currently behave the same as 'tap', not a true long press.")] string type, |
Copilot
AI
Apr 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For maui_gesture, elementId is typed/described as optional, but tap/longpress are implemented by delegating to the tap action, which requires elementId. Consider failing fast when type is tap/longpress and elementId is missing, or update the parameter description to clarify it’s required for those gesture types.
Copilot
AI
Apr 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
distance/durationMs default to null, but the agent request model uses non-nullable double Distance/int DurationMs with defaults. Sending JSON null for these fields can cause deserialization to fail on the agent. Consider passing explicit defaults when omitted (matching the agent defaults), or ensure null values aren’t serialized/sent.
Uh oh!
There was an error while loading. Please reload this page.