#
tokens: 3761/50000 5/5 files
lines: off (toggle) GitHub
raw markdown copy
# Directory Structure

```
├── .gitignore
├── .python-version
├── LICENSE
├── pyproject.toml
├── README.md
├── server.py
└── uv.lock
```

# Files

--------------------------------------------------------------------------------
/.python-version:
--------------------------------------------------------------------------------

```
3.13

```

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
# Python-generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# Virtual environments
.venv

# Environment variables
.env

```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
# MCP-FREDAPI

**FRED (Federal Reserve Economic Data) API integration with Model Context Protocol (MCP)**

## Table of Contents

- [Introduction](#introduction)
- [Installation](#installation)
- [Configuration](#configuration)
  - [FRED API Key](#fred-api-key)
  - [Claude/Cursor Configuration](#claudecursor-configuration)
- [Available Tools](#available-tools)
- [Parameters](#parameters)
- [Examples](#examples)
  - [Common Economic Series IDs](#common-economic-series-ids)
- [Contributing](#contributing)
- [License](#license)
- [References](#references)

## Introduction

MCP-FREDAPI provides access to economic data from the Federal Reserve Bank of St. Louis (FRED) through the Model Context Protocol. This integration allows AI assistants like Claude to retrieve economic time series data directly when used with Cursor or other MCP-compatible environments.

This package integrates with the [official FRED API](https://fred.stlouisfed.org/docs/api/fred/), focusing specifically on the [series_observations endpoint](https://fred.stlouisfed.org/docs/api/fred/series_observations.html) which provides time series data for economic indicators.

## Installation

There are two installation methods:

### Method 1: Using pip

Install the required dependencies:

```terminal
pip install "mcp[cli]" httpx python-dotenv
```

Clone this repository:

```terminal
git clone https://github.com/Jaldekoa/mcp-fredapi.git
cd mcp-fredapi
```

### Method 2: Using uv (Recommended)

This method is recommended as it matches the configuration shown in mcp.json.

1. First, install uv if you don't have it yet:

```terminal
pip install uv
```

2. Clone this repository:

```terminal
git clone https://github.com/Jaldekoa/mcp-fredapi.git
cd mcp-fredapi
```

3. Use uv to run the server (no need to install dependencies separately):

```terminal
uv run --with mcp --with httpx mcp run server.py
```

## Configuration

### FRED API Key

You'll need a FRED API key, which you can obtain from [FRED API](https://fred.stlouisfed.org/docs/api/api_key.html).

Create a `.env` file in the project root:

```
FRED_API_KEY=your_api_key_here
```

### Claude/Cursor Configuration

To configure Cursor to use this MCP server, add the following to your `~/.cursor/mcp.json` file:

```json
{
  "mcpServers": {
    "mcp-fredapi": {
      "command": "uv",
      "args": ["--directory", "/path/to/mcp-fredapi", "run", "--with", "mcp", "--with", "httpx", "mcp", "run", "server.py"]
    }
  }
}
```

Replace `/path/to/mcp-fredapi` with the actual path to the repository on your system. For example:

```json
{
  "mcpServers": {
    "mcp-fredapi": {
      "command": "uv",
      "args": ["--directory", "/path/to/mcp-fredapi", "run", "--with", "mcp", "--with", "httpx", "mcp", "run", "server.py"]
    }
  }
}
```

Note: On Windows, you can use either forward slashes `/` or double backslashes `\\` in the path.

## Available Tools

### get_fred_series_observations

Retrieves economic time series observations from FRED.

When using Claude in Cursor, you can access this tool directly with:

```
@mcp-fredapi:get_fred_series_observations
```

## Parameters

The `get_fred_series_observations` tool accepts the following parameters. For complete technical details about each parameter, please refer to the [official FRED API documentation](https://fred.stlouisfed.org/docs/api/fred/series_observations.html).

| Parameter | Type | Description | Allowed Values | Default Value | Status |
|-----------|------|-------------|---------------|---------------|--------|
| series_id | str | The ID of the economic series | - | (Required) | ✅ Works |
| sort_order | str | Sort order of observations | 'asc', 'desc' | 'asc' | ✅ Works |
| units | str | Data value transformation | 'lin', 'chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log' | 'lin' | ✅ Works |
| frequency | str | Frequency of observations | 'd', 'w', 'bw', 'm', 'q', 'sa', 'a', 'wef', 'weth', 'wew', 'wetu', 'wem', 'wesu', 'wesa', 'bwew', 'bwem' | None | ✅ Works |
| aggregation_method | str | Aggregation method for frequency | 'avg', 'sum', 'eop' | 'avg' | ✅ Works |
| output_type | int | Output type of observations | 1, 2, 3, 4 | 1 | ✅ Works |
| realtime_start | str | Start of real-time period (YYYY-MM-DD) | - | None | ❌ Not working |
| realtime_end | str | End of real-time period (YYYY-MM-DD) | - | None | ❌ Not working |
| limit | int/str | Maximum number of observations to return | Between 1 and 100000 | 10 | ❌ Not working |
| offset | int/str | Number of observations to skip from the beginning | - | 0 | ❌ Not working |
| observation_start | str | Start date of observations (YYYY-MM-DD) | - | None | ❌ Not working |
| observation_end | str | End date of observations (YYYY-MM-DD) | - | None | ❌ Not working |
| vintage_dates | str | Comma-separated list of vintage dates | - | None | ❌ Not working |

> [!WARNING]
> 
> Due to current limitations with the MCP implementation, only certain parameters are working properly:
> - ✅ **Working parameters**: `series_id`, `sort_order`, `units`, `frequency` , aggregation_method`, and `output_type`.
> - ❌ **Non-working parameters**: `realtime_start`, `realtime_end`, `limit`, `offset`, `observation_start`, `observation_end`, and `vintage_dates`.
>
> For best results, stick with the working parameters in your queries. Future updates may resolve these limitations.

