Continuando il nostro viaggio con il nostro server di documentazione MCP dal post precedente, possiamo approfondire un po’ di più la storia del TLS e come stiamo generando il certificato Let’s Encrypt all’avvio.
Se vuoi vedere il codice sorgente completo, il repository github è linkato in fondo al post.
Quindi diamo uno sguardo più da vicino ai dettagli. Quando abbiamo provato a integrare il nostro server MCP in ChatGPT e Gemini, è emersa la necessità del TLS. Poiché usiamo anche Let’s Encrypt in tutto DownToZero, abbiamo riutilizzato principalmente questo processo in questo container.
sequenceDiagram
participant App as get_certificate()
participant LE as Let's Encrypt (ACME)
participant Axum as Axum HTTP Server
participant FS as Filesystem
App->>LE: Create Account (NewAccount)
App->>LE: Create New Order (Identifier::Dns(domain))
LE-->>App: Order (status = Pending)
App->>App: Create oneshot channel (snd, rcv)
App->>LE: Fetch authorizations
LE-->>App: Authorization incl. HTTP-01 challenge
App->>App: Compute token + key_authorization (secret)
App->>Axum: Start server on acme_port serving<br/>/.well-known/acme-challenge/{token} -> secret
App->>App: Sleep 2s
App->>LE: challenge.set_ready()
LE->>Axum: GET /.well-known/acme-challenge/{token}
Axum-->>LE: 200 secret (key_authorization)
App->>LE: poll_ready (with backoff)
LE-->>App: Order Ready
App->>LE: finalize()
LE-->>App: private_key_pem
App->>LE: poll_certificate()
LE-->>App: cert_chain_pem
App->>FS: write certs/{domain}.cert.pem
App->>FS: write certs/{domain}.key.pem
App->>Axum: snd.send() (graceful shutdown)
App-->>App: Ok(())
Come puoi vedere, il protocollo ACME è piuttosto semplice. Ora arriva la parte complicata: integrare questo nel nostro server MCP.
certs/{domain}.cert.pem e certs/{domain}.key.pem, il che significa che abbiamo già certificati validi.// caricamento del certificato e della chiave dal file
let cert_file = format!("certs/{}.cert.pem", domain);
let key_file = format!("certs/{}.key.pem", domain);
let tls_config = RustlsConfig::from_pem_file(cert_file, key_file)
.await
.unwrap();
log::info!("listening on https://[::]:{}", config.port);
let sse_config = SseServerConfig {
bind: format!("[::]:{}", config.port).parse().unwrap(),
sse_path: "/sse".to_string(),
post_path: "/message".to_string(),
ct: tokio_util::sync::CancellationToken::new(),
sse_keep_alive: None,
};
let (sse_server, router) = SseServer::new(sse_config);
let addr = sse_server.config.bind;
let ct = sse_server.with_service(DowntozeroTool::new);
let server = axum_server::bind_rustls(addr, tls_config).serve(router.into_make_service());
tokio::spawn(async move {
if let Err(e) = server.await {
log::error!("sse server shutdown with error, {e}");
}
});
Abbiamo anche implementato un fallback per le esecuzioni locali: se non è presente una configurazione di dominio, avviamo il server MCP con HTTP semplice. Questo rende possibile il testing locale senza la necessità di un certificato o del DNS durante lo sviluppo.