Skip to content

Use ergonomic provider initialization pattern#2675

Merged
jlowin merged 1 commit intomainfrom
ergonomic-provider-init
Dec 22, 2025
Merged

Use ergonomic provider initialization pattern#2675
jlowin merged 1 commit intomainfrom
ergonomic-provider-init

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Dec 22, 2025

The FastMCP constructor already accepts a providers kwarg, but examples and convenience methods were using a less ergonomic three-step pattern. This updates them to use the cleaner approach.

Before:

provider = OpenAPIProvider(openapi_spec=spec, client=client)
mcp = FastMCP("API Server")
mcp.add_provider(provider)

After:

provider = OpenAPIProvider(openapi_spec=spec, client=client)
mcp = FastMCP("API Server", providers=[provider])

Changes apply to:

  • SQLite provider example
  • OpenAPI provider documentation
  • FastMCP.from_openapi() and FastMCP.from_fastapi() convenience methods

Updates examples and convenience methods to pass providers directly to FastMCP's __init__ instead of calling add_provider() after instantiation.
@marvin-context-protocol marvin-context-protocol Bot added enhancement Improvement to existing functionality. For issues and smaller PR improvements. provider Related to the FastMCP Provider class documentation Updates to docs, examples, or guides. Primary change is documentation-related. labels Dec 22, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 22, 2025

Walkthrough

This pull request refactors provider setup across multiple files to consolidate the two-step initialization process into a single-step approach. Previously, code would instantiate a FastMCP server and then invoke add_provider() separately. The changes update the FastMCP constructor to accept an optional providers parameter, allowing providers to be passed directly during instantiation. All call sites—including examples in examples/providers/sqlite/server.py, the OpenAPI server module, and the factory methods from_openapi() and from_fastapi()—are updated to use this new constructor-based pattern. The runtime behavior of provider attachment remains unchanged; only the initialization style is modified.

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Use ergonomic provider initialization pattern' clearly and specifically summarizes the main change: adopting a cleaner, more ergonomic approach to provider initialization in FastMCP.
Description check ✅ Passed The description provides a clear explanation of the change with before/after code examples and lists all affected areas, though it lacks the structured checklist items from the template.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
✨ 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 ergonomic-provider-init

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 96c7a74 and ab63256.

📒 Files selected for processing (4)
  • examples/providers/sqlite/server.py
  • src/fastmcp/server/openapi/server.py
  • src/fastmcp/server/providers/openapi/__init__.py
  • src/fastmcp/server/server.py
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Use Python ≥ 3.10 with full type annotations
Never use bare except - be specific with exception types

Files:

  • src/fastmcp/server/server.py
  • src/fastmcp/server/providers/openapi/__init__.py
  • src/fastmcp/server/openapi/server.py
  • examples/providers/sqlite/server.py
src/fastmcp/**/__init__.py

📄 CodeRabbit inference engine (AGENTS.md)

Core types that define a module's purpose should be exported (e.g., Middleware from fastmcp.server.middleware), while specialized features can live in submodules

Files:

  • src/fastmcp/server/providers/openapi/__init__.py
🧠 Learnings (1)
📚 Learning: 2025-11-03T17:36:13.363Z
Learnt from: jlowin
Repo: jlowin/fastmcp PR: 2355
File: docs/clients/client.mdx:226-246
Timestamp: 2025-11-03T17:36:13.363Z
Learning: In FastMCP documentation, prefer showing the happy path in onboarding examples without over-explaining edge cases or adding defensive checks, as this reduces cognitive burden for new users learning the API.

Applied to files:

  • src/fastmcp/server/openapi/server.py
🧬 Code graph analysis (1)
examples/providers/sqlite/server.py (1)
src/fastmcp/server/server.py (1)
  • FastMCP (157-2949)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Run tests: Python 3.10 on windows-latest
  • GitHub Check: Run tests: Python 3.13 on ubuntu-latest
  • GitHub Check: Run tests: Python 3.10 on ubuntu-latest
  • GitHub Check: Run tests with lowest-direct dependencies
🔇 Additional comments (7)
examples/providers/sqlite/server.py (1)

104-105: LGTM!

The ergonomic provider initialization pattern is correctly applied. The provider is instantiated and passed directly to the FastMCP constructor via the providers kwarg, which aligns with the constructor's providers: Sequence[Provider] | None parameter.

src/fastmcp/server/openapi/server.py (3)

11-11: LGTM!

The module docstring example correctly demonstrates the new ergonomic provider initialization pattern.


54-57: LGTM!

The "New approach" example in the class docstring correctly demonstrates the recommended pattern with providers=[provider].


90-96: LGTM!

The deprecation warning message correctly guides users to the new ergonomic pattern. The internal add_provider() call on line 117 is intentionally preserved for backwards compatibility in this deprecated wrapper class.

src/fastmcp/server/server.py (2)

2779-2791: LGTM!

The from_openapi() factory method now correctly uses the single-step provider initialization pattern by passing providers=[provider] directly to the constructor. This aligns with the PR objectives and is cleaner than the previous two-step approach.


2825-2848: LGTM!

The from_fastapi() factory method correctly mirrors the from_openapi() change, passing providers=[provider] directly to the constructor for consistent ergonomic initialization.

src/fastmcp/server/providers/openapi/__init__.py (1)

6-14: LGTM!

The module docstring example correctly demonstrates the new ergonomic provider initialization pattern, consistent with the changes across the codebase.


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.

@jlowin jlowin merged commit bca310c into main Dec 22, 2025
13 checks passed
@jlowin jlowin deleted the ergonomic-provider-init branch December 22, 2025 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Updates to docs, examples, or guides. Primary change is documentation-related. enhancement Improvement to existing functionality. For issues and smaller PR improvements. provider Related to the FastMCP Provider class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant