evalstate HF Staff commited on
Commit
14c441f
·
verified ·
1 Parent(s): 64bec9c

Update hf_api_tool.py with token passthrough support

Browse files
Files changed (1) hide show
  1. hf_api_tool.py +20 -4
hf_api_tool.py CHANGED
@@ -13,10 +13,24 @@ DEFAULT_TIMEOUT_SEC = 30
13
 
14
 
15
  def _load_token() -> str | None:
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  token = os.getenv("HF_TOKEN")
17
  if token:
18
  return token
19
 
 
20
  token_path = Path.home() / ".cache" / "huggingface" / "token"
21
  if token_path.exists():
22
  token_value = token_path.read_text(encoding="utf-8").strip()
@@ -76,6 +90,7 @@ def hf_api_request(
76
  params: dict[str, Any] | None = None,
77
  json_body: dict[str, Any] | None = None,
78
  max_results: int | None = None,
 
79
  ) -> dict[str, Any]:
80
  """
81
  Call the Hugging Face Hub API (GET/POST only).
@@ -86,6 +101,7 @@ def hf_api_request(
86
  params: Optional query parameters.
87
  json_body: Optional JSON payload for POST requests.
88
  max_results: Max results when response is a list (defaults to HF_MAX_RESULTS).
 
89
 
90
  Returns:
91
  A dict with the response data and request metadata.
@@ -119,9 +135,7 @@ def hf_api_request(
119
  status_code = response.status
120
  except HTTPError as exc:
121
  error_body = exc.read().decode("utf-8", errors="replace")
122
- raise RuntimeError(
123
- f"HF API error {exc.code} for {url}: {error_body}"
124
- ) from exc
125
  except URLError as exc:
126
  raise RuntimeError(f"HF API request failed for {url}: {exc}") from exc
127
 
@@ -132,7 +146,9 @@ def hf_api_request(
132
 
133
  if isinstance(payload, list):
134
  limit = max_results if max_results is not None else _max_results_from_env()
135
- payload = payload[: max(limit, 0)]
 
 
136
 
137
  return {
138
  "url": url,
 
13
 
14
 
15
  def _load_token() -> str | None:
16
+ # Check for request-scoped token first (when running as MCP server)
17
+ # This allows clients to pass their own HF token via Authorization header
18
+ try:
19
+ from fast_agent.mcp.auth.context import request_bearer_token
20
+
21
+ ctx_token = request_bearer_token.get()
22
+ if ctx_token:
23
+ return ctx_token
24
+ except ImportError:
25
+ # fast_agent.mcp.auth.context not available
26
+ pass
27
+
28
+ # Fall back to HF_TOKEN environment variable
29
  token = os.getenv("HF_TOKEN")
30
  if token:
31
  return token
32
 
33
+ # Fall back to cached huggingface token file
34
  token_path = Path.home() / ".cache" / "huggingface" / "token"
35
  if token_path.exists():
36
  token_value = token_path.read_text(encoding="utf-8").strip()
 
90
  params: dict[str, Any] | None = None,
91
  json_body: dict[str, Any] | None = None,
92
  max_results: int | None = None,
93
+ offset: int | None = None,
94
  ) -> dict[str, Any]:
95
  """
96
  Call the Hugging Face Hub API (GET/POST only).
 
101
  params: Optional query parameters.
102
  json_body: Optional JSON payload for POST requests.
103
  max_results: Max results when response is a list (defaults to HF_MAX_RESULTS).
104
+ offset: Client-side offset when response is a list (defaults to 0).
105
 
106
  Returns:
107
  A dict with the response data and request metadata.
 
135
  status_code = response.status
136
  except HTTPError as exc:
137
  error_body = exc.read().decode("utf-8", errors="replace")
138
+ raise RuntimeError(f"HF API error {exc.code} for {url}: {error_body}") from exc
 
 
139
  except URLError as exc:
140
  raise RuntimeError(f"HF API request failed for {url}: {exc}") from exc
141
 
 
146
 
147
  if isinstance(payload, list):
148
  limit = max_results if max_results is not None else _max_results_from_env()
149
+ start = max(offset or 0, 0)
150
+ end = start + max(limit, 0)
151
+ payload = payload[start:end]
152
 
153
  return {
154
  "url": url,