Skip to content

Commit 52e06fd

Browse files
committed
Format with ruff
1 parent 9ac97e8 commit 52e06fd

File tree

11 files changed

+64
-18
lines changed

11 files changed

+64
-18
lines changed

mcp_python/client/sse.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def remove_request_params(url: str) -> str:
1919

2020

2121
@asynccontextmanager
22-
async def sse_client(url: str, headers: dict[str, Any] | None = None, timeout: float = 5, sse_read_timeout: float = 60 * 5):
22+
async def sse_client(
23+
url: str,
24+
headers: dict[str, Any] | None = None,
25+
timeout: float = 5,
26+
sse_read_timeout: float = 60 * 5,
27+
):
2328
"""
2429
Client transport for SSE.
2530
@@ -104,7 +109,11 @@ async def post_writer(endpoint_url: str):
104109
logger.debug(f"Sending client message: {message}")
105110
response = await client.post(
106111
endpoint_url,
107-
json=message.model_dump(by_alias=True, mode="json", exclude_none=True),
112+
json=message.model_dump(
113+
by_alias=True,
114+
mode="json",
115+
exclude_none=True,
116+
),
108117
)
109118
response.raise_for_status()
110119
logger.debug(

mcp_python/server/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@ async def run(
293293
self,
294294
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
295295
write_stream: MemoryObjectSendStream[JSONRPCMessage],
296-
initialization_options: InitializationOptions
296+
initialization_options: InitializationOptions,
297297
):
298298
with warnings.catch_warnings(record=True) as w:
299-
async with ServerSession(read_stream, write_stream, initialization_options) as session:
299+
async with ServerSession(
300+
read_stream, write_stream, initialization_options
301+
) as session:
300302
async for message in session.incoming_messages:
301303
logger.debug(f"Received message: {message}")
302304

mcp_python/server/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ async def receive_loop(session: ServerSession):
2929
async def main():
3030
version = importlib.metadata.version("mcp_python")
3131
async with stdio_server() as (read_stream, write_stream):
32-
async with ServerSession(read_stream, write_stream, InitializationOptions(server_name="mcp_python", server_version=version, capabilities=ServerCapabilities())) as session, write_stream:
32+
async with ServerSession(
33+
read_stream,
34+
write_stream,
35+
InitializationOptions(
36+
server_name="mcp_python",
37+
server_version=version,
38+
capabilities=ServerCapabilities(),
39+
),
40+
) as session, write_stream:
3341
await receive_loop(session)
3442

3543

mcp_python/server/session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class InitializationOptions:
4444
server_version: str
4545
capabilities: ServerCapabilities
4646

47+
4748
class ServerSession(
4849
BaseSession[
4950
ServerRequest,
@@ -59,7 +60,7 @@ def __init__(
5960
self,
6061
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
6162
write_stream: MemoryObjectSendStream[JSONRPCMessage],
62-
init_options: InitializationOptions
63+
init_options: InitializationOptions,
6364
) -> None:
6465
super().__init__(read_stream, write_stream, ClientRequest, ClientNotification)
6566
self._initialization_state = InitializationState.NotInitialized
@@ -78,7 +79,7 @@ async def _received_request(
7879
capabilities=self._init_options.capabilities,
7980
serverInfo=Implementation(
8081
name=self._init_options.server_name,
81-
version=self._init_options.server_version
82+
version=self._init_options.server_version,
8283
),
8384
)
8485
)

mcp_python/server/sse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ async def sse_writer():
7474
await sse_stream_writer.send(
7575
{
7676
"event": "message",
77-
"data": message.model_dump_json(by_alias=True, exclude_none=True),
77+
"data": message.model_dump_json(
78+
by_alias=True, exclude_none=True
79+
),
7880
}
7981
)
8082

mcp_python/server/stdio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
from mcp_python.types import JSONRPCMessage
99

10+
1011
@asynccontextmanager
1112
async def stdio_server(
12-
stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.AsyncFile[str] | None = None
13+
stdin: anyio.AsyncFile[str] | None = None,
14+
stdout: anyio.AsyncFile[str] | None = None,
1315
):
1416
"""
1517
Server transport for stdio: this communicates with an MCP client by reading from the current process' stdin and writing to stdout.

mcp_python/server/websocket.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ async def ws_writer():
4747
try:
4848
async with write_stream_reader:
4949
async for message in write_stream_reader:
50-
obj = message.model_dump(by_alias=True, mode="json", exclude_none=True)
50+
obj = message.model_dump(
51+
by_alias=True, mode="json", exclude_none=True
52+
)
5153
await websocket.send_json(obj)
5254
except anyio.ClosedResourceError:
5355
await websocket.close()

