Class: SchwabMCP::Tools::ListAccountOrdersTool

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Loggable
Defined in:
lib/schwab_mcp/tools/list_account_orders_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(account_name:, max_results: nil, from_date: nil, to_date: nil, status: nil, server_context:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/schwab_mcp/tools/list_account_orders_tool.rb', line 75

def self.call(account_name:, max_results: nil, from_date: nil, to_date: nil, status: nil, server_context:)
  log_info("Listing orders for account name: #{}")

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

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

    available_accounts = client.
    unless available_accounts.include?()
      log_error("Account name '#{}' not found in configured accounts")
      return MCP::Tool::Response.new([{
        type: "text",
        text: "**Error**: 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: #{}")

    from_datetime = nil
    to_datetime = nil

    if from_date
      begin
        from_datetime = DateTime.parse("#{from_date}T00:00:00Z")
      rescue Date::Error => e
        log_error("Invalid from_date format: #{from_date}")
        return MCP::Tool::Response.new([{
          type: "text",
          text: "**Error**: Invalid from_date format. Use YYYY-MM-DD format."
        }])
      end
    end

    if to_date
      begin
        to_datetime = DateTime.parse("#{to_date}T23:59:59Z")
      rescue Date::Error => e
        log_error("Invalid to_date format: #{to_date}")
        return MCP::Tool::Response.new([{
          type: "text",
          text: "**Error**: Invalid to_date format. Use YYYY-MM-DD format."
        }])
      end
    end

    log_debug("Fetching orders with params - max_results: #{max_results}, from_datetime: #{from_datetime}, to_datetime: #{to_datetime}, status: #{status}")

    orders = client.(
      account_name: ,
      max_results: max_results,
      from_entered_datetime: from_datetime,
      to_entered_datetime: to_datetime,
      status: status
    )

    if orders
      log_info("Successfully retrieved orders for #{}")

      formatted_response = format_orders_data(orders, , {
        max_results: max_results,
        from_date: from_date,
        to_date: to_date,
        status: status
      })

      MCP::Tool::Response.new([{
        type: "text",
        text: formatted_response
      }])
    else
      log_warn("Empty response from Schwab API for account: #{}")
      MCP::Tool::Response.new([{
        type: "text",
        text: "**No Data**: Empty response from Schwab API for account: #{}"
      }])
    end

  rescue => e
    log_error("Error retrieving orders for #{}: #{e.message}")
    log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}")
    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error** retrieving orders for #{}: #{e.message}\n\n#{e.backtrace.first(3).join('\n')}"
    }])
  end
end