Skip to content

Commit 4040945

Browse files
committed
Exclude Nones
1 parent 7b55252 commit 4040945

File tree

9 files changed

+15
-15
lines changed

9 files changed

+15
-15
lines changed

mcp_python/client/sse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def post_writer(endpoint_url: str):
104104
logger.debug(f"Sending client message: {message}")
105105
response = await client.post(
106106
endpoint_url,
107-
json=message.model_dump(by_alias=True, mode="json"),
107+
json=message.model_dump(by_alias=True, mode="json", exclude_none=True),
108108
)
109109
response.raise_for_status()
110110
logger.debug(

mcp_python/client/stdio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def stdin_writer():
7070
try:
7171
async with write_stream_reader:
7272
async for message in write_stream_reader:
73-
json = message.model_dump_json(by_alias=True)
73+
json = message.model_dump_json(by_alias=True, exclude_none=True)
7474
await process.stdin.send((json + "\n").encode())
7575
except anyio.ClosedResourceError:
7676
await anyio.lowlevel.checkpoint()

mcp_python/server/sse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def sse_writer():
7474
await sse_stream_writer.send(
7575
{
7676
"event": "message",
77-
"data": message.model_dump_json(by_alias=True),
77+
"data": message.model_dump_json(by_alias=True, exclude_none=True),
7878
}
7979
)
8080

mcp_python/server/stdio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def stdout_writer():
4848
try:
4949
async with write_stream_reader:
5050
async for message in write_stream_reader:
51-
json = message.model_dump_json(by_alias=True)
51+
json = message.model_dump_json(by_alias=True, exclude_none=True)
5252
await stdout.write(json + "\n")
5353
await stdout.flush()
5454
except anyio.ClosedResourceError:

mcp_python/server/websocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ 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")
50+
obj = message.model_dump(by_alias=True, mode="json", exclude_none=True)
5151
await websocket.send_json(obj)
5252
except anyio.ClosedResourceError:
5353
await websocket.close()

mcp_python/shared/session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ 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")
135+
jsonrpc="2.0", id=request_id, **request.model_dump(by_alias=True, mode="json", exclude_none=True)
136136
)
137137

138138
# TODO: Support progress callbacks
@@ -150,7 +150,7 @@ async def send_notification(self, notification: SendNotificationT) -> None:
150150
Emits a notification, which is a one-way message that does not expect a response.
151151
"""
152152
jsonrpc_notification = JSONRPCNotification(
153-
jsonrpc="2.0", **notification.model_dump(by_alias=True, mode="json")
153+
jsonrpc="2.0", **notification.model_dump(by_alias=True, mode="json", exclude_none=True)
154154
)
155155

156156
await self._write_stream.send(JSONRPCMessage(jsonrpc_notification))
@@ -165,7 +165,7 @@ async def _send_response(
165165
jsonrpc_response = JSONRPCResponse(
166166
jsonrpc="2.0",
167167
id=request_id,
168-
result=response.model_dump(by_alias=True, mode="json"),
168+
result=response.model_dump(by_alias=True, mode="json", exclude_none=True),
169169
)
170170
await self._write_stream.send(JSONRPCMessage(jsonrpc_response))
171171

@@ -180,7 +180,7 @@ async def _receive_loop(self) -> None:
180180
await self._incoming_message_stream_writer.send(message)
181181
elif isinstance(message.root, JSONRPCRequest):
182182
validated_request = self._receive_request_type.model_validate(
183-
message.root.model_dump(by_alias=True, mode="json")
183+
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
184184
)
185185
responder = RequestResponder(
186186
request_id=message.root.id,
@@ -196,7 +196,7 @@ async def _receive_loop(self) -> None:
196196
await self._incoming_message_stream_writer.send(responder)
197197
elif isinstance(message.root, JSONRPCNotification):
198198
notification = self._receive_notification_type.model_validate(
199-
message.root.model_dump(by_alias=True, mode="json")
199+
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
200200
)
201201

202202
await self._received_notification(notification)

tests/client/test_session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async def mock_server():
3535
jsonrpc_request = await client_to_server_receive.receive()
3636
assert isinstance(jsonrpc_request.root, JSONRPCRequest)
3737
request = ClientRequest.model_validate(
38-
jsonrpc_request.model_dump(by_alias=True, mode="json")
38+
jsonrpc_request.model_dump(by_alias=True, mode="json", exclude_none=True)
3939
)
4040
assert isinstance(request.root, InitializeRequest)
4141

@@ -59,14 +59,14 @@ 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"),
62+
result=result.model_dump(by_alias=True, mode="json", exclude_none=True),
6363
)
6464
)
6565
)
6666
jsonrpc_notification = await client_to_server_receive.receive()
6767
assert isinstance(jsonrpc_notification.root, JSONRPCNotification)
6868
initialized_notification = ClientNotification.model_validate(
69-
jsonrpc_notification.model_dump(by_alias=True, mode="json")
69+
jsonrpc_notification.model_dump(by_alias=True, mode="json", exclude_none=True)
7070
)
7171

7272
async def listen_session():

tests/server/test_stdio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async def test_stdio_server():
1818
]
1919

2020
for message in messages:
21-
stdin.write(message.model_dump_json() + "\n")
21+
stdin.write(message.model_dump_json(by_alias=True, exclude_none=True) + "\n")
2222
stdin.seek(0)
2323

2424
async with stdio_server(

tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_jsonrpc_request():
1515

1616
request = JSONRPCMessage.model_validate(json_data)
1717
assert isinstance(request.root, JSONRPCRequest)
18-
ClientRequest.model_validate(request.model_dump(by_alias=True))
18+
ClientRequest.model_validate(request.model_dump(by_alias=True, exclude_none=True))
1919

2020
assert request.root.jsonrpc == "2.0"
2121
assert request.root.id == 1

0 commit comments

Comments
 (0)