Skip to content

Feat(mcp): serve MCP at /mcp, off by default - #74

Merged
adibhanna merged 12 commits into
mainfrom
feat/mcp-http
Nov 5, 2026
Merged

adibhanna merged 12 commits into
mainfrom
feat/mcp-http

Conversation

@adibhanna

Copy link
Copy Markdown
Contributor

Serves the MCP tools at /mcp, off by default, so assistants on other machines can use a vault without installing zn. The tools and scope rules come from github.com/ZenNotes/tui/mcpserver at v0.17.0, the same code as zn mcp.

#56, ZenNotes/zennotes#965

What it does

  • ZENNOTES_MCP_ENABLED turns on the route. While it is off, the route does not exist and the other MCP variables are not read.
  • MCP tokens open /mcp and nothing else. The server's token and browser sessions do not open /mcp.
  • One scope comes from environment variables, or a credentials file binds several tokens to their own scopes.
  • A setting that cannot work stops the server from starting.
  • Requests are stateless, with no MCP session IDs.
  • Tool calls take turns, and open requests, answer sizes and request bodies have limits that follow the container's memory.

The README's new "MCP for AI assistants" section has the setup and the limits.

Also in this branch

  • The search for a free note or file name stops when the filesystem refuses, and gives up after 10,000 tries.
  • Uploads over 8 MiB work in the image, which now ships a /tmp, and leave no temp file.
  • An attachment renamed to a name with a space stays linked.
  • /api/assets/raw sends a sandbox Content-Security-Policy on everything but PDFs, and serves scripts and wasm as plain text.
  • In a container with a memory limit, the server sets a soft limit of 70% of it at startup. A GOMEMLIMIT you set wins.
  • A limit on how long request headers may take.

Compatibility

  • The server now needs Go 1.26.
  • release.json keeps version 2.82.1, and only vendorHash changes. The README describes /mcp as "since server 2.83.0", which the release sets.

Tests

  • Go 1.26.10: go vet ./... and go test -p 1 -count=1 ./... pass.
  • Shared contract from the v0.17.0 tag through this server: go test ./internal/mcp ./internal/remote passes with both contract variables set. The /mcp run alone is 85 tests and subtests, none skipped.
  • The real image, built from this branch for linux/arm64: it starts healthy, /mcp is absent by default and answers once on, tokens stay on their own side, scoped calls behave, and it comes back after a restart. Tokens and note text do not reach the log.
  • nix-build with CI's pinned image passes on aarch64. CI's nix job covers x86_64.

New CI: mcp-contract runs the shared contract through /mcp from the tui tag go.mod pins, and docker-upload uploads 12 MiB to the running image.

Not tested: the hop through OpenAI's tunnel-client, which needs an OpenAI tunnel.

Looking for an unused note, attachment, folder or template name now stops on any answer but
"exists" or "does not exist", and gives up after 10,000 tries, instead of holding the vault's
lock for as long as it runs. Note titles and uploaded file names are cut to 200 bytes without
splitting a character, and control characters are removed from them.
…file

An upload that size spills to a temp file. The scratch image had no /tmp, so it failed there,
and now ships one any user can write to. The temp file also outlived the request, because
net/http only cleans up the form on the request it started with, and the router hands the
handler a copy. A new CI job uploads 12 MiB to the running image, since only that shows this.
The Go base image digest is refreshed.
A Markdown link to `a b.png` or `a(1).png` is not a link unless it is in angle brackets, and a
rename or a move could bring either in. The rewrite now adds them, as zn does. A note holding
only a bracketed link also did not count as holding an attachment, so a rename skipped it and
the link went stale, which already affected links the server writes itself on upload.
ZENNOTES_MCP_ENABLED turns on a route with the tools and scope rules of `zn mcp`,
which reach the vault through the server's own API in-process. MCP tokens open /mcp
and nothing else, and the server's token and browser sessions do not open /mcp. One
scope comes from environment variables, or a credentials file binds several tokens to
their own scopes. A setting that cannot work stops the server from starting. A new CI
job runs the shared scope contract through /mcp.

The server now needs Go 1.26, and imports github.com/ZenNotes/tui/mcpserver. Not
releasable yet: go.mod has a local replace, and requires a tui tag that does not exist.

#56, ZenNotes/zennotes#965
The server now requires ZENNOTES_VAULT_PATH with MCP on, which stops a vault switch, so the
check for one is gone. A blank, empty or null allow list is refused rather than sharing the
whole vault. In-process calls reach only the routes the tools use, stop if their time is up,
and hold at most 64 MiB or twice the note limit of a response, and `read_note` checks a note's
size before reading it. Search takes a `limit`, which a scoped client needs so that hidden
matches cannot crowd out visible ones. CI and releases use Go 1.26.x rather than exactly the
version in go.mod.
…he container

`/api/assets/raw` sends a `sandbox` Content-Security-Policy on everything but PDFs, and serves
scripts and wasm as plain text, since a vault's files can come from someone with less access
than whoever views them. Images still draw, SVG included.

In a container with a memory limit, the server sets a soft limit of 70% of it at startup. A
GOMEMLIMIT someone set wins.

Also: a limit on how long request headers may take, an end to the origins remembered for the
CORS log, and the preflight allows Mcp-Protocol-Version.
A blank, empty or null deny list is refused, as an allow list already was. How many tool calls
run at once follows the memory limit. go.mod names a toolchain, so that a build gets the latest
fixes. The README covers the new limits, read-only containers and single-note deny entries.
A tool's answer stays in memory until its client reads it, so a memory limit now also lowers how
many requests may be open (8 up to 800 MiB, 16 up to 2 GiB, otherwise 32) and the largest answer
a tool may get from the API, to no less than 16 MiB. The README says what this costs: a small
server under a burst answers some requests with a 503.
The largest answer to a tool call follows the memory limit, as the largest response from the API
does, and is counted after JSON encoding. The README says what this means for a note made almost
entirely of quotes, and that a deny entry naming a single note shuts a credential out once that
note moves.
Drops the local replace, so CI and the Nix and Docker builds can build this
branch. The vendor hash follows go.mod; the version stays 2.82.1 until the
release sets 2.83.0.
Windows reports mode 666 for any file a user can write, and says a path under a file does not
exist. The second means the name reads as free and the write refuses, so the search still ends
on its first try.
@adibhanna
adibhanna merged commit 09b46e1 into main Nov 5, 2026
16 checks passed
@adibhanna
adibhanna deleted the feat/mcp-http branch November 5, 2026 14:25
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.

1 participant