MCP server support (-lib net_server)
McpServer
MCP server. Serve listens on the HTTP port the configuration's "http_port" names; an "http_port" of 0 asks the system for a free port, which GetBoundPort answers once the server is listening, so a program that starts the server in a thread can poll for the port instead of picking a number.
The configuration is a JSON object with these keys, all required: "http_port" (an integer 0..65535), "http_session_timeout" (an integer number of seconds, greater than 0), "debug" (true or false), and the strings "name", "version" and "instructions" the server answers an MCP initialize request with. A Nil configuration, a missing key or a value of the wrong type is refused with a message naming the key (see GetLastError) instead of being dereferenced, and leaves the server free to be started again.
Operations
GetBoundPort # function
Gets the port the MCP server is bound to. An "http_port" of 0 is bound to a port the system chose; once Serve has the server listening this answers that port, so a program that starts the server in a thread can poll here instead of picking a port number.
function : GetBoundPort() ~ IntReturn
| Type | Description |
|---|---|
| Int | bound port, or 0 if the server is not yet listening |
Example
# serve on a port the system chooses, then find out which
thread := System.Concurrency.Thread->New() {
New() { Parent(); }
method : public : Run(arg : System.Base) ~ Nil {
builder := Data.JSON.JsonBuilder->New();
config := builder->PushObject();
config->Insert("name", "My MCP server");
config->Insert("version", "1.0.0");
config->Insert("instructions", "What this server is for");
config->Insert("http_port", 0);
config->Insert("http_session_timeout", 30);
config->Insert("debug", false);
Web.HTTP.Server.MCP.McpServer->Serve(builder->Pop(), MyMcpHandler->New());
}
};
thread->Execute(Nil);
while(Web.HTTP.Server.MCP.McpServer->GetBoundPort() = 0) {
System.Concurrency.Thread->Sleep(10);
};
port := Web.HTTP.Server.MCP.McpServer->GetBoundPort();GetLastError # function
Gets why the last call to Serve was refused: the server was already initialized, the handler was Nil, or the configuration was Nil, not a JSON object, missing a key or held a value of the wrong type or out of range. The message names the key. Nil once a Serve has been accepted; a port that cannot be bound is reported by the web server on standard error instead.
function : GetLastError() ~ StringReturn
| Type | Description |
|---|---|
| String | why the last Serve was refused, Nil if it was not |
Example
Web.HTTP.Server.MCP.McpServer->Serve(config, MyMcpHandler->New());
reason := Web.HTTP.Server.MCP.McpServer->GetLastError();
if(reason <> Nil) {
"MCP server not started: {$reason}"->ErrorLine();
};IsDebug # function
Returns rather the server is in debug mode
function : IsDebug() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true is in debug mode, false otherwise |
Serve # function
Initializes the MCP server and serves until the web server is stopped. The configuration's "http_port" is the port to listen on; 0 takes a port the system chooses (see GetBoundPort), a negative port is refused. A configuration that is Nil, is not a JSON object, lacks one of the required keys (see the class) or holds a value of the wrong type is refused with a message naming the key, on standard error and from GetLastError, and the server may be started again with a valid one.
function : Serve(config:JsonElement, handler:McpRequestHandler) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| config | JsonElement | JSON configuration (see the class for the keys) |
| handler | McpRequestHandler | MCP request handler |