|
| 1 | +from typing import AsyncContextManager |
| 2 | + |
| 3 | +import fastmcp |
| 4 | + |
| 5 | +from enapter import async_, http |
| 6 | + |
| 7 | + |
| 8 | +class Server(async_.Routine): |
| 9 | + |
| 10 | + def __init__(self, host: str, port: int, http_api_base_url: str) -> None: |
| 11 | + super().__init__() |
| 12 | + self._host = host |
| 13 | + self._port = port |
| 14 | + self._http_api_base_url = http_api_base_url |
| 15 | + |
| 16 | + async def _run(self) -> None: |
| 17 | + mcp = fastmcp.FastMCP() |
| 18 | + self._register_tools(mcp) |
| 19 | + await mcp.run_async( |
| 20 | + transport="streamable-http", |
| 21 | + show_banner=False, |
| 22 | + host=self._host, |
| 23 | + port=self._port, |
| 24 | + ) |
| 25 | + |
| 26 | + def _register_tools(self, mcp: fastmcp.FastMCP) -> None: |
| 27 | + mcp.tool( |
| 28 | + self._list_sites, |
| 29 | + name="list_sites", |
| 30 | + description="List all sites to which the authenticated user has access.", |
| 31 | + ) |
| 32 | + mcp.tool( |
| 33 | + self._get_site, |
| 34 | + name="get_site", |
| 35 | + description="Get site by ID.", |
| 36 | + ) |
| 37 | + mcp.tool( |
| 38 | + self._list_devices, |
| 39 | + name="list_devices", |
| 40 | + description="List devices.", |
| 41 | + ) |
| 42 | + mcp.tool( |
| 43 | + self._get_device, |
| 44 | + name="get_device", |
| 45 | + description="Get device by ID.", |
| 46 | + ) |
| 47 | + |
| 48 | + async def _list_sites(self) -> list: |
| 49 | + async with self._new_http_api_client() as client: |
| 50 | + async with client.sites.list() as stream: |
| 51 | + return [site.to_dto() async for site in stream] |
| 52 | + |
| 53 | + async def _get_site(self, site_id: str) -> dict: |
| 54 | + async with self._new_http_api_client() as client: |
| 55 | + site = await client.sites.get(site_id) |
| 56 | + return site.to_dto() |
| 57 | + |
| 58 | + async def _list_devices( |
| 59 | + self, |
| 60 | + expand_manifest: bool = False, |
| 61 | + expand_properties: bool = False, |
| 62 | + expand_connectivity: bool = False, |
| 63 | + site_id: str | None = None, |
| 64 | + ) -> list: |
| 65 | + async with self._new_http_api_client() as client: |
| 66 | + async with client.devices.list( |
| 67 | + expand_manifest=expand_manifest, |
| 68 | + expand_properties=expand_properties, |
| 69 | + expand_connectivity=expand_connectivity, |
| 70 | + site_id=site_id, |
| 71 | + ) as stream: |
| 72 | + return [device.to_dto() async for device in stream] |
| 73 | + |
| 74 | + async def _get_device( |
| 75 | + self, |
| 76 | + device_id: str, |
| 77 | + expand_manifest: bool = False, |
| 78 | + expand_properties: bool = False, |
| 79 | + expand_connectivity: bool = False, |
| 80 | + ) -> dict: |
| 81 | + async with self._new_http_api_client() as client: |
| 82 | + device = await client.devices.get( |
| 83 | + device_id, |
| 84 | + expand_manifest=expand_manifest, |
| 85 | + expand_properties=expand_properties, |
| 86 | + expand_connectivity=expand_connectivity, |
| 87 | + ) |
| 88 | + return device.to_dto() |
| 89 | + |
| 90 | + def _new_http_api_client(self) -> AsyncContextManager[http.api.Client]: |
| 91 | + # FIXME: Client instance gets created for each request. |
| 92 | + headers = fastmcp.server.dependencies.get_http_headers() |
| 93 | + token = headers["x-enapter-auth-token"] |
| 94 | + return http.api.Client( |
| 95 | + config=http.api.Config(token=token, base_url=self._http_api_base_url) |
| 96 | + ) |
0 commit comments