Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README. Co-authored-by: Cursor <cursoragent@cursor.com>
104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
/**
|
|
* Nomadro MCP server (stdio).
|
|
* Tools = packages/sdk/tools.json → HTTP /api/public/v1
|
|
*
|
|
* Env:
|
|
* NOMADRO_PUBLIC_ORIGIN default https://nomadro.com
|
|
* NOMADRO_PUBLIC_API_KEY optional
|
|
* NOMADRO_SOCIAL_GUEST_ID optional
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
import {
|
|
CallToolRequestSchema,
|
|
ListToolsRequestSchema,
|
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const TOOLS = JSON.parse(
|
|
readFileSync(join(__dirname, "../../sdk/tools.json"), "utf8"),
|
|
);
|
|
const PREFIX = "/api/public/v1";
|
|
const VERSION = "1.0.0";
|
|
|
|
function baseUrl() {
|
|
return (process.env.NOMADRO_PUBLIC_ORIGIN || "https://nomadro.com").replace(
|
|
/\/$/,
|
|
"",
|
|
);
|
|
}
|
|
|
|
function fillPath(template, args) {
|
|
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
if (args[key] == null) throw new Error(`missing ${key}`);
|
|
return encodeURIComponent(String(args[key]));
|
|
});
|
|
}
|
|
|
|
async function callTool(name, args = {}) {
|
|
const tool = TOOLS.find((t) => t.name === name);
|
|
if (!tool) throw new Error(`unknown tool ${name}`);
|
|
const pathParams = [...tool.path.matchAll(/\{(\w+)\}/g)].map((m) => m[1]);
|
|
const path = fillPath(tool.path, args);
|
|
const url = new URL(`${baseUrl()}${PREFIX}${path}`);
|
|
const headers = { Accept: "application/json" };
|
|
if (process.env.NOMADRO_PUBLIC_API_KEY) {
|
|
headers["X-Nomadro-Key"] = process.env.NOMADRO_PUBLIC_API_KEY;
|
|
}
|
|
if (process.env.NOMADRO_SOCIAL_GUEST_ID) {
|
|
headers["X-Social-Guest-Id"] = process.env.NOMADRO_SOCIAL_GUEST_ID;
|
|
}
|
|
let body;
|
|
if (tool.method === "GET") {
|
|
for (const [k, v] of Object.entries(args)) {
|
|
if (pathParams.includes(k) || v == null || v === "") continue;
|
|
url.searchParams.set(k, String(v));
|
|
}
|
|
} else {
|
|
headers["Content-Type"] = "application/json";
|
|
const payload = { ...args };
|
|
for (const p of pathParams) delete payload[p];
|
|
body = JSON.stringify(payload);
|
|
}
|
|
const res = await fetch(url, { method: tool.method, headers, body });
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok) {
|
|
throw new Error(data.detail || data.error || `HTTP ${res.status}`);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
export async function startNomadroMcp() {
|
|
const server = new Server(
|
|
{ name: "nomadro", version: VERSION },
|
|
{ capabilities: { tools: {} } },
|
|
);
|
|
|
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
tools: TOOLS.map((t) => ({
|
|
name: t.name,
|
|
description: `${t.description} [${t.method} ${t.path}]`,
|
|
inputSchema: t.inputSchema,
|
|
})),
|
|
}));
|
|
|
|
server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
try {
|
|
const data = await callTool(req.params.name, req.params.arguments || {});
|
|
return {
|
|
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
};
|
|
} catch (e) {
|
|
return {
|
|
isError: true,
|
|
content: [{ type: "text", text: e instanceof Error ? e.message : String(e) }],
|
|
};
|
|
}
|
|
});
|
|
|
|
await server.connect(new StdioServerTransport());
|
|
}
|