diff --git a/examples/minimal/main.go b/examples/minimal/main.go index 3fa2a5b..d018dbc 100644 --- a/examples/minimal/main.go +++ b/examples/minimal/main.go @@ -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 } diff --git a/tools/mcp/mcp.go b/tools/mcp/mcp.go index 628b62f..5a6cf17 100644 --- a/tools/mcp/mcp.go +++ b/tools/mcp/mcp.go @@ -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 { diff --git a/tools/mcp/mcp_test.go b/tools/mcp/mcp_test.go index ca17b5a..1516e4d 100644 --- a/tools/mcp/mcp_test.go +++ b/tools/mcp/mcp_test.go @@ -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) {