resources.go 3.3 KB

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