MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestKeepAliveMethodNotFound

Function TestKeepAliveMethodNotFound

mcp/mcp_test.go:942–995  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

940}
941
942func TestKeepAliveMethodNotFound(t *testing.T) {
943 synctest.Test(t, func(t *testing.T) {
944 ctx := context.Background()
945
946 ct, st := NewInMemoryTransports()
947
948 // Server that rejects ping with method-not-found, simulating a
949 // server that does not implement the optional ping method.
950 s := NewServer(testImpl, nil)
951 AddTool(s, greetTool(), sayHi)
952 s.AddReceivingMiddleware(func(next MethodHandler) MethodHandler {
953 return func(ctx context.Context, method string, req Request) (Result, error) {
954 if method == "ping" {
955 return nil, jsonrpc2.ErrMethodNotFound
956 }
957 return next(ctx, method, req)
958 }
959 })
960 ss, err := s.Connect(ctx, st, nil)
961 if err != nil {
962 t.Fatal(err)
963 }
964 defer ss.Close()
965
966 clientOpts := &ClientOptions{
967 KeepAlive: 50 * time.Millisecond,
968 }
969 c := NewClient(testImpl, clientOpts)
970 cs, err := c.Connect(ctx, ct, nil)
971 if err != nil {
972 t.Fatal(err)
973 }
974 defer cs.Close()
975
976 // Advance past several keepalive cycles.
977 time.Sleep(200 * time.Millisecond)
978
979 // The session should still be alive despite the server not
980 // supporting ping.
981 result, err := cs.CallTool(ctx, &CallToolParams{
982 Name: "greet",
983 Arguments: map[string]any{"Name": "user"},
984 })
985 if err != nil {
986 t.Fatalf("call failed after keepalive with method-not-found: %v", err)
987 }
988 if len(result.Content) == 0 {
989 t.Fatal("expected content in result")
990 }
991 if textContent, ok := result.Content[0].(*TextContent); !ok || textContent.Text != "hi user" {
992 t.Fatalf("unexpected result: %v", result.Content[0])
993 }
994 })
995}
996
997func TestElicitationUnsupportedMethod(t *testing.T) {
998 ctx := context.Background()

Callers

nothing calls this directly

Calls 10

ConnectMethod · 0.95
ConnectMethod · 0.95
NewInMemoryTransportsFunction · 0.85
NewServerFunction · 0.85
AddToolFunction · 0.85
greetToolFunction · 0.85
NewClientFunction · 0.85
CallToolMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…