Example MCP Server
A simple Model Context Protocol (MCP) server implementation in Node.js with TypeScript for use with Claude Desktop.
What is MCP?
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.
Project Structure
.
├── 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
Features
- ✅ 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
Prerequisites
- Node.js 16+ (or compatible version)
- npm or yarn
Getting Started
1. Install Dependencies
npm install
2. Build TypeScript to JavaScript
npm run build
This compiles TypeScript from src/ to JavaScript in dist/.
3. Run the MCP Server
npm start
The server will start and listen for JSON-RPC requests on stdin.
Available Tools
ping
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"}]}}
system_info
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}"
}]
}
}
MCP Protocol Methods
tools/list
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": {} }
},
...
]
}
}
tools/call
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" }
]
}
}
Registering with Claude Desktop
To use this MCP server with Claude Desktop:
Build the project:
npm run buildLocate 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.
Adding New Tools
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
Development
For development with auto-reload, you can use ts-node:
npm run dev
This will run the TypeScript directly without compilation (requires ts-node to be installed).
Error Handling
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.
Architecture
MCP Server (src/mcp/server.ts)
- Reads JSON-RPC 2.0 messages from stdin
- Routes requests to appropriate handlers
- Validates incoming requests
- Writes responses to stdout
- Handles errors gracefully
Tool Registry (src/tools/registry.ts)
- Central registry of available tools
- Provides tool list for discovery
- Dispatches tool calls to appropriate handlers
Tool Handlers
Each tool has:
- A
Tooldefinition (name, description, input schema) - A
ToolHandlerfunction (async execution logic)
Troubleshooting
Server not starting
- Ensure Node.js is installed:
node --version - Check that dependencies are installed:
npm install - Build the project:
npm run build
Tools not visible in Claude
- Verify the full path in
claude_desktop_config.jsonis correct - Restart Claude Desktop after configuration changes
- Check server logs for errors
Connection issues
- Ensure the server process stays running
- Check file permissions on the compiled files
- Verify stdin/stdout are not being intercepted
License
MIT