Claude Integration

Give a Claude agent autonomous USDC spending within your policy guardrails.

Overview

The SDK exports bithavenTools — an array of Claude-compatible tool definitions that you pass directly to the tools parameter in an Anthropic API call. Claude will autonomously decide when to check balances, send payments, and request approvals.

Setup

npm install @bithaven/mcp-sdk @anthropic-ai/sdk

Minimal Example

Pass bithavenTools to Claude and it will call them as needed:

import Anthropic from '@anthropic-ai/sdk';
import { bithavenTools } from '@bithaven/mcp-sdk';

const anthropic = new Anthropic();

const response = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1024,
  tools: bithavenTools,
  messages: [
    { role: 'user', content: 'Check my wallet balance' },
  ],
});

// Claude will call bithaven_check_balance via tool_use

Full Agent Loop

A production agent needs to execute tool calls and feed results back. Here's the complete pattern:

import Anthropic from '@anthropic-ai/sdk';
import {
  BithavenClient,
  bithavenTools,
  toolNameMap,
  PolicyDeniedError,
} from '@bithaven/mcp-sdk';

const bithaven = new BithavenClient({
  apiKey: process.env.BITHAVEN_API_KEY!,
});
const anthropic = new Anthropic();

// Execute a Bithaven tool call
async function executeTool(name: string, input: any): Promise<string> {
  const tool = toolNameMap[name];

  switch (tool) {
    case 'check_balance':
      return JSON.stringify(await bithaven.checkBalance());

    case 'send_payment':
      try {
        return JSON.stringify(await bithaven.sendPayment(input));
      } catch (err) {
        if (err instanceof PolicyDeniedError) {
          return JSON.stringify({
            error: 'Payment denied by policy',
            violations: err.violations,
            suggestion: 'Use bithaven_request_approval to ask for permission.',
          });
        }
        throw err;
      }

    case 'get_tx_history':
      return JSON.stringify(await bithaven.getTxHistory(input));

    case 'request_approval':
      return JSON.stringify(await bithaven.requestApproval(input));

    default:
      return JSON.stringify({ error: `Unknown tool: ${name}` });
  }
}

// Agent loop
async function runAgent(userMessage: string) {
  const messages: Anthropic.MessageParam[] = [
    { role: 'user', content: userMessage },
  ];

  while (true) {
    const response = await anthropic.messages.create({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1024,
      system:
        'You are an AI agent with a Bithaven USDC wallet. ' +
        'Check your balance before sending payments. ' +
        'If denied by policy, explain why and offer to request approval.',
      tools: bithavenTools as any,
      messages,
    });

    messages.push({ role: 'assistant', content: response.content });

    // Print text responses
    for (const block of response.content) {
      if (block.type === 'text') console.log(block.text);
    }

    // Execute any tool calls
    const toolUses = response.content.filter(b => b.type === 'tool_use');
    if (toolUses.length === 0) break; // Agent is done

    const results: Anthropic.ToolResultBlockParam[] = [];
    for (const tu of toolUses) {
      if (tu.type !== 'tool_use') continue;
      const result = await executeTool(tu.name, tu.input);
      results.push({
        type: 'tool_result',
        tool_use_id: tu.id,
        content: result,
      });
    }

    messages.push({ role: 'user', content: results });
  }
}

// Run
runAgent('Send 5 USDC to 0x742d...2bD68 for API credits');

Available Tools

The bithavenTools array exports four tools:

Tool NameScopeDescription
bithaven_check_balancereadBalance and spend caps
bithaven_send_paymentwriteSend USDC with policy evaluation
bithaven_get_tx_historyreadTransaction history
bithaven_request_approvalwriteEscalate to human

Environment Variables

BITHAVEN_API_KEY=bh_live_...    # From dashboard
ANTHROPIC_API_KEY=sk-ant-...   # From console.anthropic.com