Build an MCP Server: Complete MCP Tutorial for Beginners
Mar 09, 2026 5 Min Read 47 Views
(Last Updated)
MCP (Model Context Protocol) server development is rapidly gaining popularity among developers who want to build more intelligent, networked applications. With the rise of AI tools and automation systems, many developers are actively exploring applications that connect to various tools and services.
MCP server technology offers an opportunity for those new to AI integration and automation. The easiest place to begin learning about these new systems is by building a beginner-level MCP server, which is what we will do in this blog post.
Quick Answer:
Creating a beginner-level MCP server requires configuring a basic server, specifying the tools and resources it will offer, and linking it to an MCP-compatible client so applications can safely use those facilities.
Table of contents
- Understanding MCP Servers and How They Work
- Real-world Example:
- Working Mechanism
- Key Components of an MCP Server
- a. MCP Host
- b. MCP Client
- c. MCP Server
- d. MCP Protocol
- e. External Resources (Databases, File Systems, APIs)
- Prerequisites for Building an MCP Server
- Setting Up the Development Environment
- Step-by-Step Guide to Building an MCP Server
- Create Project Folder & Initialize Node.js
- Create server.js File
- Set Up the WebSocket Server
- Handle Client Connections
- Handle Messages From Clients
- Manage Client Disconnection
- Test With a Client File
- Conclusion
- FAQs
- What is the main purpose of an MCP server?
- Do beginners need advanced programming knowledge to understand MCP servers?
- Can an MCP server connect to multiple resources at the same time?
Understanding MCP Servers and How They Work
An MCP server allows applications or AI models to access tools, data, and services. It is a central system that enables developers to register various resources, including APIs, databases, files, and functions, so that they can be easily accessed in a consistent format.
Instead of creating individual connections to each tool, the developer uses the MCP server to provide a common interface that surfaces these capabilities in a standardized, structured manner.
Real-world Example:
Consider a company with a database of customer information, a payment API, and a file system for reports. The company can ensure that they are located behind an MCP server rather than linking an AI assistant to all of these systems. The AI assistant can then solicit customer information, payments, or reports from the MCP server, allowing access to these resources through a single central hub.
Also Read: How Do Database Servers Work?
Working Mechanism
- The process begins on the MCP Host, where applications or AI systems are executed. Within it, several MCP Clients make requests whenever the system needs resources such as data, files, or internet information.
- Rather than connecting to databases, file systems, or external services, these MCP Clients use the MCP protocol, which serves as a standard communication format for communicating with MCP Servers.
- The request is sent to the corresponding MCP Server, which manages a specific resource. For example, a single MCP server connects to a MySQL database via TCP (Transmission Control Protocol) to read or write structured data.
- Another MCP server is linked to a File System via NFS (Network File System) to access files and folders, and another MCP server is connected to the Internet via HTTPS (Hypertext Transfer Protocol Secure) to communicate securely with external APIs, websites, and online services.
- Once the request is received, the MCP Server interprets it into the format necessary to serve the resource, performs the task (e.g., executing a database query, reading files, or invoking an API), and the result is returned to the MCP Client via the MCP protocol to allow the application or AI system to utilize the requested resource or service.
Model Context Protocol (MCP) was introduced by Anthropic in 2024 to create a standard way for AI systems to connect with external tools, data sources, and services.
Key Components of an MCP Server
The following are the key components of an MCP server that enable applications and AI systems to communicate with various tools and resources.
a. MCP Host
The primary environment on which the application or AI system runs is the MCP Host. It also handles MCP clients and gatewaying requests to various MCP servers.
b. MCP Client
The MCP Client will send requests where the application or AI system requires access to a tool, data source, or service. It interacts with MCP servers through the MCP protocol.
c. MCP Server
MCP Server is the intermediary between MCP clients and external resources. It accepts client requests, processes them, and communicates with the relevant resource to accomplish the task.
d. MCP Protocol
The MCP Protocol denotes the communication pattern between MCP consumers and MCP servers. It ensures that requests and responses are organized and understood by both parties.
e. External Resources (Databases, File Systems, APIs)
Systems that host data or services are called external resources. They can be databases, file storage systems, APIs, or internet services that the MCP server accesses to satisfy client requests.
Prerequisites for Building an MCP Server
Before starting your MCP server, make sure you have the following ready. These will make the setup smooth and prevent errors:
- Basic Programming Knowledge – You should understand programming concepts in Python or JavaScript, depending on which language your MCP server will use.
- Python / Node.js Installed – Install Python (for Python-based MCP servers) or Node.js (for JavaScript-based servers).
- Text Editor or IDE – Tools like VS Code, PyCharm, or Sublime Text help you write and manage your code easily.
- MCP Library / SDK – Download the official MCP library or SDK to handle Model Context Protocol communication.
- Basic Networking Knowledge – Understand how servers and clients communicate over HTTP or WebSocket.
- Terminal / Command Line Basics – You should be able to navigate folders, run scripts, and install packages via terminal or command prompt.
Setting Up the Development Environment
1. Install Python (python.org) or Node.js (nodejs.org) and add to PATH.
2. Open a code editor like VS Code or PyCharm.
3. Create a project folder for your MCP server files.
4. Install MCP library:
- Python: pip install mcp
- Node.js: npm install mcp
5. Verify installation:
- Python: python –version
- Node.js: node -v
- MCP library test: run a simple script to check it works. The reference code is provided below.
For Example:
For Python:
——-
import mcp
print("MCP library is working!")
——-
Save as test_mcp.py and run:
python test_mcp.py
——-
You should see:
MCP library is working!
For Node.js:
——-
const mcp = require('mcp');
console.log("MCP library is working!");
——-
Save as test_mcp.js and run:
node test_mcp.js
——-
You should see:
MCP library is working!
This confirms your MCP setup is correct.
6. For Python, create a virtual environment by running the command below in your terminal:
- python -m venv venv → activate it before installing packages.
Here’s how to activate a Python virtual environment on different systems:
Windows (Command Prompt):
venv\Scripts\activate
Windows (PowerShell):
venv\Scripts\Activate.ps1
macOS / Linux:
source venv/bin/activate
Step-by-Step Guide to Building an MCP Server
Currently, we will use Node.js as our primary tool for building the MCP server.
1. Create Project Folder & Initialize Node.js
First, we create a new folder in our server files to keep everything organized. We then run npm init -y to create a package.json file. This file tracks your project and the libraries that you install. And lastly, we install the ws library to help us build a WebSocket server that the MCP clients will connect to.
mkdir mcp-server
cd mcp-server
npm init -y
npm install ws
2. Create server.js File
Then we create a file named server.js and put all our server code in it. This is similar to the server’s brain. In this case, we have instructions on how the server should behave when a client connects, sends messages, or disconnects.
const WebSocket = require('ws');
// Create a WebSocket server on port 3000
const wss = new WebSocket.Server({ port: 3000 }, () => {
console.log('MCP server running on ws://localhost:3000');
});
// Handle new client connections
wss.on('connection', (ws) => {
console.log('New client connected');
// Send a welcome message
ws.send(JSON.stringify({ message: 'Welcome to MCP server!' }));
// Handle messages from clients
ws.on('message', (data) => {
const msg = data.toString();
console.log('Received from client:', msg);
// Echo the message back to client
ws.send(JSON.stringify({ message: `Server received: ${msg}` }));
});
// Handle client disconnect
ws.on('close', () => {
console.log('Client disconnected');
});
});
3. Set Up the WebSocket Server
In server.js, we make a WebSocket server on port 3000. That is, our server is currently waiting to accept any client attempting to connect to ws://localhost:3000. After the server starts, it displays a message indicating that it is running.
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 }, () => {
console.log('MCP server running on ws://localhost:3000');
});
4. Handle Client Connections
The server is executed when a client connects. It records a new client connected and provides a welcome message to the Client. This is a connection between the server and the client.
wss.on('connection', (ws) => {
console.log('New client connected');
ws.send(JSON.stringify({ message: 'Welcome to MCP server!' }));
5. Handle Messages From Clients
The server is waiting for messages from the Client. Upon receiving something, it logs it to the console and sends a confirmation message back to the Client. This is referred to as echoing, as it indicates that the server is communicating.
ws.on('message', (data) => {
const msg = data.toString();
console.log('Received from client:', msg);
ws.send(JSON.stringify({ message: `Server received: ${msg}` }));
});
6. Manage Client Disconnection
If the Client disconnects, the server notices and records it as Client disconnected. This maintains a record of the number of clients still associated, and the server does not attempt to send any messages to a client that is already dead.
ws.on('close', () => {
console.log('Client disconnected');
});
});
7. Test With a Client File
Lastly, we built a test client file, client.js, to test our server. The Client connects to the server, relays a message to it, and displays any response it receives. This will help us ensure our MCP server is operational and able to interact with customers.
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:3000');
ws.on('open', () => {
console.log('Connected to server');
ws.send('Hello Server!');
});
ws.on('message', (data) => {
console.log('Received from server:', data.toString());
});
Boost Your Professional Career – Enroll in the Intel & IITM Pravartak Certified AI/ML Course offered by HCL GUVI and Start Your Journey in Artificial Intelligence.
Note:
Run the server:
node server.js
Run the client:
node client.js
You’ll see messages in both terminals showing real-time communication between server and client.
Conclusion
Building an MCP server helps you understand how modern applications and AI systems connect with different tools and resources. By learning the basic concepts and components, you can get a clear idea of how MCP servers organize communication between applications and external systems.
FAQs
What is the main purpose of an MCP server?
An MCP server helps applications or AI systems connect with tools, databases, file systems, and online services through a single system.
Do beginners need advanced programming knowledge to understand MCP servers?
No, beginners can start by learning the basic concepts of MCP servers before moving to advanced implementation and coding.
Can an MCP server connect to multiple resources at the same time?
Yes, an MCP server can connect to resources such as databases, file systems, and APIs from a single location.



Did you enjoy this article?