prompts.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package mcp
  2. import "net/http"
  3. /* Prompts */
  4. // ListPromptsRequest is sent from the client to request a list of prompts and
  5. // prompt templates the server has.
  6. type ListPromptsRequest struct {
  7. PaginatedRequest
  8. Header http.Header `json:"-"`
  9. }
  10. // ListPromptsResult is the server's response to a prompts/list request from
  11. // the client.
  12. type ListPromptsResult struct {
  13. PaginatedResult
  14. Prompts []Prompt `json:"prompts"`
  15. }
  16. // GetPromptRequest is used by the client to get a prompt provided by the
  17. // server.
  18. type GetPromptRequest struct {
  19. Request
  20. Params GetPromptParams `json:"params"`
  21. Header http.Header `json:"-"`
  22. }
  23. type GetPromptParams struct {
  24. // The name of the prompt or prompt template.
  25. Name string `json:"name"`
  26. // Arguments to use for templating the prompt.
  27. Arguments map[string]string `json:"arguments,omitempty"`
  28. }
  29. // GetPromptResult is the server's response to a prompts/get request from the
  30. // client.
  31. type GetPromptResult struct {
  32. Result
  33. // An optional description for the prompt.
  34. Description string `json:"description,omitempty"`
  35. Messages []PromptMessage `json:"messages"`
  36. }
  37. // Prompt represents a prompt or prompt template that the server offers.
  38. // If Arguments is non-nil and non-empty, this indicates the prompt is a template
  39. // that requires argument values to be provided when calling prompts/get.
  40. // If Arguments is nil or empty, this is a static prompt that takes no arguments.
  41. type Prompt struct {
  42. // Meta is a metadata object that is reserved by MCP for storing additional information.
  43. Meta *Meta `json:"_meta,omitempty"`
  44. // The name of the prompt or prompt template.
  45. Name string `json:"name"`
  46. // An optional description of what this prompt provides
  47. Description string `json:"description,omitempty"`
  48. // A list of arguments to use for templating the prompt.
  49. // The presence of arguments indicates this is a template prompt.
  50. Arguments []PromptArgument `json:"arguments,omitempty"`
  51. }
  52. // GetName returns the name of the prompt.
  53. func (p Prompt) GetName() string {
  54. return p.Name
  55. }
  56. // PromptArgument describes an argument that a prompt template can accept.
  57. // When a prompt includes arguments, clients must provide values for all
  58. // required arguments when making a prompts/get request.
  59. type PromptArgument struct {
  60. // The name of the argument.
  61. Name string `json:"name"`
  62. // A human-readable description of the argument.
  63. Description string `json:"description,omitempty"`
  64. // Whether this argument must be provided.
  65. // If true, clients must include this argument when calling prompts/get.
  66. Required bool `json:"required,omitempty"`
  67. }
  68. // Role represents the sender or recipient of messages and data in a
  69. // conversation.
  70. type Role string
  71. const (
  72. RoleUser Role = "user"
  73. RoleAssistant Role = "assistant"
  74. )
  75. // PromptMessage describes a message returned as part of a prompt.
  76. //
  77. // This is similar to `SamplingMessage`, but also supports the embedding of
  78. // resources from the MCP server.
  79. type PromptMessage struct {
  80. Role Role `json:"role"`
  81. Content Content `json:"content"` // Can be TextContent, ImageContent, AudioContent or EmbeddedResource
  82. }
  83. // PromptListChangedNotification is an optional notification from the server
  84. // to the client, informing it that the list of prompts it offers has changed. This
  85. // may be issued by servers without any previous subscription from the client.
  86. type PromptListChangedNotification struct {
  87. Notification
  88. }
  89. // PromptOption is a function that configures a Prompt.
  90. // It provides a flexible way to set various properties of a Prompt using the functional options pattern.
  91. type PromptOption func(*Prompt)
  92. // ArgumentOption is a function that configures a PromptArgument.
  93. // It allows for flexible configuration of prompt arguments using the functional options pattern.
  94. type ArgumentOption func(*PromptArgument)
  95. //
  96. // Core Prompt Functions
  97. //
  98. // NewPrompt creates a new Prompt with the given name and options.
  99. // The prompt will be configured based on the provided options.
  100. // Options are applied in order, allowing for flexible prompt configuration.
  101. func NewPrompt(name string, opts ...PromptOption) Prompt {
  102. prompt := Prompt{
  103. Name: name,
  104. }
  105. for _, opt := range opts {
  106. opt(&prompt)
  107. }
  108. return prompt
  109. }
  110. // WithPromptDescription adds a description to the Prompt.
  111. // The description should provide a clear, human-readable explanation of what the prompt does.
  112. func WithPromptDescription(description string) PromptOption {
  113. return func(p *Prompt) {
  114. p.Description = description
  115. }
  116. }
  117. // WithArgument adds an argument to the prompt's argument list.
  118. // The argument will be configured based on the provided options.
  119. func WithArgument(name string, opts ...ArgumentOption) PromptOption {
  120. return func(p *Prompt) {
  121. arg := PromptArgument{
  122. Name: name,
  123. }
  124. for _, opt := range opts {
  125. opt(&arg)
  126. }
  127. if p.Arguments == nil {
  128. p.Arguments = make([]PromptArgument, 0)
  129. }
  130. p.Arguments = append(p.Arguments, arg)
  131. }
  132. }
  133. //
  134. // Argument Options
  135. //
  136. // ArgumentDescription adds a description to a prompt argument.
  137. // The description should explain the purpose and expected values of the argument.
  138. func ArgumentDescription(desc string) ArgumentOption {
  139. return func(arg *PromptArgument) {
  140. arg.Description = desc
  141. }
  142. }
  143. // RequiredArgument marks an argument as required in the prompt.
  144. // Required arguments must be provided when getting the prompt.
  145. func RequiredArgument() ArgumentOption {
  146. return func(arg *PromptArgument) {
  147. arg.Required = true
  148. }
  149. }