mcp: fix flaky ExampleLoggingTransport (#483)
Reads are technically racy with writes. Sort for stability.
This commit is contained in:
parent
208bfe25fd
commit
b636b1633b
2 changed files with 17 additions and 6 deletions
|
|
@ -39,12 +39,15 @@ func ExampleLoggingTransport() {
|
||||||
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
|
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(b.String())
|
// Sort for stability: reads are concurrent to writes.
|
||||||
// Output:
|
for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) {
|
||||||
// write: {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"client","version":"v0.0.1"},"protocolVersion":"2025-06-18"}}
|
fmt.Println(line)
|
||||||
// read: {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-06-18","serverInfo":{"name":"server","version":"v0.0.1"}}}
|
}
|
||||||
// write: {"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// read: {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-06-18","serverInfo":{"name":"server","version":"v0.0.1"}}}
|
||||||
|
// write: {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"client","version":"v0.0.1"},"protocolVersion":"2025-06-18"}}
|
||||||
|
// write: {"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
// Use of this source code is governed by an MIT-style
|
// Use of this source code is governed by an MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Uses strings.SplitSeq.
|
||||||
|
//go:build go1.24
|
||||||
|
|
||||||
package mcp_test
|
package mcp_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -9,6 +12,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||||
)
|
)
|
||||||
|
|
@ -29,12 +34,15 @@ func ExampleLoggingTransport() {
|
||||||
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
|
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(b.String())
|
// Sort for stability: reads are concurrent to writes.
|
||||||
// Output:
|
for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) {
|
||||||
// write: {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"client","version":"v0.0.1"},"protocolVersion":"2025-06-18"}}
|
fmt.Println(line)
|
||||||
// read: {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-06-18","serverInfo":{"name":"server","version":"v0.0.1"}}}
|
}
|
||||||
// write: {"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// read: {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-06-18","serverInfo":{"name":"server","version":"v0.0.1"}}}
|
||||||
|
// write: {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"client","version":"v0.0.1"},"protocolVersion":"2025-06-18"}}
|
||||||
|
// write: {"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// !-loggingtransport
|
// !-loggingtransport
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue