count_tokens
Calculate token count for text using specific Gemini AI models to manage input length and API usage.
Instructions
Count tokens for a given text with a specific model
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | Text to count tokens for | |
| model | No | Model to use for token counting | gemini-2.5-flash |
Implementation Reference
- src/enhanced-stdio-server.ts:698-735 (handler)The primary handler function for the 'count_tokens' tool. It uses the Google Generative AI SDK to count tokens in the provided text using the specified model (default: gemini-2.5-flash) and returns the token count in the MCP response format.private async countTokens(id: any, args: any): Promise<MCPResponse> { try { const model = args.model || 'gemini-2.5-flash'; const result = await this.genAI.models.countTokens({ model, contents: [{ parts: [{ text: args.text }], role: 'user' }] }); return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `Token count: ${result.totalTokens}` }], metadata: { tokenCount: result.totalTokens, model } } }; } catch (error) { return { jsonrpc: '2.0', id, error: { code: -32603, message: error instanceof Error ? error.message : 'Internal error' } }; }
- src/enhanced-stdio-server.ts:308-322 (schema)Input schema specification for the count_tokens tool, defining required 'text' parameter and optional 'model' with enum from available GEMINI_MODELS.inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'Text to count tokens for' }, model: { type: 'string', description: 'Model to use for token counting', enum: Object.keys(GEMINI_MODELS), default: 'gemini-2.5-flash' } }, required: ['text']
- src/enhanced-stdio-server.ts:306-323 (registration)Registration of the 'count_tokens' tool in the getAvailableTools() method's return array, including name, description, and input schema.name: 'count_tokens', description: 'Count tokens for a given text with a specific model', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'Text to count tokens for' }, model: { type: 'string', description: 'Model to use for token counting', enum: Object.keys(GEMINI_MODELS), default: 'gemini-2.5-flash' } }, required: ['text'] }
- src/enhanced-stdio-server.ts:480-481 (registration)Dispatch case in handleToolCall() method that routes 'count_tokens' calls to the countTokens handler.case 'count_tokens': return await this.countTokens(request.id, args);