模型上下文协议服务器

一个用于开发MCP(模型上下文协议)服务器的教程项目,提供多种工具如时间查询、计算器和项目分析等,适用于AI工具集成和开发测试。

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

使用说明

我的第一个mcp

模型上下文协议(MCP)服务器开发教程-第一台MCP服务器

介绍

本项目是学习开发模型上下文协议(MCP)服务器的教程。实现可在Claude Code等AI工具中使用的简单MCP服务器。

提供工具

Tool 说明 使用示例
get_current_time 查询当前时间“现在几点了?”
calculate 四则运算计算器 “请计算123+456”
get_random_number 生成随机数字“请抽取彩票号码”
reverse_string 颠倒字符串 “hello颠倒”
get_server_info 查询服务器信息“MCP服务器信息”
analyze_structure 项目结构分析 “展示项目结构”
analyze_dependencies 依赖性分析 “请分析package.json”
count_lines 代码行数统计 “告诉我代码行数”
server_status 查询服务器状态“请检查服务器状态”

截图

在Claude Code中运行Tool

“请计算123加456”→运行calculate Tool→“123+456=579”

MCP服务器自动检测

.mcp.json 项目进入文件时自动检测

MCP检查器UI

使用MCP Inspector测试和调试服务器

MCP检查工具목록

5개 工具(get_current_time、compute、get_random_number、reverse_string、get_server_info)

安装

要求

  • Node.js 20+
  • npm或pnpm

安装方法

# 저장소 클론
git clone https://github.com/dh1789/my-first-mcp.git
cd my-first-mcp

# 의존성 설치
npm install

# 빌드
npm run build

使用方法

连接到Claude Code

# MCP 서버 등록
claude mcp add my-first-mcp -- node /path/to/my-first-mcp/dist/index.js

# 연결 확인
claude mcp list

使用MCP Inspector进行测试

npm run inspect
# 브라우저에서 http://localhost:6274 접속

直接运行

npm start

开发

项目结构

my-first-mcp/
├── src/
│   ├── index.ts              # MCP 서버 진입점
│   ├── tools.ts              # Day 1: 기본 Tool 로직
│   ├── resources.ts          # Day 2: Resource 로직
│   ├── prompts.ts            # Day 2: Prompt 로직
│   ├── project-analyzer.ts   # Day 3: 프로젝트 분석기
│   ├── advanced.ts           # Day 5: 고급 패턴 (캐싱, 보안, 로깅)
│   └── *.test.ts             # 유닛 테스트
├── dist/                     # 빌드 결과물
├── package.json
├── tsconfig.json
└── vitest.config.ts

脚本

npm run build     # TypeScript 빌드
npm test          # 유닛 테스트 실행 (31 tests)
npm run test:watch # 테스트 watch 모드
npm run inspect   # MCP Inspector 실행
npm start         # 서버 실행

测试

包括TDD方式的120个单元测试:

npm test

# 결과
# ✓ src/tools.test.ts (31 tests)
# ✓ src/resources.test.ts (11 tests)
# ✓ src/prompts.test.ts (13 tests)
# ✓ src/project-analyzer.test.ts (18 tests)
# ✓ src/advanced.test.ts (30 tests)
# ✓ src/package-config.test.ts (14 tests)
# ✓ src/shebang.test.ts (3 tests)
# Test Files  7 passed (7)
# Tests       120 passed (120)

技术堆栈

  • 运行时:Node.js 20+
  • 语言:TypeScript 5.7
  • MCP-SDK:@modelcontextprotocol/sdk
  • 验证:zod
  • 测试:邀请

相关文档

许可证

MIT许可证-可自由使用、修改和分发。

贡献

欢迎话题和PR!

  1. 分叉
  2. 创建Feature branch(git checkout -b feature/amazing-feature)
  3. 承诺(git commit -m 'Add amazing feature')
  4. 推(git push origin feature/amazing-feature)
  5. 创建Pull Request

作者