Skip to content

feat: ✨ Add tracing to weather tool#255

Merged
evaline-ju merged 11 commits intokagenti:mainfrom
evaline-ju:weather-trace-tool
Apr 22, 2026
Merged

feat: ✨ Add tracing to weather tool#255
evaline-ju merged 11 commits intokagenti:mainfrom
evaline-ju:weather-trace-tool

Conversation

@evaline-ju
Copy link
Copy Markdown
Contributor

@evaline-ju evaline-ju commented Apr 21, 2026

Summary

  • Added OpenTelemetry distributed tracing: W3C traceparent propagation via ASGI middleware connects FastMCP's span to the caller's trace
    • The middleware pattern follows the same as the weather-service agent, mainly to attach the tool attributes on the existing FastMCP span to not end up duplicate tool call spans
  • get_weather made async with run_in_executor to keep the resilient requests session (retry/backoff) without blocking the event loop.
  • run_server switched from mcp.run() to mcp.http_app() + uvicorn to enable middleware injection
  • MCP_TRANSPORT is removed since FastMCP SSE transport should be deprecated as of 2.3.2 in favor of streamable-http

Related issue(s)

Updates an example for kagenti/kagenti#619

For example: see kagenti/kagenti#1301

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
@evaline-ju evaline-ju changed the title ✨ Add tracing to weather tool feat: ✨ Add tracing to weather tool Apr 21, 2026
@evaline-ju evaline-ju marked this pull request as ready for review April 21, 2026 19:56
Copy link
Copy Markdown
Contributor

@esnible esnible left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tool had some comments. Consider them; if you feel the tool is wrong ignore them.

 ┌─────┬─────────────────┬──────────────────────┬────────────┬────────────────────────────────────────────────────────────────────────────────┐   
  │  #  │      File       │         Line         │  Severity  │                                    Comment                                     │
  ├─────┼─────────────────┼──────────────────────┼────────────┼────────────────────────────────────────────────────────────────────────────────┤   
  │ 1   │ weather_tool.py │ ~168 (run_server     │ suggestion │ Stale comment: # transport can be specified with MCP_TRANSPORT env variable —  │ 
  │     │                 │ comment)             │            │ MCP_TRANSPORT is no longer used                                                │
  ├─────┼─────────────────┼─────────────────────────┼────────────┼─────────────────────────────────────────────────────────────────────────────┤   
  │     │                 │                         │            │ except Exception re-raises but loses the user-friendly error messages the   │
  │ 2   │ weather_tool.py │ ~126 (except Exception) │ suggestion │ original had. Callers now get a raw traceback instead of "Weather service   │   
  │     │                 │                         │            │ temporarily unavailable"                                                    │ 
  ├─────┼─────────────────┼─────────────────────────┼────────────┼─────────────────────────────────────────────────────────────────────────────┤   
  │     │                 │                         │            │ logger.info(f"Tracing initialized: ...") — f-string in logging; prefer      │
  │ 3   │ weather_tool.py │ ~95 (setup_tracing)     │ nit        │ logger.info("Tracing initialized: service=%s otlp=%s", service_name,        │   
  │     │                 │                         │            │ otlp_endpoint) for lazy evaluation                                          │
  ├─────┼─────────────────┼─────────────────────────┼────────────┼─────────────────────────────────────────────────────────────────────────────┤   
  │     │                 │ ~108 ("city not found"  │            │ "City not found" is set as StatusCode.ERROR on the span — arguably this is  │
  │ 4   │ weather_tool.py │ branch)                 │ nit        │ a valid tool result (no city), not an error. Consider StatusCode.OK with a  │   
  │     │                 │                         │            │ distinguishing attribute                                                    │
  ├─────┼─────────────────┼─────────────────────────┼────────────┼─────────────────────────────────────────────────────────────────────────────┤   
  │ 5   │ pyproject.toml  │ ~8                      │ praise     │ Good: httpx dependency removed since it's no longer directly needed — keeps │
  │     │                 │                         │            │  the dep list clean                                                         │   
  ├─────┼─────────────────┼─────────────────────────┼────────────┼─────────────────────────────────────────────────────────────────────────────┤
  │     │                 │ ~162                    │            │ BaseHTTPMiddleware has known performance caveats in Starlette (wraps entire │   
  │ 6   │ weather_tool.py │ (BaseHTTPMiddleware)    │ nit        │  response body in memory). Fine for this example, but worth knowing if this │   
  │     │                 │                         │            │  pattern gets copied to production agents                                   │
  └─────┴─────────────────┴─────────────────────────┴────────────┴─────────────────────────────────────────────────────────────────────────────┘   

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
@evaline-ju evaline-ju merged commit 8655e29 into kagenti:main Apr 22, 2026
10 checks passed
@evaline-ju evaline-ju deleted the weather-trace-tool branch April 22, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants