@iqai/mcp-atp
Version:
Mcp server for ATP (IQAI's Agent Tokenization Platform) access
68 lines • 2.67 kB
JavaScript
import { DEFAULT_LIMIT, DEFAULT_PAGE } from "../config.js";
import { env } from "../config.js";
import { LogType, } from "../types.js";
export class AgentLogsService {
async getLogs({ agentTokenContract, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT, }) {
try {
const endPoint = `${env.ATP_API_URL}/logs`;
const queryParams = new URLSearchParams({
agentTokenContract,
page: page.toString(),
limit: limit.toString(),
});
const response = await fetch(`${endPoint}?${queryParams.toString()}`);
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
return (await response.json());
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to fetch agent logs: ${error.message}`);
}
throw new Error("Failed to fetch agent logs due to an unknown error.");
}
}
async addLog({ agentTokenContract, content, apiKey, txHash, chainId, }) {
try {
const endPoint = `${env.ATP_API_URL}/logs`;
const response = await fetch(endPoint, {
method: "POST",
headers: {
apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({
agentTokenContract,
content,
txHash,
chainId,
type: LogType.Agent,
}),
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
return (await response.json());
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to add agent log: ${error.message}`);
}
throw new Error("Failed to add agent log due to an unknown error.");
}
}
formatLogs(data) {
if (!data.logs.length) {
return "No logs found for this agent.";
}
const logsText = data.logs
.map((log) => {
const date = new Date(log.createdAt).toLocaleString();
return `[${date}] [${log.type}] ${log.content} \n Tx Hash: ${log.txHash} \n Chain ID: ${log.chainId}`;
})
.join("\n");
return `Agent Logs (Page ${data.page} of ${data.totalPages}, showing ${data.logs.length} of ${data.total} logs):\n${logsText}`;
}
}
//# sourceMappingURL=agent-logs.js.map