Here we start from the base that this repository gives. In other words, we assume we already have an API set up and running.
In the src folder we create a new folder called mcp, and within that we create a file called server.ts. (The naming is up to you, but this is an example). Here we will set up the MCP server itself.
We add the following code to server.ts:
// Importing the MCP Server class from the SDK
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
// Creating the MCP server instance with a name and version
const server = new McpServer({
name: "My MCP Server",
version: "1.0.0",
});
// Exporting the server so that we can use it in other parts of our application
export { server };Now we have created the MCP server instance. Next, we need to set up the API endpoint that clients will use to communicate with this server.
In the src folder, we open the index.ts file (or whatever file you have set up your express server in).
First let's add the neccesary imports
// Importing the MCP Server instance we created earlier
import { server } from "./mcp/server.js";
// Importing the Streamable HTTP Server Transport from the SDK to handle HTTP requests
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";Then lets create the endpoint for handling the MCP trafic
// Adding the endpoint for MCP requests. Note: it doens't have to be called /mcp
app.post("/mcp", async (req, res) => {
// Creating a new transport for each request to handle stateless sessions
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined // We are leaving this undefined for stateless sessions
});
// Connecting the transport to the server and handling the request
await server.connect(transport);
// Handling the incoming request and sending back the response
await transport.handleRequest(req, res, req.body);
});The entire file should look something like this now
import express from "express";
// Importing the MCP Server instance we created earlier
import { server } from "./mcp/server.js";
// Importing the Streamable HTTP Server Transport from the SDK to handle HTTP requests
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
const port = process.env.PORT || 3000;
const app = express();
app.use(express.json());
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/mcp", async (req, res) => {
// Creating a new transport for each request to handle stateless sessions
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined // We are leaving this undefined for stateless sessions
});
// Connecting the transport to the server and handling the request
await server.connect(transport);
// Handling the incoming request and sending back the response
await transport.handleRequest(req, res, req.body);
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});Now we can run the application and verify that it runs. We can even add it as a MCP Server for our GitHub Copilot, or the AI tool we prefer. After configuring it and running it, it should look something like this.
