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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! MCP (Model Context Protocol) Command Handler
//!
//! Handles starting the MCP server for code analysis tools.
use anyhow::Result;
use std::path::Path;
/// Handle MCP server command
///
/// Starts a Model Context Protocol server that exposes Ruchy's code analysis,
/// scoring, linting, formatting, and transpilation capabilities as MCP tools.
///
/// # Arguments
/// * `name` - Server name for MCP identification
/// * `streaming` - Enable streaming updates
/// * `timeout` - Session timeout in seconds
/// * `min_score` - Minimum quality score threshold
/// * `max_complexity` - Maximum complexity threshold
/// * `verbose` - Enable verbose logging
/// * `config` - Optional configuration file path
///
/// # Examples
/// ```no_run
/// // This function is typically called by the CLI
/// // handle_mcp_command("ruchy-mcp", false, 3600, 0.8, 10, false, None);
/// ```
///
/// # Errors
/// Returns error if MCP server cannot be started or configured
#[cfg(feature = "mcp")]
pub fn handle_mcp_command(
name: &str,
_streaming: bool,
_timeout: u64,
_min_score: f64,
_max_complexity: u32,
verbose: bool,
_config: Option<&Path>,
) -> Result<()> {
use anyhow::Context;
use ruchy::mcp::{create_ruchy_mcp_server, create_ruchy_tools, StdioTransport};
if verbose {
eprintln!("🚀 Starting Ruchy MCP Server: {}", name);
}
// Create the MCP server with tools
let server = create_ruchy_mcp_server().context("Failed to create MCP server")?;
// Register all Ruchy tools
let tools = create_ruchy_tools();
if verbose {
eprintln!(" Registered {} tools:", tools.len());
for (tool_name, tool) in &tools {
eprintln!(" - {}: {}", tool_name, tool.description());
}
}
if verbose {
eprintln!(" Transport: stdio");
eprintln!(" Awaiting MCP client connection...");
}
// Create async runtime for the server
let runtime = tokio::runtime::Runtime::new().context("Failed to create async runtime")?;
runtime.block_on(async {
let transport = StdioTransport::new();
if verbose {
eprintln!("✅ MCP server running");
}
// Run the server with stdio transport
server.run(transport).await.context("MCP server error")
})
}
#[cfg(not(feature = "mcp"))]
pub fn handle_mcp_command(
_name: &str,
_streaming: bool,
_timeout: u64,
_min_score: f64,
_max_complexity: u32,
_verbose: bool,
_config: Option<&Path>,
) -> Result<()> {
anyhow::bail!("MCP support not enabled. Rebuild with: cargo build --features mcp")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mcp_handler_stub() {
// MCP handler tests require the mcp feature
// This is a placeholder
}
// ===== EXTREME TDD Round 150 - MCP Handler Tests =====
#[test]
#[cfg(not(feature = "mcp"))]
fn test_handle_mcp_command_no_feature() {
// Without the mcp feature the command must fail with an error the caller can
// report (main prints it and exits 1) — never by exiting the process itself,
// which killed every test binary that touched this function (PMAT-104).
let err = handle_mcp_command("test-server", false, 3600, 0.8, 10, false, None)
.expect_err("mcp stub must return Err without the mcp feature");
assert!(
err.to_string().contains("MCP support not enabled"),
"unexpected error text: {err}"
);
}
#[test]
fn test_mcp_command_accepts_parameters() {
// Verify function signature
let _ = handle_mcp_command("test-server", false, 3600, 0.8, 10, false, None);
}
#[test]
fn test_mcp_command_with_streaming() {
let _ = handle_mcp_command(
"streaming-server",
true, // streaming
3600,
0.8,
10,
false,
None,
);
}
#[test]
fn test_mcp_command_with_verbose() {
let _ = handle_mcp_command(
"verbose-server",
false,
3600,
0.8,
10,
true, // verbose
None,
);
}
#[test]
fn test_mcp_command_various_timeouts() {
let timeouts = [60, 300, 3600, 86400];
for timeout in &timeouts {
let _ = handle_mcp_command("test", false, *timeout, 0.8, 10, false, None);
}
}
#[test]
fn test_mcp_command_various_scores() {
let scores = [0.0, 0.5, 0.8, 1.0];
for score in &scores {
let _ = handle_mcp_command("test", false, 3600, *score, 10, false, None);
}
}
#[test]
fn test_mcp_command_various_complexity() {
let complexities = [1, 5, 10, 20, 50];
for complexity in &complexities {
let _ = handle_mcp_command("test", false, 3600, 0.8, *complexity, false, None);
}
}
#[test]
fn test_mcp_command_with_config() {
let _ = handle_mcp_command(
"config-server",
false,
3600,
0.8,
10,
false,
Some(Path::new("/path/to/config.toml")),
);
}
#[test]
fn test_mcp_command_all_options() {
let _ = handle_mcp_command(
"full-server",
true,
7200,
0.9,
15,
true,
Some(Path::new("./mcp.toml")),
);
}
#[test]
fn test_mcp_command_empty_name() {
let _ = handle_mcp_command("", false, 3600, 0.8, 10, false, None);
}
}