Navigation
MCP Server in Node.js: Config-Driven AI Integration - MCP Implementation

MCP Server in Node.js: Config-Driven AI Integration

Build Node.js-based MCP servers using custom tools that load env vars from config files, seamlessly integrating AI platforms like Cursor AI.

Developer Tools
4.4(111 reviews)
166 saves
77 comments

Ranked in the top 1% of all AI tools in its category

About MCP Server in Node.js

What is MCP Server in Node.js: Config-Driven AI Integration?

MCP Server in Node.js is a configurable framework enabling AI-powered development environments like Cursor AI to execute custom tools. Built using the Model Context Protocol (MCP), this server exposes functionality such as data operations or code analysis via standardized interfaces. The config-driven approach allows developers to define tools (e.g., arithmetic operations or API key retrieval) through structured configurations, ensuring seamless integration with AI-driven IDEs.

How to use MCP Server in Node.js: Config-Driven AI Integration?

  1. Clone the repository and install dependencies using npm install or manual package configuration.
  2. Configure the mcp.json file in the ./cursor directory with absolute paths to your Node.js executable and server script.
  3. Optionally run ./scripts/update_config.sh to automate path configuration.
  4. Launch the server and enable Cursor AI's Agent Mode to trigger tools via natural language prompts like "add 3 and 5" or "retrieve API key".

MCP Server in Node.js Features

Key Features of MCP Server in Node.js: Config-Driven AI Integration?

  • Two built-in tools: arithmetic addition and secure API key retrieval via environment variables.
  • Zod-based schema validation ensures input parameters adhere to required formats.
  • Standard I/O transport mechanism for IDE integration compatibility.
  • Environment variable loading through configuration files for secure deployments.
  • Modular architecture supporting extension with custom tools beyond the provided examples.

Use cases of MCP Server in Node.js: Config-Driven AI Integration?

Common applications include:

  • Automating repetitive tasks like credential management during development workflows
  • Performing real-time calculations within code suggestions
  • Integrating external APIs for data enrichment during code writing
  • Implementing validation checks for code standards compliance
  • Creating IDE-specific tools tailored to team workflows

MCP Server in Node.js FAQ

FAQ from MCP Server in Node.js: Config-Driven AI Integration?

How do I update environment variables securely?
Modify the env section in mcp.json and avoid hardcoding sensitive values in source files.
Can I add custom tools beyond the examples?
Yes - extend the tools array in server configuration with new functions following the MCP schema standards.
What Node.js versions are supported?
Requires Node.js 20+ for ES Module compatibility and modern API features used in the SDK.
How do I debug tool executions?
Enable verbose logging in the McpServer initialization options and monitor standard output streams.

Content

MCP Server in Node.js

Overview

MCP (Model Context Protocol) is a framework that allows you to integrate custom tools into AI-assisted development environments—such as Cursor AI. MCP servers expose functionality (like data retrieval or code analysis) so that an LLM-based IDE can call these tools on demand. Learn more about MCP in the Model Context Protocol Introduction.

This project demonstrates an MCP server built in Node.js that provides two basic tools. One tool, add , accepts two numbers and returns their sum, while the other, getApiKey , retrieves the API key from the environment (via the API_KEY variable).

Requirements

  • Node.js: Version 20 or higher is required.

Features

  • MCP Integration: Exposes tool functionality to LLM-based IDEs.
  • Addition Tool: Accepts two numeric parameters and returns their sum.
  • Env Var Retrieval: Demonstrates how to load an example environment variable from the configuration file.
  • Input Validation: Uses Zod for schema validation.
  • Standard I/O Transport: Connects via StdioServerTransport for integration with development environments.

Installation

  1. Clone the Repository

    git clone

cd <repository_directory>
  1. Install Dependencies

You can install the project dependencies in one of two ways:

Option 1: Install using the existingpackage.json

Simply run:

    npm install

Option 2: Install dependencies manually

If you prefer, delete the existing package.json and install the required packages manually:

    npm install @modelcontextprotocol/sdk @coinpaprika/api-nodejs-client zod

Then, update the newly generated package.json file to include the following line, which enables ES Modules:

    "type": "module"

Integrating with Cursor AI

This project includes a ./cursor subdirectory that contains an mcp.json file for configuring the MCP server. Cursor AI uses this file to automatically discover and launch your MCP server. Open the file and update the fields as follows:

The ./cursor/mcp.json Structure

Below is the full JSON structure of the configuration file:

{
  "mcpServers": {
    "MCP Server Boilerplate": {
      "command": "/path/to/node",
      "args": ["/path/to/mcp-server.js"],
      "env": {
        "API_KEY": "abc-1234567890"
      }
    }
  }
}
  • mcpServers:
    An object mapping server names to their configuration.

  • MCP Server Boilerplate:
    This is the key for your server configuration. You can name it as you like.

  • command:
    Specifies the absolute path to your Node.js executable. For example:

    /home/john/.nvm/versions/node/v20.13.1/bin/node
    
  • args:
    An array containing the absolute path to your MCP server file. For example:

    ["/home/john/mcp-server-node/index.js"]
    
  • env: (Optional)
    Defines environment variables for your MCP server process. In this example, the API_KEY is set to "abc-1234567890". Adjust this value as needed for your environment.

You can verify the absolute path to your Node.js executable by running which node in your terminal.

Optional: Configuration Automation Scripts

Easily configure your local environment by automatically updating the mcp.json file with the correct absolute paths. To apply your local settings, run the following commands from your project root:

chmod +x ./scripts/update_config.sh
./scripts/update_config.sh

This script replaces the placeholder paths in mcp.json with your machine’s absolute paths for Python and the server script, ensuring your configuration settings are always accurate.

Optional: Global Cursor settings

You can also move the mcp.json file to your global Cursor AI configuration directory located at ~/.cursor to make the configuration available globally.

Using the MCP Tool in Cursor Composer (Agent Mode)

With the MCP server integrated into Cursor AI and with Agent mode enabled in Cursor Composer, simply use a natural language prompt like:

add 3 and 5

or

what is my API key?

The AI agent will infer the available add or getApiKey tool from your MCP server and execute it accordingly.

Code Overview

The project comprises the following key parts:

  • MCP Server Initialization:
    The MCP server is instantiated using McpServer from the MCP SDK and connected via StdioServerTransport.

  • Tool Definitions:

    • add:

Defined with a Zod schema that accepts two numbers (a and b) and returns their sum as text.
* getApiKey:
Retrieves the API key from the environment variable API_KEY and returns it as text.

What is MCP?

Model Context Protocol (MCP) provides a standardized approach to integrate custom tools into AI-assisted development environments. With MCP, you can define tools that perform specific tasks—such as retrieving external data, validating code, or enforcing coding standards—and the AI assistant in your IDE can call these tools automatically based on context. This helps improve developer productivity, ensures consistent quality, and streamlines workflows.

References & Resources

License

This project is licensed under the MIT License.

Related MCP Servers & Clients