MCP Server Demo

MCP Server Demo

A minimal Model Context Protocol server demo that exposes tools through HTTP API, including greeting, weather lookup, and HTTP request capabilities. Demonstrates MCP server implementation with stdio communication and HTTP gateway functionality.

Category
访问服务器

README

MCP Server Demo

一个最小可用的 Model Context Protocol (MCP) 演示项目:

  • server.js 通过 stdio 方式启动 MCP Server,并注册工具
  • client.js 作为 HTTP API 网关,内部与 MCP Server 建立长连接,将工具暴露为 HTTP 接口
  • tools/ 目录下包含三个示例工具:helloToolweatherToolnetworkTool

运行环境

  • Node.js 18+(推荐 20+)

安装依赖

npm install

启动 HTTP API(会自动启动 MCP Server)

node client.js

启动后控制台会看到:

  • MCP server 已通过 stdio 启动并连接
  • HTTP API 监听在 http://localhost:3000

健康检查

curl http://localhost:3000/health

列出可用工具

curl http://localhost:3000/tools

调用工具(通用)

HTTP 方法:POST 路径:/tools/:name 请求体:工具所需参数(JSON)


工具列表与用法

helloTool

  • 描述:输出问候语
  • 参数:
    • name (string):要问候的名字

示例:

curl -X POST http://localhost:3000/tools/helloTool \
  -H "Content-Type: application/json" \
  -d '{"name":"Han"}'

weatherTool

  • 描述:查询城市天气(示例内置了几座城市,未命中则返回默认值)
  • 参数:
    • city (string):城市名

示例:

curl -X POST http://localhost:3000/tools/weatherTool \
  -H "Content-Type: application/json" \
  -d '{"city":"Beijing"}'

networkTool

  • 描述:请求指定 URL,返回 HTTP 状态、耗时、以及响应摘要/完整内容
  • 参数:
    • url (string, url):目标地址
    • method ("GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE",默认 "GET")
    • timeoutMs (number, 默认 8000):超时时间毫秒
    • fullBody (boolean, 默认 true):是否返回完整 body(若为 JSON 将尝试解析)
    • maxBodyBytes (number, 默认 262144):非 JSON 响应在 fullBody 下返回的最大字节数,超过即截断
    • headers (record<string,string>,可选):请求头
    • query (record<string,string|number|boolean>,可选):追加到 URL 的查询参数
    • body (any,可选):请求体(仅非 GET/HEAD 有效)
    • bodyType ("json" | "text" | "form",默认 "json"):请求体序列化方式

返回字段(核心):

  • statusoklatencyMs
  • contentTypeisJson
  • bodyJson(当响应为 JSON 且解析成功)
  • body(非 JSON 文本,可能被截断)
  • bodySnippet(未开启 fullBody 或 JSON 解析失败时的片段)
  • truncated(是否发生截断)
  • headers(响应头)
  • request(回显请求信息,含 url/method/headers/bodyPreview

示例:

  • GET 带查询参数
curl -X POST http://localhost:3000/tools/networkTool \
  -H "Content-Type: application/json" \
  -d '{
    "url":"https://httpbin.org/get",
    "query": {"q":"hello","page":2},
    "headers": {"X-Debug":"1"}
  }'
  • POST JSON
curl -X POST http://localhost:3000/tools/networkTool \
  -H "Content-Type: application/json" \
  -d '{
    "url":"https://httpbin.org/post",
    "method":"POST",
    "bodyType":"json",
    "body": {"foo":"bar"}
  }'
  • POST 表单
curl -X POST http://localhost:3000/tools/networkTool \
  -H "Content-Type: application/json" \
  -d '{
    "url":"https://httpbin.org/post",
    "method":"POST",
    "bodyType":"form",
    "body": {"a":1,"b":"x"}
  }'
  • 发送纯文本
curl -X POST http://localhost:3000/tools/networkTool \
  -H "Content-Type: application/json" \
  -d '{
    "url":"https://httpbin.org/post",
    "method":"POST",
    "bodyType":"text",
    "body":"raw text"
  }'

目录结构

.
├─ client.js        # HTTP 网关,连接 MCP Server 并暴露 /tools/*
├─ server.js        # MCP Server,注册工具并通过 stdio 提供服务
├─ tools/
│  ├─ helloTool.js
│  ├─ weatherTool.js
│  └─ networkTool.js
├─ package.json
└─ README.md

关闭与清理

client.js 支持 Ctrl+C 优雅退出,会尝试关闭与 MCP 的连接及 HTTP 服务器。

常见问题

  • 无法联网:检查本机代理/防火墙;networkTool 默认 8s 超时,可调整 timeoutMs
  • 返回体太大:可通过 maxBodyBytes 控制非 JSON 响应的最大返回字节
  • 需要仅看片段:将 fullBody 设为 false,仅返回 bodySnippet

推荐服务器

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

官方
精选