## Examples

### Getting US GDP Data

When using Claude in Cursor, you can ask for GDP data like this:

```
Can you get the latest GDP data from FRED?

@mcp-fredapi:get_fred_series_observations
{
  "series_id": "GDP"
}
```

### Getting GDP Data in Descending Order

```
Can you get the GDP data in descending order (newest first)?

@mcp-fredapi:get_fred_series_observations
{
  "series_id": "GDP",
  "sort_order": "desc"
}
```

### Getting Annual GDP Data

```
Can you get annual GDP data?

@mcp-fredapi:get_fred_series_observations
{
  "series_id": "GDP",
  "frequency": "a"
}
```

### Getting Inflation Rate

To get consumer price index data with percent change:

```
What's the recent inflation rate in the US?

@mcp-fredapi:get_fred_series_observations
{
  "series_id": "CPIAUCSL",
  "units": "pch",
  "frequency": "m"
}
```

### Different Output Format

```
Show me GDP data in a different format.

@mcp-fredapi:get_fred_series_observations
{
  "series_id": "GDP",
  "output_type": 2
}
```

## Contributing

Contributions are welcome. Please follow these steps:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Commit your changes (`git commit -m 'Add an amazing feature'`)
5. Push to the branch (`git push origin feature/amazing-feature`)
6. Open a Pull Request

## License

This project is licensed under the MIT License - see the LICENSE file for details.

## References

