Skip to content

Commit b2beaa1

Browse files
committed
feat(mcp): start full sidecar in mcp-stdio mode for networking support
Previously mcp-stdio created protocol objects in-process with no networking, so discover_peers always returned empty. Now it spawns the full Node (QUIC, gossip, peer discovery) in a background task, waits for /healthz, then runs MCP stdio with the shared protocol/discovery handles. Uses port range 18200-18203 to avoid conflicts.
1 parent c0f2ea2 commit b2beaa1

1 file changed

Lines changed: 60 additions & 12 deletions

File tree

‎trustchain-node/src/main.rs‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ async fn main() -> anyhow::Result<()> {
213213

214214
#[cfg(feature = "mcp")]
215215
Commands::McpStdio { name, data_dir } => {
216+
// MCP stdio starts a full sidecar in the background so that
217+
// all networking (QUIC, gossip, peer discovery) is available
218+
// to MCP tool calls. Logs go to stderr (stdout is the MCP
219+
// transport).
216220
let dir = data_dir.unwrap_or_else(|| {
217221
let home = std::env::var("HOME")
218222
.or_else(|_| std::env::var("USERPROFILE"))
@@ -221,6 +225,14 @@ async fn main() -> anyhow::Result<()> {
221225
});
222226
std::fs::create_dir_all(&dir).ok();
223227

228+
// Logging to stderr only (stdout is the MCP JSON-RPC channel).
229+
let filter = tracing_subscriber::EnvFilter::try_new("warn")
230+
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn"));
231+
tracing_subscriber::fmt()
232+
.with_env_filter(filter)
233+
.with_writer(std::io::stderr)
234+
.init();
235+
224236
let key_path = dir.join("identity.key");
225237
let identity = if key_path.exists() {
226238
Identity::load(&key_path)?
@@ -231,18 +243,54 @@ async fn main() -> anyhow::Result<()> {
231243
id
232244
};
233245

234-
let db_path = dir.join("trustchain.db");
235-
let store = trustchain_core::SqliteBlockStore::open(&db_path)
236-
.map_err(|e| anyhow::anyhow!("Failed to open database: {e}"))?;
237-
let protocol = trustchain_core::TrustChainProtocol::new(identity.clone(), store);
238-
let discovery = trustchain_transport::PeerDiscovery::new(identity.pubkey_hex(), vec![]);
239-
240-
trustchain_transport::mcp::run_mcp_stdio(
241-
std::sync::Arc::new(tokio::sync::Mutex::new(protocol)),
242-
std::sync::Arc::new(discovery),
243-
vec![], // stdio mode: no seed nodes configured (user passes via config)
244-
)
245-
.await?;
246+
// Use a high port range to avoid conflicts with user sidecars.
247+
let port_base: u16 = 18200;
248+
let http_addr = format!("127.0.0.1:{}", port_base + 2);
249+
250+
let config = NodeConfig {
251+
quic_addr: format!("0.0.0.0:{port_base}"),
252+
grpc_addr: format!("127.0.0.1:{}", port_base + 1),
253+
http_addr: http_addr.clone(),
254+
proxy_addr: format!("127.0.0.1:{}", port_base + 3),
255+
key_path,
256+
db_path: dir.join("trustchain.db"),
257+
agent_name: Some(name.clone()),
258+
..NodeConfig::default()
259+
};
260+
261+
let node = Node::new(identity, config);
262+
263+
// Grab shared handles before moving node into the background task.
264+
let protocol = node.protocol.clone();
265+
let discovery = node.discovery.clone();
266+
let seeds = crate::config::default_seed_nodes();
267+
268+
// Start the full sidecar in the background.
269+
tokio::spawn(async move {
270+
if let Err(e) = node.run().await {
271+
tracing::error!("Sidecar error: {e}");
272+
}
273+
});
274+
275+
// Wait for the sidecar HTTP server to be ready.
276+
let healthz_url = format!("http://{http_addr}/healthz");
277+
let client = reqwest::Client::new();
278+
let mut ready = false;
279+
for _ in 0..60 {
280+
tokio::time::sleep(Duration::from_millis(500)).await;
281+
if let Ok(resp) = client.get(&healthz_url).send().await {
282+
if resp.status().is_success() {
283+
ready = true;
284+
break;
285+
}
286+
}
287+
}
288+
if !ready {
289+
anyhow::bail!("MCP sidecar did not become ready within 30 seconds");
290+
}
291+
292+
// Run MCP stdio using the same protocol/discovery as the sidecar.
293+
trustchain_transport::mcp::run_mcp_stdio(protocol, discovery, seeds).await?;
246294
}
247295

248296
Commands::Sidecar {

0 commit comments

Comments
 (0)