Decorator tools (@peach.tool)
@peach.tool turns a Python function in your codops repo into a tool that an
AI assistant can call: Claude Code, the Claude apps, or any other client that
speaks the Model Context Protocol (MCP).
from pipe_algorithms_lib.compute import peach
@peach.tool()
def search_articles(query: str, limit: int = 10) -> list[dict]:
"""Full-text search over the article catalogue. Returns id, title and
publication date, newest first."""
...
On deploy, every @peach.tool function in your repo is served from one MCP
server for your codops:
https://api.<codops>.<envDomain>/v2/mcp
The server sits behind your codops' GitLab sign-in, like the rest of your
codops' protected pages. A repo without any @peach.tool gets no MCP server.
Three things have to be true before an assistant can call your tool:
- the function is deployed from your repo's
peach/folder; - the MCP server is switched on for your codops, which the Peach team does once;
- the client is connected and signed in.
@peach.endpoint and @peach.task, the other members of the peach
namespace, are covered on the Decorator endpoints and
Decorator tasks pages.
Writing a tool
The decorator only tags the function and returns it unchanged, so calling it in a notebook behaves exactly as before. Everything the client sees is read from the function itself.
| What the client sees | Where it comes from |
|---|---|
| Tool name | name=, or the function's name |
| Description | description=, or the docstring |
| Input schema | the signature: annotations and defaults |
| Result | the return value |
Name
Letters, digits, _ and - only, at most 64 characters, and unique across the
whole repo. Two tools with the same name fail the build.
@peach.tool(name="package-version")
def package_version(package: str) -> dict:
...
name and description have to be literal strings. The CI reads them without
running your code, so a variable or an f-string there fails the build.
Description
Write the description for the model that has to decide when to call the tool, not for a colleague reading the code. Say what the tool returns, what the arguments mean, and when it is the wrong tool.
@peach.tool()
def episode_stats(episode_id: str, days: int = 7) -> dict:
"""Play counts and completion rate for one episode over the last `days`
days. `episode_id` is the URN, e.g. urn:svt:episode:123. Use
search_episodes first if you only have a title."""
...
A tool with a vague description gets called at the wrong moments, or not at all.
Arguments
The input schema is built from the signature. Annotations become JSON types and incoming arguments are coerced to them; a parameter without a default is required.
@peach.tool()
def find(
query: str, # required
limit: int = 10, # "5" is accepted and becomes 5
since: str | None = None, # optional
tags: list[str] | None = None,
):
...
Stick to annotations that have a JSON shape: str, int, float, bool,
list, dict, Optional, or a pydantic model. An unannotated parameter
accepts any JSON value.
Arguments that do not fit the schema never reach your function. The client gets a failed tool call naming the offending field, which a model usually corrects on its own.
Result
A str is sent as is. Anything else is sent as JSON, and a dict is also
delivered as structured content, which clients can read field by field.
Keep results small. Whatever you return lands in the model's context: return the twenty rows that answer the question, not the whole table.
Errors
Raising makes the tool call fail, and the client receives the exception's type and message:
ValueError: unknown episode urn:svt:episode:999
Unlike an endpoint's 500, the message is not
hidden, because a model can only fix a call whose error it can read. So raise
errors with a message that says what to do differently, and keep secrets,
connection strings and host names out of them. The full traceback goes to the
replica log and never to the client.
Sync or async
Either works. A plain def runs in a worker thread, so a slow call does not
block the other tools.
Running a tool as a Ray task
By default a tool runs inside the MCP server itself, in the base
Python environment. That suits quick lookups.
For anything that needs its own environment, more CPU or memory, or simply takes
a while, put @peach.tool above @peach.task. The tool call then runs as a
Ray task, with all of peach.task's arguments:
@peach.tool()
@peach.task(py_env="ml", num_cpus=2)
def embed(text: str) -> list[float]:
"""Embedding vector for a piece of text, 1024 floats."""
from sentence_transformers import SentenceTransformer
...
The first call of such a tool can take several seconds while Ray starts a worker for that environment. Later calls are fast.
Deploying
Put the file in the peach/ folder at the top level of your codops repo, next
to your decorator endpoints:
peach/
codops.txt a single line naming your codops id, e.g. "sesr"
tools.py top-level .py files only, not recursive
Pushing to your default branch deploys it. The CI finds every @peach.tool by
static analysis, builds one MCP server from all of them, and adds it to your
codops' Serve applications. The same file can hold tools, endpoints and tasks.
Your base environment needs pipe-algorithms-lib 2.15.0 or later, the first
version with peach.tool.
Helpers have to live in the same file
As with endpoints, the whole source file is copied into the generated
server and only files directly under peach/ are scanned. A helper your
tool calls has to be in the same file.
Import libraries inside the function
Keep the top of the file to from pipe_algorithms_lib.compute import peach
and the standard library, and import everything else inside the function
body, as embed does above. The file is loaded while the server is being
assembled, in a process that has Ray and FastAPI but not your environment's
packages, so a module-scope import pandas can fail the deployment.
Switching the server on
Deploying a tool does not expose it. The MCP server is off by default for every codops, and stays unreachable from outside the cluster until the Peach team switches it on for yours. Ask in your usual Peach support channel and say which of the two sign-in methods you need:
| Sign-in | For | Status |
|---|---|---|
| OAuth (GitLab sign-in in the browser) | Claude Code, the Claude web and desktop apps | available on request |
peach-cli token |
scripts and clients that only let you set a header | available on request |
Until then, requests to /v2/mcp on your codops are refused.
Connecting a client
Claude Code
Where browser sign-in (OAuth) is switched on for your codops, the URL is all Claude Code needs:
claude mcp add --transport http peach-<codops> \
https://api.<codops>.<envDomain>/v2/mcp
Then run /mcp inside Claude Code, pick the server and choose
Authenticate. Your browser opens the GitLab sign-in, then a Peach page
asking whether to let Claude Code use your tools. After you allow it, /mcp
shows the server as connected and lists your tools. There is no token to copy,
and Claude Code renews the sign-in on its own.
Your codops dashboard shows the exact command for your codops, under Overview → MCP tools.
With a peach-cli token
Where browser sign-in is not switched on yet, sign in with
peach-cli and pass your token as a header:
peach auth login
claude mcp add --transport http peach-<codops> \
https://api.<codops>.<envDomain>/v2/mcp \
--header "Authorization: Bearer $(python3 -c "import json,os;print(json.load(open(os.path.expanduser('~/.peach/credentials.json')))['token'])")"
The token lasts 12 hours. When it expires the server shows as failed: sign in again, then remove and re-add the server with the new token. A token is only valid on the environment you signed in to.
The Claude apps
Where OAuth is switched on for your codops, add the server as a custom
connector: Settings → Connectors → Add custom connector, and paste the
/v2/mcp URL. Claude sends you to the GitLab sign-in, then to a Peach page
asking whether to let Claude use your tools. After you allow it, the tools
appear in the conversation.
That access is limited to this one MCP server. It does not open anything else on Peach.
On a Team or Enterprise plan, an owner of the Claude organisation has to add the connector first.
Anything else
The server speaks MCP over Streamable HTTP and is stateless: plain JSON-RPC in
a POST, a JSON response back. That makes it easy to check by hand:
curl -s https://api.<codops>.<envDomain>/v2/mcp \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
Who can call your tools
Anyone who can sign in to your codops, which means the members of your codops' GitLab group, can call every tool on the server. There is no per-tool permission.
Keep that in mind when you write a tool:
- A tool runs with your codops' credentials, not the caller's. Whatever it can read or change, every member can now read or change through an assistant.
- An assistant decides on its own when to call a tool. Prefer read-only tools, and think twice before exposing one that writes, deletes or triggers a job.
- The server logs who called which tool, in the replica log.
Testing a tool
Because the decorator returns your function unchanged, test the logic by calling it directly in a notebook or a unit test:
search_articles("eurovision", limit=3)
Once deployed, the tools/list call above shows the name, description and
schema exactly as a model sees them. Read the description there with fresh
eyes: it is all the model knows about your tool.
Arguments
| Argument | Meaning |
|---|---|
name |
the tool name. Defaults to the function's name. A literal string. |
description |
what the model reads. Defaults to the docstring. A literal string. |
@peach.tool without parentheses works too.
Troubleshooting
The build fails with a duplicate or invalid tool name. Names have to be
unique across all files in peach/ and match letters, digits, _ and -.
The build fails because an endpoint collides with the MCP server. The
server owns the /mcp route. Rename the endpoint, or give it another
route_prefix.
/v2/mcp answers 418 or is not found. The server is not switched on for
your codops, or your repo has no @peach.tool yet.
401 Invalid or expired token. With a peach-cli token: sign in again
with peach auth login and update the header, and check that you signed in to
the same environment as the URL. With browser sign-in: run /mcp and
authenticate again.
Claude Code offers no Authenticate option. Browser sign-in is not switched
on for your codops. Use a peach-cli token, or ask for it to be switched on.
403. You are signed in but not a member of the codops' GitLab group.
The assistant never calls the tool. Look at the description. It should say what the tool is for in the words a user would use when asking.
Related
- Decorator endpoints covers
@peach.endpointand thepeach/folder. - Decorator tasks covers
@peach.task, which a tool can be stacked on. - Python environments defines the environments that
py_envselects. - Command line covers
peach auth loginand the token.