Handle Sampling Requests with SamplingHandler
mainThe Client can act as a host for an LLM by implementing the SamplingHandler protocol. When an MCP server sends a sampling/createMessage request, the client uses the provided samplingHandler to generate a response.
To implement sampling, conform to SamplingHandler and pass your implementation to the Client initializer.
public protocol SamplingHandler: Sendable {
func sample(_ request: CreateMessageRequest) async throws -> CreateMessageResponse.Result
}struct MySamplingHandler: SamplingHandler {
func sample(_ request: CreateMessageRequest) async throws -> CreateMessageResponse.Result {
// Logic to call an LLM and return a result
return .init(role: .assistant, content: [.text("Hello from the client!")], model: "gpt-4")
}
}
let client = Client(transport: myTransport, samplingHandler: MySamplingHandler())