Skip to content

Commit fd9d5b9

Browse files
committed
reviewer comments
1 parent c890482 commit fd9d5b9

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

docs/server.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ _ = mcp.NewServer(&mcp.Implementation{Name: "server"}, &mcp.ServerOptions{
158158

159159
### Logging
160160

161-
MCP servers can send logging messages to MCP clients so their users can keep informed of progress.
161+
MCP servers can send logging messages to MCP clients.
162162
(This form of logging is distinct from server-side logging, where the
163163
server produces logs that remain server-side, for use by server maintainers.)
164164

@@ -180,7 +180,6 @@ Servers always report the logging capability.
180180

181181

182182
**Client-side**:
183-
184183
Set [`ClientOptions.LoggingMessageHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.LoggingMessageHandler) to receive log messages.
185184

186185
Call [`ClientSession.SetLevel`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.SetLevel) to change the log level for a session.
@@ -193,12 +192,17 @@ func Example_logging() {
193192
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
194193

195194
// Create a client that displays log messages.
195+
done := make(chan struct{}) // solely for the example
196+
var nmsgs atomic.Int32
196197
c := mcp.NewClient(
197198
&mcp.Implementation{Name: "client", Version: "v0.0.1"},
198199
&mcp.ClientOptions{
199200
LoggingMessageHandler: func(_ context.Context, r *mcp.LoggingMessageRequest) {
200201
m := r.Params.Data.(map[string]any)
201202
fmt.Println(m["msg"], m["value"])
203+
if nmsgs.Add(1) == 2 { // number depends on logger calls below
204+
close(done)
205+
}
202206
},
203207
})
204208

@@ -231,7 +235,7 @@ func Example_logging() {
231235
// Wait for them to arrive on the client.
232236
// In a real application, the log messages would appear asynchronously
233237
// while other work was happening.
234-
time.Sleep(500 * time.Millisecond)
238+
<-done
235239

236240
// Output:
237241
// info shows up 1

docs/troubleshooting.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ func ExampleLoggingTransport() {
2929
ctx := context.Background()
3030
t1, t2 := mcp.NewInMemoryTransports()
3131
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
32-
if _, err := server.Connect(ctx, t1, nil); err != nil {
32+
serverSession, err := server.Connect(ctx, t1, nil)
33+
if err != nil {
3334
log.Fatal(err)
3435
}
36+
defer serverSession.Wait()
3537

3638
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
3739
var b bytes.Buffer
3840
logTransport := &mcp.LoggingTransport{Transport: t2, Writer: &b}
39-
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
41+
clientSession, err := client.Connect(ctx, logTransport, nil)
42+
if err != nil {
4043
log.Fatal(err)
4144
}
45+
defer clientSession.Close()
46+
4247
// Sort for stability: reads are concurrent to writes.
4348
for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) {
4449
fmt.Println(line)

internal/docs/server.src.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ requests.
5656

5757
### Logging
5858

59-
MCP servers can send logging messages to MCP clients so their users can keep informed of progress.
59+
MCP servers can send logging messages to MCP clients.
6060
(This form of logging is distinct from server-side logging, where the
6161
server produces logs that remain server-side, for use by server maintainers.)
6262

@@ -78,7 +78,6 @@ Servers always report the logging capability.
7878

7979

8080
**Client-side**:
81-
8281
Set [`ClientOptions.LoggingMessageHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.LoggingMessageHandler) to receive log messages.
8382

8483
Call [`ClientSession.SetLevel`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.SetLevel) to change the log level for a session.

mcp/server_example_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"fmt"
1010
"log"
1111
"log/slog"
12-
"time"
12+
"sync/atomic"
1313

1414
"github.com/modelcontextprotocol/go-sdk/mcp"
1515
)
@@ -94,12 +94,17 @@ func Example_logging() {
9494
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
9595

9696
// Create a client that displays log messages.
97+
done := make(chan struct{}) // solely for the example
98+
var nmsgs atomic.Int32
9799
c := mcp.NewClient(
98100
&mcp.Implementation{Name: "client", Version: "v0.0.1"},
99101
&mcp.ClientOptions{
100102
LoggingMessageHandler: func(_ context.Context, r *mcp.LoggingMessageRequest) {
101103
m := r.Params.Data.(map[string]any)
102104
fmt.Println(m["msg"], m["value"])
105+
if nmsgs.Add(1) == 2 { // number depends on logger calls below
106+
close(done)
107+
}
103108
},
104109
})
105110

@@ -132,7 +137,7 @@ func Example_logging() {
132137
// Wait for them to arrive on the client.
133138
// In a real application, the log messages would appear asynchronously
134139
// while other work was happening.
135-
time.Sleep(500 * time.Millisecond)
140+
<-done
136141

137142
// Output:
138143
// info shows up 1

0 commit comments

Comments
 (0)