char_at
Retrieve a specific character from text by position using zero-based indexing, with support for counting from the end with negative indices.
Instructions
Get the character at a specific index (0-based). Supports negative indices.
Args:
text (string): The text to index into
index (number): Position (0-based, negative counts from end)
Returns: The character at that position, or error if out of bounds.
Example: char_at("hello", 1) → 'e'; char_at("hello", -1) → 'o'
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to index into | |
| index | Yes | Position (0-based, negative from end) |
Implementation Reference
- src/tools/spelling.ts:60-85 (handler)The core handler function implementing the 'char_at' tool logic. It extracts the character at the specified index (supporting negative indices from the end), validates the index, and returns structured output including the character, validity, and error message if applicable.export function charAt(input: CharAtInput): CharAtOutput { const { text, index } = input; const characters = [...text]; // Handle negative indices (Python-style) const normalizedIndex = index < 0 ? characters.length + index : index; if (normalizedIndex < 0 || normalizedIndex >= characters.length) { return { text, index, character: null, valid: false, text_length: characters.length, error: `Index ${index} is out of bounds. Valid range: 0 to ${characters.length - 1} (or -${characters.length} to -1 for negative indices).`, }; } return { text, index, character: characters[normalizedIndex], valid: true, text_length: characters.length, }; }
- src/tools/spelling.ts:46-58 (schema)TypeScript interfaces defining the input (text and index) and output (character, validity, length, optional error) schemas for the charAt handler.export interface CharAtInput { text: string; index: number; } export interface CharAtOutput { text: string; index: number; character: string | null; valid: boolean; text_length: number; error?: string; }
- src/index.ts:243-275 (registration)MCP server registration of the 'char_at' tool, including Zod input schema validation, detailed description with examples, annotations, and async wrapper that invokes the charAt handler and formats the response.server.registerTool( "char_at", { title: "Character At Index", description: `Get the character at a specific index (0-based). Supports negative indices. Args: - text (string): The text to index into - index (number): Position (0-based, negative counts from end) Returns: The character at that position, or error if out of bounds. Example: char_at("hello", 1) → 'e'; char_at("hello", -1) → 'o'`, inputSchema: z.object({ text: z.string().min(1).describe("The text to index into"), index: z.number().int().describe("Position (0-based, negative from end)"), }).strict(), annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }, }, async (params) => { const result = charAt({ text: params.text, index: params.index, }); const text = result.valid ? `Character at index ${result.index} in "${result.text}" is '${result.character}'` : result.error || "Invalid index"; return { content: [{ type: "text" as const, text }], }; } );