Skip to content

Commit

Permalink
feat: Add ping support to clients and server (#7)
Browse files Browse the repository at this point in the history
Unlike other methods, the unimplemented server has a default implemenation for
the ping method.
  • Loading branch information
kyleconroy authored Dec 11, 2024
1 parent a121aa3 commit 20d167d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Client interface {
ReadResource(ctx context.Context, req *Request[ReadResourceRequest]) (*Response[ReadResourceResponse], error)
ListResourceTemplates(ctx context.Context, req *Request[ListResourceTemplatesRequest]) (*Response[ListResourceTemplatesResponse], error)
Completion(ctx context.Context, req *Request[CompletionRequest]) (*Response[CompletionResponse], error)
Ping(ctx context.Context, req *Request[PingRequest]) (*Response[PingResponse], error)
}

type StdioClient struct {
Expand Down Expand Up @@ -166,3 +167,7 @@ func (c *StdioClient) ListResourceTemplates(ctx context.Context, request *Reques
func (c *StdioClient) Completion(ctx context.Context, request *Request[CompletionRequest]) (*Response[CompletionResponse], error) {
return clientCallUnary[CompletionRequest, CompletionResponse](ctx, c, "completion", request)
}

func (c *StdioClient) Ping(ctx context.Context, request *Request[PingRequest]) (*Response[PingResponse], error) {
return clientCallUnary[PingRequest, PingResponse](ctx, c, "ping", request)
}
7 changes: 7 additions & 0 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ func TestEndToEnd(t *testing.T) {
t.Fatalf("expected protocol version 1.0.0, got %s", resp.Result.ProtocolVersion)
}
}

{
_, err := client.Ping(ctx, mcp.NewRequest(&mcp.PingRequest{}))
if err != nil {
t.Fatalf("failed to ping server: %v", err)
}
}
}
6 changes: 6 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,9 @@ type CompletionResult struct {
HasMore bool `json:"hasMore"`
Total int `json:"total"`
}

type PingRequest struct {
}

type PingResponse struct {
}
8 changes: 8 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Server interface {
ReadResource(ctx context.Context, req *Request[ReadResourceRequest]) (*Response[ReadResourceResponse], error)
ListResourceTemplates(ctx context.Context, req *Request[ListResourceTemplatesRequest]) (*Response[ListResourceTemplatesResponse], error)
Completion(ctx context.Context, req *Request[CompletionRequest]) (*Response[CompletionResponse], error)
Ping(ctx context.Context, req *Request[PingRequest]) (*Response[PingResponse], error)
}

type UnimplementedServer struct{}
Expand Down Expand Up @@ -61,6 +62,10 @@ func (s *UnimplementedServer) Completion(ctx context.Context, req *Request[Compl
return nil, fmt.Errorf("unimplemented")
}

func (s *UnimplementedServer) Ping(ctx context.Context, req *Request[PingRequest]) (*Response[PingResponse], error) {
return NewResponse(&PingResponse{}), nil
}

func process[T, V any](ctx context.Context, cfg *serverConfig, msg jsonrpc.Request, params *T, method func(ctx context.Context, req *Request[T]) (*Response[V], error)) (any, error) {
var interceptor Interceptor
if len(cfg.interceptors) > 0 {
Expand Down Expand Up @@ -168,6 +173,9 @@ func (s StdioServer) Listen(ctx context.Context, r io.Reader, w io.Writer) error
case "resources/templates/list":
params := &ListResourceTemplatesRequest{}
result, err = process(ctx, cfg, msg, params, srv.ListResourceTemplates)
case "ping":
params := &PingRequest{}
result, err = process(ctx, cfg, msg, params, srv.Ping)
default:
if msg.ID == "" {
// Ignore notifications
Expand Down

0 comments on commit 20d167d

Please sign in to comment.