Skip to content

Commit a925a35

Browse files
committed
log character counts and estimated tokens
1 parent 34b3ed4 commit a925a35

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

crates/mcp-apollo-server/src/operations.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::tree_shake::TreeShaker;
2424
pub struct Operation {
2525
tool: Tool,
2626
source_text: String,
27+
character_count: usize,
2728
}
2829
impl AsRef<Tool> for Operation {
2930
fn as_ref(&self) -> &Tool {
@@ -99,9 +100,23 @@ impl Operation {
99100
));
100101
};
101102

103+
let tool = Tool::new(operation_name.clone(), description, schema);
104+
let character_count = tool_character_length(&tool);
105+
match character_count {
106+
Ok(length) => tracing::info!(
107+
"Tool {} loaded with a character count of {}",
108+
operation_name,
109+
length
110+
),
111+
Err(_) => tracing::info!(
112+
"Tool {} loaded with an unknown character count",
113+
operation_name
114+
),
115+
}
102116
Ok(Operation {
103-
tool: Tool::new(operation_name, description, schema),
117+
tool,
104118
source_text: source_text.to_string(),
119+
character_count: character_count.unwrap_or_default(),
105120
})
106121
}
107122

@@ -225,6 +240,15 @@ impl Operation {
225240

226241
lines.join("\n")
227242
}
243+
244+
pub fn tool_character_length(&self) -> usize {
245+
self.character_count
246+
}
247+
}
248+
249+
fn tool_character_length(tool: &Tool) -> Result<usize, serde_json::Error> {
250+
let tool_schema_string = serde_json::to_string_pretty(&serde_json::json!(tool.input_schema))?;
251+
Ok(tool.name.len() + tool.description.len() + tool_schema_string.len())
228252
}
229253

230254
fn get_json_schema(

crates/mcp-apollo-server/src/server.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ impl Server {
6666
)?);
6767
}
6868

69-
info!(
69+
tracing::debug!(
7070
"Loaded operations:\n{}",
7171
serde_json::to_string_pretty(&operations)?
7272
);
7373

74+
tracing::info!("Size of tools:");
75+
let total_characters = operations
76+
.iter()
77+
.map(|op| op.tool_character_length())
78+
.sum::<usize>();
79+
tracing::info!(" characters: {}", total_characters);
80+
tracing::info!(" estimated tokens: {}", total_characters / 4);
81+
7482
// Load headers
7583
let mut default_headers = HeaderMap::new();
7684
default_headers.append(CONTENT_TYPE, HeaderValue::from_static("application/json"));

0 commit comments

Comments
 (0)