Skip to content

Commit f9f1854

Browse files
committed
fix check failures
1 parent 1bae687 commit f9f1854

File tree

9 files changed

+152
-107
lines changed

9 files changed

+152
-107
lines changed

instrumentation-genai/opentelemetry-instrumentation-mcp/examples/simple-client-server/client/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mcp import ClientSession, StdioServerParameters
44
from mcp.client.stdio import stdio_client
5+
from pydantic import AnyUrl
56

67
from opentelemetry import trace
78

@@ -38,7 +39,7 @@ async def run():
3839

3940
# Read a resource
4041
print("READING RESOURCE")
41-
_ = await session.read_resource("greeting://hello")
42+
_ = await session.read_resource(AnyUrl("greeting://hello"))
4243

4344
# Call pingweb tool
4445
print("CALL PINGWEB TOOL")

instrumentation-genai/opentelemetry-instrumentation-mcp/examples/simple-client-server/server/mcp_simple_tool/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .server import main
44

5-
sys.exit(main()) # type: ignore[call-arg]
5+
sys.exit(main())

instrumentation-genai/opentelemetry-instrumentation-mcp/examples/simple-client-server/server/mcp_simple_tool/server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ def call_http() -> str:
1313
mcp = FastMCP("PythonMcpDemoFoo")
1414

1515

16-
@mcp.tool()
16+
@mcp.tool() # type: ignore[misc]
1717
def add(a: int, b: int) -> int:
1818
"""Add two numbers"""
1919
return a + b
2020

2121

22-
@mcp.tool()
22+
@mcp.tool() # type: ignore[misc]
2323
def subtract(a: int, b: int) -> int:
2424
"""Subtract two numbers"""
2525
return a - b
2626

2727

28-
@mcp.tool()
28+
@mcp.tool() # type: ignore[misc]
2929
def pingweb() -> str:
3030
"""Ping a web URL and return status"""
3131
return call_http()
3232

3333

3434
# Add a dynamic greeting resource
35-
@mcp.resource("greeting://{name}")
35+
@mcp.resource("greeting://{name}") # type: ignore[misc]
3636
def get_greeting(name: str) -> str:
3737
"""Get a personalized greeting"""
3838
return f"Hello, {name}!"

instrumentation-genai/opentelemetry-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/instrumentation.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
from typing import Any, Callable, Collection, Coroutine, Dict, Optional, Tuple
2020

21-
from wrapt import register_post_import_hook
21+
from wrapt import register_post_import_hook # type: ignore[import-untyped]
2222

2323
from opentelemetry import trace
2424
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -108,23 +108,25 @@ def _uninstrument(self, **kwargs: Any) -> None:
108108
unwrap(self._SERVER_MODULE, "Server._handle_notification")
109109

110110
@staticmethod
111-
def _register_hook(module: str, target: str, wrapper: Callable) -> None:
111+
def _register_hook(
112+
module: str, target: str, wrapper: Callable[..., Any]
113+
) -> None:
112114
"""Register a post-import hook for wrapping a function."""
113115
# pylint: disable=import-outside-toplevel
114-
from wrapt import wrap_function_wrapper
116+
from wrapt import wrap_function_wrapper # type: ignore[import-untyped]
115117

116-
register_post_import_hook(
117-
lambda _: wrap_function_wrapper(module, target, wrapper),
118-
module,
119-
)
118+
def hook(_module: Any) -> None:
119+
wrap_function_wrapper(module, target, wrapper)
120+
121+
register_post_import_hook(hook, module)
120122

121123
def _wrap_session_send(
122124
self,
123-
wrapped: Callable,
125+
wrapped: Callable[..., Coroutine[Any, Any, Any]],
124126
instance: Any,
125127
args: Tuple[Any, ...],
126128
kwargs: Dict[str, Any],
127-
) -> Coroutine:
129+
) -> Coroutine[Any, Any, Any]:
128130
"""
129131
Wrap BaseSession.send_request and send_notification methods.
130132
@@ -174,7 +176,7 @@ async def async_wrapper() -> Any:
174176
) as span:
175177
# Inject trace context
176178
ctx = trace.set_span_in_context(span)
177-
carrier = {}
179+
carrier: Dict[str, Any] = {}
178180
self.propagators.inject(carrier=carrier, context=ctx)
179181
message_json["params"]["_meta"].update(carrier)
180182

@@ -201,7 +203,7 @@ async def async_wrapper() -> Any:
201203

202204
async def _wrap_server_handle_request(
203205
self,
204-
wrapped: Callable,
206+
wrapped: Callable[..., Coroutine[Any, Any, Any]],
205207
instance: Any,
206208
args: Tuple[Any, ...],
207209
kwargs: Dict[str, Any],
@@ -223,7 +225,7 @@ async def _wrap_server_handle_request(
223225

224226
async def _wrap_server_handle_notification(
225227
self,
226-
wrapped: Callable,
228+
wrapped: Callable[..., Coroutine[Any, Any, Any]],
227229
instance: Any,
228230
args: Tuple[Any, ...],
229231
kwargs: Dict[str, Any],
@@ -245,7 +247,7 @@ async def _wrap_server_handle_notification(
245247

246248
async def _wrap_server_message_handler(
247249
self,
248-
wrapped: Callable,
250+
wrapped: Callable[..., Coroutine[Any, Any, Any]],
249251
instance: Any,
250252
args: Tuple[Any, ...],
251253
kwargs: Dict[str, Any],

instrumentation-genai/opentelemetry-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/py.typed

Whitespace-only changes.

instrumentation-genai/opentelemetry-instrumentation-mcp/tests/test_instrumentor.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
class TestMcpInstrumentorInit(unittest.TestCase):
2525
"""Test McpInstrumentor initialization."""
2626

27-
def test_init_default(self):
27+
def test_init_default(self) -> None:
2828
"""Test default initialization."""
2929
instrumentor = McpInstrumentor()
3030
self.assertIsNotNone(instrumentor)
3131
self.assertIsNotNone(instrumentor.tracer)
3232
self.assertIsNotNone(instrumentor.propagators)
3333

34-
def test_init_with_tracer_provider(self):
34+
def test_init_with_tracer_provider(self) -> None:
3535
"""Test initialization with custom tracer provider."""
3636
mock_provider = MagicMock(spec=TracerProvider)
3737
mock_tracer = MagicMock()
@@ -42,7 +42,7 @@ def test_init_with_tracer_provider(self):
4242
instrumentor = McpInstrumentor(tracer_provider=mock_provider)
4343
self.assertIsNotNone(instrumentor.tracer)
4444

45-
def test_init_with_propagators(self):
45+
def test_init_with_propagators(self) -> None:
4646
"""Test initialization with custom propagators."""
4747
mock_propagators = MagicMock()
4848
instrumentor = McpInstrumentor(propagators=mock_propagators)
@@ -52,7 +52,7 @@ def test_init_with_propagators(self):
5252
class TestMcpInstrumentorDependencies(unittest.TestCase):
5353
"""Test instrumentation dependencies."""
5454

55-
def test_instrumentation_dependencies(self):
55+
def test_instrumentation_dependencies(self) -> None:
5656
"""Test that instrumentation_dependencies returns correct packages."""
5757
instrumentor = McpInstrumentor()
5858
deps = instrumentor.instrumentation_dependencies()
@@ -70,15 +70,17 @@ def setUp(self):
7070
@patch(
7171
"opentelemetry.instrumentation.mcp.instrumentation.register_post_import_hook"
7272
)
73-
def test_instrument_registers_hooks(self, mock_register):
73+
def test_instrument_registers_hooks(self, mock_register: MagicMock):
7474
"""Test that _instrument registers all required hooks."""
7575
self.instrumentor._instrument()
7676
self.assertEqual(mock_register.call_count, 4)
7777

7878
@patch(
7979
"opentelemetry.instrumentation.mcp.instrumentation.register_post_import_hook"
8080
)
81-
def test_instrument_registers_session_hooks(self, mock_register):
81+
def test_instrument_registers_session_hooks(
82+
self, mock_register: MagicMock
83+
):
8284
"""Test that _instrument registers session hooks."""
8385
self.instrumentor._instrument()
8486
# Verify mcp.shared.session is registered (appears twice for send_request and send_notification)
@@ -92,7 +94,7 @@ def test_instrument_registers_session_hooks(self, mock_register):
9294
@patch(
9395
"opentelemetry.instrumentation.mcp.instrumentation.register_post_import_hook"
9496
)
95-
def test_instrument_registers_server_hooks(self, mock_register):
97+
def test_instrument_registers_server_hooks(self, mock_register: MagicMock):
9698
"""Test that _instrument registers server hooks."""
9799
self.instrumentor._instrument()
98100
# Verify mcp.server.lowlevel.server is registered (appears twice for _handle_request and _handle_notification)
@@ -111,37 +113,41 @@ def setUp(self):
111113
self.instrumentor = McpInstrumentor()
112114

113115
@patch("opentelemetry.instrumentation.mcp.instrumentation.unwrap")
114-
def test_uninstrument_unwraps_all(self, mock_unwrap):
116+
def test_uninstrument_unwraps_all(self, mock_unwrap: MagicMock):
115117
"""Test that _uninstrument unwraps all hooks."""
116118
self.instrumentor._uninstrument()
117119
self.assertEqual(mock_unwrap.call_count, 4)
118120

119121
@patch("opentelemetry.instrumentation.mcp.instrumentation.unwrap")
120-
def test_uninstrument_unwraps_send_request(self, mock_unwrap):
122+
def test_uninstrument_unwraps_send_request(self, mock_unwrap: MagicMock):
121123
"""Test that _uninstrument unwraps send_request."""
122124
self.instrumentor._uninstrument()
123125
mock_unwrap.assert_any_call(
124126
"mcp.shared.session", "BaseSession.send_request"
125127
)
126128

127129
@patch("opentelemetry.instrumentation.mcp.instrumentation.unwrap")
128-
def test_uninstrument_unwraps_send_notification(self, mock_unwrap):
130+
def test_uninstrument_unwraps_send_notification(
131+
self, mock_unwrap: MagicMock
132+
):
129133
"""Test that _uninstrument unwraps send_notification."""
130134
self.instrumentor._uninstrument()
131135
mock_unwrap.assert_any_call(
132136
"mcp.shared.session", "BaseSession.send_notification"
133137
)
134138

135139
@patch("opentelemetry.instrumentation.mcp.instrumentation.unwrap")
136-
def test_uninstrument_unwraps_handle_request(self, mock_unwrap):
140+
def test_uninstrument_unwraps_handle_request(self, mock_unwrap: MagicMock):
137141
"""Test that _uninstrument unwraps handle_request."""
138142
self.instrumentor._uninstrument()
139143
mock_unwrap.assert_any_call(
140144
"mcp.server.lowlevel.server", "Server._handle_request"
141145
)
142146

143147
@patch("opentelemetry.instrumentation.mcp.instrumentation.unwrap")
144-
def test_uninstrument_unwraps_handle_notification(self, mock_unwrap):
148+
def test_uninstrument_unwraps_handle_notification(
149+
self, mock_unwrap: MagicMock
150+
):
145151
"""Test that _uninstrument unwraps handle_notification."""
146152
self.instrumentor._uninstrument()
147153
mock_unwrap.assert_any_call(
@@ -152,33 +158,33 @@ def test_uninstrument_unwraps_handle_notification(self, mock_unwrap):
152158
class TestMcpInstrumentorSerialize(unittest.TestCase):
153159
"""Test serialize static method."""
154160

155-
def test_serialize_simple_dict(self):
161+
def test_serialize_simple_dict(self) -> None:
156162
"""Test serializing a simple dictionary."""
157163
result = McpInstrumentor.serialize({"key": "value"})
158164
self.assertEqual(result, '{"key": "value"}')
159165

160-
def test_serialize_nested_dict(self):
166+
def test_serialize_nested_dict(self) -> None:
161167
"""Test serializing a nested dictionary."""
162168
result = McpInstrumentor.serialize({"outer": {"inner": "value"}})
163169
self.assertEqual(result, '{"outer": {"inner": "value"}}')
164170

165-
def test_serialize_with_numbers(self):
171+
def test_serialize_with_numbers(self) -> None:
166172
"""Test serializing dictionary with numbers."""
167173
result = McpInstrumentor.serialize({"int": 42, "float": 3.14})
168174
self.assertIn('"int": 42', result)
169175
self.assertIn('"float": 3.14', result)
170176

171-
def test_serialize_with_list(self):
177+
def test_serialize_with_list(self) -> None:
172178
"""Test serializing dictionary with list."""
173179
result = McpInstrumentor.serialize({"list": [1, 2, 3]})
174180
self.assertEqual(result, '{"list": [1, 2, 3]}')
175181

176-
def test_serialize_empty_dict(self):
182+
def test_serialize_empty_dict(self) -> None:
177183
"""Test serializing empty dictionary."""
178184
result = McpInstrumentor.serialize({})
179185
self.assertEqual(result, "{}")
180186

181-
def test_serialize_non_serializable(self):
187+
def test_serialize_non_serializable(self) -> None:
182188
"""Test serializing non-serializable object returns empty string."""
183189

184190
class NonSerializable:
@@ -187,12 +193,12 @@ class NonSerializable:
187193
result = McpInstrumentor.serialize({"obj": NonSerializable()})
188194
self.assertEqual(result, "")
189195

190-
def test_serialize_with_none(self):
196+
def test_serialize_with_none(self) -> None:
191197
"""Test serializing dictionary with None value."""
192198
result = McpInstrumentor.serialize({"key": None})
193199
self.assertEqual(result, '{"key": null}')
194200

195-
def test_serialize_with_boolean(self):
201+
def test_serialize_with_boolean(self) -> None:
196202
"""Test serializing dictionary with boolean values."""
197203
result = McpInstrumentor.serialize({"true": True, "false": False})
198204
self.assertIn('"true": true', result)

0 commit comments

Comments
 (0)