Update mcp/clients.py
Browse files- mcp/clients.py +7 -8
mcp/clients.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
# mcp/clients.py
|
2 |
-
```python
|
3 |
import os, httpx
|
4 |
from functools import lru_cache
|
5 |
from typing import Any, Dict
|
@@ -10,7 +9,7 @@ class APIError(Exception):
|
|
10 |
class BaseClient:
|
11 |
def __init__(self, base_url: str, api_key_env: str = None):
|
12 |
self.base = base_url.rstrip("/")
|
13 |
-
self.key
|
14 |
self._client = httpx.AsyncClient(timeout=10)
|
15 |
|
16 |
async def request(self, method: str, path: str, **kwargs) -> Any:
|
@@ -18,13 +17,13 @@ class BaseClient:
|
|
18 |
headers = kwargs.pop("headers", {})
|
19 |
if self.key:
|
20 |
headers["Authorization"] = f"Bearer {self.key}"
|
21 |
-
|
22 |
try:
|
23 |
-
|
24 |
except httpx.HTTPStatusError as e:
|
25 |
-
raise APIError(f"{
|
26 |
-
|
27 |
-
return
|
28 |
|
29 |
async def close(self):
|
30 |
-
await self._client.aclose()
|
|
|
1 |
# mcp/clients.py
|
|
|
2 |
import os, httpx
|
3 |
from functools import lru_cache
|
4 |
from typing import Any, Dict
|
|
|
9 |
class BaseClient:
|
10 |
def __init__(self, base_url: str, api_key_env: str = None):
|
11 |
self.base = base_url.rstrip("/")
|
12 |
+
self.key = os.getenv(api_key_env) if api_key_env else None
|
13 |
self._client = httpx.AsyncClient(timeout=10)
|
14 |
|
15 |
async def request(self, method: str, path: str, **kwargs) -> Any:
|
|
|
17 |
headers = kwargs.pop("headers", {})
|
18 |
if self.key:
|
19 |
headers["Authorization"] = f"Bearer {self.key}"
|
20 |
+
resp = await self._client.request(method, url, headers=headers, **kwargs)
|
21 |
try:
|
22 |
+
resp.raise_for_status()
|
23 |
except httpx.HTTPStatusError as e:
|
24 |
+
raise APIError(f"{resp.status_code} @ {url}") from e
|
25 |
+
ct = resp.headers.get("Content-Type", "")
|
26 |
+
return resp.json() if ct.startswith("application/json") else resp.text
|
27 |
|
28 |
async def close(self):
|
29 |
+
await self._client.aclose()
|