mcpo的简单使用
1.安装依赖
conda create -n mcpo python=3.11
conda activate mcpo
pip install mcpo pip install uv
2.随便从https://github.com/modelcontextprotocol/servers?tab=readme-ov-file
找一个mcp服务使用就行,我这里选的是爬虫
然后安装
pip install mcp-server-fetch
3.然后我们运行
uvx mcpo --port 8000 -- uvx mcp-server-fetch
4.此时在浏览器打开
http://localhost:8000/docs
就可以看到接口文档了
然后写个py脚本测试一下
import requests
import jsondef fetch_webpage(url, max_length=10000, start_index=0, raw=False):"""Fetch content from a URL using the MCP Fetch server.Args:url (str): The URL to fetchmax_length (int): Maximum number of characters to returnstart_index (int): Start content from this character indexraw (bool): Get raw HTML content without markdown conversionReturns:dict: The response from the server containing the fetched content"""try:# Make a POST request to the fetch endpointresponse = requests.post("http://localhost:8000/fetch",json={"url": url,"max_length": max_length,"start_index": start_index,"raw": raw})# Ensure the request was successfulresponse.raise_for_status()# Parse the responsereturn response.json()except Exception as e:return {"error": str(e)}# Example usage
if __name__ == "__main__":# Fetch the specific URL you requestedtarget_url = "https://docs.cline.bot"result = fetch_webpage(target_url)print(result)
over
mcpo出现的意义非常好,节省了我们配置的时间,从此,我们就可以像访问接口一样使用mcp服务了
5.通过配置文件启动多个mcp服务同时
mcpo.json
{"mcpServers": {"fetch": {"command": "uvx","args": ["mcp-server-fetch"]},"time": {"command": "uvx","args": ["mcp-server-time", "--local-timezone=Asia/Shanghai"]}}}
执行下面的命令
uvx mcpo --config mcpo.json --port 8000 --api-key "your-secret-key"
对应接口文档如下所示