mcpserver

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcpserver serves zn's MCP tools over HTTP, for a ZenNotes server to mount in its own process. It is the only public package in this module, and exists so that the tools and scope rules behind `zn mcp` are one implementation wherever they run. See docs/ideas/mcp-http-transport.md.

A caller presents a token, the token picks a scope, and nothing the caller sends can change it. Each request stands alone (stateless), so there is no session another credential could borrow, and a restart loses nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashToken

func HashToken(token string) [sha256.Size]byte

HashToken is the form a Credential holds a token in.

func NewHandler

func NewHandler(o Options) (http.Handler, error)

NewHandler builds the handler. A bad credential is an error, never a wider scope.

func ValidateScope

func ValidateScope(c Credential) error

ValidateScope reports what is wrong with a credential's scope, so a server can refuse to start. Access has to be stated, and the rules are the ones `zn mcp` applies to its flags.

Types

type Credential

type Credential struct {
	// Name identifies the credential in the server's log. It is not a secret.
	Name        string
	TokenSHA256 [sha256.Size]byte
	// Access is read, comment or write. It is required: full access has to be
	// written down.
	Access string
	// Allow and Deny work as --allow-path and --deny-path do on `zn mcp`.
	Allow, Deny []string
}

Credential is one token and the scope it grants. The token itself never reaches this package: a credentials file holds only its hash, so everything does.

type Options

type Options struct {
	// Transport carries the tools' API calls to the server, without the network,
	// and adds whatever credential the server's API wants. Required.
	Transport http.RoundTripper
	// BaseURL is how the transport expects those calls to be addressed, including
	// any base path. Clients never see it.
	BaseURL string
	// Credentials are the tokens this handler accepts. At least one is required.
	Credentials []Credential
	// AllowedOrigins are the Origin header values to accept. A request carrying any
	// other Origin is refused, and one carrying none is unaffected. This adds no
	// CORS headers.
	AllowedOrigins []string
	// MaxNoteBytes is the server's note limit, from which the request limit follows.
	MaxNoteBytes int64
	// Version is reported to clients.
	Version string
	// Logf, when set, gets one line per tool call: credential name, tool, outcome,
	// duration. Never a token, an argument or a result. It also gets how many requests
	// were rejected, and what an operator must fix in the scope.
	Logf func(format string, args ...any)
	// ReadTimeout bounds reading a request body, and WriteTimeout everything after it.
	// They default to 60 seconds and a little over the longest tool call, and a credential that cannot write
	// gets at most 15 seconds to send its body. A server with websockets cannot set
	// either on its listener, so they apply here, per request.
	ReadTimeout, WriteTimeout time.Duration
	// MaxInFlight is how many requests are open at once before the rest get 503. It
	// defaults to 32, shared equally between credentials, so one cannot starve others. An
	// answer stays in memory until its client reads it, so a small server should set this
	// lower.
	MaxInFlight int
	// SlotWait is how long a request waits for one of those. It defaults to 2 seconds.
	SlotWait time.Duration
	// MaxToolCalls is how many tool calls run at once, across credentials, while the
	// rest wait. It defaults to 4. A call can hold a whole listing and a search's matches
	// in memory, and this is what bounds that, so a small server should set it lower.
	MaxToolCalls int
	// MaxResponseBytes is the largest answer to a tool call, as it goes on the wire. It
	// defaults to 64 MiB, and a call over it gets a tool error, since an answer waits in
	// memory until its client reads it.
	MaxResponseBytes int64
	// MaxBodyBytes is how many bytes of request bodies are held at once, shared equally
	// between credentials, before the rest get 503. It defaults to 64 MiB. A credential
	// always has room for its largest allowed request.
	MaxBodyBytes int64
}

Options configure a handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL