|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > MCP. - MCP (Model-Context Protocol) server operations > Task Management - Execute code and scripts > MCP Commands - Task Management |
MiniRobot Language (MRL) - MCP Task Management Commands
MCP.RunCodeSync
Execute Code and Wait for Result
Purpose
The MCP.RunCodeSync command executes code synchronously, blocking the robot execution until the code completes and returns a result. This is useful when you need the result immediately before continuing with the script.
This command is particularly useful when:
• You need the execution result before proceeding
• Running quick computations that complete immediately
• The result is required for the next steps in the script
• You want simpler code without job tracking
Syntax
MCP.RunCodeSync|$$CODE|$$LANGUAGE|$$RETURN_VAR
Parameters
$$CODE - The code to execute. This can be any supported programming language code as a string.
$$LANGUAGE - The programming language of the code (e.g., python, javascript, powershell).
$$RETURN_VAR - The variable that will receive the execution result (output or error message).
Return Value
The command returns the output of the executed code directly in the specified variable. If the execution fails, the variable will contain the error message. The robot execution is blocked until the code completes.
Examples
' Example 1: Simple Python calculation
MCP.RunCodeSync|print(2 + 2)|python|$$Result
PRT.Result: $$Result
' Example 2: Get current date/time
MCP.RunCodeSync|import datetime; print(datetime.datetime.now())|python|$$Now
PRT.Current time: $$Now
' Example 3: Process data and use result
MEM.S|$$Data|10,20,30,40,50
MCP.RunCodeSync|data="10,20,30,40,50"; print(sum(int(x) for x in data.split(',')))|python|$$Sum
PRT.Sum of values: $$Sum
' Example 4: JavaScript execution
MCP.RunCodeSync|console.log('Hello from JS')|javascript|$$JsResult
PRT.JS output: $$JsResult
Remarks
Unlike RunCodeAsync, the synchronous version blocks robot execution until the code completes. This makes it simpler to use but unsuitable for long-running operations that would freeze the robot.
The command handles both stdout and stderr. Standard output is returned on success, while error output is returned if the execution fails.
There is typically a timeout limit for synchronous execution to prevent indefinite blocking. If the code exceeds this limit, it may be terminated and return a timeout error.
Error Conditions
The command will fail with a parameter error if:
• The code parameter is missing or empty
• The language parameter is missing or unsupported
• The return variable parameter is missing
• The code execution times out or crashes
See also:
• MCP.RunCodeAsync - Execute Code Asynchronously
• MCP.RunScriptAsync - Execute Script Asynchronously
• MCP.RunScriptSync - Execute Script Synchronously