Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
9bb4597
1
Parent(s): b31d42f
add in memory client
Browse files
src/fastmcp/client/__init__.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
from .websocket import WebSocketClient
|
| 2 |
from .sse import SSEClient
|
| 3 |
-
from .stdio import StdioClient
|
|
|
|
| 4 |
|
| 5 |
-
__all__ = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from .websocket import WebSocketClient
|
| 2 |
from .sse import SSEClient
|
| 3 |
+
from .stdio import StdioClient, UvxClient
|
| 4 |
+
from .memory import InMemoryClient
|
| 5 |
|
| 6 |
+
__all__ = [
|
| 7 |
+
"StdioClient",
|
| 8 |
+
"SSEClient",
|
| 9 |
+
"WebSocketClient",
|
| 10 |
+
"UvxClient",
|
| 11 |
+
"InMemoryClient",
|
| 12 |
+
]
|
src/fastmcp/client/memory.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextlib
|
| 2 |
+
from typing import Any, TypeVar
|
| 3 |
+
|
| 4 |
+
from mcp.server import Server
|
| 5 |
+
from mcp.shared.memory import create_connected_server_and_client_session
|
| 6 |
+
from typing_extensions import Unpack
|
| 7 |
+
|
| 8 |
+
from fastmcp.client.base import BaseClient, ClientKwargs
|
| 9 |
+
|
| 10 |
+
T = TypeVar("T")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class InMemoryClient(BaseClient):
|
| 14 |
+
"""Client that connects to an in-memory MCP server.
|
| 15 |
+
|
| 16 |
+
This client creates and manages an in-memory connection to a server,
|
| 17 |
+
without using any external processes or network connections.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
server: Server[Any],
|
| 23 |
+
raise_exceptions: bool = False,
|
| 24 |
+
**kwargs: Unpack[ClientKwargs],
|
| 25 |
+
):
|
| 26 |
+
"""Initialize an InMemoryClient that connects to an in-memory MCP server.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
server: The MCP server instance to connect to
|
| 30 |
+
raise_exceptions: Whether to raise exceptions from the server
|
| 31 |
+
**kwargs: Additional arguments for BaseClient
|
| 32 |
+
"""
|
| 33 |
+
super().__init__(**kwargs)
|
| 34 |
+
self.server = server
|
| 35 |
+
self.raise_exceptions = raise_exceptions
|
| 36 |
+
self._cm_session = None
|
| 37 |
+
|
| 38 |
+
@contextlib.asynccontextmanager
|
| 39 |
+
async def _connect(self):
|
| 40 |
+
"""Set up in-memory connection and session"""
|
| 41 |
+
self._cm_session = create_connected_server_and_client_session(
|
| 42 |
+
server=self.server,
|
| 43 |
+
read_timeout_seconds=self._read_timeout_seconds,
|
| 44 |
+
sampling_callback=self._sampling_callback,
|
| 45 |
+
list_roots_callback=self._list_roots_callback,
|
| 46 |
+
logging_callback=self._logging_callback,
|
| 47 |
+
message_handler=self._message_handler,
|
| 48 |
+
raise_exceptions=self.raise_exceptions,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
async with self._cm_session as session:
|
| 52 |
+
# No need to call initialize as create_connected_server_and_client_session already does
|
| 53 |
+
async with self._set_session((None, None), session):
|
| 54 |
+
yield self
|