|
| 1 | +"""MCP resource definitions and handlers for AutoBangumi. |
| 2 | +
|
| 3 | +``RESOURCES`` lists static resources; ``RESOURCE_TEMPLATES`` lists URI |
| 4 | +templates for parameterised lookups. ``handle_resource`` resolves a URI |
| 5 | +to its JSON payload. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import logging |
| 10 | + |
| 11 | +from mcp import types |
| 12 | + |
| 13 | +from module.conf import VERSION |
| 14 | +from module.manager import TorrentManager |
| 15 | +from module.models import Bangumi |
| 16 | +from module.rss import RSSEngine |
| 17 | + |
| 18 | +from .tools import _bangumi_to_dict |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +RESOURCES = [ |
| 23 | + types.Resource( |
| 24 | + uri="autobangumi://anime/list", |
| 25 | + name="All tracked anime", |
| 26 | + description="List of all anime subscriptions being tracked by AutoBangumi", |
| 27 | + mimeType="application/json", |
| 28 | + ), |
| 29 | + types.Resource( |
| 30 | + uri="autobangumi://status", |
| 31 | + name="Program status", |
| 32 | + description="Current AutoBangumi program status, version, and state", |
| 33 | + mimeType="application/json", |
| 34 | + ), |
| 35 | + types.Resource( |
| 36 | + uri="autobangumi://rss/feeds", |
| 37 | + name="RSS feeds", |
| 38 | + description="All configured RSS feeds with health status", |
| 39 | + mimeType="application/json", |
| 40 | + ), |
| 41 | +] |
| 42 | + |
| 43 | +RESOURCE_TEMPLATES = [ |
| 44 | + types.ResourceTemplate( |
| 45 | + uriTemplate="autobangumi://anime/{id}", |
| 46 | + name="Anime details", |
| 47 | + description="Detailed information about a specific tracked anime by ID", |
| 48 | + mimeType="application/json", |
| 49 | + ), |
| 50 | +] |
| 51 | + |
| 52 | + |
| 53 | +def handle_resource(uri: str) -> str: |
| 54 | + """Return a JSON string for the given MCP resource URI. |
| 55 | +
|
| 56 | + Supported URIs: |
| 57 | + - ``autobangumi://anime/list`` - all tracked anime |
| 58 | + - ``autobangumi://status`` - program version and running state |
| 59 | + - ``autobangumi://rss/feeds`` - configured RSS feeds |
| 60 | + - ``autobangumi://anime/{id}`` - single anime by integer ID |
| 61 | + """ |
| 62 | + if uri == "autobangumi://anime/list": |
| 63 | + with TorrentManager() as manager: |
| 64 | + items = manager.bangumi.search_all() |
| 65 | + return json.dumps([_bangumi_to_dict(b) for b in items], ensure_ascii=False) |
| 66 | + |
| 67 | + elif uri == "autobangumi://status": |
| 68 | + from module.api.program import program |
| 69 | + |
| 70 | + return json.dumps( |
| 71 | + { |
| 72 | + "version": VERSION, |
| 73 | + "running": program.is_running, |
| 74 | + "first_run": program.first_run, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + elif uri == "autobangumi://rss/feeds": |
| 79 | + with RSSEngine() as engine: |
| 80 | + feeds = engine.rss.search_all() |
| 81 | + return json.dumps( |
| 82 | + [ |
| 83 | + { |
| 84 | + "id": f.id, |
| 85 | + "name": f.name, |
| 86 | + "url": f.url, |
| 87 | + "enabled": f.enabled, |
| 88 | + "connection_status": f.connection_status, |
| 89 | + "last_checked_at": f.last_checked_at, |
| 90 | + } |
| 91 | + for f in feeds |
| 92 | + ], |
| 93 | + ensure_ascii=False, |
| 94 | + ) |
| 95 | + |
| 96 | + elif uri.startswith("autobangumi://anime/"): |
| 97 | + anime_id = uri.split("/")[-1] |
| 98 | + try: |
| 99 | + anime_id = int(anime_id) |
| 100 | + except ValueError: |
| 101 | + return json.dumps({"error": f"Invalid anime ID: {anime_id}"}) |
| 102 | + with TorrentManager() as manager: |
| 103 | + result = manager.search_one(anime_id) |
| 104 | + if isinstance(result, Bangumi): |
| 105 | + return json.dumps(_bangumi_to_dict(result), ensure_ascii=False) |
| 106 | + return json.dumps({"error": result.msg_en}) |
| 107 | + |
| 108 | + return json.dumps({"error": f"Unknown resource: {uri}"}) |
0 commit comments