Skip to content

Update FastMCP constructor to respect Pydantic settings configuration #1244

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: main
Choose a base branch
from

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

@saqadri saqadri requested a review from a team as a code owner August 6, 2025 21:59
@saqadri saqadri requested a review from Kludex August 6, 2025 21:59
@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

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