Skip to content

Commit ac2a659

Browse files
committed
💡🥅 Improve errors and comments
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
1 parent e3f616d commit ac2a659

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

mcp/weather_tool/weather_tool.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def setup_tracing() -> None:
8484
)
8585
)
8686

87-
logger.info(f"Tracing initialized: service={service_name} otlp={otlp_endpoint}")
87+
logger.info("Tracing initialized: service=%s otlp=%s", service_name, otlp_endpoint)
8888

8989

9090
@mcp.tool(annotations={"readOnlyHint": True, "destructiveHint": False, "idempotentHint": True})
@@ -111,8 +111,8 @@ async def get_weather(city: str) -> str:
111111

112112
if not data or "results" not in data:
113113
result = f"City {city} not found"
114-
span.set_attribute("error.type", "tool_error")
115-
span.set_status(Status(StatusCode.ERROR, result))
114+
span.set_attribute("gen_ai.tool.call.result", result)
115+
span.set_status(Status(StatusCode.OK))
116116
return result
117117

118118
latitude = data["results"][0]["latitude"]
@@ -137,6 +137,12 @@ async def get_weather(city: str) -> str:
137137
span.set_status(Status(StatusCode.OK))
138138
return result
139139

140+
except requests.RequestException as e:
141+
logger.warning("Weather API error for '%s': %s", city, e)
142+
span.set_attribute("error.type", type(e).__name__)
143+
span.set_status(Status(StatusCode.ERROR, str(e)))
144+
span.record_exception(e)
145+
return f"Weather service temporarily unavailable for {city}"
140146
except Exception as e:
141147
span.set_attribute("error.type", type(e).__name__)
142148
span.set_status(Status(StatusCode.ERROR, str(e)))
@@ -160,8 +166,7 @@ async def _trace_propagation_middleware(request, call_next):
160166
otel_context.detach(token)
161167

162168

163-
# host can be specified with HOST env variable
164-
# transport can be specified with MCP_TRANSPORT env variable (defaults to streamable-http)
169+
# Environment variables: host can be specified with HOST, port with PORT
165170
def run_server():
166171
"Run the MCP server"
167172
setup_tracing()

0 commit comments

Comments
 (0)