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
+81
-5
@@ -1,6 +1,6 @@
|
||||
"""MCP Files Server — browse and read files via WebDAV/oCIS."""
|
||||
|
||||
import os, sys, contextlib, base64, io
|
||||
import os, sys, contextlib, base64, io, re
|
||||
from xml.etree import ElementTree as ET
|
||||
from typing import Annotated
|
||||
|
||||
@@ -10,7 +10,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, StreamingResponse
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||
from common import get_current_user, OAUTH_ROUTES, BearerAuthMiddleware
|
||||
@@ -30,7 +31,27 @@ def _ocis_user(u):
|
||||
return c[0] if c else u
|
||||
|
||||
def _auth(u): c = OCIS_CREDS.get(u); return httpx.BasicAuth(c[0], c[1]) if c else None
|
||||
def _dav(u, p=""): return f"{OCIS}/remote.php/dav/files/{_ocis_user(u)}/{p.lstrip('/')}"
|
||||
|
||||
# Regex fuer bereits korrekt encodierte %XX-Sequenzen (Roundtrip-sicher):
|
||||
# ein rohes '%', das NICHT vor 2 Hex-Ziffern steht, ist ein literales Prozentzeichen.
|
||||
_ENC_SAFE_RE = re.compile(r"%(?![0-9A-Fa-f]{2})")
|
||||
|
||||
|
||||
def _enc_path(p: str) -> str:
|
||||
"""Bereite einen WebDAV-Pfad fuer die URL auf (roundtrip-sicher).
|
||||
|
||||
httpx encodiert Leerzeichen/Umlaute automatisch, aber '#', '?' und ein
|
||||
literales '%' wuerden roh in der URL bleiben ('#' -> Fragment, '?' ->
|
||||
Query-Start, '%' -> kaputte/mehrdeutige Pfade). oCIS liefert hrefs bereits
|
||||
encodiert ('%20', '%C3%BC') zurueck; ein naives quote() wuerde das doppelt
|
||||
encodieren. Deshalb: nur '#'/'?' und Prozent-Zeichen ausserhalb gueltiger
|
||||
%XX-Sequenzen ersetzen, den Rest httpx ueberlassen.
|
||||
"""
|
||||
p = p.replace("#", "%23").replace("?", "%3F")
|
||||
return _ENC_SAFE_RE.sub("%25", p)
|
||||
|
||||
|
||||
def _dav(u, p=""): return f"{OCIS}/remote.php/dav/files/{_ocis_user(u)}/{_enc_path(p.lstrip('/'))}"
|
||||
|
||||
def _propfind(user, path="", depth=1):
|
||||
body = '<?xml version="1.0"?><d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/><d:displayname/><d:getcontentlength/><d:getlastmodified/><d:getcontenttype/></d:prop></d:propfind>'
|
||||
@@ -264,6 +285,59 @@ def move_file(
|
||||
return f"Fehler: HTTP {r.status_code}"
|
||||
|
||||
|
||||
async def raw_file(request):
|
||||
"""Roher Datei-Transfer (beliebige Binaerdateien, jede Groesse).
|
||||
|
||||
GET /raw?path=<ocis> -> Datei herunterladen (gestreamt)
|
||||
PUT /raw?path=<ocis> -> Datei hochladen (Body = Rohbytes)
|
||||
Token-geschuetzt ueber BearerAuthMiddleware.
|
||||
"""
|
||||
user = get_current_user()
|
||||
if not user:
|
||||
return Response("unauthorized", status_code=401)
|
||||
path = request.query_params.get("path", "")
|
||||
if not path:
|
||||
return Response("missing path", status_code=400)
|
||||
url = _dav(user, path)
|
||||
|
||||
if request.method == "GET":
|
||||
client = httpx.AsyncClient(auth=_auth(user), timeout=httpx.Timeout(300.0, connect=10.0))
|
||||
try:
|
||||
req = client.build_request("GET", url)
|
||||
r = await client.send(req, stream=True)
|
||||
except Exception as e:
|
||||
await client.aclose()
|
||||
return Response(f"Fehler: {e}", status_code=502)
|
||||
if r.status_code >= 400:
|
||||
await r.aclose(); await client.aclose()
|
||||
return Response(f"HTTP {r.status_code}", status_code=r.status_code)
|
||||
ct = r.headers.get("content-type", _guess_mime(path, None))
|
||||
|
||||
async def body():
|
||||
try:
|
||||
async for chunk in r.aiter_bytes():
|
||||
yield chunk
|
||||
finally:
|
||||
await r.aclose(); await client.aclose()
|
||||
|
||||
return StreamingResponse(body(), media_type=ct)
|
||||
|
||||
# PUT (Upload) -- Body puffern, damit Content-Length gesetzt wird (oCIS
|
||||
# akzeptiert kein chunked PUT ohne Laenge).
|
||||
ct = request.headers.get("content-type") or _guess_mime(path, None)
|
||||
body = await request.body()
|
||||
client = httpx.AsyncClient(auth=_auth(user), timeout=httpx.Timeout(600.0, connect=10.0))
|
||||
try:
|
||||
r = await client.put(url, content=body, headers={"Content-Type": ct})
|
||||
except Exception as e:
|
||||
await client.aclose()
|
||||
return Response(f"Fehler: {e}", status_code=502)
|
||||
await client.aclose()
|
||||
if r.status_code in (200, 201, 204):
|
||||
return Response("ok", status_code=200)
|
||||
return Response(f"HTTP {r.status_code}", status_code=r.status_code)
|
||||
|
||||
|
||||
def create_app():
|
||||
from contextlib import asynccontextmanager
|
||||
mcp_app = mcp.streamable_http_app()
|
||||
@@ -272,11 +346,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_file, 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=5103)
|
||||
uvicorn.run(create_app(), host="0.0.0.0", port=5103)
|
||||
Reference in new issue
Block a user