fix(calendar): TZID Europe/Berlin for timed events/tasks, resolve real resource href on delete/update, fix _report_tasks XML; sync deployed servers (owamail, openwearables, ghostfolio), docs and tests
This commit is contained in:
1 parent
7c01e7be53
commit
75185ef2ba
41 files changed
+8834
-324
No files matched your search
+257
-6
@@ -1,6 +1,6 @@
|
||||
"""MCP Notes Server — reads/writes Joplin notes via Joplin CLI Data API."""
|
||||
|
||||
import os, sys, contextlib, base64
|
||||
import os, sys, contextlib, base64, json, mimetypes
|
||||
from typing import Annotated
|
||||
|
||||
import httpx
|
||||
@@ -8,7 +8,8 @@ from pydantic import Field
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
from mcp.types import TextContent, ImageContent, EmbeddedResource, BlobResourceContents
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
from starlette.routing import Mount, Route
|
||||
from starlette.responses import Response
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||
from common import get_current_user, OAUTH_ROUTES, BearerAuthMiddleware, load_config
|
||||
@@ -48,7 +49,7 @@ def _post(user, path, json_data):
|
||||
if not api:
|
||||
return None, "Kein Joplin-Zugang"
|
||||
try:
|
||||
r = httpx.post(f"{api['url']}{path}?token={api['token']}", json=json_data, timeout=30)
|
||||
r = httpx.post(f"{api['url']}{path}", params={"token": api["token"]}, json=json_data, timeout=30)
|
||||
if r.status_code >= 400:
|
||||
return None, f"Joplin HTTP {r.status_code}"
|
||||
return r.json(), None
|
||||
@@ -69,7 +70,7 @@ def _put(user, path, json_data):
|
||||
if not api:
|
||||
return None, "Kein Joplin-Zugang"
|
||||
try:
|
||||
r = httpx.put(f"{api['url']}{path}?token={api['token']}", json=json_data, timeout=30)
|
||||
r = httpx.put(f"{api['url']}{path}", params={"token": api["token"]}, json=json_data, timeout=30)
|
||||
if r.status_code >= 400:
|
||||
return None, f"Joplin HTTP {r.status_code}"
|
||||
return r.json(), None
|
||||
@@ -112,6 +113,62 @@ def _all_items(user, path, params=None):
|
||||
return items, None
|
||||
|
||||
|
||||
MAX_ATTACHMENT_BYTES = 2 * 1024 * 1024 * 1024
|
||||
|
||||
|
||||
def _decode_base64(content_base64):
|
||||
s = re.sub(r"\s+", "", content_base64 or "")
|
||||
if s.startswith("data:") and "," in s:
|
||||
s = s.split(",", 1)[1]
|
||||
try:
|
||||
return base64.b64decode(s, validate=True), None
|
||||
except Exception as e:
|
||||
return None, f"Ungueltiges Base64: {e}"
|
||||
|
||||
|
||||
def _guess_mime(filename, mime):
|
||||
if mime:
|
||||
return mime
|
||||
return mimetypes.guess_type(filename or "")[0] or "application/octet-stream"
|
||||
|
||||
|
||||
def _resource_link(title, mime, resource_id):
|
||||
label = (title or "Anhang").replace("[", "(").replace("]", ")")
|
||||
if (mime or "").startswith("image/"):
|
||||
return f""
|
||||
return f"[{label}](:/{resource_id})"
|
||||
|
||||
|
||||
def _strip_resource_refs(body, resource_id):
|
||||
"""Remove Markdown/HTML links pointing at :/resourceId from a note body."""
|
||||
rid = re.escape(resource_id)
|
||||
body = re.sub(r"!?\[[^\]]*\]\(:/" + rid + r'(?:\s+"[^"]*")?\)', "", body)
|
||||
body = re.sub(r"<[^>]*:/" + rid + r"[^>]*>", "", body)
|
||||
body = re.sub(r"\n{3,}", "\n\n", body).strip()
|
||||
return body + ("\n" if body else "")
|
||||
|
||||
|
||||
def _upload_resource(user, path, filename, content, mime, props, method="post"):
|
||||
"""Create (POST) or replace content (PUT) of a Joplin resource via multipart/form-data.
|
||||
|
||||
Joplin expects the file in the 'data' field and the properties as JSON in the
|
||||
'props' field (see Data API docs, POST/PUT /resources)."""
|
||||
api = _api(user)
|
||||
if not api:
|
||||
return None, "Kein Joplin-Zugang"
|
||||
files = {"data": (filename or "attachment.bin", content, mime or "application/octet-stream")}
|
||||
data = {"props": json.dumps(props or {})}
|
||||
try:
|
||||
fn = httpx.post if method == "post" else httpx.put
|
||||
r = fn(f"{api['url']}{path}", params={"token": api["token"]},
|
||||
files=files, data=data, timeout=120)
|
||||
if r.status_code >= 400:
|
||||
return None, f"Joplin HTTP {r.status_code}: {r.text[:200]}"
|
||||
return r.json(), None
|
||||
except Exception as e:
|
||||
return None, str(e)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def list_notebooks() -> str:
|
||||
"""List all Joplin notebooks (folders). Call this first to see notebook names and IDs."""
|
||||
@@ -224,6 +281,116 @@ def read_resource(
|
||||
uri=uri, blob=base64.b64encode(content).decode(), mimeType=mime))]
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def add_attachment(
|
||||
note_id: Annotated[str, Field(description="Note ID from list_notes or search_notes")],
|
||||
filename: Annotated[str, Field(description="File name incl. extension, e.g. 'angebot.pdf' or 'foto.jpg'. Used as resource title and to guess the MIME type.")],
|
||||
content_base64: Annotated[str, Field(description="File content as a Base64 string (raw bytes). A 'data:...;base64,' prefix and line breaks are accepted.")],
|
||||
mime: Annotated[str, Field(description="MIME type, e.g. 'application/pdf'. Leave empty to guess it from the filename.")] = "",
|
||||
title: Annotated[str, Field(description="Title shown for the attachment in Joplin. Leave empty to use the filename.")] = "",
|
||||
) -> str:
|
||||
"""Add an attachment (image, PDF, any file) to a note. Uploads the file as a Joplin resource and appends a Markdown link at the end of the note body. Max 25 MB."""
|
||||
user = get_current_user()
|
||||
if not user: return "Error: not authenticated"
|
||||
if not _valid_id(note_id): return "Fehler: ungueltige note_id (erwartet 32 Hex-Zeichen)"
|
||||
if not filename: return "Fehler: filename erforderlich"
|
||||
content, err = _decode_base64(content_base64)
|
||||
if err: return f"Fehler: {err}"
|
||||
if not content: return "Fehler: content_base64 ist leer"
|
||||
if len(content) > MAX_ATTACHMENT_BYTES:
|
||||
return f"Fehler: Anhang zu gross ({len(content):,} bytes, max {MAX_ATTACHMENT_BYTES:,})"
|
||||
mime = _guess_mime(filename, mime)
|
||||
props = {"title": title or filename, "mime": mime}
|
||||
res, err = _upload_resource(user, "/resources", filename, content, mime, props, method="post")
|
||||
if err: return f"Fehler: {err}"
|
||||
rid = res.get("id", "")
|
||||
r, err = _get(user, f"/notes/{note_id}", {"fields": "id,title,body"})
|
||||
if err: return f"Anhang hochgeladen (resource id: {rid}), aber Notiz-Lesen fehlgeschlagen: {err}"
|
||||
note = r.json()
|
||||
new_body = (note.get("body", "") or "").rstrip() + "\n\n" + _resource_link(props["title"], mime, rid) + "\n"
|
||||
_, err = _put(user, f"/notes/{note_id}", {"body": new_body})
|
||||
if err: return f"Anhang hochgeladen (resource id: {rid}), aber Verlinkung fehlgeschlagen: {err}"
|
||||
return f"Anhang hinzugefuegt: {props['title']} ({mime}, {len(content):,} bytes), resource id: {rid}, verlinkt in Notiz: {note.get('title','?')}"
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def update_attachment(
|
||||
resource_id: Annotated[str, Field(description="Attachment/resource ID from list_note_resources")],
|
||||
content_base64: Annotated[str, Field(description="New file content as a Base64 string. Leave empty to keep the current content (then only title/mime change).")] = "",
|
||||
filename: Annotated[str, Field(description="New file name, only used together with content_base64.")] = "",
|
||||
mime: Annotated[str, Field(description="New MIME type. Leave empty to keep the current one.")] = "",
|
||||
title: Annotated[str, Field(description="New title shown in Joplin. Leave empty to keep the current one.")] = "",
|
||||
) -> str:
|
||||
"""Edit an attachment: replace its file content and/or change its title and MIME type. Only the given fields are changed. Get resource_id from list_note_resources."""
|
||||
user = get_current_user()
|
||||
if not user: return "Error: not authenticated"
|
||||
if not _valid_id(resource_id): return "Fehler: ungueltige resource_id (erwartet 32 Hex-Zeichen)"
|
||||
if not content_base64 and not title and not mime:
|
||||
return "Nichts zu aendern: content_base64, title oder mime angeben"
|
||||
rmeta, err = _get(user, f"/resources/{resource_id}", {"fields": "id,title,mime,filename"})
|
||||
if err: return f"Fehler: {err}"
|
||||
meta = rmeta.json()
|
||||
new_title = title or meta.get("title", "") or "attachment"
|
||||
new_mime = mime or meta.get("mime", "")
|
||||
if content_base64:
|
||||
content, err = _decode_base64(content_base64)
|
||||
if err: return f"Fehler: {err}"
|
||||
if not content: return "Fehler: content_base64 ist leer"
|
||||
if len(content) > MAX_ATTACHMENT_BYTES:
|
||||
return f"Fehler: Anhang zu gross ({len(content):,} bytes, max {MAX_ATTACHMENT_BYTES:,})"
|
||||
fn = filename or meta.get("filename") or new_title
|
||||
new_mime = _guess_mime(fn, new_mime)
|
||||
props = {"title": new_title, "mime": new_mime}
|
||||
res, err = _upload_resource(user, f"/resources/{resource_id}", fn, content, new_mime, props, method="put")
|
||||
if err: return f"Fehler: {err}"
|
||||
return f"Anhang-Inhalt ersetzt: {new_title} ({new_mime}, {len(content):,} bytes), resource id: {resource_id}"
|
||||
payload = {}
|
||||
if title: payload["title"] = title
|
||||
if mime: payload["mime"] = mime
|
||||
d, err = _put(user, f"/resources/{resource_id}", payload)
|
||||
if err: return f"Fehler: {err}"
|
||||
return f"Anhang aktualisiert ({', '.join(sorted(payload))}): {d.get('title', new_title)} (resource id: {resource_id})"
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def delete_attachment(
|
||||
resource_id: Annotated[str, Field(description="Attachment/resource ID from list_note_resources")],
|
||||
note_id: Annotated[str, Field(description="Note ID whose body also references the attachment. Recommended: only then is the Markdown link reliably removed. Leave empty to try all notes that Joplin reports for the resource.")] = "",
|
||||
remove_links: Annotated[bool, Field(description="True (default): also remove the Markdown links to the attachment from the note body. False: keep the (then broken) links.")] = True,
|
||||
) -> str:
|
||||
"""Delete an attachment permanently (Joplin has no trash for resources) and remove its links from the note body. Get resource_id from list_note_resources."""
|
||||
user = get_current_user()
|
||||
if not user: return "Error: not authenticated"
|
||||
if not _valid_id(resource_id): return "Fehler: ungueltige resource_id (erwartet 32 Hex-Zeichen)"
|
||||
if note_id and not _valid_id(note_id): return "Fehler: ungueltige note_id (erwartet 32 Hex-Zeichen)"
|
||||
rmeta, err = _get(user, f"/resources/{resource_id}", {"fields": "id,title"})
|
||||
if err: return f"Fehler: {err}"
|
||||
rtitle = rmeta.json().get("title", resource_id)
|
||||
cleaned = []
|
||||
if remove_links:
|
||||
notes, err = _all_items(user, f"/resources/{resource_id}/notes", {"fields": "id,title"})
|
||||
if err: return f"Fehler: {err}"
|
||||
if note_id:
|
||||
notes = [n for n in notes if n["id"] == note_id] or [{"id": note_id}]
|
||||
for n in notes:
|
||||
r, err = _get(user, f"/notes/{n['id']}", {"fields": "id,title,body"})
|
||||
if err: continue
|
||||
d = r.json()
|
||||
body = d.get("body", "") or ""
|
||||
if f":/{resource_id}" in body:
|
||||
_, err = _put(user, f"/notes/{n['id']}", {"body": _strip_resource_refs(body, resource_id)})
|
||||
if not err:
|
||||
cleaned.append(d.get("title", n["id"]))
|
||||
_, err = _delete(user, f"/resources/{resource_id}")
|
||||
if err: return f"Fehler beim Loeschen: {err}"
|
||||
msg = f"Anhang geloescht (nicht wiederherstellbar): {rtitle} (resource id: {resource_id})"
|
||||
if cleaned:
|
||||
msg += f", Referenz entfernt aus: {', '.join(cleaned)}"
|
||||
elif not note_id:
|
||||
msg += ". Hinweis: keine verlinkte Notiz gefunden (Joplins Link-Tabelle kann leer sein) — note_id angeben, um den Link sicher zu entfernen"
|
||||
return msg
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def create_note(
|
||||
notebook: Annotated[str, Field(description="Notebook name or ID where the note is created")],
|
||||
@@ -298,6 +465,88 @@ def delete_note(
|
||||
return f"Notiz in den Papierkorb verschoben: {note_title} (in Joplin wiederherstellbar)"
|
||||
|
||||
|
||||
async def raw_resource(request):
|
||||
"""Roher Resource-Transfer (beliebige Binaerdateien).
|
||||
|
||||
GET /raw?resource_id=<id> -> Resource als Rohbytes
|
||||
PUT /raw?resource_id=<id>[&filename=&mime=&title=] -> vorhandene Resource ersetzen
|
||||
PUT /raw?note_id=<id>&filename=<name>[&mime=&title=] -> neue Resource anlegen + in Notiz verlinken
|
||||
Token-geschuetzt ueber BearerAuthMiddleware.
|
||||
"""
|
||||
user = get_current_user()
|
||||
if not user:
|
||||
return Response("unauthorized", status_code=401)
|
||||
q = request.query_params
|
||||
|
||||
if request.method == "GET":
|
||||
rid = q.get("resource_id", "")
|
||||
if not _valid_id(rid):
|
||||
return Response("invalid resource_id", status_code=400)
|
||||
rmeta, err = _get(user, f"/resources/{rid}", {"fields": "id,title,mime"})
|
||||
if err:
|
||||
return Response(f"Fehler: {err}", status_code=404)
|
||||
meta = rmeta.json()
|
||||
mime = meta.get("mime", "application/octet-stream")
|
||||
title = meta.get("title", "attachment")
|
||||
rfile, err = _get(user, f"/resources/{rid}/file")
|
||||
if err:
|
||||
return Response(f"Fehler: {err}", status_code=404)
|
||||
return Response(rfile.content, media_type=mime,
|
||||
headers={"Content-Disposition": f'attachment; filename="{title}"'})
|
||||
|
||||
# PUT (Upload / Ersetzen)
|
||||
content = await request.body()
|
||||
if not content:
|
||||
return Response("leerer Body", status_code=400)
|
||||
if len(content) > MAX_ATTACHMENT_BYTES:
|
||||
return Response(f"zu gross ({len(content):,} > {MAX_ATTACHMENT_BYTES:,})", status_code=413)
|
||||
rid = q.get("resource_id", "")
|
||||
note_id = q.get("note_id", "")
|
||||
filename = q.get("filename", "")
|
||||
mime = q.get("mime", "")
|
||||
title = q.get("title", "")
|
||||
|
||||
if rid:
|
||||
if not _valid_id(rid):
|
||||
return Response("invalid resource_id", status_code=400)
|
||||
rmeta, err = _get(user, f"/resources/{rid}", {"fields": "id,title,mime,filename"})
|
||||
if err:
|
||||
return Response(f"Fehler: {err}", status_code=404)
|
||||
meta = rmeta.json()
|
||||
fn = filename or meta.get("filename") or meta.get("title") or "attachment.bin"
|
||||
new_mime = _guess_mime(fn, mime or meta.get("mime", ""))
|
||||
new_title = title or meta.get("title") or fn
|
||||
props = {"title": new_title, "mime": new_mime}
|
||||
res, err = _upload_resource(user, f"/resources/{rid}", fn, content, new_mime, props, method="put")
|
||||
if err:
|
||||
return Response(f"Fehler: {err}", status_code=502)
|
||||
return Response(f"ok resource {rid} ersetzt ({len(content):,} bytes)", status_code=200)
|
||||
|
||||
if note_id:
|
||||
if not _valid_id(note_id):
|
||||
return Response("invalid note_id", status_code=400)
|
||||
if not filename:
|
||||
return Response("filename erforderlich", status_code=400)
|
||||
new_mime = _guess_mime(filename, mime)
|
||||
new_title = title or filename
|
||||
props = {"title": new_title, "mime": new_mime}
|
||||
res, err = _upload_resource(user, "/resources", filename, content, new_mime, props, method="post")
|
||||
if err:
|
||||
return Response(f"Fehler: {err}", status_code=502)
|
||||
new_rid = res.get("id", "")
|
||||
r, err = _get(user, f"/notes/{note_id}", {"fields": "id,title,body"})
|
||||
if err:
|
||||
return Response(f"Anhang angelegt ({new_rid}), Notiz-Lesen fehlgeschlagen: {err}", status_code=200)
|
||||
note = r.json()
|
||||
new_body = (note.get("body", "") or "").rstrip() + "\n\n" + _resource_link(new_title, new_mime, new_rid) + "\n"
|
||||
_, err = _put(user, f"/notes/{note_id}", {"body": new_body})
|
||||
if err:
|
||||
return Response(f"Anhang angelegt ({new_rid}), Verlinkung fehlgeschlagen: {err}", status_code=200)
|
||||
return Response(f"ok resource {new_rid} angelegt + verlinkt", status_code=200)
|
||||
|
||||
return Response("resource_id oder note_id erforderlich", status_code=400)
|
||||
|
||||
|
||||
def create_app():
|
||||
from contextlib import asynccontextmanager
|
||||
mcp_app = mcp.streamable_http_app()
|
||||
@@ -306,11 +555,13 @@ def create_app():
|
||||
async with contextlib.AsyncExitStack() as stack:
|
||||
await stack.enter_async_context(mcp_app.router.lifespan_context(mcp_app))
|
||||
yield
|
||||
routes = list(OAUTH_ROUTES) + [Mount("/", app=mcp_app)]
|
||||
routes = list(OAUTH_ROUTES) + [
|
||||
Route("/raw", raw_resource, methods=["GET", "PUT"]),
|
||||
] + [Mount("/", app=mcp_app)]
|
||||
app = Starlette(routes=routes, lifespan=lifespan)
|
||||
app.add_middleware(BearerAuthMiddleware)
|
||||
return app
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(create_app(), host="127.0.0.1", port=5104)
|
||||
uvicorn.run(create_app(), host="0.0.0.0", port=5104)
|
||||
Reference in new issue
Block a user