Skip to content

Feature/use request context #191

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

Closed

Conversation

Edison-A-N
Copy link

Describe your changes

Upgrade the mcp package to version >=1.10.1 to utilize the new mcp_server.request_context.request feature. This will allow us to retrieve request information directly from the context.

Issue ticket number and link (if applicable)

#190

Screenshots of the feature / bugfix

Checklist before requesting a review

  • Added relevant tests
  • Run ruff & mypy
  • All tests pass

@Edison-A-N Edison-A-N force-pushed the feature/use-request-context branch from 2546f1d to 80f9b44 Compare July 5, 2025 05:27
Copy link

codecov bot commented Jul 13, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
fastapi_mcp/server.py 66.66% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@shahar4499
Copy link
Contributor

@Edison-A-N Nice one. Thanks! Can you add a test for it?

@Edison-A-N Edison-A-N force-pushed the feature/use-request-context branch from 80f9b44 to 7d72849 Compare July 17, 2025 07:08
@Edison-A-N
Copy link
Author

Thank you for the reply.

Summary of the change:

  • The main change in this patch is to update the internal implementation to use context.request for passing request information (headers, cookies, query params, body, etc.) via HTTPRequestInfo.
  • The external API and usage remain unchanged. All existing tests that verify header passthrough and tool invocation (such as test_headers_passthrough_to_tool_handler and related tests) still pass, confirming that the new context-based mechanism is functionally equivalent from the user's perspective.
  • The rest of the patch is dependency updates.

About the missing lines:

  • The lines reported as missing coverage are defensive checks in the constructor of FastApiMCP:
    • Raising a ValueError if both include_operations and exclude_operations are provided.
    • Raising a ValueError if both include_tags and exclude_tags are provided.
  • These branches are not new in this patch and are not hit by any test, as they are only triggered by invalid configuration (which should not happen in normal usage).

Do I need to add tests for these?

  • From a practical perspective, the main logic and all user-facing behaviors are already covered by existing tests.
  • The missing lines are only for error handling in case of misconfiguration, and are not related to the core logic or the context/request refactor.
  • However, if 100% patch coverage is required, it is possible to add two simple tests that instantiate FastApiMCP with both include/exclude options and assert that a ValueError is raised. This would bring coverage to 100%, but does not affect the correctness or safety of the main change.

Conclusion:

  • The context/request refactor is already fully covered by the existing tests, as the external behavior is unchanged.
  • The missing lines are not related to the patch's main logic, but are defensive checks for invalid configuration.
  • I am happy to add tests for these error branches if strict coverage is required, but functionally, the patch is already well-tested.

Let me know if you would like me to add these extra tests for completeness!

@shahar4499
Copy link
Contributor

Hey @Edison-A-N , so, since we prioritized supporting Streamable HTTP, since it was the most requested (and long overdue) item, we already used request_context properly and removed the previous hack, as part of this PR: #206.

Closing this PR, feel free to comment if I missed something. Thanks!

@shahar4499 shahar4499 closed this Jul 28, 2025
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