Skip to content

Conversation

saqadri
Copy link
Contributor

@saqadri saqadri commented Aug 6, 2025

Fix environment variable loading in FastMCP by preserving Pydantic Settings behavior

Motivation and Context

PR #1198 introduced a regression where environment variables (e.g., FASTMCP_HOST=0.0.0.0) were no longer being loaded by FastMCP. The issue occurred because the refactored code explicitly passed all settings parameters to the Pydantic Settings constructor, which overrides environment variable values.

This fix ensures that only explicitly provided parameters are passed to the Settings constructor, allowing Pydantic's intended priority order to work correctly.

How Has This Been Tested?

Tested the following scenarios:

  • Environment variables are correctly loaded when no explicit parameters are provided
  • Explicit parameters override environment variables when provided
  • .env file values are loaded with correct priority
  • Default values from the Settings class are used as fallback

Example test cases:

server = FastMCP()  # host="127.0.0.1" from Settings default

# With FASTMCP_HOST=0.0.0.0 in environment
server = FastMCP()  # host="0.0.0.0" from env var

server = FastMCP(host="192.168.1.1")  # host="192.168.1.1" from explicit param

Breaking Changes

No breaking changes. This restores the original behavior where environment variables are properly loaded.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the [MCP Documentation](https://modelcontextprotocol.io)
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Settings Priority Order

This fix preserves Pydantic Settings' intended priority order (highest to lowest):

  1. Explicit parameters passed to FastMCP() constructor
  2. Environment variables (e.g., FASTMCP_HOST)
  3. .env file values
  4. Default values defined in the Settings class

@Kludex
Copy link
Member

Kludex commented Aug 7, 2025

I would prefer to revert that PR as to have this one merged.

It's very annoying that this was build the way it was. We need to deprecate some of those arguments and move to run().

@saqadri
Copy link
Contributor Author

saqadri commented Aug 7, 2025

I would prefer to revert that PR as to have this one merged.

It's very annoying that this was build the way it was. We need to deprecate some of those arguments and move to run().

@Kludex yeah neither approach is ideal. If you prefer I can just update this PR with the revert of that specific file. #1198 had a handful of other small changes too

@saqadri
Copy link
Contributor Author

saqadri commented Aug 28, 2025

@Kludex please let me know what you'd prefer for this change

@felixweinberger felixweinberger added v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes needs more work Not ready to be merged yet, needs additional changes. needs more eyes Needs alignment among maintainers whether this is something we want to add needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention labels Sep 23, 2025
@felixweinberger
Copy link
Contributor

Hi @saqadri thanks for this contribution, will bring up in the maintainer meeting to figure out a path forward.

@felixweinberger felixweinberger self-assigned this Sep 30, 2025
@felixweinberger felixweinberger added this to the Python SDK v2.0 milestone Sep 30, 2025
@felixweinberger felixweinberger removed needs more work Not ready to be merged yet, needs additional changes. needs more eyes Needs alignment among maintainers whether this is something we want to add v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes labels Sep 30, 2025
@felixweinberger felixweinberger removed this from the Python SDK v2.0 milestone Sep 30, 2025
@felixweinberger felixweinberger removed their assignment Oct 10, 2025
@felixweinberger felixweinberger added the needs more eyes Needs alignment among maintainers whether this is something we want to add label Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention needs more eyes Needs alignment among maintainers whether this is something we want to add

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants