In this article, we’ll explore how to build a Model Context Protocol (MCP) server using JavaScript. We’ll define what MCP is, why it matters for AI applications, and guide you through creating a basic server, exposing it to a network, and testing it with tools like Postman and Visual Studio Code.
Introduction
In 2025, AI tools like ChatGPT and AI bots on platforms such as WhatsApp and Telegram are ubiquitous, assisting with tasks from writing and translation to building complex applications. These tools rely heavily on context—the information provided in prompts—to generate relevant responses. For example:
- A generic prompt like “Generate a resume” yields a broad, fill-in-the-blank template.
- A specific prompt, such as “My name is El, I graduated from UCBC in Computer Engineering in 2026, built apps like Troto used by thousands, and worked at Imcon as a software developer contributing to blockchain projects in zero-knowledge proofs. Generate my resume,” provides more context, resulting in a tailored response.
However, when building complex systems, manually embedding context in prompts becomes limiting. This is where the Model Context Protocol (MCP) shines, enabling AI tools to dynamically access and compute context from your system.
What is MCP?
MCP (Model Context Protocol) is a protocol designed to provide AI tools with structured, dynamic access to contextual data from your application. Unlike static text prompts, MCP allows you to define Tools, Resources, and Prompts that AI systems can interact with programmatically. This makes it ideal for applications requiring real-time, context-aware AI interactions.
MCP provides the following key components:
- Tools: Functions that perform specific tasks, such as calculations or data processing.
- Resources: Data or state managed by the server, accessible to AI tools.
- Prompts: Predefined instructions or templates for AI to process.
The MCP protocol offers SDKs in multiple languages, including TypeScript, Python, and Rust. For this tutorial, we’ll use the TypeScript SDK to build an MCP server. Refer to the official documentation for more details: modelcontextprotocol.io/docs/sdk.
Building a Basic MCP Server
Let’s start by creating a simple MCP server that exposes a tool for adding two numbers. We’ll use the TypeScript SDK and a stdio transport for local communication.
Basic MCP Server Code
Below is the code for a basic MCP server, saved as server.js:
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// Initialize the MCP server
const server = new McpServer({
name: "basic-mcp-server",
version: "1.0.0",
});
// Register a tool to add two numbers
server.registerTool(
"add",
{
title: "Addition",
description: "Adds two numbers and returns the result",
inputSchema: z.object({ a: z.number(), b: z.number() }),
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
})
);
// Set up stdio transport and connect the server
const transport = new StdioServerTransport();
await server.connect(transport);
Full example can be found at Basic MCP Server Repository.
This code:
- Imports necessary modules from the MCP SDK and
zodfor input validation. - Creates an MCP server instance with a name and version.
- Registers a tool named
addthat takes two numbers (aandb) and returns their sum as text. - Uses a stdio transport to allow communication via system calls.
Connecting with an MCP Client
To interact with the server, you need an MCP client. Below is a simple client that connects to the server and calls the add tool, saved as client.js:
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
// Set up stdio transport to communicate with the server
const transport = new StdioClientTransport({
command: "node",
args: ["server.js"],
});
// Initialize the MCP client
const client = new Client({
name: "basic-mcp-client",
version: "1.0.0",
});
// Connect to the server and call the tool
async function main() {
await client.connect(transport);
const result = await client.callTool({
name: "add",
arguments: { a: 32, b: 20 },
});
console.log("Server response:", result);
}
main().catch(console.error);
To run:
- Save the client code as
client.jsin the same directory asserver.js. - Run the client with
node client.js.
The client will call the add tool, passing a=32 and b=20, and log the result (52).
Exposing the MCP Server to a Network
While a local stdio-based server is useful, exposing the MCP server to a network (local or internet) allows broader access. We’ll use Express.js and the MCP SDK’s HTTP transport to create a network-accessible server.
Network-Accessible MCP Server Code
Below is the code for an HTTP-based MCP server, saved as http-server.js:
import express from "express";
import { randomUUID } from "node:crypto";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
const app = express();
app.use(express.json());
const transports = {};
// Handle MCP requests
app.post("/mcp", async (req, res) => {
const sessionId = req.headers["mcp-session-id"];
let transport;
if (sessionId && transports[sessionId]) {
// Reuse existing session
transport = transports[sessionId];
} else if (!sessionId && isInitializeRequest(req.body)) {
// Create new session
transport = new StreamableHTTPServerTransport({
sessionIdGenerator: () => randomUUID(),
onsessioninitialized: (sessionId) => {
transports[sessionId] = transport;
},
});
// Clean up transport on session close
transport.onclose = () => {
if (transport.sessionId) {
delete transports[transport.sessionId];
}
};
// Initialize MCP server
const server = new McpServer({
name: "http-mcp-server",
version: "1.0.0",
});
// Register the addition tool
server.registerTool(
"add",
{
title: "Addition",
description: "Adds two numbers and returns the result",
inputSchema: z.object({ a: z.number(), b: z.number() }),
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
})
);
await server.connect(transport);
} else {
return res.status(400).json({
jsonrpc: "2.0",
error: { code: -32000, message: "Invalid request: missing or invalid session ID" },
id: null,
});
}
// Handle the request
await transport.handleRequest(req, res, req.body);
});
// Handle GET and DELETE requests for session management
const handleSessionRequest = async (req, res) => {
const sessionId = req.headers["mcp-session-id"];
if (!sessionId || !transports[sessionId]) {
return res.status(400).send("Invalid or missing session ID");
}
const transport = transports[sessionId];
await transport.handleRequest(req, res);
};
app.get("/mcp", handleSessionRequest);
app.delete("/mcp", handleSessionRequest);
// Start the server
app.listen(3004, () => {
console.log("MCP server running on http://localhost:3004/mcp");
});
Full example can be found at HTTP MCP Server Repository.
This code:
- Sets up an Express.js server to handle HTTP requests.
- Manages sessions using a unique
mcp-session-idheader. - Creates a new MCP server instance for each session, registering the
addtool. - Listens on port 3004 for incoming requests.
Run the server with node http-server.js.
Testing the MCP Server
Using Postman
Postman supports MCP servers through its MCP feature:
Open Postman and click “New” > “MCP”.

Select “HTTP” as the server type.

Enter the server URL: http://localhost:3004/mcp.Postman will display available tools (e.g., add), prompts, and resources. Send a request to call the add tool with inputs like { "a": 32, "b": 20 }.

Using Visual Studio Code
To integrate the MCP server with VSCode:
- Run the following command in your terminal:
code --add-mcp '{"name":"my-server","type":"http","url":"http://localhost:3004/mcp"}' - VSCode will recognize the server, allowing you to interact with its tools and resources.
Conclusion
In this article, we built a basic MCP server using the TypeScript SDK, exposed it to a network with Express.js, and tested it using Postman and VSCode. MCP empowers AI applications by providing dynamic, structured context, making it a powerful tool for developers. Try extending this server by adding more tools or resources to suit your application’s needs!