Example MCP Server

Example MCP Server

A Node.js/TypeScript MCP server template with sample tools (ping and system_info) that demonstrates how to build custom tools for Claude Desktop using stdio transport.

Category
访问服务器

README

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: ping and system_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:

  1. Build the project:

    npm run build
    
  2. 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
  3. 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/to with the full path to your project folder.

  4. Restart Claude Desktop to load the new MCP server.

  5. Claude will now be able to discover and call the tools provided by this server.

Adding New Tools

To add a new tool:

  1. 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" }],
      };
    };
    
  2. 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 }],
    ]);
    
  3. 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 Tool definition (name, description, input schema)
  • A ToolHandler function (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.json is 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

References

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选