notes-mcp-oauth
by authgeist
README.md
# notes-mcp-oauth
A personal notes server for AI assistants, protected with OAuth. It follows the [2026-07-28 authorization specification](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization) of the Model Context Protocol (MCP).
It runs on one computer and has three parts: the MCP server, [Keycloak](https://www.keycloak.org) for the logins, and a small demo client that shows each step. It is a learning project, not a production template.
[Run it](#running-it) | [How it works](#flows) | [Known limits](#known-limits) | [Threats and design](docs/plan.md)
## What it shows
| Feature | Standard |
| --- | --- |
| Clients discover by themselves how to log in | [RFC 9728](https://www.rfc-editor.org/rfc/rfc9728.html), Protected Resource Metadata |
| Clients identify themselves with a URL, without registering first | [Client ID Metadata Documents](https://www.ietf.org/archive/id/draft-ietf-oauth-client-id-metadata-document-03.html) (CIMD, an IETF draft) |
| Tokens work only at this server | [RFC 8707](https://www.rfc-editor.org/rfc/rfc8707.html), Resource Indicators, plus strict audience checks |
| Each tool needs its own permission | OAuth scopes and MCP scope challenges |
| Deleting needs a fresh second factor | [RFC 9470](https://www.rfc-editor.org/rfc/rfc9470.html), Step-Up Authentication |
Dynamic Client Registration is deliberately not used. The 2026-07-28 specification deprecates it.
## Architecture
```mermaid
flowchart TB
user(["User with a browser"])
client["MCP client"]
doc["Client metadata document<br/>the client's ID is this URL"]
subgraph rs["MCP server = resource server"]
direction TB
meta["Protected Resource Metadata"]
gate["Token check<br/>signature, issuer, audience, expiry"]
policy["Tool check<br/>scope and, to delete, fresh login"]
tools["Notes tools"]
db[("SQLite<br/>notes by owner")]
gate --> policy --> tools --> db
end
subgraph as["Keycloak = authorization server"]
direction TB
login["Login, consent and one-time code"]
issue["Token endpoint"]
keys["Public signing keys"]
end
client -->|"1. discover"| meta
client -->|"2. open login page"| user
user -->|"3. log in and consent"| login
login -->|"4. fetch client details"| doc
client -->|"5. exchange code for token"| issue
client -->|"6. call tool with token"| gate
gate -.->|"fetch and cache"| keys
```
The MCP server never sees the password or the one-time code, and it never issues tokens. Keycloak never sees the notes.
## Flows
### 1. Discovery
The client knows only the server's address. The first refusal tells it where to go next.
```mermaid
sequenceDiagram
participant C as MCP client
participant M as MCP server
participant K as Keycloak
C->>M: POST /mcp without a token
M-->>C: 401 with resource_metadata URL and scope notes:read
C->>M: GET /.well-known/oauth-protected-resource/mcp
M-->>C: resource, authorization_servers, scopes_supported
Note over C: Check that resource is the address it called
C->>K: GET /.well-known/oauth-authorization-server/realms/notes
K-->>C: issuer, endpoints, PKCE and CIMD support
Note over C: Check issuer, PKCE S256 and CIMD support
```
### 2. Authorization
The client's ID is an HTTPS address. Keycloak reads the document there to learn the client's name and allowed callback addresses.
```mermaid
sequenceDiagram
actor U as User in browser
participant C as MCP client
participant K as Keycloak
participant D as Client metadata document
C->>U: Open the Keycloak login page
U->>K: Authorization request with client_id URL, PKCE challenge,<br/>state, scope notes:read and resource
K->>D: GET the client_id URL
D-->>K: client_id, client_name, redirect_uris
Note over K: Check trusted domain, matching client_id,<br/>callback address and resource
K->>U: Login and consent
U->>K: Log in and agree
K-->>U: Redirect to the callback with code, state and iss
U->>C: Deliver code, state and iss
Note over C: Check state and iss
C->>K: Code, PKCE verifier and resource
K-->>C: Access token with aud set to the MCP server only
```
### 3. Tool call
```mermaid
sequenceDiagram
participant C as MCP client
participant M as MCP server
participant K as Keycloak
C->>M: tools/call read_note with access token
opt Signing keys not cached
M->>K: GET public keys
K-->>M: Keys
end
Note over M: Check signature, issuer, audience and expiry
alt Token not valid
M-->>C: 401 invalid_token
else Scope missing
M-->>C: 403 insufficient_scope with the scope needed
else Allowed
Note over M: Look only in this user's notes
M-->>C: The note
end
```
### 4. Step-up for deleting
Deleting cannot be undone, so it needs the `notes:delete` scope **and** a second factor used in the last 5 minutes.
```mermaid
sequenceDiagram
actor U as User in browser
participant C as MCP client
participant M as MCP server
participant K as Keycloak
C->>M: tools/call delete_note with access token
alt Scope notes:delete missing
M-->>C: 403 insufficient_scope, scope notes:delete
else Scope present, but login too weak or too old
M-->>C: 401 insufficient_user_authentication,<br/>acr_values 2, max_age 300
end
C->>U: Open the login page again
U->>K: Authorization request with earlier scopes plus notes:delete
Note over K: notes:delete always requires password and one-time code
K->>U: Ask for both, even if already logged in
U->>K: Password and one-time code
K-->>U: Redirect with a new code
U->>C: Deliver the code
C->>K: Code, PKCE verifier and resource
K-->>C: New token with notes:delete, acr 2, amr otp and a fresh auth_time
C->>M: tools/call delete_note with the new token
Note over M: Check scope, acr, amr and auth_time in the token itself
M-->>C: Note deleted
```
## Tools and permissions
| Tools | Scope | Fresh second factor |
| --- | --- | --- |
| `list_notes`, `search_notes`, `read_note` | `notes:read` | No |
| `create_note`, `update_note`, `restore_note_version` | `notes:write` | No |
| `delete_note`, `delete_all_notes` | `notes:delete` | Yes |
`update_note` keeps the previous version, so a bad edit can be undone.
## Known limits
- **Two Keycloak features used here are experimental:** Client ID Metadata Documents and resource indicators. Keycloak says they may change in breaking ways, so the version is pinned to 26.8.2.
- The CIMD standard is still a draft.
- Keycloak asks for consent once and then remembers it. After that, another program on the same computer could pretend to be the demo client and read or write notes. It could not delete them.
- Keycloak's consent page does not show the address the user is sent back to. The MCP specification requires that, so this rule is not met yet.
- MCP clients that run inside a web page cannot connect. The server refuses all requests made by web pages.
- A stolen access token works until it expires, after at most 5 minutes.
- Step-up shows that the user was present recently. It does not confirm each single deletion.
## Running it
You need [Node.js 24](https://nodejs.org) (24.15 or newer), [Docker](https://docs.docker.com/get-started/get-docker/) and OpenSSL 3.2 or newer, which comes with Git for Windows. It has been tried on Windows only. Run the commands in the project folder.
Install the packages:
```powershell
npm ci
```
Create the local certificates, random passwords and the secret for the demo user's one-time codes. They go into `.local/`, which git ignores. Running it again only adds what is missing:
```powershell
node scripts/setup-local.mjs
```
Start Keycloak and the small web server for the client metadata document. The first time, this downloads them:
```powershell
docker compose up -d
```
Run all the tests and checks. This waits until Keycloak is ready, and takes a few minutes, mostly waiting for new one-time codes:
```powershell
npm run check
```
### Try the demo yourself
Start the server and leave it running. Stop it with Ctrl+C. Notes are kept in `.local/notes.sqlite`:
```powershell
npm start
```
In a second terminal, start the demo client. It creates one note, reads it and deletes it again. Each new permission opens a browser tab: to read, to write and to delete. A tab where you had nothing to do closes by itself, if the browser allows it:
```powershell
npm run demo
```
In a third terminal, show the demo user's name, password and a one-time code. Run it again when the login page asks for a code, because each one works only once and for a short time:
```powershell
node scripts/demo-user.mjs
```
Your browser warns about the login page's certificate because it was made on your own computer. The same is true for Keycloak's admin console at <https://localhost:8443/admin>. Its user name and password are in `.local/keycloak.env`.
### Stop it
This stops the containers and deletes Keycloak's data. Do it also after changing `keycloak/notes-realm.json`, because the file is only read at the first start:
```powershell
docker compose down
```
## How it is tested
`npm run check` runs everything below, and stops at the first failure. The MCP server must not be running at the same time, because the checks start their own, with a temporary notes file.
| Part | What it does | Needs Keycloak |
| --- | --- | --- |
| `npm test` | Checks types and runs the tests of the storage, token checks, tools and client, with locally made tokens | No |
| `scripts/check-keycloak.mjs` | Keycloak advertises what MCP needs | Yes |
| `scripts/check-login.mjs` | Logs in through Keycloak's real pages. Wrong clients, addresses, audiences and stolen codes are refused. With `--show-token` it prints what is in the token | Yes |
| `scripts/check-stepup.mjs` | Deleting always needs a new one-time code, and ways around it fail | Yes |
| `scripts/check-server.mjs` | Real and misused tokens at the real server, and all the permissions | Yes |
| `scripts/check-client.mjs` | The demo client from start to finish, with a script as the user | Yes |
| `scripts/check-attacks.mjs` | A genuine token for another service, another user, client IDs that point inside the network, and tokens in the log | Yes |
On Windows with Node 24.15, `npm test` sometimes stops with `Worker exited unexpectedly`. Node itself crashes while the tests open many connections. It also happens with a bare Node program, without this project's code. Run the tests again if you see it. `npm run check` does that once by itself.
## Where things are
| Folder | Content |
| --- | --- |
| `src/` | The MCP server. `auth/` checks tokens and logins, `http/` answers requests, `mcp/` has the tools, `notes/` the storage |
| `client/` | The demo client. `login.ts` has all the login decisions |
| `keycloak/` | The Keycloak configuration: permissions, login steps and client rules. `theme/` hides the user name on the page for the one-time code |
| `client-document/` | The client metadata document, and two others for tests |
| `scripts/` | Setup and checks |
| `docs/` | The [project plan](docs/plan.md) with threats and decisions, and the [spec and Keycloak notes](docs/spec-notes.md): what is a MUST, what is a SHOULD, and what Keycloak supports |
## Licence
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues