> ## Documentation Index
> Fetch the complete documentation index at: https://docs.snack.money/llms.txt
> Use this file to discover all available pages before exploring further.

# Model Context Protocol (MCP)

> Use MCP protocol with x402 payments for AI agent tool calling

## What is MCP?

The **Model Context Protocol (MCP)** is an open protocol that standardizes how AI systems interact with external tools and data sources. Snack Money implements MCP with x402 payment support, enabling AI agents to make paid API calls seamlessly.

## How It Works

MCP uses JSON-RPC 2.0 for communication. When an AI agent calls a Snack Money tool through MCP:

1. **Tool Discovery** - Agent queries available tools via `tools/list`
2. **Tool Invocation** - Agent calls a tool with `tools/call`
3. **Payment Required** - Server returns error code `-32402` with payment requirements
4. **Payment Signing** - Agent signs the payment using x402 protocol
5. **Retry with Payment** - Agent retries with payment proof
6. **Success** - Server processes the payment and returns results

## MCP Payment Flow

```
Client                           Snack Money API
  |                                    |
  |-------- tools/call -------->      |
  |        (no payment)               |
  |                                    |
  |<------ Error -32402 --------      |
  |     (payment required)            |
  |                                    |
  |-- Sign with x402 client -->       |
  |                                    |
  |-------- tools/call -------->      |
  |     (with payment proof)          |
  |                                    |
  |<-------- Success ----------       |
  |      (with receipt)               |
```

## Available Tools

Snack Money provides the following MCP tools, each requiring x402 payment:

| Tool            | Description                      | Parameters                          |
| --------------- | -------------------------------- | ----------------------------------- |
| `pay_farcaster` | Send USDC to a Farcaster user    | `receiver`, `amount`, `description` |
| `pay_twitter`   | Send USDC to a Twitter/X user    | `receiver`, `amount`, `description` |
| `pay_email`     | Send USDC to an email address    | `receiver`, `amount`, `description` |
| `pay_web`       | Send USDC to a web domain owner  | `receiver`, `amount`, `description` |
| `pay_github`    | Send USDC to a GitHub user       | `receiver`, `amount`, `description` |
| `batch_pay`     | Send USDC to multiple recipients | `payments[]`                        |

## Quick Start

### 1. Install Dependencies

<CodeGroup>
  ```bash npm theme={null}
  npm install @x402/core @x402/evm @x402/svm
  ```

  ```bash yarn theme={null}
  yarn add @x402/core @x402/evm @x402/svm
  ```
</CodeGroup>

### 2. Initialize MCP Session

```javascript theme={null}
const response = await fetch('https://api.snack.money/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'initialize',
    params: {
      protocolVersion: '0.1.0',
      clientInfo: { name: 'my-agent', version: '1.0.0' }
    },
    id: 1
  })
});
```

### 3. List Available Tools

```javascript theme={null}
const response = await fetch('https://api.snack.money/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'tools/list',
    id: 2
  })
});

const { result } = await response.json();
console.log('Available tools:', result.tools);
```

### 4. Call a Tool with Payment

```javascript theme={null}
import { X402 } from '@x402/core';
import { createEvmClient } from '@x402/evm';

// Initialize x402 client
const x402Client = createEvmClient({
  privateKey: process.env.EVM_PRIVATE_KEY,
  chainId: 8453 // Base mainnet
});

// First call - will require payment
const firstCall = await fetch('https://api.snack.money/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'tools/call',
    params: {
      name: 'pay_twitter',
      arguments: {
        receiver: 'alice',
        amount: 0.01,
        description: 'Thanks for the help!'
      }
    },
    id: 3
  })
});

const response = await firstCall.json();

// Check if payment is required
if (response.error?.code === -32402) {
  const { taskId, paymentRequired } = response.error.data;

  // Sign payment
  const signedPayment = await x402Client.createPaymentPayload(
    paymentRequired
  );

  // Retry with payment
  const paidCall = await fetch('https://api.snack.money/mcp', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      method: 'tools/call',
      params: {
        name: 'pay_twitter',
        arguments: {
          receiver: 'alice',
          amount: 0.01,
          description: 'Thanks for the help!'
        },
        _meta: {
          taskId,
          x402Payment: signedPayment
        }
      },
      id: 4
    })
  });

  const result = await paidCall.json();
  console.log('Payment successful:', result.result);
}
```

## Full Example

Check out the complete [MCP example](https://github.com/snack-money/snack-money-examples/tree/main/mcp) in our GitHub repository for:

* Base (EVM) network implementation
* Solana network implementation
* Tool listing and discovery
* Batch payment support
* Error handling

## Integration with AI Agents

### Coinbase CDP Payments MCP

Snack Money's MCP implementation is compatible with [Coinbase CDP Payments MCP](https://docs.cdp.coinbase.com/payments-mcp/welcome), allowing you to:

* Use Snack Money as a payment provider in CDP-powered AI agents
* Send payments to social media users through CDP's MCP interface
* Integrate with Coinbase's wallet infrastructure
* Leverage CDP's security and compliance features

For detailed integration instructions, see the [CDP Payments MCP documentation](https://docs.cdp.coinbase.com/payments-mcp/welcome).

### Other AI Development Tools

Popular AI development tools that support MCP include:

* **Claude Desktop** - Native MCP support for tool calling
* **Continue.dev** - VS Code extension with MCP support
* **Cline** - Command-line interface for MCP tools
* **Custom Agents** - Build your own using the MCP SDK

## Security Considerations

* All payments require cryptographic signatures via x402
* Task IDs prevent replay attacks
* Payments are verified on-chain before processing
* Rate limiting applies to prevent abuse

## Related Documentation

* [x402 Protocol](/introduction/x402) - Learn about the payment protocol
* [A2A Protocol](/introduction/a2a) - Agent-to-Agent communication
* [API Reference](/api-reference/introduction) - Full API documentation
