MCP服务器

一个用TypeScript编写的轻量级MCP服务器,用于本地测试Model Context Protocol客户端,提供模拟天气报告功能。

  • by SkyH34D
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 skyh34d/first-mcp

使用说明

基本mcp·TypeScript中的最小mcp服务器

状态: 概念验证——专为使用模型上下文协议客户端进行快速本地测试而设计。

此存储库包含 微型MCP服务器 用TypeScript编写。
它发布了一个工具——fetch-weather--它返回任何城市的模拟天气预报。
使用它来了解传输、工具和模式是如何工作的 @modelcontextprotocol/sdk.


✨ 特性

特性 描述
轻量级 单文件实现(main.ts).
标准运输 通过标准I/O进行通信(非常适合通过管道连接到LLM)。
Zod验证 具有运行时验证的强类型参数。
模拟天气 演示 content 没有外部API的块。

🛠 需求

  • Node.js≥18(ESM和顶级)await 支持)
  • pnpm/npm/yarn安装依赖项
  • ts节点 (可选) 对于dev模式

🚀 快速启动

# 1 — Clone
git clone https://github.com/SkyH34D/basic-mcp.git
cd first-mcp

# 2 — Install
pnpm install           # or: npm install / yarn install

# 3 — Run (non-sense, it's LLM's job)
npx -y tsx main.ts
# The server now listens on STDIN/STDOUT for MCP messages

发送工具调用,例如:

{
  "tool": "fetch-weather",
  "params": { "city": "Madrid" }
}

你会得到:

{
  "content": [
    { "type": "text", "text": "The weather in Madrid is sunny." }
  ]
}

🗂 项目布局

basic-mcp/
├─ main.ts         # Main MCP server
├─ package.json
└─ pnpm-lock.yaml

🧩 运作原理

  1. 实例化 McpServer 带着一个 name 和 version.
  2. 注册 这 fetch-weather 工具via server.tool(),提供:
    • 唯一的ID和描述
    • Zod模式 city
    • 一个异步处理程序,返回 content 数组
  3. 创建 StdioServerTransport 使用标准I/O。
  4. 连接 随着 await server.connect(transport) 然后开始听。

📦 NPM脚本

脚本 运行 描述
dev ts-node 热装式开发。
build tsc 将TypeScript编译为 dist/.
start node 执行编译后的构建。

(编辑 package.json 以适应您的工具。)


📝 一切/想法

  • 用真正的API(例如OpenWeatherMap)替换模拟天气。
  • 添加更多工具——汇率、时区查找等。
  • 切换到WebSocket/HTTP传输以供远程使用。
  • 为工具处理程序编写测试。

📄 许可证

MIT许可证。看 许可证 了解更多信息。