Add warning log when rejecting request with unknown/expired session ID (#2212)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
This commit is contained in:
parent
92f1b1500d
commit
eaf971cf25
2 changed files with 6 additions and 2 deletions
|
|
@ -272,6 +272,7 @@ class StreamableHTTPSessionManager:
|
||||||
# Unknown or expired session ID - return 404 per MCP spec
|
# Unknown or expired session ID - return 404 per MCP spec
|
||||||
# TODO: Align error code once spec clarifies
|
# TODO: Align error code once spec clarifies
|
||||||
# See: https://github.com/modelcontextprotocol/python-sdk/issues/1821
|
# See: https://github.com/modelcontextprotocol/python-sdk/issues/1821
|
||||||
|
logger.info(f"Rejected request with unknown or expired session ID: {request_mcp_session_id[:64]}")
|
||||||
error_response = JSONRPCError(
|
error_response = JSONRPCError(
|
||||||
jsonrpc="2.0",
|
jsonrpc="2.0",
|
||||||
id=None,
|
id=None,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Tests for StreamableHTTPSessionManager."""
|
"""Tests for StreamableHTTPSessionManager."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
|
@ -269,7 +270,7 @@ async def test_stateless_requests_memory_cleanup():
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_unknown_session_id_returns_404():
|
async def test_unknown_session_id_returns_404(caplog: pytest.LogCaptureFixture):
|
||||||
"""Test that requests with unknown session IDs return HTTP 404 per MCP spec."""
|
"""Test that requests with unknown session IDs return HTTP 404 per MCP spec."""
|
||||||
app = Server("test-unknown-session")
|
app = Server("test-unknown-session")
|
||||||
manager = StreamableHTTPSessionManager(app=app)
|
manager = StreamableHTTPSessionManager(app=app)
|
||||||
|
|
@ -299,7 +300,8 @@ async def test_unknown_session_id_returns_404():
|
||||||
async def mock_receive():
|
async def mock_receive():
|
||||||
return {"type": "http.request", "body": b"{}", "more_body": False} # pragma: no cover
|
return {"type": "http.request", "body": b"{}", "more_body": False} # pragma: no cover
|
||||||
|
|
||||||
await manager.handle_request(scope, mock_receive, mock_send)
|
with caplog.at_level(logging.INFO):
|
||||||
|
await manager.handle_request(scope, mock_receive, mock_send)
|
||||||
|
|
||||||
# Find the response start message
|
# Find the response start message
|
||||||
response_start = next(
|
response_start = next(
|
||||||
|
|
@ -315,6 +317,7 @@ async def test_unknown_session_id_returns_404():
|
||||||
assert error_data["id"] is None
|
assert error_data["id"] is None
|
||||||
assert error_data["error"]["code"] == INVALID_REQUEST
|
assert error_data["error"]["code"] == INVALID_REQUEST
|
||||||
assert error_data["error"]["message"] == "Session not found"
|
assert error_data["error"]["message"] == "Session not found"
|
||||||
|
assert "Rejected request with unknown or expired session ID: non-existent-session-id" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue