Files
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2bd709db63 build(deps): bump github.com/mark3labs/mcp-go from 0.37.0 to 0.43.0 (#5844)
Bumps [github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go) from 0.37.0 to 0.43.0.
- [Release notes](https://github.com/mark3labs/mcp-go/releases)
- [Commits](https://github.com/mark3labs/mcp-go/compare/v0.37.0...v0.43.0)

---
updated-dependencies:
- dependency-name: github.com/mark3labs/mcp-go
  dependency-version: 0.43.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-11-05 08:17:30 +00:00

62 lines
2.0 KiB
Go

package server
import (
"context"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
)
// EnableSampling enables sampling capabilities for the server.
// This allows the server to send sampling requests to clients that support it.
func (s *MCPServer) EnableSampling() {
s.capabilitiesMu.Lock()
defer s.capabilitiesMu.Unlock()
enabled := true
s.capabilities.sampling = &enabled
}
// RequestSampling sends a sampling request to the client.
// The client must have declared sampling capability during initialization.
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")
}
// Check if the session supports sampling requests
if samplingSession, ok := session.(SessionWithSampling); ok {
return samplingSession.RequestSampling(ctx, request)
}
// Check for inprocess sampling handler in context
if handler := InProcessSamplingHandlerFromContext(ctx); handler != nil {
return handler.CreateMessage(ctx, request)
}
return nil, fmt.Errorf("session does not support sampling")
}
// SessionWithSampling extends ClientSession to support sampling requests.
type SessionWithSampling interface {
ClientSession
RequestSampling(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error)
}
// inProcessSamplingHandlerKey is the context key for storing inprocess sampling handler
type inProcessSamplingHandlerKey struct{}
// WithInProcessSamplingHandler adds a sampling handler to the context for inprocess clients
func WithInProcessSamplingHandler(ctx context.Context, handler SamplingHandler) context.Context {
return context.WithValue(ctx, inProcessSamplingHandlerKey{}, handler)
}
// InProcessSamplingHandlerFromContext retrieves the inprocess sampling handler from context
func InProcessSamplingHandlerFromContext(ctx context.Context) SamplingHandler {
if handler, ok := ctx.Value(inProcessSamplingHandlerKey{}).(SamplingHandler); ok {
return handler
}
return nil
}