Skip to content

Commit 209dfbf

Browse files
authored
adding telemetry for mcp tool names available and mcp tool names selected (#2655)
1 parent d0aa8b8 commit 209dfbf

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,16 @@ impl ToolManagerBuilder {
374374
Err(e) => {
375375
error!("Error initializing mcp client for server {}: {:?}", name, &e);
376376
os.telemetry
377-
.send_mcp_server_init(&os.database, conversation_id.clone(), name, Some(e.to_string()), 0)
377+
.send_mcp_server_init(
378+
&os.database,
379+
conversation_id.clone(),
380+
name,
381+
Some(e.to_string()),
382+
0,
383+
Some("".to_string()),
384+
Some("".to_string()),
385+
0,
386+
)
378387
.await
379388
.ok();
380389
let _ = messenger.send_tools_list_result(Err(e)).await;
@@ -1292,6 +1301,19 @@ fn spawn_orchestrator_task(
12921301
format!("{:.2}", time_taken)
12931302
});
12941303
pending.write().await.remove(&server_name);
1304+
1305+
let result_tools = match &result {
1306+
Ok(tools_result) => {
1307+
let names: Vec<String> = tools_result
1308+
.tools
1309+
.iter()
1310+
.filter_map(|tool| tool.get("name")?.as_str().map(String::from))
1311+
.collect();
1312+
names
1313+
},
1314+
Err(_) => vec![],
1315+
};
1316+
12951317
let (tool_filter, alias_list) = {
12961318
let agent_lock = agent.lock().await;
12971319

@@ -1386,6 +1408,7 @@ fn spawn_orchestrator_task(
13861408
&alias_list,
13871409
regex,
13881410
telemetry_clone,
1411+
&result_tools,
13891412
)
13901413
.await;
13911414
if let Some(sender) = &loading_status_sender {
@@ -1620,6 +1643,7 @@ async fn process_tool_specs(
16201643
alias_list: &HashMap<HostToolName, ModelToolName>,
16211644
regex: &Regex,
16221645
telemetry: &TelemetryThread,
1646+
result_tools: &[String],
16231647
) -> eyre::Result<()> {
16241648
// Tools are subjected to the following validations:
16251649
// 1. ^[a-zA-Z][a-zA-Z0-9_]*$,
@@ -1632,6 +1656,14 @@ async fn process_tool_specs(
16321656
let mut hasher = DefaultHasher::new();
16331657
let mut number_of_tools = 0_usize;
16341658

1659+
let number_of_tools_in_mcp_server = result_tools.len();
1660+
1661+
let all_tool_names = if !result_tools.is_empty() {
1662+
Some(result_tools.join(","))
1663+
} else {
1664+
None
1665+
};
1666+
16351667
for spec in specs.iter_mut() {
16361668
let model_tool_name = alias_list.get(&spec.name).cloned().unwrap_or({
16371669
if !regex.is_match(&spec.name) {
@@ -1662,6 +1694,11 @@ async fn process_tool_specs(
16621694
// Native origin is the default, and since this function never reads native tools, if we still
16631695
// have it, that would indicate a tool that should not be included.
16641696
specs.retain(|spec| !matches!(spec.tool_origin, ToolOrigin::Native));
1697+
let loaded_tool_names = if specs.is_empty() {
1698+
None
1699+
} else {
1700+
Some(specs.iter().map(|spec| spec.name.clone()).collect::<Vec<_>>().join(","))
1701+
};
16651702
// Send server load success metric datum
16661703
let conversation_id = conversation_id.to_string();
16671704
let _ = telemetry
@@ -1671,6 +1708,9 @@ async fn process_tool_specs(
16711708
server_name.to_string(),
16721709
None,
16731710
number_of_tools,
1711+
all_tool_names,
1712+
loaded_tool_names,
1713+
number_of_tools_in_mcp_server,
16741714
)
16751715
.await;
16761716
// Tool name translation. This is beyond of the scope of what is

crates/chat-cli/src/telemetry/core.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::telemetry::definitions::types::{
3434
CodewhispererterminalCustomToolLatency,
3535
CodewhispererterminalCustomToolOutputTokenSize,
3636
CodewhispererterminalIsToolValid,
37+
CodewhispererterminalMcpServerAllToolsCount,
3738
CodewhispererterminalMcpServerInitFailureReason,
3839
CodewhispererterminalToolName,
3940
CodewhispererterminalToolUseId,
@@ -366,6 +367,9 @@ impl Event {
366367
server_name,
367368
init_failure_reason,
368369
number_of_tools,
370+
all_tool_names,
371+
loaded_tool_names,
372+
all_tools_count,
369373
} => Some(
370374
CodewhispererterminalMcpServerInit {
371375
create_time: self.created_time,
@@ -379,6 +383,11 @@ impl Event {
379383
number_of_tools as i64,
380384
)),
381385
codewhispererterminal_client_application: self.client_application.map(Into::into),
386+
codewhispererterminal_mcp_server_all_tool_names: all_tool_names.map(Into::into),
387+
codewhispererterminal_mcp_server_loaded_tool_names: loaded_tool_names.map(Into::into),
388+
codewhispererterminal_mcp_server_all_tools_count: Some(
389+
CodewhispererterminalMcpServerAllToolsCount(all_tools_count as i64),
390+
),
382391
}
383392
.into_metric_datum(),
384393
),
@@ -615,6 +624,9 @@ pub enum EventType {
615624
server_name: String,
616625
init_failure_reason: Option<String>,
617626
number_of_tools: usize,
627+
all_tool_names: Option<String>,
628+
loaded_tool_names: Option<String>,
629+
all_tools_count: usize,
618630
},
619631
AgentConfigInit {
620632
conversation_id: String,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,26 @@ impl TelemetryThread {
353353
Ok(self.tx.send(telemetry_event)?)
354354
}
355355

356+
#[allow(clippy::too_many_arguments)]
356357
pub async fn send_mcp_server_init(
357358
&self,
358359
database: &Database,
359360
conversation_id: String,
360361
server_name: String,
361362
init_failure_reason: Option<String>,
362363
number_of_tools: usize,
364+
all_tool_names: Option<String>,
365+
loaded_tool_names: Option<String>,
366+
all_tools_count: usize,
363367
) -> Result<(), TelemetryError> {
364368
let mut telemetry_event = Event::new(crate::telemetry::EventType::McpServerInit {
365369
conversation_id,
366370
server_name,
367371
init_failure_reason,
368372
number_of_tools,
373+
all_tool_names,
374+
loaded_tool_names,
375+
all_tools_count,
369376
});
370377
set_event_metadata(database, &mut telemetry_event).await;
371378

crates/chat-cli/telemetry_definitions.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@
145145
"type": "string",
146146
"description": "Name of the MCP server"
147147
},
148+
{
149+
"name": "codewhispererterminal_mcpServerAllToolNames",
150+
"type": "string",
151+
"description": "All the tool names available in the MCP server. If there are multiple tools, then the tool names are comma-delimited."
152+
},
153+
{
154+
"name": "codewhispererterminal_mcpServerLoadedToolNames",
155+
"type": "string",
156+
"description": "All the tool names loaded from MCP server as part of agent definition. If there are multiple tools, then the tool names are comma-delimited."
157+
},
148158
{
149159
"name": "codewhispererterminal_mcpServerInitFailureReason",
150160
"type": "string",
@@ -153,7 +163,12 @@
153163
{
154164
"name": "codewhispererterminal_toolsPerMcpServer",
155165
"type": "int",
156-
"description": "The number of tools provided by a mcp server"
166+
"description": "The number of tools loaded into the conversation by the mcp server"
167+
},
168+
{
169+
"name": "codewhispererterminal_mcpServerAllToolsCount",
170+
"type": "int",
171+
"description": "The number of tools available in the MCP server."
157172
},
158173
{
159174
"name": "codewhispererterminal_isCustomTool",
@@ -454,7 +469,10 @@
454469
"required": false
455470
},
456471
{ "type": "codewhispererterminal_toolsPerMcpServer" },
457-
{ "type": "codewhispererterminal_clientApplication" }
472+
{ "type": "codewhispererterminal_clientApplication" },
473+
{ "type": "codewhispererterminal_mcpServerAllToolNames" },
474+
{ "type": "codewhispererterminal_mcpServerLoadedToolNames" },
475+
{ "type": "codewhispererterminal_mcpServerAllToolsCount" }
458476
]
459477
},
460478
{

0 commit comments

Comments
 (0)