Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
- name: Install dependencies
run: uv sync

- name: Run Ruff check
run: uv run ruff check
- name: Run linter
run: uv run just lint

- name: Run Ruff format
run: uv run ruff format --check
- name: Run tests
run: uv run just test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ path to `uvx`.

```bash
# Claude CLI
claude mcp add polygon -e POLYGON_API_KEY=your_api_key_here -- uvx --from git+https://github.com/polygon-io/mcp_polygon@v0.4.1 mcp_polygon
claude mcp add polygon -e POLYGON_API_KEY=your_api_key_here -- uvx --from git+https://github.com/polygon-io/mcp_polygon@v0.5.0 mcp_polygon
```

This command will install the MCP server in your current project.
Expand Down Expand Up @@ -83,7 +83,7 @@ Make sure you complete the various fields.
"command": "<path_to_your_uvx_install>/uvx",
"args": [
"--from",
"git+https://github.com/polygon-io/mcp_polygon@v0.4.1",
"git+https://github.com/polygon-io/mcp_polygon@v0.5.0",
"mcp_polygon"
],
"env": {
Expand Down
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
lint:
uv run ruff format
uv run ruff check --fix

test:
uv run pytest -v tests
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": "0.2",
"name": "mcp_polygon",
"version": "0.4.1",
"version": "0.5.0",
"description": "MCP server providing access to Polygon.io financial market data API",
"author": {
"name": "Polygon.io",
Expand Down
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[project]
name = "mcp_polygon"
version = "0.4.1"
version = "0.5.0"
description = "A MCP server project"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"mcp[cli]>=1.9.3",
"polygon-api-client>=1.15.3",
"mcp[cli]>=1.15.0",
"polygon-api-client>=1.15.4",
]
[[project.authors]]
name = "Polygon"
Expand All @@ -18,7 +18,9 @@ build-backend = "hatchling.build"

[dependency-groups]
dev = [
"ruff>=0.12.4",
"ruff>=0.13.2",
"pytest>=8.4.0",
"rust-just>=1.42.4",
]

[project.scripts]
Expand Down
81 changes: 81 additions & 0 deletions src/mcp_polygon/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import json
import csv
import io
from typing import Any


def json_to_csv(json_input: str | dict) -> str:
"""
Convert JSON to flattened CSV format.

Args:
json_input: JSON string or dict. If the JSON has a 'results' key containing
a list, it will be extracted. Otherwise, the entire structure
will be wrapped in a list for processing.

Returns:
CSV string with headers and flattened rows
"""
# Parse JSON if it's a string
if isinstance(json_input, str):
data = json.loads(json_input)
else:
data = json_input

if isinstance(data, dict) and "results" in data:
records = data["results"]
elif isinstance(data, list):
records = data
else:
records = [data]

flattened_records = [_flatten_dict(record) for record in records]

if not flattened_records:
return ""

# Get all unique keys across all records (for consistent column ordering)
all_keys = []
seen = set()
for record in flattened_records:
for key in record.keys():
if key not in seen:
all_keys.append(key)
seen.add(key)

output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=all_keys, lineterminator="\n")
writer.writeheader()
writer.writerows(flattened_records)

return output.getvalue()


def _flatten_dict(
d: dict[str, Any], parent_key: str = "", sep: str = "_"
) -> dict[str, Any]:
"""
Flatten a nested dictionary by joining keys with separator.

Args:
d: Dictionary to flatten
parent_key: Key from parent level (for recursion)
sep: Separator to use between nested keys

Returns:
Flattened dictionary with no nested structures
"""
items = []
for k, v in d.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k

if isinstance(v, dict):
# Recursively flatten nested dicts
items.extend(_flatten_dict(v, new_key, sep=sep).items())
elif isinstance(v, list):
# Convert lists to comma-separated strings
items.append((new_key, str(v)))
else:
items.append((new_key, v))

return dict(items)
Loading
Loading