blog.pierrehenry.be

How to Build a Clean MCP Server Setup in Minutes

Photo by Dylann Hendricks | 딜란 How to Build a Clean MCP Server Setup in Minutes - Photo by Dylann Hendricks | 딜란 on Unsplash

Alright, let’s get straight into it. I want to show you how my MCP server works. This is going to be a quick one, just a couple of minutes, because I’m a bit short on time, but I really wanted to share this with you before I head out.

The Architecture: Keeping It Clean and Simple

So, here’s how I’ve set things up. At the root of my project, I’ve got a routes folder. Inside, there’s a single file: context.route.ts. I decided to keep all my routes in this one file instead of dumping them into index.ts or server.ts. It just feels cleaner that way. Trust me, having a dedicated routes file makes things easier to manage, especially as your project grows.

What’s Inside the Context Route?

In this context route, I basically display my services. For my MCP server, the sample endpoint just lists out what I do, who I am, and my details. There’s also a new endpoint I added recently. The idea is simple:

Here’s a rough example of what the route might look like:

 1// context.route.ts
 2import { FastifyInstance } from 'fastify';
 3
 4export default async function (fastify: FastifyInstance) {
 5  fastify.get('/context', async (request, reply) => {
 6    return {
 7      title: 'MCP Server Services',
 8      description: 'All the services I offer, who I am, and my details.',
 9      keywords: ['MCP', 'API', 'services', 'TypeScript']
10    };
11  });
12}

Photo by Denny Bú How to Build a Clean MCP Server Setup in Minutes - Photo by Denny Bú on Unsplash

The Server: Fastify, but You’ve Got Options

The main server file, server.ts, is super straightforward. I’m using Fastify here, but honestly, you can use Express (which is probably the most popular TypeScript/JavaScript framework out there), or even NestJS. I’ve used Nest before, and it’s great. They all get the job done.

Here’s the basic setup:

 1// server.ts
 2import Fastify from 'fastify';
 3import contextRoutes from './routes/context.route';
 4import dotenv from 'dotenv';
 5
 6dotenv.config();
 7
 8const server = Fastify();
 9const PORT = process.env.PORT || 3000;
10
11server.register(contextRoutes);
12
13server.listen({ port: Number(PORT) }, (err, address) => {
14  if (err) {
15    console.error(err);
16    process.exit(1);
17  }
18  console.log(`Server running at ${address}`);
19});

A quick note: I used to hardcode the port number, but that’s not great practice. So now, I’m pulling it from an .env file using the dotenv package. Make sure your PORT variable is in uppercase in your .env file, or you’ll run into issues. (Yeah, I made that typo myself. Lesson learned.)

Cables and wires How to Build a Clean MCP Server Setup in Minutes - Photo by Kier in Sight Archives on Unsplash

The Rest: Package Scripts and TypeScript Config

The rest of the setup is pretty standard:

That’s it. It’s a really simple API, but it’s built specifically for my MCP server. The structure, especially the endpoints and output, is a bit different from a typical RESTful API, but otherwise, it’s pretty close.

Key Takeaways



Pierre-Henry Soria

GitHub · PierreHenry.Dev · YouTube

<< Previous Post

|

Next Post >>

#Behind the Scenes #Entrepreneurship #Mcp Servers #Server Architecture #Server Setup #Tech #Technology