Skip to main content
Glama
forgejo-readonly-mcp
by aiekick
README.md
# forgejo-readonly-mcp

A read-only MCP server for Forgejo. It lets an MCP client read the state of
repositories and of Forgejo Actions: runs, jobs, statuses and logs. It
cannot write anything, by design.

## The read-only contract

Three independent locks, each enough on its own:

1. **The client only speaks GET.** `ReadOnlyClient` has a single network
   method, and it builds GET requests only. No tool can send another verb.
2. **The tool list is closed.** There is no "call any endpoint" tool. Each
   tool assembles its own path from validated segments under `/api/v1`. An
   owner, repo or path component with `/`, `..` or any character outside
   `[A-Za-z0-9_.-]` is refused by name.
3. **The token only carries read scopes.** Even a bug in this code would be
   refused by Forgejo.

Two further guards:
- A redirect to another origin is refused, because urllib would forward the
  token with it.
- A plain `http://` URL is refused unless `FORGEJO_ALLOW_HTTP=1` is set.

Left out on purpose: runners and their registration tokens, secrets,
variables, artifacts, and the run-wide log zip.

## Tools

| Tool | Reads |
|---|---|
| `repo_info` | default branch, visibility, counters |
| `list_branches`, `list_tags` | refs with their head commit |
| `list_commits` | commits on a ref, optionally touching a path |
| `get_commit` | full message, parents, stats, changed files |
| `commit_status` | combined CI status of a commit and each check |
| `list_dir`, `read_file` | the tree and files at a ref |
| `list_runs` | Actions runs, filtered by status, event, ref, sha, workflow |
| `get_run`, `list_jobs` | one run and its jobs |
| `job_logs` | a job log: tail (200 by default), head, or grep with context |

## Token

In Forgejo, go to Settings, then Applications, then Generate new token. Give
it `read:repository` only (add `read:organization` if you read
organization repositories you are not a member of).

## Install

```sh
python3 -m venv .venv
.venv/bin/pip install -e .
```

## Register in Claude Code

Keep the token in a file only its owner reads, and point the server at it.
Passing it with `-e FORGEJO_TOKEN=...` would copy it in clear into
`~/.claude.json`.

```sh
install -m 600 /dev/null ~/.config/forgejo-readonly-mcp/token   # then paste the token in it
claude mcp add forgejo \
  -e FORGEJO_URL=https://forge.example.com \
  -e FORGEJO_TOKEN_FILE=$HOME/.config/forgejo-readonly-mcp/token \
  -- /path/to/forgejo-readonly-mcp/.venv/bin/forgejo-readonly-mcp
```

`FORGEJO_TOKEN` still works for hosts without a file; setting both is
refused.

Optional: `FORGEJO_ALLOWED_REPOS=owner/repo,owner/other` restricts the
server to those repositories. Any other repository is refused by name.

## Tests

The tests run against a local fake server and never reach a real forge:

```sh
.venv/bin/python -m unittest discover -s tests
```

## License

MIT, see [LICENSE](LICENSE).