Skip to content

Commit cadf02e

Browse files
authored
execution count optional lazy loading (#1548)
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
1 parent 4d15916 commit cadf02e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

mcpgateway/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ class ToolRead(BaseModelWithConfigDict):
12771277
enabled: bool
12781278
reachable: bool
12791279
gateway_id: Optional[str]
1280-
execution_count: int
1280+
execution_count: Optional[int] = Field(None)
12811281
metrics: Optional[ToolMetrics] = Field(None)
12821282
name: str
12831283
displayName: Optional[str] = Field(None, description="Display name for the tool (shown in UI)") # noqa: N815

mcpgateway/services/tool_service.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from mcp.client.streamable_http import streamablehttp_client
3636
from sqlalchemy import and_, case, delete, desc, Float, func, not_, or_, select
3737
from sqlalchemy.exc import IntegrityError
38-
from sqlalchemy.orm import joinedload, Session
38+
from sqlalchemy.orm import joinedload, selectinload, Session
3939

4040
# First-Party
4141
from mcpgateway.common.models import Gateway as PydanticGateway
@@ -360,9 +360,10 @@ def _convert_tool_to_read(self, tool: DbTool, include_metrics: bool = True) -> T
360360
tool_dict = tool.__dict__.copy()
361361
tool_dict.pop("_sa_instance_state", None)
362362

363-
tool_dict["execution_count"] = tool.execution_count
364363
tool_dict["metrics"] = tool.metrics_summary if include_metrics else None
365364

365+
tool_dict["execution_count"] = tool.execution_count if include_metrics else None
366+
366367
tool_dict["request_type"] = tool.request_type
367368
tool_dict["annotations"] = tool.annotations or {}
368369

@@ -405,6 +406,7 @@ def _convert_tool_to_read(self, tool: DbTool, include_metrics: bool = True) -> T
405406
tool_dict["custom_name_slug"] = getattr(tool, "custom_name_slug", "") or ""
406407
tool_dict["tags"] = getattr(tool, "tags", []) or []
407408
tool_dict["team"] = getattr(tool, "team", None)
409+
408410
return ToolRead.model_validate(tool_dict)
409411

410412
async def _record_tool_metric(self, db: Session, tool: DbTool, start_time: float, success: bool, error_message: Optional[str]) -> None:
@@ -848,7 +850,19 @@ async def list_server_tools(
848850
True
849851
"""
850852

851-
query = select(DbTool).options(joinedload(DbTool.gateway)).join(server_tool_association, DbTool.id == server_tool_association.c.tool_id).where(server_tool_association.c.server_id == server_id)
853+
if include_metrics:
854+
query = (
855+
select(DbTool)
856+
.options(joinedload(DbTool.gateway))
857+
.options(selectinload(DbTool.metrics))
858+
.join(server_tool_association, DbTool.id == server_tool_association.c.tool_id)
859+
.where(server_tool_association.c.server_id == server_id)
860+
)
861+
else:
862+
query = (
863+
select(DbTool).options(joinedload(DbTool.gateway)).join(server_tool_association, DbTool.id == server_tool_association.c.tool_id).where(server_tool_association.c.server_id == server_id)
864+
)
865+
852866
cursor = None # Placeholder for pagination; ignore for now
853867
logger.debug(f"Listing server tools for server_id={server_id} with include_inactive={include_inactive}, cursor={cursor}")
854868

0 commit comments

Comments
 (0)