Skip to main content
Glama

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
NameRequiredDescriptionDefault
textYesThe text to index into
indexYesPosition (0-based, negative from end)

Implementation Reference

  • 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, }; }
  • 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 }], }; } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Aaryan-Kapoor/mcp-character-tools'

If you have feedback or need assistance with the MCP directory API, please join our Discord server