#!/usr/bin/env node /** * `hfmarketdata-mcp` — stdio entry point. * * HFMD_API_KEY=hfmd_live_… hfmarketdata-mcp * hfmarketdata-mcp --version | --help | --selftest * * All logs go to stderr (stdout is the MCP channel). */ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { HfmdClient } from './client.js'; import { createServer, SERVER_VERSION } from './server.js'; import { TOOL_NAMES } from './tools.js'; const argv = process.argv.slice(2); if (argv.includes('--version') || argv.includes('-v')) { console.log(SERVER_VERSION); process.exit(0); } if (argv.includes('--help') || argv.includes('-h')) { console.log(`hfmarketdata-mcp ${SERVER_VERSION} — MCP server for https://www.hfmarketdata.io Usage: hfmarketdata-mcp start the stdio MCP server (used by Claude Code, Cursor, Codex…) hfmarketdata-mcp --selftest call /v1/status and print the rate-limit headroom Env: HFMD_API_KEY optional bearer key (free account: 120 req/min; keyless: 30 req/h per IP) HFMD_BASE_URL API base URL (default https://www.hfmarketdata.io) Tools: ${TOOL_NAMES.join(', ')} Resources: hfmarketdata://status, hfmarketdata://limits Prompts: term-structure-analysis, compare-contracts, fundamentals-snapshot`); process.exit(0); } if (argv.includes('--selftest')) { const client = new HfmdClient(); client .get>('/v1/status') .then((r) => { const datasets = (r.body?.datasets ?? {}) as Record; console.log(`OK ${client.baseUrl} (${r.ms} ms, ${client.keyless ? 'keyless' : 'api key set'}) — datasets: ${Object.keys(datasets).join(', ') || 'n/a'}`); console.log(JSON.stringify(r.rate)); process.exit(0); }) .catch((e) => { console.error(String(e?.describe?.() ?? e)); process.exit(1); }); } else { const { server, client } = createServer(); const transport = new StdioServerTransport(); server .connect(transport) .then(() => console.error(`hfmarketdata-mcp ${SERVER_VERSION} ready on stdio — ${client.baseUrl} (${client.keyless ? 'keyless, 30 req/h' : 'API key set'})`)) .catch((e) => { console.error('hfmarketdata-mcp failed to start:', e); process.exit(1); }); const shutdown = () => { server.close().finally(() => process.exit(0)); }; process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); }