HttpTransport
Ktor/Netty MCP server for desktop clients, speaking the Streamable HTTP transport. Exposes the JSON-RPC surface at POST /mcp (plus GET /mcp for the server stream, DELETE /mcp to end a session, and a /health probe), enforces bearer auth via a TokenStore when requireAuth is set, optionally terminates TLS, and advertises itself over mDNS (_mcp._tcp). Prefer building one through DroidMcp.Builder.enableHttpServer rather than directly.
Request hardening, applied before any tool runs:
an
Originheader outside allowedOrigins is rejected with 403 (DNS-rebinding / drive-by browser protection — native MCP clients send noOrigin);POSTbodies must beapplication/json(415 otherwise) and at most MAX_BODY_BYTES (413);an
MCP-Protocol-Versionheader outside McpProtocolImpl.SUPPORTED_PROTOCOL_VERSIONS is rejected with 400;every request after
initializemust carry theMcp-Session-Idissued to the same client (400 when missing, 404 when unknown or owned by another client).
A tools/call that reports progress or asks for elicitation answers with an SSE stream (when the client accepts text/event-stream); every other reply is plain JSON. GET /mcp opens the session's standalone SSE stream (one per session; a new one replaces the old), which carries notifications/{tools,resources,prompts}/list_changed whenever registry, resources or prompts change, plus a keep-alive comment every KEEPALIVE_MS ms.
Parameters
Tools to serve.
Plaintext port; overridden by TlsConfig.httpsPort when tls is set.
Fixed primary token (at least MIN_TOKEN_LENGTH characters); a random one is generated when null and requireAuth is true.
Require a Bearer token on every request. When false the server is open.
Serve only read-only tools.
Android context for mDNS registration; mDNS is skipped when null.
Version reported in initialize and the mDNS TXT record.
Optional per-call audit hook.
TLS material; when set the server binds HTTPS and exposes tlsFingerprint.
Exact Origin values (scheme://host:port) permitted to call the server.
Resource providers served by the resources/... methods.
Prompts served by the prompts/... methods.
Throws
if bearerToken is shorter than MIN_TOKEN_LENGTH.
Constructors
Properties
Current primary token, or null on an open server. Tracks rotateToken.
Whether the server terminates TLS.
mDNS service name advertised on the network, derived from the device model (sanitised, ≤63 chars).
SHA-256 fingerprint of the server certificate to pin in the pairing QR, or null when TLS is disabled. See TlsConfig.certFingerprintSha256.
Functions
Mint a revocable token for a named client. Re-pairing an existing label replaces its token. Returns the new token, or null on an open server.
Snapshot of paired clients (excludes the primary). Empty on an open server.
Revoke a paired client's token. @return true if a client with that label existed.
Rotate the primary bearer token, invalidating the old one. Paired-client tokens are unaffected. Returns the new token, or null on an open server. Surface the new value in a fresh pairing QR.
Start the embedded server and register mDNS. No-op if already running; rethrows bind failures. Synchronized with stop so concurrent start/stop can't orphan a bound server.
Stop the server and unregister mDNS. No-op if not running.