Skip to content

Serve books to MCP clients - #12

Merged
monorkin merged 9 commits into
mainfrom
mcp
Nov 25, 2026
Merged

monorkin merged 9 commits into
mainfrom
mcp

Conversation

@monorkin

@monorkin monorkin commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Adds writebook mcp, an MCP server that lets agents read and edit books directly on all configured instances. To give Claude access to it, run:

claude mcp add writebook -- writebook mcp

It can also be used as a server to give anyone's agent access to a Writebook instance using their own logins:

writebook mcp --instance https://books.example.com --serve 127.0.0.1:8080

On stdio

  • It serves every instance in writebook instance list, using your sign-ins.
  • list_instances lists the instances, and every other tool takes an instance argument (an alias or URL). Without it, a tool uses the default instance. Instances that aren't in the list are refused.
  • The tools that change books are listed only if at least one sign-in can change books. A read-only sign-in is refused before anything is sent.
  • --read-only serves only the tools that read.

With --serve

  • It serves one instance over HTTP at /mcp.
  • Every request must include a Writebook token as a Bearer header, and a request without one gets a 401.
  • The token is only sent to that instance, and the server never uses a stored login.
  • A rejected token fails the call with "did not accept your Writebook token".
  • It accepts any Host header. Host checks stop a web page from using a local server's stored login, and this server doesn't use one.
  • It serves plain HTTP, so it needs https in front unless it only listens on the machine itself.

Docker image

.github/workflows/publish-image.yml, based on Fizzy's, publishes ghcr.io/basecamp/writebook-cli for amd64 and arm64. The image is signed with cosign and has build provenance attestations.

  • Every push to main publishes :main and :sha-<commit>.
  • A release tag v1.2.3 publishes :1.2.3, :1.2, :1 and :latest. Until 1.0 there's no major-only tag such as :0.
  • A release candidate publishes only its own version, so it never becomes latest.
  • A release image is only built after the same checks as release.yml pass: the tag is on main, it matches Cargo.toml, and CI passes.
docker run -p 8080:8080 ghcr.io/basecamp/writebook-cli mcp --instance https://books.example.com --serve 0.0.0.0:8080

Logs

Both modes write structured logs to stderr, one JSON object per line:

  • at startup: the instance, the transport, the listening address, and whether it's read-only
  • for every HTTP request: the method, the status and how long it took
  • for every tool call: the tool, ok or error with a fixed reason (token_rejected, writebook_error with Writebook's status, or failed), and how long it took

They never include tokens, client IP addresses, tool arguments, book contents or error messages, since an error message can quote a title, a URL or a search. rmcp's own logs are filtered out because they can quote requests.

Tools

  • To read: list_books, book_contents, read_book, read_page, search, and list_instances on stdio
  • To change books: create_page, update_page, create_section

update_page needs the fingerprint that read_page returned. If the page changed since then, nothing is written and the tool says to read it again.

There are no tools yet for deleting or moving leaves, or for pictures.

Notes

  • It's built on rmcp, the official Rust MCP SDK, with tokio and axum. The release binary grows from 11.3 MB to 17.1 MB.
  • read.rs now exposes the functions that return its data, so the tools share them with book contents, search and read.
  • api::Client::with_token makes a client for a token someone handed over, which is never renewed from stored sign-ins.

Testing

  • tests/mcp.rs drives the server over stdio and HTTP against fake Writebooks, including picking an instance and keeping WRITEBOOK_TOKEN to its own host.
  • A live run against a local Writebook covered listing, creating, searching, a stale update, a good update, reading back, and a rejected token.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 08:39
@monorkin
monorkin changed the base branch from security-fixes to main November 25, 2026 08:39

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

Unresolved issues allow write tools to report success with unusable IDs or fingerprints, MCP reads to fall back to anonymous access after sign-in is lost, and search results to exceed the advertised 50-match limit.

3 open issues
What changed in this PR

Extends the Writebook CLI so MCP clients can read and edit books on a configured instance, building on the security changes in #11.

Changes:

  • Adds stdio and HTTP MCP transports with stored sign-ins or per-request Bearer tokens.
  • Exposes reading, searching, creation, and fingerprint-guarded page updates through shared CLI logic.
  • Adds integration tests, setup documentation, and MCP runtime dependencies.

[!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 transports, authentication, read-only tools, and guarded updates.
src/​read.rs Exposes shared read/search functions and typed token-rejection errors.
src/​mcp/​tools.rs Defines MCP tool schemas and read/write operations.
src/​mcp.rs Implements MCP transports, credential handling, and request dispatch.
src/​main.rs Adds the mcp command and its options.
src/​creds.rs Exposes token and transport helpers; adds write-access detection.
src/​api.rs Adds nonrenewable supplied-token clients and typed rejection errors.
README.md Documents local and HTTP MCP setup.
Cargo.toml Adds rmcp, tokio, and axum dependencies.
Cargo.lock Records the new dependencies and their transitive packages.

Review effort: Balanced

Comment thread src/mcp.rs Resolved
Comment thread src/mcp/tools.rs Resolved
Comment thread src/mcp/tools.rs Resolved
Comment thread tests/mcp.rs Fixed

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

Unresolved MCP credential and capability-handling defects can break startup, misadvertise write access, and return anonymous results, while error logging and image-publication rules still lack required safeguards.

4 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Suppress unsanitized stderr diagnostics in MCP mode

src/​mcp.rs:161

When refreshing an expired stdio sign-in fails, this lookup prints the full server-provided error description via src/creds.rs:200, bypassing the sanitized logger. This breaks JSON-only logging and can expose sensitive error contents. Suppress direct stderr diagnostics in MCP mode and emit only fixed structured reasons; the legacy warning at src/read.rs:609 also needs this treatment.

Medium severity Reject unauthenticated MCP HTML fallbacks

src/​mcp/​tools.rs:188

When the JSON endpoint is unavailable or redirects, this HTML fallback can return published-only books even after detecting that the supplied token was not accepted. Add an authenticated-only path for MCP that reports rejection instead of succeeding anonymously, while preserving the CLI fallback. Apply it to search at src/mcp/tools.rs:253 as well, including book-scoped searches where check_token currently accepts a missing manifest API.

3 resolved since last review

Review effort: Balanced

Comment thread .github/workflows/publish-image.yml Resolved
Comment thread .github/workflows/publish-image.yml Resolved
Comment thread src/creds.rs Resolved
Comment thread src/creds.rs Resolved
…nly real full-access tokens as able to change books
@monorkin
monorkin merged commit 4361ea6 into main Nov 25, 2026
12 checks passed
@monorkin
monorkin deleted the mcp branch November 25, 2026 09:28
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