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 = &Annotations{}
47		}
48		r.Annotations.Audience = audience
49		r.Annotations.Priority = priority
50	}
51}
52
53// ResourceTemplateOption is a function that configures a ResourceTemplate.
54// It provides a flexible way to set various properties of a ResourceTemplate using the functional options pattern.
55type ResourceTemplateOption func(*ResourceTemplate)
56
57// NewResourceTemplate creates a new ResourceTemplate with the given URI template, name and options.
58// The template will be configured based on the provided options.
59// Options are applied in order, allowing for flexible template configuration.
60func NewResourceTemplate(uriTemplate string, name string, opts ...ResourceTemplateOption) ResourceTemplate {
61	template := ResourceTemplate{
62		URITemplate: &URITemplate{Template: uritemplate.MustNew(uriTemplate)},
63		Name:        name,
64	}
65
66	for _, opt := range opts {
67		opt(&template)
68	}
69
70	return template
71}
72
73// WithTemplateDescription adds a description to the ResourceTemplate.
74// The description should provide a clear, human-readable explanation of what resources this template represents.
75func WithTemplateDescription(description string) ResourceTemplateOption {
76	return func(t *ResourceTemplate) {
77		t.Description = description
78	}
79}
80
81// WithTemplateMIMEType sets the MIME type for the ResourceTemplate.
82// This should only be set if all resources matching this template will have the same type.
83func WithTemplateMIMEType(mimeType string) ResourceTemplateOption {
84	return func(t *ResourceTemplate) {
85		t.MIMEType = mimeType
86	}
87}
88
89// WithTemplateAnnotations adds annotations to the ResourceTemplate.
90// Annotations can provide additional metadata about the template's intended use.
91func WithTemplateAnnotations(audience []Role, priority float64) ResourceTemplateOption {
92	return func(t *ResourceTemplate) {
93		if t.Annotations == nil {
94			t.Annotations = &Annotations{}
95		}
96		t.Annotations.Audience = audience
97		t.Annotations.Priority = priority
98	}
99}