Skip to content

Commit 5f80b89

Browse files
committed
Fix mcp instrumentation
Change-Id: I6506a60a343036a43089982aa52d4a7165db9b0d Co-developed-by: Cursor <noreply@cursor.com>
1 parent 56c3a8d commit 5f80b89

5 files changed

Lines changed: 52 additions & 6 deletions

File tree

instrumentation-loongsuite/loongsuite-instrumentation-agentscope/src/opentelemetry/instrumentation/agentscope/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def _setup_tracing_patch(self, wrapped, instance, args, kwargs):
9696
"""Replace setup_tracing with no-op to use OTEL instead."""
9797
pass
9898

99+
def _check_tracing_enabled_patch(self, wrapped, instance, args, kwargs):
100+
"""Return False to disable tracing in native AgentScope library."""
101+
return False
102+
99103
def _instrument(self, **kwargs: Any) -> None:
100104
"""Enable AgentScope instrumentation."""
101105
tracer_provider = kwargs.get("tracer_provider")
@@ -199,6 +203,18 @@ def wrap_formatter_with_tracer(wrapped, instance, args, kwargs):
199203
logger.debug("Patched setup_tracing")
200204
except Exception as e:
201205
logger.warning(f"Failed to patch setup_tracing: {e}")
206+
207+
# Patch _check_tracing_enabled to return False
208+
# We always want to disable tracing in native AgentScope library
209+
try:
210+
wrap_function_wrapper(
211+
module="agentscope.tracing._trace",
212+
name="_check_tracing_enabled",
213+
wrapper=self._check_tracing_enabled_patch,
214+
)
215+
logger.debug("Patched _check_tracing_enabled")
216+
except Exception as e:
217+
logger.warning(f"Failed to patch _check_tracing_enabled: {e}")
202218

203219
def _uninstrument(self, **kwargs: Any) -> None:
204220
"""Disable AgentScope instrumentation."""
@@ -269,3 +285,11 @@ def _uninstrument(self, **kwargs: Any) -> None:
269285
logger.debug("Uninstrumented setup_tracing")
270286
except Exception as e:
271287
logger.warning(f"Failed to uninstrument setup_tracing: {e}")
288+
289+
try:
290+
import agentscope.tracing # noqa: PLC0415
291+
292+
unwrap(agentscope.tracing, "_check_tracing_enabled")
293+
logger.debug("Uninstrumented _check_tracing_enabled")
294+
except Exception as e:
295+
logger.warning(f"Failed to uninstrument _check_tracing_enabled: {e}")

instrumentation-loongsuite/loongsuite-instrumentation-mcp/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies = [
3131

3232
[project.optional-dependencies]
3333
instruments = [
34-
"mcp >= 1.3.0, <= 1.13.1",
34+
"mcp >= 1.3.0, <= 1.25.0",
3535
]
3636
test = [
3737
"opentelemetry-sdk",

instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from opentelemetry.instrumentation.mcp.utils import (
2121
_get_logger,
22+
_get_streamable_http_client_name,
2223
_is_version_supported,
2324
_is_ws_installed,
2425
)
@@ -53,6 +54,8 @@
5354
for method_name, rpc_name in RPC_NAME_MAPPING.items()
5455
]
5556

57+
_streamable_http_client_name = _get_streamable_http_client_name()
58+
5659

5760
class MCPInstrumentor(BaseInstrumentor):
5861
"""
@@ -99,7 +102,7 @@ def _instrument(self, **kwargs: Any) -> None:
99102
)
100103
wrap_function_wrapper(
101104
module="mcp.client.streamable_http",
102-
name="streamablehttp_client",
105+
name=_streamable_http_client_name,
103106
wrapper=streamable_http_client_wrapper(),
104107
)
105108
wrap_function_wrapper(
@@ -140,10 +143,10 @@ def _uninstrument(self, **kwargs: Any) -> None:
140143
try:
141144
import mcp.client.streamable_http # noqa: PLC0415
142145

143-
unwrap(mcp.client.streamable_http, "streamablehttp_client")
146+
unwrap(mcp.client.streamable_http, _streamable_http_client_name)
144147
except Exception:
145148
logger.warning(
146-
"Fail to uninstrument streamablehttp_client", exc_info=True
149+
"Fail to uninstrument streamable_http_client", exc_info=True
147150
)
148151

149152
try:

instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
_has_mcp_types = False
1919

2020
MIN_SUPPORTED_VERSION = (1, 3, 0)
21-
MAX_SUPPORTED_VERSION = (1, 13, 1)
21+
MAX_SUPPORTED_VERSION = (1, 25, 0)
2222
MCP_PACKAGE_NAME = "mcp"
2323
DEFAULT_MAX_ATTRIBUTE_LENGTH = 1024 * 1024
2424

25+
# Version thresholds for API changes
26+
# v1.24.0: streamable_http_client was added (streamablehttp_client deprecated)
27+
STREAMABLE_HTTP_CLIENT_NEW_NAME_VERSION = (1, 24, 0)
28+
# Streamable HTTP client function names
29+
STREAMABLE_HTTP_CLIENT_NEW_NAME = "streamable_http_client"
30+
STREAMABLE_HTTP_CLIENT_OLD_NAME = "streamablehttp_client"
31+
2532
_max_attributes_length = None
2633

2734

@@ -77,6 +84,18 @@ def _is_version_supported() -> bool:
7784
)
7885

7986

87+
def _get_streamable_http_client_name() -> str:
88+
"""
89+
Get the correct streamable HTTP client function name based on MCP version.
90+
- v1.24.0+: uses `streamable_http_client` (new name)
91+
- v1.3.0 - v1.23.x: uses `streamablehttp_client` (old name)
92+
"""
93+
current_version = _get_mcp_version()
94+
if current_version >= STREAMABLE_HTTP_CLIENT_NEW_NAME_VERSION:
95+
return STREAMABLE_HTTP_CLIENT_NEW_NAME
96+
return STREAMABLE_HTTP_CLIENT_OLD_NAME
97+
98+
8099
def _is_capture_content_enabled() -> bool:
81100
capture_content = environ.get(
82101
MCPEnvironmentVariables.CAPTURE_INPUT_ENABLED, "true"

loongsuite-distro/src/loongsuite/distro/bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def extract_package_name_from_requirement(req_str: str) -> str:
105105
106106
Examples:
107107
"redis >= 2.6" -> "redis"
108-
"opentelemetry-instrumentation==0.60b1" -> "opentelemetry-instrumentation"
108+
"opentelemetry-instrumentation==0.60b0" -> "opentelemetry-instrumentation"
109109
"package-name~=1.0" -> "package-name"
110110
111111
Args:

0 commit comments

Comments
 (0)