resources.go

  1package mcp
  2
  3import "github.com/yosida95/uritemplate/v3"
  4
  5// ResourceOption is a function that configures a Resource.
  6// It provides a flexible way to set various properties of a Resource using the functional options pattern.
  7type ResourceOption func(*Resource)
  8
  9// NewResource creates a new Resource with the given URI, name and options.
 10// The resource will be configured based on the provided options.
 11// Options are applied in order, allowing for flexible resource configuration.
 12func NewResource(uri string, name string, opts ...ResourceOption) Resource {
 13	resource := Resource{
 14		URI:  uri,
 15		Name: name,
 16	}
 17
 18	for _, opt := range opts {
 19		opt(&resource)
 20	}
 21
 22	return resource
 23}
 24
 25// WithResourceDescription adds a description to the Resource.
 26// The description should provide a clear, human-readable explanation of what the resource represents.
 27func WithResourceDescription(description string) ResourceOption {
 28	return func(r *Resource) {
 29		r.Description = description
 30	}
 31}
 32
 33// WithMIMEType sets the MIME type for the Resource.
 34// This should indicate the format of the resource's contents.
 35func WithMIMEType(mimeType string) ResourceOption {
 36	return func(r *Resource) {
 37		r.MIMEType = mimeType
 38	}
 39}
 40
 41// WithAnnotations adds annotations to the Resource.
 42// Annotations can provide additional metadata about the resource's intended use.
 43func WithAnnotations(audience []Role, priority float64) ResourceOption {
 44	return func(r *Resource) {
 45		if r.Annotations == nil {
 46			r.Annotations = &struct {
 47				Audience []Role  `json:"audience,omitempty"`
 48				Priority float64 `json:"priority,omitempty"`
 49			}{}
 50		}
 51		r.Annotations.Audience = audience
 52		r.Annotations.Priority = priority
 53	}
 54}
 55
 56// ResourceTemplateOption is a function that configures a ResourceTemplate.
 57// It provides a flexible way to set various properties of a ResourceTemplate using the functional options pattern.
 58type ResourceTemplateOption func(*ResourceTemplate)
 59
 60// NewResourceTemplate creates a new ResourceTemplate with the given URI template, name and options.
 61// The template will be configured based on the provided options.
 62// Options are applied in order, allowing for flexible template configuration.
 63func NewResourceTemplate(uriTemplate string, name string, opts ...ResourceTemplateOption) ResourceTemplate {
 64	template := ResourceTemplate{
 65		URITemplate: &URITemplate{Template: uritemplate.MustNew(uriTemplate)},
 66		Name:        name,
 67	}
 68
 69	for _, opt := range opts {
 70		opt(&template)
 71	}
 72
 73	return template
 74}
 75
 76// WithTemplateDescription adds a description to the ResourceTemplate.
 77// The description should provide a clear, human-readable explanation of what resources this template represents.
 78func WithTemplateDescription(description string) ResourceTemplateOption {
 79	return func(t *ResourceTemplate) {
 80		t.Description = description
 81	}
 82}
 83
 84// WithTemplateMIMEType sets the MIME type for the ResourceTemplate.
 85// This should only be set if all resources matching this template will have the same type.
 86func WithTemplateMIMEType(mimeType string) ResourceTemplateOption {
 87	return func(t *ResourceTemplate) {
 88		t.MIMEType = mimeType
 89	}
 90}
 91
 92// WithTemplateAnnotations adds annotations to the ResourceTemplate.
 93// Annotations can provide additional metadata about the template's intended use.
 94func WithTemplateAnnotations(audience []Role, priority float64) ResourceTemplateOption {
 95	return func(t *ResourceTemplate) {
 96		if t.Annotations == nil {
 97			t.Annotations = &struct {
 98				Audience []Role  `json:"audience,omitempty"`
 99				Priority float64 `json:"priority,omitempty"`
100			}{}
101		}
102		t.Annotations.Audience = audience
103		t.Annotations.Priority = priority
104	}
105}