faved-mcp 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- faved_mcp/__init__.py +3 -0
- faved_mcp/__main__.py +92 -0
- faved_mcp/client.py +306 -0
- faved_mcp/config.py +66 -0
- faved_mcp/server.py +827 -0
- faved_mcp/setup_cli.py +86 -0
- faved_mcp-0.1.0.dist-info/METADATA +146 -0
- faved_mcp-0.1.0.dist-info/RECORD +12 -0
- faved_mcp-0.1.0.dist-info/WHEEL +5 -0
- faved_mcp-0.1.0.dist-info/entry_points.txt +2 -0
- faved_mcp-0.1.0.dist-info/licenses/LICENSE +21 -0
- faved_mcp-0.1.0.dist-info/top_level.txt +1 -0
faved_mcp/__init__.py
ADDED
faved_mcp/__main__.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Console entry point: setup wizard, stdio for local clients, streamable HTTP for containers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import asyncio
|
|
7
|
+
import hmac
|
|
8
|
+
import sys
|
|
9
|
+
from dataclasses import replace
|
|
10
|
+
|
|
11
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
12
|
+
from starlette.responses import JSONResponse
|
|
13
|
+
|
|
14
|
+
from . import __version__
|
|
15
|
+
from .config import Config, config_path
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BearerAuth(BaseHTTPMiddleware):
|
|
19
|
+
def __init__(self, app, token: str):
|
|
20
|
+
super().__init__(app)
|
|
21
|
+
self.token = token
|
|
22
|
+
|
|
23
|
+
async def dispatch(self, request, call_next):
|
|
24
|
+
if request.url.path == "/health":
|
|
25
|
+
return JSONResponse({"ok": True})
|
|
26
|
+
header = request.headers.get("authorization", "")
|
|
27
|
+
expected = f"Bearer {self.token}"
|
|
28
|
+
if not hmac.compare_digest(header, expected):
|
|
29
|
+
return JSONResponse({"ok": False, "code": "unauthorized", "message": "Bearer token required"}, status_code=401)
|
|
30
|
+
return await call_next(request)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _parser() -> argparse.ArgumentParser:
|
|
34
|
+
parser = argparse.ArgumentParser(
|
|
35
|
+
prog="faved-mcp",
|
|
36
|
+
description="MCP server for Faved. Run without arguments to start the stdio server.",
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument("command", nargs="?", default="serve", choices=["serve", "setup"], help="serve (default) or setup")
|
|
39
|
+
parser.add_argument("--transport", choices=["stdio", "http", "streamable-http"], help="override FAVED_MCP_TRANSPORT")
|
|
40
|
+
parser.add_argument("--host", help="override FAVED_MCP_HOST")
|
|
41
|
+
parser.add_argument("--port", type=int, help="override FAVED_MCP_PORT")
|
|
42
|
+
parser.add_argument("--version", action="version", version=f"faved-mcp {__version__}")
|
|
43
|
+
return parser
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def main() -> None:
|
|
47
|
+
args = _parser().parse_args()
|
|
48
|
+
|
|
49
|
+
if args.command == "setup":
|
|
50
|
+
from .setup_cli import run_setup
|
|
51
|
+
|
|
52
|
+
raise SystemExit(run_setup())
|
|
53
|
+
|
|
54
|
+
config = Config.from_env()
|
|
55
|
+
if args.transport:
|
|
56
|
+
config = replace(config, transport=args.transport.lower())
|
|
57
|
+
if args.host:
|
|
58
|
+
config = replace(config, host=args.host)
|
|
59
|
+
if args.port:
|
|
60
|
+
config = replace(config, port=args.port)
|
|
61
|
+
|
|
62
|
+
if not config.credentials_configured():
|
|
63
|
+
print(
|
|
64
|
+
"No Faved credentials configured.\n"
|
|
65
|
+
"Run `faved-mcp setup` once, or set FAVED_URL / FAVED_USERNAME / FAVED_PASSWORD.\n"
|
|
66
|
+
f"Config file: {config_path()}",
|
|
67
|
+
file=sys.stderr,
|
|
68
|
+
)
|
|
69
|
+
raise SystemExit(2)
|
|
70
|
+
|
|
71
|
+
from .server import mcp
|
|
72
|
+
|
|
73
|
+
transport = config.transport
|
|
74
|
+
if transport == "stdio":
|
|
75
|
+
asyncio.run(mcp.run_stdio_async())
|
|
76
|
+
return
|
|
77
|
+
|
|
78
|
+
if transport not in {"http", "streamable-http"}:
|
|
79
|
+
sys.exit(f"Unsupported transport '{transport}'. Use stdio or http.")
|
|
80
|
+
|
|
81
|
+
if not config.token:
|
|
82
|
+
sys.exit("FAVED_MCP_TOKEN is required for the HTTP transport.")
|
|
83
|
+
|
|
84
|
+
import uvicorn
|
|
85
|
+
|
|
86
|
+
app = mcp.streamable_http_app(host=config.host)
|
|
87
|
+
app.add_middleware(BearerAuth, token=config.token)
|
|
88
|
+
uvicorn.run(app, host=config.host, port=config.port)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
if __name__ == "__main__":
|
|
92
|
+
main()
|
faved_mcp/client.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"""Thin client over Faved's public REST API.
|
|
2
|
+
|
|
3
|
+
All HTTP lives here so tools and tests share one seam. No database access.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
from typing import Any, Iterable
|
|
10
|
+
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
TRACKING_PARAMS = {
|
|
15
|
+
"utm_source",
|
|
16
|
+
"utm_medium",
|
|
17
|
+
"utm_campaign",
|
|
18
|
+
"utm_term",
|
|
19
|
+
"utm_content",
|
|
20
|
+
"utm_id",
|
|
21
|
+
"fbclid",
|
|
22
|
+
"gclid",
|
|
23
|
+
"mc_cid",
|
|
24
|
+
"mc_eid",
|
|
25
|
+
"ref",
|
|
26
|
+
"ref_src",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class FavedError(Exception):
|
|
31
|
+
"""Structured failure surfaced to MCP clients."""
|
|
32
|
+
|
|
33
|
+
def __init__(self, code: str, message: str, details: Any = None, status: int | None = None):
|
|
34
|
+
super().__init__(message)
|
|
35
|
+
self.code = code
|
|
36
|
+
self.message = message
|
|
37
|
+
self.details = details
|
|
38
|
+
self.status = status
|
|
39
|
+
|
|
40
|
+
def as_dict(self) -> dict[str, Any]:
|
|
41
|
+
out: dict[str, Any] = {"ok": False, "code": self.code, "message": self.message}
|
|
42
|
+
if self.details is not None:
|
|
43
|
+
out["details"] = self.details
|
|
44
|
+
return out
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def normalize_url(url: str) -> str:
|
|
48
|
+
"""Stable comparison key: no fragment, no tracking params, no default port, no trailing slash."""
|
|
49
|
+
parts = urlsplit(url.strip())
|
|
50
|
+
scheme = parts.scheme.lower() or "https"
|
|
51
|
+
host = (parts.hostname or "").lower()
|
|
52
|
+
port = parts.port
|
|
53
|
+
netloc = host
|
|
54
|
+
if port and not ((scheme == "http" and port == 80) or (scheme == "https" and port == 443)):
|
|
55
|
+
netloc = f"{host}:{port}"
|
|
56
|
+
query = [
|
|
57
|
+
(k, v)
|
|
58
|
+
for k, v in parse_qsl(parts.query, keep_blank_values=True)
|
|
59
|
+
if k.lower() not in TRACKING_PARAMS and not k.lower().startswith("utm_")
|
|
60
|
+
]
|
|
61
|
+
path = parts.path.rstrip("/") or ""
|
|
62
|
+
return urlunsplit((scheme, netloc, path, urlencode(sorted(query)), ""))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _as_int(value: Any) -> int | None:
|
|
66
|
+
try:
|
|
67
|
+
return int(value)
|
|
68
|
+
except (TypeError, ValueError):
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class FavedClient:
|
|
73
|
+
def __init__(self, base_url: str, username: str, password: str, timeout: float = 15.0):
|
|
74
|
+
self.base_url = base_url.rstrip("/")
|
|
75
|
+
self.username = username
|
|
76
|
+
self.password = password
|
|
77
|
+
self._http = httpx.Client(
|
|
78
|
+
base_url=self.base_url,
|
|
79
|
+
timeout=timeout,
|
|
80
|
+
follow_redirects=True,
|
|
81
|
+
headers={"Accept": "application/json"},
|
|
82
|
+
)
|
|
83
|
+
self._logged_in = False
|
|
84
|
+
self._csrf = ""
|
|
85
|
+
|
|
86
|
+
# ---------------------------------------------------------------- session
|
|
87
|
+
|
|
88
|
+
def close(self) -> None:
|
|
89
|
+
self._http.close()
|
|
90
|
+
|
|
91
|
+
def _prime_csrf(self) -> None:
|
|
92
|
+
response = self._http.get("/api/app-info")
|
|
93
|
+
if response.status_code >= 500:
|
|
94
|
+
raise FavedError("upstream", "Faved app-info failed", status=response.status_code)
|
|
95
|
+
self._csrf = self._http.cookies.get("CSRF-TOKEN", "") or ""
|
|
96
|
+
|
|
97
|
+
def login(self) -> None:
|
|
98
|
+
self._prime_csrf()
|
|
99
|
+
if not self.username or not self.password:
|
|
100
|
+
raise FavedError("auth", "FAVED_USERNAME and FAVED_PASSWORD are required")
|
|
101
|
+
response = self._http.post(
|
|
102
|
+
"/api/auth/login",
|
|
103
|
+
json={"username": self.username, "password": self.password},
|
|
104
|
+
headers=self._write_headers(),
|
|
105
|
+
)
|
|
106
|
+
payload = self._payload(response)
|
|
107
|
+
if response.status_code >= 400 or (isinstance(payload, dict) and payload.get("success") is False):
|
|
108
|
+
raise FavedError("auth", "Faved login failed", payload, response.status_code)
|
|
109
|
+
self._logged_in = True
|
|
110
|
+
|
|
111
|
+
def _write_headers(self) -> dict[str, str]:
|
|
112
|
+
return {"X-CSRF-TOKEN": self._csrf} if self._csrf else {}
|
|
113
|
+
|
|
114
|
+
def _ensure_session(self) -> None:
|
|
115
|
+
if not self._logged_in:
|
|
116
|
+
self.login()
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def _payload(response: httpx.Response) -> Any:
|
|
120
|
+
try:
|
|
121
|
+
return response.json()
|
|
122
|
+
except (json.JSONDecodeError, ValueError):
|
|
123
|
+
return response.text
|
|
124
|
+
|
|
125
|
+
@staticmethod
|
|
126
|
+
def _unwrap(payload: Any, status: int) -> Any:
|
|
127
|
+
if isinstance(payload, dict) and payload.get("success") is False:
|
|
128
|
+
raise FavedError("upstream", str(payload.get("message") or "Faved refused the request"), payload, status)
|
|
129
|
+
if isinstance(payload, dict) and payload.get("success") is True and "data" in payload:
|
|
130
|
+
return payload["data"]
|
|
131
|
+
return payload
|
|
132
|
+
|
|
133
|
+
def request(self, method: str, path: str, **kwargs: Any) -> Any:
|
|
134
|
+
self._ensure_session()
|
|
135
|
+
method = method.upper()
|
|
136
|
+
if method != "GET":
|
|
137
|
+
kwargs.setdefault("headers", {})
|
|
138
|
+
kwargs["headers"] = {**self._write_headers(), **kwargs["headers"]}
|
|
139
|
+
response = self._http.request(method, path, **kwargs)
|
|
140
|
+
|
|
141
|
+
if response.status_code == 401:
|
|
142
|
+
self._logged_in = False
|
|
143
|
+
self.login()
|
|
144
|
+
kwargs["headers"] = {**self._write_headers(), **kwargs.get("headers", {})}
|
|
145
|
+
response = self._http.request(method, path, **kwargs)
|
|
146
|
+
|
|
147
|
+
if response.status_code == 403 and "csrf" in str(self._payload(response)).lower():
|
|
148
|
+
self._prime_csrf()
|
|
149
|
+
kwargs["headers"] = {**self._write_headers(), **kwargs.get("headers", {})}
|
|
150
|
+
response = self._http.request(method, path, **kwargs)
|
|
151
|
+
|
|
152
|
+
payload = self._payload(response)
|
|
153
|
+
if response.status_code >= 400:
|
|
154
|
+
raise FavedError("upstream", self._message(payload, response), payload, response.status_code)
|
|
155
|
+
return self._unwrap(payload, response.status_code)
|
|
156
|
+
|
|
157
|
+
@staticmethod
|
|
158
|
+
def _message(payload: Any, response: httpx.Response) -> str:
|
|
159
|
+
if isinstance(payload, dict):
|
|
160
|
+
for key in ("message", "error", "detail"):
|
|
161
|
+
if payload.get(key):
|
|
162
|
+
return str(payload[key])
|
|
163
|
+
return f"Faved returned HTTP {response.status_code}"
|
|
164
|
+
|
|
165
|
+
# ------------------------------------------------------------------- data
|
|
166
|
+
|
|
167
|
+
def app_info(self) -> dict[str, Any]:
|
|
168
|
+
return self.request("GET", "/api/app-info")
|
|
169
|
+
|
|
170
|
+
def current_user(self) -> dict[str, Any]:
|
|
171
|
+
data = self.request("GET", "/api/settings/user")
|
|
172
|
+
if isinstance(data, dict) and isinstance(data.get("user"), dict):
|
|
173
|
+
return data["user"]
|
|
174
|
+
return data if isinstance(data, dict) else {}
|
|
175
|
+
|
|
176
|
+
def items(self) -> list[dict[str, Any]]:
|
|
177
|
+
data = self.request("GET", "/api/items")
|
|
178
|
+
return list(data.values()) if isinstance(data, dict) else list(data or [])
|
|
179
|
+
|
|
180
|
+
def tags(self) -> dict[int, dict[str, Any]]:
|
|
181
|
+
data = self.request("GET", "/api/tags")
|
|
182
|
+
rows = data.values() if isinstance(data, dict) else (data or [])
|
|
183
|
+
return {int(row["id"]): row for row in rows}
|
|
184
|
+
|
|
185
|
+
def tag_paths(self) -> dict[int, str]:
|
|
186
|
+
tags = self.tags()
|
|
187
|
+
cache: dict[int, str] = {}
|
|
188
|
+
|
|
189
|
+
def path(tag_id: int) -> str:
|
|
190
|
+
if tag_id in cache:
|
|
191
|
+
return cache[tag_id]
|
|
192
|
+
tag = tags.get(tag_id)
|
|
193
|
+
if not tag:
|
|
194
|
+
return ""
|
|
195
|
+
parent = _as_int(tag.get("parent")) or 0
|
|
196
|
+
cache[tag_id] = f"{path(parent)}/{tag['title']}".strip("/") if parent else tag["title"]
|
|
197
|
+
return cache[tag_id]
|
|
198
|
+
|
|
199
|
+
return {tag_id: path(tag_id) for tag_id in tags}
|
|
200
|
+
|
|
201
|
+
def resolve_tag(self, ref: int | str, create_missing: bool = False) -> int:
|
|
202
|
+
tags = self.tags()
|
|
203
|
+
if isinstance(ref, int) or str(ref).isdigit():
|
|
204
|
+
tag_id = int(ref)
|
|
205
|
+
if tag_id not in tags:
|
|
206
|
+
raise FavedError("not_found", f"Tag {tag_id} does not exist")
|
|
207
|
+
return tag_id
|
|
208
|
+
|
|
209
|
+
wanted = "/".join(part.strip() for part in str(ref).split("/") if part.strip()).lower()
|
|
210
|
+
paths = self.tag_paths()
|
|
211
|
+
for tag_id, path in paths.items():
|
|
212
|
+
if path.lower() == wanted:
|
|
213
|
+
return tag_id
|
|
214
|
+
if "/" not in wanted:
|
|
215
|
+
by_title = [tag_id for tag_id, row in tags.items() if str(row.get("title", "")).casefold() == wanted]
|
|
216
|
+
if len(by_title) == 1:
|
|
217
|
+
return by_title[0]
|
|
218
|
+
if len(by_title) > 1:
|
|
219
|
+
raise FavedError("ambiguous", f"Several tags are named '{ref}'; use a full path")
|
|
220
|
+
if not create_missing:
|
|
221
|
+
raise FavedError("not_found", f"Tag path '{ref}' does not exist")
|
|
222
|
+
created = self.request("POST", "/api/tags", json={"title": str(ref)})
|
|
223
|
+
new_id = _as_int((created or {}).get("tag_id")) if isinstance(created, dict) else None
|
|
224
|
+
if new_id:
|
|
225
|
+
return new_id
|
|
226
|
+
for tag_id, path in self.tag_paths().items():
|
|
227
|
+
if path.lower() == wanted:
|
|
228
|
+
return tag_id
|
|
229
|
+
raise FavedError("upstream", f"Tag '{ref}' was created but cannot be resolved")
|
|
230
|
+
|
|
231
|
+
def resolve_tags(self, refs: Iterable[int | str] | None, create_missing: bool = False) -> list[int]:
|
|
232
|
+
out: list[int] = []
|
|
233
|
+
for ref in refs or []:
|
|
234
|
+
tag_id = self.resolve_tag(ref, create_missing=create_missing)
|
|
235
|
+
if tag_id not in out:
|
|
236
|
+
out.append(tag_id)
|
|
237
|
+
return out
|
|
238
|
+
|
|
239
|
+
# ------------------------------------------------------------------ items
|
|
240
|
+
|
|
241
|
+
def get_item(self, item_id: int) -> dict[str, Any] | None:
|
|
242
|
+
for item in self.items():
|
|
243
|
+
if int(item["id"]) == item_id:
|
|
244
|
+
return item
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
def find_by_url(self, url: str) -> dict[str, Any] | None:
|
|
248
|
+
key = normalize_url(url)
|
|
249
|
+
for item in self.items():
|
|
250
|
+
if normalize_url(item.get("url", "")) == key:
|
|
251
|
+
return item
|
|
252
|
+
return None
|
|
253
|
+
|
|
254
|
+
def create_item(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
255
|
+
return self.request("POST", "/api/items", json=payload)
|
|
256
|
+
|
|
257
|
+
def update_item(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
258
|
+
return self.request("PATCH", "/api/items", json=payload)
|
|
259
|
+
|
|
260
|
+
def delete_items(self, item_ids: list[int]) -> Any:
|
|
261
|
+
return self.request("POST", "/api/items/delete", json={"item-ids": item_ids})
|
|
262
|
+
|
|
263
|
+
def set_item_tags(self, item_ids: list[int], add: list[int], remove: list[int]) -> Any:
|
|
264
|
+
return self.request(
|
|
265
|
+
"PATCH",
|
|
266
|
+
"/api/items/tags",
|
|
267
|
+
json={"item-ids": item_ids, "tag-ids-all": add, "tag-ids-some": remove},
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
def fetch_metadata(self, url: str) -> dict[str, Any]:
|
|
271
|
+
return self.request("POST", "/api/url/fetch-metadata", json={"url": url})
|
|
272
|
+
|
|
273
|
+
# ------------------------------------------------------------------- tags
|
|
274
|
+
|
|
275
|
+
def create_tag(self, title: str, description: str = "") -> Any:
|
|
276
|
+
payload: dict[str, Any] = {"title": title}
|
|
277
|
+
if description:
|
|
278
|
+
payload["description"] = description
|
|
279
|
+
return self.request("POST", "/api/tags", json=payload)
|
|
280
|
+
|
|
281
|
+
def update_tag(self, tag_id: int, title: str, description: str, parent: int = 0) -> Any:
|
|
282
|
+
return self.request(
|
|
283
|
+
"PATCH",
|
|
284
|
+
"/api/tags/update",
|
|
285
|
+
json={"tag-id": str(tag_id), "title": title, "description": description, "parent": parent},
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
def set_tag_pinned(self, tag_id: int, pinned: bool) -> Any:
|
|
289
|
+
return self.request("PATCH", "/api/tags/update-pinned", json={"tag-id": tag_id, "pinned": pinned})
|
|
290
|
+
|
|
291
|
+
def set_tag_color(self, tag_id: int, color: str) -> Any:
|
|
292
|
+
return self.request("PATCH", "/api/tags/update-color", json={"tag-id": tag_id, "color": color})
|
|
293
|
+
|
|
294
|
+
def delete_tag(self, tag_id: int) -> Any:
|
|
295
|
+
return self.request("DELETE", "/api/tags", json={"tag-id": tag_id})
|
|
296
|
+
|
|
297
|
+
# ---------------------------------------------------------- import/export
|
|
298
|
+
|
|
299
|
+
def import_html(self, content: bytes, source: str = "browser") -> Any:
|
|
300
|
+
files = {"bookmark-file-html": ("bookmarks.html", content, "text/html")}
|
|
301
|
+
data = {"import-source-name": source}
|
|
302
|
+
return self.request("POST", "/api/import/bookmarks", files=files, data=data)
|
|
303
|
+
|
|
304
|
+
def import_pocket(self, content: bytes) -> Any:
|
|
305
|
+
files = {"pocket-zip": ("pocket.zip", content, "application/zip")}
|
|
306
|
+
return self.request("POST", "/api/import/pocket", files=files)
|
faved_mcp/config.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Configuration: environment variables first, then a local setup file."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def config_path() -> Path:
|
|
12
|
+
override = os.getenv("FAVED_MCP_CONFIG")
|
|
13
|
+
if override:
|
|
14
|
+
return Path(override).expanduser()
|
|
15
|
+
return Path.home() / ".config" / "faved-mcp" / "config.json"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _file_values() -> dict:
|
|
19
|
+
path = config_path()
|
|
20
|
+
try:
|
|
21
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
22
|
+
except FileNotFoundError:
|
|
23
|
+
return {}
|
|
24
|
+
except (OSError, ValueError):
|
|
25
|
+
return {}
|
|
26
|
+
return data if isinstance(data, dict) else {}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _bool(name: str, default: bool = False) -> bool:
|
|
30
|
+
raw = os.getenv(name)
|
|
31
|
+
if raw is None:
|
|
32
|
+
return default
|
|
33
|
+
return raw.strip().lower() in {"1", "true", "yes", "on"}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class Config:
|
|
38
|
+
base_url: str
|
|
39
|
+
username: str
|
|
40
|
+
password: str
|
|
41
|
+
transport: str = "stdio"
|
|
42
|
+
host: str = "127.0.0.1"
|
|
43
|
+
port: int = 8090
|
|
44
|
+
token: str = ""
|
|
45
|
+
enable_write: bool = False
|
|
46
|
+
allow_local_paths: bool = False
|
|
47
|
+
timeout: float = 15.0
|
|
48
|
+
|
|
49
|
+
@classmethod
|
|
50
|
+
def from_env(cls) -> "Config":
|
|
51
|
+
saved = _file_values()
|
|
52
|
+
return cls(
|
|
53
|
+
base_url=(os.getenv("FAVED_URL") or saved.get("base_url") or "http://localhost").rstrip("/"),
|
|
54
|
+
username=os.getenv("FAVED_USERNAME") or str(saved.get("username") or ""),
|
|
55
|
+
password=os.getenv("FAVED_PASSWORD") or str(saved.get("password") or ""),
|
|
56
|
+
transport=(os.getenv("FAVED_MCP_TRANSPORT") or "stdio").lower(),
|
|
57
|
+
host=os.getenv("FAVED_MCP_HOST", "127.0.0.1"),
|
|
58
|
+
port=int(os.getenv("FAVED_MCP_PORT", "8090")),
|
|
59
|
+
token=os.getenv("FAVED_MCP_TOKEN", ""),
|
|
60
|
+
enable_write=_bool("FAVED_MCP_ENABLE_WRITE", False),
|
|
61
|
+
allow_local_paths=_bool("FAVED_MCP_ALLOW_LOCAL_PATHS", False),
|
|
62
|
+
timeout=float(os.getenv("FAVED_MCP_TIMEOUT", "15")),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def credentials_configured(self) -> bool:
|
|
66
|
+
return bool(self.username and self.password)
|