Spaces:
Running
Running
File size: 14,988 Bytes
db5f035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""A2A compliance validation endpoint β checks agent protocol compliance."""
import logging
import uuid
import httpx
from fastapi import APIRouter, Depends
from src.core.rate_limiter import rate_limit_by_ip
from src.core.url_validator import validate_public_url
from src.schemas.validate import ValidateRequest, ValidationCheck, ValidateResponse
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/agents", tags=["agents"])
@router.post("/validate", response_model=ValidateResponse, dependencies=[Depends(rate_limit_by_ip)])
async def validate_agent(data: ValidateRequest) -> ValidateResponse:
"""Validate an agent's A2A protocol compliance.
Runs a series of checks and returns detailed pass/fail results.
"""
validate_public_url(data.url)
base_url = data.url.rstrip("/")
card_url = base_url + "/.well-known/agent-card.json"
checks: list[ValidationCheck] = []
suggestions: list[str] = []
agent_name: str | None = None
skills_count = 0
card: dict | None = None
first_skill_id: str | None = None
# --- Check 1: agent_card_reachable ---
resp = None
try:
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get(card_url)
if resp.status_code == 200:
checks.append(ValidationCheck(
name="agent_card_reachable",
passed=True,
message=f"Agent card fetched successfully from {card_url}",
severity="error",
))
else:
checks.append(ValidationCheck(
name="agent_card_reachable",
passed=False,
message=f"Agent card returned HTTP {resp.status_code}",
severity="error",
))
except httpx.TimeoutException:
checks.append(ValidationCheck(
name="agent_card_reachable",
passed=False,
message="Agent card request timed out (10s)",
severity="error",
))
except (httpx.ConnectError, httpx.HTTPError) as exc:
checks.append(ValidationCheck(
name="agent_card_reachable",
passed=False,
message=f"Could not reach agent card endpoint: {exc}",
severity="error",
))
if not resp or resp.status_code != 200:
# Can't proceed without a card β return early
return ValidateResponse(
url=base_url,
valid=False,
checks=checks,
agent_name=None,
skills_count=0,
suggestions=["Ensure your agent serves /.well-known/agent-card.json"],
)
# --- Check 2: agent_card_valid_json ---
try:
card = resp.json()
if not isinstance(card, dict):
raise ValueError("Not a JSON object")
checks.append(ValidationCheck(
name="agent_card_valid_json",
passed=True,
message="Agent card is valid JSON",
severity="error",
))
except Exception:
checks.append(ValidationCheck(
name="agent_card_valid_json",
passed=False,
message="Agent card response is not valid JSON",
severity="error",
))
return ValidateResponse(
url=base_url,
valid=False,
checks=checks,
agent_name=None,
skills_count=0,
suggestions=["Agent card must return a valid JSON object"],
)
# --- Check 3: required_fields ---
name = card.get("name")
url_field = card.get("url")
skills_raw = card.get("skills")
missing = []
if not name:
missing.append("name")
if not url_field:
missing.append("url")
if not isinstance(skills_raw, list):
missing.append("skills (array)")
if missing:
checks.append(ValidationCheck(
name="required_fields",
passed=False,
message=f"Missing required fields: {', '.join(missing)}",
severity="error",
))
else:
checks.append(ValidationCheck(
name="required_fields",
passed=True,
message="All required fields present (name, url, skills)",
severity="error",
))
agent_name = name if isinstance(name, str) else None
skills_list = skills_raw if isinstance(skills_raw, list) else []
# --- Check 4: has_description ---
description = card.get("description", "")
if description and isinstance(description, str) and description.strip():
checks.append(ValidationCheck(
name="has_description",
passed=True,
message="Agent has a description",
severity="warning",
))
else:
checks.append(ValidationCheck(
name="has_description",
passed=False,
message="Agent card is missing a description",
severity="warning",
))
suggestions.append("Add a description to your agent card")
# --- Check 5: has_version ---
version = card.get("version")
if version:
checks.append(ValidationCheck(
name="has_version",
passed=True,
message=f"Version: {version}",
severity="warning",
))
else:
checks.append(ValidationCheck(
name="has_version",
passed=False,
message="Agent card is missing a version field",
severity="warning",
))
# --- Check 6: skills_valid ---
valid_skills = 0
invalid_skills = []
for i, s in enumerate(skills_list):
if not isinstance(s, dict):
invalid_skills.append(f"Skill {i}: not a JSON object")
continue
s_id = s.get("id", s.get("skill_key", ""))
s_name = s.get("name", "")
s_desc = s.get("description", "")
s_missing = []
if not s_id:
s_missing.append("id")
if not s_name:
s_missing.append("name")
if not s_desc:
s_missing.append("description")
if s_missing:
invalid_skills.append(
f"Skill '{s_id or s_name or i}': missing {', '.join(s_missing)}"
)
else:
valid_skills += 1
if first_skill_id is None:
first_skill_id = s_id
skills_count = valid_skills
if invalid_skills:
checks.append(ValidationCheck(
name="skills_valid",
passed=False,
message=f"{len(invalid_skills)} skill(s) invalid: {'; '.join(invalid_skills[:3])}",
severity="error",
))
elif valid_skills == 0:
checks.append(ValidationCheck(
name="skills_valid",
passed=False,
message="No valid skills found",
severity="error",
))
else:
checks.append(ValidationCheck(
name="skills_valid",
passed=True,
message=f"{valid_skills} skill(s) valid",
severity="error",
))
# --- Check 7: skills_have_examples ---
has_examples = any(
isinstance(s, dict) and s.get("examples")
for s in skills_list
)
if has_examples:
checks.append(ValidationCheck(
name="skills_have_examples",
passed=True,
message="At least one skill has examples",
severity="info",
))
else:
checks.append(ValidationCheck(
name="skills_have_examples",
passed=False,
message="No skills have examples",
severity="info",
))
suggestions.append(
"Add examples to your skills \u2014 they improve discoverability "
"and help users understand your agent"
)
# --- Check 8: has_pricing ---
pricing = card.get("pricing")
if pricing:
checks.append(ValidationCheck(
name="has_pricing",
passed=True,
message="Pricing info present",
severity="warning",
))
else:
checks.append(ValidationCheck(
name="has_pricing",
passed=False,
message="No pricing information in agent card",
severity="warning",
))
suggestions.append("Add pricing info so users know the cost upfront")
# --- Check 9: jsonrpc_reachable ---
# Only attempt if we found at least one valid skill
rpc_resp = None
rpc_data = None
if first_skill_id:
rpc_payload = {
"jsonrpc": "2.0",
"id": "validation-test",
"method": "tasks/send",
"params": {
"id": f"validate-{uuid.uuid4().hex[:12]}",
"message": {
"role": "user",
"parts": [{"type": "text", "content": "Hello, this is a validation test."}],
},
"metadata": {"skill_id": first_skill_id},
},
}
try:
async with httpx.AsyncClient(timeout=10.0) as client:
rpc_resp = await client.post(base_url, json=rpc_payload)
if rpc_resp.status_code == 200:
checks.append(ValidationCheck(
name="jsonrpc_reachable",
passed=True,
message="JSON-RPC endpoint responded successfully",
severity="error",
))
else:
checks.append(ValidationCheck(
name="jsonrpc_reachable",
passed=False,
message=f"JSON-RPC endpoint returned HTTP {rpc_resp.status_code}",
severity="error",
))
except httpx.TimeoutException:
checks.append(ValidationCheck(
name="jsonrpc_reachable",
passed=False,
message="JSON-RPC request timed out (30s)",
severity="error",
))
except (httpx.ConnectError, httpx.HTTPError) as exc:
checks.append(ValidationCheck(
name="jsonrpc_reachable",
passed=False,
message=f"Could not reach JSON-RPC endpoint: {exc}",
severity="error",
))
else:
checks.append(ValidationCheck(
name="jsonrpc_reachable",
passed=False,
message="Skipped β no valid skills found to test",
severity="error",
))
# --- Check 10: jsonrpc_response_format ---
if rpc_resp and rpc_resp.status_code == 200:
try:
rpc_data = rpc_resp.json()
has_jsonrpc = rpc_data.get("jsonrpc") == "2.0"
has_id = "id" in rpc_data
has_result_or_error = "result" in rpc_data or "error" in rpc_data
if has_jsonrpc and has_id and has_result_or_error:
checks.append(ValidationCheck(
name="jsonrpc_response_format",
passed=True,
message="Response follows JSON-RPC 2.0 format",
severity="error",
))
else:
fmt_missing = []
if not has_jsonrpc:
fmt_missing.append("jsonrpc: '2.0'")
if not has_id:
fmt_missing.append("id")
if not has_result_or_error:
fmt_missing.append("result or error")
checks.append(ValidationCheck(
name="jsonrpc_response_format",
passed=False,
message=f"Response missing: {', '.join(fmt_missing)}",
severity="error",
))
except Exception:
checks.append(ValidationCheck(
name="jsonrpc_response_format",
passed=False,
message="JSON-RPC response is not valid JSON",
severity="error",
))
else:
checks.append(ValidationCheck(
name="jsonrpc_response_format",
passed=False,
message="Skipped β JSON-RPC endpoint not reachable",
severity="error",
))
# --- Check 11: task_completed ---
task_state = None
if rpc_data and "result" in rpc_data:
result = rpc_data["result"]
status = result.get("status", {}) if isinstance(result, dict) else {}
task_state = status.get("state") if isinstance(status, dict) else None
if task_state == "completed":
checks.append(ValidationCheck(
name="task_completed",
passed=True,
message="Test task completed successfully",
severity="warning",
))
else:
checks.append(ValidationCheck(
name="task_completed",
passed=False,
message=f"Task state: {task_state or 'unknown'} (expected 'completed')",
severity="warning",
))
suggestions.append(
"Your agent returned a failed status. "
"Check your LLM API key and error handling"
)
else:
checks.append(ValidationCheck(
name="task_completed",
passed=False,
message="Skipped β no valid JSON-RPC result",
severity="warning",
))
# --- Check 12: has_artifacts ---
if rpc_data and "result" in rpc_data:
result = rpc_data["result"]
artifacts = result.get("artifacts", []) if isinstance(result, dict) else []
if artifacts and isinstance(artifacts, list) and len(artifacts) > 0:
checks.append(ValidationCheck(
name="has_artifacts",
passed=True,
message=f"Task returned {len(artifacts)} artifact(s)",
severity="warning",
))
else:
checks.append(ValidationCheck(
name="has_artifacts",
passed=False,
message="Task did not return any artifacts",
severity="warning",
))
suggestions.append(
"Successful tasks should include at least one artifact with the result"
)
else:
checks.append(ValidationCheck(
name="has_artifacts",
passed=False,
message="Skipped β no valid JSON-RPC result",
severity="warning",
))
# Determine overall validity: all error-severity checks must pass
valid = all(c.passed for c in checks if c.severity == "error")
return ValidateResponse(
url=base_url,
valid=valid,
checks=checks,
agent_name=agent_name,
skills_count=skills_count,
suggestions=suggestions,
)
|