使用说明
配备Cursor AI的MCP服务器实验室
构建您自己的MCP服务器/实践集
将Cursor和MCP相结合,逐步实现各种工具(Math、文件搜索、PDF/Office RAG、Web搜索摘要、基于SSE的聊天机器人、Function Calling)。
另外,执行从Smidery加载MCP服务器的操作。
- 顺序思维
- 塔维利
- 勇敢的搜索
本课程也将在ClaudeDesktop中进行实践。
使用API
- OpenAI API
- Tavily API:LLM的搜索API,用于实时获取和总结Web搜索结果
- Brave Search API:一个安全、快速的搜索引擎,帮助您在不收集用户信息的情况下查找所需信息。
- Google maps API:提供地图查看、寻路、周边搜索等功能
文件夹结构
MCP_Server/
├─ Call_Weather_API.py # OpenAI Function Calling + OpenWeather API 데모
├─ Cursor/
│ ├─ order.py # 주문 객체 생성 유틸
│ └─ payment.py # 결제/배송/영수증 처리 유틸
├─ explorer-server/
│ └─ main.py # C:/ 전체 파일명 검색 MCP 서버(File-Search)
├─ math-server/
│ └─ math_server.py # 사칙연산 도구 MCP 서버(Math)
├─ rag-server/
│ ├─ main.py # PDF 기반 RAG MCP 서버(PDF-RAG)
│ ├─ office.py # Word/Excel 기반 RAG MCP 서버(Office-RAG)
│ ├─ 스마트팜.pdf
│ └─ office/
│ ├─ 미세먼지가 치매에 미치는 영향.docx
│ ├─ 자동차_리뷰.xlsx
│ ├─ 책_리뷰.xlsx
│ └─ 한옥의 장점과 단점.docx
├─ test-server/
│ ├─ function_calling.py # LangChain Tool + OpenAI Functions 에이전트 데모
│ ├─ main.py # 간단 인사 스크립트
│ ├─ mcp_basic.py # 최소 MCP 서버(add) 예제
│ ├─ mcp_client.py # SSE 클라이언트 예제(직접 함수 호출 데모 포함)
│ ├─ mcp_sample.py # (샘플 예제 파일)
│ ├─ mcp_server.py # GPT-4o MCP 서버(ask_gpt)
│ ├─ pyproject.toml # test-server 의존성 정의 예시
│ ├─ sse_client.py # SSE 클라이언트: /sse 엔드포인트 접속
│ └─ sse_server.py # FastAPI + MCP + SSE 서버(chatbot)
└─ web-search-server/
└─ main.py # Tavily 기반 웹 검색 요약 MCP 서버(WebSearch)
事前准备
- 建议使用Python 3.11或更高版本
- 安装必需的软件包
pip install -U \
mcp \
langchain langchain-openai langchain-community \
chromadb \
python-dotenv requests \
pandas openpyxl python-docx \
fastapi uvicorn
# 함수호출/날씨 데모에 필요
pip install -U openai
- 环境变量(.env)设置示例
OPENAI_API_KEY=sk-... # OpenAI 키
TAVILY_API_KEY=tvly-... # Tavily 웹 검색 API 키
Cursor集成(MCP服务器注册)
c:\Users\user\.cursor\mcp.json 示例:
{
"mcpServers": {
"math-server": {
"command": "C:/Users/user/anaconda3/python.exe",
"args": ["C:/Users/user/MCP_Server/math-server/math_server.py"]
},
"rag-server": {
"command": "C:/Users/user/anaconda3/python.exe",
"args": ["C:/Users/user/MCP_Server/rag-server/main.py"]
},
"file-search": {
"command": "C:/Users/user/anaconda3/python.exe",
"args": ["C:/Users/user/MCP_Server/explorer-server/main.py"]
},
"office-server": {
"command": "C:/Users/user/anaconda3/python.exe",
"args": ["C:/Users/user/MCP_Server/rag-server/office.py"]
},
"web-search": {
"command": "C:/Users/user/anaconda3/python.exe",
"args": ["C:/Users/user/MCP_Server/web-search-server/main.py"]
},
"mcp-sequentialthinking-tools": {
"command": "npx",
"args": [
"-y",
"@smithery/cli@latest",
"run",
"@xinzhongyouhai/mcp-sequentialthinking-tools",
"--key",
"key"
]
},
"brave-search": {
"command": "npx",
"args": [
"-y",
"@smithery/cli@latest",
"run",
"@smithery-ai/brave-search",
"--key",
"key",
"--profile",
"profile"
]
}
}
}
每个练习的执行和使用方法
1)Math MCP服务器(math-server/math_server.py)
- 运行:
python math-server/math_server.py - 工具
add(a, b)添加Subtract(a, b)减号
2)文件搜索MCP服务器(explorer-server/main.py)
- 运行:
python explorer-server/main.py - 工具
find_file(keyword)→基本路线C:/在中按文件名搜索。必要时源的ROOT_DIR可更改
3)PDF RAG MCP服务器(rag-server/main.py)
- 运行:
python rag-server/main.py - 工具
ask_pdf(query)→rag-server/스마트팜.pdf分割/嵌入后基于搜索的响应
- 注意:要使用其他PDF,请使用源的
PDF_PATH修改
4)Office RAG MCP服务器(rag-server/office.py)
- 运行:
python rag-server/office.py - 工具
ask_office(query)→rag-server/office/加载、拆分、嵌入我的整个Word/Excel文档并基于搜索进行响应
- 所需软件包:
pandas,openpyxl,python-docx
5)Web搜索摘要MCP服务器(web-search-server/main.py)
- 运行:
python web-search-server/main.py - 工具
web_search(query)→使用Tavily API搜索后的GPT摘要
- 环境变量:
TAVILY_API_KEY需要,还需要OpenAI密钥 - 型号:基本
gpt-4(摘要)。必要时gpt-4o可以更改为等
6)基于SSE的聊天机器人服务器/客户端(test-server/sse_server.py, test-server/sse_client.py)
- 服务器运行:
python test-server/sse_server.py→http://127.0.0.1:3000/sse - 客户端运行:
python test-server/sse_client.py http://127.0.0.1:3000/sse - 工具
- 服务器内部MCP工具
chat(input)→GPT-4o对话
- 服务器内部MCP工具
7) OpenAI函数调用+代理실습
- LangChain功能代理:
python test-server/function_calling.py- 已注册
add/subtract自动选择并运行工具
- 已注册
- 调用OpenAI函数+天气API:
python Call_Weather_API.py- 文件中
openai_api_key,weather_api_key需要设置值
- 文件中
8) Claude桌面
- 谷歌地图
- 塔维利
9)其他样品
test-server/mcp_basic.py:最小MCP服务器(add)→python test-server/mcp_basic.pytest-server/mcp_server.py:GPT-4o MCP服务器(ask_gpt)→python test-server/mcp_server.pytest-server/mcp_client.py:简单呼叫演示(相同流程呼叫)→python test-server/mcp_client.py
实践目标摘要
- 了解如何创建MCP服务器和Cursor集成
- 基于LangChain的RAG实现(PDF、Word/Excel)
- 配置外部搜索API(Tavily)+LLM摘要管道
- 通过服务器-客户端(SSE)实现基于流的MCP通信
- https://smithery.ai/从中装入mcp服务器
- 体验OpenAI Function Calling和工具选择代理
故障射击
- OpenAI/Tavily密钥设置:
.env在。OPENAI_API_KEY,TAVILY_API_KEY添加 - Excel加载错误:
openpyxl需要安装(pip install openpyxl) - Chroma/嵌入错误:
chromadb,langchain-community验证和重新安装版本 - Windows路径问题:验证执行路径/绝对路径;
mcp.json检查的python路径
