| 1 | // Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mcp |
| 6 | |
| 7 | import ( |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "maps" |
| 11 | |
| 12 | internaljson "github.com/modelcontextprotocol/go-sdk/internal/json" |
| 13 | "github.com/modelcontextprotocol/go-sdk/internal/mcpgodebug" |
| 14 | ) |
| 15 | |
| 16 | // Optional annotations for the client. The client can use annotations to inform |
| 17 | // how objects are used or displayed. |
| 18 | type Annotations struct { |
| 19 | // Describes who the intended customer of this object or data is. |
| 20 | // |
| 21 | // It can include multiple entries to indicate content useful for multiple |
| 22 | // audiences (e.g., []Role{"user", "assistant"}). |
| 23 | Audience []Role `json:"audience,omitempty"` |
| 24 | // The moment the resource was last modified, as an ISO 8601 formatted string. |
| 25 | // |
| 26 | // Should be an ISO 8601 formatted string (e.g., "2025-01-12T15:00:58Z"). |
| 27 | // |
| 28 | // Examples: last activity timestamp in an open file, timestamp when the |
| 29 | // resource was attached, etc. |
| 30 | LastModified string `json:"lastModified,omitempty"` |
| 31 | // Describes how important this data is for operating the server. |
| 32 | // |
| 33 | // A value of 1 means "most important," and indicates that the data is |
| 34 | // effectively required, while 0 means "least important," and indicates that the |
| 35 | // data is entirely optional. |
| 36 | Priority float64 `json:"priority,omitempty"` |
| 37 | } |
| 38 | |
| 39 | // CallToolParams is used by clients to call a tool. |
| 40 | type CallToolParams struct { |
| 41 | // Meta is reserved by the protocol to allow clients and servers to |
| 42 | // attach additional metadata to their responses. |
| 43 | Meta `json:"_meta,omitempty"` |
| 44 | // Name is the name of the tool to call. |
| 45 | Name string `json:"name"` |
| 46 | // Arguments holds the tool arguments. It can hold any value that can be |
| 47 | // marshaled to JSON. |
| 48 | Arguments any `json:"arguments,omitempty"` |
| 49 | } |
| 50 | |
| 51 | // CallToolParamsRaw is passed to tool handlers on the server. Its arguments |
| 52 | // are not yet unmarshaled (hence "raw"), so that the handlers can perform |
| 53 | // unmarshaling themselves. |
| 54 | type CallToolParamsRaw struct { |
| 55 | // This property is reserved by the protocol to allow clients and servers to |
| 56 | // attach additional metadata to their responses. |
| 57 | Meta `json:"_meta,omitempty"` |
| 58 | // Name is the name of the tool being called. |
| 59 | Name string `json:"name"` |
| 60 | // Arguments is the raw arguments received over the wire from the client. It |
| 61 | // is the responsibility of the tool handler to unmarshal and validate the |
| 62 | // Arguments (see [AddTool]). |
| 63 | Arguments json.RawMessage `json:"arguments,omitempty"` |
| 64 | } |
| 65 | |
| 66 | // A CallToolResult is the server's response to a tool call. |
| 67 | // |
| 68 | // The [ToolHandler] and [ToolHandlerFor] handler functions return this result, |
| 69 | // though [ToolHandlerFor] populates much of it automatically as documented at |
| 70 | // each field. |
| 71 | type CallToolResult struct { |
| 72 | // This property is reserved by the protocol to allow clients and servers to |
| 73 | // attach additional metadata to their responses. |
| 74 | Meta `json:"_meta,omitempty"` |
| 75 | |
| 76 | // A list of content objects that represent the unstructured result of the tool |
| 77 | // call. |
| 78 | // |
| 79 | // When using a [ToolHandlerFor] with structured output, if Content is unset |
| 80 | // it will be populated with JSON text content corresponding to the |
| 81 | // structured output value. |
| 82 | Content []Content `json:"content"` |
| 83 | |
| 84 | // StructuredContent is an optional value that represents the structured |
| 85 | // result of the tool call. It must marshal to a JSON object. |
| 86 | // |
| 87 | // When using a [ToolHandlerFor] with structured output, you should not |
| 88 | // populate this field. It will be automatically populated with the typed Out |
| 89 | // value. |
| 90 | StructuredContent any `json:"structuredContent,omitempty"` |
| 91 | |
| 92 | // IsError reports whether the tool call ended in an error. |
| 93 | // |
| 94 | // If not set, this is assumed to be false (the call was successful). |
| 95 | // |
| 96 | // Any errors that originate from the tool should be reported inside the |
| 97 | // Content field, with IsError set to true, not as an MCP protocol-level |
| 98 | // error response. Otherwise, the LLM would not be able to see that an error |
| 99 | // occurred and self-correct. |
| 100 | // |
| 101 | // However, any errors in finding the tool, an error indicating that the |
| 102 | // server does not support tool calls, or any other exceptional conditions, |
| 103 | // should be reported as an MCP error response. |
| 104 | // |
| 105 | // When using a [ToolHandlerFor], this field is automatically set when the |
| 106 | // tool handler returns an error, and the error string is included as text in |
| 107 | // the Content field. |
| 108 | IsError bool `json:"isError,omitempty"` |
| 109 | |
| 110 | // The error passed to setError, if any. |
| 111 | // It is not marshaled, and therefore it is only visible on the server. |
| 112 | // Its only use is in server sending middleware, where it can be accessed |
| 113 | // with getError. |
| 114 | err error |
| 115 | } |
| 116 | |
| 117 | // seterroroverwrite is a compatibility parameter that restores the pre-1.6.0 |
| 118 | // behavior of [CallToolResult.SetError], where Content was always overwritten |
| 119 | // with the error text. See the documentation for the mcpgodebug package for |
| 120 | // instructions on how to enable it. |
| 121 | // The option will be removed in the 1.8.0 version of the SDK. |
| 122 | var seterroroverwrite = mcpgodebug.Value("seterroroverwrite") |
| 123 | |
| 124 | // SetError sets the error for the tool result and sets IsError to true. |
| 125 | // If Content has not already been populated, it is set to the error text. |
| 126 | // If Content has already been populated, it is left unchanged, allowing callers |
| 127 | // to provide a user-friendly message while still recording the underlying error |
| 128 | // for inspection via [GetError] in server middleware. |
| 129 | // |
| 130 | // To restore the previous behavior where Content was always overwritten, |
| 131 | // set MCPGODEBUG=seterroroverwrite=1. |
| 132 | func (r *CallToolResult) SetError(err error) { |
| 133 | if len(r.Content) == 0 || seterroroverwrite == "1" { |
| 134 | r.Content = []Content{&TextContent{Text: err.Error()}} |
| 135 | } |
| 136 | r.IsError = true |
| 137 | r.err = err |
| 138 | } |
| 139 | |
| 140 | // GetError returns the error set with SetError, or nil if none. |
| 141 | // This function always returns nil on clients. |
| 142 | func (r *CallToolResult) GetError() error { |
| 143 | return r.err |
| 144 | } |
| 145 | |
| 146 | func (*CallToolResult) isResult() {} |
| 147 | |
| 148 | // UnmarshalJSON handles the unmarshalling of content into the Content |
| 149 | // interface. |
| 150 | func (x *CallToolResult) UnmarshalJSON(data []byte) error { |
| 151 | type res CallToolResult // avoid recursion |
| 152 | var wire struct { |
| 153 | res |
| 154 | Content []*wireContent `json:"content"` |
| 155 | } |
| 156 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 157 | return err |
| 158 | } |
| 159 | var err error |
| 160 | if wire.res.Content, err = contentsFromWire(wire.Content, nil); err != nil { |
| 161 | return err |
| 162 | } |
| 163 | *x = CallToolResult(wire.res) |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func (x *CallToolParams) isParams() {} |
| 168 | func (x *CallToolParams) GetProgressToken() any { return getProgressToken(x) } |
| 169 | func (x *CallToolParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 170 | |
| 171 | func (x *CallToolParamsRaw) isParams() {} |
| 172 | func (x *CallToolParamsRaw) GetProgressToken() any { return getProgressToken(x) } |
| 173 | func (x *CallToolParamsRaw) SetProgressToken(t any) { setProgressToken(x, t) } |
| 174 | |
| 175 | type CancelledParams struct { |
| 176 | // This property is reserved by the protocol to allow clients and servers to |
| 177 | // attach additional metadata to their responses. |
| 178 | Meta `json:"_meta,omitempty"` |
| 179 | // An optional string describing the reason for the cancellation. This may be |
| 180 | // logged or presented to the user. |
| 181 | Reason string `json:"reason,omitempty"` |
| 182 | // The ID of the request to cancel. |
| 183 | // |
| 184 | // This must correspond to the ID of a request previously issued in the same |
| 185 | // direction. |
| 186 | RequestID any `json:"requestId"` |
| 187 | } |
| 188 | |
| 189 | func (x *CancelledParams) isParams() {} |
| 190 | func (x *CancelledParams) GetProgressToken() any { return getProgressToken(x) } |
| 191 | func (x *CancelledParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 192 | |
| 193 | // RootCapabilities describes a client's support for roots. |
| 194 | type RootCapabilities struct { |
| 195 | // ListChanged reports whether the client supports notifications for |
| 196 | // changes to the roots list. |
| 197 | ListChanged bool `json:"listChanged,omitempty"` |
| 198 | } |
| 199 | |
| 200 | // Capabilities a client may support. Known capabilities are defined here, in |
| 201 | // this schema, but this is not a closed set: any client can define its own, |
| 202 | // additional capabilities. |
| 203 | type ClientCapabilities struct { |
| 204 | // NOTE: any addition to ClientCapabilities must also be reflected in |
| 205 | // [ClientCapabilities.clone]. |
| 206 | |
| 207 | // Experimental reports non-standard capabilities that the client supports. |
| 208 | // The caller should not modify the map after assigning it. |
| 209 | Experimental map[string]any `json:"experimental,omitempty"` |
| 210 | // Extensions reports extensions that the client supports. |
| 211 | // Keys are extension identifiers in "{vendor-prefix}/{extension-name}" format. |
| 212 | // Values are per-extension settings objects; use [ClientCapabilities.AddExtension] |
| 213 | // to ensure nil settings are normalized to empty objects. |
| 214 | // The caller should not modify the map or its values after assigning it. |
| 215 | Extensions map[string]any `json:"extensions,omitempty"` |
| 216 | // Roots describes the client's support for roots. |
| 217 | // |
| 218 | // Deprecated: use RootsV2. As described in #607, Roots should have been a |
| 219 | // pointer to a RootCapabilities value. Roots will be continue to be |
| 220 | // populated, but any new fields will only be added in the RootsV2 field. |
| 221 | Roots struct { |
| 222 | // ListChanged reports whether the client supports notifications for |
| 223 | // changes to the roots list. |
| 224 | ListChanged bool `json:"listChanged,omitempty"` |
| 225 | } `json:"roots,omitempty"` |
| 226 | // RootsV2 is present if the client supports roots. When capabilities are explicitly configured via [ClientOptions.Capabilities] |
| 227 | RootsV2 *RootCapabilities `json:"-"` |
| 228 | // Sampling is present if the client supports sampling from an LLM. |
| 229 | Sampling *SamplingCapabilities `json:"sampling,omitempty"` |
| 230 | // Elicitation is present if the client supports elicitation from the server. |
| 231 | Elicitation *ElicitationCapabilities `json:"elicitation,omitempty"` |
| 232 | } |
| 233 | |
| 234 | // AddExtension adds an extension with the given name and settings. |
| 235 | // If settings is nil, an empty map is used to ensure valid JSON serialization |
| 236 | // (the spec requires an object, not null). |
| 237 | // The settings map should not be modified after the call. |
| 238 | func (c *ClientCapabilities) AddExtension(name string, settings map[string]any) { |
| 239 | if c.Extensions == nil { |
| 240 | c.Extensions = make(map[string]any) |
| 241 | } |
| 242 | if settings == nil { |
| 243 | settings = map[string]any{} |
| 244 | } |
| 245 | c.Extensions[name] = settings |
| 246 | } |
| 247 | |
| 248 | // clone returns a copy of the ClientCapabilities. |
| 249 | // Values in the Extensions and Experimental maps are shallow-copied. |
| 250 | func (c *ClientCapabilities) clone() *ClientCapabilities { |
| 251 | cp := *c |
| 252 | cp.Experimental = maps.Clone(c.Experimental) |
| 253 | cp.Extensions = maps.Clone(c.Extensions) |
| 254 | cp.RootsV2 = shallowClone(c.RootsV2) |
| 255 | if c.Sampling != nil { |
| 256 | x := *c.Sampling |
| 257 | x.Tools = shallowClone(c.Sampling.Tools) |
| 258 | x.Context = shallowClone(c.Sampling.Context) |
| 259 | cp.Sampling = &x |
| 260 | } |
| 261 | if c.Elicitation != nil { |
| 262 | x := *c.Elicitation |
| 263 | x.Form = shallowClone(c.Elicitation.Form) |
| 264 | x.URL = shallowClone(c.Elicitation.URL) |
| 265 | cp.Elicitation = &x |
| 266 | } |
| 267 | return &cp |
| 268 | } |
| 269 | |
| 270 | // shallowClone returns a shallow clone of *p, or nil if p is nil. |
| 271 | func shallowClone[T any](p *T) *T { |
| 272 | if p == nil { |
| 273 | return nil |
| 274 | } |
| 275 | x := *p |
| 276 | return &x |
| 277 | } |
| 278 | |
| 279 | func (c *ClientCapabilities) toV2() *clientCapabilitiesV2 { |
| 280 | return &clientCapabilitiesV2{ |
| 281 | ClientCapabilities: *c, |
| 282 | Roots: c.RootsV2, |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // clientCapabilitiesV2 is a version of ClientCapabilities that fixes the bug |
| 287 | // described in #607: Roots should have been a pointer to value type |
| 288 | // RootCapabilities. |
| 289 | type clientCapabilitiesV2 struct { |
| 290 | ClientCapabilities |
| 291 | Roots *RootCapabilities `json:"roots,omitempty"` |
| 292 | } |
| 293 | |
| 294 | func (c *clientCapabilitiesV2) toV1() *ClientCapabilities { |
| 295 | caps := c.ClientCapabilities |
| 296 | caps.RootsV2 = c.Roots |
| 297 | // Sync Roots from RootsV2 for backward compatibility (#607). |
| 298 | if caps.RootsV2 != nil { |
| 299 | caps.Roots = *caps.RootsV2 |
| 300 | } |
| 301 | return &caps |
| 302 | } |
| 303 | |
| 304 | type CompleteParamsArgument struct { |
| 305 | // The name of the argument |
| 306 | Name string `json:"name"` |
| 307 | // The value of the argument to use for completion matching. |
| 308 | Value string `json:"value"` |
| 309 | } |
| 310 | |
| 311 | // CompleteContext represents additional, optional context for completions. |
| 312 | type CompleteContext struct { |
| 313 | // Previously-resolved variables in a URI template or prompt. |
| 314 | Arguments map[string]string `json:"arguments,omitempty"` |
| 315 | } |
| 316 | |
| 317 | // CompleteReference represents a completion reference type (ref/prompt ref/resource). |
| 318 | // The Type field determines which other fields are relevant. |
| 319 | type CompleteReference struct { |
| 320 | Type string `json:"type"` |
| 321 | // Name is relevant when Type is "ref/prompt". |
| 322 | Name string `json:"name,omitempty"` |
| 323 | // URI is relevant when Type is "ref/resource". |
| 324 | URI string `json:"uri,omitempty"` |
| 325 | } |
| 326 | |
| 327 | func (r *CompleteReference) UnmarshalJSON(data []byte) error { |
| 328 | type wireCompleteReference CompleteReference // for naive unmarshaling |
| 329 | var r2 wireCompleteReference |
| 330 | if err := internaljson.Unmarshal(data, &r2); err != nil { |
| 331 | return err |
| 332 | } |
| 333 | switch r2.Type { |
| 334 | case "ref/prompt", "ref/resource": |
| 335 | if r2.Type == "ref/prompt" && r2.URI != "" { |
| 336 | return fmt.Errorf("reference of type %q must not have a URI set", r2.Type) |
| 337 | } |
| 338 | if r2.Type == "ref/resource" && r2.Name != "" { |
| 339 | return fmt.Errorf("reference of type %q must not have a Name set", r2.Type) |
| 340 | } |
| 341 | default: |
| 342 | return fmt.Errorf("unrecognized content type %q", r2.Type) |
| 343 | } |
| 344 | *r = CompleteReference(r2) |
| 345 | return nil |
| 346 | } |
| 347 | |
| 348 | func (r *CompleteReference) MarshalJSON() ([]byte, error) { |
| 349 | // Validation for marshalling: ensure consistency before converting to JSON. |
| 350 | switch r.Type { |
| 351 | case "ref/prompt": |
| 352 | if r.URI != "" { |
| 353 | return nil, fmt.Errorf("reference of type %q must not have a URI set for marshalling", r.Type) |
| 354 | } |
| 355 | case "ref/resource": |
| 356 | if r.Name != "" { |
| 357 | return nil, fmt.Errorf("reference of type %q must not have a Name set for marshalling", r.Type) |
| 358 | } |
| 359 | default: |
| 360 | return nil, fmt.Errorf("unrecognized reference type %q for marshalling", r.Type) |
| 361 | } |
| 362 | |
| 363 | type wireReference CompleteReference |
| 364 | return json.Marshal(wireReference(*r)) |
| 365 | } |
| 366 | |
| 367 | type CompleteParams struct { |
| 368 | // This property is reserved by the protocol to allow clients and servers to |
| 369 | // attach additional metadata to their responses. |
| 370 | Meta `json:"_meta,omitempty"` |
| 371 | // The argument's information |
| 372 | Argument CompleteParamsArgument `json:"argument"` |
| 373 | Context *CompleteContext `json:"context,omitempty"` |
| 374 | Ref *CompleteReference `json:"ref"` |
| 375 | } |
| 376 | |
| 377 | func (*CompleteParams) isParams() {} |
| 378 | |
| 379 | type CompletionResultDetails struct { |
| 380 | HasMore bool `json:"hasMore,omitempty"` |
| 381 | Total int `json:"total,omitempty"` |
| 382 | Values []string `json:"values"` |
| 383 | } |
| 384 | |
| 385 | // The server's response to a completion/complete request |
| 386 | type CompleteResult struct { |
| 387 | // This property is reserved by the protocol to allow clients and servers to |
| 388 | // attach additional metadata to their responses. |
| 389 | Meta `json:"_meta,omitempty"` |
| 390 | Completion CompletionResultDetails `json:"completion"` |
| 391 | } |
| 392 | |
| 393 | func (*CompleteResult) isResult() {} |
| 394 | |
| 395 | type CreateMessageParams struct { |
| 396 | // This property is reserved by the protocol to allow clients and servers to |
| 397 | // attach additional metadata to their responses. |
| 398 | Meta `json:"_meta,omitempty"` |
| 399 | // A request to include context from one or more MCP servers (including the |
| 400 | // caller), to be attached to the prompt. The client may ignore this request. |
| 401 | // |
| 402 | // The default is "none". Values "thisServer" and |
| 403 | // "allServers" are soft-deprecated. Servers SHOULD only use these values if |
| 404 | // the client declares ClientCapabilities.sampling.context. These values may |
| 405 | // be removed in future spec releases. |
| 406 | IncludeContext string `json:"includeContext,omitempty"` |
| 407 | // The maximum number of tokens to sample, as requested by the server. The |
| 408 | // client may choose to sample fewer tokens than requested. |
| 409 | MaxTokens int64 `json:"maxTokens"` |
| 410 | Messages []*SamplingMessage `json:"messages"` |
| 411 | // Optional metadata to pass through to the LLM provider. The format of this |
| 412 | // metadata is provider-specific. |
| 413 | Metadata any `json:"metadata,omitempty"` |
| 414 | // The server's preferences for which model to select. The client may ignore |
| 415 | // these preferences. |
| 416 | ModelPreferences *ModelPreferences `json:"modelPreferences,omitempty"` |
| 417 | StopSequences []string `json:"stopSequences,omitempty"` |
| 418 | // An optional system prompt the server wants to use for sampling. The client |
| 419 | // may modify or omit this prompt. |
| 420 | SystemPrompt string `json:"systemPrompt,omitempty"` |
| 421 | Temperature float64 `json:"temperature,omitempty"` |
| 422 | } |
| 423 | |
| 424 | func (x *CreateMessageParams) isParams() {} |
| 425 | func (x *CreateMessageParams) GetProgressToken() any { return getProgressToken(x) } |
| 426 | func (x *CreateMessageParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 427 | |
| 428 | // CreateMessageWithToolsParams is a sampling request that includes tools. |
| 429 | // It extends the basic [CreateMessageParams] fields with tools, tool choice, |
| 430 | // and messages that support array content (for parallel tool calls). |
| 431 | // |
| 432 | // Use with [ServerSession.CreateMessageWithTools]. |
| 433 | type CreateMessageWithToolsParams struct { |
| 434 | Meta `json:"_meta,omitempty"` |
| 435 | IncludeContext string `json:"includeContext,omitempty"` |
| 436 | MaxTokens int64 `json:"maxTokens"` |
| 437 | // Messages supports array content for tool_use and tool_result blocks. |
| 438 | Messages []*SamplingMessageV2 `json:"messages"` |
| 439 | Metadata any `json:"metadata,omitempty"` |
| 440 | ModelPreferences *ModelPreferences `json:"modelPreferences,omitempty"` |
| 441 | StopSequences []string `json:"stopSequences,omitempty"` |
| 442 | SystemPrompt string `json:"systemPrompt,omitempty"` |
| 443 | Temperature float64 `json:"temperature,omitempty"` |
| 444 | // Tools is the list of tools available for the model to use. |
| 445 | Tools []*Tool `json:"tools,omitempty"` |
| 446 | // ToolChoice controls how the model should use tools. |
| 447 | ToolChoice *ToolChoice `json:"toolChoice,omitempty"` |
| 448 | } |
| 449 | |
| 450 | func (x *CreateMessageWithToolsParams) isParams() {} |
| 451 | func (x *CreateMessageWithToolsParams) GetProgressToken() any { return getProgressToken(x) } |
| 452 | func (x *CreateMessageWithToolsParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 453 | |
| 454 | // toBase converts to CreateMessageParams by taking the content block from each |
| 455 | // message. Tools and ToolChoice are dropped. Returns an error if any message |
| 456 | // has multiple content blocks, since SamplingMessage only supports one. |
| 457 | func (p *CreateMessageWithToolsParams) toBase() (*CreateMessageParams, error) { |
| 458 | var msgs []*SamplingMessage |
| 459 | for _, m := range p.Messages { |
| 460 | if len(m.Content) > 1 { |
| 461 | return nil, fmt.Errorf("message has %d content blocks; use CreateMessageWithToolsHandler to support multiple content", len(m.Content)) |
| 462 | } |
| 463 | var content Content |
| 464 | if len(m.Content) > 0 { |
| 465 | content = m.Content[0] |
| 466 | } |
| 467 | msgs = append(msgs, &SamplingMessage{Content: content, Role: m.Role}) |
| 468 | } |
| 469 | return &CreateMessageParams{ |
| 470 | Meta: p.Meta, |
| 471 | IncludeContext: p.IncludeContext, |
| 472 | MaxTokens: p.MaxTokens, |
| 473 | Messages: msgs, |
| 474 | Metadata: p.Metadata, |
| 475 | ModelPreferences: p.ModelPreferences, |
| 476 | StopSequences: p.StopSequences, |
| 477 | SystemPrompt: p.SystemPrompt, |
| 478 | Temperature: p.Temperature, |
| 479 | }, nil |
| 480 | } |
| 481 | |
| 482 | // SamplingMessageV2 describes a message issued to or received from an |
| 483 | // LLM API, supporting array content for parallel tool calls. The "V2" refers |
| 484 | // to the 2025-11-25 spec, which changed content from a single block to |
| 485 | // single-or-array. In v2 of the SDK, this will replace [SamplingMessage]. |
| 486 | // |
| 487 | // When marshaling, a single-element Content slice is marshaled as a single |
| 488 | // object for compatibility with pre-2025-11-25 implementations. When |
| 489 | // unmarshaling, a single JSON content object is accepted and wrapped in a |
| 490 | // one-element slice. |
| 491 | type SamplingMessageV2 struct { |
| 492 | Content []Content `json:"content"` |
| 493 | Role Role `json:"role"` |
| 494 | } |
| 495 | |
| 496 | var samplingWithToolsAllow = map[string]bool{ |
| 497 | "text": true, "image": true, "audio": true, |
| 498 | "tool_use": true, "tool_result": true, |
| 499 | } |
| 500 | |
| 501 | // MarshalJSON marshals the message. A single-element Content slice is marshaled |
| 502 | // as a single object for backward compatibility. |
| 503 | func (m *SamplingMessageV2) MarshalJSON() ([]byte, error) { |
| 504 | if len(m.Content) == 1 { |
| 505 | return json.Marshal(&SamplingMessage{Content: m.Content[0], Role: m.Role}) |
| 506 | } |
| 507 | type msg SamplingMessageV2 // avoid recursion |
| 508 | return json.Marshal((*msg)(m)) |
| 509 | } |
| 510 | |
| 511 | func (m *SamplingMessageV2) UnmarshalJSON(data []byte) error { |
| 512 | type msg SamplingMessageV2 // avoid recursion |
| 513 | var wire struct { |
| 514 | msg |
| 515 | Content json.RawMessage `json:"content"` |
| 516 | } |
| 517 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 518 | return err |
| 519 | } |
| 520 | var err error |
| 521 | if wire.msg.Content, err = unmarshalContent(wire.Content, samplingWithToolsAllow); err != nil { |
| 522 | return err |
| 523 | } |
| 524 | *m = SamplingMessageV2(wire.msg) |
| 525 | return nil |
| 526 | } |
| 527 | |
| 528 | // The client's response to a sampling/create_message request from the server. |
| 529 | // The client should inform the user before returning the sampled message, to |
| 530 | // allow them to inspect the response (human in the loop) and decide whether to |
| 531 | // allow the server to see it. |
| 532 | type CreateMessageResult struct { |
| 533 | // This property is reserved by the protocol to allow clients and servers to |
| 534 | // attach additional metadata to their responses. |
| 535 | Meta `json:"_meta,omitempty"` |
| 536 | Content Content `json:"content"` |
| 537 | // The name of the model that generated the message. |
| 538 | Model string `json:"model"` |
| 539 | Role Role `json:"role"` |
| 540 | // The reason why sampling stopped, if known. |
| 541 | // |
| 542 | // Standard values: |
| 543 | // - "endTurn": natural end of the assistant's turn |
| 544 | // - "stopSequence": a stop sequence was encountered |
| 545 | // - "maxTokens": reached the maximum token limit |
| 546 | // - "toolUse": the model wants to use one or more tools |
| 547 | StopReason string `json:"stopReason,omitempty"` |
| 548 | } |
| 549 | |
| 550 | func (*CreateMessageResult) isResult() {} |
| 551 | func (r *CreateMessageResult) UnmarshalJSON(data []byte) error { |
| 552 | type result CreateMessageResult // avoid recursion |
| 553 | var wire struct { |
| 554 | result |
| 555 | Content *wireContent `json:"content"` |
| 556 | } |
| 557 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 558 | return err |
| 559 | } |
| 560 | var err error |
| 561 | if wire.result.Content, err = contentFromWire(wire.Content, map[string]bool{"text": true, "image": true, "audio": true}); err != nil { |
| 562 | return err |
| 563 | } |
| 564 | *r = CreateMessageResult(wire.result) |
| 565 | return nil |
| 566 | } |
| 567 | |
| 568 | // CreateMessageWithToolsResult is the client's response to a |
| 569 | // sampling/create_message request that included tools. Content is a slice to |
| 570 | // support parallel tool calls (multiple tool_use blocks in one response). |
| 571 | // |
| 572 | // Use [ServerSession.CreateMessageWithTools] to send a sampling request with |
| 573 | // tools and receive this result type. |
| 574 | // |
| 575 | // When unmarshaling, a single JSON content object is accepted and wrapped in a |
| 576 | // one-element slice, for compatibility with clients that return a single block. |
| 577 | type CreateMessageWithToolsResult struct { |
| 578 | Meta `json:"_meta,omitempty"` |
| 579 | Content []Content `json:"content"` |
| 580 | Model string `json:"model"` |
| 581 | Role Role `json:"role"` |
| 582 | // The reason why sampling stopped. |
| 583 | // |
| 584 | // Standard values: "endTurn", "stopSequence", "maxTokens", "toolUse". |
| 585 | StopReason string `json:"stopReason,omitempty"` |
| 586 | } |
| 587 | |
| 588 | // createMessageWithToolsResultAllow lists content types valid in assistant responses. |
| 589 | // tool_result is excluded: it only appears in user messages. |
| 590 | var createMessageWithToolsResultAllow = map[string]bool{ |
| 591 | "text": true, "image": true, "audio": true, |
| 592 | "tool_use": true, |
| 593 | } |
| 594 | |
| 595 | func (*CreateMessageWithToolsResult) isResult() {} |
| 596 | |
| 597 | // MarshalJSON marshals the result. When Content has a single element, it is |
| 598 | // marshaled as a single object for compatibility with pre-2025-11-25 |
| 599 | // implementations that expect a single content block. |
| 600 | func (r *CreateMessageWithToolsResult) MarshalJSON() ([]byte, error) { |
| 601 | if len(r.Content) == 1 { |
| 602 | return json.Marshal(&CreateMessageResult{ |
| 603 | Meta: r.Meta, |
| 604 | Content: r.Content[0], |
| 605 | Model: r.Model, |
| 606 | Role: r.Role, |
| 607 | StopReason: r.StopReason, |
| 608 | }) |
| 609 | } |
| 610 | type result CreateMessageWithToolsResult // avoid recursion |
| 611 | return json.Marshal((*result)(r)) |
| 612 | } |
| 613 | |
| 614 | func (r *CreateMessageWithToolsResult) UnmarshalJSON(data []byte) error { |
| 615 | type result CreateMessageWithToolsResult // avoid recursion |
| 616 | var wire struct { |
| 617 | result |
| 618 | Content json.RawMessage `json:"content"` |
| 619 | } |
| 620 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 621 | return err |
| 622 | } |
| 623 | var err error |
| 624 | if wire.result.Content, err = unmarshalContent(wire.Content, createMessageWithToolsResultAllow); err != nil { |
| 625 | return err |
| 626 | } |
| 627 | *r = CreateMessageWithToolsResult(wire.result) |
| 628 | return nil |
| 629 | } |
| 630 | |
| 631 | // toWithTools converts a CreateMessageResult to CreateMessageWithToolsResult. |
| 632 | func (r *CreateMessageResult) toWithTools() *CreateMessageWithToolsResult { |
| 633 | var content []Content |
| 634 | if r.Content != nil { |
| 635 | content = []Content{r.Content} |
| 636 | } |
| 637 | return &CreateMessageWithToolsResult{ |
| 638 | Meta: r.Meta, |
| 639 | Content: content, |
| 640 | Model: r.Model, |
| 641 | Role: r.Role, |
| 642 | StopReason: r.StopReason, |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | type GetPromptParams struct { |
| 647 | // This property is reserved by the protocol to allow clients and servers to |
| 648 | // attach additional metadata to their responses. |
| 649 | Meta `json:"_meta,omitempty"` |
| 650 | // Arguments to use for templating the prompt. |
| 651 | Arguments map[string]string `json:"arguments,omitempty"` |
| 652 | // The name of the prompt or prompt template. |
| 653 | Name string `json:"name"` |
| 654 | } |
| 655 | |
| 656 | func (x *GetPromptParams) isParams() {} |
| 657 | func (x *GetPromptParams) GetProgressToken() any { return getProgressToken(x) } |
| 658 | func (x *GetPromptParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 659 | |
| 660 | // The server's response to a prompts/get request from the client. |
| 661 | type GetPromptResult struct { |
| 662 | // This property is reserved by the protocol to allow clients and servers to |
| 663 | // attach additional metadata to their responses. |
| 664 | Meta `json:"_meta,omitempty"` |
| 665 | // An optional description for the prompt. |
| 666 | Description string `json:"description,omitempty"` |
| 667 | Messages []*PromptMessage `json:"messages"` |
| 668 | } |
| 669 | |
| 670 | func (*GetPromptResult) isResult() {} |
| 671 | |
| 672 | // InitializeParams is sent by the client to initialize the session. |
| 673 | type InitializeParams struct { |
| 674 | // This property is reserved by the protocol to allow clients and servers to |
| 675 | // attach additional metadata to their responses. |
| 676 | Meta `json:"_meta,omitempty"` |
| 677 | // Capabilities describes the client's capabilities. |
| 678 | Capabilities *ClientCapabilities `json:"capabilities"` |
| 679 | // ClientInfo provides information about the client. |
| 680 | ClientInfo *Implementation `json:"clientInfo"` |
| 681 | // ProtocolVersion is the latest version of the Model Context Protocol that |
| 682 | // the client supports. |
| 683 | ProtocolVersion string `json:"protocolVersion"` |
| 684 | } |
| 685 | |
| 686 | func (p *InitializeParams) toV2() *initializeParamsV2 { |
| 687 | return &initializeParamsV2{ |
| 688 | InitializeParams: *p, |
| 689 | Capabilities: p.Capabilities.toV2(), |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // initializeParamsV2 works around the mistake in #607: Capabilities.Roots |
| 694 | // should have been a pointer. |
| 695 | type initializeParamsV2 struct { |
| 696 | InitializeParams |
| 697 | Capabilities *clientCapabilitiesV2 `json:"capabilities"` |
| 698 | } |
| 699 | |
| 700 | func (p *initializeParamsV2) toV1() *InitializeParams { |
| 701 | p1 := p.InitializeParams |
| 702 | if p.Capabilities != nil { |
| 703 | p1.Capabilities = p.Capabilities.toV1() |
| 704 | } |
| 705 | return &p1 |
| 706 | } |
| 707 | |
| 708 | func (x *InitializeParams) isParams() {} |
| 709 | func (x *InitializeParams) GetProgressToken() any { return getProgressToken(x) } |
| 710 | func (x *InitializeParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 711 | |
| 712 | // InitializeResult is sent by the server in response to an initialize request |
| 713 | // from the client. |
| 714 | type InitializeResult struct { |
| 715 | // This property is reserved by the protocol to allow clients and servers to |
| 716 | // attach additional metadata to their responses. |
| 717 | Meta `json:"_meta,omitempty"` |
| 718 | // Capabilities describes the server's capabilities. |
| 719 | Capabilities *ServerCapabilities `json:"capabilities"` |
| 720 | // Instructions describing how to use the server and its features. |
| 721 | // |
| 722 | // This can be used by clients to improve the LLM's understanding of available |
| 723 | // tools, resources, etc. It can be thought of like a "hint" to the model. For |
| 724 | // example, this information may be added to the system prompt. |
| 725 | Instructions string `json:"instructions,omitempty"` |
| 726 | // The version of the Model Context Protocol that the server wants to use. This |
| 727 | // may not match the version that the client requested. If the client cannot |
| 728 | // support this version, it must disconnect. |
| 729 | ProtocolVersion string `json:"protocolVersion"` |
| 730 | ServerInfo *Implementation `json:"serverInfo"` |
| 731 | } |
| 732 | |
| 733 | func (*InitializeResult) isResult() {} |
| 734 | |
| 735 | type InitializedParams struct { |
| 736 | // Meta is reserved by the protocol to allow clients and servers to attach |
| 737 | // additional metadata to their responses. |
| 738 | Meta `json:"_meta,omitempty"` |
| 739 | } |
| 740 | |
| 741 | func (x *InitializedParams) isParams() {} |
| 742 | func (x *InitializedParams) GetProgressToken() any { return getProgressToken(x) } |
| 743 | func (x *InitializedParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 744 | |
| 745 | type ListPromptsParams struct { |
| 746 | // This property is reserved by the protocol to allow clients and servers to |
| 747 | // attach additional metadata to their responses. |
| 748 | Meta `json:"_meta,omitempty"` |
| 749 | // An opaque token representing the current pagination position. If provided, |
| 750 | // the server should return results starting after this cursor. |
| 751 | Cursor string `json:"cursor,omitempty"` |
| 752 | } |
| 753 | |
| 754 | func (x *ListPromptsParams) isParams() {} |
| 755 | func (x *ListPromptsParams) GetProgressToken() any { return getProgressToken(x) } |
| 756 | func (x *ListPromptsParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 757 | func (x *ListPromptsParams) cursorPtr() *string { return &x.Cursor } |
| 758 | |
| 759 | // The server's response to a prompts/list request from the client. |
| 760 | type ListPromptsResult struct { |
| 761 | // This property is reserved by the protocol to allow clients and servers to |
| 762 | // attach additional metadata to their responses. |
| 763 | Meta `json:"_meta,omitempty"` |
| 764 | // An opaque token representing the pagination position after the last returned |
| 765 | // result. If present, there may be more results available. |
| 766 | NextCursor string `json:"nextCursor,omitempty"` |
| 767 | Prompts []*Prompt `json:"prompts"` |
| 768 | } |
| 769 | |
| 770 | func (x *ListPromptsResult) isResult() {} |
| 771 | func (x *ListPromptsResult) nextCursorPtr() *string { return &x.NextCursor } |
| 772 | |
| 773 | type ListResourceTemplatesParams struct { |
| 774 | // This property is reserved by the protocol to allow clients and servers to |
| 775 | // attach additional metadata to their responses. |
| 776 | Meta `json:"_meta,omitempty"` |
| 777 | // An opaque token representing the current pagination position. If provided, |
| 778 | // the server should return results starting after this cursor. |
| 779 | Cursor string `json:"cursor,omitempty"` |
| 780 | } |
| 781 | |
| 782 | func (x *ListResourceTemplatesParams) isParams() {} |
| 783 | func (x *ListResourceTemplatesParams) GetProgressToken() any { return getProgressToken(x) } |
| 784 | func (x *ListResourceTemplatesParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 785 | func (x *ListResourceTemplatesParams) cursorPtr() *string { return &x.Cursor } |
| 786 | |
| 787 | // The server's response to a resources/templates/list request from the client. |
| 788 | type ListResourceTemplatesResult struct { |
| 789 | // This property is reserved by the protocol to allow clients and servers to |
| 790 | // attach additional metadata to their responses. |
| 791 | Meta `json:"_meta,omitempty"` |
| 792 | // An opaque token representing the pagination position after the last returned |
| 793 | // result. If present, there may be more results available. |
| 794 | NextCursor string `json:"nextCursor,omitempty"` |
| 795 | ResourceTemplates []*ResourceTemplate `json:"resourceTemplates"` |
| 796 | } |
| 797 | |
| 798 | func (x *ListResourceTemplatesResult) isResult() {} |
| 799 | func (x *ListResourceTemplatesResult) nextCursorPtr() *string { return &x.NextCursor } |
| 800 | |
| 801 | type ListResourcesParams struct { |
| 802 | // This property is reserved by the protocol to allow clients and servers to |
| 803 | // attach additional metadata to their responses. |
| 804 | Meta `json:"_meta,omitempty"` |
| 805 | // An opaque token representing the current pagination position. If provided, |
| 806 | // the server should return results starting after this cursor. |
| 807 | Cursor string `json:"cursor,omitempty"` |
| 808 | } |
| 809 | |
| 810 | func (x *ListResourcesParams) isParams() {} |
| 811 | func (x *ListResourcesParams) GetProgressToken() any { return getProgressToken(x) } |
| 812 | func (x *ListResourcesParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 813 | func (x *ListResourcesParams) cursorPtr() *string { return &x.Cursor } |
| 814 | |
| 815 | // The server's response to a resources/list request from the client. |
| 816 | type ListResourcesResult struct { |
| 817 | // This property is reserved by the protocol to allow clients and servers to |
| 818 | // attach additional metadata to their responses. |
| 819 | Meta `json:"_meta,omitempty"` |
| 820 | // An opaque token representing the pagination position after the last returned |
| 821 | // result. If present, there may be more results available. |
| 822 | NextCursor string `json:"nextCursor,omitempty"` |
| 823 | Resources []*Resource `json:"resources"` |
| 824 | } |
| 825 | |
| 826 | func (x *ListResourcesResult) isResult() {} |
| 827 | func (x *ListResourcesResult) nextCursorPtr() *string { return &x.NextCursor } |
| 828 | |
| 829 | type ListRootsParams struct { |
| 830 | // This property is reserved by the protocol to allow clients and servers to |
| 831 | // attach additional metadata to their responses. |
| 832 | Meta `json:"_meta,omitempty"` |
| 833 | } |
| 834 | |
| 835 | func (x *ListRootsParams) isParams() {} |
| 836 | func (x *ListRootsParams) GetProgressToken() any { return getProgressToken(x) } |
| 837 | func (x *ListRootsParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 838 | |
| 839 | // The client's response to a roots/list request from the server. This result |
| 840 | // contains an array of Root objects, each representing a root directory or file |
| 841 | // that the server can operate on. |
| 842 | type ListRootsResult struct { |
| 843 | // This property is reserved by the protocol to allow clients and servers to |
| 844 | // attach additional metadata to their responses. |
| 845 | Meta `json:"_meta,omitempty"` |
| 846 | Roots []*Root `json:"roots"` |
| 847 | } |
| 848 | |
| 849 | func (*ListRootsResult) isResult() {} |
| 850 | |
| 851 | type ListToolsParams struct { |
| 852 | // This property is reserved by the protocol to allow clients and servers to |
| 853 | // attach additional metadata to their responses. |
| 854 | Meta `json:"_meta,omitempty"` |
| 855 | // An opaque token representing the current pagination position. If provided, |
| 856 | // the server should return results starting after this cursor. |
| 857 | Cursor string `json:"cursor,omitempty"` |
| 858 | } |
| 859 | |
| 860 | func (x *ListToolsParams) isParams() {} |
| 861 | func (x *ListToolsParams) GetProgressToken() any { return getProgressToken(x) } |
| 862 | func (x *ListToolsParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 863 | func (x *ListToolsParams) cursorPtr() *string { return &x.Cursor } |
| 864 | |
| 865 | // The server's response to a tools/list request from the client. |
| 866 | type ListToolsResult struct { |
| 867 | // This property is reserved by the protocol to allow clients and servers to |
| 868 | // attach additional metadata to their responses. |
| 869 | Meta `json:"_meta,omitempty"` |
| 870 | // An opaque token representing the pagination position after the last returned |
| 871 | // result. If present, there may be more results available. |
| 872 | NextCursor string `json:"nextCursor,omitempty"` |
| 873 | Tools []*Tool `json:"tools"` |
| 874 | } |
| 875 | |
| 876 | func (x *ListToolsResult) isResult() {} |
| 877 | func (x *ListToolsResult) nextCursorPtr() *string { return &x.NextCursor } |
| 878 | |
| 879 | // The severity of a log message. |
| 880 | // |
| 881 | // These map to syslog message severities, as specified in RFC-5424: |
| 882 | // https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1 |
| 883 | type LoggingLevel string |
| 884 | |
| 885 | type LoggingMessageParams struct { |
| 886 | // This property is reserved by the protocol to allow clients and servers to |
| 887 | // attach additional metadata to their responses. |
| 888 | Meta `json:"_meta,omitempty"` |
| 889 | // The data to be logged, such as a string message or an object. Any JSON |
| 890 | // serializable type is allowed here. |
| 891 | Data any `json:"data"` |
| 892 | // The severity of this log message. |
| 893 | Level LoggingLevel `json:"level"` |
| 894 | // An optional name of the logger issuing this message. |
| 895 | Logger string `json:"logger,omitempty"` |
| 896 | } |
| 897 | |
| 898 | func (x *LoggingMessageParams) isParams() {} |
| 899 | func (x *LoggingMessageParams) GetProgressToken() any { return getProgressToken(x) } |
| 900 | func (x *LoggingMessageParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 901 | |
| 902 | // Hints to use for model selection. |
| 903 | // |
| 904 | // Keys not declared here are currently left unspecified by the spec and are up |
| 905 | // to the client to interpret. |
| 906 | type ModelHint struct { |
| 907 | // A hint for a model name. |
| 908 | // |
| 909 | // The client should treat this as a substring of a model name; for example: - |
| 910 | // `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022` - `sonnet` |
| 911 | // should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc. - |
| 912 | // `claude` should match any Claude model |
| 913 | // |
| 914 | // The client may also map the string to a different provider's model name or a |
| 915 | // different model family, as long as it fills a similar niche; for example: - |
| 916 | // `gemini-1.5-flash` could match `claude-3-haiku-20240307` |
| 917 | Name string `json:"name,omitempty"` |
| 918 | } |
| 919 | |
| 920 | // The server's preferences for model selection, requested of the client during |
| 921 | // sampling. |
| 922 | // |
| 923 | // Because LLMs can vary along multiple dimensions, choosing the "best" model is |
| 924 | // rarely straightforward. Different models excel in different areas—some are |
| 925 | // faster but less capable, others are more capable but more expensive, and so |
| 926 | // on. This interface allows servers to express their priorities across multiple |
| 927 | // dimensions to help clients make an appropriate selection for their use case. |
| 928 | // |
| 929 | // These preferences are always advisory. The client may ignore them. It is also |
| 930 | // up to the client to decide how to interpret these preferences and how to |
| 931 | // balance them against other considerations. |
| 932 | type ModelPreferences struct { |
| 933 | // How much to prioritize cost when selecting a model. A value of 0 means cost |
| 934 | // is not important, while a value of 1 means cost is the most important factor. |
| 935 | CostPriority float64 `json:"costPriority,omitempty"` |
| 936 | // Optional hints to use for model selection. |
| 937 | // |
| 938 | // If multiple hints are specified, the client must evaluate them in order (such |
| 939 | // that the first match is taken). |
| 940 | // |
| 941 | // The client should prioritize these hints over the numeric priorities, but may |
| 942 | // still use the priorities to select from ambiguous matches. |
| 943 | Hints []*ModelHint `json:"hints,omitempty"` |
| 944 | // How much to prioritize intelligence and capabilities when selecting a model. |
| 945 | // A value of 0 means intelligence is not important, while a value of 1 means |
| 946 | // intelligence is the most important factor. |
| 947 | IntelligencePriority float64 `json:"intelligencePriority,omitempty"` |
| 948 | // How much to prioritize sampling speed (latency) when selecting a model. A |
| 949 | // value of 0 means speed is not important, while a value of 1 means speed is |
| 950 | // the most important factor. |
| 951 | SpeedPriority float64 `json:"speedPriority,omitempty"` |
| 952 | } |
| 953 | |
| 954 | type PingParams struct { |
| 955 | // This property is reserved by the protocol to allow clients and servers to |
| 956 | // attach additional metadata to their responses. |
| 957 | Meta `json:"_meta,omitempty"` |
| 958 | } |
| 959 | |
| 960 | func (x *PingParams) isParams() {} |
| 961 | func (x *PingParams) GetProgressToken() any { return getProgressToken(x) } |
| 962 | func (x *PingParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 963 | |
| 964 | type ProgressNotificationParams struct { |
| 965 | // This property is reserved by the protocol to allow clients and servers to |
| 966 | // attach additional metadata to their responses. |
| 967 | Meta `json:"_meta,omitempty"` |
| 968 | // The progress token which was given in the initial request, used to associate |
| 969 | // this notification with the request that is proceeding. |
| 970 | ProgressToken any `json:"progressToken"` |
| 971 | // An optional message describing the current progress. |
| 972 | Message string `json:"message,omitempty"` |
| 973 | // The progress thus far. This should increase every time progress is made, even |
| 974 | // if the total is unknown. |
| 975 | Progress float64 `json:"progress"` |
| 976 | // Total number of items to process (or total progress required), if known. |
| 977 | // Zero means unknown. |
| 978 | Total float64 `json:"total,omitempty"` |
| 979 | } |
| 980 | |
| 981 | func (*ProgressNotificationParams) isParams() {} |
| 982 | |
| 983 | // IconTheme specifies the theme an icon is designed for. |
| 984 | type IconTheme string |
| 985 | |
| 986 | const ( |
| 987 | // IconThemeLight indicates the icon is designed for a light background. |
| 988 | IconThemeLight IconTheme = "light" |
| 989 | // IconThemeDark indicates the icon is designed for a dark background. |
| 990 | IconThemeDark IconTheme = "dark" |
| 991 | ) |
| 992 | |
| 993 | // Icon provides visual identifiers for their resources, tools, prompts, and implementations |
| 994 | // See [/specification/draft/basic/index#icons] for notes on icons |
| 995 | // |
| 996 | // TODO(iamsurajbobade): update specification url from draft. |
| 997 | type Icon struct { |
| 998 | // Source is A URI pointing to the icon resource (required). This can be: |
| 999 | // - An HTTP/HTTPS URL pointing to an image file |
| 1000 | // - A data URI with base64-encoded image data |
| 1001 | Source string `json:"src"` |
| 1002 | // Optional MIME type if the server's type is missing or generic |
| 1003 | MIMEType string `json:"mimeType,omitempty"` |
| 1004 | // Optional size specification (e.g., ["48x48"], ["any"] for scalable formats like SVG, or ["48x48", "96x96"] for multiple sizes) |
| 1005 | Sizes []string `json:"sizes,omitempty"` |
| 1006 | // Optional theme specifier. "light" indicates the icon is designed for a light |
| 1007 | // background, "dark" indicates the icon is designed for a dark background. |
| 1008 | Theme IconTheme `json:"theme,omitempty"` |
| 1009 | } |
| 1010 | |
| 1011 | // A prompt or prompt template that the server offers. |
| 1012 | type Prompt struct { |
| 1013 | // See [specification/2025-06-18/basic/index#general-fields] for notes on _meta |
| 1014 | // usage. |
| 1015 | Meta `json:"_meta,omitempty"` |
| 1016 | // A list of arguments to use for templating the prompt. |
| 1017 | Arguments []*PromptArgument `json:"arguments,omitempty"` |
| 1018 | // An optional description of what this prompt provides |
| 1019 | Description string `json:"description,omitempty"` |
| 1020 | // Intended for programmatic or logical use, but used as a display name in past |
| 1021 | // specs or fallback (if title isn't present). |
| 1022 | Name string `json:"name"` |
| 1023 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1024 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1025 | Title string `json:"title,omitempty"` |
| 1026 | // Icons for the prompt, if any. |
| 1027 | Icons []Icon `json:"icons,omitempty"` |
| 1028 | } |
| 1029 | |
| 1030 | // Describes an argument that a prompt can accept. |
| 1031 | type PromptArgument struct { |
| 1032 | // Intended for programmatic or logical use, but used as a display name in past |
| 1033 | // specs or fallback (if title isn't present). |
| 1034 | Name string `json:"name"` |
| 1035 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1036 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1037 | Title string `json:"title,omitempty"` |
| 1038 | // A human-readable description of the argument. |
| 1039 | Description string `json:"description,omitempty"` |
| 1040 | // Whether this argument must be provided. |
| 1041 | Required bool `json:"required,omitempty"` |
| 1042 | } |
| 1043 | |
| 1044 | type PromptListChangedParams struct { |
| 1045 | // This property is reserved by the protocol to allow clients and servers to |
| 1046 | // attach additional metadata to their responses. |
| 1047 | Meta `json:"_meta,omitempty"` |
| 1048 | } |
| 1049 | |
| 1050 | func (x *PromptListChangedParams) isParams() {} |
| 1051 | func (x *PromptListChangedParams) GetProgressToken() any { return getProgressToken(x) } |
| 1052 | func (x *PromptListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1053 | |
| 1054 | // Describes a message returned as part of a prompt. |
| 1055 | // |
| 1056 | // This is similar to SamplingMessage, but also supports the embedding of |
| 1057 | // resources from the MCP server. |
| 1058 | type PromptMessage struct { |
| 1059 | Content Content `json:"content"` |
| 1060 | Role Role `json:"role"` |
| 1061 | } |
| 1062 | |
| 1063 | // UnmarshalJSON handles the unmarshalling of content into the Content |
| 1064 | // interface. |
| 1065 | func (m *PromptMessage) UnmarshalJSON(data []byte) error { |
| 1066 | type msg PromptMessage // avoid recursion |
| 1067 | var wire struct { |
| 1068 | msg |
| 1069 | Content *wireContent `json:"content"` |
| 1070 | } |
| 1071 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 1072 | return err |
| 1073 | } |
| 1074 | var err error |
| 1075 | if wire.msg.Content, err = contentFromWire(wire.Content, nil); err != nil { |
| 1076 | return err |
| 1077 | } |
| 1078 | *m = PromptMessage(wire.msg) |
| 1079 | return nil |
| 1080 | } |
| 1081 | |
| 1082 | type ReadResourceParams struct { |
| 1083 | // This property is reserved by the protocol to allow clients and servers to |
| 1084 | // attach additional metadata to their responses. |
| 1085 | Meta `json:"_meta,omitempty"` |
| 1086 | // The URI of the resource to read. The URI can use any protocol; it is up to |
| 1087 | // the server how to interpret it. |
| 1088 | URI string `json:"uri"` |
| 1089 | } |
| 1090 | |
| 1091 | func (x *ReadResourceParams) isParams() {} |
| 1092 | func (x *ReadResourceParams) GetProgressToken() any { return getProgressToken(x) } |
| 1093 | func (x *ReadResourceParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1094 | |
| 1095 | // The server's response to a resources/read request from the client. |
| 1096 | type ReadResourceResult struct { |
| 1097 | // This property is reserved by the protocol to allow clients and servers to |
| 1098 | // attach additional metadata to their responses. |
| 1099 | Meta `json:"_meta,omitempty"` |
| 1100 | Contents []*ResourceContents `json:"contents"` |
| 1101 | } |
| 1102 | |
| 1103 | func (*ReadResourceResult) isResult() {} |
| 1104 | |
| 1105 | // A known resource that the server is capable of reading. |
| 1106 | type Resource struct { |
| 1107 | // See [specification/2025-06-18/basic/index#general-fields] for notes on _meta |
| 1108 | // usage. |
| 1109 | Meta `json:"_meta,omitempty"` |
| 1110 | // Optional annotations for the client. |
| 1111 | Annotations *Annotations `json:"annotations,omitempty"` |
| 1112 | // A description of what this resource represents. |
| 1113 | // |
| 1114 | // This can be used by clients to improve the LLM's understanding of available |
| 1115 | // resources. It can be thought of like a "hint" to the model. |
| 1116 | Description string `json:"description,omitempty"` |
| 1117 | // The MIME type of this resource, if known. |
| 1118 | MIMEType string `json:"mimeType,omitempty"` |
| 1119 | // Intended for programmatic or logical use, but used as a display name in past |
| 1120 | // specs or fallback (if title isn't present). |
| 1121 | Name string `json:"name"` |
| 1122 | // The size of the raw resource content, in bytes (i.e., before base64 encoding |
| 1123 | // or any tokenization), if known. |
| 1124 | // |
| 1125 | // This can be used by Hosts to display file sizes and estimate context window |
| 1126 | // usage. |
| 1127 | Size int64 `json:"size,omitempty"` |
| 1128 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1129 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1130 | // |
| 1131 | // If not provided, the name should be used for display (except for Tool, where |
| 1132 | // Annotations.Title should be given precedence over using name, if |
| 1133 | // present). |
| 1134 | Title string `json:"title,omitempty"` |
| 1135 | // The URI of this resource. |
| 1136 | URI string `json:"uri"` |
| 1137 | // Icons for the resource, if any. |
| 1138 | Icons []Icon `json:"icons,omitempty"` |
| 1139 | } |
| 1140 | |
| 1141 | type ResourceListChangedParams struct { |
| 1142 | // This property is reserved by the protocol to allow clients and servers to |
| 1143 | // attach additional metadata to their responses. |
| 1144 | Meta `json:"_meta,omitempty"` |
| 1145 | } |
| 1146 | |
| 1147 | func (x *ResourceListChangedParams) isParams() {} |
| 1148 | func (x *ResourceListChangedParams) GetProgressToken() any { return getProgressToken(x) } |
| 1149 | func (x *ResourceListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1150 | |
| 1151 | // A template description for resources available on the server. |
| 1152 | type ResourceTemplate struct { |
| 1153 | // See [specification/2025-06-18/basic/index#general-fields] for notes on _meta |
| 1154 | // usage. |
| 1155 | Meta `json:"_meta,omitempty"` |
| 1156 | // Optional annotations for the client. |
| 1157 | Annotations *Annotations `json:"annotations,omitempty"` |
| 1158 | // A description of what this template is for. |
| 1159 | // |
| 1160 | // This can be used by clients to improve the LLM's understanding of available |
| 1161 | // resources. It can be thought of like a "hint" to the model. |
| 1162 | Description string `json:"description,omitempty"` |
| 1163 | // The MIME type for all resources that match this template. This should only be |
| 1164 | // included if all resources matching this template have the same type. |
| 1165 | MIMEType string `json:"mimeType,omitempty"` |
| 1166 | // Intended for programmatic or logical use, but used as a display name in past |
| 1167 | // specs or fallback (if title isn't present). |
| 1168 | Name string `json:"name"` |
| 1169 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1170 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1171 | // |
| 1172 | // If not provided, the name should be used for display (except for Tool, where |
| 1173 | // Annotations.Title should be given precedence over using name, if |
| 1174 | // present). |
| 1175 | Title string `json:"title,omitempty"` |
| 1176 | // A URI template (according to RFC 6570) that can be used to construct resource |
| 1177 | // URIs. |
| 1178 | URITemplate string `json:"uriTemplate"` |
| 1179 | // Icons for the resource template, if any. |
| 1180 | Icons []Icon `json:"icons,omitempty"` |
| 1181 | } |
| 1182 | |
| 1183 | // The sender or recipient of messages and data in a conversation. |
| 1184 | type Role string |
| 1185 | |
| 1186 | // Represents a root directory or file that the server can operate on. |
| 1187 | type Root struct { |
| 1188 | // See [specification/2025-06-18/basic/index#general-fields] for notes on _meta |
| 1189 | // usage. |
| 1190 | Meta `json:"_meta,omitempty"` |
| 1191 | // An optional name for the root. This can be used to provide a human-readable |
| 1192 | // identifier for the root, which may be useful for display purposes or for |
| 1193 | // referencing the root in other parts of the application. |
| 1194 | Name string `json:"name,omitempty"` |
| 1195 | // The URI identifying the root. This *must* start with file:// for now. This |
| 1196 | // restriction may be relaxed in future versions of the protocol to allow other |
| 1197 | // URI schemes. |
| 1198 | URI string `json:"uri"` |
| 1199 | } |
| 1200 | |
| 1201 | type RootsListChangedParams struct { |
| 1202 | // This property is reserved by the protocol to allow clients and servers to |
| 1203 | // attach additional metadata to their responses. |
| 1204 | Meta `json:"_meta,omitempty"` |
| 1205 | } |
| 1206 | |
| 1207 | func (x *RootsListChangedParams) isParams() {} |
| 1208 | func (x *RootsListChangedParams) GetProgressToken() any { return getProgressToken(x) } |
| 1209 | func (x *RootsListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1210 | |
| 1211 | // TODO: to be consistent with ServerCapabilities, move the capability types |
| 1212 | // below directly above ClientCapabilities. |
| 1213 | |
| 1214 | // SamplingCapabilities describes the client's support for sampling. |
| 1215 | type SamplingCapabilities struct { |
| 1216 | // Context indicates the client supports includeContext values other than "none". |
| 1217 | Context *SamplingContextCapabilities `json:"context,omitempty"` |
| 1218 | // Tools indicates the client supports tools and toolChoice in sampling requests. |
| 1219 | Tools *SamplingToolsCapabilities `json:"tools,omitempty"` |
| 1220 | } |
| 1221 | |
| 1222 | // SamplingContextCapabilities indicates the client supports context inclusion. |
| 1223 | type SamplingContextCapabilities struct{} |
| 1224 | |
| 1225 | // SamplingToolsCapabilities indicates the client supports tool use in sampling. |
| 1226 | type SamplingToolsCapabilities struct{} |
| 1227 | |
| 1228 | // ToolChoice controls how the model uses tools during sampling. |
| 1229 | type ToolChoice struct { |
| 1230 | // Mode controls tool invocation behavior: |
| 1231 | // - "auto": Model decides whether to use tools (default) |
| 1232 | // - "required": Model must use at least one tool |
| 1233 | // - "none": Model must not use any tools |
| 1234 | Mode string `json:"mode,omitempty"` |
| 1235 | } |
| 1236 | |
| 1237 | // ElicitationCapabilities describes the capabilities for elicitation. |
| 1238 | // |
| 1239 | // If neither Form nor URL is set, the 'Form' capability is assumed. |
| 1240 | type ElicitationCapabilities struct { |
| 1241 | Form *FormElicitationCapabilities `json:"form,omitempty"` |
| 1242 | URL *URLElicitationCapabilities `json:"url,omitempty"` |
| 1243 | } |
| 1244 | |
| 1245 | // FormElicitationCapabilities describes capabilities for form elicitation. |
| 1246 | type FormElicitationCapabilities struct{} |
| 1247 | |
| 1248 | // URLElicitationCapabilities describes capabilities for url elicitation. |
| 1249 | type URLElicitationCapabilities struct{} |
| 1250 | |
| 1251 | // Describes a message issued to or received from an LLM API. |
| 1252 | // |
| 1253 | // For assistant messages, Content may be text, image, audio, or tool_use. |
| 1254 | // For user messages, Content may be text, image, audio, or tool_result. |
| 1255 | type SamplingMessage struct { |
| 1256 | Content Content `json:"content"` |
| 1257 | Role Role `json:"role"` |
| 1258 | } |
| 1259 | |
| 1260 | // UnmarshalJSON handles the unmarshalling of content into the Content |
| 1261 | // interface. |
| 1262 | func (m *SamplingMessage) UnmarshalJSON(data []byte) error { |
| 1263 | type msg SamplingMessage // avoid recursion |
| 1264 | var wire struct { |
| 1265 | msg |
| 1266 | Content *wireContent `json:"content"` |
| 1267 | } |
| 1268 | if err := internaljson.Unmarshal(data, &wire); err != nil { |
| 1269 | return err |
| 1270 | } |
| 1271 | // Allow text, image, audio, tool_use, and tool_result in sampling messages |
| 1272 | var err error |
| 1273 | if wire.msg.Content, err = contentFromWire(wire.Content, map[string]bool{"text": true, "image": true, "audio": true, "tool_use": true, "tool_result": true}); err != nil { |
| 1274 | return err |
| 1275 | } |
| 1276 | *m = SamplingMessage(wire.msg) |
| 1277 | return nil |
| 1278 | } |
| 1279 | |
| 1280 | type SetLoggingLevelParams struct { |
| 1281 | // This property is reserved by the protocol to allow clients and servers to |
| 1282 | // attach additional metadata to their responses. |
| 1283 | Meta `json:"_meta,omitempty"` |
| 1284 | // The level of logging that the client wants to receive from the server. The |
| 1285 | // server should send all logs at this level and higher (i.e., more severe) to |
| 1286 | // the client as notifications/message. |
| 1287 | Level LoggingLevel `json:"level"` |
| 1288 | } |
| 1289 | |
| 1290 | func (x *SetLoggingLevelParams) isParams() {} |
| 1291 | func (x *SetLoggingLevelParams) GetProgressToken() any { return getProgressToken(x) } |
| 1292 | func (x *SetLoggingLevelParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1293 | |
| 1294 | // Definition for a tool the client can call. |
| 1295 | type Tool struct { |
| 1296 | // See [specification/2025-06-18/basic/index#general-fields] for notes on _meta |
| 1297 | // usage. |
| 1298 | Meta `json:"_meta,omitempty"` |
| 1299 | // Optional additional tool information. |
| 1300 | // |
| 1301 | // Display name precedence order is: title, annotations.title, then name. |
| 1302 | Annotations *ToolAnnotations `json:"annotations,omitempty"` |
| 1303 | // A human-readable description of the tool. |
| 1304 | // |
| 1305 | // This can be used by clients to improve the LLM's understanding of available |
| 1306 | // tools. It can be thought of like a "hint" to the model. |
| 1307 | Description string `json:"description,omitempty"` |
| 1308 | // InputSchema holds a JSON Schema object defining the expected parameters |
| 1309 | // for the tool. |
| 1310 | // |
| 1311 | // From the server, this field may be set to any value that JSON-marshals to |
| 1312 | // valid JSON schema (including json.RawMessage). However, for tools added |
| 1313 | // using [AddTool], which automatically validates inputs and outputs, the |
| 1314 | // schema must be in a draft the SDK understands. Currently, the SDK uses |
| 1315 | // github.com/google/jsonschema-go for inference and validation, which only |
| 1316 | // supports the 2020-12 draft of JSON schema. To do your own validation, use |
| 1317 | // [Server.AddTool]. |
| 1318 | // |
| 1319 | // From the client, this field will hold the default JSON marshaling of the |
| 1320 | // server's input schema (a map[string]any). |
| 1321 | InputSchema any `json:"inputSchema"` |
| 1322 | // Intended for programmatic or logical use, but used as a display name in past |
| 1323 | // specs or fallback (if title isn't present). |
| 1324 | Name string `json:"name"` |
| 1325 | // OutputSchema holds an optional JSON Schema object defining the structure |
| 1326 | // of the tool's output returned in the StructuredContent field of a |
| 1327 | // CallToolResult. |
| 1328 | // |
| 1329 | // From the server, this field may be set to any value that JSON-marshals to |
| 1330 | // valid JSON schema (including json.RawMessage). However, for tools added |
| 1331 | // using [AddTool], which automatically validates inputs and outputs, the |
| 1332 | // schema must be in a draft the SDK understands. Currently, the SDK uses |
| 1333 | // github.com/google/jsonschema-go for inference and validation, which only |
| 1334 | // supports the 2020-12 draft of JSON schema. To do your own validation, use |
| 1335 | // [Server.AddTool]. |
| 1336 | // |
| 1337 | // From the client, this field will hold the default JSON marshaling of the |
| 1338 | // server's output schema (a map[string]any). |
| 1339 | OutputSchema any `json:"outputSchema,omitempty"` |
| 1340 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1341 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1342 | // If not provided, Annotations.Title should be used for display if present, |
| 1343 | // otherwise Name. |
| 1344 | Title string `json:"title,omitempty"` |
| 1345 | // Icons for the tool, if any. |
| 1346 | Icons []Icon `json:"icons,omitempty"` |
| 1347 | } |
| 1348 | |
| 1349 | // Additional properties describing a Tool to clients. |
| 1350 | // |
| 1351 | // NOTE: all properties in ToolAnnotations are hints. They are not |
| 1352 | // guaranteed to provide a faithful description of tool behavior (including |
| 1353 | // descriptive properties like title). |
| 1354 | // |
| 1355 | // Clients should never make tool use decisions based on ToolAnnotations |
| 1356 | // received from untrusted servers. |
| 1357 | type ToolAnnotations struct { |
| 1358 | // If true, the tool may perform destructive updates to its environment. If |
| 1359 | // false, the tool performs only additive updates. |
| 1360 | // |
| 1361 | // (This property is meaningful only when ReadOnlyHint == false.) |
| 1362 | // |
| 1363 | // Default: true |
| 1364 | DestructiveHint *bool `json:"destructiveHint,omitempty"` |
| 1365 | // If true, calling the tool repeatedly with the same arguments will have no |
| 1366 | // additional effect on the its environment. |
| 1367 | // |
| 1368 | // (This property is meaningful only when ReadOnlyHint == false.) |
| 1369 | // |
| 1370 | // Default: false |
| 1371 | IdempotentHint bool `json:"idempotentHint,omitempty"` |
| 1372 | // If true, this tool may interact with an "open world" of external entities. If |
| 1373 | // false, the tool's domain of interaction is closed. For example, the world of |
| 1374 | // a web search tool is open, whereas that of a memory tool is not. |
| 1375 | // |
| 1376 | // Default: true |
| 1377 | OpenWorldHint *bool `json:"openWorldHint,omitempty"` |
| 1378 | // If true, the tool does not modify its environment. |
| 1379 | // |
| 1380 | // Default: false |
| 1381 | ReadOnlyHint bool `json:"readOnlyHint,omitempty"` |
| 1382 | // A human-readable title for the tool. |
| 1383 | Title string `json:"title,omitempty"` |
| 1384 | } |
| 1385 | |
| 1386 | type ToolListChangedParams struct { |
| 1387 | // This property is reserved by the protocol to allow clients and servers to |
| 1388 | // attach additional metadata to their responses. |
| 1389 | Meta `json:"_meta,omitempty"` |
| 1390 | } |
| 1391 | |
| 1392 | func (x *ToolListChangedParams) isParams() {} |
| 1393 | func (x *ToolListChangedParams) GetProgressToken() any { return getProgressToken(x) } |
| 1394 | func (x *ToolListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1395 | |
| 1396 | // Sent from the client to request resources/updated notifications from the |
| 1397 | // server whenever a particular resource changes. |
| 1398 | type SubscribeParams struct { |
| 1399 | // This property is reserved by the protocol to allow clients and servers to |
| 1400 | // attach additional metadata to their responses. |
| 1401 | Meta `json:"_meta,omitempty"` |
| 1402 | // The URI of the resource to subscribe to. |
| 1403 | URI string `json:"uri"` |
| 1404 | } |
| 1405 | |
| 1406 | func (*SubscribeParams) isParams() {} |
| 1407 | |
| 1408 | // Sent from the client to request cancellation of resources/updated |
| 1409 | // notifications from the server. This should follow a previous |
| 1410 | // resources/subscribe request. |
| 1411 | type UnsubscribeParams struct { |
| 1412 | // This property is reserved by the protocol to allow clients and servers to |
| 1413 | // attach additional metadata to their responses. |
| 1414 | Meta `json:"_meta,omitempty"` |
| 1415 | // The URI of the resource to unsubscribe from. |
| 1416 | URI string `json:"uri"` |
| 1417 | } |
| 1418 | |
| 1419 | func (*UnsubscribeParams) isParams() {} |
| 1420 | |
| 1421 | // A notification from the server to the client, informing it that a resource |
| 1422 | // has changed and may need to be read again. This should only be sent if the |
| 1423 | // client previously sent a resources/subscribe request. |
| 1424 | type ResourceUpdatedNotificationParams struct { |
| 1425 | // This property is reserved by the protocol to allow clients and servers to |
| 1426 | // attach additional metadata to their responses. |
| 1427 | Meta `json:"_meta,omitempty"` |
| 1428 | // The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to. |
| 1429 | URI string `json:"uri"` |
| 1430 | } |
| 1431 | |
| 1432 | func (*ResourceUpdatedNotificationParams) isParams() {} |
| 1433 | |
| 1434 | // TODO(jba): add CompleteRequest and related types. |
| 1435 | |
| 1436 | // A request from the server to elicit additional information from the user via the client. |
| 1437 | type ElicitParams struct { |
| 1438 | // This property is reserved by the protocol to allow clients and servers to |
| 1439 | // attach additional metadata to their responses. |
| 1440 | Meta `json:"_meta,omitempty"` |
| 1441 | // The mode of elicitation to use. |
| 1442 | // |
| 1443 | // If unset, will be inferred from the other fields. |
| 1444 | Mode string `json:"mode"` |
| 1445 | // The message to present to the user. |
| 1446 | Message string `json:"message"` |
| 1447 | // A JSON schema object defining the requested elicitation schema. |
| 1448 | // |
| 1449 | // From the server, this field may be set to any value that can JSON-marshal |
| 1450 | // to valid JSON schema (including json.RawMessage for raw schema values). |
| 1451 | // Internally, the SDK uses github.com/google/jsonschema-go for validation, |
| 1452 | // which only supports the 2020-12 draft of the JSON schema spec. |
| 1453 | // |
| 1454 | // From the client, this field will use the default JSON marshaling (a |
| 1455 | // map[string]any). |
| 1456 | // |
| 1457 | // Only top-level properties are allowed, without nesting. |
| 1458 | // |
| 1459 | // This is only used for "form" elicitation. |
| 1460 | RequestedSchema any `json:"requestedSchema,omitempty"` |
| 1461 | // The URL to present to the user. |
| 1462 | // |
| 1463 | // This is only used for "url" elicitation. |
| 1464 | URL string `json:"url,omitempty"` |
| 1465 | // The ID of the elicitation. |
| 1466 | // |
| 1467 | // This is only used for "url" elicitation. |
| 1468 | ElicitationID string `json:"elicitationId,omitempty"` |
| 1469 | } |
| 1470 | |
| 1471 | func (x *ElicitParams) isParams() {} |
| 1472 | |
| 1473 | func (x *ElicitParams) GetProgressToken() any { return getProgressToken(x) } |
| 1474 | func (x *ElicitParams) SetProgressToken(t any) { setProgressToken(x, t) } |
| 1475 | |
| 1476 | // The client's response to an elicitation/create request from the server. |
| 1477 | type ElicitResult struct { |
| 1478 | // This property is reserved by the protocol to allow clients and servers to |
| 1479 | // attach additional metadata to their responses. |
| 1480 | Meta `json:"_meta,omitempty"` |
| 1481 | // The user action in response to the elicitation. |
| 1482 | // - "accept": User submitted the form/confirmed the action |
| 1483 | // - "decline": User explicitly declined the action |
| 1484 | // - "cancel": User dismissed without making an explicit choice |
| 1485 | Action string `json:"action"` |
| 1486 | // The submitted form data, only present when action is "accept". |
| 1487 | // Contains values matching the requested schema. |
| 1488 | Content map[string]any `json:"content,omitempty"` |
| 1489 | } |
| 1490 | |
| 1491 | func (*ElicitResult) isResult() {} |
| 1492 | |
| 1493 | // ElicitationCompleteParams is sent from the server to the client, informing it that an out-of-band elicitation interaction has completed. |
| 1494 | type ElicitationCompleteParams struct { |
| 1495 | // This property is reserved by the protocol to allow clients and servers to |
| 1496 | // attach additional metadata to their responses. |
| 1497 | Meta `json:"_meta,omitempty"` |
| 1498 | // The ID of the elicitation that has completed. This must correspond to the |
| 1499 | // elicitationId from the original elicitation/create request. |
| 1500 | ElicitationID string `json:"elicitationId"` |
| 1501 | } |
| 1502 | |
| 1503 | func (*ElicitationCompleteParams) isParams() {} |
| 1504 | |
| 1505 | // An Implementation describes the name and version of an MCP implementation, with an optional |
| 1506 | // title for UI representation. |
| 1507 | type Implementation struct { |
| 1508 | // Intended for programmatic or logical use, but used as a display name in past |
| 1509 | // specs or fallback (if title isn't present). |
| 1510 | Name string `json:"name"` |
| 1511 | // Intended for UI and end-user contexts — optimized to be human-readable and |
| 1512 | // easily understood, even by those unfamiliar with domain-specific terminology. |
| 1513 | Title string `json:"title,omitempty"` |
| 1514 | Version string `json:"version"` |
| 1515 | // WebsiteURL for the server, if any. |
| 1516 | WebsiteURL string `json:"websiteUrl,omitempty"` |
| 1517 | // Icons for the Server, if any. |
| 1518 | Icons []Icon `json:"icons,omitempty"` |
| 1519 | } |
| 1520 | |
| 1521 | // CompletionCapabilities describes the server's support for argument autocompletion. |
| 1522 | type CompletionCapabilities struct{} |
| 1523 | |
| 1524 | // LoggingCapabilities describes the server's support for sending log messages to the client. |
| 1525 | type LoggingCapabilities struct{} |
| 1526 | |
| 1527 | // PromptCapabilities describes the server's support for prompts. |
| 1528 | type PromptCapabilities struct { |
| 1529 | // Whether this server supports notifications for changes to the prompt list. |
| 1530 | ListChanged bool `json:"listChanged,omitempty"` |
| 1531 | } |
| 1532 | |
| 1533 | // ResourceCapabilities describes the server's support for resources. |
| 1534 | type ResourceCapabilities struct { |
| 1535 | // ListChanged reports whether the client supports notifications for |
| 1536 | // changes to the resource list. |
| 1537 | ListChanged bool `json:"listChanged,omitempty"` |
| 1538 | // Subscribe reports whether this server supports subscribing to resource |
| 1539 | // updates. |
| 1540 | Subscribe bool `json:"subscribe,omitempty"` |
| 1541 | } |
| 1542 | |
| 1543 | // ToolCapabilities describes the server's support for tools. |
| 1544 | type ToolCapabilities struct { |
| 1545 | // ListChanged reports whether the client supports notifications for |
| 1546 | // changes to the tool list. |
| 1547 | ListChanged bool `json:"listChanged,omitempty"` |
| 1548 | } |
| 1549 | |
| 1550 | // ServerCapabilities describes capabilities that a server supports. |
| 1551 | type ServerCapabilities struct { |
| 1552 | // NOTE: any addition to ServerCapabilities must also be reflected in |
| 1553 | // [ServerCapabilities.clone]. |
| 1554 | |
| 1555 | // Experimental reports non-standard capabilities that the server supports. |
| 1556 | // The caller should not modify the map after assigning it. |
| 1557 | Experimental map[string]any `json:"experimental,omitempty"` |
| 1558 | // Extensions reports extensions that the server supports. |
| 1559 | // Keys are extension identifiers in "{vendor-prefix}/{extension-name}" format. |
| 1560 | // Values are per-extension settings objects; use [ServerCapabilities.AddExtension] |
| 1561 | // to ensure nil settings are normalized to empty objects. |
| 1562 | // The caller should not modify the map or its values after assigning it. |
| 1563 | Extensions map[string]any `json:"extensions,omitempty"` |
| 1564 | // Completions is present if the server supports argument autocompletion |
| 1565 | // suggestions. |
| 1566 | Completions *CompletionCapabilities `json:"completions,omitempty"` |
| 1567 | // Logging is present if the server supports log messages. |
| 1568 | Logging *LoggingCapabilities `json:"logging,omitempty"` |
| 1569 | // Prompts is present if the server supports prompts. |
| 1570 | Prompts *PromptCapabilities `json:"prompts,omitempty"` |
| 1571 | // Resources is present if the server supports resources. |
| 1572 | Resources *ResourceCapabilities `json:"resources,omitempty"` |
| 1573 | // Tools is present if the supports tools. |
| 1574 | Tools *ToolCapabilities `json:"tools,omitempty"` |
| 1575 | } |
| 1576 | |
| 1577 | // AddExtension adds an extension with the given name and settings. |
| 1578 | // If settings is nil, an empty map is used to ensure valid JSON serialization |
| 1579 | // (the spec requires an object, not null). |
| 1580 | // The settings map should not be modified after the call. |
| 1581 | func (c *ServerCapabilities) AddExtension(name string, settings map[string]any) { |
| 1582 | if c.Extensions == nil { |
| 1583 | c.Extensions = make(map[string]any) |
| 1584 | } |
| 1585 | if settings == nil { |
| 1586 | settings = map[string]any{} |
| 1587 | } |
| 1588 | c.Extensions[name] = settings |
| 1589 | } |
| 1590 | |
| 1591 | // clone returns a copy of the ServerCapabilities. |
| 1592 | // Values in the Extensions and Experimental maps are shallow-copied. |
| 1593 | func (c *ServerCapabilities) clone() *ServerCapabilities { |
| 1594 | cp := *c |
| 1595 | cp.Experimental = maps.Clone(c.Experimental) |
| 1596 | cp.Extensions = maps.Clone(c.Extensions) |
| 1597 | cp.Completions = shallowClone(c.Completions) |
| 1598 | cp.Logging = shallowClone(c.Logging) |
| 1599 | cp.Prompts = shallowClone(c.Prompts) |
| 1600 | cp.Resources = shallowClone(c.Resources) |
| 1601 | cp.Tools = shallowClone(c.Tools) |
| 1602 | return &cp |
| 1603 | } |
| 1604 | |
| 1605 | const ( |
| 1606 | methodCallTool = "tools/call" |
| 1607 | notificationCancelled = "notifications/cancelled" |
| 1608 | methodComplete = "completion/complete" |
| 1609 | methodCreateMessage = "sampling/createMessage" |
| 1610 | methodElicit = "elicitation/create" |
| 1611 | notificationElicitationComplete = "notifications/elicitation/complete" |
| 1612 | methodGetPrompt = "prompts/get" |
| 1613 | methodInitialize = "initialize" |
| 1614 | notificationInitialized = "notifications/initialized" |
| 1615 | methodListPrompts = "prompts/list" |
| 1616 | methodListResourceTemplates = "resources/templates/list" |
| 1617 | methodListResources = "resources/list" |
| 1618 | methodListRoots = "roots/list" |
| 1619 | methodListTools = "tools/list" |
| 1620 | notificationLoggingMessage = "notifications/message" |
| 1621 | methodPing = "ping" |
| 1622 | notificationProgress = "notifications/progress" |
| 1623 | notificationPromptListChanged = "notifications/prompts/list_changed" |
| 1624 | methodReadResource = "resources/read" |
| 1625 | notificationResourceListChanged = "notifications/resources/list_changed" |
| 1626 | notificationResourceUpdated = "notifications/resources/updated" |
| 1627 | notificationRootsListChanged = "notifications/roots/list_changed" |
| 1628 | methodSetLevel = "logging/setLevel" |
| 1629 | methodSubscribe = "resources/subscribe" |
| 1630 | notificationToolListChanged = "notifications/tools/list_changed" |
| 1631 | methodUnsubscribe = "resources/unsubscribe" |
| 1632 | ) |