11import json
22import traceback
33from typing import Any , Dict , ForwardRef , List , Optional , Type , Union
4-
4+ import logging
55from fastapi import HTTPException
66
77from mcp import ClientSession , types
2727 INTERNAL_ERROR : 500 ,
2828}
2929
30+ logger = logging .getLogger (__name__ )
31+
3032
3133def process_tool_response (result : CallToolResult ) -> list :
3234 """Universal response processor for all tool endpoints"""
@@ -51,7 +53,7 @@ def process_tool_response(result: CallToolResult) -> list:
5153
5254def name_needs_alias (name : str ) -> bool :
5355 """Check if a field name needs aliasing (for now if it starts with '__')."""
54- return name .startswith ('__' )
56+ return name .startswith ("__" )
5557
5658
5759def generate_alias_name (original_name : str , existing_names : set ) -> str :
@@ -65,7 +67,7 @@ def generate_alias_name(original_name: str, existing_names: set) -> str:
6567 Returns:
6668 An alias name that doesn't conflict with existing names
6769 """
68- alias_name = original_name .lstrip ('_' )
70+ alias_name = original_name .lstrip ("_" )
6971 # Handle potential naming conflicts
7072 original_alias_name = alias_name
7173 suffix_counter = 1
@@ -171,12 +173,14 @@ def _process_schema_property(
171173 )
172174
173175 if name_needs_alias (name ):
174- other_names = set ().union (nested_properties , nested_fields , _model_cache )
176+ other_names = set ().union (
177+ nested_properties , nested_fields , _model_cache
178+ )
175179 alias_name = generate_alias_name (name , other_names )
176180 aliased_field = Field (
177181 default = nested_pydantic_field .default ,
178182 description = nested_pydantic_field .description ,
179- alias = name
183+ alias = name ,
180184 )
181185 nested_fields [alias_name ] = (nested_type_hint , aliased_field )
182186 else :
@@ -245,7 +249,7 @@ def get_model_fields(form_model_name, properties, required_fields, schema_defs=N
245249 aliased_field = Field (
246250 default = pydantic_field_info .default ,
247251 description = pydantic_field_info .description ,
248- alias = param_name
252+ alias = param_name ,
249253 )
250254 # Use the generated type hint and Field info
251255 model_fields [alias_name ] = (python_type_hint , aliased_field )
@@ -274,7 +278,7 @@ def make_endpoint_func(
274278 ): # Parameterized endpoint
275279 async def tool (form_data : FormModel ) -> ResponseModel :
276280 args = form_data .model_dump (exclude_none = True , by_alias = True )
277- print (f"Calling endpoint: { endpoint_name } , with args: { args } " )
281+ logger . info (f"Calling endpoint: { endpoint_name } , with args: { args } " )
278282 try :
279283 result = await session .call_tool (endpoint_name , arguments = args )
280284
@@ -299,7 +303,7 @@ async def tool(form_data: FormModel) -> ResponseModel:
299303 return final_response
300304
301305 except McpError as e :
302- print (
306+ logger . info (
303307 f"MCP Error calling { endpoint_name } : { traceback .format_exc ()} "
304308 )
305309 status_code = MCP_ERROR_TO_HTTP_STATUS .get (e .error .code , 500 )
@@ -312,7 +316,7 @@ async def tool(form_data: FormModel) -> ResponseModel:
312316 ),
313317 )
314318 except Exception as e :
315- print (
319+ logger . info (
316320 f"Unexpected error calling { endpoint_name } : { traceback .format_exc ()} "
317321 )
318322 raise HTTPException (
@@ -329,7 +333,7 @@ def make_endpoint_func_no_args(
329333 endpoint_name : str , session : ClientSession
330334 ): # Parameterless endpoint
331335 async def tool (): # No parameters
332- print (f"Calling endpoint: { endpoint_name } , with no args" )
336+ logger . info (f"Calling endpoint: { endpoint_name } , with no args" )
333337 try :
334338 result = await session .call_tool (
335339 endpoint_name , arguments = {}
@@ -353,7 +357,7 @@ async def tool(): # No parameters
353357 return final_response
354358
355359 except McpError as e :
356- print (
360+ logger . info (
357361 f"MCP Error calling { endpoint_name } : { traceback .format_exc ()} "
358362 )
359363 status_code = MCP_ERROR_TO_HTTP_STATUS .get (e .error .code , 500 )
@@ -367,7 +371,7 @@ async def tool(): # No parameters
367371 ),
368372 )
369373 except Exception as e :
370- print (
374+ logger . info (
371375 f"Unexpected error calling { endpoint_name } : { traceback .format_exc ()} "
372376 )
373377 raise HTTPException (
0 commit comments