Skip to content

Commit 22024f4

Browse files
committed
docs
1 parent 4d84bc2 commit 22024f4

File tree

5 files changed

+451
-31
lines changed

5 files changed

+451
-31
lines changed
345 Bytes
Binary file not shown.

www/docs/pages/clients/advanced-sampling.mdx

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

384383
The 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

www/docs/pages/servers/advanced-sampling.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,48 @@ func getTextFromContent(content interface{}) string {
352352
}
353353
```
354354

355+
## Transport Support
356+
357+
Sampling is supported on the following transports:
358+
359+
### STDIO Transport
360+
361+
STDIO transport provides full sampling support with JSON-RPC message passing:
362+
363+
```go
364+
// Start STDIO server with sampling
365+
server.ServeStdio(mcpServer)
366+
```
367+
368+
The client must implement a `SamplingHandler` and declare sampling capability during initialization.
369+
370+
### In-Process Transport
371+
372+
In-process transport offers the most efficient sampling implementation with direct method calls:
373+
374+
```go
375+
// Create in-process client with sampling handler
376+
mcpClient, err := client.NewInProcessClientWithSamplingHandler(mcpServer, samplingHandler)
377+
if err != nil {
378+
log.Fatal(err)
379+
}
380+
```
381+
382+
**Benefits of in-process sampling:**
383+
- **Direct Method Calls**: No JSON-RPC serialization overhead
384+
- **Type Safety**: Compile-time type checking
385+
386+
### Unsupported Transports
387+
388+
The following transports do not currently support sampling:
389+
- **SSE Transport**: One-way streaming nature prevents bidirectional sampling
390+
- **StreamableHTTP Transport**: Stateless HTTP requests don't support sampling callbacks
391+
392+
For these transports, consider implementing LLM integration directly in your tool handlers rather than using sampling.
393+
355394
## Next Steps
356395

357396
- Learn about [client-side sampling implementation](/clients/advanced-sampling)
358397
- Explore [advanced server features](/servers/advanced)
359-
- Check out the [sampling examples](https://github.com/mark3labs/mcp-go/tree/main/examples/sampling_server)
398+
- Check out the [sampling examples](https://github.com/mark3labs/mcp-go/tree/main/examples/sampling_server)
399+
- See [in-process sampling documentation](/transports/inprocess#sampling-support) for embedded scenarios

www/docs/pages/transports/index.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Transport layers handle the communication between MCP clients and servers. Each
1313

1414
## Transport Comparison
1515

16-
| Transport | Use Case | Pros | Cons |
17-
|-----------|----------|------|------|
18-
| **STDIO** | CLI tools, desktop apps | Simple, secure, no network | Single client, local only |
19-
| **SSE** | Web apps, real-time | Multi-client, real-time, web-friendly | HTTP overhead, one-way streaming |
20-
| **StreamableHTTP** | Web services, APIs | Standard protocol, caching, load balancing | No real-time, more complex |
21-
| **In-Process** | Embedded, testing | No serialization, fastest | Same process only |
16+
| Transport | Use Case | Pros | Cons | Sampling Support |
17+
|-----------|----------|------|------|------------------|
18+
| **STDIO** | CLI tools, desktop apps | Simple, secure, no network | Single client, local only | ✅ Full support |
19+
| **SSE** | Web apps, real-time | Multi-client, real-time, web-friendly | HTTP overhead, one-way streaming | ❌ Not supported |
20+
| **StreamableHTTP** | Web services, APIs | Standard protocol, caching, load balancing | No real-time, more complex | ❌ Not supported |
21+
| **In-Process** | Embedded, testing | No serialization, fastest | Same process only | ✅ Full support |
2222

2323
## Quick Example
2424

@@ -131,12 +131,14 @@ func handleEcho(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResu
131131
- Testing and development
132132
- High-performance scenarios
133133
- Library integrations
134+
- Sampling-enabled applications
134135

135136
**Example use cases:**
136137
- Testing MCP implementations
137138
- Embedded analytics engines
138139
- High-frequency trading systems
139140
- Real-time game servers
141+
- LLM-powered applications with bidirectional communication
140142

141143
## Transport Configuration
142144

0 commit comments

Comments
 (0)