Spaces:
Running
Running
Jeremiah Lowin Claude commited on
Commit ·
4f48aed
1
Parent(s): 81da84f
Fix formatting per pre-commit hooks
Browse files🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
src/fastmcp/server/context.py
CHANGED
|
@@ -181,15 +181,15 @@ class Context:
|
|
| 181 |
@property
|
| 182 |
def session_id(self) -> str | None:
|
| 183 |
"""Get the MCP session ID for HTTP transports.
|
| 184 |
-
|
| 185 |
Returns the session ID that can be used as a key for session-based
|
| 186 |
data storage (e.g., Redis) to share data between tool calls within
|
| 187 |
the same client session.
|
| 188 |
-
|
| 189 |
Returns:
|
| 190 |
The session ID for HTTP transports (SSE, StreamableHTTP), or None
|
| 191 |
for stdio and in-memory transports which don't use session IDs.
|
| 192 |
-
|
| 193 |
Example:
|
| 194 |
```python
|
| 195 |
@server.tool
|
|
@@ -202,6 +202,7 @@ class Context:
|
|
| 202 |
"""
|
| 203 |
try:
|
| 204 |
from fastmcp.server.dependencies import get_http_headers
|
|
|
|
| 205 |
headers = get_http_headers(include_all=True)
|
| 206 |
return headers.get("mcp-session-id")
|
| 207 |
except RuntimeError:
|
|
|
|
| 181 |
@property
|
| 182 |
def session_id(self) -> str | None:
|
| 183 |
"""Get the MCP session ID for HTTP transports.
|
| 184 |
+
|
| 185 |
Returns the session ID that can be used as a key for session-based
|
| 186 |
data storage (e.g., Redis) to share data between tool calls within
|
| 187 |
the same client session.
|
| 188 |
+
|
| 189 |
Returns:
|
| 190 |
The session ID for HTTP transports (SSE, StreamableHTTP), or None
|
| 191 |
for stdio and in-memory transports which don't use session IDs.
|
| 192 |
+
|
| 193 |
Example:
|
| 194 |
```python
|
| 195 |
@server.tool
|
|
|
|
| 202 |
"""
|
| 203 |
try:
|
| 204 |
from fastmcp.server.dependencies import get_http_headers
|
| 205 |
+
|
| 206 |
headers = get_http_headers(include_all=True)
|
| 207 |
return headers.get("mcp-session-id")
|
| 208 |
except RuntimeError:
|
tests/server/test_context.py
CHANGED
|
@@ -92,7 +92,7 @@ class TestSessionId:
|
|
| 92 |
def test_session_id_with_http_headers(self, context):
|
| 93 |
"""Test that session_id returns the value from mcp-session-id header."""
|
| 94 |
mock_headers = {"mcp-session-id": "test-session-123"}
|
| 95 |
-
|
| 96 |
with patch(
|
| 97 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 98 |
):
|
|
@@ -101,15 +101,15 @@ class TestSessionId:
|
|
| 101 |
def test_session_id_without_http_headers(self, context):
|
| 102 |
"""Test that session_id returns None when no HTTP headers are available."""
|
| 103 |
with patch(
|
| 104 |
-
"fastmcp.server.dependencies.get_http_headers",
|
| 105 |
-
side_effect=RuntimeError("No active HTTP request found.")
|
| 106 |
):
|
| 107 |
assert context.session_id is None
|
| 108 |
|
| 109 |
def test_session_id_with_missing_header(self, context):
|
| 110 |
"""Test that session_id returns None when mcp-session-id header is missing."""
|
| 111 |
mock_headers = {"other-header": "value"}
|
| 112 |
-
|
| 113 |
with patch(
|
| 114 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 115 |
):
|
|
@@ -118,7 +118,7 @@ class TestSessionId:
|
|
| 118 |
def test_session_id_with_empty_header(self, context):
|
| 119 |
"""Test that session_id returns None when mcp-session-id header is empty."""
|
| 120 |
mock_headers = {"mcp-session-id": ""}
|
| 121 |
-
|
| 122 |
with patch(
|
| 123 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 124 |
):
|
|
|
|
| 92 |
def test_session_id_with_http_headers(self, context):
|
| 93 |
"""Test that session_id returns the value from mcp-session-id header."""
|
| 94 |
mock_headers = {"mcp-session-id": "test-session-123"}
|
| 95 |
+
|
| 96 |
with patch(
|
| 97 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 98 |
):
|
|
|
|
| 101 |
def test_session_id_without_http_headers(self, context):
|
| 102 |
"""Test that session_id returns None when no HTTP headers are available."""
|
| 103 |
with patch(
|
| 104 |
+
"fastmcp.server.dependencies.get_http_headers",
|
| 105 |
+
side_effect=RuntimeError("No active HTTP request found."),
|
| 106 |
):
|
| 107 |
assert context.session_id is None
|
| 108 |
|
| 109 |
def test_session_id_with_missing_header(self, context):
|
| 110 |
"""Test that session_id returns None when mcp-session-id header is missing."""
|
| 111 |
mock_headers = {"other-header": "value"}
|
| 112 |
+
|
| 113 |
with patch(
|
| 114 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 115 |
):
|
|
|
|
| 118 |
def test_session_id_with_empty_header(self, context):
|
| 119 |
"""Test that session_id returns None when mcp-session-id header is empty."""
|
| 120 |
mock_headers = {"mcp-session-id": ""}
|
| 121 |
+
|
| 122 |
with patch(
|
| 123 |
"fastmcp.server.dependencies.get_http_headers", return_value=mock_headers
|
| 124 |
):
|