Repository: jlowin/fastmcp
Pull request #351 (2025-05-07 02:24:58)

Note that get_http_request is deprecated


Patch:
diff --git a/docs/servers/context.mdx b/docs/servers/context.mdx
index 3776500..3a84286 100644
--- a/docs/servers/context.mdx
+++ b/docs/servers/context.mdx
@@ -295,7 +295,6 @@ async def request_info(ctx: Context) -> dict:
 
 ### Advanced Access
 
-For advanced use cases, you can access the underlying MCP session, FastMCP server, and HTTP requests.
 
 #### FastMCP Server and Sessions
 
diff --git a/pyproject.toml b/pyproject.toml
index a209275..15e9881 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -83,7 +83,6 @@ fallback-version = "0.0.0"
 [tool.pytest.ini_options]
 asyncio_mode = "auto"
 asyncio_default_fixture_loop_scope = "session"
-asyncio_default_test_loop_scope = "session"
 filterwarnings = []
 
 
diff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.py
index 31a19d0..79a38d7 100644
--- a/src/fastmcp/server/context.py
+++ b/src/fastmcp/server/context.py
@@ -1,5 +1,6 @@
 from __future__ import annotations as _annotations
 
+import warnings
 from collections.abc import Generator
 from contextlib import contextmanager
 from contextvars import ContextVar, Token
@@ -234,4 +235,13 @@ class Context:
     def get_http_request(self) -> Request:
         """Get the active starlette request."""
 
+        # Deprecation warning, added in FastMCP 2.2.11
+        warnings.warn(
+            "Context.get_http_request() is deprecated and will be removed in a future version. "
+            "Use get_http_request() from fastmcp.server.dependencies instead. "
+            "See https://gofastmcp.com/patterns/http-requests for more details.",
+            DeprecationWarning,
+            stacklevel=2,
+        )
+
         return fastmcp.server.dependencies.get_http_request()


Test patch:
diff --git a/tests/server/test_context.py b/tests/server/test_context.py
new file mode 100644
index 0000000..4243ab4
--- /dev/null
+++ b/tests/server/test_context.py
@@ -0,0 +1,59 @@
+import warnings
+from unittest.mock import MagicMock, patch
+
+import pytest
+from starlette.requests import Request
+
+from fastmcp.server.context import Context
+
+
+class TestContextDeprecations:
+    def test_get_http_request_deprecation_warning(self):
+        """Test that using Context.get_http_request() raises a deprecation warning."""
+        # Create a mock FastMCP instance
+        mock_fastmcp = MagicMock()
+        context = Context(fastmcp=mock_fastmcp)
+
+        # Patch the dependency function to return a mock request
+        mock_request = MagicMock(spec=Request)
+        with patch(
+            "fastmcp.server.dependencies.get_http_request", return_value=mock_request
+        ):
+            # Check that the deprecation warning is raised
+            with pytest.warns(
+                DeprecationWarning, match="Context.get_http_request\\(\\) is deprecated"
+            ):
+                request = context.get_http_request()
+
+            # Verify the function still works and returns the request
+            assert request is mock_request
+
+    def test_get_http_request_deprecation_message(self):
+        """Test that the deprecation warning has the correct message with guidance."""
+        # Create a mock FastMCP instance
+        mock_fastmcp = MagicMock()
+        context = Context(fastmcp=mock_fastmcp)
+
+        # Patch the dependency function to return a mock request
+        mock_request = MagicMock(spec=Request)
+        with patch(
+            "fastmcp.server.dependencies.get_http_request", return_value=mock_request
+        ):
+            # Capture and check the specific warning message
+            with warnings.catch_warnings(record=True) as w:
+                warnings.simplefilter("always")
+                context.get_http_request()
+
+                assert len(w) == 1
+                warning = w[0]
+                assert issubclass(warning.category, DeprecationWarning)
+                assert "Context.get_http_request() is deprecated" in str(
+                    warning.message
+                )
+                assert (
+                    "Use get_http_request() from fastmcp.server.dependencies instead"
+                    in str(warning.message)
+                )
+                assert "https://gofastmcp.com/patterns/http-requests" in str(
+                    warning.message
+                )