Class: SchwabMCP::Tools::GetOrderTool

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Loggable
Defined in:
lib/schwab_mcp/tools/get_order_tool.rb

Class Method Summary collapse

Methods included from Loggable

log_debug, log_error, log_fatal, log_info, log_warn, logger

Class Method Details

.call(order_id:, account_name:, server_context:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/schwab_mcp/tools/get_order_tool.rb', line 37

def self.call(order_id:, account_name:, server_context:)
  log_info("Getting order details for order ID: #{order_id} in account: #{account_name}")

  unless .end_with?('_ACCOUNT')
    log_error("Invalid account name format: #{account_name}")
    return MCP::Tool::Response.new([{
      type: "text",
      text: "**Error**: Account name must end with '_ACCOUNT'. Example: 'TRADING_BROKERAGE_ACCOUNT'"
    }])
  end

  unless order_id.match?(/^\d+$/)
    log_error("Invalid order ID format: #{order_id}")
    return MCP::Tool::Response.new([{
      type: "text",
      text: "**Error**: Order ID must be numeric. Example: '123456789'"
    }])
  end

  begin
    client = SchwabClientFactory.create_client
    return SchwabClientFactory.client_error_response unless client

    available_accounts = client.
    unless available_accounts.include?()
      log_error("Account name '#{account_name}' not found in configured accounts")
      return MCP::Tool::Response.new([{
        type: "text",
        text: "**Error**: Account name '#{account_name}' not found in configured accounts.\n\nAvailable accounts: #{available_accounts.join(', ')}\n\nTo configure: Add the account to your schwab_rb configuration file."
      }])
    end

    log_debug("Using account name: #{account_name}")
    log_debug("Fetching order details for order ID: #{order_id}")

    order = client.get_order(order_id, account_name: ) # returns SchwabRb::DataObjects::Order
    if order
      log_info("Successfully retrieved order details for order ID: #{order_id}")
      formatted_response = format_order_object(order, order_id, )
      MCP::Tool::Response.new([{
        type: "text",
        text: formatted_response
      }])
    else
      log_warn("Empty response from Schwab API for order ID: #{order_id}")
      MCP::Tool::Response.new([{
        type: "text",
        text: "**No Data**: Empty response from Schwab API for order ID: #{order_id}. Order may not exist or may be in a different account."
      }])
    end
  rescue => e
    log_error("Error retrieving order details for order ID #{order_id}: #{e.message}")
    log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}")
    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error** retrieving order details for order ID #{order_id}: #{e.message}\n\n#{e.backtrace.first(3).join('\n')}"
    }])
  end
end