package server
import (
"context"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
)
func (s *MCPServer ) EnableSampling () {
s .capabilitiesMu .Lock ()
defer s .capabilitiesMu .Unlock ()
enabled := true
s .capabilities .sampling = &enabled
}
func (s *MCPServer ) RequestSampling (ctx context .Context , request mcp .CreateMessageRequest ) (*mcp .CreateMessageResult , error ) {
session := ClientSessionFromContext (ctx )
if session == nil {
return nil , fmt .Errorf ("no active session" )
}
if samplingSession , ok := session .(SessionWithSampling ); ok {
return samplingSession .RequestSampling (ctx , request )
}
if handler := InProcessSamplingHandlerFromContext (ctx ); handler != nil {
return handler .CreateMessage (ctx , request )
}
return nil , fmt .Errorf ("session does not support sampling" )
}
type SessionWithSampling interface {
ClientSession
RequestSampling (ctx context .Context , request mcp .CreateMessageRequest ) (*mcp .CreateMessageResult , error )
}
type inProcessSamplingHandlerKey struct {}
func WithInProcessSamplingHandler (ctx context .Context , handler SamplingHandler ) context .Context {
return context .WithValue (ctx , inProcessSamplingHandlerKey {}, handler )
}
func InProcessSamplingHandlerFromContext (ctx context .Context ) SamplingHandler {
if handler , ok := ctx .Value (inProcessSamplingHandlerKey {}).(SamplingHandler ); ok {
return handler
}
return nil
}
The pages are generated with Golds v0.8.4 . (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 .