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
This commit is contained in:
MartinPSDev 2025-05-09 18:54:51 -03:00
parent df48224ab6
commit e5705119bf
2 changed files with 11 additions and 9 deletions

View File

@ -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

View File

@ -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",