-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathfastmcp.py
55 lines (43 loc) · 1.46 KB
/
fastmcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pathlib import Path
from typing import Any
from fastmcp.client import Client, ClientTransport
from fastmcp.server import FastMCP
from pydantic import AnyUrl
from mcp.types import CallToolResult, Tool
from ..mcp import MCPServer
class FastMCPServer(MCPServer):
"""
Support fastmcp transport implementations, include in-memory fastmcp servers.
"""
def __init__(
self,
transport: ClientTransport | FastMCP | AnyUrl | Path | str,
name: str | None = None,
):
self._client = Client(transport)
if not name:
if isinstance(transport, FastMCP):
name = transport.name
else:
name = str(transport)
self._name = name
async def connect(self):
await self._client.__aenter__()
async def cleanup(self):
await self._client.__aexit__(None, None, None)
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, exc_type, exc_value, traceback):
await self._client.__aexit__(exc_type, exc_value, traceback)
@property
def name(self) -> str:
return self._name
async def list_tools(self) -> list[Tool]:
return await self._client.list_tools()
async def call_tool(
self, tool_name: str, arguments: dict[str, Any] | None
) -> CallToolResult:
return await self._client.call_tool(
tool_name, arguments, _return_raw_result=True
)