Skip to content

Require API key and handle server errors #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ uv sync
POLYGON_API_KEY=your_api_key_here uv run mcp_polygon
```

The server will exit with an error if `POLYGON_API_KEY` is not set.

<details>
<summary>Local Dev Config for claude_desktop_config.json</summary>

Expand Down
4 changes: 2 additions & 2 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

# Ensure the server process doesn't exit immediately when run as an MCP server
def start_server():
polygon_api_key = os.environ.get("POLYGON_API_KEY", "")
polygon_api_key = os.environ.get("POLYGON_API_KEY")
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Retrieval and validation of POLYGON_API_KEY is duplicated in server.py; consider centralizing this logic in a shared helper function.

Copilot uses AI. Check for mistakes.

if not polygon_api_key:
print("Warning: POLYGON_API_KEY environment variable not set.")
raise EnvironmentError("POLYGON_API_KEY environment variable not set.")
else:
print("Starting Polygon MCP server with API key configured.")

Expand Down
15 changes: 12 additions & 3 deletions src/mcp_polygon/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from datetime import datetime, date

POLYGON_API_KEY = os.environ.get("POLYGON_API_KEY", "")
POLYGON_API_KEY = os.environ.get("POLYGON_API_KEY")
if not POLYGON_API_KEY:
print("Warning: POLYGON_API_KEY environment variable not set.")
raise EnvironmentError("POLYGON_API_KEY environment variable not set.")

polygon_client = RESTClient(POLYGON_API_KEY)

Expand Down Expand Up @@ -794,4 +794,13 @@ async def list_stock_financials(

def run():
"""Run the Polygon MCP server."""
poly_mcp.run()
try:
poly_mcp.run()
except Exception as exc:
print(f"Error running Polygon MCP server: {exc}")
raise
finally:
try:
polygon_client.close()
except Exception as close_exc:
print(f"Error closing Polygon REST client: {close_exc}")
Comment on lines +799 to +806
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching a broad Exception may mask unexpected errors; consider catching more specific exceptions or allowing critical errors to propagate.

Suggested change
except Exception as exc:
print(f"Error running Polygon MCP server: {exc}")
raise
finally:
try:
polygon_client.close()
except Exception as close_exc:
print(f"Error closing Polygon REST client: {close_exc}")
except RuntimeError as exc:
print(f"Runtime error while running Polygon MCP server: {exc}")
raise
except ValueError as exc:
print(f"Value error while running Polygon MCP server: {exc}")
raise
finally:
try:
polygon_client.close()
except OSError as close_exc:
print(f"OS error while closing Polygon REST client: {close_exc}")

Copilot uses AI. Check for mistakes.

Comment on lines +800 to +806
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using a logging framework instead of print statements for error reporting to ensure consistent log management and easier debugging.

Suggested change
print(f"Error running Polygon MCP server: {exc}")
raise
finally:
try:
polygon_client.close()
except Exception as close_exc:
print(f"Error closing Polygon REST client: {close_exc}")
logging.error(f"Error running Polygon MCP server: {exc}")
raise
finally:
try:
polygon_client.close()
except Exception as close_exc:
logging.error(f"Error closing Polygon REST client: {close_exc}")

Copilot uses AI. Check for mistakes.