resource

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package resource provides a type-safe fluent builder for serving MCP resources and URI-template (dynamic) resources, mirroring toolkit's style.

New(server, uri, name, description, read) builds a static resource; read is a typed func(ctx) (Content, error) — no raw SDK request/result. NewTemplate builds a templated resource whose read func also receives the concrete URI and the variables extracted from it (the SDK extracts none). Chain optional config, then register with Add:

  • WithMIMEType / WithTitle / WithDescription / WithAnnotations — both.
  • WithSize — static resources only.

Add panics if the URI is malformed (fails url.Parse), and NewTemplate panics on an invalid RFC 6570 template — surfacing the SDK's panics, consistent with toolkit.InputSchema.

Content envelopes (content.go) spare handlers from building []*mcp.ResourceContents by hand, filling the URI and a default MIME type: Text (text/plain), Blob (application/octet-stream), JSON[T] (application/json), and Raw as a verbatim escape hatch, with NewText / NewBlob / NewJSON constructors.

Template variables are exposed as a Vars map (not a typed struct): Get / Lookup / Has / List / Int accessors over the matched RFC 6570 values (Lookup is the comma-ok form that distinguishes absent from present-but-empty). The SDK gives no decode for templates, so a map keeps the surface minimal; a typed variant can be added later.

Sentinels (errors.go) follow the repo convention. ErrNotFound and ErrTemplateMismatch are return-side sentinels: returning either from a read func yields mcp.ResourceNotFoundError (CodeResourceNotFound) on the wire. Cross-transport errors.Is is not promised. ErrInvalidVars wraps a failed Vars.Int conversion. ErrNoContent is returned when a read func produces no content (a nil Content or an empty Raw), surfaced as a real error rather than a silent empty read.

Subscriptions (resources/updated) are not yet supported. list-changed works for free (the SDK fires it on AddResource/RemoveResources), but subscriptions need SubscribeHandler/UnsubscribeHandler set at mcp.NewServer construction, which the caller — not this package — owns.

Index

Constants

View Source
const (
	// MIMEText is the fallback MIME type for Text content.
	MIMEText = "text/plain"
	// MIMEBlob is the fallback MIME type for Blob content.
	MIMEBlob = "application/octet-stream"
	// MIMEJSON is the MIME type JSON content always serves.
	MIMEJSON = "application/json"
)

Default MIME types for the built-in Content shapes, exported so callers can pass them to WithMIMEType instead of repeating the literals.

Variables

View Source
var (
	// ErrNotFound signals a resource (or templated instance) does not exist.
	ErrNotFound = errors.New("resource not found")
	// ErrTemplateMismatch signals a read URI did not match the URI template.
	ErrTemplateMismatch = errors.New("uri does not match template")
	// ErrInvalidVars signals a template variable could not be converted.
	ErrInvalidVars = errors.New("invalid template variable")
	// ErrNoContent signals a read func produced no content — a nil Content or
	// an empty Raw. It is a real internal error (not not-found), so it passes
	// through toWireErr unchanged rather than masquerading as a missing
	// resource.
	ErrNoContent = errors.New("resource produced no content")
)

Package sentinels, following the repo convention (no umbrella sentinel).

ErrNotFound and ErrTemplateMismatch are return-side sentinels: a read func returning either yields mcp.ResourceNotFoundError (CodeResourceNotFound) on the wire. Cross-transport errors.Is is not promised — the SDK serializes errors to a JSON-RPC code — same caveat as the elicit sentinels.

Functions

This section is empty.

Types

type Blob

type Blob struct {
	Data []byte
	MIME string
}

Blob is binary content, base64-encoded on the wire by the SDK. MIME overrides the resource's MIMEType; absent both, MIMEBlob.

func NewBlob

func NewBlob(b []byte) Blob

NewBlob wraps b as Blob content with a default MIME. To override the MIME per content, use a Blob struct literal (Blob{Data: b, MIME: …}).

type Content

type Content interface {
	// contains filtered or unexported methods
}

Content produces the contents of a single resource read. The uri and a fallback MIME type are supplied by the package at read time, so handlers need not repeat them.

type JSON

type JSON[T any] struct {
	Value T
}

JSON marshals Value and serves it as text with MIME MIMEJSON unconditionally — the resource's declared MIMEType (and WithMIMEType) do not apply to JSON content.

func NewJSON

func NewJSON[T any](v T) JSON[T]

NewJSON wraps v as JSON content.

