|
<< 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.MCPSimulate
Injects a command into the MCP server's listener queue for testing.
Intention
This is a powerful debugging and testing command. It allows a script to act like an external client and place a command directly into the MCP server's queue when it's running in Listener Mode. This is invaluable for testing how your main script logic handles incoming commands without needing to set up a separate client application to send them.
By default, the command can only be executed once to prevent accidental loops.
This behavior can be overridden with an optional parameter.
Illustration
✉️ Self-Addressed Stamped Envelope: This command lets your script write a letter (a command), put it in an envelope, and drop it into its own mailbox (the server queue) to test if the mail-handling logic works correctly.
Syntax
HTP.MCPSimulate|$$MODE|$$PAYLOAD[|$$RESET][|$$JID]
Parameter Explanation
P1 - $$MOD - (Variable, Required)
A variable containing the mode of the command to inject. Valid values are the strings "RUNSCR" (to run script code directly) or "RUNPTH" (to run a script from a file path).
P2 - $$PAY - (Variable, Required)
A variable containing the payload for the command. This should be the script code if mode is "RUNSCR", or the file path if mode is "RUNPTH".
P3 - $$RES - (Numeric, Optional)
A numeric flag. If set to a non-zero value (e.g., 1), it will reset the one-time execution lock, allowing HTP.MCPSimulate to be called again. If omitted or 0, the command can only be run once.
Examples
'
'SPR Script-file: MCP_Server
'===========================================================
'#EXE:?path\
'#SPI:ForceWrite
VAR.$$CFG=?path\mcp_config.json
MCP.Init|$$CFG
MCP.SetLogToFile|1
MCP.SetLogLevel|3
' --- Step 1: Start the Server ---
MCP.Start|$$RES
JIZ.$$RES|Fail
GSB.WaitReady
MCP.ShowWindow|1
' --- Step 2: Create and Queue a Test Script ---
PRT.Generating and queueing a test script...
VAR.$$AIS='$$RES=25$crlf$MCP.ReturnResult|$$RES'
MCP.Simulate|RUNSCR|$$AIS|1|$$JID
IVV.$$JID=0
MBX.Simulate could not create a job.
GTO.Fail
EIF.
PRT.Test job queued with JobID: $$JID
' --- Step 3: Enter the Master Loop (Client Polling & Robot Working) ---
PRT.Entering Master Loop to process queue and wait for test result...
VAR.$$TMR=TIMER ' Start a timeout for the whole test
:MasterLoop
' --- Part A: Act as Robot Worker ---
' Check for any command in the queue and execute it immediately if found.
MCP.GetNextCommand|$$PAY|$$CMD|$$NID
IVV.$$NID!0
$$JID=$$NID
EIF.
' If a command was found (not null)
JIS.$$CMD|:Execute
' --- Part B: Act as Test Client ---
' Check the status of OUR test job.
MCP.CheckJobResult|$$JID|$$STA|$$CON
' Check if the test is finished (completed or failed)
IVS.$$STA=COMPLETED
PRT.---------------------------------------
PRT.TEST SUCCEEDED!
PRT.Final Result for Test Job $$JID: [$$STA] -> $$CON
PRT.---------------------------------------
GTO.Finish
EIF.
IVS.$$STA=FAILED
PRT.---------------------------------------
PRT.TEST FAILED!
PRT.Final Result for Test Job $$JID: [$$STA] -> $$CON
PRT.---------------------------------------
GTO.Finish
EIF.
IVS.$$STA=TIMED_OUT
PRT.---------------------------------------
PRT.TEST FAILED (Timeout)!
PRT.Final Result for Test Job $$JID: [$$STA] -> $$CON
PRT.---------------------------------------
GTO.Finish
EIF.
' --- Part C: Master Timeout & Yield ---
' Check if the entire test has timed out
IVV.(TIMER-$$TMR)>30
PRT.Master loop timed out waiting for a result.
GTO.Fail
EIF.
PAU.250|ms
GTO.MasterLoop
' --- Subroutine for non-blocking execution ---
:Execute
PRT.--- Dequeued: $$CMD (JobID: $$JID) ---
' This block is purely asynchronous.
' 1. Launch the script. The command returns the PID.
MCP.RunCommand|$$JID|$$PAY|$$PID
' 2. Check for launch failure and log it.
IVV.$$PID=0
PRT.Error: Failed to launch script for JobID $$JID.
' Inform the SLL that this job failed.
MCP.FinalizeJob|$$JID|2|Launch Failed
ELS.
PRT.Launched JobID $$JID as PID $$PID.
EIF.
' 3. Immediately return to the main loop.
RET.
'===========================================================
:Fail
DBP.Server is not running.
GTO.Finish
:WaitReady
PRT.Waiting for server to become ready...
FOR.$$CNT|1|50
MCP.GetStatusByte|$$STA
JIV.$$STA=1|Ready
JIV.$$STA=255|Fail
PAU.100|ms
NEX.
PRT.Error: Timed out waiting for server to become ready.
GTO.Fail
:Ready
RET.
:Finish
PRT.Script finished. Shutting down server.
MCP.Stop
ENR.
Remarks
- This command is intended for debugging and has no effect if the server is not running in Listener Mode.
- The command does not return a value. Errors are logged internally.
- The one-time execution is controlled by a global flag, g_mcp_simulation_has_run.
See also: