mcp-use 1.3.12__py3-none-any.whl → 1.3.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mcp-use might be problematic. Click here for more details.

Files changed (101) hide show
  1. mcp_use/__init__.py +1 -1
  2. mcp_use/adapters/.deprecated +0 -0
  3. mcp_use/adapters/__init__.py +18 -7
  4. mcp_use/adapters/base.py +12 -185
  5. mcp_use/adapters/langchain_adapter.py +12 -219
  6. mcp_use/agents/adapters/__init__.py +10 -0
  7. mcp_use/agents/adapters/base.py +193 -0
  8. mcp_use/agents/adapters/langchain_adapter.py +228 -0
  9. mcp_use/agents/base.py +1 -1
  10. mcp_use/agents/managers/__init__.py +19 -0
  11. mcp_use/agents/managers/base.py +36 -0
  12. mcp_use/agents/managers/server_manager.py +131 -0
  13. mcp_use/agents/managers/tools/__init__.py +15 -0
  14. mcp_use/agents/managers/tools/base_tool.py +19 -0
  15. mcp_use/agents/managers/tools/connect_server.py +69 -0
  16. mcp_use/agents/managers/tools/disconnect_server.py +43 -0
  17. mcp_use/agents/managers/tools/get_active_server.py +29 -0
  18. mcp_use/agents/managers/tools/list_servers_tool.py +53 -0
  19. mcp_use/agents/managers/tools/search_tools.py +328 -0
  20. mcp_use/agents/mcpagent.py +16 -14
  21. mcp_use/agents/remote.py +14 -1
  22. mcp_use/auth/.deprecated +0 -0
  23. mcp_use/auth/__init__.py +19 -4
  24. mcp_use/auth/bearer.py +11 -12
  25. mcp_use/auth/oauth.py +11 -620
  26. mcp_use/auth/oauth_callback.py +16 -207
  27. mcp_use/client/__init__.py +1 -0
  28. mcp_use/client/auth/__init__.py +6 -0
  29. mcp_use/client/auth/bearer.py +23 -0
  30. mcp_use/client/auth/oauth.py +629 -0
  31. mcp_use/client/auth/oauth_callback.py +214 -0
  32. mcp_use/client/client.py +356 -0
  33. mcp_use/client/config.py +106 -0
  34. mcp_use/client/connectors/__init__.py +20 -0
  35. mcp_use/client/connectors/base.py +470 -0
  36. mcp_use/client/connectors/http.py +304 -0
  37. mcp_use/client/connectors/sandbox.py +332 -0
  38. mcp_use/client/connectors/stdio.py +109 -0
  39. mcp_use/client/connectors/utils.py +13 -0
  40. mcp_use/client/connectors/websocket.py +257 -0
  41. mcp_use/client/exceptions.py +31 -0
  42. mcp_use/client/middleware/__init__.py +50 -0
  43. mcp_use/client/middleware/logging.py +31 -0
  44. mcp_use/client/middleware/metrics.py +314 -0
  45. mcp_use/client/middleware/middleware.py +266 -0
  46. mcp_use/client/session.py +162 -0
  47. mcp_use/client/task_managers/__init__.py +20 -0
  48. mcp_use/client/task_managers/base.py +145 -0
  49. mcp_use/client/task_managers/sse.py +84 -0
  50. mcp_use/client/task_managers/stdio.py +69 -0
  51. mcp_use/client/task_managers/streamable_http.py +86 -0
  52. mcp_use/client/task_managers/websocket.py +68 -0
  53. mcp_use/client.py +12 -344
  54. mcp_use/config.py +20 -97
  55. mcp_use/connectors/.deprecated +0 -0
  56. mcp_use/connectors/__init__.py +46 -20
  57. mcp_use/connectors/base.py +12 -455
  58. mcp_use/connectors/http.py +13 -300
  59. mcp_use/connectors/sandbox.py +13 -306
  60. mcp_use/connectors/stdio.py +13 -104
  61. mcp_use/connectors/utils.py +15 -8
  62. mcp_use/connectors/websocket.py +13 -252
  63. mcp_use/exceptions.py +33 -18
  64. mcp_use/managers/.deprecated +0 -0
  65. mcp_use/managers/__init__.py +56 -17
  66. mcp_use/managers/base.py +13 -31
  67. mcp_use/managers/server_manager.py +13 -119
  68. mcp_use/managers/tools/__init__.py +45 -15
  69. mcp_use/managers/tools/base_tool.py +5 -16
  70. mcp_use/managers/tools/connect_server.py +5 -67
  71. mcp_use/managers/tools/disconnect_server.py +5 -41
  72. mcp_use/managers/tools/get_active_server.py +5 -26
  73. mcp_use/managers/tools/list_servers_tool.py +5 -51
  74. mcp_use/managers/tools/search_tools.py +17 -321
  75. mcp_use/middleware/.deprecated +0 -0
  76. mcp_use/middleware/__init__.py +89 -50
  77. mcp_use/middleware/logging.py +14 -26
  78. mcp_use/middleware/metrics.py +30 -303
  79. mcp_use/middleware/middleware.py +39 -246
  80. mcp_use/session.py +13 -149
  81. mcp_use/task_managers/.deprecated +0 -0
  82. mcp_use/task_managers/__init__.py +48 -20
  83. mcp_use/task_managers/base.py +13 -140
  84. mcp_use/task_managers/sse.py +13 -79
  85. mcp_use/task_managers/stdio.py +13 -64
  86. mcp_use/task_managers/streamable_http.py +15 -81
  87. mcp_use/task_managers/websocket.py +13 -63
  88. mcp_use/telemetry/events.py +58 -0
  89. mcp_use/telemetry/telemetry.py +71 -1
  90. mcp_use/types/.deprecated +0 -0
  91. mcp_use/types/sandbox.py +13 -18
  92. {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/METADATA +59 -34
  93. mcp_use-1.3.13.dist-info/RECORD +109 -0
  94. mcp_use-1.3.12.dist-info/RECORD +0 -64
  95. mcp_use-1.3.12.dist-info/licenses/LICENSE +0 -21
  96. /mcp_use/{observability → agents/observability}/__init__.py +0 -0
  97. /mcp_use/{observability → agents/observability}/callbacks_manager.py +0 -0
  98. /mcp_use/{observability → agents/observability}/laminar.py +0 -0
  99. /mcp_use/{observability → agents/observability}/langfuse.py +0 -0
  100. {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/WHEEL +0 -0
  101. {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/entry_points.txt +0 -0
@@ -1,461 +1,18 @@
1
- """
2
- Base connector for MCP implementations.
3
-
4
- This module provides the base connector interface that all MCP connectors
5
- must implement.
6
- """
7
-
1
+ # mcp_use/connectors/base.py
8
2
  import warnings
9
- from abc import ABC, abstractmethod
10
- from datetime import timedelta
11
- from typing import Any
12
-
13
- from mcp import ClientSession, Implementation
14
- from mcp.client.session import ElicitationFnT, LoggingFnT, MessageHandlerFnT, SamplingFnT
15
- from mcp.shared.exceptions import McpError
16
- from mcp.types import (
17
- CallToolResult,
18
- GetPromptResult,
19
- Prompt,
20
- PromptListChangedNotification,
21
- ReadResourceResult,
22
- Resource,
23
- ResourceListChangedNotification,
24
- ServerCapabilities,
25
- ServerNotification,
26
- Tool,
27
- ToolListChangedNotification,
28
- )
29
- from pydantic import AnyUrl
30
-
31
- import mcp_use
32
-
33
- from ..logging import logger
34
- from ..middleware import Middleware, MiddlewareManager
35
- from ..task_managers import ConnectionManager
36
-
37
-
38
- class BaseConnector(ABC):
39
- """Base class for MCP connectors.
40
-
41
- This class defines the interface that all MCP connectors must implement.
42
- """
43
-
44
- def __init__(
45
- self,
46
- sampling_callback: SamplingFnT | None = None,
47
- elicitation_callback: ElicitationFnT | None = None,
48
- message_handler: MessageHandlerFnT | None = None,
49
- logging_callback: LoggingFnT | None = None,
50
- middleware: list[Middleware] | None = None,
51
- ):
52
- """Initialize base connector with common attributes."""
53
- self.client_session: ClientSession | None = None
54
- self._connection_manager: ConnectionManager | None = None
55
- self._tools: list[Tool] | None = None
56
- self._resources: list[Resource] | None = None
57
- self._prompts: list[Prompt] | None = None
58
- self._connected = False
59
- self._initialized = False # Track if client_session.initialize() has been called
60
- self.auto_reconnect = True # Whether to automatically reconnect on connection loss (not configurable for now)
61
- self.sampling_callback = sampling_callback
62
- self.elicitation_callback = elicitation_callback
63
- self.message_handler = message_handler
64
- self.logging_callback = logging_callback
65
- self.capabilities: ServerCapabilities | None = None
66
-
67
- # Set up middleware manager
68
- self.middleware_manager = MiddlewareManager()
69
- if middleware:
70
- for mw in middleware:
71
- self.middleware_manager.add_middleware(mw)
72
-
73
- @property
74
- def client_info(self) -> Implementation:
75
- """Get the client info for the connector."""
76
- return Implementation(
77
- name="mcp-use",
78
- version=mcp_use.__version__,
79
- url="https://github.com/mcp-use/mcp-use",
80
- )
81
-
82
- async def _internal_message_handler(self, message: Any) -> None:
83
- """Wrap the user-provided message handler."""
84
- if isinstance(message, ServerNotification):
85
- if isinstance(message.root, ToolListChangedNotification):
86
- logger.debug("Received tool list changed notification")
87
- elif isinstance(message.root, ResourceListChangedNotification):
88
- logger.debug("Received resource list changed notification")
89
- elif isinstance(message.root, PromptListChangedNotification):
90
- logger.debug("Received prompt list changed notification")
91
-
92
- # Call the user's handler
93
- if self.message_handler:
94
- await self.message_handler(message)
95
-
96
- @abstractmethod
97
- async def connect(self) -> None:
98
- """Establish a connection to the MCP implementation."""
99
- pass
100
-
101
- @property
102
- @abstractmethod
103
- def public_identifier(self) -> str:
104
- """Get the identifier for the connector."""
105
- pass
106
-
107
- async def disconnect(self) -> None:
108
- """Close the connection to the MCP implementation."""
109
- if not self._connected:
110
- logger.debug("Not connected to MCP implementation")
111
- return
112
-
113
- logger.debug("Disconnecting from MCP implementation")
114
- await self._cleanup_resources()
115
- self._connected = False
116
- logger.debug("Disconnected from MCP implementation")
117
-
118
- async def _cleanup_resources(self) -> None:
119
- """Clean up all resources associated with this connector."""
120
- errors = []
121
-
122
- # First stop the connection manager, this closes the ClientSession inside
123
- # the same task where it was opened, avoiding cancel-scope mismatches.
124
- if self._connection_manager:
125
- try:
126
- logger.debug("Stopping connection manager")
127
- await self._connection_manager.stop()
128
- except Exception as e:
129
- error_msg = f"Error stopping connection manager: {e}"
130
- logger.warning(error_msg)
131
- errors.append(error_msg)
132
- finally:
133
- self._connection_manager = None
134
-
135
- # Ensure the client_session reference is cleared (it should already be
136
- # closed by the connection manager). Only attempt a direct __aexit__ if
137
- # the connection manager did *not* exist, this covers edge-cases like
138
- # failed connections where no manager was started.
139
- if self.client_session:
140
- try:
141
- if not self._connection_manager:
142
- logger.debug("Closing client session (no connection manager)")
143
- await self.client_session.__aexit__(None, None, None)
144
- except Exception as e:
145
- error_msg = f"Error closing client session: {e}"
146
- logger.warning(error_msg)
147
- errors.append(error_msg)
148
- finally:
149
- self.client_session = None
150
-
151
- # Reset tools
152
- self._tools = None
153
- self._resources = None
154
- self._prompts = None
155
- self._initialized = False # Reset initialization flag
156
-
157
- if errors:
158
- logger.warning(f"Encountered {len(errors)} errors during resource cleanup")
159
-
160
- async def initialize(self) -> dict[str, Any]:
161
- """Initialize the MCP session and return session information."""
162
- if not self.client_session:
163
- raise RuntimeError("MCP client is not connected")
164
-
165
- # Check if already initialized
166
- if self._initialized:
167
- return {"status": "already_initialized"}
168
-
169
- # Initialize the session
170
- result = await self.client_session.initialize()
171
- self._initialized = True # Mark as initialized
172
-
173
- self.capabilities = result.capabilities
174
-
175
- if self.capabilities.tools:
176
- # Get available tools directly from client session
177
- try:
178
- tools_result = await self.client_session.list_tools()
179
- self._tools = tools_result.tools if tools_result else []
180
- except Exception as e:
181
- logger.error(f"Error listing tools for connector {self.public_identifier}: {e}")
182
- self._tools = []
183
- else:
184
- self._tools = []
185
-
186
- if self.capabilities.resources:
187
- # Get available resources directly from client session
188
- try:
189
- resources_result = await self.client_session.list_resources()
190
- self._resources = resources_result.resources if resources_result else []
191
- except Exception as e:
192
- logger.error(f"Error listing resources for connector {self.public_identifier}: {e}")
193
- self._resources = []
194
- else:
195
- self._resources = []
196
-
197
- if self.capabilities.prompts:
198
- # Get available prompts directly from client session
199
- try:
200
- prompts_result = await self.client_session.list_prompts()
201
- self._prompts = prompts_result.prompts if prompts_result else []
202
- except Exception as e:
203
- logger.error(f"Error listing prompts for connector {self.public_identifier}: {e}")
204
- self._prompts = []
205
- else:
206
- self._prompts = []
207
-
208
- logger.debug(
209
- f"MCP session initialized with {len(self._tools)} tools, "
210
- f"{len(self._resources)} resources, "
211
- f"and {len(self._prompts)} prompts"
212
- )
213
-
214
- return result
215
-
216
- @property
217
- def tools(self) -> list[Tool]:
218
- """Get the list of available tools.
219
-
220
- .. deprecated::
221
- This property is deprecated because it may return stale data when the server
222
- sends list change notifications. Use `await list_tools()` instead to ensure
223
- you always get the latest data.
224
- """
225
- warnings.warn(
226
- "The 'tools' property is deprecated and may return stale data. "
227
- "Use 'await list_tools()' instead to ensure fresh data.",
228
- DeprecationWarning,
229
- stacklevel=2,
230
- )
231
- if self._tools is None:
232
- raise RuntimeError("MCP client is not initialized")
233
- return self._tools
234
-
235
- @property
236
- def resources(self) -> list[Resource]:
237
- """Get the list of available resources.
238
3
 
239
- .. deprecated::
240
- This property is deprecated because it may return stale data when the server
241
- sends list change notifications. Use `await list_resources()` instead to ensure
242
- you always get the latest data.
243
- """
244
- warnings.warn(
245
- "The 'resources' property is deprecated and may return stale data. "
246
- "Use 'await list_resources()' instead to ensure fresh data.",
247
- DeprecationWarning,
248
- stacklevel=2,
249
- )
250
- if self._resources is None:
251
- raise RuntimeError("MCP client is not initialized")
252
- return self._resources
4
+ from typing_extensions import deprecated
253
5
 
254
- @property
255
- def prompts(self) -> list[Prompt]:
256
- """Get the list of available prompts.
6
+ from mcp_use.client.connectors.base import BaseConnector as _BaseConnector
257
7
 
258
- .. deprecated::
259
- This property is deprecated because it may return stale data when the server
260
- sends list change notifications. Use `await list_prompts()' instead to ensure
261
- you always get the latest data.
262
- """
263
- warnings.warn(
264
- "The 'prompts' property is deprecated and may return stale data. "
265
- "Use 'await list_prompts()' instead to ensure fresh data.",
266
- DeprecationWarning,
267
- stacklevel=2,
268
- )
269
- if self._prompts is None:
270
- raise RuntimeError("MCP client is not initialized")
271
- return self._prompts
272
-
273
- @property
274
- def is_connected(self) -> bool:
275
- """Check if the connector is actually connected and the connection is alive.
276
-
277
- This property checks not only the connected flag but also verifies that
278
- the underlying connection manager and streams are still active.
279
-
280
- Returns:
281
- True if the connector is connected and the connection is alive, False otherwise.
282
- """
283
-
284
- # Check if we have a client session
285
- if not self.client_session:
286
- # Update the connected flag since we don't have a client session
287
- self._connected = False
288
- return False
289
-
290
- # First check the basic connected flag
291
- if not self._connected:
292
- return False
293
-
294
- # Check if we have a connection manager and if its task is still running
295
- if self._connection_manager:
296
- try:
297
- # Check if the connection manager task is done (indicates disconnection)
298
- if hasattr(self._connection_manager, "_task") and self._connection_manager._task:
299
- if self._connection_manager._task.done():
300
- logger.debug("Connection manager task is done, marking as disconnected")
301
- self._connected = False
302
- return False
303
-
304
- # For HTTP-based connectors, also check if streams are still open
305
- # Use the get_streams method to get the current connection
306
- streams = self._connection_manager.get_streams()
307
- if streams:
308
- # Connection should be a tuple of (read_stream, write_stream)
309
- if isinstance(streams, tuple) and len(streams) == 2:
310
- read_stream, write_stream = streams
311
- # Check if streams are closed using getattr with default value
312
- if getattr(read_stream, "_closed", False):
313
- logger.debug("Read stream is closed, marking as disconnected")
314
- self._connected = False
315
- return False
316
- if getattr(write_stream, "_closed", False):
317
- logger.debug("Write stream is closed, marking as disconnected")
318
- self._connected = False
319
- return False
320
-
321
- except Exception as e:
322
- # If we can't check the connection state, assume disconnected for safety
323
- logger.debug(f"Error checking connection state: {e}, marking as disconnected")
324
- self._connected = False
325
- return False
326
-
327
- return True
328
-
329
- async def _ensure_connected(self) -> None:
330
- """Ensure the connector is connected, reconnecting if necessary.
331
-
332
- Raises:
333
- RuntimeError: If connection cannot be established and auto_reconnect is False.
334
- """
335
- if not self.client_session:
336
- raise RuntimeError("MCP client is not connected")
337
-
338
- if not self.is_connected:
339
- if self.auto_reconnect:
340
- logger.debug("Connection lost, attempting to reconnect...")
341
- try:
342
- await self.connect()
343
- logger.debug("Reconnection successful")
344
- except Exception as e:
345
- raise RuntimeError(f"Failed to reconnect to MCP server: {e}") from e
346
- else:
347
- raise RuntimeError(
348
- "Connection to MCP server has been lost. Auto-reconnection is disabled. Please reconnect manually."
349
- )
350
-
351
- async def call_tool(
352
- self, name: str, arguments: dict[str, Any], read_timeout_seconds: timedelta | None = None
353
- ) -> CallToolResult:
354
- """Call an MCP tool with automatic reconnection handling.
355
-
356
- Args:
357
- name: The name of the tool to call.
358
- arguments: The arguments to pass to the tool.
359
- read_timeout_seconds: timeout seconds when calling tool
360
-
361
- Returns:
362
- The result of the tool call.
363
-
364
- Raises:
365
- RuntimeError: If the connection is lost and cannot be reestablished.
366
- """
367
-
368
- # Ensure we're connected
369
- await self._ensure_connected()
370
-
371
- logger.debug(f"Calling tool '{name}' with arguments: {arguments}")
372
- try:
373
- result = await self.client_session.call_tool(name, arguments, read_timeout_seconds)
374
- logger.debug(f"Tool '{name}' called with result: {result}")
375
- return result
376
- except Exception as e:
377
- # Check if the error might be due to connection loss
378
- if not self.is_connected:
379
- raise RuntimeError(f"Tool call '{name}' failed due to connection loss: {e}") from e
380
- else:
381
- # Re-raise the original error if it's not connection-related
382
- raise
383
-
384
- async def list_tools(self) -> list[Tool]:
385
- """List all available tools from the MCP implementation."""
386
-
387
- if self.capabilities and not self.capabilities.tools:
388
- logger.debug(f"Server {self.public_identifier} does not support tools")
389
- return []
390
-
391
- # Ensure we're connected
392
- await self._ensure_connected()
393
-
394
- logger.debug("Listing tools")
395
- try:
396
- result = await self.client_session.list_tools()
397
- self._tools = result.tools
398
- return result.tools
399
- except McpError as e:
400
- logger.error(f"Error listing tools for connector {self.public_identifier}: {e}")
401
- return []
402
-
403
- async def list_resources(self) -> list[Resource]:
404
- """List all available resources from the MCP implementation."""
405
-
406
- if self.capabilities and not self.capabilities.resources:
407
- logger.debug(f"Server {self.public_identifier} does not support resources")
408
- return []
409
-
410
- # Ensure we're connected
411
- await self._ensure_connected()
412
-
413
- logger.debug("Listing resources")
414
- try:
415
- result = await self.client_session.list_resources()
416
- self._resources = result.resources
417
- return result.resources
418
- except McpError as e:
419
- logger.warning(f"Error listing resources for connector {self.public_identifier}: {e}")
420
- return []
421
-
422
- async def read_resource(self, uri: AnyUrl) -> ReadResourceResult:
423
- """Read a resource by URI."""
424
- await self._ensure_connected()
425
-
426
- logger.debug(f"Reading resource: {uri}")
427
- result = await self.client_session.read_resource(uri)
428
- return result
429
-
430
- async def list_prompts(self) -> list[Prompt]:
431
- """List all available prompts from the MCP implementation."""
432
-
433
- if self.capabilities and not self.capabilities.prompts:
434
- logger.debug(f"Server {self.public_identifier} does not support prompts")
435
- return []
436
-
437
- await self._ensure_connected()
438
-
439
- logger.debug("Listing prompts")
440
- try:
441
- result = await self.client_session.list_prompts()
442
- self._prompts = result.prompts
443
- return result.prompts
444
- except McpError as e:
445
- logger.error(f"Error listing prompts for connector {self.public_identifier}: {e}")
446
- return []
447
-
448
- async def get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult:
449
- """Get a prompt by name."""
450
- await self._ensure_connected()
451
-
452
- logger.debug(f"Getting prompt: {name}")
453
- result = await self.client_session.get_prompt(name, arguments)
454
- return result
8
+ warnings.warn(
9
+ "mcp_use.connectors.base is deprecated. "
10
+ "Use mcp_use.client.connectors.base. "
11
+ "This import will be removed in version 1.4.0",
12
+ DeprecationWarning,
13
+ stacklevel=2,
14
+ )
455
15
 
456
- async def request(self, method: str, params: dict[str, Any] | None = None) -> Any:
457
- """Send a raw request to the MCP implementation."""
458
- await self._ensure_connected()
459
16
 
460
- logger.debug(f"Sending request: {method} with params: {params}")
461
- return await self.client_session.request({"method": method, "params": params or {}})
17
+ @deprecated("Use mcp_use.client.connectors.base.BaseConnector")
18
+ class BaseConnector(_BaseConnector): ...