Skip to content

Commit b4dc8d4

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

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 18 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,16 @@ 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!(" {}: {}", operation_name, length),
107+
Err(_) => tracing::info!(" {}: unknown", operation_name),
108+
}
102109
Ok(Operation {
103-
tool: Tool::new(operation_name, description, schema),
110+
tool,
104111
source_text: source_text.to_string(),
112+
character_count: character_count.unwrap_or_default(),
105113
})
106114
}
107115

@@ -225,6 +233,15 @@ impl Operation {
225233

226234
lines.join("\n")
227235
}
236+
237+
pub fn tool_character_length(&self) -> usize {
238+
self.character_count
239+
}
240+
}
241+
242+
fn tool_character_length(tool: &Tool) -> Result<usize, serde_json::Error> {
243+
let tool_schema_string = serde_json::to_string_pretty(&serde_json::json!(tool.input_schema))?;
244+
Ok(tool.name.len() + tool.description.len() + tool_schema_string.len())
228245
}
229246

230247
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!("Tool character lengths");
75+
let total_characters = operations
76+
.iter()
77+
.map(|op| op.tool_character_length())
78+
.sum::<usize>();
79+
tracing::info!("Total 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)