@@ -18,6 +18,7 @@ import (
1818 " fmt"
1919
2020 " github.com/mark3labs/mcp-go/client"
21+ " github.com/mark3labs/mcp-go/client/transport"
2122 " github.com/mark3labs/mcp-go/mcp"
2223)
2324
@@ -66,18 +67,18 @@ func main() {
6667 // Create sampling handler
6768 samplingHandler := &MySamplingHandler{}
6869
70+ // Create stdio transport
71+ stdioTransport := transport.NewStdio (" /path/to/mcp/server" , nil )
72+
6973 // Create client with sampling support
70- mcpClient , err := client.NewStdioClient (
71- " /path/to/mcp/server" ,
72- client.WithSamplingHandler (samplingHandler),
73- )
74- if err != nil {
75- log.Fatalf (" Failed to create client: %v " , err)
76- }
77- defer mcpClient.Close ()
74+ mcpClient := client.NewClient (stdioTransport, client.WithSamplingHandler (samplingHandler))
7875
79- // Connect to server
76+ // Start the client
8077 ctx := context.Background ()
78+ if err := mcpClient.Start (ctx); err != nil {
79+ log.Fatalf (" Failed to start client: %v " , err)
80+ }
81+ defer mcpClient.Close ()
8182 if err := mcpClient.Connect (ctx); err != nil {
8283 log.Fatalf (" Failed to connect: %v " , err)
8384 }
@@ -150,18 +151,18 @@ func main() {
150151
151152 serverPath := os.Args [1 ]
152153
154+ // Create stdio transport
155+ stdioTransport := transport.NewStdio (serverPath, nil )
156+
153157 // Create client with mock sampling handler
154- mcpClient , err := client.NewStdioClient (
155- serverPath,
156- client.WithSamplingHandler (&MockSamplingHandler{}),
157- )
158- if err != nil {
159- log.Fatalf (" Failed to create client: %v " , err)
160- }
161- defer mcpClient.Close ()
158+ mcpClient := client.NewClient (stdioTransport, client.WithSamplingHandler (&MockSamplingHandler{}))
162159
163- // Connect and test
160+ // Start the client
164161 ctx := context.Background ()
162+ if err := mcpClient.Start (ctx); err != nil {
163+ log.Fatalf (" Failed to start client: %v " , err)
164+ }
165+ defer mcpClient.Close ()
165166 if err := mcpClient.Connect (ctx); err != nil {
166167 log.Fatalf (" Failed to connect: %v " , err)
167168 }
@@ -375,10 +376,8 @@ When you provide a sampling handler, the client automatically declares the sampl
375376
376377``` go
377378// This automatically adds sampling capability
378- mcpClient , err := client.NewStdioClient (
379- serverPath,
380- client.WithSamplingHandler (handler), // Enables sampling capability
381- )
379+ stdioTransport := transport.NewStdio (serverPath, nil )
380+ mcpClient := client.NewClient (stdioTransport, client.WithSamplingHandler (handler))
382381```
383382
384383The client will include this in the initialization request:
@@ -460,8 +459,39 @@ go build -o my_client
460459./my_client ./sampling_server
461460```
462461
462+ ## Transport Support
463+
464+ Sampling is available on the following transports:
465+
466+ ### STDIO Transport
467+
468+ For STDIO clients, create the transport and client separately:
469+
470+ ``` go
471+ stdioTransport := transport.NewStdio (" /path/to/server" , nil )
472+ mcpClient := client.NewClient (stdioTransport, client.WithSamplingHandler (&MySamplingHandler{}))
473+ ```
474+
475+ ### In-Process Transport
476+
477+ For in-process clients, use the dedicated constructor:
478+
479+ ``` go
480+ mcpClient , err := client.NewInProcessClientWithSamplingHandler (
481+ mcpServer,
482+ &MySamplingHandler{},
483+ )
484+ ```
485+
486+ In-process sampling uses direct method calls instead of JSON-RPC serialization.
487+
488+ ### Unsupported Transports
489+
490+ SSE and StreamableHTTP transports do not support sampling due to their one-way or stateless nature.
491+
463492## Next Steps
464493
465494- Learn about [ server-side sampling implementation] ( /servers/advanced-sampling )
466495- Explore [ client operations] ( /clients/operations )
467- - Check out the [ sampling examples] ( https://github.com/mark3labs/mcp-go/tree/main/examples/sampling_client )
496+ - Check out the [ sampling examples] ( https://github.com/mark3labs/mcp-go/tree/main/examples/sampling_client )
497+ - See [ in-process transport documentation] ( /transports/inprocess ) for embedded scenarios
0 commit comments