|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > MCP. - MCP (Model-Context Protocol) server operations > !Server-Operations - Commands for interacting with a running MCP server (signals, actions) > MCP. - MCP Server Operations |
SPR Script Language
HTP.RunScript
Launches an SPR script in a new, isolated parallel process.
Intention
This is the primary and safest method for executing scripts, especially those generated by an AI. The HTP.RunScript command takes a string of script code, saves it to a temporary file, and launches a completely new robot instance to run it. This creates a "sandbox" environment.
Because the script runs in a separate process, any errors, crashes, or infinite loops within it will not affect the main (parent) script. The parent script can safely monitor the child process, wait for its result with HTP.WaitForResult, and terminate it if it misbehaves.
Illustration
📦 Send a Task: Think of this command as putting a set of instructions (the script code) into a sealed box and handing it to a new, temporary worker (the parallel robot process). The worker goes off to complete the task independently, and you can check on their progress later.
Syntax
HTP.RunScript|$$SRC[|$$PID][|$$INH]
Parameter Explanation
P1 - $$SRC - (Variable, Required)
A variable containing the complete, multi-line SPR script code to be executed. Line breaks should be represented by the $crlf$ special variable.
P2 - $$PID - (Variable, Optional)
A variable to store the numeric Process ID (PID) of the newly created robot process. This PID can be used with HTP.WaitForResult.
P3 - $$INH - (Numeric, Optional)
Reserved for future use. This flag is intended to control whether the child script inherits context (variables, etc.) from the parent.
Examples
'***********************************
' HTP.RunScript - Sample 1: Simple Asynchronous Execution
'***********************************
' Define the script code in a variable
VAR.$$cod=MBX.Hello from the parallel robot!$crlf$ENR.
' Run the script and get its PID
HTP.RunScript|$$cod|$$pid
IVV.$$pid > 0
MBX.Successfully launched new robot process with PID: $$pid
ELS.
MBX.Failed to launch the script.
EIF.
ENR.
'
'
'SPR Script-file: MCP_Server
'Purpose: Start a MCP-Server and simulate a MCP-Signal
'Author: MINIPC\theog
'Creation date: 07-24-2025 at 10:24:18
'===========================================================
'#EXE:?path\
'#SPI:ForceWrite
HTP.SetLogLevel|3
VAR.$$CFG=?path\mcp_config.json
VAR.$$POR=5555
VAR.$$ADD=127.0.0.1
PRT.Generating 100-line test script...
VAR.$$AIS=
FOR.$$IXX|1|4
VAR.$$AIS+PRT.This is line $$IXX->Line $$IXX$crlf$
NEX.
VAR.$$AIS+DMP.Speed$crlf$
VAR.$$AIS+MBX.!$crlf$
VAR.$$AIS+ENR.$crlf$
' --- Step 1: Initialize the Server ---
' This loads the config file (or creates it) and sets the port, etc.
MCP.Init|$$CFG
' --- Step 2: Start the Server in LISTENER mode ---
VAR.$$MSG=Server Start (Listener Mode)
' Start in Listener Mode (1)
MCP.Start|$$RES
DBP.Server started as TID: $$RES
JIZ.$$RES|Fail
MCP.IsRunning|$$RES
JIZ.$$RES|Fail
HTP.MCPShowWindow|1
' --- Step 3: Poll the Listener Queue ---
:Loop
PAU.500|ms
MCP.IsRunning|$$RES
JIZ.$$RES|Fail
HTP.MCP Simulate|RUNSCR|$$AIS
' Check the queue count
HTP.MCPGetQueueCount|$$RES
JIZ.$$RES|Loop
' Get the next command and its type from the queue
HTP.MCPGetNextCommand|$$RET|$$CMD
VAV.$$CMD=$$CMD
VAV.$$RET=$$RET
JNS.$$CMD|Loop
PRT.---------------------------------------
PRT.Dequeued Command: $$CMD
PRT.Ret=$$RET
PRT.CMD=$$CMD
PRT.---------------------------------------
' --- Use SCS to check the command type and prepare the script code ---
SCS.$$CMD
CAS.RUNSCR
' The payload in $$RET is the script code. Copy it to the execution variable.
VAR.$$SRC=$$RET
GTO.Execute
CAS.RUNPTH
' The payload in $$RET is a file path. Read the file's content into the execution variable.
CFF.$$RET|$$SRC
GTO.Execute
CAE.
' This is the default case for any unknown command types.
PRT.Unknown command in queue: $$CMD
GTO.Loop
ESC.
' This part should not be reached, but it acts as a safeguard.
GTO.Loop
:Execute
' --- Launch the script from the common variable and get its PID ---
HTP.RunScript|$$SRC|$$PID|0
JIZ.$$PID|ExecError
PRT.Launched script with PID: $$PID
' --- Wait for the script to finish, with a 5-minute timeout ---
HTP.WaitScript|$$PID|300000|$$STA
IVV.$$STA=1
PRT.Error: Script with PID $$PID timed out and was terminated.
ELS.
PRT.Script with PID $$PID finished successfully.
' (Future logic to retrieve the script's result file would go here)
EIF.
GTO.Loop
:ExecError
PRT.Error: Failed to launch script.
GTO.Loop
'===========================================================
:Fail
DBP.Server is not running.
ENR.
Remarks
- This command is asynchronous. It starts the new process and immediately continues with the next line in the parent script without waiting for the child to finish.
- The command relies on a successful handshake between the parent and child process via the KOM-Interface. A failure to launch or handshake will result in a PID of 0.
- To get a result back from the executed script, you must use HTP.WaitForResult.
See also: