kobold_token_count
Count tokens in text to manage AI model input limits and optimize text processing for KoboldAI integration.
Instructions
Count tokens in text
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| apiUrl | No | http://localhost:5001 | |
| text | Yes |
Implementation Reference
- src/index.ts:346-357 (handler)Shared handler logic for all POST-endpoint tools like kobold_token_count: validates arguments using the tool's schema, proxies the request to the KoboldAI API endpoint, and returns the JSON response.if (postEndpoints[name]) { const { endpoint, schema } = postEndpoints[name]; const parsed = schema.safeParse(args); if (!parsed.success) { throw new Error(`Invalid arguments: ${parsed.error}`); } const result = await makeRequest(`${apiUrl}${endpoint}`, 'POST', requestData); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], isError: false, };
- src/index.ts:46-48 (schema)Zod input schema for the kobold_token_count tool: optional apiUrl and required text string for token counting.const TokenCountSchema = BaseConfigSchema.extend({ text: z.string(), });
- src/index.ts:198-202 (registration)Registration of kobold_token_count in the ListTools response, providing name, description, and input schema.{ name: "kobold_token_count", description: "Count tokens in text", inputSchema: zodToJsonSchema(TokenCountSchema), },
- src/index.ts:333-333 (registration)Mapping of kobold_token_count to its KoboldAI API endpoint and schema in the tool dispatch handler.kobold_token_count: { endpoint: '/api/extra/tokencount', schema: TokenCountSchema },
- src/index.ts:146-162 (helper)Helper function used by all tool handlers to make HTTP requests to the KoboldAI backend API.async function makeRequest(url: string, method = 'GET', body: Record<string, unknown> | null = null) { const options: RequestInit = { method, headers: body ? { 'Content-Type': 'application/json' } : undefined, }; if (body && method !== 'GET') { options.body = JSON.stringify(body); } const response = await fetch(url, options); if (!response.ok) { throw new Error(`KoboldAI API error: ${response.statusText}`); } return response.json(); }