llm-wiki-mcp
by erichi
README.md
# llm-wiki-mcp
An MCP server that gives every AI client the same markdown knowledge bases (KBs). A KB is a GitHub repo of markdown pages. Every write is one git commit.
- **Remote:** a Cloudflare Worker on the free plan. Streamable HTTP at `/mcp`, GitHub OAuth for people, bearer tokens for bots, D1 full-text search.
- **Local:** a stdio server for Claude Code and Codex that works on local clones. No database.
- **Skills:** each KB's `skills/<name>/SKILL.md` folders are served through the MCP Skills extension (SEP-2640). Clients without it call the `guide` tool.
The contract is [docs/SPEC.md](docs/SPEC.md).
## Already have a Karpathy-style LLM wiki on GitHub?
You keep raw sources, LLM-maintained pages, an index, a log and a schema file (`CLAUDE.md` or `AGENTS.md`) in one or more repos. This server can serve them as they are, after a few moves.
1. **Map your parts to the layout.** The server reads any `.md` file in the repo. It writes only in `wiki/`, `documents/`, new files in `sources/`, `index.md` and `log.md`.
- raw sources → `sources/` (read-only once filed)
- wiki pages → `wiki/` (`map` defaults to `wiki/`; `project` reads `wiki/projects/`)
- index → `index.md`. If it is missing, `index_entry` fails with `NOT_FOUND`. Create it with one heading per section first.
- log → `log.md`. If it is missing, the first `append_log` creates it with a `# Log` title.
- schema file → copy it to `skills/<kb>-guide/SKILL.md` with frontmatter `name: <kb>-guide` and a `description`. The name must equal the folder name. Keep the original `CLAUDE.md` or `AGENTS.md` for local agents.
The page folder is fixed. Pages at the repo root stay readable and searchable, but the server refuses writes there. Move them: `mkdir -p wiki && git ls-files '*.md' | grep -v / | grep -vxE 'index.md|log.md|CLAUDE.md|AGENTS.md|README.md' | xargs -I{} git mv {} wiki/`
2. **Page header.** On every write to `wiki/*.md` the server checks for `# Title`, `**Summary**:` and `**Tags**:`, then sets `**Created**:` (new pages) and `**Last Updated**:`. Old pages without the header stay readable. `edit_page` on such a page fails with `HEADER_MISSING` until the full header, `**Created**:` included, exists. To backfill, ask your own LLM: "Add the header block from docs/SPEC.md to every page in wiki/ that has none. Keep the body as it is."
3. **Binary files.** A KB holds markdown only: writes of other files fail with `BINARY_REFUSED`. Install [examples/hooks/pre-commit](examples/hooks/pre-commit) in each repo to block new binaries. Move PDFs and images to a local vault folder. Refer to them in text, for example `[[vault:scans/lease.pdf]]`: the server keeps such a link as plain text, and the local `vault_find` tool finds the file.
4. **Config.** One entry per repo. `reads` gives one-way read access:
```jsonc
"KB_CONFIG": {
"kbs": [
{ "id": "work", "repo": "owner/work-wiki", "branch": "main", "reads": [] },
{ "id": "notes", "repo": "owner/notes-wiki", "branch": "main", "reads": ["work"] }
]
}
```
A `notes` session can read and search `work` and link `[[work:Page]]`. A `work` session cannot see `notes`.
5. **Token.** Create a fine-grained GitHub token for only those repos, with **Contents: Read and write**. Store it: `npx wrangler secret put GITHUB_TOKEN`.
6. **Index existing pages.** Search, `map` and `project` read the D1 index, so fill it once per KB:
```sh
REINDEX_SECRET=... npm run reindex -- https://mcp.example.com work --all
REINDEX_SECRET=... npm run reindex -- https://mcp.example.com notes --all
```
Then copy [examples/.github/workflows/reindex.yml](examples/.github/workflows/reindex.yml) into each repo, so every push updates the index.
7. **Connect and test.** Connect a client (see [Clients](#clients)). Call `guide` with `kb: "notes"`, then `search` for the title of a page you know. The first hit must be that page.
## Configuration
| Name | Where | What |
|---|---|---|
| `KB_CONFIG` | `wrangler.jsonc` vars | `{kbs: [{id, repo, branch, reads, about?}], vaults: []}`. Ids: lowercase, digits, dashes. `about` is one line on what the KB holds. Clients read it to decide when to search the KB. |
| `ALLOWED_GITHUB_LOGINS` | vars | Comma-separated GitHub logins that may sign in. |
| `TZ_OFFSET_MINUTES` | vars | Minutes east of UTC for page and log dates. Default `0`. |
| `GITHUB_TOKEN` | secret | Fine-grained token, Contents read and write on the KB repos only. |
| `REINDEX_SECRET` | secret | HMAC key for `POST /reindex`. |
| `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` | secret | GitHub OAuth App. Optional. |
| `COOKIE_ENCRYPTION_KEY` | secret | 32+ random characters. Signs remembered consent. Optional. |
Without the three OAuth secrets, the Worker serves bot tokens only. `/mcp` answers 401 to everything else.
The local server reads a JSON file given by `--config` or `LLM_WIKI_CONFIG`. Each KB has `path` (the local clone) instead of `repo`. See [examples/local-config.json](examples/local-config.json). Relative paths and `~` resolve against the config file.
## Local setup
```sh
npm install
npm run build
node dist/stdio.js --config ~/kb/config.json
```
Each write goes to a temp file, is renamed into place, and is committed alone with `git commit --only`. Other changes in the clone stay uncommitted. The commit trailer is `Client: local`; set `LLM_WIKI_CLIENT` to change it. Search scans the files; no database is needed.
## Deploy to Cloudflare
You need a Cloudflare account and, for a custom domain, a zone on Cloudflare.
In a public fork, keep your real values out of git. Copy `wrangler.jsonc` to `wrangler.deploy.jsonc` (git-ignored), fill that copy, and add `-c wrangler.deploy.jsonc` to each `wrangler` command below. The token script reads the same file from `WRANGLER_CONFIG=wrangler.deploy.jsonc`.
1. `npm install`, then `npx wrangler login`.
2. `npx wrangler d1 create llm-wiki-mcp`. Copy the id to `<D1_DATABASE_ID>` in `wrangler.jsonc`.
3. `npx wrangler kv namespace create OAUTH_KV`. Copy the id to `<KV_NAMESPACE_ID>`.
4. Set `<KB_DOMAIN>` (for example `mcp.example.com`), `KB_CONFIG` and `ALLOWED_GITHUB_LOGINS` in `wrangler.jsonc`. To use the `workers.dev` URL, delete `routes` and `dev`.
5. `npm run db:migrate` creates the tables in D1.
6. Set the secrets: `npx wrangler secret put GITHUB_TOKEN`, then `REINDEX_SECRET`, and, for OAuth, `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`, `COOKIE_ENCRYPTION_KEY`.
7. `npm run deploy`. Check `curl https://mcp.example.com/health`.
8. Index each KB: `REINDEX_SECRET=... npm run reindex -- https://mcp.example.com <kb> --all`.
### GitHub OAuth App
GitHub → Settings → Developer settings → OAuth Apps → New OAuth App.
- Homepage URL: `https://mcp.example.com`
- Authorization callback URL: `https://mcp.example.com/callback`
Put the client id and a new client secret into the Worker secrets. The flow: the MCP client registers itself, the user sees a consent page, signs in with GitHub, and the Worker checks the login against `ALLOWED_GITHUB_LOGINS`. Commits from OAuth sessions carry `Client: oauth/<login>`.
### Bot tokens
```sh
npm run token -- create nightly # prints kbt_… once
npm run token -- list
npm run token -- revoke nightly
```
KV stores only the SHA-256 of a token, with `{name, scopes: "*", created}`. Commits carry `Client: bot/<name>`. Add `--local` to work on the `wrangler dev` state.
### Reindex Action
Search reads D1. The server updates D1 after its own writes. Commits made elsewhere (a local clone, the GitHub web editor) reach D1 through `POST /reindex`. Copy [examples/.github/workflows/reindex.yml](examples/.github/workflows/reindex.yml) into each KB repo and set the secret `REINDEX_SECRET` and the variables `LLM_WIKI_URL` and `LLM_WIKI_KB`. The Action sends changed paths (40 at most per call), or `all: true` for larger pushes. `all: true` downloads the repo as one tarball.
## Clients
**Claude Code**, remote with a bot token:
```sh
claude mcp add --transport http kb https://mcp.example.com/mcp --header "Authorization: Bearer kbt_..."
```
Leave out `--header` to sign in with OAuth (`/mcp` in Claude Code starts the login). Local:
```sh
claude mcp add kb -- node /path/to/llm-wiki-mcp/dist/stdio.js --config ~/kb/config.json
```
**Claude app** (web, desktop, mobile): Settings → Connectors → Add custom connector. URL: `https://mcp.example.com/mcp`. Sign in with GitHub when asked.
**Codex** (`~/.codex/config.toml`):
```toml
[mcp_servers.kb]
url = "https://mcp.example.com/mcp"
bearer_token_env_var = "KB_TOKEN" # remove this line and run `codex mcp login kb` for OAuth
```
**Check a deployment** with a bot token. It writes `documents/e2e/E2E Test.md` and one log line in the `work` KB:
```sh
LLM_WIKI_URL=https://mcp.example.com LLM_WIKI_TOKEN=kbt_... npm run e2e
```
### Project context in a code repo
Give each project hub a `remote:` line in its frontmatter with the git remote URL, for example `remote: git@github.com:owner/shop.git`. Several remotes are comma-separated. Then add one rule to your global `CLAUDE.md` or `AGENTS.md`:
```markdown
In a code repo, first call the KB tool `project` with `kb: work` and `repo` set to the output of `git remote get-url origin`.
```
`project` matches `repo` against `remote:` first. Without a match, it compares the last path segment with the `repo:` and `remote:` values, so a folder path also works. A git worktree has the same remote as its main repo.
## Tools
`guide`, `map`, `search`, `read`, `project`, `backlinks`, `write_page`, `edit_page`, `append_log`, `index_entry`, `vault_find`. Every tool except `vault_find` takes `kb`. Errors start with a code and end with what to do next, for example `REVISION_CONFLICT: … Try instead: read the page, merge your change, and write again with the new sha.` Details are in the spec.
## Skills over MCP (SEP-2640)
The server declares `capabilities.extensions["io.modelcontextprotocol/skills"] = {directoryRead: true}` and implements:
- `skills/list` and `skills/get`: entries with `uri`, verbatim `frontmatter`, and `resources` (`uri`, `sha256:` digest, byte size of every file).
- `resources/list` and `resources/read` for skill files, and `resources/directory/read` for skill folders.
A skill at `skills/<name>/` in KB `work` has the URI `skill://work/<name>/SKILL.md`: the KB id is the path prefix, and the last segment is the skill name. A skill whose frontmatter `name` differs from its folder name is not served, as the SEP requires.
Where this differs from the full SEP:
- `skills/list` returns one page. It never sets `nextCursor`, `ttlMs` or `cacheScope`.
- Nested skills are served as supporting files of the outer skill. They do not get their own entries.
- The listing covers the skills of every configured KB. Skills are read-only rules, so one-way KB access applies to the tools, not to skills.
## Limits on the free plan
- A Worker request may make 50 subrequests. `POST /reindex` reads at most 40 paths one by one; more paths use the tarball.
- A Worker request gets 10 ms of CPU time. A full reindex of a large KB can exceed it. Re-run it, or index in batches of paths.
- No Durable Objects: each MCP request builds a new server (stateless streamable HTTP, JSON responses).
## Development
```sh
npm test # vitest: tools on temp git clones, GitHub with a mocked fetch, D1 on node:sqlite, MCP, Worker routes
npm run typecheck
npm run build # dist/stdio.js
npm run dev # wrangler dev; put secrets and a test KB_CONFIG in .dev.vars
```
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues