Files

405 lines
18 KiB
JavaScript

// Vercel Function: the MCP server, public at /mcp (vercel.json rewrites it here).
//
// A port of scripts/query.py and scripts/mcp_server.py, which stay the
// reference (and the stdio server for local agents); tests/mcp.test.mjs checks
// both give the same answers on the same data.json. The site itself is static
// files from _site/ that Vercel serves without calling this code.
//
// Both eras of the protocol: a request carrying
// _meta["io.modelcontextprotocol/protocolVersion"] is answered statelessly under
// 2026-07-28; `initialize` and anything without that key get the handshake-era
// answers that clients up to 2025-11-25 expect.
const MODERN = "2026-07-28";
const LEGACY = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
const SUPPORTED = [MODERN, ...LEGACY];
const META = "io.modelcontextprotocol/";
// How long a client may cache the tool list: tools change with a deploy, data weekly.
const TTL_MS = 3_600_000;
const SERVER = { name: "tech-pay-spain", title: "Spanish Top Tech Companies", version: "1.1.0" };
const PARSE_ERROR = -32700, INVALID_REQUEST = -32600, METHOD_NOT_FOUND = -32601;
const INVALID_PARAMS = -32602, INTERNAL_ERROR = -32603;
const HEADER_MISMATCH = -32020, UNSUPPORTED_VERSION = -32022;
// The privacy thresholds come from data.json, so the text cannot drift from them.
const instructions = (data) =>
"Salaries of people working in Spain, from Levels.fyi submissions, in euros per year. " +
"Base is gross salary; total adds bonus and stock. " +
`Every figure comes from ${data.min_group} or more people, the middle half (p25-p75) ` +
`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.
const 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." },
};
const READ_ONLY = { readOnlyHint: true, openWorldHint: false };
const TOOL_NAMES = ["salary_stats", "top_companies", "company_salaries", "list_filters"];
export const tools = (data) => [
{ 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: `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 " +
`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 },
];
// --- query.py ------------------------------------------------------------------
class QueryError extends Error {}
const norm = (text) => String(text ?? "").normalize("NFKD").replace(/[^\x00-\x7f]/g, "")
.toLowerCase().replace(/-/g, " ").split(/\s+/).filter(Boolean).join(" ");
// An integer, a float with nothing after the point or a string of digits; else null.
const whole = (v) => typeof v === "number" ? (Number.isInteger(v) ? v : null)
: typeof v === "string" && /^\s*-?\d+\s*$/.test(v) ? parseInt(v, 10) : null;
function resolveRole(data, role) {
if (!role) return "";
const want = norm(role);
for (const r of data.roles) if (want === norm(r.slug) || want === norm(r.name)) return r.slug;
const close = data.roles.filter((r) => norm(r.name).includes(want) || norm(r.slug).includes(want))
.map((r) => r.slug);
if (close.length === 1) return close[0];
throw new QueryError(`unknown role '${role}'; ` +
(close.length ? "did you mean " + close.join(", ") : "see list_filters"));
}
function resolveRegion(data, region) {
if (!region) return "";
const want = norm(region);
const entries = Object.entries(data.regions);
for (const [code, n] of entries)
if ([norm(code), norm(n.en), norm(n.es)].includes(want)) return code;
for (const [code, n] of entries)
if (norm(n.en).includes(want) || norm(n.es).includes(want)) return code;
throw new QueryError(`unknown region '${region}'; use a code like MD or a name like Cataluña`);
}
function resolveYears(data, years) {
if (years === undefined || years === null || years === "") return "";
if (data.years.includes(String(years))) return String(years);
const n = whole(years);
if (n === null)
throw new QueryError(`years must be a number or one of ${data.years.join(", ")}`);
for (const bucket of data.years) {
const [low, high] = bucket.replace("+", "").split("-");
if (n >= Number(low) && (high === undefined || n <= Number(high))) return bucket;
}
throw new QueryError(`no bucket holds ${n} years; buckets are ${data.years.join(", ")}`);
}
function resolveLimit(limit) {
if (limit === undefined || limit === null || limit === "") return 20;
const n = whole(limit);
if (n === null) throw new QueryError("limit must be a whole number from 1 to 200");
return Math.max(1, Math.min(n, 200));
}
function select(data, { role, region, years }) {
const r = resolveRole(data, role), g = resolveRegion(data, region), y = resolveYears(data, years);
return [`${r}|${g}|${y}`, { role: r || null, region: g || null, years: y || null }];
}
const figures = (a) => ({
people: a.n, median_base_eur: a.median, p25_base_eur: a.p25, p75_base_eur: a.p75,
median_total_eur: a.total, share_at_60k_base: a.share,
});
const withheld = (data, selection) => ({
selection, result: null,
reason: `fewer than ${data.min_group} salaries match; widen the selection (drop region or years)`,
});
export function stats(data, args = {}) {
const [key, selection] = select(data, args);
const a = data.cells[key];
return a ? { selection, result: figures(a) } : withheld(data, selection);
}
export function topCompanies(data, args = {}) {
const [key, selection] = select(data, args);
const limit = resolveLimit(args.limit);
const found = data.company_cells[key];
if (!found) return withheld(data, selection);
const want = norm(args.search);
const rows = [];
for (const a of found) {
const c = data.companies[a.c];
if (want && !norm(c.name).includes(want)) continue;
rows.push({ company: c.name, url: c.url, ...figures(a) });
}
return { selection, companies_listed: rows.length, result: rows.slice(0, limit) };
}
export function company(data, name, args = {}) {
const [, selection] = select(data, args);
const want = norm(name);
let matches = data.companies.map((c, i) => [c, i]).filter(([c]) => norm(c.name).includes(want));
const exact = matches.filter(([c]) => norm(c.name) === want);
if (exact.length) matches = exact;
if (!matches.length)
throw new QueryError(`no company matching '${name}' has ${data.min_group}+ salaries`);
if (matches.length > 1)
throw new QueryError("several companies match: " +
matches.slice(0, 15).map(([c]) => c.name).join(", "));
const [c, i] = matches[0];
const asked = [selection.role || "", selection.region || "", selection.years || ""];
const groups = [];
for (const [key, found] of Object.entries(data.company_cells)) {
const parts = key.split("|");
if (parts.some((part, k) => asked[k] && part !== asked[k])) continue;
if (!asked.some(Boolean) && parts.filter(Boolean).length > 1) continue;
for (const a of found) {
if (a.c !== i) continue;
const [r, g, y] = parts;
groups.push({ role: r || null, region: g || null, years: y || null, ...figures(a) });
}
}
const rank = (x) => [x.role !== null, x.region !== null, x.years !== null, -x.people];
groups.sort((p, q) => {
const a = rank(p), b = rank(q);
for (let k = 0; k < a.length; k++) if (a[k] !== b[k]) return a[k] < b[k] ? -1 : 1;
return 0;
});
return { company: c.name, url: c.url, cities: c.cities, selection, groups };
}
export function filters(data) {
return {
roles: data.roles.map((r) => ({ slug: r.slug, name: r.name })),
regions: data.regions, years: data.years, min_group: data.min_group,
quartile_min: data.quartile_min ?? null, round_eur: data.round_eur ?? null,
threshold_eur: data.threshold, company_floor_eur: data.company_floor ?? null,
min_base_eur: data.min_base ?? null, reported: data.reported ?? null,
data_as_of: data.data_as_of || data.generated,
};
}
// --- mcp_server.py --------------------------------------------------------------
function call(data, name, args) {
if (name === "salary_stats") return stats(data, args);
if (name === "top_companies") return topCompanies(data, args);
if (name === "company_salaries") return company(data, args.name ?? "", args);
return filters(data);
}
function error(id, code, message, detail) {
const out = { jsonrpc: "2.0", id, error: { code, message } };
if (detail !== undefined) out.error.data = detail;
return out;
}
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
const paramsOf = (msg) => isObject(msg) && isObject(msg.params) ? msg.params : {};
// The version a modern request names in its _meta; null for the handshake era.
function protocolVersion(msg) {
const meta = paramsOf(msg)._meta;
return isObject(meta) ? meta[META + "protocolVersion"] ?? null : null;
}
export function handle(data, msg) {
if (!isObject(msg)) return error(null, INVALID_REQUEST, "invalid request: expected a JSON-RPC object");
const { method, id } = msg;
if (id === undefined || id === null) return null;
if (typeof method !== "string") return error(id, INVALID_REQUEST, "invalid request: no method");
if (msg.params != null && !isObject(msg.params)) return error(id, INVALID_PARAMS, "params must be an object");
try {
return answer(data, id, method, paramsOf(msg), protocolVersion(msg));
} catch (e) { // a bug answering one request must not take the server down
return error(id, INTERNAL_ERROR, `internal error: ${e.message}`);
}
}
function answer(data, id, method, params, version) {
const ok = (result) => ({ jsonrpc: "2.0", id, result });
if (method === "initialize" || version === null) {
if (method === "initialize") {
const asked = params.protocolVersion;
return ok({ protocolVersion: LEGACY.includes(asked) ? asked : 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 callTool(data, id, params, {});
return error(id, METHOD_NOT_FOUND, `method not found: ${method}`);
}
if (version !== MODERN)
return error(id, UNSUPPORTED_VERSION, "Unsupported protocol version",
{ supported: SUPPORTED, requested: version });
const modern = { resultType: "complete", _meta: { [META + "serverInfo"]: SERVER } };
if (method === "server/discover")
return ok({ ...modern, supportedVersions: 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 callTool(data, id, params, modern);
return error(id, METHOD_NOT_FOUND, `method not found: ${method}`);
}
function callTool(data, id, params, extra) {
const name = params.name;
const args = params.arguments ?? {};
if (!isObject(args)) return error(id, INVALID_PARAMS, "arguments must be an object");
if (!TOOL_NAMES.includes(name)) return error(id, INVALID_PARAMS, `unknown tool '${name}'`);
let result;
try {
const out = call(data, name, args);
result = { content: [{ type: "text", text: JSON.stringify(out) }], structuredContent: out };
} catch (e) {
if (!(e instanceof QueryError)) throw e;
result = { content: [{ type: "text", text: e.message }], isError: true };
}
return { jsonrpc: "2.0", id, result: { ...result, ...extra } };
}
// A message, or a batch (2025-03-26 clients may send one), to what goes back.
export function respond(data, msg) {
if (!Array.isArray(msg)) return handle(data, msg);
if (!msg.length) return error(null, INVALID_REQUEST, "invalid request: empty batch");
const replies = msg.map((m) => handle(data, m)).filter(Boolean);
return replies.length ? replies : null;
}
// --- HTTP -------------------------------------------------------------------------
const 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",
};
// The body of a 405, for a person or an agent who opened the URL to see what it is.
const about = (origin) => ({
error: "This is an MCP server: POST JSON-RPC to it (Streamable HTTP, no sign-in).",
server: SERVER, endpoint: `${origin}/mcp`, docs: `${origin}/llms.txt`, protocolVersions: SUPPORTED,
});
// Mcp-Name may come as =?base64?...?= when the name is not plain ASCII.
function headerValue(value) {
if (!value || !value.startsWith("=?base64?") || !value.endsWith("?=")) return value ?? null;
try {
return new TextDecoder().decode(Uint8Array.from(atob(value.slice(9, -2)), (ch) => ch.charCodeAt(0)));
} catch {
return null;
}
}
const repr = (v) => v === undefined || v === null ? "None" : `'${v}'`;
// Why a POST's headers do not fit its body, or null; `headers` has lowercase names.
function headerProblem(headers, msg) {
const sent = headers["mcp-protocol-version"] ?? null;
const version = protocolVersion(msg);
if (version !== null) {
if (sent !== version)
return [HEADER_MISMATCH, `MCP-Protocol-Version header ${repr(sent)} does not match the body's ${repr(version)}`];
if ((headers["mcp-method"] ?? null) !== msg.method)
return [HEADER_MISMATCH, `Mcp-Method header ${repr(headers["mcp-method"])} does not match the body's ${repr(msg.method)}`];
if (["tools/call", "resources/read", "prompts/get"].includes(msg.method)) {
const name = paramsOf(msg).name || paramsOf(msg).uri;
if (headerValue(headers["mcp-name"]) !== name)
return [HEADER_MISMATCH, `Mcp-Name header does not match the body's ${repr(name)}`];
}
return null;
}
if (sent === MODERN)
return [HEADER_MISMATCH, "MCP-Protocol-Version is 2026-07-28 but the body has no " +
"_meta io.modelcontextprotocol/protocolVersion"];
if (sent !== null && !LEGACY.includes(sent))
return [UNSUPPORTED_VERSION, "Unsupported protocol version", { supported: SUPPORTED, requested: sent }];
return null;
}
// Status and body for one parsed POST to /mcp.
export function reply(data, headers, msg) {
if (Array.isArray(msg)) {
const out = respond(data, msg);
return out ? [200, out] : [202, null];
}
if (isObject(msg) && msg.id !== undefined && msg.id !== null) {
const problem = headerProblem(headers, msg);
if (problem) return [400, error(msg.id, ...problem)];
}
const out = handle(data, msg);
if (out === null) return [202, null];
const code = out.error?.code;
if ([PARSE_ERROR, INVALID_REQUEST, HEADER_MISMATCH, UNSUPPORTED_VERSION].includes(code)) return [400, out];
if (code === METHOD_NOT_FOUND && protocolVersion(msg) !== null) return [404, out];
return [200, out];
}
const json = (body, status = 200, extra = {}) => new Response(body === null ? null : JSON.stringify(body), {
status, headers: { ...CORS, ...extra, ...(body === null ? {} : { "Content-Type": "application/json" }) },
});
let DATA; // one parse per instance; a deploy with new data starts new instances
// The deployment's own static copy, so the answers always match the site's.
async function load(url) {
if (!DATA) {
const answer = await fetch(new URL("/api/v1/data.json", url));
if (!answer.ok) throw new Error(`data.json answered ${answer.status}`);
DATA = await answer.json();
}
return DATA;
}
export default {
async fetch(request) {
const url = new URL(request.url);
if (request.method === "OPTIONS") return new Response(null, { status: 204, headers: CORS });
// No server-initiated stream: the spec's answer for GET is 405.
if (request.method !== "POST") return json(about(url.origin), 405, { Allow: "POST, OPTIONS" });
let msg;
try {
msg = JSON.parse(await request.text());
} catch {
return json(error(null, PARSE_ERROR, "parse error"), 400);
}
try {
const [status, body] = reply(await load(url), Object.fromEntries(request.headers), msg);
return json(body, status);
} catch (e) { // data.json unreadable: still answer in JSON-RPC, not with an HTML 500
return json(error(isObject(msg) ? msg.id ?? null : null, INTERNAL_ERROR, `internal error: ${e.message}`), 500);
}
},
};