Metadata-Version: 2.5
Name: valohai-mcp
Version: 0.1.0
Summary: MCP server for the Valohai MLOps platform
License-Expression: MIT
License-File: LICENSE
Requires-Python: >=3.14
Requires-Dist: fastmcp>=4.2.3
Requires-Dist: httpx>=0.28.1
Requires-Dist: pydantic>=2.12
Description-Content-Type: text/markdown

# valohai-mcp (research preview)

An [MCP](https://modelcontextprotocol.io) server for Valohai. It lets AI assistants (Claude Code, Claude Desktop, IDE agents) work with Valohai as the signed-in user. For example, you can ask "find my latest failed execution and explain what happened".

This is a research preview: tools, their output and the hosted server may change or break without notice.

## Install in Claude Code

```sh
claude mcp add --transport http valohai https://mcp.valohai.com/mcp
```

Then run `/mcp` in Claude Code, choose `valohai` and sign in to Valohai. Ask "find my latest failed execution and explain what happened", or run `/mcp__valohai__explain_latest_failure`.

Signing in gives the server a Valohai access token. A read-only token (`token:read` scope) can't change data; a full one (`token` scope) can. The tools see only what you can see in Valohai.

## Run it locally

Install [uv](https://docs.astral.sh/uv/) and clone this repository. Create a Valohai API token at `<your Valohai URL>/auth/tokens/`; it can be read-only. Keep the token in a variable or a secret store rather than typing it on the command line, where it would end up in your shell history.

### With a Bearer token from the client

Run the server against your Valohai installation:

```sh
VALOHAI_HOST=https://valohai.example.com uv run valohai-mcp
```

Connect Claude Code, sending your token with each request:

```sh
claude mcp add --transport http valohai http://localhost:8765/mcp --header "Authorization: Bearer $VALOHAI_TOKEN"
```

The server checks the token with Valohai and uses it to call the Valohai API. The client saves the token in its configuration.

### With `VALOHAI_HOST` and `VALOHAI_TOKEN`

Run the server in token mode:

```sh
VALOHAI_HOST=https://valohai.example.com VALOHAI_TOKEN="$VALOHAI_TOKEN" uv run valohai-mcp
```

Connect Claude Code with no credentials:

```sh
claude mcp add --transport http valohai http://localhost:8765/mcp
```

The server uses the token for every Valohai API call. It doesn't check the token when it starts: if Valohai rejects it, tool calls fail with an error that says so. Check the token with `curl -H "Authorization: Token $VALOHAI_TOKEN" https://valohai.example.com/api/v0/users/me/`.

Anything that can reach the server's port acts as the token's user, except web pages in a browser, which the server refuses by their `Host` and `Origin` headers. Keep the server on localhost.

### Configuration

| Variable                 | Default                   | Meaning                                                                  |
| ------------------------ | ------------------------- | ------------------------------------------------------------------------ |
| `VALOHAI_HOST`           | `https://app.valohai.com` | The Valohai installation. It must equal that installation's OIDC issuer. |
| `VALOHAI_TOKEN`          | none (OAuth mode)         | Token mode: every API call uses this token, and clients don't sign in    |
| `VALOHAI_MCP_HOST`       | `localhost`               | Address to listen on                                                     |
| `VALOHAI_MCP_PORT`       | `8765`                    | Port to listen on                                                        |
| `VALOHAI_MCP_PUBLIC_URL` | `http://{host}:{port}`    | The URL that MCP clients use to reach this server                        |

### Health check

`GET /health` answers `200 OK` while the server is running. It needs no token and doesn't call Valohai, so it's a liveness check: it doesn't tell whether Valohai is reachable.

The server refuses requests whose `Host` isn't localhost or the host of `VALOHAI_MCP_PUBLIC_URL`, with 421. A probe that connects by IP address, such as a Kubernetes `httpGet` probe, must send that host in its `Host` header.

## Tools and prompts

| Name                            | Kind   | What it does                                                                                                                                   |
| ------------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `get_context`                   | tool   | Orientation: the user's projects, or for one project its newest commit and what it defines, environments, and unfinished and recent work      |
| `list_executions`               | tool   | The most recent executions, by default only the user's own; can be filtered by status and step                                                 |
| `get_execution_failure_details` | tool   | For one execution, given by ID, URL or counter (by default the user's latest failure): what ran, the status log, alerts and the end of the log |
| `compare_executions`            | tool   | Two executions side by side (by default the user's latest and the previous one of its step): what each is, what it ran and its price           |
| `explain_latest_failure`        | prompt | "Find my latest failed execution and explain what happened"                                                                                    |

## How it works

Valohai is the OAuth authorization server. When an MCP client connects, it finds Valohai through this server's metadata, registers itself, and asks the user to sign in to Valohai and approve access. The Valohai access token it receives is sent with each MCP request, and this server uses it to call the Valohai API. In OAuth mode, this server stores no credentials; in token mode, it holds the token from its environment.

```
MCP client ──(MCP, Bearer token)──> valohai-mcp ──(Bearer token)──> Valohai API (/api/v0/)
     └──(discovery, registration, sign-in, token)──> Valohai OAuth (/oidc/…)
```

## Development

Install [uv](https://docs.astral.sh/uv/), [just](https://just.systems/) and [prek](https://prek.j178.dev/).

```sh
just setup        # Python, dependencies and commit hooks
just serve        # run the server (configured with VALOHAI_* environment variables)
just dev-client   # check the OAuth flow by hand against a running server
just test         # pytest
just test-all     # pytest on all supported Python versions
just lint         # prek: ruff, typos, whitespace
```

Put your own recipes in `justfile.local`; it is imported when it exists and ignored by git.

See [AGENTS.md](AGENTS.md) for the code layout and conventions.
