internal/mcp: implement ping, and test request interleaving
Also validate that requests observe the lifecycle rules defined in the spec. Change-Id: I302282f81e053e76926e0bb295f7dbf8d526f02d Reviewed-on: https://go-review.googlesource.com/c/tools/+/667575 Reviewed-by: Jonathan Amsterdam <jba@google.com> Commit-Queue: Robert Findley <rfindley@google.com> Auto-Submit: Robert Findley <rfindley@google.com> TryBot-Bypass: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
ef446f82d7
commit
fe938f6eb7
1 changed files with 81 additions and 18 deletions
|
|
@ -7,6 +7,7 @@ package mcp
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
|
@ -22,7 +23,10 @@ type hiParams struct {
|
|||
Name string
|
||||
}
|
||||
|
||||
func sayHi(_ context.Context, v hiParams) ([]Content, error) {
|
||||
func sayHi(ctx context.Context, cc *ClientConnection, v hiParams) ([]Content, error) {
|
||||
if err := cc.Ping(ctx); err != nil {
|
||||
return nil, fmt.Errorf("ping failed: %v", err)
|
||||
}
|
||||
return []Content{TextContent{Text: "hi " + v.Name}}, nil
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +41,7 @@ func TestEndToEnd(t *testing.T) {
|
|||
|
||||
// The 'fail' tool returns this error.
|
||||
failure := errors.New("mcp failure")
|
||||
s.AddTools(MakeTool("fail", "just fail", func(context.Context, struct{}) ([]Content, error) {
|
||||
s.AddTools(MakeTool("fail", "just fail", func(context.Context, *ClientConnection, struct{}) ([]Content, error) {
|
||||
return nil, failure
|
||||
}))
|
||||
|
||||
|
|
@ -67,10 +71,15 @@ func TestEndToEnd(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := slices.Collect(c.Servers()); len(got) != 1 {
|
||||
t.Errorf("after connection, Servers() has length %d, want 1", len(got))
|
||||
}
|
||||
|
||||
if err := sc.Ping(ctx); err != nil {
|
||||
t.Fatalf("ping failed: %v", err)
|
||||
}
|
||||
|
||||
gotTools, err := sc.ListTools(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("tools/list failed: %v", err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue