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