Add dependency injection documentation and DI-style dependencies#2980
Add dependency injection documentation and DI-style dependencies#2980
Conversation
- Add docs/servers/dependency-injection.mdx with comprehensive DI guide - Add CurrentRequest(), CurrentHeaders(), CurrentAccessToken() dependencies - Move DI content from context.mdx to dedicated page
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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. WalkthroughAdds 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)
✅ Passed checks (1 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
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 WorksAs 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.
There was a problem hiding this comment.
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 incontext.mdx. The HTTP Request section was moved todependency-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()`.
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.mdxpage covering:CurrentContext())Depends(), caching, context managers, nestingNew dependencies for API consistency:
The function equivalents (
get_http_request(),get_http_headers(),get_access_token()) remain available for use outside DI context.