MCP server Python example: a minimal server from the official docs

For agent builders4 min read

An MCP server Python example can be a single file with one decorated function. This post builds a minimal one that counts words in a text, following the pattern in the official MCP weather server tutorial. It covers the install, the tool, how to run it, and the one logging mistake that silently breaks stdio servers.

What you are building

The official tutorial builds a weather server with two tools. MCP servers can offer three kinds of capability: resources, which are file-like data a client can read; tools, which are functions the model can call with user approval; and prompts, which are ready-made templates. The tutorial focuses on tools, and so does this example.

Source: Build an MCP server (official docs)(opens in a new tab)

Set up the project

The tutorial uses uv. These are its commands for macOS and Linux.

Shell
uv init wordtoolscd wordtoolsuv venvsource .venv/bin/activateuv add "mcp[cli]"touch server.py

On Windows the tutorial gives the PowerShell equivalents: activate with .venv\Scripts\activate and create the file with new-item server.py.

The MCP server Python example in full

The tutorial imports MCPServer from mcp.server, creates one with a name, and decorates each tool function with mcp.tool(). Type hints and the docstring become the tool definition, so there is no separate schema to write. Older tutorials and many blog posts use a different class name, so follow the page for the version of the SDK you install.

Python
from mcp.server import MCPServer mcp = MCPServer("wordtools")  @mcp.tool()async def count_words(text: str) -> int:    """Count the words in a piece of text.     Args:        text: The text to count words in.    """    return len(text.split())  if __name__ == "__main__":    mcp.run(transport="stdio")

The structure copies the tutorial: server object, decorated async function with a docstring that has an Args section, and a run call at the bottom. Only the tool body is new.

Run it

Shell
uv run server.py

The tutorial says this starts the server and that it then listens for messages from MCP hosts. A stdio server does not print a banner or open a port. It waits for a host to launch it and speak to it on standard input and output.

The mistake that breaks stdio servers

The tutorial has a warning that is easy to skip. For servers that use stdio, never write to standard output. Output on stdout corrupts the JSON-RPC messages and breaks the server, and print() writes to stdout by default. If you need to log, write to stderr or to a file through the logging module.

Python
import loggingimport sys logging.basicConfig(stream=sys.stderr, level=logging.INFO)logging.info("count_words called")

Connect it to a host

A host is the application that uses the server, such as the Claude for Desktop app in the tutorial. The tutorial walks through that setup in its own page, so use it for the exact configuration steps for your version. Once connected, the model sees your tool's name, description and parameters, and asks the user before it calls it.

Keep the logic testable

A tool is easiest to trust when its logic is a plain function that you can test without any host. Put the work in a normal function, and let the decorated tool only call it. Then a normal test runner can check the behavior in milliseconds.

Python
def word_count(text: str) -> int:    return len(text.split())  def test_word_count():    assert word_count("") == 0    assert word_count("one two  three") == 3

In server.py, the tool body becomes return word_count(text). The host sees the same tool, and your tests no longer depend on the protocol.

Make the tool easy to use

Most failures with tools are failures of description. Anthropic's article on building agents recommends writing tool descriptions with example usage, edge cases and the boundary between similar tools, and choosing parameter names that make the right call obvious. It also describes changing arguments to make mistakes harder, using the example that requiring absolute file paths avoided errors.

Source: Building effective agents(opens in a new tab)

Apply that to count_words. State what counts as a word, say what happens with empty text, and keep the name specific. If you add a second tool, make sure a model can tell from the descriptions alone which one to pick.

Next steps

  • Add a second tool and a test that calls the Python functions directly, without a host.
  • Read the tutorial's weather tools to see error handling for network calls.
  • Read the official page on security best practices before you expose a tool that can write or delete data.

The code above follows the official tutorial's structure, but it has not been run against every SDK release. Install the SDK, run the file and read any import error before you build on it.