Class: SchwabMCP::Tools::PreviewOrderTool

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Loggable
Defined in:
lib/schwab_mcp/tools/preview_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(server_context:, **params) ⇒ Object



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
# File 'lib/schwab_mcp/tools/preview_order_tool.rb', line 89

def self.call(server_context:, **params)
  log_info("Previewing #{params[:strategy_type]} order for account name: #{params[:account_name]}")

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

  begin
    validate_strategy_params(params)
    client = SchwabClientFactory.create_client
    return SchwabClientFactory.client_error_response unless client

     = (client, params[:account_name])
    return  if .is_a?(MCP::Tool::Response)

    order_builder = SchwabRb::Orders::OrderFactory.build(
      strategy_type: params[:strategy_type],
      price: params[:price],
      quantity: params[:quantity] || 1,
      order_instruction: (params[:order_instruction] || "open").to_sym,
      credit_debit: (params[:credit_debit] || "credit").to_sym,
      # Iron Condor params
      put_short_symbol: params[:put_short_symbol],
      put_long_symbol: params[:put_long_symbol],
      call_short_symbol: params[:call_short_symbol],
      call_long_symbol: params[:call_long_symbol],
      # Vertical spread params
      short_leg_symbol: params[:short_leg_symbol],
      long_leg_symbol: params[:long_leg_symbol],
      # Single option params
      symbol: params[:symbol]
    )

    log_debug("Making preview order API request")
    response = client.preview_order(account_name: params[:account_name], order: order_builder, return_data_objects: true)

    if response
      log_info("Successfully previewed #{params[:strategy_type]} order")
      formatted_response = format_preview_response(response, params)
      MCP::Tool::Response.new([{
        type: "text",
        text: formatted_response
      }])
    else
      log_warn("Empty response from Schwab API for order preview")
      error_msg = "**No Data**: Empty response from Schwab API for order preview"
      MCP::Tool::Response.new([{
        type: "text",
        text: Redactor.redact_formatted_text(error_msg)
      }])
    end

  rescue => e
    log_error("Error previewing #{params[:strategy_type]} order: #{e.message}")
    error_msg = "**Error** previewing #{params[:strategy_type]} order: #{e.message}\n\n#{e.backtrace.first(3).join('\\n')}"
    MCP::Tool::Response.new([{
      type: "text",
      text: Redactor.redact_formatted_text(error_msg)
    }])
  end
end