Document MCP tool contracts and structured results
This commit is contained in:
+16
-8
@@ -26,9 +26,20 @@ import smeta_mcp
|
||||
log = logging.getLogger("smeta_web")
|
||||
|
||||
|
||||
def _tool_result(value: Any) -> CallToolResult:
|
||||
payload = json.dumps(value, ensure_ascii=False, default=str)
|
||||
if len(payload) > 1_000_000:
|
||||
raise ValueError("Tool result is too large; narrow the request")
|
||||
is_error = isinstance(value, dict) and "error" in value
|
||||
normalized = json.loads(payload)
|
||||
structured = (normalized if isinstance(normalized, dict) else {"items": normalized}
|
||||
if isinstance(normalized, list) else {"value": normalized})
|
||||
return CallToolResult(content=[TextContent(type="text", text=payload)],
|
||||
structuredContent=structured, isError=is_error)
|
||||
|
||||
|
||||
def _error_result(code: str, detail: str) -> CallToolResult:
|
||||
return CallToolResult(content=[TextContent(type="text", text=json.dumps(
|
||||
{"error": code, "detail": detail}, ensure_ascii=False))], isError=True)
|
||||
return _tool_result({"error": code, "detail": detail})
|
||||
|
||||
|
||||
_NORMS = {
|
||||
@@ -78,7 +89,8 @@ def _bounded_schema(schema: dict) -> dict:
|
||||
def _domain_schema(meta: dict) -> dict:
|
||||
schema = _bounded_schema(meta["inputSchema"])
|
||||
schema["properties"] = {**schema.get("properties", {}),
|
||||
"tab_id": {"type": "string", "maxLength": 128}}
|
||||
"tab_id": {"type": "string", "maxLength": 128,
|
||||
"description": "Собственная вкладка; если не указана, используется вкладка MCP-клиента."}}
|
||||
schema["additionalProperties"] = False
|
||||
return schema
|
||||
|
||||
@@ -192,11 +204,7 @@ class InfosmetaMCP(FastMCP):
|
||||
return smeta_mcp.TOOLS[name]["fn"](**args)
|
||||
|
||||
result = await asyncio.to_thread(run)
|
||||
payload = json.dumps(result, ensure_ascii=False, default=str)
|
||||
if len(payload) > 1_000_000:
|
||||
raise ValueError("Tool result is too large; narrow the request")
|
||||
content = [TextContent(type="text", text=payload)]
|
||||
return CallToolResult(content=content, isError=bool(isinstance(result, dict) and "error" in result))
|
||||
return _tool_result(result)
|
||||
|
||||
|
||||
def create_mcp_app(provider: InfosmetaOAuthProvider, *, allowed_hosts: list[str]):
|
||||
|
||||
Reference in New Issue
Block a user