Skip to content
Docs
Get API key
MCP

Trading Economics MCP - Getting Started

Sample code for connecting to the Trading Economics MCP server from OpenAI (GPT), Anthropic (Claude), Google (Gemini), and LangChain.

The Trading Economics MCP allows LLMs and AI applications to access live economic and financial data through standard tool/function calling interfaces. These sample integrations show how to connect to the hosted MCP server, discover available tools, and run chat loops that dispatch tool calls back to Trading Economics in real time.

Prerequisites

Bash
# Set your API keys
export TRADING_ECONOMICS_API_KEY="your_te_key"   # Get it at https://tradingeconomics.com/api/
export OPENAI_API_KEY="..."                       # ChatGPT
export ANTHROPIC_API_KEY="..."                    # Claude
export GOOGLE_API_KEY="..."                       # Gemini

Example

Install:

Bash
npm install openai @modelcontextprotocol/sdk dotenv
JavaScript
import OpenAI from "openai";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const teApiKey = process.env.TRADING_ECONOMICS_API_KEY;

// 1. Connect to the hosted MCP server.
const transport = new StreamableHTTPClientTransport(
  new URL("https://mcp.tradingeconomics.com"),
  { requestInit: { headers: { Authorization: `Bearer ${teApiKey}` } } }
);
const mcp = new Client({ name: "openai-example", version: "1.0.0" });
await mcp.connect(transport);

// 2. Translate MCP tools into OpenAI tool definitions.
const { tools: mcpTools } = await mcp.listTools();
const openaiTools = mcpTools.map((t) => ({
  type: "function",
  function: {
    name: t.name,
    description: t.description ?? "",
    parameters: t.inputSchema ?? { type: "object", properties: {} },
  },
}));

// 3. Dispatch tool calls from the model back to the MCP server.
async function executeTool(name, args) {
  const result = await mcp.callTool({ name, arguments: args });
  return (result.content ?? [])
    .map((b) => (b.type === "text" ? b.text : JSON.stringify(b)))
    .join("\n");
}

// 4. Chat loop.
const messages = [
  { role: "system", content: "You are a precise economic-data assistant." },
  { role: "user", content: "What is the current GDP of Brazil?" },
];

for (let step = 0; step < 8; step++) {
  const res = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages,
    tools: openaiTools,
    tool_choice: "auto",
  });
  const msg = res.choices[0].message;
  messages.push(msg);
  if (!msg.tool_calls?.length) {
    console.log(msg.content);
    break;
  }
  for (const tc of msg.tool_calls) {
    const out = await executeTool(tc.function.name, JSON.parse(tc.function.arguments));
    messages.push({ role: "tool", tool_call_id: tc.id, content: out });
  }
}

await mcp.close();

Next steps