|
| 1 | + |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import argparse |
| 5 | +from dotenv import load_dotenv |
| 6 | + |
| 7 | +# Redirect stdout to stderr immediately to avoid polluting an MCP channel |
| 8 | +original_stdout = sys.stdout |
| 9 | +sys.stdout = sys.stderr |
| 10 | + |
| 11 | +from hiero_sdk_python import Client, Network, AccountId, PrivateKey |
| 12 | +from hedera_agent_kit.mcp import HederaMCPToolkit |
| 13 | +from hedera_agent_kit.shared.configuration import Configuration, Context |
| 14 | +from hedera_agent_kit.plugins import ( |
| 15 | + core_token_plugin, |
| 16 | + core_account_plugin, |
| 17 | + core_consensus_plugin, core_consensus_query_plugin, |
| 18 | +) |
| 19 | + |
| 20 | +def log(message: str, level: str = "info"): |
| 21 | + prefix = "❌" if level == "error" else "⚠️" if level == "warn" else "✅" |
| 22 | + sys.stderr.write(f"{prefix} {message}\n") |
| 23 | + |
| 24 | +def parse_args(): |
| 25 | + parser = argparse.ArgumentParser(description="Hedera MCP Server") |
| 26 | + parser.add_argument("--tools", type=str, help="Comma-separated list of tools to enable") |
| 27 | + parser.add_argument("--agent-mode", type=str, choices=["autonomous", "returnBytes"], default="autonomous", help="Agent mode") |
| 28 | + parser.add_argument("--account-id", type=str, help="Hedera Account ID") |
| 29 | + parser.add_argument("--public-key", type=str, help="Public Key") |
| 30 | + parser.add_argument("--ledger-id", type=str, choices=["testnet", "mainnet"], default="testnet", help="Ledger ID") |
| 31 | + return parser.parse_args() |
| 32 | + |
| 33 | +def main(): |
| 34 | + load_dotenv(os.path.join(os.getcwd(), ".env")) # or parent .env |
| 35 | + load_dotenv(os.path.join(os.path.dirname(os.getcwd()), ".env")) |
| 36 | + |
| 37 | + args = parse_args() |
| 38 | + |
| 39 | + # Client setup |
| 40 | + if args.ledger_id == "mainnet": |
| 41 | + network: Network = Network( |
| 42 | + network="mainnet" |
| 43 | + ) |
| 44 | + client: Client = Client(network) |
| 45 | + log("Using Mainnet", "info") |
| 46 | + else: |
| 47 | + network: Network = Network( |
| 48 | + network="testnet" |
| 49 | + ) |
| 50 | + client: Client = Client(network) |
| 51 | + log("Using Testnet", "info") |
| 52 | + |
| 53 | + operator_id = os.getenv("HEDERA_OPERATOR_ID") |
| 54 | + operator_key = os.getenv("HEDERA_OPERATOR_KEY") |
| 55 | + |
| 56 | + if operator_id and operator_key: |
| 57 | + try: |
| 58 | + client.set_operator(AccountId.from_string(operator_id),PrivateKey.from_string(operator_key)) |
| 59 | + log(f"Operator set: {operator_id}", "info") |
| 60 | + except Exception as e: |
| 61 | + log(f"Failed to set operator: {e}", "error") |
| 62 | + raise |
| 63 | + else: |
| 64 | + log("No operator credentials found in environment variables", "warn") |
| 65 | + |
| 66 | + context = Context( |
| 67 | + account_id=operator_id, |
| 68 | + account_public_key=PrivateKey.from_string(operator_key).public_key().to_string(), |
| 69 | + mode=args.agent_mode |
| 70 | + ) |
| 71 | + |
| 72 | + tools_list = args.tools.split(",") if args.tools and args.tools != "all" else None |
| 73 | + |
| 74 | + config = Configuration( |
| 75 | + tools=tools_list, |
| 76 | + context=context, |
| 77 | + plugins=[core_token_plugin, core_account_plugin, core_consensus_plugin, core_consensus_query_plugin] |
| 78 | + ) |
| 79 | + |
| 80 | + server = HederaMCPToolkit(client, config) |
| 81 | + |
| 82 | + sys.stdout = original_stdout |
| 83 | + |
| 84 | + log("Hedera MCP Server running on stdio", "info") |
| 85 | + # Run the server |
| 86 | + server.run() |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + try: |
| 90 | + main() |
| 91 | + except Exception as e: |
| 92 | + log(f"Error initializing Hedera MCP server: {str(e)}", "error") |
| 93 | + sys.exit(1) |
0 commit comments