// Mastra (TypeScript/JS) trading-research agent with the YourAITools market-data tools as remote MCP servers.
// Read-only market analytics for trading agents: no trading, no custody, no keys. The free MCP tier returns
// previews; each answer names the paid x402 endpoint (USDC) for the full data. Not financial advice.
//
// npm i
// npm run smoke # list tools + one free call per server (no LLM, no wallet)
// OPENAI_API_KEY=sk-... node trading-agent.mjs "Is the ETH perp crowded short?"
//
// Pick servers with TOOLS=perpradar,smartperps. MODEL defaults to "openai/gpt-4.1-mini" (any Mastra model string).
import { MCPClient } from '@mastra/mcp';
const BASE = process.env.YAT_BASE || 'https://api.youraitools.co';
const ALL = ['perpradar', 'tasignal', 'smartperps', 'tokenverdict', 'solverdict', 'payguard'];
const PICK = (process.env.TOOLS || ALL.join(',')).split(',').map((s) => s.trim()).filter(Boolean);
const SMOKE = {
perpradar: { symbol: 'BTC' },
tasignal: { symbol: 'BTC', timeframe: '1h' },
smartperps: { symbol: 'ETH' },
tokenverdict: { token: '0x532f27101965dd16442E59d40670FaF5eBB142E4' },
solverdict: { mint: 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263' },
payguard: { address: '0x098B716B8Aaf21512996dC57EB0615e2383E2f96' },
};
const INSTRUCTIONS = `You are a careful crypto market research assistant for a trading agent.
Use the tools to gather facts (funding, open interest, indicators, smart-money positioning, token safety)
and report them plainly with the numbers and timestamps the tools return. Never place trades, never tell
the user what to buy or sell, never promise returns. Say clearly when data is stale, partial or unavailable.
This is not financial advice.`;
const mcp = new MCPClient({
id: 'youraitools-trading',
servers: Object.fromEntries(PICK.map((t) => [t, { url: new URL(`${BASE}/${t}/mcp`) }])),
timeout: 30000,
});
const text = (r) => {
const c = r?.content ?? r;
const s = Array.isArray(c) ? c.map((b) => b.text ?? '').join(' ') : typeof c === 'string' ? c : JSON.stringify(c);
return s.split('\n')[0].slice(0, 200);
};
try {
if (process.argv.includes('--smoke') || process.argv.includes('--list')) {
const sets = await mcp.getToolsets(); // { perpradar: { perp_positioning: tool }, ... }; Mastra 0.24 tools take execute({ context: args })
for (const t of PICK) {
const tools = sets[t] || {};
console.log(t.padEnd(13), Object.keys(tools).join(', '));
if (process.argv.includes('--smoke') && SMOKE[t]) {
const [name, tool] = Object.entries(tools)[0];
console.log(` ${name} ->`, text(await tool.execute({ context: SMOKE[t] }, {})));
}
}
} else {
const { Agent } = await import('@mastra/core/agent');
const agent = new Agent({ name: 'trading-research', instructions: INSTRUCTIONS, model: process.env.MODEL || 'openai/gpt-4.1-mini', tools: await mcp.getTools() });
const q = process.argv.slice(2).filter((a) => !a.startsWith('--')).join(' ') || 'Summarize BTC perp funding and smart-money positioning.';
console.log((await agent.generate(q)).text);
}
} finally {
await mcp.disconnect();
}
agentgate-source: code mirror on Cloudflare Pages. All repos