Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion newrelic/hooks/adapter_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging

from newrelic.api.function_trace import FunctionTrace
Expand All @@ -37,8 +38,10 @@ async def wrap_call_tool(wrapped, instance, args, kwargs):
bound_args = bind_args(wrapped, args, kwargs)
tool_name = bound_args.get("name") or "tool"
function_trace_name = f"{func_name}/{tool_name}"
agentic_subcomponent_data = {"type": "APM-AI_TOOL", "name": tool_name}

with FunctionTrace(name=function_trace_name, group="Llm/tool/MCP", source=wrapped):
with FunctionTrace(name=function_trace_name, group="Llm/tool/MCP", source=wrapped) as ft:
ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data))
return await wrapped(*args, **kwargs)


Expand Down
3 changes: 3 additions & 0 deletions tests/adapter_mcp/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from mcp.server.fastmcp.tools import ToolManager
from testing_support.ml_testing_utils import disabled_ai_monitoring_settings
from testing_support.validators.validate_function_not_called import validate_function_not_called
from testing_support.validators.validate_span_events import validate_span_events
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics

from newrelic.api.background_task import background_task
Expand Down Expand Up @@ -57,6 +58,7 @@ def echo_prompt(message: str):
rollup_metrics=[("Llm/tool/MCP/mcp.client.session:ClientSession.call_tool/add_exclamation", 1)],
background_task=True,
)
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_tool_tracing_via_client_session(loop, fastmcp_server):
async def _test():
Expand All @@ -75,6 +77,7 @@ async def _test():
rollup_metrics=[("Llm/tool/MCP/mcp.server.fastmcp.tools.tool_manager:ToolManager.call_tool/add_exclamation", 1)],
background_task=True,
)
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_tool_tracing_via_tool_manager(loop):
async def _test():
Expand Down