RelayShield commited on
Commit ·
18cb2e2
1
Parent(s): 6eea131
Add smolagents Tool integration for all 4 Agentic Attack Surface endpoints
Browse files- relayshield_smolagents_tool.py +242 -0
relayshield_smolagents_tool.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""RelayShield security tools for smolagents.
|
| 2 |
+
|
| 3 |
+
Four agent-specific security checks from RelayShield (https://api.relayshield.net/developers),
|
| 4 |
+
a live threat-intelligence API. Mirrors the same four tools already shipped as the
|
| 5 |
+
"RelayShield Agentic Attack Surface" MCP server on Hugging Face Spaces
|
| 6 |
+
(https://huggingface.co/spaces/relayshieldadmin/relayshield-agentic-attack-surface)
|
| 7 |
+
and as a CrewAI tools PR (crewAIInc/crewAI#6550, first two tools only — this module
|
| 8 |
+
covers all four, since it isn't constrained by that PR's earlier partial scope).
|
| 9 |
+
|
| 10 |
+
Setup:
|
| 11 |
+
pip install smolagents requests
|
| 12 |
+
export RELAYSHIELD_API_KEY="your-key-here"
|
| 13 |
+
|
| 14 |
+
Usage:
|
| 15 |
+
from relayshield_smolagents_tool import RelayShieldMCPRiskTool
|
| 16 |
+
|
| 17 |
+
tool = RelayShieldMCPRiskTool()
|
| 18 |
+
result = tool.forward(server_url="https://example.com/mcp")
|
| 19 |
+
|
| 20 |
+
Get a key at https://api.relayshield.net/developers ($499/mo for 10,000 calls,
|
| 21 |
+
or pay-per-call via x402 USDC with no key required for the same four checks —
|
| 22 |
+
see the /v1/payg/* routes in the docs).
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
import os
|
| 26 |
+
|
| 27 |
+
import requests
|
| 28 |
+
from smolagents import Tool
|
| 29 |
+
|
| 30 |
+
API_BASE_URL: str = "https://api.relayshield.net"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _relayshield_headers(api_key: str) -> dict[str, str]:
|
| 34 |
+
return {"Content-Type": "application/json", "X-RS-API-KEY": api_key}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _get_api_key() -> str | None:
|
| 38 |
+
return os.environ.get("RELAYSHIELD_API_KEY") or None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _post(path: str, payload: dict) -> tuple[dict | None, str | None]:
|
| 42 |
+
"""POSTs to a RelayShield metered endpoint. Returns (data, error_message).
|
| 43 |
+
Every RelayShield response is wrapped as {"ok": bool, "data": {...}} — a
|
| 44 |
+
valid-but-non-dict JSON body (list/string) is treated as malformed rather
|
| 45 |
+
than crashing on .get(), same class of bug CodeRabbit caught in the
|
| 46 |
+
CrewAI PR's first review pass."""
|
| 47 |
+
api_key = _get_api_key()
|
| 48 |
+
if not api_key:
|
| 49 |
+
return None, "RELAYSHIELD_API_KEY environment variable is required. Get a key at https://api.relayshield.net/developers"
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
resp = requests.post(
|
| 53 |
+
f"{API_BASE_URL}{path}",
|
| 54 |
+
json=payload,
|
| 55 |
+
headers=_relayshield_headers(api_key),
|
| 56 |
+
timeout=15,
|
| 57 |
+
)
|
| 58 |
+
resp.raise_for_status()
|
| 59 |
+
response_json = resp.json()
|
| 60 |
+
except requests.RequestException as exc:
|
| 61 |
+
return None, f"RelayShield API call to {path} failed: {exc}"
|
| 62 |
+
|
| 63 |
+
if not isinstance(response_json, dict):
|
| 64 |
+
return None, f"RelayShield API call to {path} returned an unexpected response shape."
|
| 65 |
+
return response_json.get("data", {}), None
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class RelayShieldMCPRiskTool(Tool):
|
| 69 |
+
"""Typosquat / reputation / registration-age risk check for MCP servers
|
| 70 |
+
and agent tool registries, backed by RelayShield's live threat-intel API."""
|
| 71 |
+
|
| 72 |
+
name = "relayshield_mcp_registry_risk"
|
| 73 |
+
description = (
|
| 74 |
+
"Checks an MCP server URL or package name for typosquat risk against known MCP "
|
| 75 |
+
"ecosystem domains, presence in RelayShield's criminal IOC corpus, and domain-"
|
| 76 |
+
"registration age. Use this before connecting an agent to an unfamiliar MCP server "
|
| 77 |
+
"or tool registry. Returns a verdict (CRITICAL/HIGH/MEDIUM/LOW) with findings."
|
| 78 |
+
)
|
| 79 |
+
inputs = {
|
| 80 |
+
"server_url": {
|
| 81 |
+
"type": "string",
|
| 82 |
+
"description": "Full URL of the MCP server to check, e.g. 'https://example.com/mcp'. Provide this or package_name.",
|
| 83 |
+
"nullable": True,
|
| 84 |
+
},
|
| 85 |
+
"package_name": {
|
| 86 |
+
"type": "string",
|
| 87 |
+
"description": "Package name of the MCP server if no server_url is available. Checks are more limited without a server_url.",
|
| 88 |
+
"nullable": True,
|
| 89 |
+
},
|
| 90 |
+
}
|
| 91 |
+
output_type = "string"
|
| 92 |
+
|
| 93 |
+
def forward(self, server_url: str | None = None, package_name: str | None = None) -> str:
|
| 94 |
+
if not server_url and not package_name:
|
| 95 |
+
return "Error: provide either server_url or package_name."
|
| 96 |
+
|
| 97 |
+
payload: dict[str, str] = {}
|
| 98 |
+
if server_url:
|
| 99 |
+
payload["server_url"] = server_url
|
| 100 |
+
if package_name:
|
| 101 |
+
payload["package_name"] = package_name
|
| 102 |
+
|
| 103 |
+
data, error = _post("/v1/metered/mcp-registry-risk", payload)
|
| 104 |
+
if error:
|
| 105 |
+
return error
|
| 106 |
+
|
| 107 |
+
verdict = data.get("verdict", "UNKNOWN")
|
| 108 |
+
findings = data.get("findings", [])
|
| 109 |
+
if not findings:
|
| 110 |
+
return f"Verdict: {verdict}. No red flags found. {data.get('note', '')}"
|
| 111 |
+
|
| 112 |
+
lines = [f"Verdict: {verdict}", "Findings:"]
|
| 113 |
+
for f in findings:
|
| 114 |
+
lines.append(f"- [{f.get('severity')}] {f.get('type')}: {f.get('detail')}")
|
| 115 |
+
return "\n".join(lines)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class RelayShieldPromptInjectionBreachTool(Tool):
|
| 119 |
+
"""Checks whether an email's credentials were exposed via a breach specifically
|
| 120 |
+
sourced from a prompt-injection attack against an AI agent, as opposed to
|
| 121 |
+
traditional phishing/malware-sourced breaches."""
|
| 122 |
+
|
| 123 |
+
name = "relayshield_prompt_injection_breach"
|
| 124 |
+
description = (
|
| 125 |
+
"Checks an email address for credential exposure sourced specifically from "
|
| 126 |
+
"prompt-injection attacks against AI agents (distinct from ordinary breach/phishing "
|
| 127 |
+
"sources). Use this to vet an agent identity or user account before granting it "
|
| 128 |
+
"elevated trust or access."
|
| 129 |
+
)
|
| 130 |
+
inputs = {
|
| 131 |
+
"email": {
|
| 132 |
+
"type": "string",
|
| 133 |
+
"description": "Email address to check for credential exposure sourced from prompt-injection attacks against AI agents.",
|
| 134 |
+
},
|
| 135 |
+
}
|
| 136 |
+
output_type = "string"
|
| 137 |
+
|
| 138 |
+
def forward(self, email: str) -> str:
|
| 139 |
+
data, error = _post("/v1/metered/prompt-injection-breach", {"email": email})
|
| 140 |
+
if error:
|
| 141 |
+
return error
|
| 142 |
+
|
| 143 |
+
if not data.get("found"):
|
| 144 |
+
return f"No prompt-injection-sourced exposure found for {email}. {data.get('note', '')}"
|
| 145 |
+
|
| 146 |
+
count = data.get("session_count", 0)
|
| 147 |
+
return (
|
| 148 |
+
f"Found {count} session(s) exposed via prompt-injection-sourced breach for {email}. "
|
| 149 |
+
"Treat this identity as compromised until credentials are rotated."
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
class RelayShieldTechStackCVETool(Tool):
|
| 154 |
+
"""Checks a declared technology stack (or a domain's stored stack) against
|
| 155 |
+
actively-exploited CVEs and high-EPSS-score vulnerabilities."""
|
| 156 |
+
|
| 157 |
+
name = "relayshield_tech_stack_cve"
|
| 158 |
+
description = (
|
| 159 |
+
"Checks a declared technology stack (e.g. nginx, WordPress, LangChain, CrewAI, n8n) "
|
| 160 |
+
"against actively-exploited CVEs (CISA KEV) and high-EPSS-score vulnerabilities. Covers "
|
| 161 |
+
"AI agent orchestration frameworks and their common companion infrastructure. Use this "
|
| 162 |
+
"before deploying or continuing to run a given technology stack in production."
|
| 163 |
+
)
|
| 164 |
+
inputs = {
|
| 165 |
+
"tech_stack": {
|
| 166 |
+
"type": "array",
|
| 167 |
+
"description": "List of declared technology product names, e.g. ['nginx', 'wordpress', 'cisco ios']. Provide this or domain.",
|
| 168 |
+
"nullable": True,
|
| 169 |
+
},
|
| 170 |
+
"domain": {
|
| 171 |
+
"type": "string",
|
| 172 |
+
"description": "Alternative: pull the declared tech_stack from a domain's stored profile instead of listing it inline.",
|
| 173 |
+
"nullable": True,
|
| 174 |
+
},
|
| 175 |
+
}
|
| 176 |
+
output_type = "string"
|
| 177 |
+
|
| 178 |
+
def forward(self, tech_stack: list[str] | None = None, domain: str | None = None) -> str:
|
| 179 |
+
if not tech_stack and not domain:
|
| 180 |
+
return "Error: provide either tech_stack or domain."
|
| 181 |
+
|
| 182 |
+
payload: dict = {}
|
| 183 |
+
if tech_stack:
|
| 184 |
+
payload["tech_stack"] = tech_stack
|
| 185 |
+
if domain:
|
| 186 |
+
payload["domain"] = domain
|
| 187 |
+
|
| 188 |
+
data, error = _post("/v1/metered/tech-stack-cve", payload)
|
| 189 |
+
if error:
|
| 190 |
+
return error
|
| 191 |
+
|
| 192 |
+
matched = data.get("matched_cves", [])
|
| 193 |
+
critical = data.get("critical_count", 0)
|
| 194 |
+
if not matched:
|
| 195 |
+
return f"No actively-exploited CVEs matched for {data.get('tech_stack_queried', tech_stack or domain)}."
|
| 196 |
+
|
| 197 |
+
lines = [f"{len(matched)} matched CVE(s), {critical} critical:"]
|
| 198 |
+
for cve in matched:
|
| 199 |
+
lines.append(f"- {cve.get('cve_id', '?')}: {cve.get('description', '')}")
|
| 200 |
+
return "\n".join(lines)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
class RelayShieldBulkIdentityRiskTool(Tool):
|
| 204 |
+
"""Scores up to 10 organizational domains, each with up to 5 associated agent/
|
| 205 |
+
employee emails, for combined breach/infostealer/session/CVE risk in one call."""
|
| 206 |
+
|
| 207 |
+
name = "relayshield_bulk_identity_risk"
|
| 208 |
+
description = (
|
| 209 |
+
"Scores up to 10 organizational domains, each with up to 5 associated agent or "
|
| 210 |
+
"employee emails, for combined breach/infostealer/session/CVE risk in one call. Built "
|
| 211 |
+
"for AI agent governance and identity-posture use cases — the entry point for scoring "
|
| 212 |
+
"many identities per organization in one pass rather than N sequential calls."
|
| 213 |
+
)
|
| 214 |
+
inputs = {
|
| 215 |
+
"targets": {
|
| 216 |
+
"type": "array",
|
| 217 |
+
"description": (
|
| 218 |
+
"Up to 10 objects, each shaped like "
|
| 219 |
+
"{'domain': 'acme.com', 'agents': ['ceo@acme.com']} — 'agents' is optional, up to 5 per domain."
|
| 220 |
+
),
|
| 221 |
+
},
|
| 222 |
+
}
|
| 223 |
+
output_type = "string"
|
| 224 |
+
|
| 225 |
+
def forward(self, targets: list[dict]) -> str:
|
| 226 |
+
data, error = _post("/v1/metered/bulk-identity-risk", {"targets": targets})
|
| 227 |
+
if error:
|
| 228 |
+
return error
|
| 229 |
+
|
| 230 |
+
results = data.get("results", [])
|
| 231 |
+
critical = data.get("critical_count", 0)
|
| 232 |
+
high = data.get("high_count", 0)
|
| 233 |
+
if not results:
|
| 234 |
+
return "No results returned."
|
| 235 |
+
|
| 236 |
+
lines = [f"Queried {data.get('queried', len(results))} domain(s) — {critical} critical, {high} high:"]
|
| 237 |
+
for r in results:
|
| 238 |
+
lines.append(
|
| 239 |
+
f"- {r.get('domain')}: score {r.get('domain_score')} ({r.get('domain_grade')}), "
|
| 240 |
+
f"{r.get('agent_count', 0)} agent(s) checked"
|
| 241 |
+
)
|
| 242 |
+
return "\n".join(lines)
|