internal/mcp: add notifications
Add notification logic for intialization and list changes. Change-Id: I1d392bb49d5995f2046c400da955a033ab2715ce Reviewed-on: https://go-review.googlesource.com/c/tools/+/673775 Reviewed-by: Sam Thanawalla <samthanawalla@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
ec8fb49c6b
commit
ce5b6cd926
1 changed files with 273 additions and 78 deletions
|
|
@ -39,7 +39,26 @@ func TestEndToEnd(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
ct, st := NewInMemoryTransports()
|
||||
|
||||
s := NewServer("testServer", "v1.0.0", nil)
|
||||
// Channels to check if notification callbacks happened.
|
||||
notificationChans := map[string]chan int{}
|
||||
for _, name := range []string{"initialized", "roots", "tools", "prompts", "resources"} {
|
||||
notificationChans[name] = make(chan int, 1)
|
||||
}
|
||||
waitForNotification := func(t *testing.T, name string) {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-notificationChans[name]:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("%s handler never called", name)
|
||||
}
|
||||
}
|
||||
|
||||
sopts := &ServerOptions{
|
||||
InitializedHandler: func(context.Context, *ServerSession, *InitializedParams) { notificationChans["initialized"] <- 0 },
|
||||
RootsListChangedHandler: func(context.Context, *ServerSession, *RootsListChangedParams) { notificationChans["roots"] <- 0 },
|
||||
}
|
||||
|
||||
s := NewServer("testServer", "v1.0.0", sopts)
|
||||
|
||||
// The 'greet' tool says hi.
|
||||
s.AddTools(NewTool("greet", "say hi", sayHi))
|
||||
|
|
@ -89,6 +108,9 @@ func TestEndToEnd(t *testing.T) {
|
|||
CreateMessageHandler: func(context.Context, *ClientSession, *CreateMessageParams) (*CreateMessageResult, error) {
|
||||
return &CreateMessageResult{Model: "aModel"}, nil
|
||||
},
|
||||
ToolListChangedHandler: func(context.Context, *ClientSession, *ToolListChangedParams) { notificationChans["tools"] <- 0 },
|
||||
PromptListChangedHandler: func(context.Context, *ClientSession, *PromptListChangedParams) { notificationChans["prompts"] <- 0 },
|
||||
ResourceListChangedHandler: func(context.Context, *ClientSession, *ResourceListChangedParams) { notificationChans["resources"] <- 0 },
|
||||
}
|
||||
c := NewClient("testClient", "v1.0.0", opts)
|
||||
rootAbs, err := filepath.Abs(filepath.FromSlash("testdata/files"))
|
||||
|
|
@ -103,6 +125,7 @@ func TestEndToEnd(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
waitForNotification(t, "initialized")
|
||||
if err := cs.Ping(ctx, nil); err != nil {
|
||||
t.Fatalf("ping failed: %v", err)
|
||||
}
|
||||
|
|
@ -141,6 +164,11 @@ func TestEndToEnd(t *testing.T) {
|
|||
if _, err := cs.GetPrompt(ctx, &GetPromptParams{Name: "fail"}); err == nil || !strings.Contains(err.Error(), failure.Error()) {
|
||||
t.Errorf("fail returned unexpected error: got %v, want containing %v", err, failure)
|
||||
}
|
||||
|
||||
s.AddPrompts(&ServerPrompt{Prompt: &Prompt{Name: "T"}})
|
||||
waitForNotification(t, "prompts")
|
||||
s.RemovePrompts("T")
|
||||
waitForNotification(t, "prompts")
|
||||
})
|
||||
|
||||
t.Run("tools", func(t *testing.T) {
|
||||
|
|
@ -198,6 +226,11 @@ func TestEndToEnd(t *testing.T) {
|
|||
if diff := cmp.Diff(wantFail, gotFail); diff != "" {
|
||||
t.Errorf("tools/call 'fail' mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
s.AddTools(&ServerTool{Tool: &Tool{Name: "T"}})
|
||||
waitForNotification(t, "tools")
|
||||
s.RemoveTools("T")
|
||||
waitForNotification(t, "tools")
|
||||
})
|
||||
|
||||
t.Run("resources", func(t *testing.T) {
|
||||
|
|
@ -254,6 +287,11 @@ func TestEndToEnd(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.AddResources(&ServerResource{Resource: &Resource{URI: "http://U"}})
|
||||
waitForNotification(t, "resources")
|
||||
s.RemoveResources("http://U")
|
||||
waitForNotification(t, "resources")
|
||||
})
|
||||
t.Run("roots", func(t *testing.T) {
|
||||
rootRes, err := ss.ListRoots(ctx, &ListRootsParams{})
|
||||
|
|
@ -265,6 +303,11 @@ func TestEndToEnd(t *testing.T) {
|
|||
if diff := cmp.Diff(wantRoots, gotRoots); diff != "" {
|
||||
t.Errorf("roots/list mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
c.AddRoots(&Root{URI: "U"})
|
||||
waitForNotification(t, "roots")
|
||||
c.RemoveRoots("U")
|
||||
waitForNotification(t, "roots")
|
||||
})
|
||||
t.Run("sampling", func(t *testing.T) {
|
||||
// TODO: test that a client that doesn't have the handler returns CodeUnsupportedMethod.
|
||||
|
|
@ -462,6 +505,10 @@ func TestMiddleware(t *testing.T) {
|
|||
2 >initialize
|
||||
2 <initialize
|
||||
1 <initialize
|
||||
1 >notifications/initialized
|
||||
2 >notifications/initialized
|
||||
2 <notifications/initialized
|
||||
1 <notifications/initialized
|
||||
1 >tools/list
|
||||
2 >tools/list
|
||||
2 <tools/list
|
||||
|
|
|
|||
Loading…
Reference in a new issue