Spaces:
Running
Running
fix
Browse files
src/fastmcp/client/client.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import datetime
|
| 2 |
from contextlib import AbstractAsyncContextManager
|
| 3 |
from pathlib import Path
|
| 4 |
-
from typing import Any, Literal, cast, overload
|
| 5 |
|
| 6 |
import mcp.types
|
| 7 |
from mcp import ClientSession
|
|
@@ -46,7 +46,7 @@ class Client:
|
|
| 46 |
self.transport = infer_transport(transport)
|
| 47 |
# stack to record nested context manager, None is pushed if reuse existing session
|
| 48 |
self._session_cms: list[
|
| 49 |
-
|
| 50 |
] = []
|
| 51 |
|
| 52 |
self._session_kwargs: SessionKwargs = {
|
|
@@ -70,7 +70,8 @@ class Client:
|
|
| 70 |
raise RuntimeError(
|
| 71 |
"Client is not connected. Use 'async with client:' context manager first."
|
| 72 |
)
|
| 73 |
-
self._session_cms[-1]
|
|
|
|
| 74 |
|
| 75 |
def set_roots(self, roots: RootsList | RootsHandler) -> None:
|
| 76 |
"""Set the roots for the client. This does not automatically call `send_roots_list_changed`."""
|
|
@@ -88,7 +89,7 @@ class Client:
|
|
| 88 |
|
| 89 |
async def __aenter__(self):
|
| 90 |
if self._session_cms:
|
| 91 |
-
# share the current session, push a None as
|
| 92 |
_, session = self._session_cms[-1]
|
| 93 |
self._session_cms.append((None, session))
|
| 94 |
else:
|
|
|
|
| 1 |
import datetime
|
| 2 |
from contextlib import AbstractAsyncContextManager
|
| 3 |
from pathlib import Path
|
| 4 |
+
from typing import Any, Literal, cast, overload, Tuple
|
| 5 |
|
| 6 |
import mcp.types
|
| 7 |
from mcp import ClientSession
|
|
|
|
| 46 |
self.transport = infer_transport(transport)
|
| 47 |
# stack to record nested context manager, None is pushed if reuse existing session
|
| 48 |
self._session_cms: list[
|
| 49 |
+
Tuple[AbstractAsyncContextManager[ClientSession] | None, ClientSession]
|
| 50 |
] = []
|
| 51 |
|
| 52 |
self._session_kwargs: SessionKwargs = {
|
|
|
|
| 70 |
raise RuntimeError(
|
| 71 |
"Client is not connected. Use 'async with client:' context manager first."
|
| 72 |
)
|
| 73 |
+
_, session = self._session_cms[-1]
|
| 74 |
+
return session
|
| 75 |
|
| 76 |
def set_roots(self, roots: RootsList | RootsHandler) -> None:
|
| 77 |
"""Set the roots for the client. This does not automatically call `send_roots_list_changed`."""
|
|
|
|
| 89 |
|
| 90 |
async def __aenter__(self):
|
| 91 |
if self._session_cms:
|
| 92 |
+
# share the current session, push a None as context manager to avoid close it in aexit
|
| 93 |
_, session = self._session_cms[-1]
|
| 94 |
self._session_cms.append((None, session))
|
| 95 |
else:
|