File size: 2,150 Bytes
af6094d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastmcp import FastMCP
from crypto_engine import hash_tool, worm_write_tool, proof_generate_tool, verify_proof_tool
import json

# Initialize the MCP server
mcp = FastMCP("Secure Reasoning Server")


@mcp.tool()
def hash_data(data: str) -> str:
    """
    Hash a string or JSON data using SHA-256.
    Input: data (string or JSON-serializable object as string)
    Output: SHA-256 hex digest
    """
    return hash_tool(data)


@mcp.tool()
def write_to_worm(step_data: str, hash_value: str, merkle_root: str) -> str:
    """
    Write a step record to WORM (Write Once, Read Many) storage.
    Input: step_data (JSON string), hash_value (hex string), merkle_root (hex string)
    Output: JSON record with id, timestamp, step, hash, and root
    """
    step_dict = json.loads(step_data) if isinstance(step_data, str) else step_data
    record = worm_write_tool(step_dict, hash_value, merkle_root)
    return json.dumps(record)


@mcp.tool()
def generate_proof(record_id: int) -> str:
    """
    Generate a Merkle proof for a specific record in the WORM log.
    Input: record_id (integer, the ID of the record)
    Output: JSON containing record_id, hash, merkle_proof, merkle_root, timestamp, and step_details
    """
    proof = proof_generate_tool(record_id)
    if proof is None:
        return json.dumps({"error": f"Record with ID {record_id} not found"})
    return json.dumps(proof)


@mcp.tool()
def verify_proof(target_hash: str, merkle_proof: str, merkle_root: str) -> str:
    """
    Verify if a target_hash belongs to the merkle_root using the merkle_proof.
    Input: target_hash (hex string), merkle_proof (JSON string of proof array), merkle_root (hex string)
    Output: JSON with result (true/false) and verification status message
    """
    proof_list = json.loads(merkle_proof) if isinstance(merkle_proof, str) else merkle_proof
    is_valid = verify_proof_tool(target_hash, proof_list, merkle_root)
    return json.dumps({
        "verified": is_valid,
        "message": "Proof verified successfully" if is_valid else "Proof verification failed - possible tampering"
    })


if __name__ == "__main__":
    mcp.run()