A simple Model Context Protocol (MCP) server implementation in Node.js with TypeScript for use with Claude Desktop.
The Model Context Protocol (MCP) is an open protocol that enables AI models like Claude to interact with external tools and data sources. It allows Claude Desktop to call local or remote tools, retrieve information, and perform actions through standardized JSON-RPC messaging.
This server exposes tools that Claude can discover and invoke over a stdio (standard input/output) transport, making it perfect for local integrations.
.
├── src/
│ ├── mcp/
│ │ └── server.ts # Main MCP server implementation
│ ├── tools/
│ │ ├── ping.ts # Ping tool example
│ │ ├── system-info.ts # System info tool example
│ │ └── registry.ts # Tool registry & dispatcher
│ └── types/
│ └── index.ts # TypeScript type definitions
├── dist/ # Compiled JavaScript (generated)
├── package.json
├── tsconfig.json
├── mcp.json # Claude Desktop manifest
└── README.md
- ✅ MCP-compliant JSON-RPC 2.0 server
- ✅ Stdio transport (works with Claude Desktop local tools)
- ✅ Two sample tools:
pingandsystem_info - ✅ Extensible tool registry system
- ✅ Proper error handling and validation
- ✅ TypeScript for type safety
- Node.js 16+ (or compatible version)
- npm or yarn
npm installnpm run buildThis compiles TypeScript from src/ to JavaScript in dist/.
npm startThe server will start and listen for JSON-RPC requests on stdin.
A simple test tool that returns "pong".
Request:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "ping"}}Response:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "pong"}]}}Returns detailed system information including OS, architecture, CPU count, memory usage, and uptime.
Request:
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "system_info"}}Response:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [{
"type": "text",
"text": "{\n \"platform\": \"win32\",\n \"arch\": \"x64\",\n \"osType\": \"Windows_NT\",\n ...\n}"
}]
}
}Lists all available tools.
Request:
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}Response:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "ping",
"description": "A simple ping tool...",
"inputSchema": { "type": "object", "properties": {} }
},
...
]
}
}Calls a specific tool with optional arguments.
Request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "tool_name",
"arguments": { "arg1": "value1" }
}
}Response:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{ "type": "text", "text": "Result here" }
]
}
}To use this MCP server with Claude Desktop:
-
Build the project:
npm run build
-
Locate your Claude Desktop configuration file:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
- macOS:
-
Add the server to your
claude_desktop_config.json:{ "mcpServers": { "example-mcp-server": { "command": "node", "args": ["/absolute/path/to/dist/mcp/server.js"], "env": {} } } }Replace
/absolute/path/towith the full path to your project folder. -
Restart Claude Desktop to load the new MCP server.
-
Claude will now be able to discover and call the tools provided by this server.
To add a new tool:
-
Create a new tool file in
src/tools/(e.g.,src/tools/my-tool.ts):import { Tool, ToolResult, ToolHandler } from "../types/index.js"; export const myTool: Tool = { name: "my_tool", description: "Description of what the tool does", inputSchema: { type: "object", properties: { param1: { type: "string" }, }, required: ["param1"], }, }; export const myToolHandler: ToolHandler = async (args) => { // Implementation return { content: [{ type: "text", text: "Result" }], }; };
-
Register it in
src/tools/registry.ts:import { myTool, myToolHandler } from "./my-tool.js"; const toolRegistry: Map<string, ToolEntry> = new Map([ // ... existing tools ["my_tool", { definition: myTool, handler: myToolHandler }], ]);
-
Rebuild and restart the server:
npm run build npm start
For development with auto-reload, you can use ts-node:
npm run devThis will run the TypeScript directly without compilation (requires ts-node to be installed).
The server implements proper JSON-RPC 2.0 error handling:
- -32700: Parse error
- -32600: Invalid request
- -32601: Method not found
- -32603: Internal error
All errors are returned with descriptive messages to help with debugging.
- Reads JSON-RPC 2.0 messages from stdin
- Routes requests to appropriate handlers
- Validates incoming requests
- Writes responses to stdout
- Handles errors gracefully
- Central registry of available tools
- Provides tool list for discovery
- Dispatches tool calls to appropriate handlers
Each tool has:
- A
Tooldefinition (name, description, input schema) - A
ToolHandlerfunction (async execution logic)
- Ensure Node.js is installed:
node --version - Check that dependencies are installed:
npm install - Build the project:
npm run build
- Verify the full path in
claude_desktop_config.jsonis correct - Restart Claude Desktop after configuration changes
- Check server logs for errors
- Ensure the server process stays running
- Check file permissions on the compiled files
- Verify stdin/stdout are not being intercepted
MIT