mcp_python/shared/session.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ async def send_request(
132132
self._response_streams[request_id] = response_stream
133133

134134
jsonrpc_request = JSONRPCRequest(
135-
jsonrpc="2.0", id=request_id, **request.model_dump(by_alias=True, mode="json", exclude_none=True)
135+
jsonrpc="2.0",
136+
id=request_id,
137+
**request.model_dump(by_alias=True, mode="json", exclude_none=True),
136138
)
137139

138140
# TODO: Support progress callbacks
@@ -150,7 +152,8 @@ async def send_notification(self, notification: SendNotificationT) -> None:
150152
Emits a notification, which is a one-way message that does not expect a response.
151153
"""
152154
jsonrpc_notification = JSONRPCNotification(
153-
jsonrpc="2.0", **notification.model_dump(by_alias=True, mode="json", exclude_none=True)
155+
jsonrpc="2.0",
156+
**notification.model_dump(by_alias=True, mode="json", exclude_none=True),
154157
)
155158

156159
await self._write_stream.send(JSONRPCMessage(jsonrpc_notification))
@@ -165,7 +168,9 @@ async def _send_response(
165168
jsonrpc_response = JSONRPCResponse(
166169
jsonrpc="2.0",
167170
id=request_id,
168-
result=response.model_dump(by_alias=True, mode="json", exclude_none=True),
171+
result=response.model_dump(
172+
by_alias=True, mode="json", exclude_none=True
173+
),
169174
)
170175
await self._write_stream.send(JSONRPCMessage(jsonrpc_response))
171176

@@ -180,7 +185,9 @@ async def _receive_loop(self) -> None:
180185
await self._incoming_message_stream_writer.send(message)
181186
elif isinstance(message.root, JSONRPCRequest):
182187
validated_request = self._receive_request_type.model_validate(
183-
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
188+
message.root.model_dump(
189+
by_alias=True, mode="json", exclude_none=True
190+
)
184191
)
185192
responder = RequestResponder(
186193
request_id=message.root.id,
@@ -196,7 +203,9 @@ async def _receive_loop(self) -> None:
196203
await self._incoming_message_stream_writer.send(responder)
197204
elif isinstance(message.root, JSONRPCNotification):
198205
notification = self._receive_notification_type.model_validate(
199-
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
206+
message.root.model_dump(
207+
by_alias=True, mode="json", exclude_none=True
208+
)
200209
)
201210

202211
await self._received_notification(notification)

mcp_python/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Meta(BaseModel):
4141
This parameter name is reserved by MCP to allow clients and servers to attach additional metadata to their notifications.
4242
"""
4343

44+
4445
RequestParamsT = TypeVar("RequestParamsT", bound=RequestParams)
4546
NotificationParamsT = TypeVar("NotificationParamsT", bound=NotificationParams)
4647
MethodT = TypeVar("MethodT", bound=str)

tests/client/test_session.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ async def mock_server():
5959
JSONRPCResponse(
6060
jsonrpc="2.0",
6161
id=jsonrpc_request.root.id,
62-
result=result.model_dump(by_alias=True, mode="json", exclude_none=True),
62+
result=result.model_dump(
63+
by_alias=True, mode="json", exclude_none=True
64+
),
6365
)
6466
)
6567
)
6668
jsonrpc_notification = await client_to_server_receive.receive()
6769
assert isinstance(jsonrpc_notification.root, JSONRPCNotification)
6870
initialized_notification = ClientNotification.model_validate(
69-
jsonrpc_notification.model_dump(by_alias=True, mode="json", exclude_none=True)
71+
jsonrpc_notification.model_dump(
72+
by_alias=True, mode="json", exclude_none=True
73+
)
7074
)
7175

7276
async def listen_session():

tests/server/test_session.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ async def run_server():
3131
nonlocal received_initialized
3232

3333
async with ServerSession(
34-
client_to_server_receive, server_to_client_send, InitializationOptions(server_name='mcp_python', server_version='0.1.0', capabilities=ServerCapabilities())
34+
client_to_server_receive,
35+
server_to_client_send,
36+
InitializationOptions(
37+
server_name="mcp_python",
38+
server_version="0.1.0",
39+
capabilities=ServerCapabilities(),
40+
),
3541
) as server_session:
3642
async for message in server_session.incoming_messages:
3743
if isinstance(message, Exception):

0 commit comments

Comments
 (0)