resource

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package resource registers static and RFC 6570 URI-template MCP resources.

Read handlers return Text, Blob, JSON, or Raw content. The package supplies the URI and default MIME type except for Raw. Template handlers also receive the concrete URI and extracted Vars.

NewTemplate panics on an invalid template, and Resource.Add panics on an invalid URI. ErrNotFound and ErrTemplateMismatch become the SDK resource not-found error. ErrNoContent reports a nil Content or empty Raw result.

The package does not configure resource subscriptions. Configure subscription handlers when constructing the underlying mcp.Server.

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 fixed MIME type for JSON content.
	MIMEJSON = "application/json"
)

Default MIME types for Content values.

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 nil Content or empty Raw result.
	ErrNoContent = errors.New("resource produced no content")
)

Resource errors are package-specific matchable sentinels.

Functions

This section is empty.

Types

type Blob

type Blob struct {
	Data []byte
	MIME string
}

Blob contains binary data and an optional MIME override.

func NewBlob

func NewBlob(b []byte) Blob

NewBlob returns Blob that uses the resource MIME or MIMEBlob.

type Content

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

Content produces one resource read result.

type JSON

type JSON[T any] struct {
	Value T
}

JSON marshals Value as text with MIMEJSON.

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 serves non-empty SDK content blocks without modification.

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 value builder for a static MCP resource.

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 and panics if its URI is invalid.

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 value builder for an RFC 6570 resource template.

func NewTemplate

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

NewTemplate creates a builder and panics if uriTemplate is invalid.

func (Template) Add

func (t Template) Add()

Add registers the template on the server.

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 concrete URI and its template variables.

type Text

type Text struct {
	Text string
	MIME string
}

Text contains UTF-8 text and an optional MIME override.

func NewText

func NewText(s string) Text

NewText returns Text that uses the resource MIME or MIMEText.

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 a string value or "" when the variable is absent or not a string.

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 a string value and reports whether the variable is present.

Jump to

Keyboard shortcuts

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