内容
# MCP 工具
这是一个用于创建 MCP (Model Context Protocol) 工具的模块化工具包。每个工具都是一个可以独立使用的独立可执行文件。
https://github.com/modelcontextprotocol/typescript-sdk
## 前提条件
- Node.js 23+ (或 Bun/Deno/任何支持运行 .ts 文件的东西)
- Bun (用于构建可执行文件)
## 安装 Bun
如果您没有安装 Bun,您可以使用以下方法之一进行安装:
### macOS 和 Linux
```bash
# Using curl (recommended)
curl -fsSL https://bun.sh/install | bash
# Using Homebrew
brew install oven-sh/bun/bun
# Using npm
npm install -g bun
```
### Windows
```bash
# Using PowerShell
powershell -c "irm bun.sh/install.ps1|iex"
# Using npm
npm install -g bun
# Using Scoop
scoop install bun
```
通过运行以下命令验证您的安装:
```bash
bun --version
```
## 安装
```bash
pnpm install
```
## 项目结构
该项目演示了构建 MCP 工具的模块化方法:
- 每个工具都在 `src` 目录中自己的 TypeScript 文件中定义
- 每个工具都可以构建到 `bin` 目录中的独立可执行文件中
- 主 `index.ts` 提供了一个简单的 "Hello World" 示例
### 可用工具
1. **Hello** (`src/index.ts`): 一个简单的问候工具
2. **Weather** (`src/weather.ts`): 获取某个州的天气警报
3. **Blog** (`src/blog.ts`): 用于博客文章创建和 frontmatter 生成的工具
## 创建您自己的工具
要创建一个新工具:
1. 在 `src` 目录中创建一个新的 TypeScript 文件(例如,`src/mytool.ts`)
2. 使用现有工具作为模板
3. 将构建脚本添加到 `package.json`:
```json
"build:mytool": "mkdir -p bin && bun build src/mytool.ts --compile --minify --sourcemap --outfile bin/mcp-mytool"
```
4. 更新 `build:all` 脚本以包含您的新工具
## 用法
### 构建可执行文件
```bash
# Build all tools
pnpm build
# Build a specific tool
pnpm build:hello
pnpm build:weather
pnpm build:blog
```
生成的可执行文件将在 `bin` 目录中,可以直接运行:
```bash
./bin/mcp-hello
./bin/mcp-weather
./bin/mcp-blog
```
## Cursor 笔记
将这些工具与 Cursor 一起使用时,始终使用可执行文件的完整路径:
```
/path/to/your/project/bin/mcp-hello
/path/to/your/project/bin/mcp-weather
/path/to/your/project/bin/mcp-blog
```
或者,您可以直接使用 Node 运行 TypeScript 文件:
```
/path/to/node ~/path/to/project/src/index.ts
```
## 测试
```bash
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage
```