1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use super::*;
use super::cli_executor::{CliExecutor, CliExecuteParams};
use anyhow::Result;
use jsonrpc_core::{IoHandler, Params, Value, Error as JsonRpcError};
use jsonrpc_http_server::ServerBuilder;
use jsonrpc_ws_server::{ServerBuilder as WsServerBuilder};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::broadcast;
use serde_json::json;
pub struct McpServer {
// reserved: shared MCP state, held for handler closures wired during dispatch
#[allow(dead_code)]
state: Arc<McpState>,
io_handler: IoHandler,
// reserved: broadcast channel for MCP notifications to connected clients
#[allow(dead_code)]
notification_sender: broadcast::Sender<Value>,
// reserved: CLI command executor, paired with cli-exec MCP tool
#[allow(dead_code)]
cli_executor: Arc<CliExecutor>,
// reserved: read-only mode flag enforced by mutating MCP tools
#[allow(dead_code)]
read_only: bool,
}
impl McpServer {
pub fn new(project_path: PathBuf, watch: bool, read_only: bool) -> Result<Self> {
let state = McpState::new(project_path.clone(), watch)?;
let state_arc = Arc::new(state);
let mut io_handler = IoHandler::new();
// Create CLI executor
let cli_executor = Arc::new(CliExecutor::new(project_path, read_only));
// Create broadcast channel for notifications
let (notification_sender, _) = broadcast::channel(100);
// Register the single CLI execute handler
Self::register_cli_handler(&mut io_handler, cli_executor.clone());
Ok(Self {
state: state_arc,
io_handler,
notification_sender,
cli_executor,
read_only,
})
}
pub async fn start(&self, bind: &str, port: u16) -> Result<()> {
let addr: SocketAddr = format!("{}:{}", bind, port).parse()?;
let server = ServerBuilder::new(self.io_handler.clone())
.start_http(&addr)?;
log::info!("MCP Server running on http://{}", addr);
server.wait();
Ok(())
}
pub async fn start_with_websocket(&self, bind: &str, port: u16) -> Result<()> {
let addr: SocketAddr = format!("{}:{}", bind, port).parse()?;
// Start HTTP server
let http_server = ServerBuilder::new(self.io_handler.clone())
.start_http(&addr)?;
// Start WebSocket server on port + 1
let ws_addr: SocketAddr = format!("{}:{}", bind, port + 1).parse()?;
let ws_server = WsServerBuilder::new(self.io_handler.clone())
.start(&ws_addr)?;
log::info!("MCP Server running on http://{}", addr);
log::info!("WebSocket server running on ws://{}", ws_addr);
// Wait for both servers
tokio::select! {
_ = async { http_server.wait() } => {},
_ = async { ws_server.wait() } => {},
}
Ok(())
}
fn register_cli_handler(io: &mut IoHandler, cli_executor: Arc<CliExecutor>) {
// Single CLI execute handler that wraps all CLI commands
{
let executor = cli_executor.clone();
io.add_method("rustf_cli_execute", move |params: Params| {
let executor = executor.clone();
async move {
// Parse parameters
let params: CliExecuteParams = match params.parse() {
Ok(p) => p,
Err(e) => {
return Ok(json!({
"status": "error",
"error": {
"code": "INVALID_PARAMS",
"message": format!("Invalid parameters: {}", e)
}
}));
}
};
// Execute the CLI command
let result = executor.execute(
params.command,
params.subcommand,
params.args
).await;
match result {
Ok(result) => Ok(result),
Err(e) => {
Ok(json!({
"status": "error",
"error": {
"code": "EXECUTION_ERROR",
"message": format!("Command execution failed: {}", e)
}
}))
}
}
}
});
}
// Register a help/discovery endpoint
{
let read_only = cli_executor.read_only;
io.add_method("rustf_help", move |_params: Params| {
async move {
Ok::<Value, JsonRpcError>(json!({
"status": "success",
"data": {
"description": "RustF MCP Server - CLI wrapper for remote access",
"methods": [
{
"name": "rustf_cli_execute",
"description": "Execute any rustf-cli command",
"params": {
"command": "Main command (analyze, db, schema, etc.)",
"subcommand": "Optional subcommand",
"args": "Array of arguments",
"project_path": "Optional project path"
},
"examples": [
{
"description": "Analyze project",
"params": {
"command": "analyze",
"subcommand": "project",
"args": ["--format", "json"]
}
},
{
"description": "List database tables",
"params": {
"command": "db",
"subcommand": "list-tables",
"args": []
}
}
]
}
],
"read_only_mode": read_only,
"version": env!("CARGO_PKG_VERSION")
}
}))
}
});
}
let mode = if cli_executor.read_only { "read-only" } else { "read-write" };
log::info!("Registered MCP CLI wrapper in {} mode", mode);
}
}