Building Your First MCP Server with Node.js

Building Your First MCP Server with Node.js

Building Your First MCP Server with Node.js

The Model Context Protocol (MCP) is an open standard that lets you expose tools and data to AI language models in a structured way. Think of it as a bridge between your code and AI assistants like Claude. In this post, we’ll build a simple MCP server from scratch using Node.js.

What is MCP?

MCP standardizes how AI models access external services. Instead of writing custom integrations for every tool and every AI platform, you write your logic once as an MCP server, and it works with any MCP-compatible client. It uses JSON-RPC 2.0 for communication under the hood.

Step 1: Project Setup

First, create a new directory and initialize a Node.js project:

mkdir mcp-simple-server
cd mcp-simple-server
npm init -y

Install the MCP SDK:

npm install @modelcontextprotocol/sdk

Step 2: Create the Server File

Create a file called server.js. This is the simplest MCP server that exposes a single tool called “echo” which just returns back whatever message you give it:

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

// Create server instance
const server = new Server({
  name: "simple-echo-server",
  version: "1.0.0",
});

// Register a simple tool
server.setRequestHandler("tools/list", async () => {
  return {
    tools: [
      {
        name: "echo",
        description: "Echoes back the message you send",
        inputSchema: {
          type: "object",
          properties: {
            message: {
              type: "string",
              description: "The message to echo back",
            },
          },
          required: ["message"],
        },
      },
    ],
  };
});

// Handle tool calls
server.setRequestHandler("tools/call", async (request) => {
  if (request.params.name === "echo") {
    const message = request.params.arguments.message;
    return {
      content: [
        {
          type: "text",
          text: `Echo: ${message}`,
        },
      ],
    };
  }
  throw new Error(`Unknown tool: ${request.params.name}`);
});

// Connect via stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);

Step 3: Update package.json

Add this line to your package.json so Node.js understands ES modules:

"type": "module"

Your package.json should look something like this:

{
  "name": "mcp-simple-server",
  "version": "1.0.0",
  "type": "module",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.0.0"
  }
}

Step 4: Test Your Server

The easiest way to test is with the MCP Inspector. Run this command:

npx @modelcontextprotocol/inspector node server.js

This opens a browser-based GUI where you can see your server’s tools and test them. Click “List tools” to see your echo tool, then click “Run Tool” to test it out.

Understanding the Code

Here’s what’s happening in our server:

Server Creation: We instantiate a Server with a name and version. This is your MCP server identity.

tools/list Handler: When an MCP client asks what tools are available, we respond with our tool definitions. Each tool needs a name, description, and an inputSchema that describes what parameters it accepts.

tools/call Handler: When a client calls a tool, we check the tool name and execute the corresponding logic. For echo, we just return the message wrapped with “Echo: ” prefix.

Transport: StdioServerTransport means communication happens through standard input/output. This is perfect for local development.

What’s Next?

Now that you have a working MCP server, you can expand it by adding more tools. Each tool can call databases, APIs, or perform any logic your application needs. The MCP standard keeps everything consistent and reusable across different AI platforms.

The beauty of MCP is that once you build these tools, any MCP-compatible client can use them. Whether it’s Claude Desktop, Cursor, or a custom application, your server speaks a universal language.

Key Takeaways

MCP servers are surprisingly straightforward to build. With just a few lines of Node.js code, you can expose powerful tools to AI models. Start simple like we did here, test with the Inspector, and scale from there. Happy building!

References

Model Context Protocol Official Documentation: https://modelcontextprotocol.io

MCP SDK for Node.js: https://www.npmjs.com/package/@modelcontextprotocol/sdk

Building MCP Servers with TypeScript SDK: https://dev.to/shadid12/how-to-build-mcp-servers-with-typescript-sdk-1c28

Red Hat Developer on MCP with Node.js: https://developers.redhat.com/blog/2025/01/22/quick-look-mcp-large-language-models-and-nodejs

Written by:

402 Posts

View All Posts
Follow Me :