example

package
v0.0.0-...-2756944 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package example is the reference implementation for github.com/pigfox/mcp-metered. It wires the meter to an MCP server with four fake tools, an in-memory credit store and an in-memory token table.

It connects to nothing. There is no database, no upstream API and no credential outside this file; the tools compute their answers locally. It exists to show the wiring and to give the tests something end-to-end to run against.

Index

Constants

View Source
const (
	// ToolEcho returns its input. It is the cheap, ordinary case.
	ToolEcho = "echo"

	// ToolSlowEcho returns its input after a delay, to show that a reservation
	// is held for the whole of a call rather than taken at its end.
	ToolSlowEcho = "slow_echo"

	// ToolAlwaysFails always reports failure. It demonstrates the billing rule
	// that matters most against an agent: a tool that ran and reported a domain
	// error is billed, so retrying it is not free.
	ToolAlwaysFails = "always_fails"

	// ToolExpensive is priced above the per-tool cap on purpose, so that a
	// misconfigured price is visibly refused before any credit moves.
	ToolExpensive = "expensive"
)

The example's fake tools. None of them reaches anything outside the process.

View Source
const (
	PriceEcho        metered.Cost = 1
	PriceSlowEcho    metered.Cost = 5
	PriceAlwaysFails metered.Cost = 2
	PriceExpensive   metered.Cost = 500

	// ToolCap is the example's per-tool cap. PriceExpensive is deliberately
	// above it.
	ToolCap metered.Cost = 50

	// SessionCeiling is the example's cumulative per-session ceiling.
	SessionCeiling metered.Cost = 100

	// RateLimit and RateBurst configure the example's token bucket.
	RateLimit float64 = 5
	RateBurst float64 = 10

	// DemoToken is the bearer token the example accepts, and DemoPrincipal the
	// account it maps to. Both are fixtures printed by the server at startup;
	// neither is a credential to anything.
	DemoToken     = "demo-token"
	DemoPrincipal = "demo-account"

	// StartingCredit is how much the demo account is funded with.
	StartingCredit metered.Cost = 60

	// DefaultAddr is the address the example server listens on.
	DefaultAddr = "localhost:8080"

	// MCPPath is the HTTP path the MCP endpoint is served at.
	MCPPath = "/mcp"

	// ServerName and ServerVersion identify the example server to clients.
	ServerName    = "mcp-metered-example"
	ServerVersion = "v0.1.0"

	// SlowEchoDelay is how long ToolSlowEcho takes.
	SlowEchoDelay = 250 * time.Millisecond

	// ShutdownGrace is how long [Run] gives in-flight requests to finish after
	// its context is cancelled.
	ShutdownGrace = 5 * time.Second

	// ReadHeaderTimeout bounds how long a client may take to send its headers.
	ReadHeaderTimeout = 10 * time.Second
)

The example's prices, in credits, and its limits.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, addr string, opts Options) error

Run builds the example and serves it on addr until ctx is cancelled. An empty addr means DefaultAddr.

Types

type EchoArgs

type EchoArgs struct {
	Text string `json:"text" jsonschema:"the text to echo back"`
}

EchoArgs is the input to ToolEcho and ToolSlowEcho.

type EchoResult

type EchoResult struct {
	Text string `json:"text" jsonschema:"the echoed text"`
}

EchoResult is the output of ToolEcho and ToolSlowEcho.

type NoArgs

type NoArgs struct{}

NoArgs is the input to tools that take none.

type Options

type Options struct {
	// Logger receives audit records. Nil means [slog.Default].
	Logger *slog.Logger

	// Now supplies the clock passed to the meter and the limiter. Nil means
	// [time.Now].
	Now func() time.Time

	// Sleep is how [ToolSlowEcho] waits. Nil means a real timer. Tests pass a
	// no-op so that the suite does not spend a quarter of a second proving that
	// a delay happened.
	Sleep func(time.Duration)

	// ToolCap overrides [ToolCap]. Zero means the package value. Raising it
	// past [PriceExpensive] is how [ToolExpensive] becomes callable.
	ToolCap metered.Cost

	// SessionCeiling overrides [SessionCeiling]. Zero means the package value.
	SessionCeiling metered.Cost
}

Options configures NewServer. The zero value is usable.

type Wiring

type Wiring struct {
	// Server is the MCP server with the tools registered and the meter
	// installed as receiving middleware.
	Server *mcp.Server

	// Meter is the configured meter.
	Meter *metered.Meter

	// Store is the in-memory credit store, funded with [StartingCredit].
	Store *memstore.MemStore

	// Handler serves the MCP endpoint over Streamable HTTP.
	Handler http.Handler
}

Wiring is everything NewServer built, returned so that a caller — the command, or a test — can inspect the parts rather than only the handler.

func NewServer

func NewServer(opts Options) (*Wiring, error)

NewServer builds the example: four fake tools, an in-memory store funded with StartingCredit, a bearer table holding DemoToken, a token-bucket limiter, and an auditor that logs one line per call.

Jump to

Keyboard shortcuts

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