Class: SchwabMCP::Tools::SchwabAccountDetailsTool

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



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
96
97
98
99
# File 'lib/schwab_mcp/tools/schwab_account_details_tool.rb', line 39

def self.call(account_name:, fields: nil, server_context:)
  log_info("Getting account information for account name: #{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

  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 account information with fields: #{fields}")

     = client.(account_name: , fields: fields)

    if 
      log_info("Successfully retrieved account information for #{account_name}")

      formatted_response = (, )

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

  rescue JSON::ParserError => e
    log_error("JSON parsing error: #{e.message}")
    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error**: Failed to parse API response: #{e.message}"
    }])
  rescue => e
    log_error("Error retrieving account information for #{account_name}: #{e.message}")
    log_debug("Backtrace: #{e.backtrace.first(3).join('\n')}")
    MCP::Tool::Response.new([{
      type: "text",
      text: "**Error** retrieving account information for #{account_name}: #{e.message}\n\n#{e.backtrace.first(3).join('\n')}"
    }])
  end
end