"""Zero-dependency stdio MCP Server exposing GPU-x402 tools to AI agents.""" import json import os import sys from typing import Any from .client import GPUx402Client, PaymentRequiredError DEFAULT_BASE_URL = os.getenv("GPU_X402_URL", "https://agentx402.fyi") TOOLS = [ { "name": "web_to_markdown", "description": "Fast (<500ms) URL/HTML to clean LLM-ready Markdown + SEO metadata & links ($0.005 USDC via x402; 5 free calls/day).", "inputSchema": { "type": "object", "properties": { "url": {"type": "string", "description": "Public webpage URL"}, "html": {"type": "string", "description": "Optional raw HTML instead of URL"}, "max_chars": {"type": "integer", "default": 24000}, "include_links": {"type": "boolean", "default": True}, "tx_hash": {"type": "string"}, }, }, }, { "name": "inspect_evm_wallet", "description": "Multi-chain Base + Polygon EVM wallet inspector: ETH/POL, USDC balance, tx nonce, and contract check ($0.01 USDC).", "inputSchema": { "type": "object", "required": ["address"], "properties": { "address": {"type": "string", "description": "0x... EVM wallet or contract address"}, "chains": {"type": "array", "items": {"type": "string"}, "default": ["base", "polygon"]}, "tx_hash": {"type": "string"}, }, }, }, { "name": "web_extract_json", "description": "Fetch any web URL (or HTML) and extract clean structured JSON using GPU LLM ($0.02 USDC via x402; 5 free calls/day).", "inputSchema": { "type": "object", "properties": { "url": {"type": "string", "description": "Target webpage URL"}, "html": {"type": "string", "description": "Optional raw HTML instead of URL"}, "schema_hint": {"type": "string", "description": "Desired JSON fields/instructions"}, "tx_hash": {"type": "string", "description": "Optional Base/Polygon USDC tx hash for x402 payment"}, }, }, }, { "name": "vision_analyze_json", "description": "Analyze an image (URL or base64) for OCR, invoices, UI audit, or product data as structured JSON ($0.05 USDC).", "inputSchema": { "type": "object", "properties": { "image_url": {"type": "string"}, "image_base64": {"type": "string"}, "prompt": {"type": "string"}, "tx_hash": {"type": "string"}, }, }, }, { "name": "code_security_audit", "description": "Deep security & bug audit of code or git diffs returning structured findings + fixes ($0.05 USDC).", "inputSchema": { "type": "object", "required": ["code"], "properties": { "code": {"type": "string"}, "language": {"type": "string"}, "focus": {"type": "string"}, "tx_hash": {"type": "string"}, }, }, }, { "name": "flux_generate_image", "description": "Generate a photorealistic image via Flux.2-Klein-4B BF16 on dedicated RTX 3060 ($0.15 USDC).", "inputSchema": { "type": "object", "required": ["prompt"], "properties": { "prompt": {"type": "string"}, "width": {"type": "integer", "default": 1024}, "height": {"type": "integer", "default": 1024}, "steps": {"type": "integer", "default": 4}, "tx_hash": {"type": "string"}, }, }, }, { "name": "nvenc_render_reel", "description": "Render a 1080x1920 vertical H.264 Reel/Short MP4 with smooth zoom & captions ($0.25 USDC).", "inputSchema": { "type": "object", "required": ["headline"], "properties": { "headline": {"type": "string"}, "subheadline": {"type": "string"}, "image_url": {"type": "string"}, "duration_sec": {"type": "integer", "default": 8}, "tx_hash": {"type": "string"}, }, }, }, { "name": "pdf_extract_json", "description": "Extract invoices, contracts, or reports from a PDF URL/base64 into strict JSON ($0.05 USDC).", "inputSchema": { "type": "object", "properties": { "pdf_url": {"type": "string"}, "pdf_base64": {"type": "string"}, "schema_hint": {"type": "string"}, "tx_hash": {"type": "string"}, }, }, }, { "name": "translate_json", "description": "Translate nested JSON/i18n string values into any target language preserving 100% of keys & types ($0.02 USDC).", "inputSchema": { "type": "object", "required": ["data", "target_language"], "properties": { "data": {"type": "object"}, "target_language": {"type": "string"}, "tone": {"type": "string"}, "tx_hash": {"type": "string"}, }, }, }, { "name": "batch_extract_urls", "description": "Extract & compare structured JSON across up to 5 URLs in parallel ($0.10 USDC).", "inputSchema": { "type": "object", "required": ["urls"], "properties": { "urls": {"type": "array", "items": {"type": "string"}}, "schema_hint": {"type": "string"}, "tx_hash": {"type": "string"}, }, }, }, { "name": "virtual_staging_image", "description": "Generate luxury real estate virtual staging or commercial product studio scenes on Flux.2 BF16 ($0.35 USDC).", "inputSchema": { "type": "object", "required": ["room_or_subject"], "properties": { "room_or_subject": {"type": "string"}, "style": {"type": "string", "default": "modern_luxury"}, "tx_hash": {"type": "string"}, }, }, }, ] def _send(msg: dict[str, Any]) -> None: sys.stdout.write(json.dumps(msg) + "\n") sys.stdout.flush() def main() -> None: client = GPUx402Client(base_url=os.getenv("GPU_X402_URL", DEFAULT_BASE_URL)) for line in sys.stdin: line = line.strip() if not line: continue try: req = json.loads(line) except json.JSONDecodeError: continue req_id = req.get("id") method = req.get("method") params = req.get("params", {}) if method == "initialize": _send( { "jsonrpc": "2.0", "id": req_id, "result": { "protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "gpu-x402-mcp", "version": "1.0.0"}, }, } ) elif method == "tools/list": _send({"jsonrpc": "2.0", "id": req_id, "result": {"tools": TOOLS}}) elif method == "tools/call": name = params.get("name") args = params.get("arguments", {}) try: if name == "web_to_markdown": out = client.web_markdown( url=args.get("url"), html=args.get("html"), max_chars=int(args.get("max_chars", 24000)), include_links=bool(args.get("include_links", True)), tx_hash=args.get("tx_hash"), ) elif name == "inspect_evm_wallet": out = client.chain_wallet( address=args["address"], chains=args.get("chains"), tx_hash=args.get("tx_hash"), ) elif name == "web_extract_json": out = client.extract( url=args.get("url"), html=args.get("html"), schema_hint=args.get("schema_hint", "Extract structured JSON."), tx_hash=args.get("tx_hash"), ) elif name == "vision_analyze_json": out = client.vision( image_url=args.get("image_url"), image_base64=args.get("image_base64"), prompt=args.get("prompt", "Analyze image as JSON."), tx_hash=args.get("tx_hash"), ) elif name == "code_security_audit": out = client.code_audit( code=args["code"], language=args.get("language", "auto"), focus=args.get("focus", "security, bugs, edge cases"), tx_hash=args.get("tx_hash"), ) elif name == "flux_generate_image": out = client.generate_image( prompt=args["prompt"], width=int(args.get("width", 1024)), height=int(args.get("height", 1024)), steps=int(args.get("steps", 4)), tx_hash=args.get("tx_hash"), ) elif name == "nvenc_render_reel": out = client.render_reel( headline=args["headline"], subheadline=args.get("subheadline", ""), image_url=args.get("image_url"), duration_sec=int(args.get("duration_sec", 8)), tx_hash=args.get("tx_hash"), ) elif name == "pdf_extract_json": out = client.pdf_extract( pdf_url=args.get("pdf_url"), pdf_base64=args.get("pdf_base64"), schema_hint=args.get("schema_hint", "Extract structured JSON."), tx_hash=args.get("tx_hash"), ) elif name == "translate_json": out = client.translate_json( data=args["data"], target_language=args["target_language"], tone=args.get("tone", "natural, professional"), tx_hash=args.get("tx_hash"), ) elif name == "batch_extract_urls": out = client.batch_extract( urls=args["urls"], schema_hint=args.get("schema_hint", "Extract comparative JSON."), tx_hash=args.get("tx_hash"), ) elif name == "virtual_staging_image": out = client.virtual_stage( room_or_subject=args["room_or_subject"], style=args.get("style", "modern_luxury"), tx_hash=args.get("tx_hash"), ) else: raise ValueError(f"Unknown tool: {name}") _send( { "jsonrpc": "2.0", "id": req_id, "result": {"content": [{"type": "text", "text": json.dumps(out, indent=2)}]}, } ) except PaymentRequiredError as pe: _send( { "jsonrpc": "2.0", "id": req_id, "result": { "isError": True, "content": [{"type": "text", "text": json.dumps(pe.payload, indent=2)}], }, } ) except Exception as exc: _send( { "jsonrpc": "2.0", "id": req_id, "result": { "isError": True, "content": [{"type": "text", "text": f"Error: {exc}"}], }, } ) if __name__ == "__main__": main()