← Back to list

Building an MCP Server in Node.js

A practical guide to creating your own AI tool server

Zafir Sk Heerah · 2026-06-18 10:01 · 0 claps · 3.1 min read
#ai #mcp-server #node #software-engineering #developer-tools
Open on Medium ↗
Wiki topics: AGT · AI Agents AI · AI · General 🌐 · Web Development

Building an MCP Server in Node.js

A practical guide to creating your own AI tool server

AI assistants are becoming more powerful because they can now interact with tools, APIs, databases, files, and even your local environment.

One of the easiest ways to expose these capabilities is through the Model Context Protocol (MCP).

In this guide, we’ll build a simple MCP server using Node.js.

What is MCP?

The Model Context Protocol (MCP) is a standard that allows AI applications to communicate with external tools and services.

Instead of hardcoding integrations directly into an AI app, MCP provides a structured way to expose tools such as:

  • APIs
  • Databases
  • File systems
  • Custom functions
  • Internal services
  • Automation workflows

Think of it as:

“A standard interface between AI models and external tools.”

MCP servers can then be connected to applications such as:

  • Claude Desktop
  • Cursor
  • Windsurf
  • Custom AI agents
  • Internal enterprise tools

What We’ll Build

We’ll create a simple MCP server that exposes two tools:

  1. hello
  2. get_time

The AI client will be able to call these tools dynamically.

Prerequisites

You need:

  • Node.js 18+
  • npm
  • Basic JavaScript knowledge

You can download Node.js from: Node.js

Step 1 — Create the Project

Create a new folder:

mkdir my-mcp-server
cd my-mcp-server

Initialize the project:

npm init -y

Step 2 — Install the MCP SDK

Install the official MCP SDK:

npm install @modelcontextprotocol/sdk

Official SDK repository: Model Context Protocol SDK

Step 3 — Create the Server

Create a file named:

server.js

Add the following code:

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const server = new Server(
  {
    name: "demo-mcp-server",
    version: "1.0.0",
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

server.setRequestHandler("tools/list", async () => {
  return {
    tools: [
      {
        name: "hello",
        description: "Say hello to someone",
        inputSchema: {
          type: "object",
          properties: {
            name: {
              type: "string",
            },
          },
          required: ["name"],
        },
      },
      {
        name: "get_time",
        description: "Get the current server time",
        inputSchema: {
          type: "object",
          properties: {},
        },
      },
    ],
  };
});

server.setRequestHandler("tools/call", async (request) => {
  const { name, arguments: args } = request.params;

  if (name === "hello") {
    return {
      content: [
        {
          type: "text",
          text: `Hello ${args.name}!`,
        },
      ],
    };
  }

  if (name === "get_time") {
    return {
      content: [
        {
          type: "text",
          text: new Date().toISOString(),
        },
      ],
    };
  }

  throw new Error("Unknown tool");
});

const transport = new StdioServerTransport();

await server.connect(transport);

Step 4 — Enable ES Modules

Update your package.json:

{
  "type": "module"
}

Step 5 — Run the MCP Server

Start the server:

node server.js

Your MCP server is now running.

How MCP Communication Works

The communication flow is simple:

AI Client
   ↓
MCP Client
   ↓
MCP Server
   ↓
Your Tools / APIs

The AI model never directly executes code.

Instead:

  1. The model discovers available tools
  2. The client sends a tool request
  3. The MCP server executes the logic
  4. The response is returned to the model

This separation makes integrations cleaner and safer.

Connecting to Claude Desktop

You can connect your MCP server to Claude Desktop by editing the MCP configuration.

Example configuration:

{
  "mcpServers": {
    "demo-server": {
      "command": "node",
      "args": ["/absolute/path/to/server.js"]
    }
  }
}

After restarting Claude Desktop, the tools will appear automatically.

Official MCP documentation: Model Context Protocol Documentation

Adding Real Capabilities

Once your server works, you can integrate:

  • REST APIs
  • Databases
  • PostgreSQL
  • Salesforce
  • GitHub
  • File systems
  • Internal company services
  • Automation systems
  • AI workflows

For example:

{
  name: "get_weather"
}

could call a weather API.

{
  name: "create_invoice"
}

could connect to an ERP system.

Best Practices

Validate Inputs

Never trust tool arguments blindly.

Always validate:

  • Types
  • Required fields
  • Permissions
  • Length limits

Avoid Dangerous Execution

Do not expose unrestricted:

  • shell execution
  • file deletion
  • system commands

unless properly sandboxed.

Keep Tools Focused

Good MCP tools do one thing well.

Examples:

  • search_customer
  • create_ticket
  • generate_report

instead of one giant tool handling everything.

Why MCP Matters

MCP is quickly becoming one of the most important standards for AI integrations.

Instead of building custom integrations for every AI platform, you expose tools once through MCP and reuse them across multiple clients.

This makes AI systems:

  • more modular
  • easier to maintain
  • safer
  • easier to scale

Final Thoughts

Building an MCP server in Node.js is surprisingly simple.

With only a few lines of code, you can expose your own tools to modern AI assistants and start creating powerful workflows.

As AI agents continue evolving, MCP will likely become a core layer for connecting models with real-world systems.

If you are already building APIs or automation tools in Node.js, MCP is a very natural next step.

Useful Resources


메타데이터
post_id
bb0a9990f1b7
slug
building-an-mcp-server-in-node-js-bb0a9990f1b7
url
https://medium.com/@zfir/building-an-mcp-server-in-node-js-bb0a9990f1b7
canonical_url
https://medium.com/@zfir/building-an-mcp-server-in-node-js-bb0a9990f1b7
author_url
https://medium.com/@zfir
status
ok
fetched_at
2026-06-20 20:29:01