Skip to content

Conversation

@T-rav
Copy link
Collaborator

@T-rav T-rav commented Oct 10, 2025

Summary

Fixes an issue where tools couldn't access environment variables from .env file because load_dotenv() ran after tool imports.

Changes

  • Move load_dotenv() to run before importing tool modules in mcp_server.py
  • Ensures SERPER_API_KEY and other environment variables are set before tools initialize
  • Add noqa: E402 comments for intentional late imports (required for proper initialization order)

Why This Matters

Previously, tool modules like search_tool.py would try to read os.environ.get("SERPER_API_KEY") at import time, but .env hadn't been loaded yet, so the key was empty.

Now the initialization order is:

  1. Load .env file
  2. Set up logging
  3. Import tools (which can now access env vars)

Test Plan

  • ✅ Tested with real Serper API call
  • ✅ Confirmed env variables load before tool import
  • ✅ Search tool successfully uses SERPER_API_KEY from .env
  • ✅ Real API test returns expected results with max_results parameter

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improves server startup reliability by loading environment settings before initializing tools, preventing missing-configuration errors at launch.
    • Ensures consistent behavior across environments that rely on .env files, reducing intermittent initialization issues.
    • No user action required; existing features and interfaces remain unchanged.

… are available

- Move load_dotenv() to run before importing tools
- Ensures SERPER_API_KEY and other env vars are set before tool modules initialize
- Tools can now properly reference environment variables from .env file
- Fixes issue where tools couldn't access .env variables at import time
- Add noqa comments for necessary late imports

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@coderabbitai
Copy link

coderabbitai bot commented Oct 10, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The server now loads environment variables from .env before importing tool modules. Imports of health_check_tool and search_tool are moved after load_dotenv(). Comments and noqa markers clarify the order. No function signatures or bindings changed.

Changes

Cohort / File(s) Summary
Startup env loading and import order
docker/mcp_server.py
Call load_dotenv() added before tool imports; moved imports of health_check_tool and search_tool after dotenv load; added comments and noqa for import order; logging and existing bindings unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Proc as Process
  participant Server as MCP Server
  participant Dotenv as .env Loader
  participant Tools as Tool Modules

  Proc->>Server: Start module
  Server->>Dotenv: load_dotenv()
  Note right of Dotenv: Ensure env vars are set
  Server->>Tools: import health_check_tool, search_tool
  Note right of Tools: Modules read env on import
  Server->>Server: Configure logging/bind tools
  Server-->>Proc: Ready
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitched my nose at dawn’s first light,
Loaded secrets, tucked in tight—
Then hopped to tools with jaunty cheer,
“Env is set, you’re safe in here!”
Logs hum softly, bindings stay—
A tidy warren starts the day. 🐇✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/load-env-before-tools

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9298117 and f8c2463.

📒 Files selected for processing (1)
  • docker/mcp_server.py (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@T-rav T-rav merged commit adaa15d into main Oct 10, 2025
11 checks passed
@T-rav T-rav deleted the fix/load-env-before-tools branch October 10, 2025 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants