#!/usr/bin/env python3 """RunsBy Autonomy Levels: a read-only MCP server (stdio). Python 3.8+, standard library only, no key. It answers from the public, static levels API that www.runsby.ai rebuilds every day: https://www.runsby.ai/data/levels/index.json and https://www.runsby.ai/data/levels/.json It never writes anything and never sends anything but GET requests to that one base URL. Run it from an MCP client config, for example: {"mcpServers": {"runsby-levels": {"command": "python3", "args": ["/path/to/runsby_levels_mcp.py"]}}} Download: https://www.runsby.ai/mcp/runsby_levels_mcp.py License of the data: CC BY 4.0, credit RunsBy.ai. Env RUNSBY_LEVELS_BASE overrides the base URL (for testing against a local build, e.g. file:///path/site/data/levels). Tools: list_levels graded listings, optionally filtered by level (A0 to A5) and current_only get_level one listing's level, grade, build mark, hands, rated and expiry dates, evidence links level_counts today's counts: current grades per level, expired grades, not graded """ import json, os, re, sys, urllib.error, urllib.request BASE = os.environ.get('RUNSBY_LEVELS_BASE', 'https://www.runsby.ai/data/levels').rstrip('/') PROTOCOL = '2025-06-18' SLUG_RE = re.compile(r'^[a-z0-9][a-z0-9-]{0,80}$') RESERVED = {'index'} # index.json is the listing index, never a business ABOUT = ('Data: RunsBy Autonomy Levels (https://www.runsby.ai/standard/), CC BY 4.0, credit RunsBy.ai. ' 'A slug with no grade is "Not graded", never A0. Expired grades (current: false) are not counted as a level today.') def fetch(name): """GET BASE/.json. Returns the parsed JSON, or None when there is no such file.""" try: req = urllib.request.Request(f'{BASE}/{name}.json', headers={'User-Agent': 'runsby-levels-mcp/1'}) with urllib.request.urlopen(req, timeout=20) as r: return json.loads(r.read().decode('utf-8')) except urllib.error.HTTPError as e: if e.code == 404: return None raise except (FileNotFoundError, urllib.error.URLError) as e: if isinstance(e, FileNotFoundError) or isinstance(getattr(e, 'reason', None), FileNotFoundError): return None raise TOOLS = [ {'name': 'list_levels', 'title': 'List graded businesses', 'description': 'Businesses graded on the RunsBy Autonomy Levels (A0 Human-run to A5 Self-running), with level, ' 'evidence grade and whether the grade is current. Optional filters: level, current_only.', 'inputSchema': {'type': 'object', 'properties': { 'level': {'type': 'string', 'enum': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']}, 'current_only': {'type': 'boolean', 'description': 'Leave out expired grades (default false)'}}, 'additionalProperties': False}, 'annotations': {'readOnlyHint': True, 'openWorldHint': False}}, {'name': 'get_level', 'title': 'Get one business\'s level', 'description': 'One business\'s RunsBy level by its slug (as in https://www.runsby.ai/b/.html): level, ' 'grade, build mark, hands, rated and expiry dates, current, evidence links.', 'inputSchema': {'type': 'object', 'properties': {'slug': {'type': 'string'}}, 'required': ['slug'], 'additionalProperties': False}, 'annotations': {'readOnlyHint': True, 'openWorldHint': False}}, {'name': 'level_counts', 'title': 'Counts per level today', 'description': 'How many current grades sit at each level today, how many grades are expired, and how many ' 'listings are not graded yet.', 'inputSchema': {'type': 'object', 'properties': {}, 'additionalProperties': False}, 'annotations': {'readOnlyHint': True, 'openWorldHint': False}}, ] def call(name, args): if name == 'list_levels': idx = fetch('index') rows = idx['listings'] if args.get('level'): rows = [r for r in rows if r['level'] == args['level']] if args.get('current_only'): rows = [r for r in rows if r['current']] return {'as_of': idx['as_of'], 'count': len(rows), 'listings': rows, 'about': ABOUT} if name == 'get_level': slug = str(args.get('slug', '')).strip().lower() if not SLUG_RE.match(slug): raise ValueError('slug must be lowercase letters, digits and hyphens') rec = None if slug in RESERVED else fetch(slug) if rec is None: return {'slug': slug, 'level': None, 'status': 'Not graded', 'note': 'No RunsBy grade on file for this slug (Not graded, or not listed). This is not A0.', 'about': ABOUT} return dict(rec, about=ABOUT) if name == 'level_counts': idx = fetch('index') return {'as_of': idx['as_of'], **idx['counts'], 'about': ABOUT} raise KeyError(name) def handle(msg): mid, method, params = msg.get('id'), msg.get('method'), msg.get('params') or {} if mid is None: return None # notification (e.g. notifications/initialized): no reply try: if method == 'initialize': res = {'protocolVersion': params.get('protocolVersion') or PROTOCOL, 'capabilities': {'tools': {'listChanged': False}}, 'serverInfo': {'name': 'runsby-levels', 'title': 'RunsBy Autonomy Levels (read-only)', 'version': '1.0.0'}, 'instructions': ABOUT} elif method == 'ping': res = {} elif method == 'tools/list': res = {'tools': TOOLS} elif method == 'tools/call': name = params.get('name') if name not in {t['name'] for t in TOOLS}: return {'jsonrpc': '2.0', 'id': mid, 'error': {'code': -32602, 'message': f'unknown tool: {name}'}} try: out = call(name, params.get('arguments') or {}) res = {'content': [{'type': 'text', 'text': json.dumps(out, ensure_ascii=False)}], 'structuredContent': out, 'isError': False} except Exception as e: # tool errors go back to the model, not as protocol errors res = {'content': [{'type': 'text', 'text': f'Error: {e}'}], 'isError': True} else: return {'jsonrpc': '2.0', 'id': mid, 'error': {'code': -32601, 'message': f'method not found: {method}'}} return {'jsonrpc': '2.0', 'id': mid, 'result': res} except Exception as e: return {'jsonrpc': '2.0', 'id': mid, 'error': {'code': -32603, 'message': str(e)}} def main(): for line in sys.stdin: line = line.strip() if not line: continue try: msg = json.loads(line) except ValueError: out = {'jsonrpc': '2.0', 'id': None, 'error': {'code': -32700, 'message': 'parse error'}} else: out = handle(msg) if isinstance(msg, dict) else {'jsonrpc': '2.0', 'id': None, 'error': {'code': -32600, 'message': 'invalid request'}} if out is not None: sys.stdout.write(json.dumps(out, ensure_ascii=False) + '\n') sys.stdout.flush() if __name__ == '__main__': main()