π The fast, Pythonic way to build MCP servers and clients.
Set gen_ai.operation.name on tools/call spans (#6429)
nate nowack623242e68868
diff --git a/docs/servers/telemetry.mdx b/docs/servers/telemetry.mdxindex e9088e184..102da5307 100644--- a/docs/servers/telemetry.mdx+++ b/docs/servers/telemetry.mdx@@ -453,6 +453,7 @@ FastMCP implements the [OpenTelemetry MCP semantic conventions](https://opentele| `jsonrpc.request.id` | JSON-RPC request ID (on server spans for inbound requests) || `mcp.resource.uri` | The resource URI (for resource operations) || `gen_ai.tool.name` | Tool name (on `tools/call` spans) |+| `gen_ai.operation.name` | `execute_tool` (on `tools/call` spans) || `gen_ai.prompt.name` | Prompt name (on `prompts/get` spans) || `error.type` | Error classification (the JSON-RPC error code for protocol errors, `tool_error` for ToolError, otherwise exception class name) || `rpc.response.status_code` | JSON-RPC error code (on spans that failed with a protocol error) |diff --git a/fastmcp_slim/fastmcp/client/telemetry.py b/fastmcp_slim/fastmcp/client/telemetry.pyindex 56c879a03..dd2cf6aa0 100644--- a/fastmcp_slim/fastmcp/client/telemetry.py+++ b/fastmcp_slim/fastmcp/client/telemetry.py@@ -38,6 +38,7 @@ def client_span(attrs["mcp.resource.uri"] = resource_uriif tool_name is not None:attrs["gen_ai.tool.name"] = tool_name+ attrs["gen_ai.operation.name"] = "execute_tool"if prompt_name is not None:attrs["gen_ai.prompt.name"] = prompt_namediff --git a/fastmcp_slim/fastmcp/server/telemetry.py b/fastmcp_slim/fastmcp/server/telemetry.pyindex 2db962f09..dce24c114 100644--- a/fastmcp_slim/fastmcp/server/telemetry.py+++ b/fastmcp_slim/fastmcp/server/telemetry.py@@ -168,6 +168,7 @@ def _build_server_span_attrs(attrs["mcp.resource.uri"] = resource_uriif tool_name is not None:attrs["gen_ai.tool.name"] = tool_name+ attrs["gen_ai.operation.name"] = "execute_tool"if prompt_name is not None:attrs["gen_ai.prompt.name"] = prompt_namereturn attrsdiff --git a/tests/client/telemetry/test_client_tracing.py b/tests/client/telemetry/test_client_tracing.pyindex 2e266d915..4c579e1a8 100644--- a/tests/client/telemetry/test_client_tracing.py+++ b/tests/client/telemetry/test_client_tracing.py@@ -66,6 +66,7 @@ class TestClientToolTracing:assert client_span.attributes["mcp.method.name"] == "tools/call"# gen_ai semantic conventionsassert client_span.attributes["gen_ai.tool.name"] == "add"+ assert client_span.attributes["gen_ai.operation.name"] == "execute_tool"# RPC attributes must NOT be presentassert "rpc.system" not in client_span.attributesassert "rpc.method" not in client_span.attributes@@ -227,6 +228,7 @@ class TestClientPromptTracing:assert client_span.attributes["mcp.method.name"] == "prompts/get"# gen_ai semantic conventionsassert client_span.attributes["gen_ai.prompt.name"] == "welcome"+ assert "gen_ai.operation.name" not in client_span.attributes# RPC attributes must NOT be presentassert "rpc.system" not in client_span.attributesassert "rpc.method" not in client_span.attributesdiff --git a/tests/server/telemetry/test_server_tracing.py b/tests/server/telemetry/test_server_tracing.pyindex 0fceafa41..f3817bfd7 100644--- a/tests/server/telemetry/test_server_tracing.py+++ b/tests/server/telemetry/test_server_tracing.py@@ -38,6 +38,7 @@ class TestToolTracing:assert span.attributes["mcp.method.name"] == "tools/call"# gen_ai semantic conventionsassert span.attributes["gen_ai.tool.name"] == "greet"+ assert span.attributes["gen_ai.operation.name"] == "execute_tool"# RPC attributes must NOT be presentassert "rpc.system" not in span.attributesassert "rpc.service" not in span.attributes@@ -194,6 +195,7 @@ class TestPromptTracing:assert span.attributes["mcp.method.name"] == "prompts/get"# gen_ai semantic conventionsassert span.attributes["gen_ai.prompt.name"] == "greeting"+ assert "gen_ai.operation.name" not in span.attributes# RPC attributes must NOT be presentassert "rpc.system" not in span.attributesassert "rpc.service" not in span.attributesdiff --git a/tests/telemetry/test_span_attributes.py b/tests/telemetry/test_span_attributes.pyindex 554a288cb..76ce94d1c 100644--- a/tests/telemetry/test_span_attributes.py+++ b/tests/telemetry/test_span_attributes.py@@ -176,6 +176,7 @@ def test_known_span_attributes_are_available_on_start("mcp.method.name": "tools/call","fastmcp.component.key": "tool:echo@","gen_ai.tool.name": "echo",+ "gen_ai.operation.name": "execute_tool",}assert recorder.attributes["server test"] == {"mcp.method.name": "tools/call",@@ -183,6 +184,7 @@ def test_known_span_attributes_are_available_on_start("fastmcp.component.type": "tool","fastmcp.component.key": "tool:echo@","gen_ai.tool.name": "echo",+ "gen_ai.operation.name": "execute_tool",}assert recorder.attributes["initialize test"] == {"fastmcp.span.seam": True,@@ -209,6 +211,7 @@ SPAN_HELPER_CASES = ["mcp.method.name": "tools/call","fastmcp.component.key": "tool:echo@","gen_ai.tool.name": "echo",+ "gen_ai.operation.name": "execute_tool",},id="client_span",),@@ -228,6 +231,7 @@ SPAN_HELPER_CASES = ["fastmcp.component.type": "tool","fastmcp.component.key": "tool:echo@","gen_ai.tool.name": "echo",+ "gen_ai.operation.name": "execute_tool",},id="server_span",),