|
| 1 | +"""Shared tool-metadata helpers for MCP registration. |
| 2 | +
|
| 3 | +Glama's tool score is dominated by three fields most of our handlers |
| 4 | +were missing: |
| 5 | +
|
| 6 | +1. ``title`` — human-readable name shown in tool lists. |
| 7 | +2. ``output_schema`` — declared return-shape JSON Schema. Lets callers |
| 8 | + validate responses + enables type-aware completion in the client. |
| 9 | +3. ``annotations`` — ``readOnlyHint`` / ``destructiveHint`` / |
| 10 | + ``idempotentHint`` / ``openWorldHint`` (spec: MCP 2024-11-05+). |
| 11 | +
|
| 12 | +Every handler schema dict may carry these keys; ``tool_kwargs()`` pulls |
| 13 | +whichever are present and returns a kwargs mapping ready to hand to |
| 14 | +``mcp.tool(**...)``. The helper tolerates missing keys so the upgrade |
| 15 | +is incremental — handlers that have been refreshed light up with the |
| 16 | +full metadata; older handlers keep their existing description-only |
| 17 | +registration until they're touched. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +# Named annotation presets so every handler converges on the same |
| 25 | +# semantics. Presets name the CAPABILITY, not the handler. |
| 26 | + |
| 27 | +# Pure read. Safe to call repeatedly. No state change. |
| 28 | +READ_ONLY: dict[str, Any] = { |
| 29 | + "readOnlyHint": True, |
| 30 | + "destructiveHint": False, |
| 31 | + "idempotentHint": True, |
| 32 | + "openWorldHint": False, |
| 33 | +} |
| 34 | + |
| 35 | +# Reads and produces new state, but running twice has the same effect |
| 36 | +# as running once (e.g. storing a memory that dedups / merges). |
| 37 | +IDEMPOTENT_WRITE: dict[str, Any] = { |
| 38 | + "readOnlyHint": False, |
| 39 | + "destructiveHint": False, |
| 40 | + "idempotentHint": True, |
| 41 | + "openWorldHint": False, |
| 42 | +} |
| 43 | + |
| 44 | +# Writes new state on every call; subsequent calls produce new rows. |
| 45 | +NON_IDEMPOTENT_WRITE: dict[str, Any] = { |
| 46 | + "readOnlyHint": False, |
| 47 | + "destructiveHint": False, |
| 48 | + "idempotentHint": False, |
| 49 | + "openWorldHint": False, |
| 50 | +} |
| 51 | + |
| 52 | +# Mutates or removes existing state in a way that can't be undone |
| 53 | +# without data loss. |
| 54 | +DESTRUCTIVE: dict[str, Any] = { |
| 55 | + "readOnlyHint": False, |
| 56 | + "destructiveHint": True, |
| 57 | + "idempotentHint": True, |
| 58 | + "openWorldHint": False, |
| 59 | +} |
| 60 | + |
| 61 | +# Read-only but reaches to external state (browser, subprocess, |
| 62 | +# filesystem outside our DB). |
| 63 | +READ_ONLY_EXTERNAL: dict[str, Any] = { |
| 64 | + "readOnlyHint": True, |
| 65 | + "destructiveHint": False, |
| 66 | + "idempotentHint": True, |
| 67 | + "openWorldHint": True, |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +def tool_kwargs(schema: dict[str, Any]) -> dict[str, Any]: |
| 72 | + """Extract mcp.tool(**kwargs) from a handler schema dict. |
| 73 | +
|
| 74 | + Returns the keys FastMCP accepts: ``description``, ``title``, |
| 75 | + ``output_schema``, ``annotations``, ``tags``. Unknown keys are |
| 76 | + ignored so handlers can carry arbitrary auxiliary metadata. |
| 77 | + """ |
| 78 | + out: dict[str, Any] = {} |
| 79 | + if "description" in schema: |
| 80 | + out["description"] = schema["description"] |
| 81 | + if "title" in schema: |
| 82 | + out["title"] = schema["title"] |
| 83 | + # Support both ``output_schema`` (snake) and ``outputSchema`` (camel). |
| 84 | + if "output_schema" in schema: |
| 85 | + out["output_schema"] = schema["output_schema"] |
| 86 | + elif "outputSchema" in schema: |
| 87 | + out["output_schema"] = schema["outputSchema"] |
| 88 | + if "annotations" in schema: |
| 89 | + out["annotations"] = schema["annotations"] |
| 90 | + if "tags" in schema: |
| 91 | + out["tags"] = schema["tags"] |
| 92 | + return out |
0 commit comments