- [FRED API Documentation - Series Observations](https://fred.stlouisfed.org/docs/api/fred/series_observations.html) - Official documentation for the FRED API endpoint used in this project.
- [FRED API](https://fred.stlouisfed.org/docs/api/api_key.html) - Information on obtaining an API key and general API documentation.
- [Model Context Protocol](https://modelcontextprotocol.github.io/) - Documentation for the Model Context Protocol.

```

--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------

```toml
[project]
name = "mcp-fredapi"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
    "httpx>=0.28.1",
    "mcp[cli]>=1.6.0",
    "python-dotenv>=1.1.0",
]

```

--------------------------------------------------------------------------------
/server.py:
--------------------------------------------------------------------------------

```python
from mcp.server.fastmcp import FastMCP
from typing import Annotated, Optional , Literal
from dotenv import load_dotenv
from pydantic import Field
import httpx
import os

load_dotenv()

mcp = FastMCP("mcp-fredapi", dependencies=["httpx", "python-dotenv"])

FRED_API_URL = "https://api.stlouisfed.org/fred"


async def make_request(url: str, params: dict):
    """Make a request to the Federal Reserve Economic Data API."""
    
    if FRED_API_KEY := os.getenv("FRED_API_KEY"):
        params["api_key"] = FRED_API_KEY

    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(url, params=params)
            response.raise_for_status()
            return response.json()

        except httpx.HTTPStatusError as e:
            raise ConnectionError(f"Failed to fetch data from the FRED API: {e}")

@mcp.tool(name="get_fred_series_observations", description="""Get series observations from the Fred API.""")
async def get_fred_series_observations(
    series_id: Annotated[str, Field(description="The id for a series.")],
    realtime_start: Annotated[Optional[str], Field(description="The start of the real-time period. Format: YYYY-MM-DD. Defaults to today's date.")] = None,
    realtime_end: Annotated[Optional[str], Field(description="The end of the real-time period. Format: YYYY-MM-DD. Defaults to today's date.")] = None,
    limit: Annotated[Optional[int | str], Field(description="Maximum number of observations to return. Defaults to 10.")] = 10,
    offset: Annotated[Optional[int | str], Field(description="Number of observations to offset from first. Defaults to 0.")] = 0,
    sort_order: Annotated[Literal['asc', 'desc'], Field(description="Sort order of observations. Options: 'asc' or 'desc'. Defaults to 'asc'.")] = 'asc',
    observation_start: Annotated[Optional[str], Field(description="Start date of observations. Format: YYYY-MM-DD.")] = None,
    observation_end: Annotated[Optional[str], Field(description="End date of observations. Format: YYYY-MM-DD.")] = None,
    units: Annotated[Literal['lin', 'chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log'], Field(description="Data value transformation. Options: 'lin', 'chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log'. Defaults to 'lin'.")] = 'lin',
    frequency: Annotated[Literal['d', 'w', 'bw', 'm', 'q', 'sa', 'a', 'wef', 'weth', 'wew', 'wetu', 'wem', 'wesu', 'wesa', 'bwew', 'bwem'], Field(description="Frequency of observations. Options: 'd', 'w', 'bw', 'm', 'q', 'sa', 'a', 'wef', 'weth', 'wew', 'wetu', 'wem', 'wesu', 'wesa', 'bwew', 'bwem'. Defaults to no value for no frequency aggregation.")] = None,
    aggregation_method: Annotated[Literal['avg', 'sum', 'eop'], Field(description="Aggregation method for frequency. Options: 'avg', 'sum', 'eop'. Defaults to 'avg'.")] = 'avg',
    output_type: Annotated[Literal[1, 2, 3, 4], Field(description="Output type of observations. Options: 1, 2, 3, 4. Defaults to 1.")] = 1,
    vintage_dates: Annotated[Optional[str], Field(description="Comma-separated list of vintage dates.")] = None,
):
    """Get series observations from the Fred API.
    
    Args:
        series_id (str): The id for a series.
        realtime_start (str): The start of the real-time period. YYYY-MM-DD formatted string, optional, default: today's date.
        realtime_end (str): The end of the real-time period. YYYY-MM-DD formatted string, optional, default: today's date.
        limit (int or str): The maximum number of observations to return. Optional, default: 1000.
        offset (int or str): The number of observations to offset from the first observation. Optional, default: 0.
        sort_order (str): The sort order of the observations. Possible values: "asc" or "desc". Optional, default: "asc".
        observation_start (str): The start date of the observations to get. YYYY-MM-DD formatted string. Optional, default: 1776-07-04 (earliest available).
        observation_end (str): The end date of the observations to get. YYYY-MM-DD formatted string. Optional, default: 9999-12-31 (latest available).
        units (str): A key that indicates a data value transformation. Posible values: 'lin', 'chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log'. Optional, default: 'lin' (No transformation).
        frequency (str): The frequency of the observations. Posible values: 'd', 'w', 'bw', 'm', 'q', 'sa', 'a', 'wef', 'weth', 'wew', 'wetu', 'wem', 'wesu', 'wesa', 'bwew', 'bwem'. Optional, default: no value for no frequency aggregation.
        aggregation_method (str): A key that indicates the aggregation method used for frequency aggregation. This parameter has no affect if the frequency parameter is not set. Posible values: 'avg', 'sum', 'eop'. Optional, default: "avg".
        output_type (int): The output type of the observations. Optional, default: 1.
        vintage_dates (str): A comma-separated list of vintage dates to return. Optional, default: no vintage dates are set by default.

    Returns:
        dict[str, str]: A dictionary containing the observations or data values for an economic data series.
    """
    params = {
        "series_id": series_id, # ✅
        "realtime_start": realtime_start, # ❌
        "realtime_end": realtime_end, # ❌
        "limit": limit, # ✅
        "offset": offset, # ✅
        "sort_order": sort_order, # ✅
        "observation_start": observation_start, # ❌
        "observation_end": observation_end, # ❌
        "units": units, # ✅
        "frequency": frequency, # ✅
        "aggregation_method": aggregation_method, # ✅
        "output_type": output_type, # ✅
        "vintage_dates": vintage_dates, # ❌
        "file_type": "json"
    }
    
    data = await make_request(f"{FRED_API_URL}/series/observations", params)

    if not data:
        raise ConnectionError("Failed to fetch data from the FRED API")
    
    observations = data["observations"]

    if not observations:
        raise ValueError("No observations found for the given series")
    
    return observations


if __name__ == "__main__":
    mcp.run()

```