Class: SchwabMCP::Tools::GetPriceHistoryTool
- Inherits:
-
MCP::Tool
- Object
- MCP::Tool
- SchwabMCP::Tools::GetPriceHistoryTool
- Extended by:
- Loggable
- Defined in:
- lib/schwab_mcp/tools/get_price_history_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(symbol:, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, server_context:) ⇒ Object
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 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/schwab_mcp/tools/get_price_history_tool.rb', line 65 def self.call(symbol:, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, server_context:) log_info("Getting price history for symbol: #{symbol}") begin client = SchwabClientFactory.create_client return SchwabClientFactory.client_error_response unless client parsed_start = nil parsed_end = nil if start_datetime begin parsed_start = DateTime.parse(start_datetime) rescue ArgumentError return MCP::Tool::Response.new([{ type: "text", text: "**Error**: Invalid start_datetime format. Use ISO format like '2024-01-01T00:00:00Z'" }]) end end if end_datetime begin parsed_end = DateTime.parse(end_datetime) rescue ArgumentError return MCP::Tool::Response.new([{ type: "text", text: "**Error**: Invalid end_datetime format. Use ISO format like '2024-01-31T23:59:59Z'" }]) end end if (start_datetime || end_datetime) && (period_type || period) return MCP::Tool::Response.new([{ type: "text", text: "**Error**: Cannot use start_datetime/end_datetime with period_type/period. Choose one approach." }]) end period_type_enum = nil frequency_type_enum = nil if period_type case period_type when "day" period_type_enum = SchwabRb::PriceHistory::PeriodTypes::DAY when "month" period_type_enum = SchwabRb::PriceHistory::PeriodTypes::MONTH when "year" period_type_enum = SchwabRb::PriceHistory::PeriodTypes::YEAR when "ytd" period_type_enum = SchwabRb::PriceHistory::PeriodTypes::YEAR_TO_DATE end end if frequency_type case frequency_type when "minute" frequency_type_enum = SchwabRb::PriceHistory::FrequencyTypes::MINUTE when "daily" frequency_type_enum = SchwabRb::PriceHistory::FrequencyTypes::DAILY when "weekly" frequency_type_enum = SchwabRb::PriceHistory::FrequencyTypes::WEEKLY when "monthly" frequency_type_enum = SchwabRb::PriceHistory::FrequencyTypes::MONTHLY end end log_debug("Making price history API request for symbol: #{symbol}") price_history = client.get_price_history( symbol.upcase, period_type: period_type_enum, period: period, frequency_type: frequency_type_enum, frequency: frequency, start_datetime: parsed_start, end_datetime: parsed_end, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close ) if price_history log_info("Successfully retrieved price history for #{symbol}") summary = if price_history.empty? "No price data available for the specified parameters" else "Retrieved #{price_history.count} price candles\n" \ "First candle: #{price_history.first_candle&.to_h}\n" \ "Last candle: #{price_history.last_candle&.to_h}" end # Show a compact JSON representation for advanced users json_preview = begin require "json" JSON.pretty_generate(price_history.to_h) rescue price_history.to_h.inspect end MCP::Tool::Response.new([{ type: "text", text: "**Price History for #{symbol.upcase}:**\n\n#{summary}\n\n```json\n#{json_preview}\n```" }]) else log_warn("Empty response from Schwab API for symbol: #{symbol}") MCP::Tool::Response.new([{ type: "text", text: "**No Data**: Empty response from Schwab API for symbol: #{symbol}" }]) end rescue => e log_error("Error retrieving price history for #{symbol}: #{e.message}") log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}") MCP::Tool::Response.new([{ type: "text", text: "**Error** retrieving price history for #{symbol}: #{e.message}\n\n#{e.backtrace.first(3).join('\n')}" }]) end end |