type Raw

type Raw struct {
	Contents []*mcp.ResourceContents
}

Raw is an escape hatch: it serves the given contents verbatim, for handlers that need multiple sub-resources or custom per-content metadata. Unlike the other shapes, Raw does not stamp the URI or fill a fallback MIME — the caller owns each block's fields. Contents must be non-empty; an empty Raw yields ErrNoContent rather than a silent empty read.

type ReadFunc

type ReadFunc func(ctx context.Context) (Content, error)

ReadFunc reads a static resource's content.

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource is a fluent registration builder, distinct from the SDK's mcp.Resource. It is a value type — builder methods return a copy.

func New

func New(
	server *mcp.Server,
	uri, name, description string,
	read ReadFunc,
) Resource

New starts a static-resource registration.

func (Resource) Add

func (r Resource) Add()

Add registers the resource on the server. It panics if the URI is malformed (fails url.Parse), surfacing the SDK's AddResource panic unchanged.

func (Resource) WithAnnotations

func (r Resource) WithAnnotations(a *mcp.Annotations) Resource

WithAnnotations attaches client annotations to the resource.

func (Resource) WithDescription

func (r Resource) WithDescription(desc string) Resource

WithDescription overrides the description passed to New.

func (Resource) WithMIMEType

func (r Resource) WithMIMEType(mime string) Resource

WithMIMEType sets the resource's declared MIME type.

func (Resource) WithSize

func (r Resource) WithSize(n int64) Resource

WithSize sets the raw content size in bytes, advertised to clients.

func (Resource) WithTitle

func (r Resource) WithTitle(title string) Resource

WithTitle sets the resource's human-readable title.

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is a fluent registration builder for a URI-template resource, distinct from the SDK's mcp.ResourceTemplate. It is a value type — builder methods return a copy.

func NewTemplate

func NewTemplate(
	server *mcp.Server,
	uriTemplate, name, description string,
	read TemplateReadFunc,
) Template

NewTemplate starts a templated-resource registration. It parses uriTemplate immediately and panics if the template is invalid (RFC 6570), mirroring the SDK's AddResourceTemplate and toolkit.InputSchema panic conventions.

func (Template) Add

func (t Template) Add()

Add registers the template on the server. The template was already parsed and validated by NewTemplate, so any malformed-template panic fires there, not here.

func (Template) WithAnnotations

func (t Template) WithAnnotations(a *mcp.Annotations) Template

WithAnnotations attaches client annotations to the template.

func (Template) WithDescription

func (t Template) WithDescription(desc string) Template

WithDescription overrides the description passed to NewTemplate.

func (Template) WithMIMEType

func (t Template) WithMIMEType(mime string) Template

WithMIMEType sets the MIME type shared by resources matching the template.

func (Template) WithTitle

func (t Template) WithTitle(title string) Template

WithTitle sets the template's human-readable title.

type TemplateReadFunc

type TemplateReadFunc func(
	ctx context.Context,
	uri string,
	vars Vars,
) (Content, error)

TemplateReadFunc reads a templated resource for a concrete URI and the variables extracted from it.

type Text

type Text struct {
	Text string
	MIME string
}

Text is UTF-8 textual content. MIME overrides the resource's declared MIMEType; absent both, it defaults to MIMEText.

func NewText

func NewText(s string) Text

NewText wraps s as Text content with a default MIME. To override the MIME per content, use a Text struct literal (Text{Text: s, MIME: …}).

type Vars

type Vars struct {
	// contains filtered or unexported fields
}

Vars holds the variables extracted from a matched URI template.

func (Vars) Get

func (v Vars) Get(name string) string

Get returns the string value of name, or "" if absent or not a string var. Get alone cannot distinguish an absent variable from a present-but-empty one — use Lookup or Has for that.

func (Vars) Has

func (v Vars) Has(name string) bool

Has reports whether name was present in the matched URI.

func (Vars) Int

func (v Vars) Int(name string) (int, error)

Int parses name as an int, wrapping ErrInvalidVars on failure.

func (Vars) List

func (v Vars) List(name string) []string

List returns a multi-valued (exploded) variable, e.g. {/path*}.

func (Vars) Lookup

func (v Vars) Lookup(name string) (string, bool)

Lookup returns the string value of name and whether it was present, in the comma-ok idiom (cf. os.LookupEnv), so callers can tell an absent variable from a present-but-empty one.

Jump to

Keyboard shortcuts

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