Documentation Index Fetch the complete documentation index at: https://mintlify.com/microsoft/mcp-for-beginners/llms.txt
Use this file to discover all available pages before exploring further.
The JavaScript samples use the @modelcontextprotocol/sdk package and Zod for schema validation. Both samples run with Node.js and use ES module syntax.
Basic calculator server Located at 03-GettingStarted/samples/javascript, this is the simplest possible MCP server — four arithmetic tools over a stdio transport.
Clone and navigate
git clone https://github.com/microsoft/mcp-for-beginners.git
cd mcp-for-beginners/03-GettingStarted/samples/javascript
package.json {
"name" : "tutorial-mcp" ,
"version" : "1.0.0" ,
"type" : "module" ,
"scripts" : {
"start" : "node index.js"
},
"dependencies" : {
"@modelcontextprotocol/sdk" : ">=1.26.0" ,
"zod" : "^3.24.2"
}
}
Server code import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
import { z } from "zod" ;
// Create an MCP server
const server = new McpServer ({
name: "Calculator MCP Server" ,
version: "1.0.0"
});
// Add tool
server . tool (
"add" ,
{ a: z . number (), b: z . number () },
async ({ a , b }) => ({
content: [{ type: "text" , text: String ( a + b ) }]
})
);
// Subtract tool
server . tool (
"subtract" ,
{ a: z . number (), b: z . number () },
async ({ a , b }) => ({
content: [{ type: "text" , text: String ( a - b ) }]
})
);
// Multiply tool
server . tool (
"multiply" ,
{ a: z . number (), b: z . number () },
async ({ a , b }) => ({
content: [{ type: "text" , text: String ( a * b ) }]
})
);
// Divide tool — includes divide-by-zero guard
server . tool (
"divide" ,
{ a: z . number (), b: z . number () },
async ({ a , b }) => {
if ( b === 0 ) {
return {
content: [{ type: "text" , text: "Error: Cannot divide by zero" }],
isError: true
};
}
return {
content: [{ type: "text" , text: String ( a / b ) }]
};
}
);
// Connect via stdio
const transport = new StdioServerTransport ();
server . connect ( transport ). catch ( console . error );
console . log ( "Calculator MCP Server started" );
Each tool uses a Zod schema to declare its input types. The MCP SDK validates incoming arguments automatically before calling the handler. Advanced server with resources and event emitters Located at 04-PracticalImplementation/samples/javascript, this sample shows a class-based server architecture with a completion tool and a search resource.
Navigate to the advanced sample
cd 04-PracticalImplementation/samples/javascript
Server code ExtendedMcpServer class
Completion tool
Search resource
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' ;
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js' ;
import { z } from 'zod' ;
import EventEmitter from 'events' ;
class ExtendedMcpServer {
constructor ( options = {}) {
this . serverName = options . serverName || 'JavaScript MCP Server' ;
this . models = options . models || [ 'gpt-4' , 'llama-3-70b' , 'claude-3-sonnet' ];
this . events = new EventEmitter ();
this . mcpServer = new McpServer ({
name: this . serverName ,
version: options . version || '1.0.0'
});
this . registerCompletionTool ();
this . registerSearchResource ();
}
async connect () {
const transport = new StdioServerTransport ();
await this . mcpServer . connect ( transport );
}
on ( event , listener ) {
this . events . on ( event , listener );
}
}
Key patterns demonstrated Pattern Detail Class-based server Encapsulates server state and tool registration in a class Event emitters Emits request and completion events for monitoring/metrics Resources with URI templates search://{query} — clients read dynamic data via URI patternsModel validation Checks model name against a supported list before processing