#!/usr/bin/env python3 """ OnlinePrint.com — Zero-Dependency Model Context Protocol (MCP) Server Allows Claude, Cursor, Antigravity, and other MCP clients to quote, upload PDFs, and dispatch physical FedEx 2Day, FedEx Overnight, and USPS envelopes via OnlinePrint.com. """ import base64 import json import os import pathlib import sys import urllib.request API_BASE = os.environ.get("ONLINEPRINT_API_BASE", "https://onlineprint.com").rstrip("/") def http_json(method: str, path: str, payload: dict = None) -> dict: data = json.dumps(payload).encode("utf-8") if payload is not None else None req = urllib.request.Request( f"{API_BASE}{path}", data=data, headers={"Content-Type": "application/json", "User-Agent": "OnlinePrint-MCP/1.0"}, method=method, ) with urllib.request.urlopen(req, timeout=30) as r: return json.loads(r.read().decode("utf-8")) TOOLS = [ { "name": "onlineprint_quote", "description": "Get live pricing to physically print a PDF (Color or B&W) and deliver it via FedEx 2Day, FedEx Overnight, or USPS (supports P.O. Boxes).", "inputSchema": { "type": "object", "properties": { "page_count": {"type": "integer", "description": "Number of PDF pages (1-600)."}, "color": {"type": "boolean", "default": True}, "duplex": {"type": "boolean", "default": False}, "signature_required": {"type": "boolean", "default": False}, "to_address_line1": {"type": "string", "default": ""}, "to_state": {"type": "string", "default": ""}, }, "required": ["page_count"], }, }, { "name": "onlineprint_send_pdf", "description": "Upload a local PDF file to OnlinePrint.com for physical FedEx or USPS delivery. Returns an instant Square Apple Pay / Card checkout_url for the user to approve payment.", "inputSchema": { "type": "object", "properties": { "pdf_path": {"type": "string", "description": "Absolute local path to the PDF file."}, "service_key": { "type": "string", "enum": ["fedex_2day", "fedex_standard_overnight", "fedex_priority_overnight", "usps_priority", "usps_first_class"], "default": "fedex_2day", }, "color": {"type": "boolean", "default": True}, "duplex": {"type": "boolean", "default": False}, "signature_required": {"type": "boolean", "default": False}, "customer_email": {"type": "string"}, "from_name": {"type": "string"}, "from_company": {"type": "string", "default": ""}, "from_addr1": {"type": "string"}, "from_city": {"type": "string"}, "from_state": {"type": "string"}, "from_zip": {"type": "string"}, "to_name": {"type": "string"}, "to_company": {"type": "string", "default": ""}, "to_addr1": {"type": "string"}, "to_addr2": {"type": "string", "default": ""}, "to_city": {"type": "string"}, "to_state": {"type": "string"}, "to_zip": {"type": "string"}, "to_phone": {"type": "string", "default": ""}, }, "required": [ "pdf_path", "customer_email", "from_name", "from_addr1", "from_city", "from_state", "from_zip", "to_name", "to_addr1", "to_city", "to_state", "to_zip", ], }, }, { "name": "onlineprint_order_status", "description": "Check payment, print queue, and live FedEx/USPS tracking status for an OnlinePrint.com order.", "inputSchema": { "type": "object", "properties": { "public_token": {"type": "string", "description": "The public_token returned when creating the order."} }, "required": ["public_token"], }, }, ] def handle_call(name: str, args: dict) -> dict: if name == "onlineprint_quote": return http_json("POST", "/api/v1/quote", args) if name == "onlineprint_send_pdf": p = pathlib.Path(args["pdf_path"]) if not p.exists(): raise FileNotFoundError(f"PDF not found: {p}") payload = {k: v for k, v in args.items() if k != "pdf_path"} payload["filename"] = p.name payload["pdf_base64"] = base64.b64encode(p.read_bytes()).decode("ascii") payload["source_channel"] = "mcp" return http_json("POST", "/api/v1/orders", payload) if name == "onlineprint_order_status": return http_json("GET", f"/api/v1/orders/{args['public_token']}") raise ValueError(f"Unknown tool: {name}") def main() -> None: for raw_line in sys.stdin: line = raw_line.strip() if not line: continue msg = json.loads(line) mid = msg.get("id") method = msg.get("method", "") if method == "initialize": resp = { "jsonrpc": "2.0", "id": mid, "result": { "protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "onlineprint-mcp", "version": "1.0.0"}, }, } elif method == "notifications/initialized": continue elif method == "tools/list": resp = {"jsonrpc": "2.0", "id": mid, "result": {"tools": TOOLS}} elif method == "tools/call": params = msg.get("params", {}) try: out = handle_call(params.get("name", ""), params.get("arguments", {})) resp = { "jsonrpc": "2.0", "id": mid, "result": {"content": [{"type": "text", "text": json.dumps(out, indent=2)}]}, } except Exception as exc: resp = { "jsonrpc": "2.0", "id": mid, "result": {"content": [{"type": "text", "text": f"Error: {exc}"}], "isError": True}, } else: if mid is None: continue resp = {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"Method not found: {method}"}} sys.stdout.write(json.dumps(resp) + "\n") sys.stdout.flush() if __name__ == "__main__": main()