🌦️ 气象MCP服务器演示
一种与模型上下文协议(MCP)兼容的服务器,使用Ollama的LLM功能提供天气信息。此服务器公开了一个 get-weather MCP客户端可以使用该工具检索任何城市的天气信息。
🛠 先决条件
- Node.js v18+
- 奥拉马 在本地安装并运行
- Ollama模型:
llama3(或配置您的首选型号)
🚀 完成设置和安装
✅ 步骤1:克隆并安装```bash
git clone https://github.com/codewith1984/weather-mcp-server-typescript.git
cd weather-mcp-server
npm install
### ⚙️ 步骤2:设置环境```bash
# Copy environment template
cp .env.example .env
编辑.env文件以包含:```bash
OLLAMA_API_URL=http://localhost:11434/api/generate
OLLAMA_MODEL=llama3
### 🤖 步骤3:安装和设置Olama```bash
# Install Ollama (if not already installed)
# Visit https://ollama.com/ for installation instructions
# Start Ollama service
ollama serve
# In another terminal, pull the model
ollama pull llama3
# Verify Ollama is working
curl http://localhost:11434/api/version
```bash
### 步骤4:测试Ollama连接```bash
# Test if Ollama can generate responses
curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt": "Hello world",
"stream": false
}'
步骤5:运行诊断```bash
Run diagnostic to check if everything is working
npm run diagnose
## 🎯 运行MCP服务器
### 启动服务器```bash
npm start
预期产量:
🚀 MCP气象服务器正在启动。..
📡 Ollama网址:http://localhost:11434/api/generate
🤖 型号:骆驼3
✅ MCP服务器已连接并准备就绪!
🔍 MCP检验员测试
方法1:CLI检查器```bash
Install MCP Inspector globally
npm install -g @modelcontextprotocol/inspector
Run inspector
mcp-inspector
Follow the web interface instructions
## ⚙️ 配置
### 环境变量(.env文件)```bash
OLLAMA_API_URL=http://localhost:11434/api/generate
OLLAMA_MODEL=llama3
性能调整
服务器针对快速响应进行了优化:
HTTP请求超时15秒
激进的Olama参数,实现更快的发电速度
从HTTP API回退到CLI(如果需要)
🔧 故障排除
快速诊断```bash
Run the diagnostic script
npm run diagnose
### 常见问题及解决方案
#### 问题:“请求超时”错误```bash
# Check if Ollama is running
ps aux | grep ollama
# Start Ollama if not running
ollama serve
# Check if model is available
ollama list | grep llama3
# If model not found, pull it
ollama pull llama3
问题:“请求超时”错误```bash
Check if Ollama is running
ps aux | grep ollama
Start Ollama if not running
ollama serve
Check if model is available
ollama list | grep llama3
If model not found, pull it
ollama pull llama3
#### 问题:“找不到模型”错误```bash
# List available models
ollama list
# Pull the required model
ollama pull llama3
# Or try a smaller model for faster responses
ollama pull llama3:8b
```bash
#### 问题:连接错误```bash
# Verify Ollama is accessible
curl http://localhost:11434/api/version
# Check if port 11434 is open
netstat -an | grep 11434
# Restart Ollama service
pkill ollama
ollama serve
问题:MCP检查器连接失败```bash
Make sure your server is running
npm start
Check the working directory path is correct
pwd
Verify tsx is available
npm list tsx
### 性能提示```bash
# Use a smaller model for faster responses
ollama pull llama3:8b
# Update .env to use the smaller model
echo "OLLAMA_MODEL=llama3:8b" >> .env
# Monitor system resources
top -p $(pgrep ollama)
📁 项目结构```text
weather-mcp-server/
├── .env # Environment configuration
├── .env.example # Environment template
├── .gitignore # Git ignore rules
├── main.ts # MCP server implementation
├── ollamaClient.ts # Ollama API client
├── diagnose.ts # Diagnostic tool
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
## ✅ 成功检查表
完成此检查表以确保一切正常:
已安装Node.js v18+
Ollama安装并运行(Ollama服务)
模型已下载(ollama pull llama3)
已安装项目依赖项(npm安装)
已配置环境(.env文件存在)
诊断通过(npm运行诊断)
MCP服务器成功启动(npm启动)
MCP检查器连接成功
天气工具响应测试命令
## 🚀 快速启动命令```bash
# Complete setup in one go
git clone https://github.com/your-username/weather-mcp-server.git
cd weather-mcp-server
npm install
cp .env.example .env
ollama serve &
ollama pull llama3
npm run diagnose
npm start
🤝 贡献
- 分叉存储库
- 创建功能分支:git checkout-b功能名称
- 进行更改
- 使用MCP检查员进行测试
- 提交拉取请求
