Backend Development 15 min read

Implementing a Model Context Protocol (MCP) Service with APISIX and Node.js

This article explains what Model Context Protocol (MCP) is, demonstrates a simple e‑commerce example, and provides a step‑by‑step guide to create, configure, build, and debug an MCP service using APISIX Admin API, Node.js, TypeScript, and Zod, enabling natural‑language interaction with backend APIs.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Implementing a Model Context Protocol (MCP) Service with APISIX and Node.js

Model Context Protocol (MCP) is a lightweight communication standard that simplifies interactions between large language models (LLMs) and external tools, APIs, or services, offering a simpler alternative to complex frameworks like LangChain.

The article starts with a concise e‑commerce scenario where a backend API is wrapped by an MCP service, allowing users to create users, orders, and query statuses through natural language commands.

To implement an MCP service from scratch, the author chooses the open‑source APISIX gateway for its Admin API and demonstrates the required environment setup:

mkdir apisix-mcp
cd apisix-mcp
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D @types/node typescript
mkdir src
touch src/index.ts

The package.json is updated to include type: "module" and a build script, and a tsconfig.json is added with appropriate compiler options.

In src/index.ts , the MCP server is created:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

const APISIX_API_BASE = "http://127.0.0.1:9180/apisix/admin";
const APISIX_API_KEY = "edd1c9f034335f136f87ad84b625c8f1";

const server = new McpServer({
  name: "apisix-mcp",
  version: "1.0.0",
  config: {
    apiKey: { type: "string", description: "API key for APISIX Admin API authentication", default: APISIX_API_KEY },
    apiBase: { type: "string", description: "Base URL for APISIX Admin API", default: APISIX_API_BASE }
  }
});

Utility functions for calling the APISIX Admin API are defined using axios and TypeScript interfaces, followed by two MCP tools: get-routes (list all routes) and create-route (create a new route). The tools use the previously defined request helper to interact with the API and return formatted text responses.

server.tool('get-routes', '列出所有路由', {}, async (args, extra) => {
  const response = await makeAdminAPIRequest(`${APISIX_API_BASE}/routes`);
  return { content: [{ type: "text", text: JSON.stringify(response, null, 2) }] };
});

server.tool('create-route', '创建一个新的路由', routeSchema.shape, async (args, extra) => {
  const routeId = args.id || Date.now().toString();
  const response = await makeAdminAPIRequest(`${APISIX_API_BASE}/routes/${routeId}`, 'PUT', args.route);
  return { content: [{ type: "text", text: JSON.stringify(response, null, 2) }] };
});

After building the project with npm run build , a .cursor/mcp.json file declares the MCP service for the Cursor AI client, specifying the command ( node ) and the path to the built entry point.

{
  "mcpServers": {
    "apisix-mcp": {
      "command": "node",
      "args": ["E:\\projects\\apisix-mcp\\build\\index.js"]
    }
  }
}

Debugging is performed directly in Cursor, selecting an “agent” model (e.g., Claude 3.7 Sonnet) and issuing natural‑language commands such as creating a user order. Cursor calls the MCP tools, the service interacts with APISIX, and the results are verified via curl and jq commands.

The author highlights the rapid development cycle: within a couple of hours, a functional MCP service is built, allowing natural‑language control of backend APIs, and suggests that future MCP clients could replace traditional admin consoles.

MCPbackend developmentNode.jsAPI automationLLM integrationModel Context ProtocolAPISIX
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.