"""C1K MCP endpoint with the Python standard library only (MCP-01, MCP-02). C1K_MCP_TOKEN=c1k_mcp_... C1K_ORG_ID=org_... python3 docs/examples/mcp.py Each call is one JSON-RPC message in one POST; the server keeps no session. """ import itertools import json import os import sys import time import urllib.error import urllib.request TOKEN = os.environ.get("C1K_MCP_TOKEN") ORG = os.environ.get("C1K_ORG_ID") if not TOKEN or not ORG: sys.exit("Set C1K_MCP_TOKEN and C1K_ORG_ID") URL = f"https://c1k.me/mcp/{ORG}" VERSION = "2025-11-25" ids = itertools.count(1) def send(method, params=None, notification=False, initialized=True): message = {"jsonrpc": "2.0", "method": method} if params is not None: message["params"] = params if not notification: message["id"] = next(ids) req = urllib.request.Request(URL, data=json.dumps(message).encode("utf-8"), method="POST") req.add_header("Authorization", f"Bearer {TOKEN}") req.add_header("Content-Type", "application/json") req.add_header("Accept", "application/json, text/event-stream") if initialized: req.add_header("MCP-Protocol-Version", VERSION) try: with urllib.request.urlopen(req, timeout=15) as res: body = res.read() except urllib.error.HTTPError as err: raise SystemExit(f"HTTP {err.code}: {err.read().decode('utf-8', 'replace')}") if notification: return None reply = json.loads(body) if "error" in reply: raise SystemExit(f"JSON-RPC error {reply['error']['code']}: {reply['error']['message']}") return reply["result"] def call_tool(name, arguments): result = send("tools/call", {"name": name, "arguments": arguments}) if result.get("isError"): raise SystemExit(f"{name} failed: {result['content'][0]['text']}") return result["structuredContent"] info = send("initialize", {"protocolVersion": VERSION, "capabilities": {}, "clientInfo": {"name": "python-example", "version": "1.0.0"}}, initialized=False) print("connected to", info["serverInfo"]["title"], "protocol", info["protocolVersion"]) send("notifications/initialized", notification=True) tools = send("tools/list")["tools"] print("tools:", ", ".join(tool["name"] for tool in tools)) created = call_tool("create_link", {"destination_url": "https://venture.example/launch", "idempotency_key": f"mcp-example-{int(time.time())}"}) print("created", created["link"]["short_url"]) for link in call_tool("list_links", {"limit": 10})["links"]: print(link["short_url"], "->", link["destination_url"])