Skip to content

Commit 87e07b8

Browse files
committed
formatting and linting
1 parent c2bb049 commit 87e07b8

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ async def run_stdio_async(self) -> None:
472472
async def run_sse_async(self) -> None:
473473
"""Run the server using SSE transport."""
474474
import uvicorn
475+
475476
starlette_app = self.sse_app()
476477

477478
config = uvicorn.Config(

src/mcp/server/message_queue/base.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import asynccontextmanager
44
from typing import Protocol, runtime_checkable
55
from uuid import UUID
6+
67
from pydantic import ValidationError
78

89
import mcp.types as types
@@ -59,13 +60,13 @@ async def session_exists(self, session_id: UUID) -> bool:
5960
class InMemoryMessageDispatch:
6061
"""Default in-memory implementation of the MessageDispatch interface.
6162
62-
This implementation immediately dispatches messages to registered callbacks when
63+
This implementation immediately dispatches messages to registered callbacks when
6364
messages are received without any queuing behavior.
6465
"""
6566

6667
def __init__(self) -> None:
6768
self._callbacks: dict[UUID, MessageCallback] = {}
68-
# We don't need a separate _active_sessions set since _callbacks already tracks this
69+
# _callbacks tracks active sessions, no need for separate _active_sessions set
6970

7071
async def publish_message(
7172
self, session_id: UUID, message: types.JSONRPCMessage | str
@@ -74,19 +75,19 @@ async def publish_message(
7475
if session_id not in self._callbacks:
7576
logger.warning(f"Message dropped: unknown session {session_id}")
7677
return False
77-
78-
# For string messages, attempt parsing and recreate original ValidationError if invalid
78+
79+
# Parse string messages or recreate original ValidationError
7980
if isinstance(message, str):
8081
try:
8182
callback_argument = types.JSONRPCMessage.model_validate_json(message)
8283
except ValidationError as exc:
8384
callback_argument = exc
8485
else:
8586
callback_argument = message
86-
87+
8788
# Call the callback with either valid message or recreated ValidationError
8889
await self._callbacks[session_id](callback_argument)
89-
90+
9091
logger.debug(f"Message dispatched to session {session_id}")
9192
return True
9293

src/mcp/server/message_queue/redis.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from contextlib import asynccontextmanager
33
from typing import Any, cast
44
from uuid import UUID
5-
from pydantic import ValidationError
65

76
import anyio
87
from anyio import CapacityLimiter, lowlevel
8+
from pydantic import ValidationError
99

1010
import mcp.types as types
1111
from mcp.server.message_queue.base import MessageCallback
@@ -68,7 +68,7 @@ async def subscribe(self, session_id: UUID, callback: MessageCallback):
6868
await self._pubsub.unsubscribe(channel) # type: ignore
6969
await self._redis.srem(self._active_sessions_key, session_id.hex)
7070
del self._callbacks[session_id]
71-
logger.debug(f"Unsubscribed from Redis channel for session {session_id}")
71+
logger.debug(f"Unsubscribed from Redis channel: {session_id}")
7272

7373
async def _listen_for_messages(self) -> None:
7474
"""Background task that listens for messages on subscribed channels."""
@@ -84,37 +84,37 @@ async def _listen_for_messages(self) -> None:
8484

8585
channel: str = cast(str, message["channel"])
8686
expected_prefix = f"{self._prefix}session:"
87-
87+
8888
if not channel.startswith(expected_prefix):
8989
logger.debug(f"Ignoring message from non-MCP channel: {channel}")
9090
continue
91-
92-
session_hex = channel[len(expected_prefix):]
91+
92+
session_hex = channel[len(expected_prefix) :]
9393
try:
9494
session_id = UUID(hex=session_hex)
9595
expected_channel = self._session_channel(session_id)
9696
if channel != expected_channel:
9797
logger.error(f"Channel format mismatch: {channel}")
9898
continue
9999
except ValueError:
100-
logger.error(f"Received message with invalid UUID in channel: {channel}")
100+
logger.error(f"Invalid UUID in channel: {channel}")
101101
continue
102102

103103
data: str = cast(str, message["data"])
104104
try:
105105
if session_id not in self._callbacks:
106-
logger.warning(f"Message dropped: no callback for session {session_id}")
106+
logger.warning(f"Message dropped: no callback for {session_id}")
107107
continue
108-
108+
109109
# Try to parse as valid message or recreate original ValidationError
110110
try:
111111
msg = types.JSONRPCMessage.model_validate_json(data)
112112
await self._callbacks[session_id](msg)
113113
except ValidationError as exc:
114-
# Pass the identical validation error that would have occurred originally
114+
# Pass the identical validation error that would have occurred
115115
await self._callbacks[session_id](exc)
116116
except Exception as e:
117-
logger.error(f"Error processing message for session {session_id}: {e}")
117+
logger.error(f"Error processing message for {session_id}: {e}")
118118

119119
async def publish_message(
120120
self, session_id: UUID, message: types.JSONRPCMessage | str

src/mcp/server/sse.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ async def sse_writer():
137137
logger.debug("Starting SSE response task")
138138
tg.start_soon(response, scope, receive, send)
139139

140-
async with self._message_dispatch.subscribe(
141-
session_id, message_callback
142-
):
140+
async with self._message_dispatch.subscribe(session_id, message_callback):
143141
logger.debug("Yielding read and write streams")
144142
yield (read_stream, write_stream)
145143

@@ -178,8 +176,8 @@ async def handle_post_message(
178176
logger.error(f"Failed to parse message: {err}")
179177
response = Response("Could not parse message", status_code=400)
180178
await response(scope, receive, send)
181-
# Pass raw JSON string through dispatch; original ValidationError will be recreated when
182-
# the receiver tries to validate the same invalid JSON
179+
# Pass raw JSON string; receiver will recreate identical ValidationError
180+
# when parsing the same invalid JSON
183181
await self._message_dispatch.publish_message(session_id, body.decode())
184182
return
185183

tests/server/fastmcp/servers/test_file_server.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,13 @@ async def test_read_resource_file(mcp: FastMCP):
114114

115115
@pytest.mark.anyio
116116
async def test_delete_file(mcp: FastMCP, test_dir: Path):
117-
await mcp.call_tool(
118-
"delete_file", arguments={"path": str(test_dir / "example.py")}
119-
)
117+
await mcp.call_tool("delete_file", arguments={"path": str(test_dir / "example.py")})
120118
assert not (test_dir / "example.py").exists()
121119

122120

123121
@pytest.mark.anyio
124122
async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
125-
await mcp.call_tool(
126-
"delete_file", arguments={"path": str(test_dir / "example.py")}
127-
)
123+
await mcp.call_tool("delete_file", arguments={"path": str(test_dir / "example.py")})
128124
res_iter = await mcp.read_resource("file://test_dir/example.py")
129125
res_list = list(res_iter)
130126
assert len(res_list) == 1

0 commit comments

Comments
 (0)