Skip to content

Commit 8614f2d

Browse files
authored
Merge pull request #30 from apollographql/jeffrey/log-character-length
log character counts and estimated tokens
2 parents 3718b0a + 30b50f0 commit 8614f2d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,22 @@ impl Operation {
187187
));
188188
};
189189

190+
let tool: Tool = Tool::new(operation_name.clone(), description, schema);
191+
let character_count = tool_character_length(&tool);
192+
match character_count {
193+
Ok(length) => tracing::info!(
194+
"Tool {} loaded with a character count of {}. Estimated tokens: {}",
195+
operation_name,
196+
length,
197+
length / 4 // We don't know the tokenization algorithm, so we just use 4 characters per token as a rough estimate. https://docs.anthropic.com/en/docs/resources/glossary#tokens
198+
),
199+
Err(_) => tracing::info!(
200+
"Tool {} loaded with an unknown character count",
201+
operation_name
202+
),
203+
}
190204
Ok(Operation {
191-
tool: Tool::new(operation_name, description, schema),
205+
tool,
192206
source_text: source_text.to_string(),
193207
persisted_query_id,
194208
})
@@ -318,6 +332,11 @@ impl Operation {
318332
}
319333
}
320334

335+
fn tool_character_length(tool: &Tool) -> Result<usize, serde_json::Error> {
336+
let tool_schema_string = serde_json::to_string_pretty(&serde_json::json!(tool.input_schema))?;
337+
Ok(tool.name.len() + tool.description.len() + tool_schema_string.len())
338+
}
339+
321340
fn get_json_schema(
322341
operation: &Node<OperationDefinition>,
323342
graphql_schema: &GraphqlSchema,

0 commit comments

Comments
 (0)