66
77import fs from 'node:fs' ;
88
9+ import { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
10+ import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js' ;
911import type { Tool } from '@modelcontextprotocol/sdk/types.js' ;
12+ import { get_encoding } from 'tiktoken' ;
1013
1114import { cliOptions } from '../build/src/cli.js' ;
1215import { ToolCategory , labels } from '../build/src/tools/categories.js' ;
@@ -15,6 +18,42 @@ import {tools} from '../build/src/tools/tools.js';
1518const OUTPUT_PATH = './docs/tool-reference.md' ;
1619const 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
1958interface 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