Skip to content

Point MCP clients at the Writebook to sign in with - #21

Open
monorkin wants to merge 21 commits into
mainfrom
mcp-oauth-discovery
Open

monorkin wants to merge 21 commits into
mainfrom
mcp-oauth-discovery

Conversation

@monorkin

@monorkin monorkin commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

This is the CLI part of letting MCP clients sign in to Writebook with OAuth. The Writebook part is basecamp/writebook#524. mcp serve needs a Writebook with #524. Signing in with the CLI still works with older Writebooks.

Breaking change for the release notes: --serve is replaced by mcp serve, and the server no longer accepts tokens pasted into an MCP client; agents sign in with OAuth instead.

writebook mcp serve

  • writebook mcp --serve <address> is now writebook mcp serve. It listens on 127.0.0.1:8080 by default, and --bind changes the address. writebook mcp without serve still uses stdio.
  • --public-url is required: the server's https address. The instance's MCP_SERVER_URL is this address followed by /mcp.
  • The secret it shares with the instance comes from WRITEBOOK_MCP_SECRET, never a flag. The server checks it with Writebook when it starts and stops if Writebook refuses it.
  • --instance, --bind, --public-url and --read-only can also be set with WRITEBOOK_MCP_INSTANCE, WRITEBOOK_MCP_BIND, WRITEBOOK_MCP_PUBLIC_URL and WRITEBOOK_MCP_READ_ONLY.
  • The Docker image runs mcp serve --bind 0.0.0.0:80 by default, so it can be deployed with ONCE.
  • GET /up returns 200 without a token, for health checks.

Sign-in and token exchange

  • GET /.well-known/oauth-protected-resource/mcp returns the resource metadata (RFC 9728): the resource is <public url>/mcp, and the Writebook is the authorization server. Clients send that resource when they sign in, so Writebook binds their tokens to this server (RFC 8707).
  • A request to /mcp without a token gets a 401 with WWW-Authenticate: Bearer resource_metadata="…".
  • The server never passes a client's token to Writebook. It exchanges it for a short-lived Writebook token (RFC 8693) and gives that to the tools. Exchanged tokens are reused until shortly before they expire. Tokens Writebook refuses are remembered for a minute and get a 401 with error="invalid_token", so the client renews its token. If the exchange fails for another reason, the request gets a 502.
  • The server logs a warning when the instance uses plain http.

Admin scope

  • The CLI's sign-in asks for read write admin. An administrator who ticks "Manage people and settings" on the approval page can manage people in the TUI. Other people get read write.
  • Writebooks from before the admin scope reject it with invalid_scope. The CLI then asks again for read write.
  • When Writebook refuses a request with insufficient_scope, the error says which access is missing and gives the sign-in command for the profile in use. Through the MCP server, a read-only connection is told to reconnect and choose "Read and change books".
  • In the TUI, an administrator whose sign-in doesn't have the admin scope sees a note with the sign-in command, instead of role and remove actions that would fail.

Docs

doc/mcp-server.md explains how to run the server with ONCE, kamal-proxy, Caddy and Apache. The README links to it.

Tests

  • MCP: the 401s for a missing and a refused token, the metadata, --public-url, the environment variables, tools using the exchanged token, one exchange per token, remembered refusals, the 502, starting without a secret or with a wrong one, and the refusal messages.
  • Sign-in: the fallback for older Writebooks.
  • TUI: the note for an administrator without the admin scope, and the error when the scope is lost during a session.

bin/dev test run and bin/dev code lint pass.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 14:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Required MCP documentation updates are missing, and metadata tests do not assert the advertised scopes or supported bearer-token transport.

2 open issues
What changed in this PR

Adds OAuth discovery to the HTTP MCP server so clients can find the Writebook instance to sign in with. Full connector sign-in still requires Writebook-side OAuth support.

Changes:

  • Adds two public resource-metadata routes while keeping /mcp token-protected.
  • Includes a discovery URL in 401 responses, using the request host and proxy-reported HTTPS scheme.
  • Adds discovery integration tests and extracts a shared server-start helper.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
tests/​mcp.rs Tests discovery routes, the 401 challenge, and HTTPS metadata; shares server setup.
src/​mcp.rs Serves public OAuth metadata and adds discovery challenges to unauthenticated MCP responses.

