MCP服务器实践
此存储库包含用于LinkedIn个人资料抓取和天气数据检索的模型上下文协议(MCP)服务器的实现。MCP框架促进了AI服务之间的无缝集成和通信。
概述
- LinkedIn个人资料抓取器:使用全新LinkedIn个人资料数据API获取LinkedIn个人档案数据。
- 气象数据服务:使用美国国家气象局(NWS)API检索天气警报和预报。
先决条件
- Python 3.7+
httpx用于异步HTTP请求python-dotenv环境变量管理mcp用于MCP服务器实现
安装
- 克隆存储库:
git clone https://github.com/mybarefootstory/MCP-Server-Practice-2.git
cd MCP-Server-Practice-2- 安装依赖项:
pip install httpx python-dotenv mcp- 设置环境变量:
- 创建一个 .env 根目录中的文件。 - 添加您的RapidAPI密钥:
RAPIDAPI_KEY=your_rapidapi_key_hereLinkedIn个人资料抓取器
描述
使用全新LinkedIn个人资料数据API获取LinkedIn个人档案数据。服务器已初始化为 FastMCP 并监听检索简档信息的请求。
代码段
from mcp.server.fastmcp import FastMCP
import httpx
import os
from dotenv import load_dotenv
load_dotenv()
RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY")
mcp = FastMCP("linkedin_profile_scraper")
async def get_linkedin_data(linkedin_url: str) -> dict:
# Fetch LinkedIn profile data
...
@mcp.tool()
async def get_profile(linkedin_url: str) -> str:
# Get LinkedIn profile data
...
if __name__ == "__main__":
mcp.run(transport="stdio")气象数据服务
描述
使用NWS API检索天气警报和预报。服务器已初始化为 FastMCP 并提供用于获取警报和预测的工具。
代码段
from mcp.server.fastmcp import FastMCP
import httpx
mcp = FastMCP("weather")
async def make_nws_request(url: str) -> dict:
# Make a request to the NWS API
...
@mcp.tool()
async def get_alerts(state: str) -> str:
# Get weather alerts for a US state
...
@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
# Get weather forecast for a location
...
if __name__ == "__main__":
mcp.run(transport='stdio')用法
- LinkedIn个人资料抓取器:运行服务器并使用
get_profile获取LinkedIn数据的工具。 - 气象数据服务:运行服务器并使用
get_alerts和get_forecast检索天气信息的工具。
