Skip to content

Commit 92d2a74

Browse files
umaannamalaimergify[bot]TimPansino
committed
Update MCP instrumentation to check if AIM is enabled. (#1456)
* Update MCP instrumentation to check if AIM is enabled. * Linting --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Tim Pansino <[email protected]>
1 parent 9bddd09 commit 92d2a74

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

newrelic/hooks/adapter_mcp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from newrelic.common.signature import bind_args
2222
from newrelic.core.config import global_settings
2323

24-
2524
_logger = logging.getLogger(__name__)
2625

2726

@@ -30,7 +29,7 @@ async def wrap_call_tool(wrapped, instance, args, kwargs):
3029
if not transaction:
3130
return await wrapped(*args, **kwargs)
3231

33-
settings = transaction.settings if transaction.settings is not None else global_settings()
32+
settings = transaction.settings or global_settings()
3433
if not settings.ai_monitoring.enabled:
3534
return await wrapped(*args, **kwargs)
3635

@@ -48,7 +47,7 @@ async def wrap_read_resource(wrapped, instance, args, kwargs):
4847
if not transaction:
4948
return await wrapped(*args, **kwargs)
5049

51-
settings = transaction.settings if transaction.settings is not None else global_settings()
50+
settings = transaction.settings or global_settings()
5251
if not settings.ai_monitoring.enabled:
5352
return await wrapped(*args, **kwargs)
5453

@@ -74,7 +73,7 @@ async def wrap_get_prompt(wrapped, instance, args, kwargs):
7473
if not transaction:
7574
return await wrapped(*args, **kwargs)
7675

77-
settings = transaction.settings if transaction.settings is not None else global_settings()
76+
settings = transaction.settings or global_settings()
7877
if not settings.ai_monitoring.enabled:
7978
return await wrapped(*args, **kwargs)
8079

tests/adapter_mcp/test_mcp.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
# limitations under the License.
1414

1515
import pytest
16+
1617
from fastmcp.client import Client
1718
from fastmcp.client.transports import FastMCPTransport
1819
from fastmcp.server.server import FastMCP
1920
from mcp.server.fastmcp.tools import ToolManager
2021

22+
from testing_support.fixtures import function_not_called
23+
from testing_support.ml_testing_utils import disabled_ai_monitoring_settings
24+
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
25+
26+
2127
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
2228
from newrelic.api.background_task import background_task
2329

@@ -61,7 +67,6 @@ async def _test():
6167
async with Client(transport=FastMCPTransport(fastmcp_server)) as client:
6268
# Call the MCP tool, so we can validate the trace naming is correct.
6369
result = await client.call_tool("add_exclamation", {"phrase": "Python is awesome"})
64-
6570
content = str(result.content[0])
6671
assert "Python is awesome!" in content
6772

@@ -135,3 +140,38 @@ async def _test():
135140
assert "Python is cool" in content
136141

137142
loop.run_until_complete(_test())
143+
144+
145+
@disabled_ai_monitoring_settings
146+
@function_not_called("newrelic.api.function_trace", "FunctionTrace.__enter__")
147+
@background_task()
148+
def test_tool_tracing_aim_disabled(loop, fastmcp_server):
149+
async def _test():
150+
async with Client(transport=FastMCPTransport(fastmcp_server)) as client:
151+
# Call the MCP tool, so we can validate the trace naming is correct.
152+
result = await client.call_tool("add_exclamation", {"phrase": "Python is awesome"})
153+
content = str(result.content[0])
154+
assert "Python is awesome!" in content
155+
156+
loop.run_until_complete(_test())
157+
158+
159+
@disabled_ai_monitoring_settings
160+
@function_not_called("newrelic.api.function_trace", "FunctionTrace.__enter__")
161+
@background_task()
162+
def test_resource_tracing_aim_disabled(loop, fastmcp_server):
163+
run_read_resources(loop, fastmcp_server, "greeting://Python")
164+
165+
166+
@disabled_ai_monitoring_settings
167+
@function_not_called("newrelic.api.function_trace", "FunctionTrace.__enter__")
168+
@background_task()
169+
def test_prompt_tracing_aim_disabled(loop, fastmcp_server):
170+
async def _test():
171+
async with Client(transport=FastMCPTransport(fastmcp_server)) as client:
172+
result = await client.get_prompt("echo_prompt", {"message": "Python is cool"})
173+
174+
content = str(result)
175+
assert "Python is cool" in content
176+
177+
loop.run_until_complete(_test())

0 commit comments

Comments
 (0)