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.
- mcp_use/__init__.py +1 -1
- mcp_use/adapters/.deprecated +0 -0
- mcp_use/adapters/__init__.py +18 -7
- mcp_use/adapters/base.py +12 -185
- mcp_use/adapters/langchain_adapter.py +12 -219
- mcp_use/agents/adapters/__init__.py +10 -0
- mcp_use/agents/adapters/base.py +193 -0
- mcp_use/agents/adapters/langchain_adapter.py +228 -0
- mcp_use/agents/base.py +1 -1
- mcp_use/agents/managers/__init__.py +19 -0
- mcp_use/agents/managers/base.py +36 -0
- mcp_use/agents/managers/server_manager.py +131 -0
- mcp_use/agents/managers/tools/__init__.py +15 -0
- mcp_use/agents/managers/tools/base_tool.py +19 -0
- mcp_use/agents/managers/tools/connect_server.py +69 -0
- mcp_use/agents/managers/tools/disconnect_server.py +43 -0
- mcp_use/agents/managers/tools/get_active_server.py +29 -0
- mcp_use/agents/managers/tools/list_servers_tool.py +53 -0
- mcp_use/agents/managers/tools/search_tools.py +328 -0
- mcp_use/agents/mcpagent.py +16 -14
- mcp_use/agents/remote.py +14 -1
- mcp_use/auth/.deprecated +0 -0
- mcp_use/auth/__init__.py +19 -4
- mcp_use/auth/bearer.py +11 -12
- mcp_use/auth/oauth.py +11 -620
- mcp_use/auth/oauth_callback.py +16 -207
- mcp_use/client/__init__.py +1 -0
- mcp_use/client/auth/__init__.py +6 -0
- mcp_use/client/auth/bearer.py +23 -0
- mcp_use/client/auth/oauth.py +629 -0
- mcp_use/client/auth/oauth_callback.py +214 -0
- mcp_use/client/client.py +356 -0
- mcp_use/client/config.py +106 -0
- mcp_use/client/connectors/__init__.py +20 -0
- mcp_use/client/connectors/base.py +470 -0
- mcp_use/client/connectors/http.py +304 -0
- mcp_use/client/connectors/sandbox.py +332 -0
- mcp_use/client/connectors/stdio.py +109 -0
- mcp_use/client/connectors/utils.py +13 -0
- mcp_use/client/connectors/websocket.py +257 -0
- mcp_use/client/exceptions.py +31 -0
- mcp_use/client/middleware/__init__.py +50 -0
- mcp_use/client/middleware/logging.py +31 -0
- mcp_use/client/middleware/metrics.py +314 -0
- mcp_use/client/middleware/middleware.py +266 -0
- mcp_use/client/session.py +162 -0
- mcp_use/client/task_managers/__init__.py +20 -0
- mcp_use/client/task_managers/base.py +145 -0
- mcp_use/client/task_managers/sse.py +84 -0
- mcp_use/client/task_managers/stdio.py +69 -0
- mcp_use/client/task_managers/streamable_http.py +86 -0
- mcp_use/client/task_managers/websocket.py +68 -0
- mcp_use/client.py +12 -344
- mcp_use/config.py +20 -97
- mcp_use/connectors/.deprecated +0 -0
- mcp_use/connectors/__init__.py +46 -20
- mcp_use/connectors/base.py +12 -455
- mcp_use/connectors/http.py +13 -300
- mcp_use/connectors/sandbox.py +13 -306
- mcp_use/connectors/stdio.py +13 -104
- mcp_use/connectors/utils.py +15 -8
- mcp_use/connectors/websocket.py +13 -252
- mcp_use/exceptions.py +33 -18
- mcp_use/managers/.deprecated +0 -0
- mcp_use/managers/__init__.py +56 -17
- mcp_use/managers/base.py +13 -31
- mcp_use/managers/server_manager.py +13 -119
- mcp_use/managers/tools/__init__.py +45 -15
- mcp_use/managers/tools/base_tool.py +5 -16
- mcp_use/managers/tools/connect_server.py +5 -67
- mcp_use/managers/tools/disconnect_server.py +5 -41
- mcp_use/managers/tools/get_active_server.py +5 -26
- mcp_use/managers/tools/list_servers_tool.py +5 -51
- mcp_use/managers/tools/search_tools.py +17 -321
- mcp_use/middleware/.deprecated +0 -0
- mcp_use/middleware/__init__.py +89 -50
- mcp_use/middleware/logging.py +14 -26
- mcp_use/middleware/metrics.py +30 -303
- mcp_use/middleware/middleware.py +39 -246
- mcp_use/session.py +13 -149
- mcp_use/task_managers/.deprecated +0 -0
- mcp_use/task_managers/__init__.py +48 -20
- mcp_use/task_managers/base.py +13 -140
- mcp_use/task_managers/sse.py +13 -79
- mcp_use/task_managers/stdio.py +13 -64
- mcp_use/task_managers/streamable_http.py +15 -81
- mcp_use/task_managers/websocket.py +13 -63
- mcp_use/telemetry/events.py +58 -0
- mcp_use/telemetry/telemetry.py +71 -1
- mcp_use/types/.deprecated +0 -0
- mcp_use/types/sandbox.py +13 -18
- {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/METADATA +59 -34
- mcp_use-1.3.13.dist-info/RECORD +109 -0
- mcp_use-1.3.12.dist-info/RECORD +0 -64
- mcp_use-1.3.12.dist-info/licenses/LICENSE +0 -21
- /mcp_use/{observability → agents/observability}/__init__.py +0 -0
- /mcp_use/{observability → agents/observability}/callbacks_manager.py +0 -0
- /mcp_use/{observability → agents/observability}/laminar.py +0 -0
- /mcp_use/{observability → agents/observability}/langfuse.py +0 -0
- {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/WHEEL +0 -0
- {mcp_use-1.3.12.dist-info → mcp_use-1.3.13.dist-info}/entry_points.txt +0 -0
mcp_use/session.py
CHANGED
|
@@ -1,154 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# mcp_use/session.py
|
|
2
|
+
import warnings
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
which handles authentication, initialization, and tool discovery.
|
|
6
|
-
"""
|
|
4
|
+
from typing_extensions import deprecated
|
|
7
5
|
|
|
8
|
-
from
|
|
9
|
-
from typing import Any
|
|
6
|
+
from mcp_use.client.session import MCPSession as _MCPSession
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
warnings.warn(
|
|
9
|
+
"mcp_use.session.MCPSession is deprecated. "
|
|
10
|
+
"Use mcp_use.client.session.MCPSession. "
|
|
11
|
+
"This import will be removed in version 1.4.0",
|
|
12
|
+
DeprecationWarning,
|
|
13
|
+
stacklevel=2,
|
|
14
|
+
)
|
|
13
15
|
|
|
14
|
-
from .connectors.base import BaseConnector
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
class MCPSession:
|
|
18
|
-
"""Session manager for MCP connections.
|
|
19
|
-
|
|
20
|
-
This class manages the lifecycle of an MCP connection, including
|
|
21
|
-
authentication, initialization, and tool discovery.
|
|
22
|
-
"""
|
|
23
|
-
|
|
24
|
-
def __init__(
|
|
25
|
-
self,
|
|
26
|
-
connector: BaseConnector,
|
|
27
|
-
auto_connect: bool = True,
|
|
28
|
-
) -> None:
|
|
29
|
-
"""Initialize a new MCP session.
|
|
30
|
-
|
|
31
|
-
Args:
|
|
32
|
-
connector: The connector to use for communicating with the MCP implementation.
|
|
33
|
-
auto_connect: Whether to automatically connect to the MCP implementation.
|
|
34
|
-
"""
|
|
35
|
-
self.connector = connector
|
|
36
|
-
self.session_info: dict[str, Any] | None = None
|
|
37
|
-
self.auto_connect = auto_connect
|
|
38
|
-
|
|
39
|
-
async def __aenter__(self) -> "MCPSession":
|
|
40
|
-
"""Enter the async context manager.
|
|
41
|
-
|
|
42
|
-
Returns:
|
|
43
|
-
The session instance.
|
|
44
|
-
"""
|
|
45
|
-
await self.connect()
|
|
46
|
-
return self
|
|
47
|
-
|
|
48
|
-
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
49
|
-
"""Exit the async context manager.
|
|
50
|
-
|
|
51
|
-
Args:
|
|
52
|
-
exc_type: The exception type, if an exception was raised.
|
|
53
|
-
exc_val: The exception value, if an exception was raised.
|
|
54
|
-
exc_tb: The exception traceback, if an exception was raised.
|
|
55
|
-
"""
|
|
56
|
-
await self.disconnect()
|
|
57
|
-
|
|
58
|
-
async def connect(self) -> None:
|
|
59
|
-
"""Connect to the MCP implementation."""
|
|
60
|
-
await self.connector.connect()
|
|
61
|
-
|
|
62
|
-
async def disconnect(self) -> None:
|
|
63
|
-
"""Disconnect from the MCP implementation."""
|
|
64
|
-
await self.connector.disconnect()
|
|
65
|
-
|
|
66
|
-
async def initialize(self) -> dict[str, Any]:
|
|
67
|
-
"""Initialize the MCP session and discover available tools.
|
|
68
|
-
|
|
69
|
-
Returns:
|
|
70
|
-
The session information returned by the MCP implementation.
|
|
71
|
-
"""
|
|
72
|
-
# Make sure we're connected
|
|
73
|
-
if not self.is_connected and self.auto_connect:
|
|
74
|
-
await self.connect()
|
|
75
|
-
|
|
76
|
-
# Initialize the session
|
|
77
|
-
self.session_info = await self.connector.initialize()
|
|
78
|
-
|
|
79
|
-
return self.session_info
|
|
80
|
-
|
|
81
|
-
@property
|
|
82
|
-
def is_connected(self) -> bool:
|
|
83
|
-
"""Check if the connector is connected.
|
|
84
|
-
|
|
85
|
-
Returns:
|
|
86
|
-
True if the connector is connected, False otherwise.
|
|
87
|
-
"""
|
|
88
|
-
return self.connector.is_connected
|
|
89
|
-
|
|
90
|
-
# Convenience methods for MCP operations
|
|
91
|
-
async def call_tool(
|
|
92
|
-
self, name: str, arguments: dict[str, Any], read_timeout_seconds: timedelta | None = None
|
|
93
|
-
) -> CallToolResult:
|
|
94
|
-
"""Call an MCP tool.
|
|
95
|
-
|
|
96
|
-
Args:
|
|
97
|
-
name: The name of the tool to call.
|
|
98
|
-
arguments: The arguments to pass to the tool.
|
|
99
|
-
read_timeout_seconds: Optional timeout for the tool call.
|
|
100
|
-
|
|
101
|
-
Returns:
|
|
102
|
-
The result of the tool call.
|
|
103
|
-
|
|
104
|
-
Raises:
|
|
105
|
-
RuntimeError: If the connection is lost and cannot be reestablished.
|
|
106
|
-
"""
|
|
107
|
-
return await self.connector.call_tool(name, arguments, read_timeout_seconds)
|
|
108
|
-
|
|
109
|
-
async def list_tools(self) -> list[Tool]:
|
|
110
|
-
"""List all available tools from the MCP server.
|
|
111
|
-
|
|
112
|
-
Returns:
|
|
113
|
-
List of available tools.
|
|
114
|
-
"""
|
|
115
|
-
return await self.connector.list_tools()
|
|
116
|
-
|
|
117
|
-
async def list_resources(self) -> list[Resource]:
|
|
118
|
-
"""List all available resources from the MCP server.
|
|
119
|
-
|
|
120
|
-
Returns:
|
|
121
|
-
List of available resources.
|
|
122
|
-
"""
|
|
123
|
-
return await self.connector.list_resources()
|
|
124
|
-
|
|
125
|
-
async def read_resource(self, uri: AnyUrl) -> ReadResourceResult:
|
|
126
|
-
"""Read a resource by URI.
|
|
127
|
-
|
|
128
|
-
Args:
|
|
129
|
-
uri: The URI of the resource to read.
|
|
130
|
-
|
|
131
|
-
Returns:
|
|
132
|
-
The resource content.
|
|
133
|
-
"""
|
|
134
|
-
return await self.connector.read_resource(uri)
|
|
135
|
-
|
|
136
|
-
async def list_prompts(self) -> list[Prompt]:
|
|
137
|
-
"""List all available prompts from the MCP server.
|
|
138
|
-
|
|
139
|
-
Returns:
|
|
140
|
-
List of available prompts.
|
|
141
|
-
"""
|
|
142
|
-
return await self.connector.list_prompts()
|
|
143
|
-
|
|
144
|
-
async def get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult:
|
|
145
|
-
"""Get a prompt by name.
|
|
146
|
-
|
|
147
|
-
Args:
|
|
148
|
-
name: The name of the prompt to get.
|
|
149
|
-
arguments: Optional arguments for the prompt.
|
|
150
|
-
|
|
151
|
-
Returns:
|
|
152
|
-
The prompt result with messages.
|
|
153
|
-
"""
|
|
154
|
-
return await self.connector.get_prompt(name, arguments)
|
|
17
|
+
@deprecated("Use mcp_use.client.session.MCPSession")
|
|
18
|
+
class MCPSession(_MCPSession): ...
|
|
File without changes
|
|
@@ -1,20 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
from .
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
from .
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
# mcp_use/task_managers/__init__.py
|
|
2
|
+
import warnings
|
|
3
|
+
|
|
4
|
+
from typing_extensions import deprecated
|
|
5
|
+
|
|
6
|
+
from mcp_use.client.task_managers import (
|
|
7
|
+
ConnectionManager as _ConnectionManager,
|
|
8
|
+
)
|
|
9
|
+
from mcp_use.client.task_managers import (
|
|
10
|
+
SseConnectionManager as _SseConnectionManager,
|
|
11
|
+
)
|
|
12
|
+
from mcp_use.client.task_managers import (
|
|
13
|
+
StdioConnectionManager as _StdioConnectionManager,
|
|
14
|
+
)
|
|
15
|
+
from mcp_use.client.task_managers import (
|
|
16
|
+
StreamableHttpConnectionManager as _StreamableHttpConnectionManager,
|
|
17
|
+
)
|
|
18
|
+
from mcp_use.client.task_managers import (
|
|
19
|
+
WebSocketConnectionManager as _WebSocketConnectionManager,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
warnings.warn(
|
|
23
|
+
"mcp_use.task_managers is deprecated. "
|
|
24
|
+
"Use mcp_use.client.task_managers. "
|
|
25
|
+
"This import will be removed in version 1.4.0",
|
|
26
|
+
DeprecationWarning,
|
|
27
|
+
stacklevel=2,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@deprecated("Use mcp_use.client.task_managers.ConnectionManager")
|
|
32
|
+
class ConnectionManager(_ConnectionManager): ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@deprecated("Use mcp_use.client.task_managers.StdioConnectionManager")
|
|
36
|
+
class StdioConnectionManager(_StdioConnectionManager): ...
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@deprecated("Use mcp_use.client.task_managers.WebSocketConnectionManager")
|
|
40
|
+
class WebSocketConnectionManager(_WebSocketConnectionManager): ...
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@deprecated("Use mcp_use.client.task_managers.SseConnectionManager")
|
|
44
|
+
class SseConnectionManager(_SseConnectionManager): ...
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@deprecated("Use mcp_use.client.task_managers.StreamableHttpConnectionManager")
|
|
48
|
+
class StreamableHttpConnectionManager(_StreamableHttpConnectionManager): ...
|
mcp_use/task_managers/base.py
CHANGED
|
@@ -1,145 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# mcp_use/task_managers/base.py
|
|
2
|
+
import warnings
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
managers used in MCP connectors.
|
|
6
|
-
"""
|
|
4
|
+
from typing_extensions import deprecated
|
|
7
5
|
|
|
8
|
-
import
|
|
9
|
-
from abc import ABC, abstractmethod
|
|
10
|
-
from typing import Generic, TypeVar
|
|
6
|
+
from mcp_use.client.task_managers.base import ConnectionManager as _ConnectionManager
|
|
11
7
|
|
|
12
|
-
|
|
8
|
+
warnings.warn(
|
|
9
|
+
"mcp_use.task_managers.base is deprecated. "
|
|
10
|
+
"Use mcp_use.client.task_managers.base. "
|
|
11
|
+
"This import will be removed in version 1.4.0",
|
|
12
|
+
DeprecationWarning,
|
|
13
|
+
stacklevel=2,
|
|
14
|
+
)
|
|
13
15
|
|
|
14
|
-
# Type variable for connection types
|
|
15
|
-
T = TypeVar("T")
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
class ConnectionManager(
|
|
19
|
-
"""Abstract base class for connection managers.
|
|
20
|
-
|
|
21
|
-
This class defines the interface for different types of connection managers
|
|
22
|
-
used with MCP connectors.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
def __init__(self) -> None:
|
|
26
|
-
"""Initialize a new connection manager."""
|
|
27
|
-
self._ready_event = asyncio.Event()
|
|
28
|
-
self._done_event = asyncio.Event()
|
|
29
|
-
self._stop_event = asyncio.Event()
|
|
30
|
-
self._exception: Exception | None = None
|
|
31
|
-
self._connection: T | None = None
|
|
32
|
-
self._task: asyncio.Task[None] | None = None
|
|
33
|
-
|
|
34
|
-
@abstractmethod
|
|
35
|
-
async def _establish_connection(self) -> T:
|
|
36
|
-
"""Establish the connection.
|
|
37
|
-
|
|
38
|
-
This method should be implemented by subclasses to establish
|
|
39
|
-
the specific type of connection needed.
|
|
40
|
-
|
|
41
|
-
Returns:
|
|
42
|
-
The established connection.
|
|
43
|
-
|
|
44
|
-
Raises:
|
|
45
|
-
Exception: If connection cannot be established.
|
|
46
|
-
"""
|
|
47
|
-
pass
|
|
48
|
-
|
|
49
|
-
@abstractmethod
|
|
50
|
-
async def _close_connection(self) -> None:
|
|
51
|
-
"""Close the connection.
|
|
52
|
-
|
|
53
|
-
This method should be implemented by subclasses to close
|
|
54
|
-
the specific type of connection.
|
|
55
|
-
|
|
56
|
-
"""
|
|
57
|
-
pass
|
|
58
|
-
|
|
59
|
-
async def start(self) -> T:
|
|
60
|
-
"""Start the connection manager and establish a connection.
|
|
61
|
-
|
|
62
|
-
Returns:
|
|
63
|
-
The established connection.
|
|
64
|
-
|
|
65
|
-
Raises:
|
|
66
|
-
Exception: If connection cannot be established.
|
|
67
|
-
"""
|
|
68
|
-
# Reset state
|
|
69
|
-
self._ready_event.clear()
|
|
70
|
-
self._done_event.clear()
|
|
71
|
-
self._exception = None
|
|
72
|
-
|
|
73
|
-
# Create a task to establish and maintain the connection
|
|
74
|
-
self._task = asyncio.create_task(self._connection_task(), name=f"{self.__class__.__name__}_task")
|
|
75
|
-
|
|
76
|
-
# Wait for the connection to be ready or fail
|
|
77
|
-
await self._ready_event.wait()
|
|
78
|
-
|
|
79
|
-
# If there was an exception, raise it
|
|
80
|
-
if self._exception:
|
|
81
|
-
raise self._exception
|
|
82
|
-
|
|
83
|
-
# Return the connection
|
|
84
|
-
if self._connection is None:
|
|
85
|
-
raise RuntimeError("Connection was not established")
|
|
86
|
-
return self._connection
|
|
87
|
-
|
|
88
|
-
async def stop(self) -> None:
|
|
89
|
-
"""Stop the connection manager and close the connection."""
|
|
90
|
-
# Signal stop to the connection task instead of cancelling it, avoids
|
|
91
|
-
# propagating CancelledError to unrelated tasks.
|
|
92
|
-
if self._task and not self._task.done():
|
|
93
|
-
logger.debug(f"Signaling stop to {self.__class__.__name__} task")
|
|
94
|
-
self._stop_event.set()
|
|
95
|
-
# Wait for it to finish gracefully
|
|
96
|
-
await self._task
|
|
97
|
-
|
|
98
|
-
# Ensure cleanup completed
|
|
99
|
-
await self._done_event.wait()
|
|
100
|
-
logger.debug(f"{self.__class__.__name__} task completed")
|
|
101
|
-
|
|
102
|
-
def get_streams(self) -> T | None:
|
|
103
|
-
"""Get the current connection streams.
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
The current connection (typically a tuple of read_stream, write_stream) or None if not connected.
|
|
107
|
-
"""
|
|
108
|
-
return self._connection
|
|
109
|
-
|
|
110
|
-
async def _connection_task(self) -> None:
|
|
111
|
-
"""Run the connection task.
|
|
112
|
-
|
|
113
|
-
This task establishes and maintains the connection until cancelled.
|
|
114
|
-
"""
|
|
115
|
-
logger.debug(f"Starting {self.__class__.__name__} task")
|
|
116
|
-
try:
|
|
117
|
-
# Establish the connection
|
|
118
|
-
self._connection = await self._establish_connection()
|
|
119
|
-
logger.debug(f"{self.__class__.__name__} connected successfully")
|
|
120
|
-
|
|
121
|
-
# Signal that the connection is ready
|
|
122
|
-
self._ready_event.set()
|
|
123
|
-
|
|
124
|
-
# Wait until stop is requested
|
|
125
|
-
await self._stop_event.wait()
|
|
126
|
-
|
|
127
|
-
except Exception as e:
|
|
128
|
-
# Store the exception
|
|
129
|
-
self._exception = e
|
|
130
|
-
logger.error(f"Error in {self.__class__.__name__} task: {e}")
|
|
131
|
-
|
|
132
|
-
# Signal that the connection is ready (with error)
|
|
133
|
-
self._ready_event.set()
|
|
134
|
-
|
|
135
|
-
finally:
|
|
136
|
-
# Close the connection if it was established
|
|
137
|
-
if self._connection is not None:
|
|
138
|
-
try:
|
|
139
|
-
await self._close_connection()
|
|
140
|
-
except Exception as e:
|
|
141
|
-
logger.warning(f"Error closing connection in {self.__class__.__name__}: {e}")
|
|
142
|
-
self._connection = None
|
|
143
|
-
|
|
144
|
-
# Signal that the connection is done
|
|
145
|
-
self._done_event.set()
|
|
17
|
+
@deprecated("Use mcp_use.client.task_managers.base.ConnectionManager")
|
|
18
|
+
class ConnectionManager(_ConnectionManager): ...
|
mcp_use/task_managers/sse.py
CHANGED
|
@@ -1,84 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# mcp_use/task_managers/sse.py
|
|
2
|
+
import warnings
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
that ensures proper task isolation and resource cleanup.
|
|
6
|
-
"""
|
|
4
|
+
from typing_extensions import deprecated
|
|
7
5
|
|
|
8
|
-
from
|
|
6
|
+
from mcp_use.client.task_managers.sse import SseConnectionManager as _SseConnectionManager
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
warnings.warn(
|
|
9
|
+
"mcp_use.task_managers.sse is deprecated. "
|
|
10
|
+
"Use mcp_use.client.task_managers.sse. "
|
|
11
|
+
"This import will be removed in version 1.4.0",
|
|
12
|
+
DeprecationWarning,
|
|
13
|
+
stacklevel=2,
|
|
14
|
+
)
|
|
12
15
|
|
|
13
|
-
from ..logging import logger
|
|
14
|
-
from .base import ConnectionManager
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
class SseConnectionManager(
|
|
18
|
-
"""Connection manager for SSE-based MCP connections.
|
|
19
|
-
|
|
20
|
-
This class handles the proper task isolation for sse_client context managers
|
|
21
|
-
to prevent the "cancel scope in different task" error. It runs the sse_client
|
|
22
|
-
in a dedicated task and manages its lifecycle.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
def __init__(
|
|
26
|
-
self,
|
|
27
|
-
url: str,
|
|
28
|
-
headers: dict[str, str] | None = None,
|
|
29
|
-
timeout: float = 5,
|
|
30
|
-
sse_read_timeout: float = 60 * 5,
|
|
31
|
-
auth: httpx.Auth | None = None,
|
|
32
|
-
):
|
|
33
|
-
"""Initialize a new SSE connection manager.
|
|
34
|
-
|
|
35
|
-
Args:
|
|
36
|
-
url: The SSE endpoint URL
|
|
37
|
-
headers: Optional HTTP headers
|
|
38
|
-
timeout: Timeout for HTTP operations in seconds
|
|
39
|
-
sse_read_timeout: Timeout for SSE read operations in seconds
|
|
40
|
-
auth: Optional httpx.Auth instance for authentication
|
|
41
|
-
"""
|
|
42
|
-
super().__init__()
|
|
43
|
-
self.url = url
|
|
44
|
-
self.headers = headers or {}
|
|
45
|
-
self.timeout = timeout
|
|
46
|
-
self.sse_read_timeout = sse_read_timeout
|
|
47
|
-
self.auth = auth
|
|
48
|
-
self._sse_ctx = None
|
|
49
|
-
|
|
50
|
-
async def _establish_connection(self) -> tuple[Any, Any]:
|
|
51
|
-
"""Establish an SSE connection.
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
A tuple of (read_stream, write_stream)
|
|
55
|
-
|
|
56
|
-
Raises:
|
|
57
|
-
Exception: If connection cannot be established.
|
|
58
|
-
"""
|
|
59
|
-
# Create the context manager
|
|
60
|
-
self._sse_ctx = sse_client(
|
|
61
|
-
url=self.url,
|
|
62
|
-
headers=self.headers,
|
|
63
|
-
timeout=self.timeout,
|
|
64
|
-
sse_read_timeout=self.sse_read_timeout,
|
|
65
|
-
auth=self.auth,
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
# Enter the context manager
|
|
69
|
-
read_stream, write_stream = await self._sse_ctx.__aenter__()
|
|
70
|
-
|
|
71
|
-
# Return the streams
|
|
72
|
-
return (read_stream, write_stream)
|
|
73
|
-
|
|
74
|
-
async def _close_connection(self) -> None:
|
|
75
|
-
"""Close the SSE connection."""
|
|
76
|
-
|
|
77
|
-
if self._sse_ctx:
|
|
78
|
-
# Exit the context manager
|
|
79
|
-
try:
|
|
80
|
-
await self._sse_ctx.__aexit__(None, None, None)
|
|
81
|
-
except Exception as e:
|
|
82
|
-
logger.warning(f"Error closing SSE context: {e}")
|
|
83
|
-
finally:
|
|
84
|
-
self._sse_ctx = None
|
|
17
|
+
@deprecated("Use mcp_use.client.task_managers.sse.SseConnectionManager")
|
|
18
|
+
class SseConnectionManager(_SseConnectionManager): ...
|
mcp_use/task_managers/stdio.py
CHANGED
|
@@ -1,69 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# mcp_use/task_managers/stdio.py
|
|
2
|
+
import warnings
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
that ensures proper task isolation and resource cleanup.
|
|
6
|
-
"""
|
|
4
|
+
from typing_extensions import deprecated
|
|
7
5
|
|
|
8
|
-
import
|
|
9
|
-
from typing import Any, TextIO
|
|
6
|
+
from mcp_use.client.task_managers.stdio import StdioConnectionManager as _StdioConnectionManager
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
warnings.warn(
|
|
9
|
+
"mcp_use.task_managers.stdio is deprecated. "
|
|
10
|
+
"Use mcp_use.client.task_managers.stdio. "
|
|
11
|
+
"This import will be removed in version 1.4.0",
|
|
12
|
+
DeprecationWarning,
|
|
13
|
+
stacklevel=2,
|
|
14
|
+
)
|
|
13
15
|
|
|
14
|
-
from ..logging import logger
|
|
15
|
-
from .base import ConnectionManager
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
class StdioConnectionManager(
|
|
19
|
-
"""Connection manager for stdio-based MCP connections.
|
|
20
|
-
|
|
21
|
-
This class handles the proper task isolation for stdio_client context managers
|
|
22
|
-
to prevent the "cancel scope in different task" error. It runs the stdio_client
|
|
23
|
-
in a dedicated task and manages its lifecycle.
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
def __init__(
|
|
27
|
-
self,
|
|
28
|
-
server_params: StdioServerParameters,
|
|
29
|
-
errlog: TextIO = sys.stderr,
|
|
30
|
-
):
|
|
31
|
-
"""Initialize a new stdio connection manager.
|
|
32
|
-
|
|
33
|
-
Args:
|
|
34
|
-
server_params: The parameters for the stdio server
|
|
35
|
-
errlog: The error log stream
|
|
36
|
-
"""
|
|
37
|
-
super().__init__()
|
|
38
|
-
self.server_params = server_params
|
|
39
|
-
self.errlog = errlog
|
|
40
|
-
self._stdio_ctx = None
|
|
41
|
-
|
|
42
|
-
async def _establish_connection(self) -> tuple[Any, Any]:
|
|
43
|
-
"""Establish a stdio connection.
|
|
44
|
-
|
|
45
|
-
Returns:
|
|
46
|
-
A tuple of (read_stream, write_stream)
|
|
47
|
-
|
|
48
|
-
Raises:
|
|
49
|
-
Exception: If connection cannot be established.
|
|
50
|
-
"""
|
|
51
|
-
# Create the context manager
|
|
52
|
-
self._stdio_ctx = stdio_client(self.server_params, self.errlog)
|
|
53
|
-
|
|
54
|
-
# Enter the context manager
|
|
55
|
-
read_stream, write_stream = await self._stdio_ctx.__aenter__()
|
|
56
|
-
|
|
57
|
-
# Return the streams
|
|
58
|
-
return (read_stream, write_stream)
|
|
59
|
-
|
|
60
|
-
async def _close_connection(self) -> None:
|
|
61
|
-
"""Close the stdio connection."""
|
|
62
|
-
if self._stdio_ctx:
|
|
63
|
-
# Exit the context manager
|
|
64
|
-
try:
|
|
65
|
-
await self._stdio_ctx.__aexit__(None, None, None)
|
|
66
|
-
except Exception as e:
|
|
67
|
-
logger.warning(f"Error closing stdio context: {e}")
|
|
68
|
-
finally:
|
|
69
|
-
self._stdio_ctx = None
|
|
17
|
+
@deprecated("Use mcp_use.client.task_managers.stdio.StdioConnectionManager")
|
|
18
|
+
class StdioConnectionManager(_StdioConnectionManager): ...
|
|
@@ -1,86 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# mcp_use/task_managers/streamable_http.py
|
|
2
|
+
import warnings
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
that ensures proper task isolation and resource cleanup.
|
|
6
|
-
"""
|
|
4
|
+
from typing_extensions import deprecated
|
|
7
5
|
|
|
8
|
-
from
|
|
9
|
-
|
|
6
|
+
from mcp_use.client.task_managers.streamable_http import (
|
|
7
|
+
StreamableHttpConnectionManager as _StreamableHttpConnectionManager,
|
|
8
|
+
)
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
warnings.warn(
|
|
11
|
+
"mcp_use.task_managers.streamable_http is deprecated. "
|
|
12
|
+
"Use mcp_use.client.task_managers.streamable_http. "
|
|
13
|
+
"This import will be removed in version 1.4.0",
|
|
14
|
+
DeprecationWarning,
|
|
15
|
+
stacklevel=2,
|
|
16
|
+
)
|
|
13
17
|
|
|
14
|
-
from ..logging import logger
|
|
15
|
-
from .base import ConnectionManager
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
class StreamableHttpConnectionManager(
|
|
19
|
-
"""Connection manager for streamable HTTP-based MCP connections.
|
|
20
|
-
|
|
21
|
-
This class handles the proper task isolation for HTTP streaming connections
|
|
22
|
-
to prevent the "cancel scope in different task" error. It runs the http_stream_client
|
|
23
|
-
in a dedicated task and manages its lifecycle.
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
def __init__(
|
|
27
|
-
self,
|
|
28
|
-
url: str,
|
|
29
|
-
headers: dict[str, str] | None = None,
|
|
30
|
-
timeout: float = 5,
|
|
31
|
-
read_timeout: float = 60 * 5,
|
|
32
|
-
auth: httpx.Auth | None = None,
|
|
33
|
-
):
|
|
34
|
-
"""Initialize a new streamable HTTP connection manager.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
url: The HTTP endpoint URL
|
|
38
|
-
headers: Optional HTTP headers
|
|
39
|
-
timeout: Timeout for HTTP operations in seconds
|
|
40
|
-
read_timeout: Timeout for HTTP read operations in seconds
|
|
41
|
-
auth: Optional httpx.Auth instance for authentication
|
|
42
|
-
"""
|
|
43
|
-
super().__init__()
|
|
44
|
-
self.url = url
|
|
45
|
-
self.headers = headers or {}
|
|
46
|
-
self.timeout = timedelta(seconds=timeout)
|
|
47
|
-
self.read_timeout = timedelta(seconds=read_timeout)
|
|
48
|
-
self.auth = auth
|
|
49
|
-
self._http_ctx = None
|
|
50
|
-
|
|
51
|
-
async def _establish_connection(self) -> tuple[Any, Any]:
|
|
52
|
-
"""Establish a streamable HTTP connection.
|
|
53
|
-
|
|
54
|
-
Returns:
|
|
55
|
-
A tuple of (read_stream, write_stream)
|
|
56
|
-
|
|
57
|
-
Raises:
|
|
58
|
-
Exception: If connection cannot be established.
|
|
59
|
-
"""
|
|
60
|
-
# Create the context manager
|
|
61
|
-
self._http_ctx = streamablehttp_client(
|
|
62
|
-
url=self.url,
|
|
63
|
-
headers=self.headers,
|
|
64
|
-
timeout=self.timeout,
|
|
65
|
-
sse_read_timeout=self.read_timeout,
|
|
66
|
-
auth=self.auth,
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
# Enter the context manager. Ignoring the session id callback
|
|
70
|
-
read_stream, write_stream, _ = await self._http_ctx.__aenter__()
|
|
71
|
-
|
|
72
|
-
# Return the streams
|
|
73
|
-
return (read_stream, write_stream)
|
|
74
|
-
|
|
75
|
-
async def _close_connection(self) -> None:
|
|
76
|
-
"""Close the streamable HTTP connection."""
|
|
77
|
-
|
|
78
|
-
if self._http_ctx:
|
|
79
|
-
# Exit the context manager
|
|
80
|
-
try:
|
|
81
|
-
await self._http_ctx.__aexit__(None, None, None)
|
|
82
|
-
except Exception as e:
|
|
83
|
-
# Only log if it's not a normal connection termination
|
|
84
|
-
logger.debug(f"Streamable HTTP context cleanup: {e}")
|
|
85
|
-
finally:
|
|
86
|
-
self._http_ctx = None
|
|
19
|
+
@deprecated("Use mcp_use.client.task_managers.streamable_http.StreamableHttpConnectionManager")
|
|
20
|
+
class StreamableHttpConnectionManager(_StreamableHttpConnectionManager): ...
|