Skip to content

Commit fd0a919

Browse files
authored
docs: estimate tokens using tiktoken (#959)
1 parent 8d765c0 commit fd0a919

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

docs/tool-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- AUTO GENERATED DO NOT EDIT - run 'npm run docs' to update-->
22

3-
# Chrome DevTools MCP Tool Reference
3+
# Chrome DevTools MCP Tool Reference (~6661 cl100k_base tokens)
44

55
- **[Input automation](#input-automation)** (8 tools)
66
- [`click`](#click)

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"rollup-plugin-cleanup": "^3.2.1",
6868
"rollup-plugin-license": "^3.6.0",
6969
"sinon": "^21.0.0",
70+
"tiktoken": "^1.0.22",
7071
"typescript": "^5.9.2",
7172
"typescript-eslint": "^8.43.0",
7273
"yargs": "18.0.0"

scripts/generate-docs.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
import fs from 'node:fs';
88

9+
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
10+
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
911
import type {Tool} from '@modelcontextprotocol/sdk/types.js';
12+
import {get_encoding} from 'tiktoken';
1013

1114
import {cliOptions} from '../build/src/cli.js';
1215
import {ToolCategory, labels} from '../build/src/tools/categories.js';
@@ -15,6 +18,42 @@ import {tools} from '../build/src/tools/tools.js';
1518
const OUTPUT_PATH = './docs/tool-reference.md';
1619
const README_PATH = './README.md';
1720

21+
async function measureServer() {
22+
// 1. Connect to your actual MCP server
23+
const transport = new StdioClientTransport({
24+
command: 'node',
25+
args: ['./build/src/index.js'], // Point to your built MCP server
26+
});
27+
28+
const client = new Client(
29+
{name: 'measurer', version: '1.0.0'},
30+
{capabilities: {}},
31+
);
32+
await client.connect(transport);
33+
34+
// 2. Fetch all tools
35+
const toolsList = await client.listTools();
36+
37+
// 3. Serialize exactly how an LLM would see it (JSON)
38+
const jsonString = JSON.stringify(toolsList.tools, null, 2);
39+
40+
// 4. Count tokens (using cl100k_base which is standard for GPT-4/Claude-3.5 approximation)
41+
const enc = get_encoding('cl100k_base');
42+
const tokenCount = enc.encode(jsonString).length;
43+
44+
console.log(`--- Measurement Results ---`);
45+
console.log(`Total Tools: ${toolsList.tools.length}`);
46+
console.log(`JSON Character Count: ${jsonString.length}`);
47+
console.log(`Estimated Token Count: ~${tokenCount}`);
48+
49+
// Clean up
50+
enc.free();
51+
await client.close();
52+
return {
53+
tokenCount,
54+
};
55+
}
56+
1857
// Extend the MCP Tool type to include our annotations
1958
interface ToolWithAnnotations extends Tool {
2059
annotations?: {
@@ -316,7 +355,7 @@ async function generateToolDocumentation(): Promise<void> {
316355
// Generate markdown documentation
317356
let markdown = `<!-- AUTO GENERATED DO NOT EDIT - run 'npm run docs' to update-->
318357
319-
# Chrome DevTools MCP Tool Reference
358+
# Chrome DevTools MCP Tool Reference (~${(await measureServer()).tokenCount} cl100k_base tokens)
320359
321360
`;
322361

0 commit comments

Comments
 (0)