Sponsored by Deepsite.site

Blockchain CLI in Rust

Created By
FaustoS889 months ago
A simple blockchain implementation in Rust featuring SHA-256 hashing, CLI interface, and MCP server integration. Demonstrates core blockchain concepts through block creation and chain validation while exposing blockchain functionalities over a network.
Content

Blockchain CLI in Rust

This repository contains a simple blockchain demonstration implemented in Rust. The project illustrates core Rust principles through a minimal command-line application and integrates an MCP server to expose blockchain functionalities over a network.

Overview

The application implements a basic blockchain where each block includes:

  • Index: The block's position in the chain.
  • Timestamp: The creation time in ISO 8601 format.
  • Data: User-supplied content.
  • Previous Hash: The SHA-256 hash of the previous block.
  • Current Hash: The SHA-256 hash computed from the block's contents.

A menu-driven CLI lets you:

  • Add a new block.
  • Print the current blockchain.
  • Validate the blockchain's integrity.

Technologies

  • Rust: A systems programming language focused on safety and performance.
  • sha2: For computing SHA-256 hashes.
  • chrono: For accurate timestamping using UTC time.
  • serde & serde_json: For JSON serialization (used in MCP communication).

Getting Started

Prerequisites

Build and Run

To create and run the project using Cargo, execute in your terminal:

cargo new blockchain_app
cd blockchain_app && cargo run

Note: If you cloned this repository, simply navigate into the project folder and run cargo run.

Usage

When you run the application, the CLI displays the following options:

  1. Add Block: Input data to create a new block.
  2. Print Blockchain: Display all blocks in the blockchain.
  3. Validate Blockchain: Verify that each block is properly linked.
  4. Exit: Close the application.

MCP Server Integration

The project includes an MCP server that runs concurrently with the CLI. It listens on 127.0.0.1:7878 and allows external clients to interact with the blockchain using JSON commands.

Available MCP Commands

  • latest_block: Retrieves the latest block in the blockchain.

    • Example:
      echo '{"command": "latest_block"}' | nc 127.0.0.1 7878
      
    • Sample Output:
      {"status":"ok","message":"Latest block retrieved","block":{"index":0,"timestamp":"2025-02-26T10:06:59.931506+00:00","data":"Genesis Block","previous_hash":"0","hash":"f465878acf7be88a124fd7ebc9a3640793612023828d9af8ad237eefd4a1a22e"}}
      
  • add_block: Adds a new block with the provided data.

    • Example:
      echo '{"command": "add_block", "data": "sample transaction"}' | nc 127.0.0.1 7878
      
    • Sample Output:
      {"status":"ok","message":"Block added","block":null}
      

Testing the MCP Server

  1. Run the Application:
    Open a terminal, navigate to the project directory (blockchain_app), and run:

    cargo run
    

    You will see the CLI menu and a message indicating that the MCP server is listening on 127.0.0.1:7878.

  2. Query from a Separate Terminal:
    Open a new terminal window and test the MCP commands using netcat (nc). For example:

    • To retrieve the latest block:
      echo '{"command": "latest_block"}' | nc 127.0.0.1 7878
      
    • To add a new block:
      echo '{"command": "add_block", "data": "sample transaction"}' | nc 127.0.0.1 7878
      

Note: The MCP server runs in a separate thread alongside the CLI, so ensure you use a different terminal window for these tests.

Summary

This project demonstrates how to combine blockchain logic with an externally accessible MCP server in Rust. It serves as a practical example for networked tool integration.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Serper MCP ServerA Serper MCP Server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
WindsurfThe new purpose-built IDE to harness magic
ChatWiseThe second fastest AI chatbot™
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
DeepChatYour AI Partner on Desktop
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
CursorThe AI Code Editor
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Tavily Mcp
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Amap Maps高德地图官方 MCP Server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code