We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/haiqiubullish/nix-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server
"""Tool handler functions for NIX MCP Server"""
import json
import logging
from typing import Any, Dict, List, Optional
from mcp.types import TextContent
logger = logging.getLogger(__name__)
async def handle_get_global_configs(client, blockchain: Optional[str] = None, network: Optional[str] = None) -> List[TextContent]:
"""
Handle get_global_configs tool request
Args:
client: NixClient instance
blockchain: Optional blockchain name
network: Optional network name
Returns:
List containing TextContent with global configs
"""
try:
result = await client.query_global_configs(blockchain, network)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting global configs: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_network_status(client, blockchain: str, network: str) -> List[TextContent]:
"""
Handle get_network_status tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
Returns:
List containing TextContent with network status
"""
try:
result = await client.query_network_status(blockchain, network)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting network status: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_blocks(client, blockchain: str, network: str,
block_height: Optional[int] = None,
from_index: Optional[int] = None,
to_index: Optional[int] = None,
max_count: int = 10) -> List[TextContent]:
"""
Handle get_blocks tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
block_height: Specific block height
from_index: Start block index
to_index: End block index
max_count: Maximum blocks to return
Returns:
List containing TextContent with block data
"""
try:
result = await client.query_blocks(
blockchain, network, block_height,
from_index, to_index, max_count
)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting blocks: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_transaction(client, blockchain: str, network: str, transaction_hash: str) -> List[TextContent]:
"""
Handle get_transaction tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
transaction_hash: Transaction hash
Returns:
List containing TextContent with transaction data
"""
try:
from .protobuf import native_indexer_pb2
from google.protobuf.json_format import MessageToDict
query = native_indexer_pb2.TransactionQuery()
query.network.blockchain = blockchain
query.network.network = network
query.transaction_identifier.hash = transaction_hash
trace = await client.push_action_ro(
client.nix_query_contract,
"transaction",
client.protobuf_to_hex(query)
)
if not client.is_success_trace(trace):
error = trace.get("error", "Unknown error")
raise Exception(f"Failed to query transaction: {error}")
return_hex = client.extract_return_value(trace)
transactions = native_indexer_pb2.Transactions()
transactions.ParseFromString(bytes.fromhex(return_hex))
result = MessageToDict(transactions, preserving_proto_field_name=True)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting transaction: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_transactions(client, blockchain: str, network: str,
max_count: int = 10,
address: Optional[str] = None,
from_index: Optional[int] = None,
to_index: Optional[int] = None) -> List[TextContent]:
"""
Handle get_transactions tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
max_count: Maximum transactions to return
address: Optional account address filter
from_index: Start block index
to_index: End block index
Returns:
List containing TextContent with transactions data
"""
try:
from .protobuf import native_indexer_pb2
from google.protobuf.json_format import MessageToDict
query = native_indexer_pb2.TransactionsQuery()
query.network.blockchain = blockchain
query.network.network = network
query.max_transaction_count = max_count
query.from_index = from_index or 0
query.to_index = to_index or 0
if address:
query.account.address = address
trace = await client.push_action_ro(
client.nix_query_contract,
"transactions",
client.protobuf_to_hex(query)
)
if not client.is_success_trace(trace):
error = trace.get("error", "Unknown error")
raise Exception(f"Failed to query transactions: {error}")
return_hex = client.extract_return_value(trace)
transactions = native_indexer_pb2.Transactions()
transactions.ParseFromString(bytes.fromhex(return_hex))
result = MessageToDict(transactions, preserving_proto_field_name=True)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting transactions: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_account(client, blockchain: str, network: str, address: str) -> List[TextContent]:
"""
Handle get_account tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
address: Account address
Returns:
List containing TextContent with account data
"""
try:
from .protobuf import native_indexer_pb2
from google.protobuf.json_format import MessageToDict
query = native_indexer_pb2.AccountsQuery()
query.network.blockchain = blockchain
query.network.network = network
query.query_by = "sub_address"
query.address = address
query.max_result_count = 1
trace = await client.push_action_ro(
client.nix_query_contract,
"accts",
client.protobuf_to_hex(query)
)
if not client.is_success_trace(trace):
error = trace.get("error", "Unknown error")
raise Exception(f"Failed to query account: {error}")
return_hex = client.extract_return_value(trace)
accounts = native_indexer_pb2.AccountsIdentifier()
accounts.ParseFromString(bytes.fromhex(return_hex))
result = MessageToDict(accounts, preserving_proto_field_name=True)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting account: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_account_balance(client, blockchain: str, network: str, address: str) -> List[TextContent]:
"""
Handle get_account_balance tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
address: Account address
Returns:
List containing TextContent with balance data
"""
try:
from .protobuf import native_indexer_pb2
from google.protobuf.json_format import MessageToDict
query = native_indexer_pb2.NetworkAccountIdentifier()
query.network.blockchain = blockchain
query.network.network = network
query.account.address = address
trace = await client.push_action_ro(
client.nix_query_contract,
"balances",
client.protobuf_to_hex(query)
)
if not client.is_success_trace(trace):
error = trace.get("error", "Unknown error")
raise Exception(f"Failed to query balances: {error}")
return_hex = client.extract_return_value(trace)
balances = native_indexer_pb2.Amounts()
balances.ParseFromString(bytes.fromhex(return_hex))
result = MessageToDict(balances, preserving_proto_field_name=True)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting account balance: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]
async def handle_get_account_keys(client, blockchain: str, network: str, address: str) -> List[TextContent]:
"""
Handle get_account_keys tool request
Args:
client: NixClient instance
blockchain: Blockchain name
network: Network name
address: Account address
Returns:
List containing TextContent with keys data
"""
try:
from .protobuf import native_indexer_pb2
from google.protobuf.json_format import MessageToDict
query = native_indexer_pb2.NetworkAccountIdentifier()
query.network.blockchain = blockchain
query.network.network = network
query.account.address = address
trace = await client.push_action_ro(
client.nix_query_contract,
"keys",
client.protobuf_to_hex(query)
)
if not client.is_success_trace(trace):
error = trace.get("error", "Unknown error")
raise Exception(f"Failed to query keys: {error}")
return_hex = client.extract_return_value(trace)
keys = native_indexer_pb2.KeysWithMeta()
keys.ParseFromString(bytes.fromhex(return_hex))
result = MessageToDict(keys, preserving_proto_field_name=True)
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
logger.error(f"Error getting account keys: {e}")
return [TextContent(
type="text",
text=f"Error: {str(e)}"
)]