|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > MCP. - MCP (Model-Context Protocol) server operations > !Server-Lifecycle - Commands for managing the MCP server lifecycle (init, start, stop) > MCP. - MCP Server Operations |
SPR Script Language
MCP.Start
Starts the MCP HTTP server's asynchronous listener thread.
Intention
The MCP.Start command launches the MCP server's main background thread. This thread is responsible for creating the optional status window and listening for incoming TCP/IP connections on the port specified in the configuration file.
This server uses a modern, high-performance architecture. When a new client connects, the main listener thread accepts it and immediately spawns a new, separate worker thread to handle that specific client's HTTP request. This design ensures that the main listener remains highly responsive and can accept new connections even while others are being processed.
The command returns the main listener thread's handle, which can be used to check the server's status.
Prerequisites
You must call MCP.Init before using this command. The initialization routine loads the crucial configuration (e.g., port number, local-only mode) that MCP.Start requires to function.
Syntax
MCP.Start[|$$ret]
Parameter Explanation
$$ret - (Variable, Optional)
An optional variable to store the main server thread's handle. This handle is a positive number on success, or 0 on failure. If this parameter is omitted, the handle is pushed to the stack.
Examples
' Example: Start the server and wait for it to be ready
MCP.Init ' Must be called first!
MCP.Start|$$thr ' Launch the server thread
' Check if thread creation failed immediately
JIZ.$$thr|ServerStartFailed
' Best Practice: Wait for the server to be fully initialized
' This loop polls a getter function in the HTP library.
$$i = 0
:Loop
INCR.$$i
JMP.$$i=100|ServerTimeout ' Timeout after ~5 seconds
HTP.MCPGetStatusByte|$$stat ' Custom command that calls the backend getter
JIZ.$$stat|Loop ' Status 0 means still starting, so loop
JMP.$$stat=255|ServerStartFailed ' Status 255 (-1) means it failed to start
' If we reach here, server is ready!
MBX.Server is ready and listening!
' ... Your script can now safely interact with the server ...
' Finally, stop the server when done.
MCP.Stop
Remarks
•This command is asynchronous. It returns immediately while the server starts in the background. It is highly recommended to use a polling loop (as shown in the example) to wait for the server to become fully ready before attempting to send it commands.
•Only one server instance can be active at a time. Calling this command while a server is already running will have no effect.
•If thread creation fails at the OS level, the command returns 0. Always check the return value.
See also: