|
1 | 1 | from contextlib import asynccontextmanager
|
2 | 2 | from dataclasses import dataclass
|
3 |
| -from typing import Any, AsyncGenerator, Callable, Collection, Tuple, cast |
| 3 | +from typing import Any, AsyncGenerator, Callable, Collection, Tuple, cast, Union |
4 | 4 | import json
|
5 | 5 | import logging
|
6 | 6 | import traceback
|
@@ -124,16 +124,30 @@ def _transport_wrapper(self, tracer):
|
124 | 124 | async def traced_method(
|
125 | 125 | wrapped: Callable[..., Any], instance: Any, args: Any, kwargs: Any
|
126 | 126 | ) -> AsyncGenerator[
|
127 |
| - Tuple["InstrumentedStreamReader", "InstrumentedStreamWriter"], None |
| 127 | + Union[ |
| 128 | + Tuple[InstrumentedStreamReader, InstrumentedStreamWriter], |
| 129 | + Tuple[InstrumentedStreamReader, InstrumentedStreamWriter, Any] |
| 130 | + ], |
| 131 | + None |
128 | 132 | ]:
|
129 | 133 | async with wrapped(*args, **kwargs) as result:
|
130 | 134 | try:
|
131 | 135 | read_stream, write_stream = result
|
| 136 | + yield InstrumentedStreamReader( |
| 137 | + read_stream, tracer |
| 138 | + ), InstrumentedStreamWriter(write_stream, tracer) |
132 | 139 | except ValueError:
|
133 |
| - read_stream, write_stream, _ = result |
134 |
| - yield InstrumentedStreamReader( |
135 |
| - read_stream, tracer |
136 |
| - ), InstrumentedStreamWriter(write_stream, tracer) |
| 140 | + try: |
| 141 | + read_stream, write_stream, get_session_id_callback = result |
| 142 | + yield InstrumentedStreamReader( |
| 143 | + read_stream, tracer |
| 144 | + ), InstrumentedStreamWriter(write_stream, tracer), get_session_id_callback |
| 145 | + except Exception as e: |
| 146 | + logging.warning(f"mcp instrumentation _transport_wrapper exception: {e}") |
| 147 | + yield result |
| 148 | + except Exception as e: |
| 149 | + logging.warning(f"mcp instrumentation transport_wrapper exception: {e}") |
| 150 | + yield result |
137 | 151 |
|
138 | 152 | return traced_method
|
139 | 153 |
|
|
0 commit comments