Class: SchwabMCP::Tools::ListAccountTransactionsTool

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Loggable
Defined in:
lib/schwab_mcp/tools/list_account_transactions_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:, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, server_context:) ⇒ Object



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
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
169
170
171
172
173
174
# File 'lib/schwab_mcp/tools/list_account_transactions_tool.rb', line 71

def self.call(account_name:, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, server_context:)
  log_info("Listing transactions 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: #{}")

    start_date_obj = nil
    end_date_obj = nil

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

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

    log_debug("Fetching transactions with params - start_date: #{start_date_obj}, end_date: #{end_date_obj}, transaction_types: #{transaction_types}, symbol: #{symbol}")

    transactions = client.get_transactions(
      account_name: ,
      start_date: start_date_obj,
      end_date: end_date_obj,
      transaction_types: transaction_types,
      symbol: symbol
    )

    if transactions
      log_info("Successfully retrieved transactions for #{}")
      formatted_response = format_transactions_data(transactions, , {
        start_date: start_date,
        end_date: end_date,
        transaction_types: transaction_types,
        symbol: symbol
      })

      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 JSON::ParserError => e
    redactor = Redactor.new
    err_msg = redactor.redact(e.message)
    log_error("JSON parsing error: #{err_msg}")
    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error**: Failed to parse API response: #{err_msg}"
    }])
  rescue => e
    redactor = Redactor.new
    err_msg = redactor.redact(e.message)
    log_error("Error retrieving transactions for #{}: #{err_msg}")
    log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}")

    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error** retrieving transactions for #{}: #{err_msg}\n\n#{e.backtrace.first(3).join('\n')}"
    }])
  end
end