Skip to content

Commit ef6f2b0

Browse files
committed
Replace dict with model_dump for Pydantic objects
Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent edc630d commit ef6f2b0

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

mcpgateway/admin.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def admin_list_servers(
101101
"""
102102
logger.debug(f"User {user} requested server list")
103103
servers = await server_service.list_servers(db, include_inactive=include_inactive)
104-
return [server.dict(by_alias=True) for server in servers]
104+
return [server.model_dump(by_alias=True) for server in servers]
105105

106106

107107
@admin_router.get("/servers/{server_id}", response_model=ServerRead)
@@ -316,7 +316,7 @@ async def admin_list_resources(
316316
"""
317317
logger.debug(f"User {user} requested resource list")
318318
resources = await resource_service.list_resources(db, include_inactive=include_inactive)
319-
return [resource.dict(by_alias=True) for resource in resources]
319+
return [resource.model_dump(by_alias=True) for resource in resources]
320320

321321

322322
@admin_router.get("/prompts", response_model=List[PromptRead])
@@ -342,7 +342,7 @@ async def admin_list_prompts(
342342
"""
343343
logger.debug(f"User {user} requested prompt list")
344344
prompts = await prompt_service.list_prompts(db, include_inactive=include_inactive)
345-
return [prompt.dict(by_alias=True) for prompt in prompts]
345+
return [prompt.model_dump(by_alias=True) for prompt in prompts]
346346

347347

348348
@admin_router.get("/gateways", response_model=List[GatewayRead])
@@ -368,7 +368,7 @@ async def admin_list_gateways(
368368
"""
369369
logger.debug(f"User {user} requested gateway list")
370370
gateways = await gateway_service.list_gateways(db, include_inactive=include_inactive)
371-
return [gateway.dict(by_alias=True) for gateway in gateways]
371+
return [gateway.model_dump(by_alias=True) for gateway in gateways]
372372

373373

374374
@admin_router.post("/gateways/{gateway_id}/toggle")
@@ -486,7 +486,7 @@ async def admin_list_tools(
486486
"""
487487
logger.debug(f"User {user} requested tool list")
488488
tools = await tool_service.list_tools(db, include_inactive=include_inactive)
489-
return [tool.dict(by_alias=True) for tool in tools]
489+
return [tool.model_dump(by_alias=True) for tool in tools]
490490

491491

492492
@admin_router.get("/tools/{tool_id}", response_model=ToolRead)
@@ -570,7 +570,7 @@ async def admin_add_tool(
570570
logger.debug(f"Tool data built: {tool_data}")
571571
try:
572572
tool = ToolCreate(**tool_data)
573-
logger.debug(f"Validated tool data: {tool.dict()}")
573+
logger.debug(f"Validated tool data: {tool.model_dump(by_alias=True)}")
574574
await tool_service.register_tool(db, tool)
575575
return JSONResponse(
576576
content={"message": "Tool registered successfully!", "success": True},
@@ -733,7 +733,7 @@ async def admin_get_gateway(gateway_id: str, db: Session = Depends(get_db), user
733733
"""
734734
logger.debug(f"User {user} requested details for gateway ID {gateway_id}")
735735
gateway = await gateway_service.get_gateway(db, gateway_id)
736-
return gateway.dict(by_alias=True)
736+
return gateway.model_dump(by_alias=True)
737737

738738

739739
@admin_router.post("/gateways")
@@ -868,7 +868,7 @@ async def admin_get_resource(uri: str, db: Session = Depends(get_db), user: str
868868
logger.debug(f"User {user} requested details for resource URI {uri}")
869869
resource = await resource_service.get_resource_by_uri(db, uri)
870870
content = await resource_service.read_resource(db, uri)
871-
return {"resource": resource.dict(by_alias=True), "content": content}
871+
return {"resource": resource.model_dump(by_alias=True), "content": content}
872872

873873

874874
@admin_router.post("/resources")
@@ -1023,7 +1023,7 @@ async def admin_get_prompt(name: str, db: Session = Depends(get_db), user: str =
10231023
prompt_details = await prompt_service.get_prompt_details(db, name)
10241024

10251025
prompt = PromptRead.model_validate(prompt_details)
1026-
return prompt.dict(by_alias=True)
1026+
return prompt.model_dump(by_alias=True)
10271027

10281028

10291029
@admin_router.post("/prompts")

mcpgateway/federation/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ async def register_gateway(self, db: Session, url: str, name: Optional[str] = No
157157
gateway = DbGateway(
158158
name=gateway_name,
159159
url=url,
160-
capabilities=capabilities.dict(),
160+
capabilities=capabilities,
161161
last_seen=datetime.now(timezone.utc),
162162
)
163163
db.add(gateway)
@@ -350,7 +350,7 @@ async def _run_sync_loop(self, db: Session) -> None:
350350
try:
351351
# Update capabilities
352352
capabilities = await self._initialize_gateway(gateway.url)
353-
gateway.capabilities = capabilities.dict()
353+
gateway.capabilities = capabilities
354354
gateway.last_seen = datetime.now(timezone.utc)
355355
gateway.is_active = True
356356

mcpgateway/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ async def server_get_tools(
812812
"""
813813
logger.debug(f"User: {user} has listed tools for the server_id: {server_id}")
814814
tools = await tool_service.list_server_tools(db, server_id=server_id, include_inactive=include_inactive)
815-
return [tool.dict(by_alias=True) for tool in tools]
815+
return [tool.model_dump(by_alias=True) for tool in tools]
816816

817817

818818
@server_router.get("/{server_id}/resources", response_model=List[ResourceRead])
@@ -840,7 +840,7 @@ async def server_get_resources(
840840
"""
841841
logger.debug(f"User: {user} has listed resources for the server_id: {server_id}")
842842
resources = await resource_service.list_server_resources(db, server_id=server_id, include_inactive=include_inactive)
843-
return [resource.dict(by_alias=True) for resource in resources]
843+
return [resource.model_dump(by_alias=True) for resource in resources]
844844

845845

846846
@server_router.get("/{server_id}/prompts", response_model=List[PromptRead])
@@ -868,7 +868,7 @@ async def server_get_prompts(
868868
"""
869869
logger.debug(f"User: {user} has listed prompts for the server_id: {server_id}")
870870
prompts = await prompt_service.list_server_prompts(db, server_id=server_id, include_inactive=include_inactive)
871-
return [prompt.dict(by_alias=True) for prompt in prompts]
871+
return [prompt.model_dump(by_alias=True) for prompt in prompts]
872872

873873

874874
#############

tests/unit/mcpgateway/test_admin.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ async def test_admin_list_servers(self, mock_list_servers, mock_db):
136136
"""Test listing servers through admin UI."""
137137
# Setup
138138
mock_server1 = MagicMock()
139-
mock_server1.dict.return_value = {"id": 1, "name": "Server 1"}
139+
mock_server1.model_dump.return_value = {"id": 1, "name": "Server 1"}
140140
mock_server2 = MagicMock()
141-
mock_server2.dict.return_value = {"id": 2, "name": "Server 2"}
141+
mock_server2.model_dump.return_value = {"id": 2, "name": "Server 2"}
142142
mock_list_servers.return_value = [mock_server1, mock_server2]
143143

144144
# Execute
@@ -235,9 +235,9 @@ async def test_admin_list_tools(self, mock_list_tools, mock_db):
235235
"""Test listing tools through admin UI."""
236236
# Setup
237237
mock_tool1 = MagicMock()
238-
mock_tool1.dict.return_value = {"id": 1, "name": "Tool 1"}
238+
mock_tool1.model_dump.return_value = {"id": 1, "name": "Tool 1"}
239239
mock_tool2 = MagicMock()
240-
mock_tool2.dict.return_value = {"id": 2, "name": "Tool 2"}
240+
mock_tool2.model_dump.return_value = {"id": 2, "name": "Tool 2"}
241241
mock_list_tools.return_value = [mock_tool1, mock_tool2]
242242

243243
# Execute
@@ -347,9 +347,9 @@ async def test_admin_list_resources(self, mock_list_resources, mock_db):
347347
"""Test listing resources through admin UI."""
348348
# Setup
349349
mock_resource1 = MagicMock()
350-
mock_resource1.dict.return_value = {"id": 1, "name": "Resource 1"}
350+
mock_resource1.model_dump.return_value = {"id": 1, "name": "Resource 1"}
351351
mock_resource2 = MagicMock()
352-
mock_resource2.dict.return_value = {"id": 2, "name": "Resource 2"}
352+
mock_resource2.model_dump.return_value = {"id": 2, "name": "Resource 2"}
353353
mock_list_resources.return_value = [mock_resource1, mock_resource2]
354354

355355
# Execute
@@ -367,7 +367,7 @@ async def test_admin_get_resource(self, mock_read_resource, mock_get_resource, m
367367
"""Test getting a single resource through admin UI."""
368368
# Setup
369369
mock_resource = MagicMock()
370-
mock_resource.dict.return_value = {"id": 1, "name": "Resource 1"}
370+
mock_resource.model_dump.return_value = {"id": 1, "name": "Resource 1"}
371371
mock_get_resource.return_value = mock_resource
372372
mock_read_resource.return_value = {"type": "resource", "text": "content"}
373373

@@ -435,9 +435,9 @@ async def test_admin_list_prompts(self, mock_list_prompts, mock_db):
435435
"""Test listing prompts through admin UI."""
436436
# Setup
437437
mock_prompt1 = MagicMock()
438-
mock_prompt1.dict.return_value = {"id": 1, "name": "Prompt 1"}
438+
mock_prompt1.model_dump.return_value = {"id": 1, "name": "Prompt 1"}
439439
mock_prompt2 = MagicMock()
440-
mock_prompt2.dict.return_value = {"id": 2, "name": "Prompt 2"}
440+
mock_prompt2.model_dump.return_value = {"id": 2, "name": "Prompt 2"}
441441
mock_list_prompts.return_value = [mock_prompt1, mock_prompt2]
442442

443443
# Execute
@@ -537,9 +537,9 @@ async def test_admin_list_gateways(self, mock_list_gateways, mock_db):
537537
"""Test listing gateways through admin UI."""
538538
# Setup
539539
mock_gateway1 = MagicMock()
540-
mock_gateway1.dict.return_value = {"id": 1, "name": "Gateway 1"}
540+
mock_gateway1.model_dump.return_value = {"id": 1, "name": "Gateway 1"}
541541
mock_gateway2 = MagicMock()
542-
mock_gateway2.dict.return_value = {"id": 2, "name": "Gateway 2"}
542+
mock_gateway2.model_dump.return_value = {"id": 2, "name": "Gateway 2"}
543543
mock_list_gateways.return_value = [mock_gateway1, mock_gateway2]
544544

545545
# Execute
@@ -556,7 +556,7 @@ async def test_admin_get_gateway(self, mock_get_gateway, mock_db):
556556
"""Test getting a single gateway through admin UI."""
557557
# Setup
558558
mock_gateway = MagicMock()
559-
mock_gateway.dict.return_value = {"id": 1, "name": "Gateway 1"}
559+
mock_gateway.model_dump.return_value = {"id": 1, "name": "Gateway 1"}
560560
mock_get_gateway.return_value = mock_gateway
561561

562562
# Execute

tests/unit/mcpgateway/test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def test_delete_server_endpoint(self, mock_delete, test_client, auth_headers):
387387
def test_server_get_tools(self, mock_list_tools, test_client, auth_headers):
388388
"""Test listing tools associated with a server."""
389389
mock_tool = MagicMock()
390-
mock_tool.dict.return_value = MOCK_TOOL_READ
390+
mock_tool.model_dump.return_value = MOCK_TOOL_READ
391391
mock_list_tools.return_value = [mock_tool]
392392

393393
response = test_client.get("/servers/1/tools", headers=auth_headers)
@@ -400,7 +400,7 @@ def test_server_get_tools(self, mock_list_tools, test_client, auth_headers):
400400
def test_server_get_resources(self, mock_list_resources, test_client, auth_headers):
401401
"""Test listing resources associated with a server."""
402402
mock_resource = MagicMock()
403-
mock_resource.dict.return_value = MOCK_RESOURCE_READ
403+
mock_resource.model_dump.return_value = MOCK_RESOURCE_READ
404404
mock_list_resources.return_value = [mock_resource]
405405

406406
response = test_client.get("/servers/1/resources", headers=auth_headers)

0 commit comments

Comments
 (0)