使用说明
MCP Multi-API服务器🌉
一个模型上下文协议(MCP)服务器,将AI/LLM与多个真实世界的API(包括天气、金融和新闻服务)连接起来。该服务器充当标准化接口,允许任何兼容MCP的AI应用程序与外部API无缝交互,而无需进行自定义集成工作。
🚀 特性
- Multi-API支持:天气(OpenWeatherMap)、金融(Alpha Vantage)、新闻(NewsAPI)
- 符合MCP协议:全面实施Anthropic的MCP标准
- 智能高速缓存:可配置的基于TTL的缓存,以减少API调用
- 速率限制:遵守API配额的内置速率限制
- 综合录井:基于Winston的多传输日志记录
- 错误处理:强大的错误处理,带有有意义的错误消息
- TypeScript:完全打字,以获得更好的开发人员体验
📋 先决条件
- Node.js 18+
- npm或纱线
- API密钥用于:
🛠️ 安装
- 克隆存储库:
git clone https://github.com/yourusername/mcp-multi-api-server.git
cd mcp-multi-api-server
- 安装依赖项:
npm install
- 复制环境模板并添加您的API密钥:
cp .env.example .env
- 编辑
.env并添加您的API密钥:
OPENWEATHER_API_KEY=your_key_here
ALPHA_VANTAGE_API_KEY=your_key_here
NEWS_API_KEY=your_key_here
- 构建项目:
npm run build
🚀 用法
启动服务器
npm start
对于自动重新加载的开发:
npm run dev
🏗️ 建筑
src/
├── index.ts # Entry point & MCP server setup
├── api/ # API client implementations
├── tools/ # MCP tool definitions
├── utils/ # Utilities (cache, rate limiter, logger)
└── types/ # TypeScript type definitions
⚙️ 配置
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
OPENWEATHER_API_KEY |
OpenWeatherMap API密钥 | 必需 |
ALPHA_VANTAGE_API_KEY |
AlphaVantage API密钥 | 必需 |
NEWS_API_KEY |
NewsAPI密钥 | 必填 |
MCP_SERVER_PORT |
服务器端口 | 3000 |
LOG_LEVEL |
日志级别 | 信息 |
CACHE_TTL_WEATHER |
天气缓存TTL(秒) | 300 |
CACHE_TTL_FINANCE |
财务缓存TTL(秒) | 60 |
CACHE_TTL_NEWS |
新闻缓存TTL(秒) | 600 |
RATE_LIMIT_REQUESTS |
速率限制请求 | 100 |
RATE_LIMIT_WINDOW |
速率限制窗口(毫秒) | 60000 |
🧪 测试
运行测试脚本:
npm test
使用MCP检查员(目视测试)
# Install MCP Inspector globally
npm install -g @modelcontextprotocol/inspector
# Run the inspector
mcp-inspector node dist/index.js
这将打开一个web界面 http://localhost:5173 您可以在哪里:
- 查看所有可用工具
- 交互式测试工具调用
- 查看请求/响应日志
手动STDIO测试
# Start the server
node dist/index.js
# Run the test script
node dist/test-runner.js
## Testing Individual APIs
### Test Weather API
```bash
# Create a test file: test-weather.ts
import { getCurrentWeather, getWeatherForecast } from './src/api/weather.js';
async function testWeather() {
try {
console.log('Testing current weather...');
const current = await getCurrentWeather('London');
console.log('Current weather:', current);
console.log('\nTesting forecast...');
const forecast = await getWeatherForecast('Paris', 3);
console.log('Forecast:', forecast);
} catch (error) {
console.error('Error:', error);
}
}
testWeather();
运行:
npx tsx test-weather.ts
测试金融API
import { getStockQuote, getCurrencyExchange } from './src/api/finance.js';
async function testFinance() {
try {
console.log('Testing stock quote...');
const stock = await getStockQuote('AAPL');
console.log('Stock:', stock);
console.log('\nTesting currency exchange...');
const exchange = await getCurrencyExchange('USD', 'EUR');
console.log('Exchange:', exchange);
} catch (error) {
console.error('Error:', error);
}
}
testFinance();
测试新闻API
# Create a test file: test-news.ts
import { getTopHeadlines, searchNews } from './src/api/news.js';
async function testNews() {
try {
console.log('Testing headlines...');
const headlines = await getTopHeadlines('us', 'technology');
console.log('Headlines:', headlines);
console.log('\nTesting search...');
const search = await searchNews('artificial intelligence');
console.log('Search results:', search);
} catch (error) {
console.error('Error:', error);
}
}
testNews();
