Feature: Stdio MCP Transport
  As an MCP client using the stdio transport
  I want to connect to MCP servers via subprocess stdio
  So that I can discover and invoke tools from real MCP servers

  Background:
    Given the MCP stdio stub server is available

  # -------------------------------------------------------------------
  # Connection Lifecycle
  # -------------------------------------------------------------------

  Scenario: Connect to MCP stdio server performs handshake
    Given an MCP server config using the stdio stub server
    When I create a stdio transport
    And I connect the transport
    Then the MCP stdio transport should be connected
    And the server capabilities should include tools support

  Scenario: Connect with missing command raises error
    Given an MCP server config with stdio transport and no command
    When I create a stdio transport and connect
    Then the connection should fail with error containing "command"

  Scenario: Connect with non-existent command raises error
    Given an MCP server config with stdio transport and command "/nonexistent/binary"
    When I create a stdio transport and connect
    Then the connection should fail with error containing "not found"

  Scenario: Double connect is idempotent
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I connect the transport again
    Then the MCP stdio transport should be connected

  Scenario: Close transport terminates subprocess
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I close the transport
    Then the MCP stdio transport should not be connected

  # -------------------------------------------------------------------
  # Tool Discovery via Transport
  # -------------------------------------------------------------------

  Scenario: Discover tools via stdio transport
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I call "tools/list" on the transport
    Then the response should contain "tools" key
    And the tools list should have 3 tools
    And tool 0 should have name "test/echo"

  Scenario: Call unknown method raises error
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I call "nonexistent/method" on the transport
    Then the call should fail with error containing "Unknown method"

  # -------------------------------------------------------------------
  # Tool Invocation via Transport
  # -------------------------------------------------------------------

  Scenario: Invoke test/echo tool via stdio transport
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I call "tools/call" with arguments {"name": "test/echo", "arguments": {"message": "hello"}}
    Then the call should succeed
    And the response should contain "content" key

  Scenario: Invoke test/multiply tool via stdio transport
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I call "tools/call" with arguments {"name": "test/multiply", "arguments": {"a": 6, "b": 7}}
    Then the call should succeed
    And the response content should contain "42"

  Scenario: Invoke unknown tool returns error response
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I call "tools/call" with arguments {"name": "test/nonexistent", "arguments": {}}
    Then the call should fail with error containing "Unknown tool"

  # -------------------------------------------------------------------
  # MCPToolAdapter Integration with Stdio Transport
  # -------------------------------------------------------------------

  Scenario: MCPToolAdapter uses StdioMCPTransport automatically for stdio config
    Given an MCP server config for "test-server" with stdio transport
    And the config command is set to the stdio stub server path
    When I create an MCP adapter from the config
    And I connect the adapter
    Then the adapter transport type should be "stdio"
    And the adapter should be connected
    And the adapter server capabilities should be set

  Scenario: MCPToolAdapter discovers tools via StdioMCPTransport
    Given an MCP server config for "test-server" with stdio transport
    And the config command is set to the stdio stub server path
    When I create an MCP adapter from the config
    And I connect the adapter
    And I discover tools from the adapter
    Then the adapter should have 3 discovered tools

  Scenario: MCPToolAdapter invokes tools via StdioMCPTransport
    Given an MCP server config for "test-server" with stdio transport
    And the config command is set to the stdio stub server path
    When I create an MCP adapter from the config
    And I connect the adapter
    And I discover tools from the adapter
    And I invoke "test/echo" with arguments {"message": "test"}
    Then the invocation should succeed
    And the invocation result should contain key "content"

  # -------------------------------------------------------------------
  # Notifications
  # -------------------------------------------------------------------

  Scenario: Send notification to server
    Given an MCP server config using the stdio stub server
    And a connected stdio transport
    When I send a "test/custom_notification" notification with params {}
    Then the MCP stdio transport should still be connected
