Companion

object Companion

Properties

Link copied to clipboard
private const val DEFAULT_PORT: Int = 25599
Link copied to clipboard
private const val DEFAULT_TRIES: Int = 10

How far the DEFAULT port may climb past DEFAULT_PORT; an explicit port never climbs.

Link copied to clipboard
private val GSON: Gson?

No HTML escaping: it rewrites = as a \u escape, and JVM toString() output is full of = — that bloats the response and leaves its reader decoding noise. The escape only matters inside HTML.

Link copied to clipboard
Link copied to clipboard
private val LOOPBACKS: List<InetAddress>
Link copied to clipboard
private const val PATH: String

The one path served, matched exactly — /mcp without it is refused like any other miss.

Link copied to clipboard
private const val PROTOCOL_VERSION: String
Link copied to clipboard
private val URL_SAFE_TOKEN: Regex

RFC 3986 unreserved — see urlTokenUsable. A generated token (32 hex chars) always matches.

Functions

Link copied to clipboard
private fun bindAll(start: Int, tries: Int): List<HttpServer>

Bind every loopback address this host HAS on ONE shared port, trying tries ports from start, and really give back a port we turn down.

Link copied to clipboard
private fun configured(a: InetAddress): Boolean

Whether a is configured on an interface of this host — i.e. whether any client's resolver can be sent there at all. The interface table, not a probing bind: a bind answers "can THIS JVM take it", which is a different question, and it needs an ephemeral port that is not always there.

Link copied to clipboard

The list as a message spells it: 127.0.0.1, 0:0:0:0:0:0:0:1.

Link copied to clipboard
private fun onDaemonThread(name: String, body: () -> Unit)

Run body on a daemon thread and wait. HttpServer's dispatcher copies the daemon flag of whichever thread calls start(), and mod init is not one — a dispatcher started from there would keep the JVM alive after /stop. stop(0) joins it, so a released server leaves nothing behind.

Link copied to clipboard
private fun release(servers: List<HttpServer>)

Give the bound ports back, started or not: stop() on an unstarted server keeps the port, and start() on a started one throws.

Link copied to clipboard
private fun sendEmpty(ex: HttpExchange, status: Int)
Link copied to clipboard
private fun sendJson(ex: HttpExchange, status: Int, body: JsonObject)

Chunked, not buffered: toJson(body) materializes the document as a String and again as a byte[], and a String also caps how large a response can ever be. Fixing the status at the first byte only costs a 500 an OOM here could not have sent anyway. Orthogonal to SSE — the content type still selects the client's non-SSE path.

Link copied to clipboard

Idempotent. Binds the endpoint and logs the port + token; never crashes the game on failure.

Link copied to clipboard
private fun sweep(addrs: List<InetAddress>, start: Int, tries: Int): List<HttpServer>?

One pass over the range: the first port free on every addrs wins, and a port that binds only some of them is given back before moving on. Null once the range is exhausted. Each refusal is logged at DEBUG rather than kept — climbing past a taken port is what this is for, not an anomaly, and a single retained exception would be an arbitrary one of many.