feat(mcp): expose session Ping on Client
Gateway supervisors need a cheap liveness probe to detect mid-flight upstream death (crashed stdio child, restarted HTTP server). Client.Ping forwards to the SDK session's MCP ping; a non-nil error means the session is no longer usable. Also moves examples/minimal's keep-import-live statement above the REPL loop — it sat after an infinite for loop, tripping go vet's unreachable code check.
This commit is contained in:
1 parent
6f0040f606
commit
cf47b2fca8
3 files changed
+32
-1
No files matched your search
@@ -82,6 +82,8 @@ func main() {
|
||||
}
|
||||
defer rt.Close() // no-op here (no MCP), but idiomatic
|
||||
|
||||
_ = llm.ReasoningOff // keep llm import live for clarity in the demo
|
||||
|
||||
// 5. REPL loop.
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
fmt.Println("harness demo — type a message, blank line to exit")
|
||||
@@ -115,5 +117,4 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = llm.ReasoningOff // keep llm import live for clarity in the demo
|
||||
}
|
||||
@@ -125,6 +125,13 @@ func (c *Client) Tools() []tool.Tool { return c.tools }
|
||||
// Name returns the configured server name.
|
||||
func (c *Client) Name() string { return c.name }
|
||||
|
||||
// Ping verifies the session is alive by sending an MCP ping. A non-nil
|
||||
// error means the session (and for stdio servers, likely the child
|
||||
// process) is no longer usable.
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
return c.session.Ping(ctx, nil)
|
||||
}
|
||||
|
||||
// Close releases the underlying MCP session. Safe to call multiple
|
||||
// times — subsequent calls are no-ops.
|
||||
func (c *Client) Close() error {
|
||||
|
||||
@@ -150,6 +150,29 @@ func TestMCPClient_ConnectDiscovers(t *testing.T) {
|
||||
assert.ElementsMatch(t, []string{"mcp__srv__a", "mcp__srv__b"}, got)
|
||||
}
|
||||
|
||||
func TestMCPClient_PingLiveAndDead(t *testing.T) {
|
||||
// Built inline (not via startInProcessServer) because the test needs the
|
||||
// server session to kill the connection mid-life.
|
||||
ctx := context.Background()
|
||||
server := sdk.NewServer(&sdk.Implementation{Name: "test-server", Version: "v0"}, nil)
|
||||
tf := stringArgTool("noop", "no-op", func(_ context.Context, _ *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
|
||||
return &sdk.CallToolResult{Content: []sdk.Content{&sdk.TextContent{Text: ""}}}, nil
|
||||
})
|
||||
server.AddTool(tf.Tool, tf.Handler)
|
||||
clientTransport, serverTransport := sdk.NewInMemoryTransports()
|
||||
serverSession, err := server.Connect(ctx, serverTransport, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
cli, err := connectWithTransport(ctx, "srv", clientTransport, nil)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = cli.Close() })
|
||||
|
||||
require.NoError(t, cli.Ping(ctx), "ping against a live session must succeed")
|
||||
|
||||
require.NoError(t, serverSession.Close())
|
||||
assert.Error(t, cli.Ping(ctx), "ping after the server side died must fail")
|
||||
}
|
||||
|
||||
func TestMCPClient_CloseIsIdempotent(t *testing.T) {
|
||||
cli := startInProcessServer(t, "srv", []sdkToolFixture{
|
||||
stringArgTool("noop", "no-op", func(_ context.Context, _ *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
|
||||
|
||||
Reference in new issue
Block a user