Skip to content

Commit 3c5fbf7

Browse files
committed
adds output to show mcp init errors
1 parent 2c14ed6 commit 3c5fbf7

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

crates/q_cli/src/cli/chat/conversation_state.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,14 @@ mod tests {
614614
#[tokio::test]
615615
async fn test_conversation_state_history_handling_truncation() {
616616
let tool_manager = ToolManager::default();
617-
let mut conversation_state =
618-
ConversationState::new(Context::new_fake(), tool_manager.load_tools().await.unwrap(), None).await;
617+
let buf = Vec::<u8>::new();
618+
let mut output = std::io::BufWriter::new(buf);
619+
let mut conversation_state = ConversationState::new(
620+
Context::new_fake(),
621+
tool_manager.load_tools(&mut output).await.unwrap(),
622+
None,
623+
)
624+
.await;
619625

620626
// First, build a large conversation history. We need to ensure that the order is always
621627
// User -> Assistant -> User -> Assistant ...and so on.
@@ -636,8 +642,14 @@ mod tests {
636642
async fn test_conversation_state_history_handling_with_tool_results() {
637643
// Build a long conversation history of tool use results.
638644
let tool_manager = ToolManager::default();
639-
let mut conversation_state =
640-
ConversationState::new(Context::new_fake(), tool_manager.load_tools().await.unwrap(), None).await;
645+
let buf = Vec::<u8>::new();
646+
let mut output = std::io::BufWriter::new(buf);
647+
let mut conversation_state = ConversationState::new(
648+
Context::new_fake(),
649+
tool_manager.load_tools(&mut output).await.unwrap(),
650+
None,
651+
)
652+
.await;
641653
conversation_state.append_new_user_message("start".to_string()).await;
642654
for i in 0..=(MAX_CONVERSATION_STATE_HISTORY_LEN + 100) {
643655
let s = conversation_state.as_sendable_conversation_state().await;
@@ -659,8 +671,12 @@ mod tests {
659671
}
660672

661673
// Build a long conversation history of user messages mixed in with tool results.
662-
let mut conversation_state =
663-
ConversationState::new(Context::new_fake(), tool_manager.load_tools().await.unwrap(), None).await;
674+
let mut conversation_state = ConversationState::new(
675+
Context::new_fake(),
676+
tool_manager.load_tools(&mut output).await.unwrap(),
677+
None,
678+
)
679+
.await;
664680
conversation_state.append_new_user_message("start".to_string()).await;
665681
for i in 0..=(MAX_CONVERSATION_STATE_HISTORY_LEN + 100) {
666682
let s = conversation_state.as_sendable_conversation_state().await;
@@ -697,7 +713,10 @@ mod tests {
697713
ctx.fs().write(AMAZONQ_FILENAME, "test context").await.unwrap();
698714

699715
let tool_manager = ToolManager::default();
700-
let mut conversation_state = ConversationState::new(ctx, tool_manager.load_tools().await.unwrap(), None).await;
716+
let buf = Vec::<u8>::new();
717+
let mut output = std::io::BufWriter::new(buf);
718+
let mut conversation_state =
719+
ConversationState::new(ctx, tool_manager.load_tools(&mut output).await.unwrap(), None).await;
701720

702721
// First, build a large conversation history. We need to ensure that the order is always
703722
// User -> Assistant -> User -> Assistant ...and so on.

crates/q_cli/src/cli/chat/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<W: Write> ChatContext<W> {
282282
pub async fn new(
283283
ctx: Arc<Context>,
284284
settings: Settings,
285-
output: W,
285+
mut output: W,
286286
input: Option<String>,
287287
input_source: InputSource,
288288
interactive: bool,
@@ -293,9 +293,10 @@ impl<W: Write> ChatContext<W> {
293293
profile: Option<String>,
294294
) -> Result<Self> {
295295
let mcp_server_config = mcp_server_config.unwrap_or_default();
296-
let tool_manager = ToolManager::from_configs(mcp_server_config).await?;
296+
let tool_manager = ToolManager::from_configs(mcp_server_config, &mut output).await?;
297297
let ctx_clone = Arc::clone(&ctx);
298-
let conversation_state = ConversationState::new(ctx_clone, tool_manager.load_tools().await?, profile).await;
298+
let conversation_state =
299+
ConversationState::new(ctx_clone, tool_manager.load_tools(&mut output).await?, profile).await;
299300
Ok(Self {
300301
ctx,
301302
settings,

crates/q_cli/src/cli/chat/tool_manager.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::Arc;
1010

1111
use convert_case::Casing;
1212
use crossterm::{
13+
execute,
1314
queue,
1415
style,
1516
};
@@ -98,7 +99,7 @@ pub struct ToolManager {
9899
}
99100

100101
impl ToolManager {
101-
pub async fn from_configs(config: McpServerConfig) -> eyre::Result<Self> {
102+
pub async fn from_configs(config: McpServerConfig, output: &mut impl Write) -> eyre::Result<Self> {
102103
let McpServerConfig { mcp_servers } = config;
103104
let regex = regex::Regex::new(VALID_TOOL_NAME)?;
104105
let mut hasher = DefaultHasher::new();
@@ -131,14 +132,25 @@ impl ToolManager {
131132
}
132133
},
133134
Err(e) => {
135+
execute!(
136+
output,
137+
style::SetForegroundColor(style::Color::Red),
138+
style::Print("Error"),
139+
style::ResetColor,
140+
style::Print(": Init for MCP server "),
141+
style::SetForegroundColor(style::Color::Green),
142+
style::Print(&name),
143+
style::ResetColor,
144+
style::Print(format!(" has failed: {:?}", e))
145+
)?;
134146
error!("Error initializing for mcp client {}: {:?}", name, e);
135147
},
136148
}
137149
}
138150
Ok(Self { clients })
139151
}
140152

141-
pub async fn load_tools(&self) -> eyre::Result<HashMap<String, ToolSpec>> {
153+
pub async fn load_tools(&self, output: &mut impl Write) -> eyre::Result<HashMap<String, ToolSpec>> {
142154
let mut tool_specs = serde_json::from_str::<HashMap<String, ToolSpec>>(include_str!("tools/tool_index.json"))?;
143155
let load_tool = self
144156
.clients
@@ -169,6 +181,17 @@ impl ToolManager {
169181
}
170182
},
171183
Err(e) => {
184+
execute!(
185+
output,
186+
style::SetForegroundColor(style::Color::Red),
187+
style::Print("Error"),
188+
style::ResetColor,
189+
style::Print(": Failed to obtain tool specs for "),
190+
style::SetForegroundColor(style::Color::Green),
191+
style::Print(&server_name),
192+
style::ResetColor,
193+
style::Print(format!(": {:?}", e))
194+
)?;
172195
error!("Error obtaining tool spec for {}: {:?}", server_name, e);
173196
},
174197
}

0 commit comments

Comments
 (0)