| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
"log" |
| 12 |
"slices" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func ExampleLoggingTransport() { |
| 21 |
ctx := context.Background() |
| 22 |
t1, t2 := mcp.NewInMemoryTransports() |
| 23 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 24 |
serverSession, err := server.Connect(ctx, t1, nil) |
| 25 |
if err != nil { |
| 26 |
log.Fatal(err) |
| 27 |
} |
| 28 |
defer serverSession.Close() |
| 29 |
|
| 30 |
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil) |
| 31 |
var b bytes.Buffer |
| 32 |
logTransport := &mcp.LoggingTransport{Transport: t2, Writer: &b} |
| 33 |
clientSession, err := client.Connect(ctx, logTransport, nil) |
| 34 |
if err != nil { |
| 35 |
log.Fatal(err) |
| 36 |
} |
| 37 |
defer clientSession.Close() |
| 38 |
|
| 39 |
|
| 40 |
for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) { |
| 41 |
fmt.Println(line) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|