hello-mcp

hello-mcp

A minimal learning-focused MCP server that demonstrates core primitives like tools and resources through simple greeting functions. It provides a foundational example for connecting AI models to external data using both Streamable HTTP and stdio transports.

Category
访问服务器

README

hello-mcp

A minimal MCP server built as a learning exercise. Uses the Streamable HTTP transport and demonstrates the core MCP primitives: tools and resources.

What is MCP?

The Model Context Protocol (MCP) is an open standard for connecting AI models to external data and capabilities. An MCP server exposes three types of primitives:

Primitive Purpose Initiated by
Tools Do things (side effects allowed) The model
Resources Expose data (read-only) The client/user
Prompts Reusable prompt templates The client/user

Project Structure

src/
  server.js            # Entry point — HTTP transport
  stdio.js             # Entry point — stdio transport (Claude Desktop)
  mcp.js               # MCP server + tool/resource registration
  data/
    greetings.js       # Shared data (language → greeting mapping)
  tools/
    helloWorld.js      # No-input tool
    helloName.js       # Tool with a required string input
    greetName.js       # Tool with error handling and MCP logging
    listLanguages.js   # Tool that exposes the languages list to the model
  resources/
    languages.js       # Static resource exposing available languages

Transport: Streamable HTTP

This server uses the StreamableHTTPServerTransport from @modelcontextprotocol/sdk. It runs as a plain Node.js HTTP server and handles MCP messages at POST /mcp.

Key details:

  • Runs in stateless mode (sessionIdGenerator: undefined) — each request gets a fresh transport instance
  • The client must send Accept: application/json, text/event-stream or the server will reject with 406
  • The transport handles both JSON responses and SSE streams depending on the request

Tools

Tools are registered with server.registerTool(name, config, handler).

  • config.description — shown to the model so it knows when to use the tool
  • config.inputSchema — a plain object of Zod fields; the SDK wraps it in z.object() automatically
  • Input is validated at the transport layer before the handler is called — invalid input returns -32602 without ever reaching your code

hello_world

No inputs. Always returns "Hello, World!".

hello_name

Takes a required name string. Returns "Hello, {name}!".

greet_name

Takes a name string and a language string. Returns a greeting in the specified language, or an isError response if the language isn't supported. Uses sendLoggingMessage to emit info, warning, and debug log messages through the MCP protocol — visible in the MCP Inspector notifications panel.

list_languages

No inputs. Returns the list of supported languages. Useful for Claude Desktop where resources aren't surfaced to the model — the model can call this tool to discover valid options when greet_name returns an error.

Resources

Resources are registered with server.registerResource(name, uri, config, reader).

  • Each resource has a URI (e.g. languages://list) that the client uses to fetch it
  • The reader returns { contents: [{ uri, text }] }
  • Resources are read-only — no side effects

languages://list

Returns a JSON array of supported language keys. Backed by the same greetings.js data used by the greet_name tool.

Running the Server

HTTP Streaming (MCP Inspector, remote clients)

npm install
npm start

The MCP endpoint will be available at http://localhost:3000/mcp.

To run on a different port:

PORT=3001 npm start

stdio (Claude Desktop)

Claude Desktop spawns the process itself — no server needs to be running beforehand. Add the following to ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "hello-mcp": {
      "command": "node",
      "args": ["/absolute/path/to/hello-mcp/src/stdio.js"]
    }
  }
}

Quit and relaunch Claude Desktop after editing the config. Check Claude menu → Settings → Developer to confirm the server shows a green dot.

Testing with MCP Inspector

npx @modelcontextprotocol/inspector

Open the Inspector in Chrome (not Brave — its privacy settings block localhost requests), set the transport to Streamable HTTP, and enter http://localhost:3000/mcp.

You can also test with raw curl:

curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/list",
    "params": {}
  }'

Key Lessons

  • Multiple transports, one servermcp.js is transport-agnostic; server.js (HTTP) and stdio.js (stdio) are separate entry points that both import the same server instance
  • Separation of concernsserver.js owns the HTTP transport, mcp.js owns the MCP server, each tool/resource lives in its own file
  • Single source of truth — shared data in src/data/ is imported by both tools and resources; no duplication
  • Zod validation is free — defining an inputSchema gives you automatic input validation before your handler runs
  • registerTool over tool — the tool() method is deprecated; registerTool() uses a cleaner config object pattern
  • Stateless vs stateful — stateless mode is simpler and sufficient for most use cases; stateful mode adds session tracking via sessionIdGenerator
  • Factory pattern for server-dependent handlers — handlers that need to call server.sendLoggingMessage() can't import server directly (circular dependency). Instead, export a createHandler(server) factory that closes over the server instance and returns the handler function. mcp.js calls createHandler(server) at registration time.
  • isError: true vs throwing — returning isError: true sends the error text back to the model as readable tool output it can reason about; throwing produces a protocol-level JSON-RPC error the model can't see
  • MCP loggingserver.sendLoggingMessage({ level, message }) sends log messages through the MCP protocol to the client; visible in MCP Inspector's notifications panel
  • Resources vs tools in Claude Desktop — Claude Desktop doesn't surface resources to the model; expose the same data as a tool if the model needs to access it

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选