|
| 1 | +"""MCP resources for project info and settings.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +from fastmcp import Context, FastMCP |
| 8 | + |
| 9 | +COMMON_SETTINGS = [ |
| 10 | + "application/config/name", |
| 11 | + "application/config/description", |
| 12 | + "application/run/main_scene", |
| 13 | + "display/window/size/viewport_width", |
| 14 | + "display/window/size/viewport_height", |
| 15 | + "rendering/renderer/rendering_method", |
| 16 | + "physics/2d/default_gravity", |
| 17 | + "physics/3d/default_gravity", |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def register_project_resources(mcp: FastMCP) -> None: |
| 22 | + @mcp.resource("godot://project/info", mime_type="application/json") |
| 23 | + async def get_project_info(ctx: Context) -> str: |
| 24 | + """Project name, Godot version, paths, and play state.""" |
| 25 | + app = ctx.lifespan_context |
| 26 | + session = app.registry.get_active() |
| 27 | + if session is None: |
| 28 | + return json.dumps({"error": "No active Godot session", "connected": False}) |
| 29 | + |
| 30 | + info = session.to_dict() |
| 31 | + info.pop("connected_at", None) |
| 32 | + return json.dumps(info) |
| 33 | + |
| 34 | + @mcp.resource("godot://project/settings", mime_type="application/json") |
| 35 | + async def get_project_settings(ctx: Context) -> str: |
| 36 | + """Common project settings subset (display, physics, rendering).""" |
| 37 | + app = ctx.lifespan_context |
| 38 | + settings = {} |
| 39 | + errors = [] |
| 40 | + for key in COMMON_SETTINGS: |
| 41 | + try: |
| 42 | + result = await app.client.send("get_project_setting", {"key": key}) |
| 43 | + settings[key] = result.get("value") |
| 44 | + except Exception as e: |
| 45 | + errors.append({"key": key, "error": str(e)}) |
| 46 | + |
| 47 | + return json.dumps({"settings": settings, "errors": errors if errors else None}) |
0 commit comments