Skip to content

Commit 64e9e3d

Browse files
committed
log character counts and estimated tokens
1 parent ea3f676 commit 64e9e3d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Operation {
2525
tool: Tool,
2626
source_text: String,
2727
persisted_query_id: Option<String>,
28+
character_count: usize,
2829
}
2930

3031
impl AsRef<Tool> for Operation {
@@ -118,10 +119,24 @@ impl Operation {
118119
));
119120
};
120121

122+
let tool = Tool::new(operation_name.clone(), description, schema);
123+
let character_count = tool_character_length(&tool);
124+
match character_count {
125+
Ok(length) => tracing::info!(
126+
"Tool {} loaded with a character count of {}",
127+
operation_name,
128+
length
129+
),
130+
Err(_) => tracing::info!(
131+
"Tool {} loaded with an unknown character count",
132+
operation_name
133+
),
134+
}
121135
Ok(Operation {
122-
tool: Tool::new(operation_name, description, schema),
136+
tool,
123137
source_text: source_text.to_string(),
124138
persisted_query_id,
139+
character_count: character_count.unwrap_or_default(),
125140
})
126141
}
127142

@@ -247,6 +262,15 @@ impl Operation {
247262

248263
lines.join("\n")
249264
}
265+
266+
pub fn tool_character_length(&self) -> usize {
267+
self.character_count
268+
}
269+
}
270+
271+
fn tool_character_length(tool: &Tool) -> Result<usize, serde_json::Error> {
272+
let tool_schema_string = serde_json::to_string_pretty(&serde_json::json!(tool.input_schema))?;
273+
Ok(tool.name.len() + tool.description.len() + tool_schema_string.len())
250274
}
251275

252276
fn get_json_schema(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ impl Server {
110110
(None, None)
111111
};
112112

113+
tracing::info!("Size of tools:");
114+
let total_characters = operations
115+
.iter()
116+
.map(|op| op.tool_character_length())
117+
.sum::<usize>();
118+
tracing::info!(" characters: {}", total_characters);
119+
tracing::info!(" estimated tokens: {}", total_characters / 4);
120+
113121
// Load headers
114122
let mut default_headers = HeaderMap::new();
115123
default_headers.append(CONTENT_TYPE, HeaderValue::from_static("application/json"));

0 commit comments

Comments
 (0)