Introduction
If you are exploring modern backend development, you might have come across the term MCP server. MCP (Model Context Protocol) is becoming increasingly popular in AI-powered applications because it allows structured communication between clients and AI models. Building an MCP server using TypeScript is a great way to understand how modern APIs, tools, and AI integrations work together.
In this article, you will learn how to build your first MCP server step by step using TypeScript. The explanation is written in simple words so that even beginners can follow along. We will also use real-world examples to make concepts clearer.
What is an MCP Server?
An MCP server is a backend service that follows the Model Context Protocol. It acts as a bridge between a client (like a web app or AI tool) and different resources such as APIs, databases, or functions.
In simple terms, think of an MCP server as a smart assistant that:
Receives requests from a client
Understands the context
Calls the correct tool or function
Sends back structured responses
Real-world example
Imagine you are building a travel website. A user asks:
"Find me the cheapest flight from Delhi to Mumbai."
Your MCP server can:
Why Use TypeScript for MCP Server?
TypeScript is widely used in backend development because it provides type safety and better developer experience.
Benefits of using TypeScript
Helps catch errors early during development
Makes code easier to understand and maintain
Provides better IntelliSense and auto-completion
Works seamlessly with Node.js
Prerequisites
Before starting, make sure you have the following:
Basic knowledge of JavaScript or TypeScript
Node.js installed on your system
A code editor like VS Code
npm or yarn installed
Step 1: Initialize Your Project
First, create a new project folder and initialize it.
mkdir mcp-server
cd mcp-server
npm init -y
Now install required dependencies.
npm install typescript ts-node @types/node
Initialize TypeScript configuration.
npx tsc --init
This will create a tsconfig.json file.
Step 2: Install MCP SDK
To build an MCP server, you need an MCP SDK.
npm install @modelcontextprotocol/sdk
This SDK provides utilities to create servers, tools, and handlers easily.
Step 3: Create Project Structure
Create a simple folder structure.
src/
server.ts
tools/
helloTool.ts
This structure keeps your code clean and scalable.
Step 4: Create Your First Tool
Tools are functions that your MCP server can execute.
Create a file: src/tools/helloTool.ts
export const helloTool = {
name: "hello",
description: "Returns a greeting message",
execute: async (input: any) => {
return {
message: `Hello, ${input.name || "World"}!`
};
}
};
Explanation
Step 5: Create MCP Server
Now create the main server file: src/server.ts
import { createServer } from "@modelcontextprotocol/sdk";
import { helloTool } from "./tools/helloTool";
const server = createServer({
tools: [helloTool]
});
server.start();
console.log("MCP Server is running...");
What is happening here?
We import the MCP SDK
Register our tool
Start the server
Step 6: Run the Server
Use ts-node to run the server.
npx ts-node src/server.ts
If everything is correct, you will see:
"MCP Server is running..."
Step 7: Test Your MCP Server
You can test your server using a client or API tool.
Example request:
{
"tool": "hello",
"input": {
"name": "Baibhav"
}
}
Expected response
{
"message": "Hello, Baibhav!"
}
Step 8: Add Another Tool (Real-World Example)
Let’s create a more practical tool like a calculator.
export const addTool = {
name: "add",
description: "Adds two numbers",
execute: async (input: any) => {
const { a, b } = input;
return {
result: a + b
};
}
};
Register it in server.ts
import { addTool } from "./tools/addTool";
const server = createServer({
tools: [helloTool, addTool]
});
Example request
{
"tool": "add",
"input": {
"a": 5,
"b": 10
}
}
Response
{
"result": 15
}
Step 9: Best Practices for MCP Server Development
Keep tools small and focused
Each tool should do only one task. This makes debugging and scaling easier.
Use proper typing
Avoid using "any". Define proper TypeScript interfaces.
Handle errors properly
Always wrap logic in try-catch blocks to avoid crashes.
Organize your project
Use folders like tools, services, and utils for better structure.
Step 10: Where MCP Servers Are Used
MCP servers are widely used in modern applications such as:
AI chatbots
Automation tools
Developer tools
SaaS platforms
Example use case
In a customer support chatbot:
Summary
Building your first MCP server using TypeScript is a great way to understand how modern AI-driven backend systems work. In this guide, you learned how to set up a project, install the MCP SDK, create tools, run a server, and test it with real examples. By following these steps and best practices, you can start building scalable, maintainable, and powerful MCP servers for real-world applications. As you gain more experience, you can extend your server with APIs, databases, and advanced logic to create production-ready systems.