Building an MCP Server in Node.js: Connect AI Assistants to Your Systems
The Model Context Protocol (MCP) is the open standard for connecting AI assistants — Claude, ChatGPT, VS Code, Cursor and others — to tools and data. Node.js is one of the most popular languages for building MCP servers.
Setup
npm init -y
npm install @modelcontextprotocol/sdk zod
A Server With Tools
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
const server = new McpServer({ name: 'support-tools', version: '1.0.0' });
server.tool(
'search_tickets',
'Search support tickets by keyword and status',
{ query: z.string(), status: z.enum(['open', 'closed']).default('open') },
async ({ query, status }) => {
const tickets = await searchTickets(query, status);
return {
content: [{ type: 'text', text: JSON.stringify(tickets.slice(0, 10), null, 2) }],
};
}
);
await server.connect(new StdioServerTransport());
Good tool descriptions matter — the model uses them to decide when to call each tool.
Resources
Expose read-only data like documents or configuration that the assistant can load as context.
Transports
- stdio — the host launches your server as a local process; simplest for developer tools
- Streamable HTTP — a remote server, used for shared or SaaS integrations, typically with OAuth
The SDK evolves quickly — follow the official docs for the current API.
Design Tips
- Small, focused tools with clear names
- Return concise, structured results — not 5,000-line dumps
- Paginate large data
- Clear error messages the model can act on
Security
- Least privilege — read-only tools first
- Authenticate users for remote servers; respect their permissions
- Validate every input with schemas
- Require confirmation for destructive actions
- Treat data returned to the model as potentially containing prompt injection
An MCP server turns your internal systems into tools any AI assistant can use. Start with one read-only tool that saves your team time.