sampling_http_client

command
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

HTTP Sampling Client Example

This example demonstrates how to create an MCP client using HTTP transport that supports sampling requests from the server.

Overview

This client:

  • Connects to an MCP server via HTTP/HTTPS transport
  • Declares sampling capability during initialization
  • Handles incoming sampling requests from the server
  • Uses a mock LLM to generate responses (replace with real LLM integration)

Usage

  1. Start an MCP server that supports sampling (e.g., using the sampling_server example)

  2. Update the server URL in main.go:

    httpClient, err := client.NewStreamableHttpClient(
        "http://your-server:port", // Replace with your server URL
    )
    
  3. Run the client:

    go run main.go
    

Key Features

HTTP Transport with Sampling

The client creates the HTTP transport directly and then wraps it with a client that supports sampling:

httpTransport, err := transport.NewStreamableHTTP("http://localhost:8080")
mcpClient := client.NewClient(httpTransport, client.WithSamplingHandler(samplingHandler))
Sampling Handler

The MockSamplingHandler implements the client.SamplingHandler interface:

type MockSamplingHandler struct{}

func (h *MockSamplingHandler) CreateMessage(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) {
    // Process the sampling request and return LLM response
    // In production, integrate with OpenAI, Anthropic, or other LLM APIs
}
Client Configuration

The client is configured with sampling capabilities:

mcpClient := client.NewClient(
    httpTransport,
    client.WithSamplingHandler(samplingHandler),
)
// Sampling capability is automatically declared when a handler is provided

Real Implementation

For a production implementation, replace the MockSamplingHandler with a real LLM client:

type RealSamplingHandler struct {
    client *openai.Client // or other LLM client
}

func (h *RealSamplingHandler) CreateMessage(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) {
    // Convert MCP request to LLM API format
    // Call LLM API
    // Convert response back to MCP format
    // Return the result
}

HTTP-Specific Features

The HTTP transport supports:

  • Standard HTTP headers for authentication and customization
  • OAuth 2.0 authentication (using WithHTTPOAuth)
  • Custom headers (using WithHTTPHeaders)
  • Server-side events (SSE) for bidirectional communication
  • Proper error handling with HTTP status codes
  • Session management via HTTP headers

Testing

The implementation includes comprehensive tests in client/transport/streamable_http_sampling_test.go that verify:

  • Sampling request handling
  • Error scenarios
  • Bidirectional interface compliance
  • HTTP-specific error codes and responses

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL