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
|
|
@ -81,9 +81,9 @@ func (c *Client) disconnect(sc *ServerConnection) {
|
|||
// Connect connects the MCP client over the given transport and initializes an
|
||||
// MCP session.
|
||||
//
|
||||
// It returns a connection object that may be used to query the MCP server,
|
||||
// terminate the connection (with [Connection.Close]), or await server
|
||||
// termination (with [Connection.Wait]).
|
||||
// It returns an initialized [ServerConnection] object that may be used to
|
||||
// query the MCP server, terminate the connection (with [Connection.Close]), or
|
||||
// await server termination (with [Connection.Wait]).
|
||||
//
|
||||
// Typically, it is the responsibility of the client to close the connection
|
||||
// when it is no longer needed. However, if the connection is closed by the
|
||||
|
|
@ -105,7 +105,7 @@ func (c *Client) Connect(ctx context.Context, t Transport, opts *ConnectionOptio
|
|||
if err := call(ctx, sc.conn, "initialize", params, &sc.initializeResult); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := sc.conn.Notify(ctx, "initialized", &protocol.InitializedParams{}); err != nil {
|
||||
if err := sc.conn.Notify(ctx, "notifications/initialized", &protocol.InitializedParams{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sc, nil
|
||||
|
|
@ -135,11 +135,21 @@ func (cc *ServerConnection) Wait() error {
|
|||
}
|
||||
|
||||
func (sc *ServerConnection) handle(ctx context.Context, req *jsonrpc2.Request) (any, error) {
|
||||
// No need to check that the connection is initialized, since we initialize
|
||||
// it in Connect.
|
||||
switch req.Method {
|
||||
case "ping":
|
||||
// The spec says that 'ping' expects an empty object result.
|
||||
return struct{}{}, nil
|
||||
}
|
||||
return nil, jsonrpc2.ErrNotHandled
|
||||
}
|
||||
|
||||
// Ping makes an MCP "ping" request to the server.
|
||||
func (sc *ServerConnection) Ping(ctx context.Context) error {
|
||||
return call(ctx, sc.conn, "ping", nil, nil)
|
||||
}
|
||||
|
||||
// ListTools lists tools that are currently available on the server.
|
||||
func (sc *ServerConnection) ListTools(ctx context.Context) ([]protocol.Tool, error) {
|
||||
var (
|
||||
|
|
|
|||
Loading…
Reference in a new issue