Skip to main content

MCP Generator

Automatically generate MCP server code from your existing API endpoints using CodeSpar's scanner and generator.

1 min read · updated

MCP Generator

The MCP Generator scans your existing API source code, discovers endpoints, and generates a fully functional MCP server with tool definitions — turning any REST API into tools your AI agent can call.

The MCP Generator is part of CodeSpar Enterprise. It's available both as a library (@codespar/mcp-generator) and through the CLI.

Quickstart (CLI)

Generate an MCP server from an existing API without writing any generator code:

# From your API's source tree
codespar mcp generate \
  --source ./src/api \
  --framework express \
  --out ./mcp-my-api

# Inspect the generated tools
codespar tools list --server ./mcp-my-api

# Run it locally and smoke-test
codespar mcp serve ./mcp-my-api --port 3333

The CLI wraps the scanner and generator APIs described below — pick it when you want a one-shot build, or use the library when you want to embed generation in your own build pipeline.

How it works

Scan

The API Scanner parses your TypeScript/JavaScript source files and discovers API endpoints. It supports Express, Fastify, and Next.js API routes.

import { APIScanner } from "@codespar/mcp-generator";

const scanner = new APIScanner();
const endpoints = await scanner.scan("./src/api");

// Discovers:
// - HTTP method (GET, POST, PUT, DELETE)
// - Path and path parameters
// - Query parameters
// - Request body fields
// - JSDoc descriptions

Generate tools

The Generator converts discovered endpoints into MCP tool definitions with JSON Schema inputs, descriptions, and semantic names.

import { MCPGenerator } from "@codespar/mcp-generator";

const generator = new MCPGenerator();
const tools = generator.generateTools(endpoints);

// Each tool gets:
// - A semantic name (GET /api/users/:id → GetUser)
// - A description from JSDoc or path inference
// - A JSON Schema input_schema from params/body

Generate server

The generator produces a complete MCP server configuration with runnable code.

const server = generator.generateServer(
  "my-api",
  "MCP server for My API",
  endpoints,
);

// Output:
// - server.config: MCP server metadata
// - server.tools: Array of tool definitions
// - server.code: Generated TypeScript code

Example

Given this Express API:

src/api/orders.ts
/**
 * Create a new order
 */
app.post("/api/orders", async (req, res) => {
  const { customer_id, items, shipping_address } = req.body;
  // ...
});

/**
 * Get order by ID
 */
app.get("/api/orders/:id", async (req, res) => {
  // ...
});

/**
 * List orders with filters
 */
app.get("/api/orders", async (req, res) => {
  const { status, from, to, limit } = req.query;
  // ...
});

The generator produces:

Generated tools
[
  {
    "name": "CreateOrder",
    "description": "Create a new order",
    "input_schema": {
      "type": "object",
      "properties": {
        "customer_id": { "type": "string" },
        "items": { "type": "array" },
        "shipping_address": { "type": "object" }
      },
      "required": ["customer_id", "items"]
    }
  },
  {
    "name": "GetOrder",
    "description": "Get order by ID",
    "input_schema": {
      "type": "object",
      "properties": {
        "id": { "type": "string", "description": "Order ID" }
      },
      "required": ["id"]
    }
  },
  {
    "name": "ListOrders",
    "description": "List orders with filters",
    "input_schema": {
      "type": "object",
      "properties": {
        "status": { "type": "string" },
        "from": { "type": "string" },
        "to": { "type": "string" },
        "limit": { "type": "number" }
      }
    }
  }
]

Supported frameworks

FrameworkScanner support
ExpressRoutes, Router, middleware
FastifyRoute handlers, schemas
Next.jsApp Router (route.ts), Pages API (pages/api)

Naming conventions

The generator creates semantic tool names from HTTP method + path:

MethodPathGenerated name
GET/api/usersListUsers
GET/api/users/:idGetUser
POST/api/usersCreateUser
PUT/api/users/:idUpdateUser
DELETE/api/users/:idDeleteUser
POST/api/orders/:id/refundRefundOrder

Output types

interface DiscoveredEndpoint {
  method: string;        // HTTP method
  path: string;          // Route path
  handler: string;       // Handler function name
  params: string[];      // Path parameters
  queryParams: string[]; // Query parameters
  bodyFields: string[];  // Request body fields
  description?: string;  // JSDoc description
}

interface MCPToolDefinition {
  name: string;
  description: string;
  input_schema: Record<string, unknown>;
}

interface MCPServerConfig {
  name: string;
  description: string;
  tools: MCPToolDefinition[];
  endpoints: DiscoveredEndpoint[];
  code: string;           // Generated TypeScript
}

Next steps

Edit on GitHub

Last updated on