Skip to content

Commit 3b9f6ac

Browse files
committed
enh: logging
1 parent 1050639 commit 3b9f6ac

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

src/mcpo/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async def run(
222222
try:
223223
headers = json.loads(headers)
224224
except json.JSONDecodeError:
225-
print("Warning: Invalid JSON format for headers. Headers will be ignored.")
225+
logger.warning("Invalid JSON format for headers. Headers will be ignored.")
226226
headers = None
227227

228228
if server_type == "sse":

src/mcpo/utils/main.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import traceback
33
from typing import Any, Dict, ForwardRef, List, Optional, Type, Union
4-
4+
import logging
55
from fastapi import HTTPException
66

77
from mcp import ClientSession, types
@@ -27,6 +27,8 @@
2727
INTERNAL_ERROR: 500,
2828
}
2929

30+
logger = logging.getLogger(__name__)
31+
3032

3133
def 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

5254
def 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

5759
def 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

Comments
 (0)