Jeremiah Lowin commited on
Commit
9a8ac2f
·
unverified ·
2 Parent(s): 1e267a0e722f91

Merge pull request #351 from jlowin/http-docs

Browse files
docs/servers/context.mdx CHANGED
@@ -295,7 +295,6 @@ async def request_info(ctx: Context) -> dict:
295
 
296
  ### Advanced Access
297
 
298
- For advanced use cases, you can access the underlying MCP session, FastMCP server, and HTTP requests.
299
 
300
  #### FastMCP Server and Sessions
301
 
 
295
 
296
  ### Advanced Access
297
 
 
298
 
299
  #### FastMCP Server and Sessions
300
 
src/fastmcp/server/context.py CHANGED
@@ -1,5 +1,6 @@
1
  from __future__ import annotations as _annotations
2
 
 
3
  from collections.abc import Generator
4
  from contextlib import contextmanager
5
  from contextvars import ContextVar, Token
@@ -234,4 +235,13 @@ class Context:
234
  def get_http_request(self) -> Request:
235
  """Get the active starlette request."""
236
 
 
 
 
 
 
 
 
 
 
237
  return fastmcp.server.dependencies.get_http_request()
 
1
  from __future__ import annotations as _annotations
2
 
3
+ import warnings
4
  from collections.abc import Generator
5
  from contextlib import contextmanager
6
  from contextvars import ContextVar, Token
 
235
  def get_http_request(self) -> Request:
236
  """Get the active starlette request."""
237
 
238
+ # Deprecation warning, added in FastMCP 2.2.11
239
+ warnings.warn(
240
+ "Context.get_http_request() is deprecated and will be removed in a future version. "
241
+ "Use get_http_request() from fastmcp.server.dependencies instead. "
242
+ "See https://gofastmcp.com/patterns/http-requests for more details.",
243
+ DeprecationWarning,
244
+ stacklevel=2,
245
+ )
246
+
247
  return fastmcp.server.dependencies.get_http_request()
tests/server/test_context.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+ from unittest.mock import MagicMock, patch
3
+
4
+ import pytest
5
+ from starlette.requests import Request
6
+
7
+ from fastmcp.server.context import Context
8
+
9
+
10
+ class TestContextDeprecations:
11
+ def test_get_http_request_deprecation_warning(self):
12
+ """Test that using Context.get_http_request() raises a deprecation warning."""
13
+ # Create a mock FastMCP instance
14
+ mock_fastmcp = MagicMock()
15
+ context = Context(fastmcp=mock_fastmcp)
16
+
17
+ # Patch the dependency function to return a mock request
18
+ mock_request = MagicMock(spec=Request)
19
+ with patch(
20
+ "fastmcp.server.dependencies.get_http_request", return_value=mock_request
21
+ ):
22
+ # Check that the deprecation warning is raised
23
+ with pytest.warns(
24
+ DeprecationWarning, match="Context.get_http_request\\(\\) is deprecated"
25
+ ):
26
+ request = context.get_http_request()
27
+
28
+ # Verify the function still works and returns the request
29
+ assert request is mock_request
30
+
31
+ def test_get_http_request_deprecation_message(self):
32
+ """Test that the deprecation warning has the correct message with guidance."""
33
+ # Create a mock FastMCP instance
34
+ mock_fastmcp = MagicMock()
35
+ context = Context(fastmcp=mock_fastmcp)
36
+
37
+ # Patch the dependency function to return a mock request
38
+ mock_request = MagicMock(spec=Request)
39
+ with patch(
40
+ "fastmcp.server.dependencies.get_http_request", return_value=mock_request
41
+ ):
42
+ # Capture and check the specific warning message
43
+ with warnings.catch_warnings(record=True) as w:
44
+ warnings.simplefilter("always")
45
+ context.get_http_request()
46
+
47
+ assert len(w) == 1
48
+ warning = w[0]
49
+ assert issubclass(warning.category, DeprecationWarning)
50
+ assert "Context.get_http_request() is deprecated" in str(
51
+ warning.message
52
+ )
53
+ assert (
54
+ "Use get_http_request() from fastmcp.server.dependencies instead"
55
+ in str(warning.message)
56
+ )
57
+ assert "https://gofastmcp.com/patterns/http-requests" in str(
58
+ warning.message
59
+ )