19
19
from newrelic .common .object_names import callable_name
20
20
from newrelic .common .object_wrapper import wrap_function_wrapper
21
21
from newrelic .common .signature import bind_args
22
+ from newrelic .core .config import global_settings
23
+
22
24
23
25
_logger = logging .getLogger (__name__ )
24
26
@@ -28,6 +30,10 @@ async def wrap_call_tool(wrapped, instance, args, kwargs):
28
30
if not transaction :
29
31
return await wrapped (* args , ** kwargs )
30
32
33
+ settings = transaction .settings if transaction .settings is not None else global_settings ()
34
+ if not settings .ai_monitoring .enabled :
35
+ return await wrapped (* args , ** kwargs )
36
+
31
37
func_name = callable_name (wrapped )
32
38
bound_args = bind_args (wrapped , args , kwargs )
33
39
tool_name = bound_args .get ("name" ) or "tool"
@@ -42,6 +48,10 @@ async def wrap_read_resource(wrapped, instance, args, kwargs):
42
48
if not transaction :
43
49
return await wrapped (* args , ** kwargs )
44
50
51
+ settings = transaction .settings if transaction .settings is not None else global_settings ()
52
+ if not settings .ai_monitoring .enabled :
53
+ return await wrapped (* args , ** kwargs )
54
+
45
55
func_name = callable_name (wrapped )
46
56
bound_args = bind_args (wrapped , args , kwargs )
47
57
# Set a default value in case we can't parse out the URI scheme successfully
@@ -64,6 +74,10 @@ async def wrap_get_prompt(wrapped, instance, args, kwargs):
64
74
if not transaction :
65
75
return await wrapped (* args , ** kwargs )
66
76
77
+ settings = transaction .settings if transaction .settings is not None else global_settings ()
78
+ if not settings .ai_monitoring .enabled :
79
+ return await wrapped (* args , ** kwargs )
80
+
67
81
func_name = callable_name (wrapped )
68
82
bound_args = bind_args (wrapped , args , kwargs )
69
83
prompt_name = bound_args .get ("name" ) or "prompt"
@@ -81,3 +95,9 @@ def instrument_mcp_client_session(module):
81
95
wrap_function_wrapper (module , "ClientSession.read_resource" , wrap_read_resource )
82
96
if hasattr (module .ClientSession , "get_prompt" ):
83
97
wrap_function_wrapper (module , "ClientSession.get_prompt" , wrap_get_prompt )
98
+
99
+
100
+ def instrument_mcp_server_fastmcp_tools_tool_manager (module ):
101
+ if hasattr (module , "ToolManager" ):
102
+ if hasattr (module .ToolManager , "call_tool" ):
103
+ wrap_function_wrapper (module , "ToolManager.call_tool" , wrap_call_tool )
0 commit comments