Skip to content

Add dependency injection documentation and DI-style dependencies#2980

Merged
jlowin merged 3 commits intomainfrom
docs/dependencies-page
Jan 22, 2026
Merged

Add dependency injection documentation and DI-style dependencies#2980
jlowin merged 3 commits intomainfrom
docs/dependencies-page

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Jan 22, 2026

FastMCP's dependency injection system was underdocumented—users had questions about how Depends() works and there was nowhere to send them. This PR adds comprehensive documentation and makes the API more consistent.

Documentation: New docs/servers/dependency-injection.mdx page covering:

  • How DI works (type annotations vs explicit CurrentContext())
  • All built-in dependencies with both DI and function approaches
  • Custom dependencies with Depends(), caching, context managers, nesting
  • Links to Docket docs for advanced patterns

New dependencies for API consistency:

from fastmcp.dependencies import CurrentRequest, CurrentHeaders, CurrentAccessToken

@mcp.tool
async def my_tool(
    request: Request = CurrentRequest(),      # HTTP request
    headers: dict = CurrentHeaders(),          # Headers (graceful fallback)
    token: AccessToken = CurrentAccessToken(), # Auth token (raises if missing)
) -> str:
    ...

The function equivalents (get_http_request(), get_http_headers(), get_access_token()) remain available for use outside DI context.

- Add docs/servers/dependency-injection.mdx with comprehensive DI guide
- Add CurrentRequest(), CurrentHeaders(), CurrentAccessToken() dependencies
- Move DI content from context.mdx to dedicated page
@jlowin jlowin added the feature Major new functionality. Reserved for 2-4 significant PRs per release. Not for issues. label Jan 22, 2026
@marvin-context-protocol marvin-context-protocol Bot added documentation Updates to docs, examples, or guides. Primary change is documentation-related. and removed feature Major new functionality. Reserved for 2-4 significant PRs per release. Not for issues. labels Jan 22, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 22, 2026

Warning

Rate limit exceeded

@jlowin has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 0 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

Adds a new documentation page for dependency injection and consolidates dependency-related docs by removing the Runtime Dependencies section from the old context page. Exposes three HTTP-context dependencies (CurrentRequest, CurrentHeaders, CurrentAccessToken) and re-exports them from the public dependencies module. Introduces progress abstractions (ProgressLike protocol, InMemoryProgress, Progress dependency) to provide per-request progress tracking and selects appropriate progress implementation based on execution context.

Possibly related PRs

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description covers the documentation changes, new dependencies, and code examples, but lacks the contributors checklist items (issue reference, testing confirmation, documentation updates acknowledgment) required by the template. Complete the contributors checklist by specifying the related issue number and confirming manual testing, documentation updates, and development workflow compliance.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes: adding dependency injection documentation and new DI-style dependencies for API consistency.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
docs/servers/dependency-injection.mdx (2)

11-19: Add a short prerequisites section before usage.

The page jumps straight into usage without stating required setup (FastMCP installed, HTTP transport for HTTP deps, tasks extra for background deps).

➕ Suggested insertion
 </Note>
 
+## Prerequisites
+
+- Install FastMCP in your environment.
+- Use an HTTP transport (SSE or Streamable HTTP) if you plan to inject HTTP request data.
+- Install the tasks extra (`fastmcp[tasks]`) if you need background-task dependencies.
+
 ## How Dependency Injection Works

As per coding guidelines.


372-396: Add troubleshooting for common DI failures.

Common failures (no HTTP context, unauthenticated token, missing tasks extra) aren’t surfaced. A short troubleshooting section would reduce confusion.

➕ Suggested addition
 For advanced dependency patterns—like `TaskArgument()` for accessing task parameters, or custom `Dependency` subclasses—see the [Docket dependency documentation](https://chrisguidry.github.io/docket/dependencies/).
+
+## Troubleshooting
+
+### "No active HTTP request found."
+You're running outside an HTTP transport. Switch to SSE or Streamable HTTP, or use `CurrentHeaders()` / `get_http_headers()` for a safe fallback.
+
+### "No access token found."
+Ensure authentication is configured and the request is authenticated, or use `get_access_token()` when authentication is optional.
+
+### "FastMCP background tasks require the `tasks` extra."
+Install the tasks extra and restart the server.

As per coding guidelines.

Comment thread docs/servers/context.mdx
Comment thread docs/servers/dependency-injection.mdx Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/servers/context.mdx (1)

391-391: Fix broken internal link to HTTP request helpers.

Line 391 references #http-requests, but this heading no longer exists in context.mdx. The HTTP Request section was moved to dependency-injection.mdx. Update the link:

Fix
-For HTTP request access that works regardless of MCP session availability (when using HTTP transports), use the [HTTP request helpers](`#http-requests`) like `get_http_request()` and `get_http_headers()`.
+For HTTP request access that works regardless of MCP session availability (when using HTTP transports), use the [HTTP request helpers](/servers/dependency-injection#http-request) like `get_http_request()` and `get_http_headers()`.

@jlowin jlowin merged commit 884b81a into main Jan 22, 2026
13 checks passed
@jlowin jlowin deleted the docs/dependencies-page branch January 22, 2026 14:08
gfortaine pushed a commit to gfortaine/fastmcp that referenced this pull request Feb 4, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant