Skip to content

Exclude Nones when serializing models #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mcp_python/client/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def post_writer(endpoint_url: str):
logger.debug(f"Sending client message: {message}")
response = await client.post(
endpoint_url,
json=message.model_dump(by_alias=True, mode="json"),
json=message.model_dump(by_alias=True, mode="json", exclude_none=True),
)
response.raise_for_status()
logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion mcp_python/client/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def stdin_writer():
try:
async with write_stream_reader:
async for message in write_stream_reader:
json = message.model_dump_json(by_alias=True)
json = message.model_dump_json(by_alias=True, exclude_none=True)
await process.stdin.send((json + "\n").encode())
except anyio.ClosedResourceError:
await anyio.lowlevel.checkpoint()
Expand Down
2 changes: 1 addition & 1 deletion mcp_python/server/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def sse_writer():
await sse_stream_writer.send(
{
"event": "message",
"data": message.model_dump_json(by_alias=True),
"data": message.model_dump_json(by_alias=True, exclude_none=True),
}
)

Expand Down
2 changes: 1 addition & 1 deletion mcp_python/server/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def stdout_writer():
try:
async with write_stream_reader:
async for message in write_stream_reader:
json = message.model_dump_json(by_alias=True)
json = message.model_dump_json(by_alias=True, exclude_none=True)
await stdout.write(json + "\n")
await stdout.flush()
except anyio.ClosedResourceError:
Expand Down
2 changes: 1 addition & 1 deletion mcp_python/server/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def ws_writer():
try:
async with write_stream_reader:
async for message in write_stream_reader:
obj = message.model_dump(by_alias=True, mode="json")
obj = message.model_dump(by_alias=True, mode="json", exclude_none=True)
await websocket.send_json(obj)
except anyio.ClosedResourceError:
await websocket.close()
Expand Down
10 changes: 5 additions & 5 deletions mcp_python/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def send_request(
self._response_streams[request_id] = response_stream

jsonrpc_request = JSONRPCRequest(
jsonrpc="2.0", id=request_id, **request.model_dump(by_alias=True, mode="json")
jsonrpc="2.0", id=request_id, **request.model_dump(by_alias=True, mode="json", exclude_none=True)
)

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

await self._write_stream.send(JSONRPCMessage(jsonrpc_notification))
Expand All @@ -165,7 +165,7 @@ async def _send_response(
jsonrpc_response = JSONRPCResponse(
jsonrpc="2.0",
id=request_id,
result=response.model_dump(by_alias=True, mode="json"),
result=response.model_dump(by_alias=True, mode="json", exclude_none=True),
)
await self._write_stream.send(JSONRPCMessage(jsonrpc_response))

Expand All @@ -180,7 +180,7 @@ async def _receive_loop(self) -> None:
await self._incoming_message_stream_writer.send(message)
elif isinstance(message.root, JSONRPCRequest):
validated_request = self._receive_request_type.model_validate(
message.root.model_dump(by_alias=True, mode="json")
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
)
responder = RequestResponder(
request_id=message.root.id,
Expand All @@ -196,7 +196,7 @@ async def _receive_loop(self) -> None:
await self._incoming_message_stream_writer.send(responder)
elif isinstance(message.root, JSONRPCNotification):
notification = self._receive_notification_type.model_validate(
message.root.model_dump(by_alias=True, mode="json")
message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
)

await self._received_notification(notification)
Expand Down
6 changes: 3 additions & 3 deletions tests/client/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def mock_server():
jsonrpc_request = await client_to_server_receive.receive()
assert isinstance(jsonrpc_request.root, JSONRPCRequest)
request = ClientRequest.model_validate(
jsonrpc_request.model_dump(by_alias=True, mode="json")
jsonrpc_request.model_dump(by_alias=True, mode="json", exclude_none=True)
)
assert isinstance(request.root, InitializeRequest)

Expand All @@ -59,14 +59,14 @@ async def mock_server():
JSONRPCResponse(
jsonrpc="2.0",
id=jsonrpc_request.root.id,
result=result.model_dump(by_alias=True, mode="json"),
result=result.model_dump(by_alias=True, mode="json", exclude_none=True),
)
)
)
jsonrpc_notification = await client_to_server_receive.receive()
assert isinstance(jsonrpc_notification.root, JSONRPCNotification)
initialized_notification = ClientNotification.model_validate(
jsonrpc_notification.model_dump(by_alias=True, mode="json")
jsonrpc_notification.model_dump(by_alias=True, mode="json", exclude_none=True)
)

async def listen_session():
Expand Down
2 changes: 1 addition & 1 deletion tests/server/test_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def test_stdio_server():
]

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

async with stdio_server(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_jsonrpc_request():

request = JSONRPCMessage.model_validate(json_data)
assert isinstance(request.root, JSONRPCRequest)
ClientRequest.model_validate(request.model_dump(by_alias=True))
ClientRequest.model_validate(request.model_dump(by_alias=True, exclude_none=True))

assert request.root.jsonrpc == "2.0"
assert request.root.id == 1
Expand Down