Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 11: MCP Server

The nodalync-mcp crate provides an MCP (Model Context Protocol) server that enables AI assistants like Claude to query knowledge from a local Nodalync node.

Quick Start

1. Build the CLI

cargo build --release -p nodalync-cli

2. Initialize a Node

./target/release/nodalync init

3. Configure Claude Desktop

Add to your Claude Desktop MCP config (typically ~/.config/claude/mcp.json on macOS/Linux):

{
  "mcpServers": {
    "nodalync": {
      "command": "/path/to/nodalync",
      "args": ["mcp-server", "--budget", "1.0", "--auto-approve", "0.01"]
    }
  }
}

4. Restart Claude Desktop

Quit and reopen Claude Desktop to load the MCP server.

CLI Usage

# Start MCP server with defaults (1 HBAR budget, 0.01 auto-approve)
nodalync mcp-server

# Custom budget and auto-approve threshold
nodalync mcp-server --budget 5.0 --auto-approve 0.1

Options

FlagDefaultDescription
--budget, -b1.0Total session budget in HBAR
--auto-approve, -a0.01Auto-approve queries under this HBAR amount

MCP Tools

When the MCP server is running, AI agents have access to these tools:

ToolDescription
query_knowledgeQuery content by hash or natural language (paid)
list_sourcesBrowse available content with metadata
search_networkSearch connected peers for content (requires --enable-network)
preview_contentView content metadata without paying
publish_contentPublish new content from the agent
synthesize_contentCreate L3 synthesis from multiple sources
update_contentCreate a new version of existing content
delete_contentDelete content and set visibility to offline
set_visibilityChange content visibility
list_versionsList all versions of a content item
get_earningsView earnings breakdown by content
statusNode health, budget, channels, and Hedera status
deposit_hbarDeposit HBAR to the settlement contract
open_channelOpen a payment channel with a peer
close_channelClose a payment channel
close_all_channelsClose all open payment channels

Note: Natural language queries are not yet supported for query_knowledge. Use list_sources or search_network to discover content hashes first.

MCP Resources

knowledge://{hash}

Direct content access by hash. Use list_sources to discover available hashes.

URI Format: knowledge://<base58-encoded-hash>

Example:

knowledge://5dY7Kx9mT2...

Returns the content directly. Payment is handled automatically from session budget.

Architecture

┌──────────────┐     stdio      ┌─────────────────┐
│ Claude       │ ◄────────────► │ nodalync        │
│ Desktop      │     MCP        │ mcp-server      │
└──────────────┘                └────────┬────────┘
                                         │
                        ┌────────────────┼────────────────┐
                        │                │                │
                        ▼                ▼                ▼
                ┌─────────────┐  ┌─────────────┐  ┌─────────────┐
                │ nodalync-   │  │ nodalync-   │  │ Event Loop  │
                │ store       │  │ net         │  │ (background)│
                │ (local)     │  │ (P2P)       │  │             │
                └─────────────┘  └─────────────┘  └─────────────┘

Event Processing

When --enable-network is used, the MCP server spawns a background event loop that processes incoming network events (e.g., ChannelAccept messages). This enables full payment channel lifecycle support:

  1. Channel Open: Server sends ChannelOpen to peer
  2. Event Loop: Receives ChannelAccept from peer
  3. State Transition: Channel moves from OpeningOpen
  4. Payments: Channel is ready for micropayments

Budget System

The budget system prevents runaway spending:

  1. Session Budget: Total HBAR available for the session
  2. Auto-Approve Threshold: Queries below this cost are approved automatically
  3. Atomic Tracking: Thread-safe spending with compare_exchange
#![allow(unused)]
fn main() {
// Budget is tracked atomically
pub fn try_spend(&self, amount: Amount) -> Result<Amount, McpError> {
    // Atomic compare-and-swap ensures thread safety
}
}

Error Handling

ErrorCauseResolution
BudgetExceededQuery cost > remaining budgetIncrease budget or use smaller queries
ContentNotFoundHash doesn’t exist locallyEnsure content is published
StorageErrorDatabase issuesCheck permissions, disk space

Testing

# Run MCP crate tests
cargo test -p nodalync-mcp

# Test server manually
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | ./target/release/nodalync mcp-server