Your first run¶
In about five minutes you will build a tiny MCP server, put it behind codemode-mcp and run the kind of script a model would write. You do not need an API key, an account or any existing MCP setup.
You need: Python 3.11 or newer, and a terminal. Check with python --version (on some
systems the command is python3).
1. Install¶
Create a folder for this tutorial and install into a virtual environment, so nothing else on your machine is affected:
The [tokens] extra adds exact token counting. Now prepare the sandbox, which downloads
its runtime once (about 10 MB):
2. Write a tiny MCP server¶
An MCP server is a program that offers tools. This one offers two: list the books in a
small library, and get the reader ratings for one book. Save it as library_server.py:
from mcp.server.mcpserver import MCPServer
server = MCPServer("library")
BOOKS = {
"Dune": [5, 5, 4, 5],
"Neuromancer": [4, 3, 5],
"The Left Hand of Darkness": [5, 5, 5, 4, 5],
"Snow Crash": [4, 4, 3],
}
@server.tool()
def list_books() -> list[str]:
"""List the titles of every book in the library."""
return list(BOOKS)
@server.tool()
def get_ratings(title: str) -> list[int]:
"""Get every reader rating (1 to 5) for one book."""
return BOOKS[title]
if __name__ == "__main__":
server.run()
The mcp package it imports was installed along with codemode-mcp.
3. Tell codemode-mcp about it¶
Servers are listed in a file called mcp.json, in the same format Claude Desktop and
other clients use. Save this next to the server:
The key library becomes the name your scripts use. If your system only has python3,
write that as the command.
4. See what the model would see¶
Instead of two tools, the model is offered a single run_code tool. Its description ends
with your tools written as Python function signatures:
class library:
async def list_books() -> list[str]:
"""List the titles of every book in the library."""
async def get_ratings(title: str) -> list[int]:
"""Get every reader rating (1 to 5) for one book."""
5. Run a script¶
Suppose someone asks "which book has the best average rating?". With classic tool calling
the model would call list_books, then get_ratings four times, reading each result.
With Code Mode it writes one script. Save this as best.py:
best = {}
for title in await library.list_books():
ratings = await library.get_ratings(title)
best[title] = round(sum(ratings) / len(ratings), 2)
max(best.items(), key=lambda kv: kv[1])
Run it exactly as run_code would:
[5 tool call(s): library.list_books, library.get_ratings, library.get_ratings, library.get_ratings, library.get_ratings] [tokens: Code Mode 548 vs classic ~274]
["The Left Hand of Darkness", 4.8]
Five tool calls happened inside the sandbox. The only thing that would reach the model is the last line. Two things worth noticing about the script:
- Each server is an object (
library) and each tool is anasyncfunction, so youawaitit. - The value of the last expression is what gets returned.
print()output is returned too.
6. Read the numbers honestly¶
Look at the token note in that output: Code Mode 548 vs classic ~274. Code Mode used more here. Check the definitions on their own:
Servers: 1 Tools: 2 Strategy: inline Tokenizer: tiktoken:o200k_base
Tool-definition tokens per LLM request, classic MCP : 116
Tool-definition tokens per LLM request, Code Mode : 484
Saved per request (definitions only) : -317.2%
This is expected, and it is the most useful thing to learn early. The run_code
instructions have a fixed cost of roughly 450 tokens. Two tiny tools returning a few
numbers each are cheaper than that. Code Mode pays off when you have many tools, or when
tool results are large and the model needs only a small part of them. The
benchmarks page shows where the line falls.
Try it on your real servers
If you already use MCP servers in a client, point stats at them to see your own
numbers before changing anything:
init without --install only copies your server list into mcp.json. It does not
touch the client.
If something went wrong¶
| What you see | What to do |
|---|---|
codemode-mcp: command not found |
The virtual environment is not active. Run the activate line from step 1 again |
Failed to connect to MCP server 'library' |
The command in mcp.json did not start, and the message says why. Check that the virtual environment from step 1 is active, try python3 instead of python, and run from the folder that contains library_server.py |
setup hangs or fails |
It needs internet access once. Behind a proxy, set HTTPS_PROXY first |
Deno executable not found |
Run pip install --force-reinstall deno |
More in FAQ & troubleshooting.
Next: Connect your AI app, or skip to Your first Python agent if you are building your own.