Review effort: Balanced

Comment thread src/mcp.rs Resolved
Comment thread tests/mcp.rs Resolved
writebook mcp --serve publishes the protected resource metadata (RFC 9728)
at /.well-known/oauth-protected-resource, naming the Writebook it serves as
the authorization server, and a request without a token gets a 401 whose
WWW-Authenticate points there. MCP clients that sign in with OAuth, like
hosted connectors, can find where to sign the person in.
@monorkin
monorkin force-pushed the mcp-oauth-discovery branch from ca4b7e5 to fc9d5da Compare November 30, 2026 14:27
Comment thread tests/mcp.rs Fixed
…s it

Writebook now has an admin scope for managing people and settings. The
CLI's sign-in asks for read write admin, so an administrator's TUI keeps
managing people; anyone else gets read write. A sign-in that lacks the
scope gets an insufficient_scope 403, which the CLI now explains with a
note to sign in again.
… by what the sign-in can do

A Writebook without the admin scope refuses it as invalid_scope; the CLI
then asks again for read write. An insufficient_scope 403 tells a read-only
sign-in it's read-only, rather than to sign in as an administrator.
…sing scope, and add --public-url

A client renews its token when it gets a 401, so the server now checks each
token with Writebook before handling the request, and remembers accepted
ones for a minute. A refusal names what's missing: a read-only connection
is told to reconnect and choose read and change books, and a sign-in
without admin to tick Manage people and settings. --public-url names the
server's address for proxies that don't pass the original Host.
…ronment variables, so it runs under ONCE

ONCE can only pass environment variables to a container. WRITEBOOK_MCP_INSTANCE,
WRITEBOOK_MCP_READ_ONLY and WRITEBOOK_MCP_PUBLIC_URL set the mcp options,
and the image runs mcp --serve 0.0.0.0:80 unless given another command.
…he MCP environment variables through the CLI, check --public-url, drop narrating comments
…ndings

writebook mcp serves stdio as before; writebook mcp serve serves one instance
over HTTP, listening on 127.0.0.1:8080 unless --bind says otherwise. Its
options belong to it, so the "only goes with --serve" checks are gone.

The token check answers 502 when it can't reach Writebook, instead of letting
the request through. Host is parsed before it goes into an address. Scope
refusals name the profile's sign-in command and what to approve. The device
sign-in reports an unreadable answer instead of a missing code. Tests no
longer pick up WRITEBOOK_MCP_* from the environment, and cover the challenges
sent without a Host. The skill says what the image runs and when to tick the
admin box.
…n again, and trim the README to the commands
--read-only is one flag for mcp and mcp serve, so mcp --read-only serve no
longer drops it. A refusal for a WRITEBOOK_TOKEN says to use a token with the
access, since signing in again wouldn't change it, and mcp serve words every
scope refusal for a remote app. The server logs a warning when the instance
uses http. New tests cover the 502, WRITEBOOK_MCP_BIND and each way to set
read-only. The skill says only /mcp needs a token and mentions --public-url.
Comment thread tests/mcp.rs Fixed
Comment thread tests/mcp.rs Fixed
Writebook now issues MCP clients tokens bound to this server (RFC 8707) and
refuses them anywhere else. mcp serve exchanges each one for a short-lived
Writebook token (RFC 8693), authenticating with WRITEBOOK_MCP_SECRET, and
gives that to the tools. Exchanged and refused tokens are remembered, so a
client's token is exchanged once per short-lived token and junk tokens
don't reach Writebook again for a minute.

--public-url is now required, since the bound address has to match exactly,
so the server no longer builds addresses from Host or X-Forwarded-Proto, and
the root resource metadata path is gone. The server checks its secret with
Writebook when it starts.
--public-url must be https unless it's this machine. An expiry Writebook
can't mean is a failed exchange instead of a crash. The startup check says
which host it couldn't reach, and a 401 names every cause Writebook has for
it. An unreadable inventory no longer reads as a WRITEBOOK_TOKEN sign-in.
New tests cover the http warning, exchanging a token again before it runs
out, and the sign-in command a refusal names for a stored profile.
… a 401

Revoking an app's token revokes what the server exchanged it for, but the
server kept reusing its copy, so tools failed inside 200 answers for up to
five minutes and clients never renewed. Found by revoking a token against a
real Writebook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants