API Wrapper MCP
Wrapping Any API as MCP
The most common MCP pattern: wrap an existing REST API so Claude can use it. This works with any API.
typescript
// Pattern: Wrap any REST API as MCP tools
function apiTool(server, name, description, schema, endpoint, method = "GET") {
server.tool(name, description, schema, async (params) => {
const url = typeof endpoint === "function" ? endpoint(params) : endpoint;
const res = await fetch(url, {
method,
headers: { "Content-Type": "application/json" },
body: method !== "GET" ? JSON.stringify(params) : undefined,
});
const data = await res.json();
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
});
}PreviewTypeScriptRead Only
Copy this code and run it locally with
npx tsx server.ts