-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathagent-reputation.js
More file actions
225 lines (212 loc) · 8.19 KB
/
Copy pathagent-reputation.js
File metadata and controls
225 lines (212 loc) · 8.19 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// `agent_reputation` — paid MCP tool that reads ERC-8004 reputation for an
// agent (by agentId, EVM wallet, or "eip155:<chain>:<wallet>" CAIP-10 ID).
//
// Pricing: $0.01 USDC, settled `exact` in USDC on Solana mainnet.
//
// All reads are made directly against the canonical ERC-8004 reference
// deployments via ethers JsonRpcProvider — no third-party indexers, no
// cached snapshots, no fallback values. By default we query Base; the
// caller can override with `chain: "ethereum"|"base"|"arbitrum"|"optimism"|"polygon"|"bsc"`
// or pass a numeric chainId.
//
// The result includes:
// - aggregate reputation (average + count) via getReputation. The contract
// returns (int256 avgX100, uint256 count): the average ALREADY ×100, signed
// so reputation can be negative. We divide by 100 — never by count — and
// preserve the sign.
// - total ETH staked on the agent's vouches via getTotalStake
// - recent FeedbackSubmitted + ReputationStaked events (latest 25)
// - the agent's URI + wallet (Identity Registry) when resolvable
//
// All numeric responses are returned both as decimal strings (for safe
// integer transport) and as parsed Number where the value fits in float64.
import { Contract, isAddress, ZeroAddress } from 'ethers';
import { z } from 'zod';
import { paid, toolError } from '../payments.js';
import { jsonSchemaFromZod } from './_shared.js';
import { makeEvmProvider, getEvmRpcUrls } from '../lib/evm-rpc.js';
import {
IDENTITY_REGISTRY_ABI,
IDENTITY_REGISTRY_MAINNET,
REPUTATION_REGISTRY_ABI,
REPUTATION_REGISTRY_MAINNET,
readReputationAggregate,
resolveAgentId,
resolveChain,
} from '../lib/erc8004.js';
const TOOL_NAME = 'agent_reputation';
const TOOL_DESCRIPTION =
'ERC-8004 on-chain reputation for an agent: aggregate score + count + average from the canonical ReputationRegistry, total ETH staked on vouches, and the latest ReputationSubmitted/ReputationStaked events. Resolves agentId from a wallet via IdentityRegistry when needed. Reads default to Base; switch chains via "chain". Paid: $0.01 USDC.';
// Parse the agent identifier. Accepts: numeric ID, EVM wallet, or CAIP-10
// "eip155:<chainId>:<address>" (which can also override the chain selection).
function parseAgentInput(raw, defaultChain) {
const value = String(raw || '').trim();
if (!value) throw new Error('agent identifier is required');
if (/^\d+$/.test(value)) {
return { kind: 'agentId', agentId: BigInt(value), chain: defaultChain };
}
if (value.startsWith('eip155:')) {
const parts = value.split(':');
if (parts.length !== 3) throw new Error(`invalid CAIP-10 ID "${value}"`);
const chain = resolveChain(parts[1]);
const addr = parts[2];
if (!isAddress(addr)) throw new Error(`invalid wallet in CAIP-10 ID "${value}"`);
return { kind: 'wallet', wallet: addr, chain };
}
if (isAddress(value)) {
return { kind: 'wallet', wallet: value, chain: defaultChain };
}
throw new Error(
`could not parse agent identifier "${value}" — expected uint, EVM wallet, or eip155:<chain>:<addr>`,
);
}
async function readIdentity(provider, agentId) {
const id = new Contract(IDENTITY_REGISTRY_MAINNET, IDENTITY_REGISTRY_ABI, provider);
const [owner, agentWallet, uri] = await Promise.allSettled([
id.ownerOf(agentId),
id.getAgentWallet(agentId),
id.tokenURI(agentId),
]);
return {
owner: owner.status === 'fulfilled' ? owner.value : null,
agentWallet: agentWallet.status === 'fulfilled' ? agentWallet.value : null,
uri: uri.status === 'fulfilled' ? uri.value : null,
errors: [owner, agentWallet, uri]
.filter((r) => r.status === 'rejected')
.map((r) => r.reason?.message || String(r.reason)),
};
}
// Walk the last LOG_WINDOW_BLOCKS blocks (configurable) for recent vouches.
// On chains where the registry has been quiet, this can return an empty
// array — that's the truth, not a failure.
const LOG_WINDOW_BLOCKS = Number(process.env.MCP_AGENT_REP_LOG_WINDOW || 200_000);
async function readRecentEvents(provider, agentId) {
const rep = new Contract(REPUTATION_REGISTRY_MAINNET, REPUTATION_REGISTRY_ABI, provider);
const latest = await provider.getBlockNumber();
const from = Math.max(0, latest - LOG_WINDOW_BLOCKS);
const [submitted, staked] = await Promise.all([
rep.queryFilter(rep.filters.FeedbackSubmitted(agentId), from, latest),
rep.queryFilter(rep.filters.ReputationStaked(agentId), from, latest),
]);
const submittedDecoded = submitted.map((e) => ({
kind: 'submitted',
blockNumber: e.blockNumber,
txHash: e.transactionHash,
submitter: e.args?.from,
score: Number(e.args?.score),
comment: e.args?.uri || '',
}));
const stakedDecoded = staked.map((e) => ({
kind: 'staked',
blockNumber: e.blockNumber,
txHash: e.transactionHash,
staker: e.args?.staker,
score: Number(e.args?.score),
valueWei: e.args?.value?.toString?.() || '0',
}));
return {
windowBlocks: LOG_WINDOW_BLOCKS,
fromBlock: from,
toBlock: latest,
events: [...submittedDecoded, ...stakedDecoded]
.sort((a, b) => b.blockNumber - a.blockNumber)
.slice(0, 25),
};
}
// Single source of truth: Zod shape with descriptions; JSON Schema derived.
const inputZodShape = {
address: z
.string()
.min(1)
.describe(
'ERC-8004 agentId (uint), EVM wallet address (0x...), or CAIP-10 "eip155:<chainId>:<wallet>".',
),
chain: z
.string()
.describe(
'Chain to query (default: base). Accepts name or numeric chainId. Overridden by CAIP-10 input.',
)
.optional(),
};
const inputJsonSchema = jsonSchemaFromZod(inputZodShape);
export async function buildAgentReputationTool() {
const handler = await paid(
{
toolName: TOOL_NAME,
description: TOOL_DESCRIPTION,
scheme: 'exact',
priceUsd: '$0.01',
inputSchema: inputJsonSchema,
example: { address: '1', chain: 'base' },
outputExample: {
chain: 'base',
agentId: '1',
identity: { owner: '0x...', agentWallet: '0x...', uri: 'ipfs://...' },
reputation: { averageX100: '420', average: 4.2, count: '6', totalStakeWei: '0' },
events: [{ kind: 'submitted', score: 5, submitter: '0x...', comment: '' }],
},
},
async ({ address, chain }) => {
const defaultChain = resolveChain(chain);
const parsed = parseAgentInput(address, defaultChain);
// Endpoint failover: an operator override (MCP_AGENT_REP_RPC_<id>) is
// tried first, then the chain's built-in redundant public endpoints,
// each with a bounded request timeout. A single RPC outage no longer
// fails the lookup or hangs the paid call.
const overrides = [process.env[`MCP_AGENT_REP_RPC_${parsed.chain.id}`]].filter(Boolean);
const provider = makeEvmProvider(parsed.chain.id, { overrides, timeoutMs: 12_000 });
let agentId = parsed.kind === 'agentId' ? parsed.agentId : null;
let walletResolved = parsed.kind === 'wallet' ? parsed.wallet : null;
if (!agentId) {
agentId = await resolveAgentId(provider, walletResolved);
if (!agentId) {
return toolError(
'no_agent_registered_for_wallet',
`no ERC-8004 agent is registered for ${walletResolved} on ${parsed.chain.name}`,
{
chain: parsed.chain.name,
chainId: parsed.chain.id,
input: address,
resolvedWallet: walletResolved,
identityRegistry: IDENTITY_REGISTRY_MAINNET,
reputationRegistry: REPUTATION_REGISTRY_MAINNET,
},
);
}
}
const [identity, reputation, events] = await Promise.all([
readIdentity(provider, agentId),
readReputationAggregate(provider, agentId),
readRecentEvents(provider, agentId),
]);
const isZero = identity.owner === ZeroAddress;
return {
chain: parsed.chain.name,
chainId: parsed.chain.id,
agentId: agentId.toString(),
agentRegistry: `eip155:${parsed.chain.id}:${IDENTITY_REGISTRY_MAINNET}`,
reputationRegistry: REPUTATION_REGISTRY_MAINNET,
identity: isZero ? null : identity,
reputation,
events,
rpc: getEvmRpcUrls(parsed.chain.id, overrides),
fetchedAt: new Date().toISOString(),
};
},
);
return {
name: TOOL_NAME,
title: 'Agent reputation ($0.01)',
description: TOOL_DESCRIPTION,
inputSchema: inputZodShape,
// Read-only on-chain lookup — reputation events accrue between calls,
// so not idempotent.
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
},
handler,
};
}