Skip to content

Commit 44ce959

Browse files
committed
refactor: clean up code by removing unused timeout variables and improving output handling in MCP tools tests
1 parent 2b0dfd8 commit 44ce959

File tree

5 files changed

+45
-73
lines changed

5 files changed

+45
-73
lines changed

crates/codegraph-graph/src/file_watcher.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use codegraph_core::{traits::FileWatcher, ChangeEvent, Result};
22
use crossbeam_channel::Sender;
3-
use notify::{
4-
recommended_watcher, Config, Event, PollWatcher, RecursiveMode, Watcher,
5-
};
3+
use notify::{recommended_watcher, Config, Event, PollWatcher, RecursiveMode, Watcher};
64
use std::panic::{catch_unwind, AssertUnwindSafe};
75
use std::path::Path;
86
use std::time::Duration;
@@ -34,7 +32,9 @@ impl FileWatcher for FileWatcherImpl {
3432
Ok(Ok(watcher)) => Box::new(watcher),
3533
Ok(Err(e)) => return Err(codegraph_core::CodeGraphError::Notify(e)),
3634
Err(_) => {
37-
eprintln!("⚠️ macOS FSEvents watcher unavailable; falling back to polling file watcher");
35+
eprintln!(
36+
"⚠️ macOS FSEvents watcher unavailable; falling back to polling file watcher"
37+
);
3838
let tx_clone = notify_tx.clone();
3939
let poll_config = Config::default().with_poll_interval(Duration::from_secs(2));
4040
let poll_watcher = PollWatcher::new(

crates/codegraph-mcp/src/connection.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::sync::atomic::{AtomicU64, Ordering};
1212
use std::sync::Arc;
1313
use std::time::Duration;
1414
use tokio::sync::{broadcast, oneshot, RwLock};
15-
use tokio::time::timeout;
1615
use tracing::warn;
1716
use url::Url;
1817

@@ -184,7 +183,7 @@ impl McpConnection {
184183
&self,
185184
method: &str,
186185
params: Value,
187-
timeout_dur: Duration,
186+
_timeout_dur: Duration,
188187
) -> Result<JsonRpcMessage> {
189188
let id = uuid::Uuid::new_v4().to_string();
190189
let req = JsonRpcRequest::new(json!(id.clone()), method.to_string(), Some(params));
@@ -201,14 +200,13 @@ impl McpConnection {
201200
return Err(e);
202201
}
203202

204-
let res = timeout(timeout_dur, rx).await;
203+
let res = rx.await;
205204
self.in_flight.fetch_sub(1, Ordering::SeqCst);
206205
match res {
207-
Ok(Ok(msg)) => Ok(msg),
208-
Ok(Err(_canceled)) => Err(McpError::ConnectionClosed),
209-
Err(_elapsed) => {
206+
Ok(msg) => Ok(msg),
207+
Err(_canceled) => {
210208
self.pending.remove(&id);
211-
Err(McpError::RequestTimeout(method.to_string()))
209+
Err(McpError::ConnectionClosed)
212210
}
213211
}
214212
}

crates/codegraph-mcp/src/official_server.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,12 @@ impl CodeGraphMCPServer {
277277
"⚠️ CodeGraph open failed in writable mode ({}). Falling back to read-only mode.",
278278
err
279279
);
280-
codegraph_graph::CodeGraph::new_read_only()
281-
.map_err(|ro_err| format!("Failed to initialize CodeGraph database: {} | {}", err, ro_err))
280+
codegraph_graph::CodeGraph::new_read_only().map_err(|ro_err| {
281+
format!(
282+
"Failed to initialize CodeGraph database: {} | {}",
283+
err, ro_err
284+
)
285+
})
282286
})?;
283287

284288
Ok(Self {
@@ -1044,31 +1048,21 @@ impl CodeGraphMCPServer {
10441048
let config = QwenConfig::default();
10451049
let client = QwenClient::new(config.clone());
10461050

1047-
// Try to connect with a timeout to prevent hanging
1048-
let check_result = tokio::time::timeout(
1049-
std::time::Duration::from_secs(5),
1050-
client.check_availability(),
1051-
)
1052-
.await;
1053-
1054-
match check_result {
1055-
Ok(Ok(true)) => {
1051+
match client.check_availability().await {
1052+
Ok(true) => {
10561053
eprintln!("✅ Qwen2.5-Coder-14B-128K available for CodeGraph intelligence");
10571054
let mut qwen_lock = self.qwen_client.lock().await;
10581055
*qwen_lock = Some(client);
10591056
}
1060-
Ok(Ok(false)) => {
1057+
Ok(false) => {
10611058
eprintln!(
10621059
"⚠️ Qwen2.5-Coder model not found. Install with: ollama pull {}",
10631060
config.model_name
10641061
);
10651062
}
1066-
Ok(Err(e)) => {
1063+
Err(e) => {
10671064
eprintln!("❌ Failed to connect to Qwen2.5-Coder: {}", e);
10681065
}
1069-
Err(_) => {
1070-
eprintln!("⚠️ Qwen2.5-Coder connection timed out after 5 seconds - Ollama may not be running");
1071-
}
10721066
}
10731067
}
10741068
#[cfg(not(feature = "qwen-integration"))]

crates/codegraph-mcp/src/qwen.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use codegraph_core::{CodeGraphError, Result};
22
use reqwest::Client;
33
use serde::{Deserialize, Serialize};
44
use std::time::{Duration, Instant};
5-
use tokio::time::timeout;
65
use tracing::{debug, info, warn};
76

87
/// Simple Qwen2.5-Coder client for MCP integration
@@ -127,21 +126,13 @@ impl QwenClient {
127126
self.config.context_window
128127
);
129128

130-
let response = timeout(
131-
self.config.timeout,
132-
self.client
133-
.post(&format!("{}/api/chat", self.config.base_url))
134-
.json(&request)
135-
.send(),
136-
)
137-
.await
138-
.map_err(|_| {
139-
CodeGraphError::Timeout(format!(
140-
"Qwen request timeout after {:?}",
141-
self.config.timeout
142-
))
143-
})?
144-
.map_err(|e| CodeGraphError::Network(format!("Qwen request failed: {}", e)))?;
129+
let response = self
130+
.client
131+
.post(&format!("{}/api/chat", self.config.base_url))
132+
.json(&request)
133+
.send()
134+
.await
135+
.map_err(|e| CodeGraphError::Network(format!("Qwen request failed: {}", e)))?;
145136

146137
if !response.status().is_success() {
147138
let error_text = response
@@ -197,15 +188,14 @@ impl QwenClient {
197188
self.config.base_url
198189
);
199190

200-
let response = timeout(
201-
Duration::from_secs(5),
202-
self.client
203-
.get(&format!("{}/api/tags", self.config.base_url))
204-
.send(),
205-
)
206-
.await
207-
.map_err(|_| CodeGraphError::Timeout("Qwen availability check timeout".to_string()))?
208-
.map_err(|e| CodeGraphError::Network(format!("Qwen availability check failed: {}", e)))?;
191+
let response = self
192+
.client
193+
.get(&format!("{}/api/tags", self.config.base_url))
194+
.send()
195+
.await
196+
.map_err(|e| {
197+
CodeGraphError::Network(format!("Qwen availability check failed: {}", e))
198+
})?;
209199

210200
if !response.status().is_success() {
211201
return Ok(false);

test_mcp_tools.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@
5858
}),
5959
]
6060

61-
WAIT_OVERRIDE = {
62-
101: 4.0,
63-
102: 4.0,
64-
103: 20.0,
65-
104: 25.0,
66-
105: 6.0,
67-
106: 20.0,
68-
107: 25.0,
69-
}
70-
7161
def drain(proc, seconds=2.0):
7262
"""Read stdout for up to `seconds`, print as it arrives, and return captured text."""
7363
out = []
@@ -92,7 +82,16 @@ def send(proc, obj, wait=2.0, show=True):
9282
print("="*72)
9383
proc.stdin.write(s + "\n")
9484
proc.stdin.flush()
95-
return drain(proc, wait) if wait > 0 else ""
85+
# Wait indefinitely (in 5-second chunks) until we receive output or the process exits.
86+
output = ""
87+
while True:
88+
chunk = drain(proc, 5.0)
89+
output += chunk
90+
if chunk:
91+
return output
92+
# If the process exited and nothing new arrived, break to avoid hanging forever.
93+
if proc.poll() is not None:
94+
return output
9695

9796
def extract_uuid(text: str):
9897
m = UUID_RE.search(text or "")
@@ -218,16 +217,7 @@ def run():
218217
}
219218
}
220219

221-
wait_time = WAIT_OVERRIDE.get(payload["id"], 5.0)
222-
out = send(proc, payload, wait=wait_time) + drain(proc, wait_time)
223-
224-
# If the tool still hasn't produced output (possible for long-running AI calls),
225-
# keep waiting in generous chunks until we see something or the process exits.
226-
while not out.strip():
227-
extra = drain(proc, wait_time)
228-
if not extra.strip():
229-
break
230-
out += extra
220+
out = send(proc, payload, wait=0)
231221

232222
if payload["id"] == 102:
233223
vec2_output = out

0 commit comments

Comments
 (0)