Feature: Terminate TLS for the MCP endpoint inside YAMA

MCP was served by a plain httplib::Server, so remote clients could only use
cleartext HTTP. YAMA now terminates TLS itself: a small front-end owns the
MCP port, sniffs the first byte and either runs a Schannel handshake and
forwards the decrypted bytes or passes plaintext through, with httplib
moving to a loopback-only internal port. No OpenSSL, no frp change, existing
local HTTP clients keep working, and an unusable certificate fails closed
instead of falling back to cleartext.

The identity loader grew the two things a public certificate needs. ECDSA
(P-256/P-384/P-521) keys are persisted by importing PKCS#8 with an
NCRYPTBUFFER_PKCS_KEY_NAME parameter, the only route MS Software KSP
supports for Schannel; RSA keeps its old export-and-repersist path.
Fullchains are accepted, and the intermediates are added to the current
user's CA store so Schannel, which builds chains only from system stores,
actually sends them. The shared handshake also now buffers a full TLS
record before the first AcceptSecurityContext, which a partial ClientHello
would otherwise poison (SEC_E_UNKNOWN_CREDENTIALS).

Session resumption exposed a second trap: when a client resumes a session,
ChangeCipherSpec, Finished and the first application record arrive in one
segment and AcceptSecurityContext leaves the application record pending in
the session. Nothing drained it, because both callers only decrypt when new
ciphertext arrives, so the request waited until the peer gave up (the MCP
front-end saw no request at all and the client reported "fetch failed
(other side closed)"; the device port had the same hole behind its
occasional login timeouts). Decrypt now drains pending data on len == 0, and
both the front-end and the device port's handshake thread use it before
entering their receive loops.

Configuration is McpHttps, McpCertPath, McpKeyPath and McpInternalPort, the
first three editable in the MCP settings dialog (the certificate rows stay
greyed out until HTTPS is enabled and saving rejects missing files).

Verified with a real Let's Encrypt ECDSA fullchain (53 tools over TLS, 401
with no or bad token), a local three-level chain whose intermediate was
absent from the store, Node 24's fetch on a resumed session (initialize,
notifications/initialized and tools/list all complete), and the device
port's RSA handshake as a regression check. Details in docs/Mcp_Design.md
section 13.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 13:56:41 +01:00
1 parent b4cb34fea9
commit a7dd86ff0f
15 files changed
+1218 -96

No files matched your search

+26 -3
View File
@@ -6860,17 +6860,40 @@ bool CMcpServer::Start(const std::string& bind, int port) {
m_bind = bind;
m_port = port;
m_thread = std::thread([this, bind, port]() {
m_server.listen(bind, port);
// HTTPS 时 httplib 只监听回环内部端口,对外端口由 McpTlsProxy 占据。
const bool https = m_httpsEnabled;
const int backendPort = (https && m_internalPort > 0) ? m_internalPort : (https ? port + 1 : port);
// 只在 HTTPS 下记住内部端口:明文模式下把它写成 port,会让「先明文启动、再改 HTTPS
// 重启」这条路径拿到 backendPort == port,后端和前端抢同一个端口、MCP 直接起不来。
if (https) m_internalPort = backendPort;
const std::string backendBind = https ? "127.0.0.1" : bind;
m_thread = std::thread([this, backendBind, backendPort]() {
m_server.listen(backendBind, backendPort);
});
// 给 listen 一点时间绑定端口;httplib::Server::is_running() 在 listen 内部置位。
std::this_thread::sleep_for(std::chrono::milliseconds(100));
m_running.store(m_server.is_running());
return m_running.load();
if (!m_running.load()) return false;
if (https) {
// fail-closed:TLS 前端起不来(证书/私钥坏、端口被占)就整体停掉,
// 绝不让 MCP 以「以为在跑 HTTPS 其实没在监听」的状态存在。
if (!m_tlsProxy.Start(bind, port, backendPort, m_certPath, m_keyPath)) {
Mprintf("[MCP] HTTPS front-end failed to start; MCP server disabled\n");
m_server.stop();
if (m_thread.joinable()) m_thread.join();
m_running.store(false);
return false;
}
}
return true;
}
void CMcpServer::Stop() {
// 先停前端:不再接入新连接,再停后端。Stop 内部会 join 前端线程。
m_tlsProxy.Stop();
m_server.stop();
if (m_thread.joinable()) {
m_thread.join();