跳到正文
DEV Community· GrahamduesCN·· 2 小時前AI 評分52

如何在 10 分鐘內建立你的第一個 MCP 伺服器

How to build your first MCP server in 10 minutes

AI 導讀

作者示範以 Node.js 20+ 建立首個 MCP 伺服器,加入工具並完成建置後連接 Claude Desktop。MCP SDK 會處理傳輸層,StdioServerTransport 則令伺服器以子程序執行,無須 HTTP 或處理連接埠衝突。作者稱全程花了 8 分鐘,大部分時間用於閲讀文件,並指出工具崩潰會令整個 MCP 連線中斷。

正文

GrahamduesCN

I built my first MCP server last week and it was way simpler than I expected. Here is exactly how, no fluff.

Prerequisites

  • Node.js 20+
  • 10 minutes

Step 1: Scaffold

npx create-mcp-server my-first-server
cd my-first-server
npm install

This generates a complete TypeScript project with one example tool.

Step 2: Add your tool

Open src/index.ts. Replace the hello tool with whatever you want:

server.setRequestHandler(CallToolRequestSchema, async (request) => {
  const { name, arguments: args } = request.params;

  if (name === 'current_time') {
    return {
      content: [{ type: 'text', text: new Date().toISOString() }]
    };
  }

  throw new Error(`Unknown tool: ${name}`);
});

Step 3: Build and connect

npm run build
npm start

Add to Claude Desktop config and you are done.

The whole thing took me 8 minutes. Most of that was reading the docs.

What I learned

  • The MCP SDK handles all the transport layer — you just define tools
  • StdioServerTransport means your server runs as a subprocess. No HTTP, no port conflicts
  • Error handling is important. If your tool crashes, the whole MCP connection breaks

Want to try it?

npm install -g mcp-hub
mcp-hub search mcp

Built with mcp-hub. If this helps, buy me a coffee.

來源:DEV Community · dev.to