@iqai/mcp-near-agent
Version:
Mcp server for Near
166 lines (127 loc) âĸ 5.32 kB
Markdown
A Model Context Protocol (MCP) server enabling smart contract interaction, transaction handling, and event listening on the NEAR blockchain for AI agents and applications.
## đ Overview
This MCP server provides seamless integration with the NEAR Protocol blockchain for any MCP-compatible client or agent framework.
- â
Execute contract methods and transactions on NEAR blockchain
- â
Listen to and respond to contract events with AI processing
- â
View contract data and account information
- â
Handle custom logic through intelligent event listeners
- â
Compatible with any MCP client (Claude Desktop, Cursor, custom agents)
## đ AI-Driven Event Processing Workflow
The server enables an "AI in the loop" workflow:
1. đ Smart contract transaction triggers an event and pauses execution
2. đ¤ MCP server detects the event and requests AI processing from the client
3. đ§ AI client processes the event data and provides intelligent response
4. âŠī¸ Server sends AI response back to blockchain via transaction
5. â
Original smart contract resumes with the AI-provided data
## đ Installation
### Option 1: Using `pnpm dlx` (Recommended)
Run directly without installation:
```bash
pnpm dlx mcp-near
```
### Option 2: Global Installation
```bash
pnpm add -g mcp-near
```
### Option 3: From Source
```bash
git clone <repository_url>
cd mcp-near
pnpm install
pnpm run build
```
## â Configuration
Set these environment variables in your MCP client configuration:
| đ§ Variable Name | đ Description |
|------------------|----------------|
| `ACCOUNT_ID` | Your NEAR account ID for authentication đ |
| `ACCOUNT_KEY` | Private key for your NEAR account (ed25519: or secp256k1: format) đ |
| `NEAR_NETWORK_ID` | NEAR network ("mainnet", "testnet", "betanet") - defaults to "mainnet" đ |
| `NEAR_NODE_URL` | Custom NEAR RPC endpoint (optional) đ |
| `NEAR_GAS_LIMIT` | Gas limit for transactions (optional) âŊ |
## đ MCP Client Configuration
### Custom Agent Framework
```typescript
import { MCPClient } from "your-mcp-client";
const client = new MCPClient({
serverCommand: "pnpm",
serverArgs: ["dlx", "mcp-near"],
serverEnv: {
ACCOUNT_ID: "your-account.testnet",
ACCOUNT_KEY: "ed25519:your_private_key_here",
NEAR_NETWORK_ID: "testnet"
}
});
```
Start watching for specific events on a NEAR contract:
```typescript
{
eventName: "run_agent", // Event to watch for
contractId: "contract.testnet", // Contract to monitor
responseMethodName: "agent_response", // Method to call with AI response
cronExpression: "*/10 * * * * *" // Optional: polling frequency
}
```
Stop watching for specific events:
```typescript
{
contractId: "contract.testnet",
eventName: "run_agent"
}
```
List all currently watched events and statistics:
```typescript
{
includeStats: true // Optional: include performance statistics
}
```
1. **Start the MCP server** with your client
2. **Watch for events** using the MCP tool:
```
Use watch_near_event with:
- eventName: "price_request"
- contractId: "oracle.testnet"
- responseMethodName: "price_response"
```
3. **AI processes events automatically** when they occur on the blockchain
4. **Monitor with** `list_watched_near_events` to see status and statistics
When a blockchain event is detected:
1. đĄ **Event Detection**: Server monitors blockchain for specified events
2. đ¤ **AI Request**: Server requests sampling from MCP client with event data
3. đ§ **AI Processing**: Client processes event and returns intelligent response
4. đ¤ **Blockchain Response**: Server sends AI response back to contract
5. đ **Statistics**: Performance metrics are tracked and available
The server provides structured responses:
- â **Success/failure status** with detailed messages
- đ **Subscription IDs** for tracking active watchers
- đ **Performance statistics** (success rates, processing times)
- đ¯ **Event details** (contract, event type, timestamps)
- đĄ **Helpful guidance** and troubleshooting tips
The server handles common NEAR-related errors:
- đ¨ **Invalid contract calls** or method names
- đ¸ **Insufficient account balance** for transactions
- đ **Authentication issues** with account credentials
- đ **Network connectivity problems** with NEAR RPC
- đĢ **Contract execution errors** returned by smart contracts
- âąī¸ **Timeout handling** for long-running operations
- **Real-time logging** of all blockchain interactions
- **Performance metrics** for event processing
- **Error tracking** with detailed error messages
- **Statistics dashboard** via `list_watched_near_events`
- **Private keys** are handled securely in memory only
- **Environment variables** should be properly secured
- **Gas limits** prevent runaway transaction costs
- **Error handling** prevents sensitive data leakage
This MCP server is designed to work with any MCP-compatible client or agent framework. Contributions welcome!