MCP课程代码
此存储库包含多个MCP(模型上下文协议)服务器示例和一个客户端测试工具。每个项目都展示了构建和使用MCP服务器的不同方面。
项目概述
- 示例1-MCP:Math MCP服务器-提供基本的数学运算和数学笑话
- 示例3-Priv Esc:待办事项列表MCP服务器-管理用户(Alice和Bob)的待办事项列表
- Ex4-选择服务器:天气MCP服务器-返回与天气相关的笑话
- mcp-lite:MCP客户端测试器-用于测试和与MCP服务器交互的工具
先决条件
- Node.js >= 18.0.0
- npm >= 9.0.0
要检查您的版本,请执行以下操作:
node --version
npm --version安装
每个项目都有自己的依赖关系。导航到每个项目目录并安装依赖项:
Ex1-MCP(数学服务器)
cd "Ex1 - MCP"
npm installEx3-Priv Esc(待办事项列表服务器)
cd "Ex3 - Priv Esc"
npm installEx4-选择服务器(天气服务器)
cd "Ex4 - Select server"
npm installmcp-lite(mcp客户端测试仪)
cd mcp-lite
npm install可选-全局安装:
cd mcp-lite
npm install -g这使得 mcp-client 全球可用的命令。
用法
示例1-MCP:数学服务器
提供数学运算和数学笑话的服务器。
运行服务器
cd "Ex1 - MCP"
npm start或者直接:
node src/index.js可用工具
- 添加:添加两个数字(
{ "a": 5, "b": 3 }) - 减去:减去两个数字(
{ "a": 10, "b": 4 }) - 乘:将两个数字相乘(
{ "a": 7, "b": 6 }) - 划分:除以两个数字(
{ "a": 20, "b": 4 }) - 力量:将数字提高到幂(
{ "base": 2, "exponent": 8 }) - get_math_job:看一个随机的数学笑话(
{})
Claude桌面配置
添加到您的Claude Desktop配置文件(~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上):
{
"mcpServers": {
"math-server": {
"command": "/Users/katiepaxtonfear/Documents/MCP Course Code/Ex1 - MCP/run-server.sh"
}
}
}备注:更新路径以匹配您的实际项目位置。确保 run-server.sh 可执行:
chmod +x "Ex1 - MCP/run-server.sh"Ex3-Priv Esc:待办事项列表服务器
为用户(Alice和Bob)管理待办事项列表的服务器。
运行服务器
cd "Ex3 - Priv Esc"
npm start或者直接:
node src/index.js可用工具
- get_所有:获取用户的所有待办事项
{ "email": "alice@example.com" }- create_todo:为用户创建新的待办事项
{
"email": "bob@example.com",
"title": "Review pull request #123",
"priority": "high",
"status": "pending"
}- 标记全部完成:将待办事项标记为已完成
{
"email": "alice@example.com",
"todo_id": 1
}Claude桌面配置
添加到您的Claude Desktop配置文件中:
{
"mcpServers": {
"todo-list-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex3 - Priv Esc/src/index.js"
]
}
}
}备注:更新路径以匹配您的实际项目位置。
Ex4-选择服务器:天气服务器
返回与天气相关的笑话(带有幽默转折)的服务器。
运行服务器
cd "Ex4 - Select server"
npm start或者直接:
node src/index.js可用工具
- 预测:获取天气预报(实际上返回一个与天气有关的笑话)
{}Claude桌面配置
添加到您的Claude Desktop配置文件中:
{
"mcpServers": {
"weather-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex4 - Select server/src/index.js"
]
}
}
}备注:更新路径以匹配您的实际项目位置。
mcp-lite:mcp客户端测试仪
用于测试和与MCP服务器交互的命令行工具。可用于安全测试、工具枚举和服务器配置分析。
配置
创建一个 MCPserver.json 文件在 mcp-lite 目录(或指定一个自定义路径 --config):
{
"mcpServers": {
"math-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex1 - MCP/src/index.js"
]
},
"todo-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex3 - Priv Esc/src/index.js"
]
},
"weather-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex4 - Select server/src/index.js"
]
}
}
}备注:更新所有路径以匹配您的实际项目位置。
常用命令
列出所有服务器上的所有工具:
cd mcp-lite
npm start -- list
# or if installed globally:
mcp-client list列出特定服务器上的工具:
npm start -- list --server math-server
# or
mcp-client list --server math-server列出所有已配置的服务器:
npm start -- servers
# or
mcp-client servers获取服务器信息:
npm start -- info math-server
# or
mcp-client info math-server生成工具调用模板:
npm start -- generate math-server add
# or
mcp-client generate math-server add调用工具:
npm start -- call math-server add --args '{"a":5,"b":3}'
# or
mcp-client call math-server add --args '{"a":5,"b":3}'使用文件中的参数调用工具:
npm start -- call math-server add --args-file ./args.json
# or
mcp-client call math-server add --args-file ./args.jsonJSON输出(用于自动化):
npm start -- list --json
# or
mcp-client list --json自定义配置文件:
npm start -- list --config /path/to/config.json
# or
mcp-client list --config /path/to/config.json有关更详细的使用信息,请参阅 mcp-lite自述文件.
Claude桌面配置
要在Claude Desktop中使用多个服务器,请将所有服务器配置合并到一个服务器中 mcpServers 对象:
macOS位置: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows位置: %APPDATA%\Claude\claude_desktop_config.json
组合配置示例:
{
"mcpServers": {
"math-server": {
"command": "/Users/katiepaxtonfear/Documents/MCP Course Code/Ex1 - MCP/run-server.sh"
},
"todo-list-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex3 - Priv Esc/src/index.js"
]
},
"weather-server": {
"command": "node",
"args": [
"/Users/katiepaxtonfear/Documents/MCP Course Code/Ex4 - Select server/src/index.js"
]
}
}
}重要提示:
- 更新所有路径以匹配您的实际项目位置
- 找到你的Node.js路径
which node(macOS/Linux)或where node(Windows) - 确保
run-server.sh可执行:chmod +x "Ex1 - MCP/run-server.sh" - 更新配置后,重新启动Claude Desktop
故障排除
服务器无法启动
- 检查Node.js安装:
which node # macOS/Linux
where node # Windows- 验证是否安装了依赖项:
cd
npm install- 手动测试服务器:
cd
npm start- 检查文件权限 (对于shell脚本):
chmod +x "Ex1 - MCP/run-server.sh"Claude桌面问题
- 检查克劳德桌面日志 用于错误消息
- 验证路径 配置文件中的信息是正确和绝对的
- 重新启动克劳德桌面 进行配置更改后
- 手动测试服务器 首先,确保他们独立工作
mcp-lite连接问题
- 验证服务器配置 在
MCPserver.json - 检查服务器命令是否可用 在你的路径中
- 确保所需的环境变量 已设置(如果有)
- 单独测试服务器 使用
npm start在每个项目目录中
项目结构
MCP Course Code/
├── Ex1 - MCP/ # Math MCP Server
│ ├── src/
│ │ └── index.js
│ ├── package.json
│ ├── run-server.sh
│ └── claude_desktop_config.json
├── Ex3 - Priv Esc/ # Todo List MCP Server
│ ├── src/
│ │ └── index.js
│ ├── package.json
│ └── claude_desktop_config.json
├── Ex4 - Select server/ # Weather MCP Server
│ ├── src/
│ │ └── index.js
│ ├── package.json
│ └── claude_desktop_config.json
└── mcp-lite/ # MCP Client Tester
├── src/
│ ├── cli.js
│ ├── mcp-client.js
│ └── oauth-provider.js
├── package.json
└── MCPserver.json.example额外资源
- 每个项目都有自己的详细README,其中包含具体的使用说明
- 有关更多示例和详细文档,请参阅单个项目的自述文件
- MCP文件
许可证
此存储库中的所有项目都在MIT下获得许可。
