add-integers, which adds two integers. You’ll walk through project initialization, a complete server.js implementation, and quick ways to test the server from the command line and Postman.
Prerequisites:
- Node.js installed (LTS recommended)
Quick overview
- Initialize a Node project and install the MCP SDK.
- Implement an MCP
Serverthat:- exposes a
tools/listcatalog describing input schemas, - implements
tools/callto run theadd-integerstool, - communicates over stdio using
StdioServerTransport.
- exposes a
- Test via piping JSON into the script or using Postman.
Initialize the project
Create a new directory and initialize an npm project:package.json for this example enables ES modules and provides a bin entry so you can run the server directly:
Make sure
"type": "module" is present so Node.js accepts ES module import syntax in server.js.Files and commands at a glance
| Item | Purpose | Example / Command |
|---|---|---|
| Project init | Create package files | npm init -y |
| Install SDK | Add MCP SDK dependency | npm install @modelcontextprotocol/sdk |
| Run server | Execute script directly | node server.js or chmod +x server.js && ./server.js |
| Main file | MCP server implementation | server.js |
Implement server.js
Createserver.js in the project root. The file starts with a shebang so you can run it directly from the shell. Import the Server and transport classes and the request schemas from the SDK:
Implement the tools catalog (tools/list)
When a client requests the tool catalog (tools/list), return an array of tools with a clear inputSchema. This helps clients know the expected types and required fields. Here we expose a single tool, add-integers, which expects two integer properties: a and b.
Implement the tool execution (tools/call)
Handle tools/call requests by extracting the tool name and arguments, validating types, performing the operation, and returning a result in MCP content format. This example requires both arguments to be integers:
text is broadly interoperable; callers that need numbers can parse or convert the text result.
Start the server using stdio transport
UseStdioServerTransport so the server communicates over standard input/output. Wrap startup in an async main and log to stderr to avoid polluting MCP JSON responses on stdout:
server.js executable if you want to run it directly:
Test the server from the command line
You can simulate MCP JSON-RPC requests by piping JSON messages into the running script. Because the server writes status messages to stderr, the stdout will contain only the JSON response (useful for automated tests).- Test
tools/list:
- Call the
add-integerstool:
Test with Postman
Postman supports MCP-style workflows and can invoke stdio transports or HTTP transports depending on how the client and server are configured.- Create a new request in Postman.
- Choose the stdio transport and point it to the
node server.jscommand (or the path to the executable). - Send a
tools/listrequest to inspect the catalog and input schema. - Send a
tools/callrequest with:name: "add-integers"arguments: { "a": 3, "b": 3 }
- Postman will display the JSON-RPC responses similar to the command-line examples above.
If you prefer HTTP transport for testing or integration, you can adapt the server to an HTTP transport or use a proxy that translates HTTP requests to stdio MCP messages.
Recap and next steps
You now have a minimal, functioning MCP server in Node.js that:- Exposes a tools catalog with structured input schemas.
- Implements tool execution with explicit input validation and clear error handling.
- Communicates over stdio in standard MCP JSON-RPC format.
- Add more tools (file operations, DB queries, external API calls).
- Return richer
contenttypes (JSON objects, structured results, or binary data via attachments). - Add logging, metrics, and unit tests around the request handlers.
Links and references
- Node.js — https://nodejs.org/
- Model Context Protocol SDK (npm) — https://www.npmjs.com/package/@modelcontextprotocol/sdk
- JSON-RPC 2.0 spec — https://www.jsonrpc.org/specification