package mcpimport ()// Multi round-trip requests (MRTR), introduced in protocol version 2026-07-28// (SEP-2322), replace server-initiated elicitation/create,// sampling/createMessage, and roots/list requests.//// Instead of pushing a request down a held-open stream, a server returns an// [InputRequiredResult]: a result whose resultType is// [ResultTypeInputRequired] and which carries the requests it needs answered.// The client fulfils them and retries the original request with the answers in// inputResponses, echoing the opaque requestState back verbatim.// InputRequest is a single server-initiated request embedded in an// [InputRequiredResult]. Exactly one of its fields is populated, selected by// the Method field.typeInputRequeststruct {// Method is the JSON-RPC method of the embedded request: one of // [MethodElicitationCreate], [MethodSamplingCreateMessage], or // [MethodListRoots]. Method MCPMethod`json:"method"`// Elicitation is set when Method is elicitation/create. Elicitation *ElicitationParams`json:"-"`// Sampling is set when Method is sampling/createMessage. Sampling *CreateMessageParams`json:"-"`// Roots is set when Method is roots/list. The roots/list request takes no // parameters, so this is an empty struct used only as a presence marker. Roots *ListRootsParams`json:"-"`}// ListRootsParams are the (empty) parameters of a roots/list request. It// exists so that [InputRequest] can represent a roots/list request uniformly.typeListRootsParamsstruct{}// NewElicitationInputRequest builds an input request asking the client to// elicit information from the user.func ( ElicitationParams) InputRequest {returnInputRequest{Method: MethodElicitationCreate, Elicitation: &}}// NewSamplingInputRequest builds an input request asking the client to sample// from an LLM.func ( CreateMessageParams) InputRequest {returnInputRequest{Method: MethodSamplingCreateMessage, Sampling: &}}// NewRootsInputRequest builds an input request asking the client for its list// of roots.func () InputRequest {returnInputRequest{Method: MethodListRoots, Roots: &ListRootsParams{}}}// MarshalJSON encodes the input request as a JSON-RPC request object carrying// the method and its parameters.func ( InputRequest) () ([]byte, error) { := map[string]any{"method": .Method}switch .Method {caseMethodElicitationCreate:if .Elicitation == nil {returnnil, fmt.Errorf("input request %s: missing elicitation params", .Method) } ["params"] = .ElicitationcaseMethodSamplingCreateMessage:if .Sampling == nil {returnnil, fmt.Errorf("input request %s: missing sampling params", .Method) } ["params"] = .SamplingcaseMethodListRoots:// roots/list takes no parameters.default:returnnil, fmt.Errorf("unsupported input request method %q", .Method) }returnjson.Marshal()}// UnmarshalJSON decodes a JSON-RPC request object into the matching variant.func ( *InputRequest) ( []byte) error {varstruct {MCPMethod`json:"method"`json.RawMessage`json:"params"` }if := json.Unmarshal(, &); != nil {return } * = InputRequest{Method: .}switch . {caseMethodElicitationCreate:varElicitationParamsiflen(.) > 0 {if := json.Unmarshal(., &); != nil {returnfmt.Errorf("input request %s: %w", ., ) } } .Elicitation = &caseMethodSamplingCreateMessage:varCreateMessageParamsiflen(.) > 0 {if := json.Unmarshal(., &); != nil {returnfmt.Errorf("input request %s: %w", ., ) } } .Sampling = &caseMethodListRoots: .Roots = &ListRootsParams{}default:returnfmt.Errorf("unsupported input request method %q", .) }returnnil}// InputResponse is a client's answer to a single [InputRequest]. Exactly one// of its fields is populated, matching the method of the request it answers.typeInputResponsestruct {// Elicitation answers an elicitation/create input request. Elicitation *ElicitationResult`json:"-"`// Sampling answers a sampling/createMessage input request. Sampling *CreateMessageResult`json:"-"`// Roots answers a roots/list input request. Roots *ListRootsResult`json:"-"`// raw preserves the original JSON so that a response can be round-tripped // and decoded against the method of the request it answers. raw json.RawMessage}// NewElicitationInputResponse builds an input response carrying an elicitation// result.func ( ElicitationResult) InputResponse {returnInputResponse{Elicitation: &}}// NewSamplingInputResponse builds an input response carrying a sampling// result.func ( CreateMessageResult) InputResponse {returnInputResponse{Sampling: &}}// NewRootsInputResponse builds an input response carrying a roots list.func ( ListRootsResult) InputResponse {returnInputResponse{Roots: &}}// MarshalJSON encodes whichever variant is populated.func ( InputResponse) () ([]byte, error) {switch {case .Elicitation != nil:returnjson.Marshal(.Elicitation)case .Sampling != nil:returnjson.Marshal(.Sampling)case .Roots != nil:returnjson.Marshal(.Roots)caselen(.raw) > 0:return .raw, nildefault:return []byte("null"), nil }}// UnmarshalJSON stores the raw payload. Input responses are a union whose// variant is determined by the method of the corresponding [InputRequest],// which is not present in the response itself, so decoding into a concrete// type is deferred to [InputResponse.DecodeFor].func ( *InputResponse) ( []byte) error { * = InputResponse{raw: append(json.RawMessage(nil), ...)}returnnil}// DecodeFor resolves the response against the method of the request it// answers, populating the matching variant.func ( *InputResponse) ( MCPMethod) error {iflen(.raw) == 0 {returnnil }switch {caseMethodElicitationCreate:varElicitationResultif := json.Unmarshal(.raw, &); != nil {returnfmt.Errorf("input response %s: %w", , ) } .Elicitation = &caseMethodSamplingCreateMessage:varCreateMessageResultif := json.Unmarshal(.raw, &); != nil {returnfmt.Errorf("input response %s: %w", , ) } .Sampling = &caseMethodListRoots:varListRootsResultif := json.Unmarshal(.raw, &); != nil {returnfmt.Errorf("input response %s: %w", , ) } .Roots = &default:returnfmt.Errorf("unsupported input response method %q", ) }returnnil}// InputRequests maps server-assigned identifiers to the requests a client must// fulfil before retrying the original call.typeInputRequestsmap[string]InputRequest// InputResponses maps the identifiers from an [InputRequests] map to the// client's answers.typeInputResponsesmap[string]InputResponse// DecodeFor resolves every response in the map against the methods of the// corresponding requests.func ( InputResponses) ( InputRequests) error {for , := range { , := []if ! {continue }if := .DecodeFor(.Method); != nil {returnfmt.Errorf("input response %q: %w", , ) } [] = }returnnil}// MultiRoundTripResult carries the multi round-trip fields on results that may// ask the client for more input before completing. It is embedded in// [CallToolResult], [GetPromptResult], and [ReadResourceResult].typeMultiRoundTripResultstruct {// InputRequests are the requests the client must fulfil before retrying. // Present only when ResultType is [ResultTypeInputRequired]. InputRequests InputRequests`json:"inputRequests,omitempty"`// RequestState is an opaque token the client must pass back verbatim when // it retries the original request. Clients MUST NOT interpret it. RequestState string`json:"requestState,omitempty"`}// InputRequiredResult is returned by a server to indicate that it needs// additional input before it can complete a request.//// At least one of InputRequests or RequestState must be present. An empty// InputRequests map with a RequestState is a load-shedding signal: the client// should retry the request unchanged, echoing the state back.typeInputRequiredResultstruct {ResultMultiRoundTripResult}// NewInputRequiredResult builds a result asking the client for more input// before the request can be completed. requestState is echoed back by the// client on retry and may be empty.func ( InputRequests, string) *InputRequiredResult {return &InputRequiredResult{Result: Result{ResultType: ResultTypeInputRequired},MultiRoundTripResult: MultiRoundTripResult{InputRequests: ,RequestState: , }, }}// MultiRoundTripParams carries the client's answers to a previous// [InputRequiredResult] on a retry of the original request. It is embedded in// the params of every request that may take part in a multi round-trip// exchange.typeMultiRoundTripParamsstruct {// InputResponses answers the requests from a previous // [InputRequiredResult]. Each key present in that result's InputRequests // map must appear here. InputResponses InputResponses`json:"inputResponses,omitempty"`// RequestState echoes back, verbatim, the opaque state from a previous // [InputRequiredResult]. RequestState string`json:"requestState,omitempty"`}// NeedsInput reports whether the result asks the client for more input before// the original request can complete.func ( *InputRequiredResult) () bool {return != nil && .ResultType == ResultTypeInputRequired}// NeedsInput reports whether the result asks the client for more input before// the original request can complete.func ( *CallToolResult) () bool {return != nil && .ResultType == ResultTypeInputRequired}// NeedsInput reports whether the result asks the client for more input before// the original request can complete.func ( *GetPromptResult) () bool {return != nil && .ResultType == ResultTypeInputRequired}// NeedsInput reports whether the result asks the client for more input before// the original request can complete.func ( *ReadResourceResult) () bool {return != nil && .ResultType == ResultTypeInputRequired}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.