Large Language Models (LLMs) sometimes struggle with simple counting tasks like determining the number of ‘r’s in words like “strawberry” or “raspberry”. While this is often used as a gotcha test for LLMs, there’s a practical solution – creating a Model Context Protocol (MCP) server to handle these counting tasks reliably.
MCP is an innovative open standard designed to bridge the gap between large language models (LLMs) and external data sources or tools. In this tutorial, we’ll walk you through creating a simple MCP server in Python that counts letters in words. This practical example will demonstrate the fundamentals of MCP server implementation and showcase how it can significantly extend the capabilities of LLMs.
What is Model Context Protocol?
Before diving into our implementation, let’s briefly review what MCP is. The Model Context Protocol enables LLMs to interact with external tools and resources through a standardized interface. It consists of:
- Server/Client Architecture: The server (which we’ll build) provides tools and resources that an LLM client like Claude Desktop can use
- Resources: Data that can be accessed by the LLM
- Tools: Functions that perform specific tasks
- Prompts: Templates for consistent interactions
Setting Up the Development Environment
Let’s create a Python environment for our MCP server. We’ll use uv to setup our enviornment. For more information on uv check out this introduction to uv:
# Install UV package manager
curl -LsSf https://astral.sh/uv/install.sh | sh
With uv installed we can easily use uvx to create a boilerplate project
uvx create-mcp-server
This will setup our project.
cd mcp-server-count-letters
uv sync --dev --all-extras
At this point we can execute our basic server. It won’t do much at this point.
uv run mcp-server-count-letters
I recommend adding fastmcp to the development enviornment. This will allow you to start a browser based interface to interact with the MCP server.
uv pip install fastmcp
Now in our virtual environment we can run the following command and get our browser interface running.
# Activate the virtual environment
source .venv/bin/activate
# Start the MCP Inspector
fastmcp dev src/mcp_server_count_letters/server.py
By default the server will start on localhost port 5173.

We need to update the command argument to be run mcp-server-count-letters. Then click the connect button.
Creating the Letter Counter Server
Here’s our basic MCP server implementation:
from fastmcp import FastMCP
from typing import Dict, Any
# Initialize the MCP server
mcp = FastMCP("letter-counter")
# Define the letter counting tool
@mcp.tool()
def count_letter(word: str, letter: str) -> int:
count = word.lower().count(letter.lower())
return count
# Define resources for counting 'r's in strawberry and raspberry
@mcp.resource(uri="mcp://strawberry")
def r_in_strawberry() -> int:
count = count_letter("strawberry", "r")
return count
@mcp.resource(uri="mcp://raspberry")
def r_in_raspberry() -> int:
count = count_letter("raspberry", "r")
return count
# Run the server
if __name__ == "__main__":
mcp.run()
Using the Server
Now LLMs can use our server to perform accurate letter counting:
- For predefined words:
Use the "strawberry" resource and count_r tool to count r's Result: 3 Use the "raspberry" resource and count_r tool to count r's
Result: 3
- For any text:
Use count_letter tool with "hello world"
Result:1
Extending Functionality
You could extend this basic server with additional features like:
- Counting multiple letters at once
- Case-sensitive counting
- Pattern matching using regex
- Word frequency analysis
Conclusion
By creating this simple MCP server, we’ve demonstrated how to augment LLMs with precise counting capabilities. This same pattern can be applied to other tasks where LLMs need deterministic computational support.
The complete code for this project is available at our mcp-server-letter-count GitHub repository.