From e5705119bf84d3c765b37a63b88e0d1056658792 Mon Sep 17 00:00:00 2001 From: MartinPSDev Date: Fri, 9 May 2025 18:54:51 -0300 Subject: [PATCH] refactor: Add get_optional_attr helper function - Add get_optional_attr helper function to standardize optional attribute access - Update usages of getattr to use the new helper function - Update CHANGELOG.md to reflect changes --- CHANGELOG.md | 10 ++++------ src/mcpo/main.py | 10 +++++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2df7f3..bb65895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,14 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -# Changelog +## [Unreleased] -All notable changes to this project will be documented in this file. +### Changed +- 🎯 Added `get_optional_attr` helper function and standardized its usage throughout the codebase to improve consistency in optional attribute access -The format is based on Keep a Changelog, -and this project adheres to Semantic Versioning. - -## [0.0.13] - 2025-05-01 +## [0.0.13]- 2025-05-01 ### Added diff --git a/src/mcpo/main.py b/src/mcpo/main.py index 7339c71..4d86d53 100644 --- a/src/mcpo/main.py +++ b/src/mcpo/main.py @@ -16,13 +16,18 @@ from mcpo.utils.main import get_model_fields, get_tool_handler from mcpo.utils.auth import get_verify_api_key, APIKeyMiddleware +def get_optional_attr(obj: any, attr: str) -> Optional[any]: + """Helper function to get an optional attribute from an object consistently""" + return getattr(obj, attr, None) + + async def create_dynamic_endpoints(app: FastAPI, api_dependency=None): session: ClientSession = app.state.session if not session: raise ValueError("Session is not initialized in the app state.") result = await session.initialize() - server_info = getattr(result, "serverInfo", None) + server_info = get_optional_attr(result, "serverInfo") if server_info: app.title = server_info.name or app.title app.description = ( @@ -36,9 +41,8 @@ async def create_dynamic_endpoints(app: FastAPI, api_dependency=None): for tool in tools: endpoint_name = tool.name endpoint_description = tool.description - inputSchema = tool.inputSchema - outputSchema = getattr(tool, "outputSchema", None) + outputSchema = get_optional_attr(tool, "outputSchema") form_model_fields = get_model_fields( f"{endpoint_name}_form_model",