Spaces:
Running
Running
fix format
Browse files- src/fastmcp/client/client.py +1 -1
- tests/client/test_client.py +11 -7
src/fastmcp/client/client.py
CHANGED
|
@@ -65,7 +65,7 @@ class Client:
|
|
| 65 |
@property
|
| 66 |
def session(self) -> ClientSession:
|
| 67 |
"""Get the current active session. Raises RuntimeError if not connected."""
|
| 68 |
-
if
|
| 69 |
raise RuntimeError(
|
| 70 |
"Client is not connected. Use 'async with client:' context manager first."
|
| 71 |
)
|
|
|
|
| 65 |
@property
|
| 66 |
def session(self) -> ClientSession:
|
| 67 |
"""Get the current active session. Raises RuntimeError if not connected."""
|
| 68 |
+
if self._session is None:
|
| 69 |
raise RuntimeError(
|
| 70 |
"Client is not connected. Use 'async with client:' context manager first."
|
| 71 |
)
|
tests/client/test_client.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
-
from typing import cast
|
| 2 |
-
from typing_extensions import Unpack
|
| 3 |
-
from collections.abc import AsyncIterator
|
| 4 |
-
from mcp import ClientSession
|
| 5 |
import contextlib
|
| 6 |
-
from
|
|
|
|
| 7 |
|
| 8 |
import pytest
|
|
|
|
|
|
|
| 9 |
from pydantic import AnyUrl
|
|
|
|
| 10 |
|
| 11 |
from fastmcp.client import Client
|
| 12 |
-
from fastmcp.client.transports import
|
| 13 |
from fastmcp.server.server import FastMCP
|
| 14 |
|
| 15 |
|
|
@@ -164,15 +164,18 @@ async def test_client_connection(fastmcp_server):
|
|
| 164 |
# After connection
|
| 165 |
assert not client.is_connected()
|
| 166 |
|
|
|
|
| 167 |
async def test_client_nested_context_manager(fastmcp_server):
|
| 168 |
"""Test that the client connects and disconnects once in nested context manager."""
|
|
|
|
| 169 |
class MockTransport(ClientTransport):
|
| 170 |
def __init__(self):
|
| 171 |
self._connected = False
|
| 172 |
|
| 173 |
@contextlib.asynccontextmanager
|
| 174 |
async def connect_session(
|
| 175 |
-
self,
|
|
|
|
| 176 |
) -> AsyncIterator[ClientSession]:
|
| 177 |
assert not self._connected, "Transport is connected multiple times"
|
| 178 |
self._connected = True
|
|
@@ -200,6 +203,7 @@ async def test_client_nested_context_manager(fastmcp_server):
|
|
| 200 |
# After connection
|
| 201 |
assert not client.is_connected()
|
| 202 |
|
|
|
|
| 203 |
async def test_resource_template(fastmcp_server):
|
| 204 |
"""Test using a resource template with InMemoryClient."""
|
| 205 |
client = Client(transport=FastMCPTransport(fastmcp_server))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import contextlib
|
| 2 |
+
from collections.abc import AsyncIterator
|
| 3 |
+
from typing import cast
|
| 4 |
|
| 5 |
import pytest
|
| 6 |
+
from mcp import ClientSession
|
| 7 |
+
from mcp.shared.memory import create_client_server_memory_streams
|
| 8 |
from pydantic import AnyUrl
|
| 9 |
+
from typing_extensions import Unpack
|
| 10 |
|
| 11 |
from fastmcp.client import Client
|
| 12 |
+
from fastmcp.client.transports import ClientTransport, FastMCPTransport, SessionKwargs
|
| 13 |
from fastmcp.server.server import FastMCP
|
| 14 |
|
| 15 |
|
|
|
|
| 164 |
# After connection
|
| 165 |
assert not client.is_connected()
|
| 166 |
|
| 167 |
+
|
| 168 |
async def test_client_nested_context_manager(fastmcp_server):
|
| 169 |
"""Test that the client connects and disconnects once in nested context manager."""
|
| 170 |
+
|
| 171 |
class MockTransport(ClientTransport):
|
| 172 |
def __init__(self):
|
| 173 |
self._connected = False
|
| 174 |
|
| 175 |
@contextlib.asynccontextmanager
|
| 176 |
async def connect_session(
|
| 177 |
+
self,
|
| 178 |
+
**session_kwargs: Unpack[SessionKwargs],
|
| 179 |
) -> AsyncIterator[ClientSession]:
|
| 180 |
assert not self._connected, "Transport is connected multiple times"
|
| 181 |
self._connected = True
|
|
|
|
| 203 |
# After connection
|
| 204 |
assert not client.is_connected()
|
| 205 |
|
| 206 |
+
|
| 207 |
async def test_resource_template(fastmcp_server):
|
| 208 |
"""Test using a resource template with InMemoryClient."""
|
| 209 |
client = Client(transport=FastMCPTransport(fastmcp_server))
|