Skip to content

Commit cafa55d

Browse files
authored
fix: Disable the MCP safety warning in noninteractive use cases (aws#1833)
* diasble warining in non-interactive way * add test * fmt
1 parent 2c78865 commit cafa55d

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

crates/chat-cli/src/cli/chat/mcp.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub async fn add_mcp_server(ctx: &Context, output: &mut SharedWriter, args: McpA
6060

6161
if config.mcp_servers.contains_key(&args.name) && !args.force {
6262
bail!(
63-
"MCP server '{}' already exists in {} (scope {}). Use --force to overwrite.",
63+
"\nMCP server '{}' already exists in {} (scope {}). Use --force to overwrite.",
6464
args.name,
6565
config_path.display(),
6666
scope
@@ -74,11 +74,16 @@ pub async fn add_mcp_server(ctx: &Context, output: &mut SharedWriter, args: McpA
7474
"timeout": args.timeout.unwrap_or(default_timeout()),
7575
}))?;
7676

77+
writeln!(
78+
output,
79+
"\nTo learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
80+
)?;
81+
7782
config.mcp_servers.insert(args.name.clone(), tool);
7883
config.save_to_file(ctx, &config_path).await?;
7984
writeln!(
8085
output,
81-
"✓ Added MCP server '{}' to {}",
86+
"✓ Added MCP server '{}' to {}\n",
8287
args.name,
8388
scope_display(&scope, &args.profile)
8489
)?;
@@ -100,15 +105,15 @@ pub async fn remove_mcp_server(ctx: &Context, output: &mut SharedWriter, args: M
100105
config.save_to_file(ctx, &config_path).await?;
101106
writeln!(
102107
output,
103-
"✓ Removed MCP server '{}' from {}",
108+
"\n✓ Removed MCP server '{}' from {}\n",
104109
args.name,
105110
scope_display(&scope, &args.profile)
106111
)?;
107112
},
108113
None => {
109114
writeln!(
110115
output,
111-
"No MCP server named '{}' found in {}",
116+
"\nNo MCP server named '{}' found in {}\n",
112117
args.name,
113118
scope_display(&scope, &args.profile)
114119
)?;
@@ -154,7 +159,7 @@ pub async fn import_mcp_server(ctx: &Context, output: &mut SharedWriter, args: M
154159
for (name, cfg) in src_cfg.mcp_servers {
155160
if dst_cfg.mcp_servers.contains_key(&name) && !args.force {
156161
bail!(
157-
"MCP server '{}' already exists in {} (scope {}). Use --force to overwrite.",
162+
"\nMCP server '{}' already exists in {} (scope {}). Use --force to overwrite.\n",
158163
name,
159164
config_path.display(),
160165
scope
@@ -164,10 +169,15 @@ pub async fn import_mcp_server(ctx: &Context, output: &mut SharedWriter, args: M
164169
added += 1;
165170
}
166171

172+
writeln!(
173+
output,
174+
"\nTo learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
175+
)?;
176+
167177
dst_cfg.save_to_file(ctx, &config_path).await?;
168178
writeln!(
169179
output,
170-
"✓ Imported {added} MCP server(s) into {}",
180+
"✓ Imported {added} MCP server(s) into {}\n",
171181
scope_display(&scope, &args.profile)
172182
)?;
173183
Ok(())
@@ -199,7 +209,7 @@ pub async fn get_mcp_server_status(ctx: &Context, output: &mut SharedWriter, nam
199209
writeln!(output, "\n")?;
200210

201211
if !found {
202-
bail!("No MCP server named '{name}' found in any scope/profile");
212+
bail!("No MCP server named '{name}' found in any scope/profile\n");
203213
}
204214
Ok(())
205215
}
@@ -297,7 +307,7 @@ async fn ensure_config_file(
297307
ctx.fs().create_dir_all(parent).await?;
298308
}
299309
McpServerConfig::default().save_to_file(ctx, path).await?;
300-
writeln!(out, "📁 Created MCP config in '{}'", path.display())?;
310+
writeln!(out, "\n📁 Created MCP config in '{}'", path.display())?;
301311
}
302312
load_cfg(ctx, path).await
303313
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,15 @@ pub async fn chat(
356356

357357
let mcp_server_configs = match McpServerConfig::load_config(&mut output).await {
358358
Ok(config) => {
359-
execute!(
360-
output,
361-
style::Print(
362-
"To learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
363-
)
364-
)?;
359+
if interactive && !database.settings.get_bool(Setting::McpLoadedBefore).unwrap_or(false) {
360+
execute!(
361+
output,
362+
style::Print(
363+
"To learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html\n\n"
364+
)
365+
)?;
366+
}
367+
database.settings.set(Setting::McpLoadedBefore, true).await?;
365368
config
366369
},
367370
Err(e) => {

crates/chat-cli/src/database/settings.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub enum Setting {
2929
ApiCodeWhispererService,
3030
ApiQService,
3131
McpInitTimeout,
32+
McpLoadedBefore,
3233
}
3334

3435
impl AsRef<str> for Setting {
@@ -46,6 +47,7 @@ impl AsRef<str> for Setting {
4647
Self::ApiCodeWhispererService => "api.codewhisperer.service",
4748
Self::ApiQService => "api.q.service",
4849
Self::McpInitTimeout => "mcp.initTimeout",
50+
Self::McpLoadedBefore => "mcp.loadedBefore",
4951
}
5052
}
5153
}
@@ -73,6 +75,7 @@ impl TryFrom<&str> for Setting {
7375
"api.codewhisperer.service" => Ok(Self::ApiCodeWhispererService),
7476
"api.q.service" => Ok(Self::ApiQService),
7577
"mcp.initTimeout" => Ok(Self::McpInitTimeout),
78+
"mcp.loadedBefore" => Ok(Self::McpLoadedBefore),
7679
_ => Err(DatabaseError::InvalidSetting(value.to_string())),
7780
}
7881
}
@@ -190,10 +193,12 @@ mod test {
190193
assert_eq!(settings.get(Setting::TelemetryEnabled), None);
191194
assert_eq!(settings.get(Setting::OldClientId), None);
192195
assert_eq!(settings.get(Setting::ShareCodeWhispererContent), None);
196+
assert_eq!(settings.get(Setting::McpLoadedBefore), None);
193197

194198
settings.set(Setting::TelemetryEnabled, true).await.unwrap();
195199
settings.set(Setting::OldClientId, "test").await.unwrap();
196200
settings.set(Setting::ShareCodeWhispererContent, false).await.unwrap();
201+
settings.set(Setting::McpLoadedBefore, true).await.unwrap();
197202

198203
assert_eq!(settings.get(Setting::TelemetryEnabled), Some(&Value::Bool(true)));
199204
assert_eq!(
@@ -204,13 +209,16 @@ mod test {
204209
settings.get(Setting::ShareCodeWhispererContent),
205210
Some(&Value::Bool(false))
206211
);
212+
assert_eq!(settings.get(Setting::McpLoadedBefore), Some(&Value::Bool(true)));
207213

208214
settings.remove(Setting::TelemetryEnabled).await.unwrap();
209215
settings.remove(Setting::OldClientId).await.unwrap();
210216
settings.remove(Setting::ShareCodeWhispererContent).await.unwrap();
217+
settings.remove(Setting::McpLoadedBefore).await.unwrap();
211218

212219
assert_eq!(settings.get(Setting::TelemetryEnabled), None);
213220
assert_eq!(settings.get(Setting::OldClientId), None);
214221
assert_eq!(settings.get(Setting::ShareCodeWhispererContent), None);
222+
assert_eq!(settings.get(Setting::McpLoadedBefore), None);
215223
}
216224
}

0 commit comments

Comments
 (0)