Back to Browse

Building an MCP Server with Nodejs

1.1K views
Oct 21, 2025
27:18

Today, we are going to build an MCP server from almost scratch. MCP stands for Model Context Protocol, and it is a simple protocol that uses JSON over standard input and output. You could technically write one with bash, but using an SDK makes things easier since you do not have to worry about the structure of your JSON. Let us talk first about how MCP works. When you set up your server, you can register tools so that AI agents can use them. Tools can do things like fetch data, provide the current time, or even control devices in the real world. We will start with a Node.js project, running npm init to set up package.json, and then we will make an index.js file. Next, we install the MCP SDK and check our package.json to make sure the dependency is there. By default, the SDK will expect ES modules, so be sure your package.json says "type":"module." In the code, you can register a tool like “get current time,” which lets the AI agent ask the server for the current date and time. The input schema for this tool is empty, since you do not need to send anything to get the time. The most important part here is registering the tool properly and making sure the server handles the request by returning the current time as a string. Once your server is running, you can connect it to an agent (like Claude) by providing the proper command to start your server, and then registering that with the agent using something like `claude mcp add`. To check if things work, you can use `claude mcp list` to see if your server is connected. If an error happens, like a "cannot use import outside a module" error, check your type in package.json and make sure your import syntax matches the module system. If you get key validation or schema errors, double check that your tool parameters and return data fit what the server expects. When everything is right, you should be able to have the agent call your tool to get the current time, or call other tools you register, like "extract intent," which analyzes the purpose of a string. If you want to add more tools, just register them in your server code, restart the server, and reconnect. The hardest part is making sure your parameters, input schemas, and outputs match up so the server and the agent agree on what data is being sent. When you get it running, you can make powerful AI tools that reach outside of models and pull in real-world information or run software for you. The process involves some trial and error, especially when dealing with module systems and tool schemas, but once it is set up, you can build just about anything with MCP.

Download

0 formats

No download links available.

Building an MCP Server with Nodejs | NatokHD