Trading Economics MCP - Getting Started

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

# 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:

npm install openai @modelcontextprotocol/sdk dotenv
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();

Install:

npm install @anthropic-ai/sdk @modelcontextprotocol/sdk dotenv
import Anthropic from "@anthropic-ai/sdk";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const teApiKey = process.env.TRADING_ECONOMICS_API_KEY;

const transport = new StreamableHTTPClientTransport(
  new URL("https://mcp.tradingeconomics.com"),
  { requestInit: { headers: { Authorization: `Bearer ${teApiKey}` } } }
);
const mcp = new Client({ name: "claude-example", version: "1.0.0" });
await mcp.connect(transport);

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

const messages = [{ role: "user", content: "What is the current GDP of Brazil?" }];

for (let step = 0; step < 8; step++) {
  const res = await anthropic.messages.create({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 4096,
    tools: claudeTools,
    messages,
  });
  messages.push({ role: "assistant", content: res.content });

  const toolUses = res.content.filter((b) => b.type === "tool_use");
  if (!toolUses.length) {
    console.log(res.content.find((b) => b.type === "text")?.text ?? "");
    break;
  }

  const toolResults = [];
  for (const tu of toolUses) {
    const out = await mcp.callTool({ name: tu.name, arguments: tu.input });
    const text = (out.content ?? [])
      .map((b) => (b.type === "text" ? b.text : JSON.stringify(b)))
      .join("\n");
    toolResults.push({ type: "tool_result", tool_use_id: tu.id, content: text });
  }
  messages.push({ role: "user", content: toolResults });
}

await mcp.close();

Install:

npm install @google/generative-ai @modelcontextprotocol/sdk dotenv
import { GoogleGenerativeAI } from "@google/generative-ai";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const genai = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
const teApiKey = process.env.TRADING_ECONOMICS_API_KEY;

const transport = new StreamableHTTPClientTransport(
  new URL("https://mcp.tradingeconomics.com"),
  { requestInit: { headers: { Authorization: `Bearer ${teApiKey}` } } }
);
const mcp = new Client({ name: "gemini-example", version: "1.0.0" });
await mcp.connect(transport);

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

const model = genai.getGenerativeModel({
  model: "gemini-1.5-pro",
  tools: [{ functionDeclarations }],
});
const chat = model.startChat();

let result = await chat.sendMessage("What is the current GDP of Brazil?");
for (let step = 0; step < 8; step++) {
  const calls = result.response.functionCalls() ?? [];
  if (!calls.length) {
    console.log(result.response.text());
    break;
  }
  const responses = [];
  for (const call of calls) {
    const out = await mcp.callTool({ name: call.name, arguments: call.args });
    const text = (out.content ?? [])
      .map((b) => (b.type === "text" ? b.text : JSON.stringify(b)))
      .join("\n");
    responses.push({ functionResponse: { name: call.name, response: { content: text } } });
  }
  result = await chat.sendMessage(responses);
}

await mcp.close();

Install:

npm install @langchain/google-genai @langchain/core @langchain/mcp-adapters dotenv
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { HumanMessage } from "@langchain/core/messages";

const teApiKey = process.env.TRADING_ECONOMICS_API_KEY;

// 1. Connect to the hosted MCP server via the LangChain MCP adapter.
const mcpClient = new MultiServerMCPClient({
  mcpServers: {
    "trading-economics": {
      transport: "http",
      url: "https://mcp.tradingeconomics.com",
      headers: { Authorization: `Bearer ${teApiKey}` },
    },
  },
});
const tools = await mcpClient.getTools();

// 2. Bind the MCP tools to a chat model.
const llm = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-pro",
  apiKey: process.env.GOOGLE_API_KEY,
}).bindTools(tools);

// 3. Run a tool-dispatch loop.
const messages = [new HumanMessage("What is the current GDP of Brazil?")];
for (let step = 0; step < 8; step++) {
  const ai = await llm.invoke(messages);
  messages.push(ai);
  if (!ai.tool_calls?.length) {
    console.log(ai.content);
    break;
  }
  for (const tc of ai.tool_calls) {
    const tool = tools.find((t) => t.name === tc.name);
    const out = await tool.invoke(tc.args);
    messages.push({ role: "tool", tool_call_id: tc.id, content: out });
  }
}

await mcpClient.close();

Next steps