Skip to main content

winx_code_agent/
server.rs

1//! Winx MCP server facade.
2//!
3//! Protocol catalog, session lifecycle, task handling, MCP dispatch, and tool
4//! adapters live in focused submodules under `server/`.
5
6mod agents;
7mod catalog;
8mod client_profile;
9mod coherence;
10mod executions;
11mod handler;
12mod mutations;
13mod outcomes;
14mod principal;
15mod recovery;
16mod result_delivery;
17mod sessions;
18mod tasks;
19mod tool_dispatch;
20mod usage;
21mod wait_budget;
22mod workflow_types;
23
24#[cfg(test)]
25mod tests;
26
27use std::sync::Arc;
28
29use rmcp::{
30    transport::{async_rw::AsyncRwTransport, stdio},
31    ServiceExt,
32};
33use tokio::sync::Mutex;
34use tracing::info;
35
36use crate::runtime::{configured_shell_runtime, ShellRuntime};
37use crate::state::task_state::TaskRegistry;
38use crate::state::BashState;
39use sessions::SessionRegistry;
40
41pub use sessions::SessionIsolation;
42
43/// Benchmark hook: regenerate the tool catalog without the process cache.
44#[doc(hidden)]
45pub use catalog::build_tool_catalog_uncached;
46
47/// Shared shell state for one logical Winx session.
48pub type SharedBashState = Arc<Mutex<Option<BashState>>>;
49
50/// Winx service shared by stdio or HTTP transports.
51#[derive(Clone)]
52pub struct WinxService {
53    sessions: Arc<Mutex<SessionRegistry>>,
54    tasks: Arc<Mutex<TaskRegistry>>,
55    root_bootstrap: Arc<Mutex<()>>,
56    shell_runtime: Arc<dyn ShellRuntime>,
57    mutations: mutations::MutationCoordinator,
58    execution_history: Arc<executions::history::WorkflowHistory>,
59    /// Version information advertised in the MCP handshake.
60    pub version: String,
61    isolation: SessionIsolation,
62}
63
64/// Create and start the Winx MCP server.
65pub async fn start_winx_server() -> Result<(), Box<dyn std::error::Error>> {
66    Box::pin(start_winx_server_with_runtime(configured_shell_runtime().await?)).await
67}
68
69/// Create and start the stdio MCP server with an explicit shell runtime.
70pub async fn start_winx_server_with_runtime(
71    runtime: Arc<dyn ShellRuntime>,
72) -> Result<(), Box<dyn std::error::Error>> {
73    info!("Starting Winx MCP Server");
74    let service = WinxService::with_runtime(SessionIsolation::Lenient, runtime);
75    let (stdin, stdout) = stdio();
76    let transport = AsyncRwTransport::new_server(stdin, stdout);
77    let server = Box::pin(service.serve(transport)).await?;
78    server.waiting().await?;
79    Ok(())
80}