Skip to main content
Glama
README.md
# kb-mcp

[![Build, Test and Deploy](https://github.com/uw-it-aca/kb-mcp/actions/workflows/cicd.yml/badge.svg?branch=main)](https://github.com/uw-it-aca/kb-mcp/actions/workflows/cicd.yml)
[![Coverage Status](https://coveralls.io/repos/github/uw-it-aca/kb-mcp/badge.svg?branch=main)](https://coveralls.io/github/uw-it-aca/kb-mcp?branch=main)

An [MCP](https://modelcontextprotocol.io) server that makes the team knowledge
base searchable from any MCP-capable assistant: GitHub Copilot in VS Code,
or anything else that speaks the protocol. It doesn't depend on any AI provider:
the server is plain search and file reading, and the assistant does the reasoning.

The hosted server at `https://kb-mcp.axdd.s.uw.edu` serves the team's glue
repo, [`copilot-knowledge-shared`](https://github.com/uw-it-aca/copilot-knowledge-shared):
institutional knowledge, and how apps, services and dependencies fit together.
Sign-in is with GitHub, for members of the `uw-it-aca` org. Run locally, the
server can also index other checkouts, such as app repos' documentation.

## Connecting

Use the hosted server unless you need something it doesn't cover. The first
time, your assistant opens a browser for GitHub sign-in: approve
**uw-it-aca-kb-mcp**, and the browser lands on a local "Sign-in successful"
page served by your assistant. Close it and go back. You stay signed in for
about 90 days, and access ends within an hour of leaving the org.

### VS Code (GitHub Copilot)

Command Palette → **MCP: Open User Configuration**, then add:

```json
{
  "servers": {
    "kb": {
      "type": "http",
      "url": "https://kb-mcp.axdd.s.uw.edu/mcp"
    }
  }
}
```

Start it from the code lens above the entry (or **MCP: List Servers → kb →
Start**) and choose **Authenticate** when asked. In Copilot Chat's agent mode
the `kb` tools are then available; asking about architecture, deployment or
how something works across apps should lead Copilot to them.

If **Authenticate** seems to do nothing, check the **Accounts** menu (bottom
left) for a pending sign-in request, and if there isn't one, run
**Developer: Reload Window** and start the server again. VS Code can get stuck
if it first contacted the server while it was unavailable.

### Claude Code

```bash
claude mcp add --transport http --scope user kb https://kb-mcp.axdd.s.uw.edu/mcp
```

Then, in a Claude Code session, run `/mcp`, select `kb` and authenticate.
`--scope user` makes it available in every project; leave it out to add it to
the current project only.

### GitHub Copilot CLI

**In a container (how most of the team runs it), use local mode**
([below](#running-it-locally-instead)): install `kb-mcp` in the image and mount
a checkout of `copilot-knowledge-shared` read-only. There's no sign-in, so the
container never needs to reach a browser. In `~/.copilot/mcp-config.json`
inside the container:

```json
{
  "mcpServers": {
    "kb": {
      "type": "local",
      "command": "kb-mcp",
      "args": ["--config", "/home/you/kb-sources.yml"],
      "tools": ["*"]
    }
  }
}
```

The hosted server from a container isn't tested yet. Browser sign-in ends with
a redirect to `127.0.0.1` on the host, which only reaches a CLI inside a
container with host networking, and it also depends on the CLI supporting
OAuth sign-in for remote servers.

**Possibly coming: personal tokens.** The plan is to let you sign in once in
an ordinary browser on your machine, at a page on the hosted server, and get a
token to paste into the container's CLI config as an `Authorization` header.
The container would then use the hosted server directly: always current, with
no checkout to keep pulled, nothing to install in the image, and no browser
needed inside the container. The same rules as browser sign-in would apply
(`uw-it-aca` members only, re-checked hourly, tokens expire). This isn't built
yet; it's planned alongside the write-back phase, which decides what such a
token may do. Until then, use local mode as above.

On a machine with VS Code, VS Code copies its MCP servers into
`~/.copilot/mcp-config.json` for the Copilot sessions it runs, so a `kb` entry
may already be there. That entry comes from VS Code; you don't need to add it.

### Other MCP clients

Any client that supports remote MCP servers with OAuth sign-in (dynamic client
registration) should work if it receives the sign-in result on a loopback
address (`http://127.0.0.1:<port>` or `http://localhost:<port>`), as desktop
and command-line clients usually do. Clients that use another redirect
address, such as a custom `app://` scheme, are refused until that address is
added to `KB_REDIRECT_URIS`; the server log names the refused address.

### ChatGPT, claude.ai and other web assistants

Not enabled. Web assistants connect from their provider's servers, not your
machine, so turning one on means the provider's backend reads the KB on your
behalf. That's a decision for the team, not a setting to flip quietly. Doing
it means adding the provider's OAuth callback URL to `KB_REDIRECT_URIS` in
`docker/prod-values.yml`, and, for ChatGPT, a workspace admin allowing custom
connectors.

## Status

| Phase | Scope | State |
|---|---|---|
| 0 | Local stdio server over your own checkouts, read tools | Done |
| 1 | Hosted on GKE, GitHub sign-in (org membership), synced from GitHub | **Live** at `https://kb-mcp.axdd.s.uw.edu` |
| 2 | Write-back: corrections to the glue repo as reviewed PRs, issues for everything else | Planned |

## Tools

All tools are read-only.

| Tool | Does |
|---|---|
| `list_sources` | Map of the KB: each source's role, description, README overview, layout and latest commit. Glue sources first |
| `search` | Ranked full-text search over markdown heading sections; headings and paths rank higher. Falls back to matching any word for natural-language queries |
| `grep` | Regex, line by line, for hostnames, setting names, IDs and error messages |
| `read_file` | A KB file or line range, numbered, truncated at ~60k characters |
| `list_files` | The files a source contributes, optionally under a prefix |
| `recent_changes` | Newest commits that touched KB files, with the files changed |

The index is in memory and rebuilt per source when its files change (checked at
most every 2 seconds), so a `git pull` or a local edit shows up on the next call.

## Running it locally instead

Local mode needs no sign-in and can index any checkouts you have, including
app repos' docs. It reads your files, so it's only as current as your last
`git pull`.

1. Install (Python 3.11+):

   ```bash
   pipx install git+https://github.com/uw-it-aca/kb-mcp
   ```

2. Copy [`kb-sources.example.yml`](kb-sources.example.yml) to
   `~/kb-sources.yml` and point it at your checkouts. Check it:

   ```bash
   kb-mcp --config ~/kb-sources.yml --check
   ```

3. Add the server to VS Code, in your user `mcp.json` (Command Palette →
   *MCP: Open User Configuration*) or a workspace's `.vscode/mcp.json`:

   ```json
   {
     "servers": {
       "kb": {
         "type": "stdio",
         "command": "kb-mcp",
         "args": ["--config", "${userHome}/kb-sources.yml"]
       }
     }
   }
   ```

   Copilot's agent mode then lists the `kb` tools. Keep your checkouts current
   with `git pull`; the server picks up changes on its own.

   For Claude Code: `claude mcp add --scope user kb -- kb-mcp --config ~/kb-sources.yml`.

## Hosted mode (running the server)

`kb-mcp --http` serves the KB over HTTP behind GitHub sign-in (members of the
GitHub org only) and keeps its own clone of the KB repo, synced through a
GitHub App. See [testing hosted mode locally](docs/hosted-local-test.md) for
the full setup. Settings are environment variables:

| Variable | Required | Meaning |
|---|---|---|
| `KB_PUBLIC_URL` | yes | Public base URL, e.g. `https://kb.example.uw.edu` |
| `KB_SIGNING_KEY` | yes | Random secret (32+ characters) that signs registrations and tokens. Changing it signs everyone out |
| `GITHUB_ORG` | yes | Org whose members may sign in, and where the App is installed |
| `GITHUB_APP_ID`, `GITHUB_APP_CLIENT_ID`, `GITHUB_APP_CLIENT_SECRET` | yes | The GitHub App |
| `GITHUB_APP_PRIVATE_KEY` or `GITHUB_APP_PRIVATE_KEY_FILE` | yes | The App's private key, inline or as a file path |
| `KB_REPO` | | `owner/name` of the KB repo to sync. Without it, `--config` sources are read from disk as in local mode |
| `KB_SYNC_INTERVAL` | | Seconds between fetches (default 300, minimum 30) |
| `KB_DATA_DIR` | | Where the clone lives (default `/tmp/kb-mcp`) |
| `KB_REDIRECT_URIS` | | Extra exact OAuth redirect URIs to allow, comma-separated. Loopback and `vscode.dev` are always allowed |
| `KB_SETTINGS_DIR` | | Directory of setting files the server re-reads every 30 s, so they change without a restart (in the cluster: a mounted Secret). Currently one: `log-missed-queries` |
| `KB_LOG_MISSED_QUERIES` | | `true` to log searches that found nothing (see below). Ignored when `KB_SETTINGS_DIR` is set; the file there decides |
| `KB_LOG_FORMAT` | | `json` for JSON log lines outside `--http` (hosted mode always uses them) |

The GitHub App needs *Members: read* (organization) and *Contents: read*
(repository), installed on the org with only the KB repo selected.

## What's logged

The aim is to see how the KB is used, and where it falls short, without
recording who uses it.

- **Sign-ins**: `signed in GitHub user id=… login=…`, and refused or failed
  sign-ins. This is the only record tied to a person, and it says nothing
  about what they did afterwards.
- **One line per tool call**, with no user identity and no question or answer
  text: the tool, how long it took, whether it worked, and counts (search
  results and whether search fell back to matching any word, grep matches,
  files listed, commits shown). For `read_file`, the document's path, which
  lines were read and whether the output was truncated, which shows which
  docs are used and which are too long.
- **Searches that found nothing** (`search` with no results, `grep` with no
  matches): the query text is added to that line **only when
  `log-missed-queries` is on**. It's off by default. When on, the text is
  redacted like KB content and cut to 200 characters, still with no user
  identity. Natural-language questions rarely find nothing (search falls back
  to any word), so what this catches is specific terms the KB doesn't contain:
  hostnames, product names, acronyms.
- **Requests**: method, path and status. Query strings are dropped (on the
  sign-in routes they carry one-time codes), and so are health checks.
- **Sync and redaction**: commits picked up, files indexed, failed fetches,
  and the file and line of any redacted credential (never the value).

Hosted mode writes JSON lines, so these fields can be queried directly in the
cluster's logging.

## What is and isn't exposed

- **Files are listed through git** (tracked files plus untracked files that
  aren't ignored), never by walking the directory. Anything a repo's
  `.gitignore` keeps out of git, such as a local credential file, stays out of
  the KB, even if it matches an include glob.
- **Only files matching the source's include globs** are indexed or readable.
  `read_file` refuses everything else, including `..` paths and symlinks that
  leave the repo.
- **App repos contribute docs only.** Code is never indexed; the assistant
  already sees code in your workspace.
- Files over 2 MB and binary files are skipped.
- **Credentials are redacted** before anything is indexed or returned:
  private key blocks, GitHub/AWS/Google/Slack token formats, JWTs and passwords
  in URLs become `[REDACTED:<rule>]`, line numbers unchanged. The server logs
  the file and line (never the value) so the source can be fixed.
- Git runs with `core.fsmonitor` forced off, since that is the one repo-config
  setting a read-only command would otherwise execute.
- Content is returned to the assistant as reference data, and the server's
  instructions tell it to treat it that way. Even so, keep secrets out of the
  KB repos: anything in them is readable through this server.

## Deployment

Production runs at `https://kb-mcp.axdd.s.uw.edu` on GKE through the shared
uw-it-aca CI/CD and Helm chart. See [deployment](docs/deployment.md) for the
one-time setup (production GitHub App, Vault, DNS) and troubleshooting.

## Development

The test suite runs in Docker, which is also how the hosted version ships:

```bash
docker build --target app-test-container -t kb-mcp:test .
```

```bash
docker run --rm kb-mcp:test
```

Or in a virtualenv: `pip install -e '.[test]'` and then `pytest`.

Layout:

- `src/kb_mcp/config.py`: loads and validates `kb-sources.yml`
- `src/kb_mcp/sources.py`: one checkout, which files it contributes (via git), and its history
- `src/kb_mcp/index.py`: heading-section splitting and the SQLite FTS5 index
- `src/kb_mcp/kb.py`: the tool operations, as plain methods returning text
- `src/kb_mcp/server.py`: MCP wiring (`mcp` SDK 2.x `MCPServer`)
- `src/kb_mcp/hosted.py`: hosted mode (HTTP, settings from environment variables)
- `src/kb_mcp/auth.py`: OAuth authorization server that delegates sign-in to GitHub; stateless signed tokens
- `src/kb_mcp/github.py`: GitHub OAuth exchange, org membership checks and repo lookups via the GitHub App
- `src/kb_mcp/sync.py`: the hosted server's own clone of the KB repo
- `src/kb_mcp/redact.py`: credential redaction
- `src/kb_mcp/logs.py`: log format (JSON lines in hosted mode) and noise filters
- `src/kb_mcp/usage.py`: the per-tool-call log line and the live `log-missed-queries` setting