mirror of
https://github.com/pugarte7/spanish-top-tech-companies.git
synced 2026-10-26 06:58:11 +01:00
392 lines
18 KiB
Python
392 lines
18 KiB
Python
#!/usr/bin/env python3
|
|
"""MCP server over the site's salary aggregates. Standard library only.
|
|
|
|
python3 scripts/mcp_server.py # stdio, for Hermes, Claude, Cursor
|
|
python3 scripts/mcp_server.py --http --port 8787 # Streamable HTTP at /mcp
|
|
python3 scripts/mcp_server.py --data https://<site>/api/v1/data.json
|
|
|
|
It reads the same /api/v1/data.json the page does (or builds it from
|
|
companies.csv when there is no _site/), so it publishes nothing the site does
|
|
not: every figure comes from lib.MIN_GROUP people or more. The HTTP mode is
|
|
stateless JSON responses, no sessions and no SSE, which the spec allows and
|
|
which any host that runs Python can serve.
|
|
|
|
It speaks both eras of the protocol. A request whose params carry
|
|
`_meta["io.modelcontextprotocol/protocolVersion"]` is answered statelessly
|
|
under the 2026-07-28 revision (`server/discover`, `resultType`, cache hints);
|
|
`initialize` and anything without that key get the handshake-era answers that
|
|
clients up to 2025-11-25 expect. api/mcp.js is a port of this file and
|
|
tests/mcp.test.mjs checks the two answer alike.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import http.server
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import urllib.parse
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
import query # noqa: E402
|
|
|
|
MODERN = "2026-07-28"
|
|
LEGACY = ("2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05")
|
|
SUPPORTED = (MODERN, *LEGACY)
|
|
META = "io.modelcontextprotocol/"
|
|
# How long a client may cache the tool list: tools change with a deploy, data weekly.
|
|
TTL_MS = 3_600_000
|
|
SERVER = {"name": "tech-pay-spain", "title": "Spanish Top Tech Companies", "version": "1.1.0"}
|
|
|
|
PARSE_ERROR, INVALID_REQUEST, METHOD_NOT_FOUND = -32700, -32600, -32601
|
|
INVALID_PARAMS, INTERNAL_ERROR = -32602, -32603
|
|
HEADER_MISMATCH, UNSUPPORTED_VERSION = -32020, -32022
|
|
|
|
|
|
def instructions(data: dict) -> str:
|
|
# The privacy thresholds come from data.json, so the text cannot drift from them.
|
|
return (
|
|
"Salaries of people working in Spain, from Levels.fyi submissions, in euros per year. "
|
|
"Base is gross salary; total adds bonus and stock. "
|
|
f"Every figure comes from {data['min_group']} or more people, the middle half (p25-p75) "
|
|
f"from {data['quartile_min']}, and euro figures are rounded to the nearest {data['round_eur']}; "
|
|
"a selection with fewer returns "
|
|
"no result, so widen it instead of guessing. Only companies whose median base over "
|
|
"all their salaries reaches company_floor_eur (see list_filters) are counted; say so "
|
|
"when quoting a figure for Spain. Years are years of experience in "
|
|
"buckets (0-1, 2-4, 5-7, 8-11, 12-19, 20+); pass a number and it is bucketed. "
|
|
"Call list_filters first if unsure of a role or region name. Cite the source as "
|
|
"Spanish Top Tech Companies (Levels.fyi data), with the data date.")
|
|
|
|
|
|
# `years` is a string in the schema, numbers welcome: a union type trips the
|
|
# function-calling formats of some model providers.
|
|
SELECTION = {
|
|
"role": {"type": "string", "description": "Role slug or name, e.g. software-engineer, "
|
|
"Data Scientist, product-manager. Omit for all roles."},
|
|
"region": {"type": "string", "description": "Spanish region code or name, e.g. MD, "
|
|
"Madrid, CT, Cataluña. Omit for all of Spain."},
|
|
"years": {"type": "string", "description": "Years of experience, as a number such as 6 "
|
|
"or a bucket such as 5-7. Omit for any experience."},
|
|
}
|
|
READ_ONLY = {"readOnlyHint": True, "openWorldHint": False}
|
|
TOOL_NAMES = ("salary_stats", "top_companies", "company_salaries", "list_filters")
|
|
|
|
|
|
def tools(data: dict) -> list[dict]:
|
|
return [
|
|
{"name": "salary_stats", "title": "Salary statistics",
|
|
"description": "Median base, middle half (p25-p75), median total comp and share at "
|
|
"60k+ base for a role, region and experience in Spain.",
|
|
"inputSchema": {"type": "object", "properties": SELECTION},
|
|
"annotations": READ_ONLY},
|
|
{"name": "top_companies", "title": "Best-paying companies",
|
|
"description": f"Companies ranked by median base for a selection, each from {data['min_group']}+ "
|
|
"salaries. Use search to look one company up within the selection.",
|
|
"inputSchema": {"type": "object", "properties": {
|
|
**SELECTION,
|
|
"search": {"type": "string", "description": "Filter companies by name."},
|
|
"limit": {"type": "integer", "minimum": 1, "maximum": 200, "default": 20}}},
|
|
"annotations": READ_ONLY},
|
|
{"name": "company_salaries", "title": "One company's salaries",
|
|
"description": "One company in Spain: overall, and by role, region or experience "
|
|
f"where {data['min_group']}+ people report. Give a role, region or years "
|
|
"to see the groups within it, combinations included.",
|
|
"inputSchema": {"type": "object", "required": ["name"], "properties": {
|
|
"name": {"type": "string", "description": "Company name, e.g. Glovo."},
|
|
**SELECTION}},
|
|
"annotations": READ_ONLY},
|
|
{"name": "list_filters", "title": "Roles, regions and experience buckets",
|
|
"description": "The role slugs, region codes and experience buckets that have data, "
|
|
"and the date of the data.",
|
|
"inputSchema": {"type": "object", "properties": {}},
|
|
"annotations": READ_ONLY},
|
|
]
|
|
|
|
|
|
def default_data() -> dict:
|
|
built = pathlib.Path(__file__).resolve().parent.parent / "_site" / "api" / "v1" / "data.json"
|
|
if built.exists():
|
|
return query.load(built)
|
|
# By path: `import site` returns the standard library's module, loaded at startup.
|
|
import importlib.util
|
|
import lib
|
|
spec = importlib.util.spec_from_file_location("site_build", built.parents[3] / "scripts" / "site.py")
|
|
site_build = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(site_build)
|
|
return site_build.build_data(lib.load_companies())
|
|
|
|
|
|
def call(data: dict, name: str, args: dict) -> dict:
|
|
sel = {k: args.get(k) for k in SELECTION}
|
|
if name == "salary_stats":
|
|
return query.stats(data, **sel)
|
|
if name == "top_companies":
|
|
return query.top_companies(data, **sel, search=args.get("search"),
|
|
limit=args.get("limit"))
|
|
if name == "company_salaries":
|
|
return query.company(data, args.get("name", ""), **sel)
|
|
return query.filters(data)
|
|
|
|
|
|
def error(mid, code: int, message: str, detail: dict | None = None) -> dict:
|
|
out = {"jsonrpc": "2.0", "id": mid, "error": {"code": code, "message": message}}
|
|
if detail is not None:
|
|
out["error"]["data"] = detail
|
|
return out
|
|
|
|
|
|
def _params(msg) -> dict:
|
|
params = msg.get("params") if isinstance(msg, dict) else None
|
|
return params if isinstance(params, dict) else {}
|
|
|
|
|
|
def protocol_version(msg) -> str | None:
|
|
"""The version a modern request names in its _meta; None for the handshake era."""
|
|
meta = _params(msg).get("_meta")
|
|
return meta.get(META + "protocolVersion") if isinstance(meta, dict) else None
|
|
|
|
|
|
def handle(data: dict, msg) -> dict | None:
|
|
"""One JSON-RPC message in, its response out (None for notifications)."""
|
|
if not isinstance(msg, dict):
|
|
return error(None, INVALID_REQUEST, "invalid request: expected a JSON-RPC object")
|
|
method, mid = msg.get("method"), msg.get("id")
|
|
if mid is None:
|
|
return None
|
|
if not isinstance(method, str):
|
|
return error(mid, INVALID_REQUEST, "invalid request: no method")
|
|
if msg.get("params") is not None and not isinstance(msg["params"], dict):
|
|
return error(mid, INVALID_PARAMS, "params must be an object")
|
|
try:
|
|
return _answer(data, mid, method, _params(msg), protocol_version(msg))
|
|
except Exception as e: # a bug answering one request must not take the server down
|
|
return error(mid, INTERNAL_ERROR, f"internal error: {e}")
|
|
|
|
|
|
def _answer(data: dict, mid, method: str, params: dict, version: str | None) -> dict:
|
|
def ok(result):
|
|
return {"jsonrpc": "2.0", "id": mid, "result": result}
|
|
|
|
if method == "initialize" or version is None:
|
|
if method == "initialize":
|
|
asked = params.get("protocolVersion")
|
|
return ok({"protocolVersion": asked if asked in LEGACY else LEGACY[0],
|
|
"capabilities": {"tools": {"listChanged": False}},
|
|
"serverInfo": SERVER, "instructions": instructions(data)})
|
|
if method == "ping":
|
|
return ok({})
|
|
if method == "tools/list":
|
|
return ok({"tools": tools(data)})
|
|
if method == "tools/call":
|
|
return _call(data, mid, params, {})
|
|
return error(mid, METHOD_NOT_FOUND, f"method not found: {method}")
|
|
|
|
if version != MODERN:
|
|
return error(mid, UNSUPPORTED_VERSION, "Unsupported protocol version",
|
|
{"supported": list(SUPPORTED), "requested": version})
|
|
modern = {"resultType": "complete", "_meta": {META + "serverInfo": SERVER}}
|
|
if method == "server/discover":
|
|
return ok({**modern, "supportedVersions": list(SUPPORTED),
|
|
"capabilities": {"tools": {}}, "instructions": instructions(data),
|
|
"ttlMs": TTL_MS, "cacheScope": "public"})
|
|
if method == "tools/list":
|
|
return ok({**modern, "tools": tools(data), "ttlMs": TTL_MS, "cacheScope": "public"})
|
|
if method == "tools/call":
|
|
return _call(data, mid, params, modern)
|
|
return error(mid, METHOD_NOT_FOUND, f"method not found: {method}")
|
|
|
|
|
|
def _call(data: dict, mid, params: dict, extra: dict) -> dict:
|
|
name, args = params.get("name"), params.get("arguments")
|
|
if args is None:
|
|
args = {}
|
|
if not isinstance(args, dict):
|
|
return error(mid, INVALID_PARAMS, "arguments must be an object")
|
|
if name not in TOOL_NAMES:
|
|
return error(mid, INVALID_PARAMS, f"unknown tool '{name}'")
|
|
try:
|
|
out = call(data, name, args)
|
|
except query.QueryError as e:
|
|
result = {"content": [{"type": "text", "text": str(e)}], "isError": True}
|
|
else:
|
|
text = json.dumps(out, ensure_ascii=False, separators=(",", ":"))
|
|
result = {"content": [{"type": "text", "text": text}], "structuredContent": out}
|
|
return {"jsonrpc": "2.0", "id": mid, "result": {**result, **extra}}
|
|
|
|
|
|
def respond(data: dict, msg) -> dict | list | None:
|
|
"""A message, or a batch (2025-03-26 clients may send one), to what goes back."""
|
|
if isinstance(msg, list):
|
|
if not msg:
|
|
return error(None, INVALID_REQUEST, "invalid request: empty batch")
|
|
return [r for r in (handle(data, m) for m in msg) if r] or None
|
|
return handle(data, msg)
|
|
|
|
|
|
# --- Streamable HTTP ---------------------------------------------------------
|
|
|
|
CORS = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
"Access-Control-Allow-Headers": "Content-Type, Accept, Authorization, MCP-Protocol-Version, "
|
|
"Mcp-Method, Mcp-Name, Mcp-Session-Id, Last-Event-ID",
|
|
"Access-Control-Max-Age": "86400",
|
|
}
|
|
|
|
|
|
def about(origin: str) -> dict:
|
|
"""The body of a 405, for a person or an agent who opened the URL to see what it is."""
|
|
return {"error": "This is an MCP server: POST JSON-RPC to it (Streamable HTTP, no sign-in).",
|
|
"server": SERVER, "endpoint": f"{origin}/mcp", "docs": f"{origin}/llms.txt",
|
|
"protocolVersions": list(SUPPORTED)}
|
|
|
|
|
|
def _header_value(value: str | None) -> str | None:
|
|
"""Mcp-Name may come as =?base64?...?= when the name is not plain ASCII."""
|
|
if value and value.startswith("=?base64?") and value.endswith("?="):
|
|
try:
|
|
return base64.b64decode(value[9:-2]).decode()
|
|
except ValueError:
|
|
return None
|
|
return value
|
|
|
|
|
|
def header_problem(headers: dict, msg) -> tuple[int, str, dict | None] | None:
|
|
"""Why a POST's headers do not fit its body, or None. `headers` has lowercase names.
|
|
|
|
Modern requests mirror their version, method and tool name in headers, and a
|
|
mismatch is a 400 HeaderMismatch. A handshake-era request may name a version
|
|
in MCP-Protocol-Version (2025-06-18 on); one this server does not speak is a 400."""
|
|
sent = headers.get("mcp-protocol-version")
|
|
version = protocol_version(msg)
|
|
if version is not None:
|
|
if sent != version:
|
|
return HEADER_MISMATCH, f"MCP-Protocol-Version header {sent!r} does not match the body's {version!r}", None
|
|
if headers.get("mcp-method") != msg.get("method"):
|
|
return HEADER_MISMATCH, f"Mcp-Method header {headers.get('mcp-method')!r} does not match the body's {msg.get('method')!r}", None
|
|
if msg.get("method") in ("tools/call", "resources/read", "prompts/get"):
|
|
name = _params(msg).get("name") or _params(msg).get("uri")
|
|
if _header_value(headers.get("mcp-name")) != name:
|
|
return HEADER_MISMATCH, f"Mcp-Name header does not match the body's {name!r}", None
|
|
return None
|
|
if sent == MODERN:
|
|
return HEADER_MISMATCH, ("MCP-Protocol-Version is 2026-07-28 but the body has no "
|
|
"_meta io.modelcontextprotocol/protocolVersion"), None
|
|
if sent is not None and sent not in LEGACY:
|
|
return UNSUPPORTED_VERSION, "Unsupported protocol version", {"supported": list(SUPPORTED), "requested": sent}
|
|
return None
|
|
|
|
|
|
def http_reply(data: dict, headers: dict, raw: bytes | str) -> tuple[int, dict | list | None]:
|
|
"""Status and body for one POST to /mcp."""
|
|
try:
|
|
msg = json.loads(raw)
|
|
except (ValueError, UnicodeDecodeError):
|
|
return 400, error(None, PARSE_ERROR, "parse error")
|
|
if isinstance(msg, list):
|
|
reply = respond(data, msg)
|
|
return (200, reply) if reply else (202, None)
|
|
if isinstance(msg, dict) and msg.get("id") is not None:
|
|
problem = header_problem(headers, msg)
|
|
if problem:
|
|
return 400, error(msg["id"], *problem)
|
|
reply = handle(data, msg)
|
|
if reply is None:
|
|
return 202, None
|
|
code = reply.get("error", {}).get("code")
|
|
if code in (PARSE_ERROR, INVALID_REQUEST, HEADER_MISMATCH, UNSUPPORTED_VERSION):
|
|
return 400, reply
|
|
if code == METHOD_NOT_FOUND and protocol_version(msg) is not None:
|
|
return 404, reply
|
|
return 200, reply
|
|
|
|
|
|
def serve_stdio(data: dict) -> None:
|
|
for line in sys.stdin:
|
|
if not line.strip():
|
|
continue
|
|
try:
|
|
msg = json.loads(line)
|
|
except json.JSONDecodeError:
|
|
reply = error(None, PARSE_ERROR, "parse error")
|
|
else:
|
|
reply = respond(data, msg)
|
|
if reply:
|
|
sys.stdout.write(json.dumps(reply, ensure_ascii=False) + "\n")
|
|
sys.stdout.flush()
|
|
|
|
|
|
LOOPBACK = ("localhost", "127.0.0.1", "::1")
|
|
|
|
|
|
def serve_http(data: dict, host: str, port: int) -> None:
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
|
def _send(self, status: int, body=None, extra: dict | None = None) -> None:
|
|
raw = json.dumps(body, ensure_ascii=False).encode() if body is not None else b""
|
|
self.send_response(status)
|
|
for name, value in {**CORS, **(extra or {})}.items():
|
|
self.send_header(name, value)
|
|
if body is not None:
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(raw)))
|
|
self.end_headers()
|
|
self.wfile.write(raw)
|
|
|
|
def _endpoint(self) -> bool:
|
|
if urllib.parse.urlsplit(self.path).path in ("/mcp", "/mcp/"):
|
|
return True
|
|
self._send(404, {"error": "not found; the MCP endpoint is /mcp"})
|
|
return False
|
|
|
|
def do_OPTIONS(self):
|
|
self._send(204)
|
|
|
|
def _not_post(self):
|
|
# No server-initiated stream: the spec's answer for GET is 405.
|
|
if self._endpoint():
|
|
self._send(405, about(f"http://{self.headers.get('Host', f'{host}:{port}')}"),
|
|
{"Allow": "POST, OPTIONS"})
|
|
|
|
do_GET = do_DELETE = do_PUT = do_PATCH = _not_post
|
|
|
|
def do_POST(self):
|
|
if not self._endpoint():
|
|
return
|
|
# Bound to this machine, a page elsewhere must not reach it through DNS
|
|
# rebinding, so a browser's Origin has to be local too.
|
|
origin = self.headers.get("Origin")
|
|
if host in LOOPBACK and origin and urllib.parse.urlsplit(origin).hostname not in LOOPBACK:
|
|
return self._send(403, error(None, INVALID_REQUEST, f"origin {origin} not allowed"))
|
|
raw = self.rfile.read(int(self.headers.get("Content-Length") or 0))
|
|
status, body = http_reply(data, {k.lower(): v for k, v in self.headers.items()}, raw)
|
|
self._send(status, body)
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
print(f"MCP on http://{host}:{port}/mcp", file=sys.stderr, flush=True)
|
|
http.server.ThreadingHTTPServer((host, port), Handler).serve_forever()
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
p.add_argument("--data", default=os.environ.get("TECH_PAY_DATA"),
|
|
help="path or URL of data.json (default: _site/ or companies.csv)")
|
|
p.add_argument("--http", action="store_true", help="serve Streamable HTTP instead of stdio")
|
|
p.add_argument("--host", default="127.0.0.1")
|
|
p.add_argument("--port", type=int, default=int(os.environ.get("PORT", 8787)))
|
|
a = p.parse_args(argv)
|
|
data = query.load(a.data) if a.data else default_data()
|
|
if a.http:
|
|
serve_http(data, a.host, a.port)
|
|
else:
|
|
serve_stdio(data)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv[1:]))
|