Module: BSV::MCP::Tools::Helpers
- Defined in:
- lib/bsv/mcp/tools/helpers.rb
Overview
Shared helpers used across multiple MCP tools.
Class Method Summary collapse
-
.error_response(message) ⇒ MCP::Tool::Response
Build a standard MCP error response.
-
.input_to_h(input, _index) ⇒ Object
private
Convert a TransactionInput to a hash.
-
.output_to_h(output, index) ⇒ Object
private
Convert a TransactionOutput to a hash.
-
.resolve_network_sym(network_arg, server_context) ⇒ Symbol
Resolve a network string to the SDK symbol (:mainnet or :testnet).
-
.transaction_to_h(tx) ⇒ Hash
Convert a Transaction to a hash suitable for JSON responses.
-
.utxo_to_h(utxo) ⇒ Object
private
Convert a UTXO to a hash.
Class Method Details
.error_response(message) ⇒ MCP::Tool::Response
Build a standard MCP error response.
77 78 79 80 81 82 83 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 77 def self.error_response() payload = { error: } ::MCP::Tool::Response.new( [::MCP::Content::Text.new(payload.to_json)], error: true ) end |
.input_to_h(input, _index) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert a TransactionInput to a hash.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 38 def self.input_to_h(input, _index) unlock_script = input.unlocking_script { prev_txid: input.prev_tx_id.reverse.unpack1('H*'), vout: input.prev_tx_out_index, script_hex: unlock_script ? unlock_script.to_hex : '', script_asm: unlock_script ? unlock_script.to_asm : '', sequence: input.sequence } end |
.output_to_h(output, index) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert a TransactionOutput to a hash.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 51 def self.output_to_h(output, index) script = output.locking_script { index: index, satoshis: output.satoshis, script_hex: script ? script.to_hex : '', script_asm: script ? script.to_asm : '', script_type: script ? script.type : 'empty' } end |
.resolve_network_sym(network_arg, server_context) ⇒ Symbol
Resolve a network string to the SDK symbol (:mainnet or :testnet).
Checks the per-call network_arg first, then falls back to the server-level config in server_context, then defaults to :mainnet.
16 17 18 19 20 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 16 def self.resolve_network_sym(network_arg, server_context) net = network_arg net = server_context[:bsv_network] if net.nil? && server_context.is_a?(Hash) && server_context[:bsv_network] net == 'testnet' ? :testnet : :mainnet end |
.transaction_to_h(tx) ⇒ Hash
Convert a Transaction to a hash suitable for JSON responses.
26 27 28 29 30 31 32 33 34 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 26 def self.transaction_to_h(tx) { txid: tx.txid_hex, version: tx.version, lock_time: tx.lock_time, inputs: tx.inputs.each_with_index.map { |inp, i| input_to_h(inp, i) }, outputs: tx.outputs.each_with_index.map { |out, i| output_to_h(out, i) } } end |
.utxo_to_h(utxo) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert a UTXO to a hash.
64 65 66 67 68 69 70 71 |
# File 'lib/bsv/mcp/tools/helpers.rb', line 64 def self.utxo_to_h(utxo) { tx_hash: utxo.tx_hash, tx_pos: utxo.tx_pos, satoshis: utxo.satoshis, height: utxo.height } end |