Skip to content

Commit fe74019

Browse files
committed
Removed now unneeded "pragma: no cover"
1 parent 11d0456 commit fe74019

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,7 @@ async def _handle_request(
739739
request_data = None
740740
close_sse_stream_cb = None
741741
close_standalone_sse_stream_cb = None
742-
if message.message_metadata is not None and isinstance(
743-
message.message_metadata, ServerMessageMetadata
744-
): # pragma: no cover
742+
if message.message_metadata is not None and isinstance(message.message_metadata, ServerMessageMetadata):
745743
request_data = message.message_metadata.request_context
746744
close_sse_stream_cb = message.message_metadata.close_sse_stream
747745
close_standalone_sse_stream_cb = message.message_metadata.close_standalone_sse_stream

src/mcp/server/streamable_http.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async def store_event(self, stream_id: StreamId, message: JSONRPCMessage | None)
9191
Returns:
9292
The generated event ID for the stored event
9393
"""
94-
pass # pragma: no cover
94+
pass
9595

9696
@abstractmethod
9797
async def replay_events_after(
@@ -108,7 +108,7 @@ async def replay_events_after(
108108
Returns:
109109
The stream ID of the replayed events
110110
"""
111-
pass # pragma: no cover
111+
pass
112112

113113

114114
class StreamableHTTPServerTransport:
@@ -175,7 +175,7 @@ def is_terminated(self) -> bool:
175175
"""Check if this transport has been explicitly terminated."""
176176
return self._terminated
177177

178-
def close_sse_stream(self, request_id: RequestId) -> None: # pragma: no cover
178+
def close_sse_stream(self, request_id: RequestId) -> None:
179179
"""Close SSE connection for a specific request without terminating the stream.
180180
181181
This method closes the HTTP connection for the specified request, triggering
@@ -203,7 +203,7 @@ def close_sse_stream(self, request_id: RequestId) -> None: # pragma: no cover
203203
send_stream.close()
204204
receive_stream.close()
205205

206-
def close_standalone_sse_stream(self) -> None: # pragma: no cover
206+
def close_standalone_sse_stream(self) -> None:
207207
"""Close the standalone GET SSE stream, triggering client reconnection.
208208
209209
This method closes the HTTP connection for the standalone GET stream used
@@ -238,10 +238,10 @@ def _create_session_message(
238238
# Only provide close callbacks when client supports resumability
239239
if self._event_store and protocol_version >= "2025-11-25":
240240

241-
async def close_stream_callback() -> None: # pragma: no cover
241+
async def close_stream_callback() -> None:
242242
self.close_sse_stream(request_id)
243243

244-
async def close_standalone_stream_callback() -> None: # pragma: no cover
244+
async def close_standalone_stream_callback() -> None:
245245
self.close_standalone_sse_stream()
246246

247247
metadata = ServerMessageMetadata(
@@ -289,7 +289,7 @@ def _create_error_response(
289289
) -> Response:
290290
"""Create an error response with a simple string message."""
291291
response_headers = {"Content-Type": CONTENT_TYPE_JSON}
292-
if headers: # pragma: no cover
292+
if headers:
293293
response_headers.update(headers)
294294

295295
if self.mcp_session_id:
@@ -328,11 +328,11 @@ def _create_json_response(
328328
headers=response_headers,
329329
)
330330

331-
def _get_session_id(self, request: Request) -> str | None: # pragma: no cover
331+
def _get_session_id(self, request: Request) -> str | None:
332332
"""Extract the session ID from request headers."""
333333
return request.headers.get(MCP_SESSION_ID_HEADER)
334334

335-
def _create_event_data(self, event_message: EventMessage) -> dict[str, str]: # pragma: no cover
335+
def _create_event_data(self, event_message: EventMessage) -> dict[str, str]:
336336
"""Create event data dictionary from an EventMessage."""
337337
event_data = {
338338
"event": "message",
@@ -352,7 +352,7 @@ async def _clean_up_memory_streams(self, request_id: RequestId) -> None:
352352
# Close the request stream
353353
await self._request_streams[request_id][0].aclose()
354354
await self._request_streams[request_id][1].aclose()
355-
except Exception: # pragma: no cover
355+
except Exception:
356356
# During cleanup, we catch all exceptions since streams might be in various states
357357
logger.debug("Error closing memory streams - may already be closed")
358358
finally:
@@ -370,7 +370,7 @@ async def handle_request(self, scope: Scope, receive: Receive, send: Send) -> No
370370
await error_response(scope, receive, send)
371371
return
372372

373-
if self._terminated: # pragma: no cover
373+
if self._terminated:
374374
# If the session has been terminated, return 404 Not Found
375375
response = self._create_error_response(
376376
"Not Found: Session has been terminated",
@@ -381,11 +381,11 @@ async def handle_request(self, scope: Scope, receive: Receive, send: Send) -> No
381381

382382
if request.method == "POST":
383383
await self._handle_post_request(scope, request, receive, send)
384-
elif request.method == "GET": # pragma: no cover
384+
elif request.method == "GET":
385385
await self._handle_get_request(request, send)
386-
elif request.method == "DELETE": # pragma: no cover
386+
elif request.method == "DELETE":
387387
await self._handle_delete_request(request, send)
388-
else: # pragma: no cover
388+
else:
389389
await self._handle_unsupported_request(request, send)
390390

391391
def _check_accept_headers(self, request: Request) -> tuple[bool, bool]:
@@ -430,15 +430,15 @@ async def _validate_accept_header(self, request: Request, scope: Scope, send: Se
430430
async def _handle_post_request(self, scope: Scope, request: Request, receive: Receive, send: Send) -> None: # noqa: PLR0915
431431
"""Handle POST requests containing JSON-RPC messages."""
432432
writer = self._read_stream_writer
433-
if writer is None: # pragma: no cover
433+
if writer is None:
434434
raise ValueError("No read stream writer available. Ensure connect() is called first.")
435435
try:
436436
# Validate Accept header
437437
if not await self._validate_accept_header(request, scope, send):
438438
return
439439

440440
# Validate Content-Type
441-
if not self._check_content_type(request): # pragma: no cover
441+
if not self._check_content_type(request):
442442
response = self._create_error_response(
443443
"Unsupported Media Type: Content-Type must be application/json",
444444
HTTPStatus.UNSUPPORTED_MEDIA_TYPE,
@@ -458,7 +458,7 @@ async def _handle_post_request(self, scope: Scope, request: Request, receive: Re
458458

459459
try:
460460
message = jsonrpc_message_adapter.validate_python(raw_message, by_name=False)
461-
except ValidationError as e: # pragma: no cover
461+
except ValidationError as e:
462462
response = self._create_error_response(
463463
f"Validation error: {str(e)}",
464464
HTTPStatus.BAD_REQUEST,
@@ -470,7 +470,7 @@ async def _handle_post_request(self, scope: Scope, request: Request, receive: Re
470470
# Check if this is an initialization request
471471
is_initialization_request = isinstance(message, JSONRPCRequest) and message.method == "initialize"
472472

473-
if is_initialization_request: # pragma: no cover
473+
if is_initialization_request:
474474
# Check if the server already has an established session
475475
if self.mcp_session_id:
476476
# Check if request has a session ID
@@ -484,11 +484,11 @@ async def _handle_post_request(self, scope: Scope, request: Request, receive: Re
484484
)
485485
await response(scope, receive, send)
486486
return
487-
elif not await self._validate_request_headers(request, send): # pragma: no cover
487+
elif not await self._validate_request_headers(request, send):
488488
return
489489

490490
# For notifications and responses only, return 202 Accepted
491-
if not isinstance(message, JSONRPCRequest): # pragma: no cover
491+
if not isinstance(message, JSONRPCRequest):
492492
# Create response object and send it
493493
response = self._create_json_response(
494494
None,
@@ -535,23 +535,23 @@ async def _handle_post_request(self, scope: Scope, request: Request, receive: Re
535535
response_message = event_message.message
536536
break
537537
# For notifications and request, keep waiting
538-
else: # pragma: no cover
538+
else:
539539
logger.debug(f"received: {event_message.message.method}")
540540

541541
# At this point we should have a response
542542
if response_message:
543543
# Create JSON response
544544
response = self._create_json_response(response_message)
545545
await response(scope, receive, send)
546-
else: # pragma: no cover
546+
else:
547547
# This shouldn't happen in normal operation
548548
logger.error("No response message received before stream closed")
549549
response = self._create_error_response(
550550
"Error processing request: No response received",
551551
HTTPStatus.INTERNAL_SERVER_ERROR,
552552
)
553553
await response(scope, receive, send)
554-
except Exception: # pragma: no cover
554+
except Exception:
555555
logger.exception("Error processing JSON response")
556556
response = self._create_error_response(
557557
"Error processing request",
@@ -561,7 +561,7 @@ async def _handle_post_request(self, scope: Scope, request: Request, receive: Re
561561
await response(scope, receive, send)
562562
finally:
563563
await self._clean_up_memory_streams(request_id)
564-
else: # pragma: no cover
564+
else:
565565
# Create SSE stream
566566
sse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream[dict[str, str]](0)
567567

@@ -624,7 +624,7 @@ async def sse_writer():
624624
finally:
625625
await sse_stream_reader.aclose()
626626

627-
except Exception as err: # pragma: no cover
627+
except Exception as err:
628628
logger.exception("Error handling POST request")
629629
response = self._create_error_response(
630630
f"Error handling POST request: {err}",
@@ -636,7 +636,7 @@ async def sse_writer():
636636
await writer.send(Exception(err))
637637
return
638638

639-
async def _handle_get_request(self, request: Request, send: Send) -> None: # pragma: no cover
639+
async def _handle_get_request(self, request: Request, send: Send) -> None:
640640
"""Handle GET request to establish SSE.
641641
642642
This allows the server to communicate to the client without the client
@@ -728,7 +728,7 @@ async def standalone_sse_writer():
728728
await sse_stream_writer.aclose()
729729
await sse_stream_reader.aclose()
730730

731-
async def _handle_delete_request(self, request: Request, send: Send) -> None: # pragma: no cover
731+
async def _handle_delete_request(self, request: Request, send: Send) -> None:
732732
"""Handle DELETE requests for explicit session termination."""
733733
# Validate session ID
734734
if not self.mcp_session_id:
@@ -778,11 +778,11 @@ async def terminate(self) -> None:
778778
await self._write_stream_reader.aclose()
779779
if self._write_stream is not None: # pragma: no branch
780780
await self._write_stream.aclose()
781-
except Exception as e: # pragma: no cover
781+
except Exception as e:
782782
# During cleanup, we catch all exceptions since streams might be in various states
783783
logger.debug(f"Error closing streams: {e}")
784784

785-
async def _handle_unsupported_request(self, request: Request, send: Send) -> None: # pragma: no cover
785+
async def _handle_unsupported_request(self, request: Request, send: Send) -> None:
786786
"""Handle unsupported HTTP methods."""
787787
headers = {
788788
"Content-Type": CONTENT_TYPE_JSON,
@@ -798,14 +798,14 @@ async def _handle_unsupported_request(self, request: Request, send: Send) -> Non
798798
)
799799
await response(request.scope, request.receive, send)
800800

801-
async def _validate_request_headers(self, request: Request, send: Send) -> bool: # pragma: no cover
801+
async def _validate_request_headers(self, request: Request, send: Send) -> bool:
802802
if not await self._validate_session(request, send):
803803
return False
804804
if not await self._validate_protocol_version(request, send):
805805
return False
806806
return True
807807

808-
async def _validate_session(self, request: Request, send: Send) -> bool: # pragma: no cover
808+
async def _validate_session(self, request: Request, send: Send) -> bool:
809809
"""Validate the session ID in the request."""
810810
if not self.mcp_session_id:
811811
# If we're not using session IDs, return True
@@ -834,7 +834,7 @@ async def _validate_session(self, request: Request, send: Send) -> bool: # prag
834834

835835
return True
836836

837-
async def _validate_protocol_version(self, request: Request, send: Send) -> bool: # pragma: no cover
837+
async def _validate_protocol_version(self, request: Request, send: Send) -> bool:
838838
"""Validate the protocol version header in the request."""
839839
# Get the protocol version from the request headers
840840
protocol_version = request.headers.get(MCP_PROTOCOL_VERSION_HEADER)
@@ -856,7 +856,7 @@ async def _validate_protocol_version(self, request: Request, send: Send) -> bool
856856

857857
return True
858858

859-
async def _replay_events(self, last_event_id: str, request: Request, send: Send) -> None: # pragma: no cover
859+
async def _replay_events(self, last_event_id: str, request: Request, send: Send) -> None:
860860
"""Replays events that would have been sent after the specified event ID.
861861
Only used when resumability is enabled.
862862
"""
@@ -982,7 +982,7 @@ async def message_router():
982982
# send it there
983983
target_request_id = response_id
984984
# Extract related_request_id from meta if it exists
985-
elif ( # pragma: no cover
985+
elif (
986986
session_message.metadata is not None
987987
and isinstance(
988988
session_message.metadata,
@@ -1006,13 +1006,13 @@ async def message_router():
10061006
try:
10071007
# Send both the message and the event ID
10081008
await self._request_streams[request_stream_id][0].send(EventMessage(message, event_id))
1009-
except ( # pragma: no cover
1009+
except (
10101010
anyio.BrokenResourceError,
10111011
anyio.ClosedResourceError,
10121012
):
10131013
# Stream might be closed, remove from registry
10141014
self._request_streams.pop(request_stream_id, None)
1015-
else: # pragma: no cover
1015+
else:
10161016
logger.debug(
10171017
f"""Request stream {request_stream_id} not found
10181018
for message. Still processing message as the client
@@ -1043,6 +1043,6 @@ async def message_router():
10431043
await read_stream.aclose()
10441044
await write_stream_reader.aclose()
10451045
await write_stream.aclose()
1046-
except Exception as e: # pragma: no cover
1046+
except Exception as e:
10471047
# During cleanup, we catch all exceptions since streams might be in various states
10481048
logger.debug(f"Error closing streams: {e}")

src/mcp/server/streamable_http_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def run_stateless_server(*, task_status: TaskStatus[None] = anyio.TASK_STA
161161
self.app.create_initialization_options(),
162162
stateless=True,
163163
)
164-
except Exception: # pragma: no cover
164+
except Exception:
165165
logger.exception("Stateless session crashed")
166166

167167
# Assert task group is not None for type checking
@@ -181,7 +181,7 @@ async def _handle_stateful_request(self, scope: Scope, receive: Receive, send: S
181181
request_mcp_session_id = request.headers.get(MCP_SESSION_ID_HEADER)
182182

183183
# Existing session case
184-
if request_mcp_session_id is not None and request_mcp_session_id in self._server_instances: # pragma: no cover
184+
if request_mcp_session_id is not None and request_mcp_session_id in self._server_instances:
185185
transport = self._server_instances[request_mcp_session_id]
186186
logger.debug("Session already exists, handling request directly")
187187
await transport.handle_request(scope, receive, send)
@@ -261,5 +261,5 @@ class StreamableHTTPASGIApp:
261261
def __init__(self, session_manager: StreamableHTTPSessionManager):
262262
self.session_manager = session_manager
263263

264-
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: # pragma: no cover
264+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
265265
await self.session_manager.handle_request(scope, receive, send)

0 commit comments

Comments
 (0)