Add GenAI semantic-convention attributes to OpenTelemetryMiddleware

Merged
KL
5mo ago
+137-15

Adds the GenAI semantic-convention attributes from the MCP OTel spans spec (mcp.common.attributes) to OpenTelemetryMiddleware:

  • gen_ai.operation.name = execute_tool for tools/call (per the spec, set only for tool calls).
  • gen_ai.tool.name for tools/call, gen_ai.prompt.name for prompts/get.
  • error.type and rpc.response.status_code set to the JSON-RPC error code on failures; error.type = tool_error for a tools/call result carrying is_error (spec: CallToolResult with isError true).
  • mcp.protocol.version.

The span name now follows the spec's {mcp.method.name} {target} format (dropping the prior MCP handle prefix).

The tools/call result is matched against both the CallToolResult model and the dict shapes (isError/is_error) handlers may return, since result coercion happens after the middleware runs.

The sensitive opt_in attributes (gen_ai.tool.call.arguments/result, gen_ai.prompt.variable.*) are intentionally left out; they can be added later behind an opt-in flag.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.