prompts.go

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