Skip to main content
Glama
Aaryan-Kapoor

MCP Character Tools

Count Multiple Letters

count_letters
Read-onlyIdempotent

Count occurrences of multiple letters in text, returning counts and positions for each specified character. Analyze text at character level to overcome tokenization limitations.

Instructions

Count occurrences of multiple letters at once.

Efficiently counts several letters in a single call.

Args:

  • text (string): The text to analyze

  • letters (string[]): Array of letters to count

  • case_sensitive (boolean): Match case exactly (default: false)

Returns: Results for each letter with counts and positions.

Example: count_letters("strawberry", ["r", "s", "e"]) → r: 3, s: 1, e: 1

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYesThe text to analyze
lettersYesLetters to count
case_sensitiveNoMatch case exactly

Implementation Reference

  • Core handler function implementing the count_letters tool logic: counts occurrences of multiple specified letters in text, returns per-letter counts, positions, and total matches.
    export function countLetters(input: CountLettersInput): CountLettersOutput {
      const { text, letters, case_sensitive } = input;
      
      const results: LetterCount[] = letters.map(letter => {
        const searchLetter = case_sensitive ? letter : letter.toLowerCase();
        const searchText = case_sensitive ? text : text.toLowerCase();
        
        const positions: number[] = [];
        for (let i = 0; i < searchText.length; i++) {
          if (searchText[i] === searchLetter) {
            positions.push(i);
          }
        }
        
        return {
          letter,
          count: positions.length,
          positions,
        };
      });
    
      const total_matches = results.reduce((sum, r) => sum + r.count, 0);
    
      return {
        text,
        letters_searched: letters,
        case_sensitive,
        results,
        total_matches,
      };
    }
  • src/index.ts:90-126 (registration)
    MCP tool registration for 'count_letters', defining title, description, input schema (Zod validation), annotations, and async handler wrapper that invokes the core countLetters function and formats response.
    server.registerTool(
      "count_letters",
      {
        title: "Count Multiple Letters",
        description: `Count occurrences of multiple letters at once.
    
    Efficiently counts several letters in a single call.
    
    Args:
      - text (string): The text to analyze
      - letters (string[]): Array of letters to count
      - case_sensitive (boolean): Match case exactly (default: false)
    
    Returns: Results for each letter with counts and positions.
    
    Example: count_letters("strawberry", ["r", "s", "e"]) → r: 3, s: 1, e: 1`,
        inputSchema: z.object({
          text: z.string().min(1).describe("The text to analyze"),
          letters: z.array(z.string().length(1)).min(1).describe("Letters to count"),
          case_sensitive: z.boolean().default(false).describe("Match case exactly"),
        }).strict(),
        annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
      },
      async (params) => {
        const result = countLetters({
          text: params.text,
          letters: params.letters,
          case_sensitive: params.case_sensitive,
        });
        const summary = result.results
          .map(r => `'${r.letter}': ${r.count} at [${r.positions.join(', ')}]`)
          .join('\n');
        return {
          content: [{ type: "text" as const, text: `Letter counts in "${result.text}":\n${summary}\n\nTotal: ${result.total_matches}` }],
        };
      }
    );
  • Zod input schema for validating tool parameters: text, array of single letters, optional case_sensitive flag.
    inputSchema: z.object({
      text: z.string().min(1).describe("The text to analyze"),
      letters: z.array(z.string().length(1)).min(1).describe("Letters to count"),
      case_sensitive: z.boolean().default(false).describe("Match case exactly"),
    }).strict(),
  • TypeScript interfaces defining input (CountLettersInput), intermediate (LetterCount), and output (CountLettersOutput) structures for the countLetters function.
    export interface CountLettersInput {
      text: string;
      letters: string[];
      case_sensitive: boolean;
    }
    
    export interface LetterCount {
      letter: string;
      count: number;
      positions: number[];
    }
    
    export interface CountLettersOutput {
      text: string;
      letters_searched: string[];
      case_sensitive: boolean;
      results: LetterCount[];
      total_matches: number;
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true, destructiveHint=false, idempotentHint=true, and openWorldHint=false, covering safety and idempotency. The description adds value by specifying that it returns 'Results for each letter with counts and positions', which provides behavioral context beyond annotations. However, it doesn't mention rate limits, error conditions, or performance characteristics.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized with a clear purpose statement, parameter summary, return explanation, and example. It's front-loaded with the core functionality. The example is helpful but could be more concise; overall, most sentences earn their place with minimal waste.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (simple counting function), rich annotations (covering safety and idempotency), and 100% schema coverage, the description is mostly complete. It explains the return format despite no output schema. However, it lacks explicit guidance on tool selection versus siblings, which is a minor gap in this context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all parameters. The description repeats parameter names and adds minimal context (e.g., 'Array of letters to count', 'Match case exactly'), but doesn't provide significant additional meaning beyond what's in the schema. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Count occurrences of multiple letters at once') and distinguishes it from sibling tools like 'count_letter' (singular) and 'count_substring' (substrings). It specifies the verb ('count'), resource ('letters'), and scope ('multiple letters at once').

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by mentioning efficiency for counting 'several letters in a single call', but doesn't explicitly state when to use this tool versus alternatives like 'count_letter' or 'count_substring'. No exclusions or prerequisites are provided, leaving the agent to infer context from sibling names.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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