MCPO:使用MCP工具为Open-WebUI/Ollama助力
Open WebUI_ 正式支持 MCP Tool 服务器 —— 只要 MCP Tool 服务器由兼容 OpenAPI 的代理(openapi-servers)作为前端 —— 一个专门构建的代理实现可在以下网址获取:👉 https://github.com/open-webui/mcpo。
在尝试了本地设置、试验了各种配置并测试了提示词之后,我发现mcpo是一个简单的代理,它允许你将MCP服务器命令与适用于标准OpenAPI服务器的工具配合使用。这使得将你的工具与大语言模型智能体和应用程序连接起来变得很容易。
在本文中,我们将首先探究其工作原理,然后创建一个全新的MCP服务器,启动它,并将其作为一个工具添加到open-webui中。
MCPO架构
图。MCPO架构
根据解决方案架构,MCPO使用标准输入/输出(stdio)传输直接与MCP服务器进行交互。随后,在与Open-WebUI交互时,所有MCP通信都会转换为RESTful API。
准备好了,我们现在就试试吧!
目录
- 前提条件
- 配置MCPO服务器
- 配置Open-WebUI工具
- 测试工具调用
- 创建全新的MCP服务器:资费新闻反应
- 生成关税新闻反应MCP服务器代码
- 在Roo代码中测试新的MCP服务器工具调用
- 将新的MCP服务器添加到MCPO服务器配置中
- 将新的MCP服务器添加到Open-WebUI
- 测试工具调用
- 最终想法
前提条件
- 如果本地没有Ollama,请下载并安装,详见:https://ollama.com/download
- 如果您本地没有open-webui,请下载并安装,详见:https://github.com/open-webui/open-webui
- NodeJS(可译为“节点.js”,是一个基于Chrome V8引擎的JavaScript运行时环境,常用于构建网络服务器和网络应用程序 ),此处保留英文表述在技术语境中也较为常见 。如果按要求翻译则为“节点.js” 。
- Python 3.11(需要open-webui)/pip或使用uv(
curl -LsSf https://astral.sh/uv/install.sh | sh
) - VS Code + Roo Code + Google Gemini 2.5 Pro(可选,用于新MCP服务器的代码生成)
配置MCPO服务器
- 创建一个新的Python虚拟环境
$ python -m venv .venv
$ source .venv/bin/activate
2. 安装MCPO服务器
$ pip install mcpo
3. 安装MCP服务器
从这里选择服务器:https://github.com/modelcontextprotocol/servers。目前,我们尝试安装以下3个服务器,它们分别是time、memory和fetch。
# 1.time mcp server
$ pip install mcp-server-time# 2.memory mcp server
$ npm install @modelcontextprotocol/server-memory# 3.fetch mcp server
$ pip install mcp-server-fetch
4. 接下来,创建一个 config.json
文件,这样我们就可以通过一个MCPO服务器连接到多个《我的世界》服务器实例。
❯ cat config.json
{"mcpServers": {"memory": {"command": "npx","args": ["-y", "@modelcontextprotocol/server-memory"]},"time": {"command": "uvx","args": ["mcp-server-time", "--local-timezone=America/New_York"]},"fetch": {"command": "uvx","args": ["mcp-server-fetch"]}}
}
5. 运行MCPO服务器
$ uvx mcpo --config config.json --port 8001## result log
❯ uvx mcpo --config config.json --port 8001
Starting MCP OpenAPI Proxy with config file: config.json
INFO: Started server process [1190222]
INFO: Waiting for application startup.
Knowledge Graph MCP Server running on stdio
好的,至此,我们完成了MCPO服务器的设置。
6. 使用MCPO生成的文档链接验证MCP服务器。
图。MCPO为MCP工具生成的API文档
目前看起来不错。MCP服务器已启动并运行,其中一个工具名为fetch。
配置Open-WebUI工具
接下来,要在Open-WebUI中将MCPO端点添加为工具,请点击设置>工具>+。然后,输入MCPO工具的URL,再点击保存。
请见以下屏幕截图:
**Project Goal:**
Create a Python-based MCP server that provides a tool to search for recent news articles concerning international reactions to the US tariffs announced in April 2025, supporting both stdio and SSE transports.**1. Business Requirements:*** The server should enable users (or AI agents) to query for news articles about how different countries are reacting to the specified US tariffs.
* The focus should be on retrieving relevant news published within the last week.
* The server should be packaged for easy distribution and installation using pip.**2. Technical Requirements:*** **MCP Server Implementation:*** Implement using the `mcp` Python SDK (package name: `mcp`).* Support both standard I/O (`stdio`) and Server-Sent Events (`sse`) transport mechanisms, selectable via a `--transport` command-line argument.* Employ the decorator-based API (`@mcp_server.list_tools`, `@mcp_server.call_tool`) provided by `mcp.server.lowlevel.Server`.* Use `click` for command-line argument parsing (`--transport`, `--port`).* Use `starlette` and `uvicorn` to handle the web server component for SSE transport.* **Core Tool (`get_tariff_reaction_news`):*** **Functionality:** Searches the internet using the DuckDuckGo search engine (`duckduckgo-search` library) for news articles related to tariff reactions.* **Search Query Construction:*** Base query: "reactions to US tariffs April 2025"* If `country` input is provided: "reactions from [Country Name] to US tariffs April 2025"* Append `additional_keywords` if provided.* **Filtering:** Limit search results to news published within the last week (using `timelimit='w'` with `duckduckgo-search`).* **Ranking:** Default DuckDuckGo relevance/ranking.* **Tool Schemas (using Pydantic):*** **Input (`GetTariffReactionNewsInput`):*** `country`: `Optional[str]` - The specific country to focus the search on.* `additional_keywords`: `Optional[str]` - Extra terms to add to the query.* **Internal Output Models:*** `SearchResultItem`: Defines the structure for a single result (`title`, `url`, `snippet`, `source`, `published_date`).* `SearchSuccessOutput`: Contains a `list[SearchResultItem]` on success.* `SearchErrorOutput`: Contains an `error: str` field on failure.* **MCP Tool Return Type:** The `@mcp_server.call_tool` decorated function will return `list[mcp.types.TextContent]`. The `SearchSuccessOutput` or `SearchErrorOutput` models will be serialized into the `text` field of the `TextContent` block. Tool execution errors should be raised as standard Python exceptions (e.g., `ValueError`, `Exception`), which the `mcp` library will format into MCP error responses.* **Dependencies:*** `mcp[cli]>=1.6.0`: For MCP server/tool implementation and types.* `duckduckgo-search>=2025.4.1`: For executing the web search.* `pydantic>=2.11`: For defining input/output schemas and validation.* `anyio>=4.0`: Required by the `mcp` library for running the asynchronous server via stdio.* `click>=8.0`: For command-line argument parsing.* `starlette>=0.27`: For SSE transport web framework.* `uvicorn[standard]>=0.23`: For running the Starlette application.* `requests>=2.25`: (Included but not directly used).* **Development Environment:*** Use a Python virtual environment (e.g., `venv`) to manage dependencies.* Include a `requirements.txt` file (generated via `pip freeze`).* **Packaging:*** Use a `pyproject.toml` file with a `setuptools` build backend to make the server installable via `pip`.* Include a console script entry point (`tariff-news-server`) that runs the main CLI function (`main_cli`).* **Code Structure:*** Organize code logically (e.g., `schemas.py`, `tool.py`, `server.py` within a `src/tariff_news_server` package).* **Execution:*** **Stdio (Default):** Run via `python -m tariff_news_server.server` or `tariff-news-server` (if installed). Configure in MCP settings using the `python -m ...` command.* **SSE:** Run via `tariff-news-server --transport sse [--port <port>]`. Configure in MCP settings using the console script command and arguments, or as a remote SSE server pointing to the correct URL (e.g., `http://localhost:8000/mcp/sse`).**3. Useful Resources (Original Links):*** Python MCP SDK (`mcp` package): [https://github.com/modelcontextprotocol/python-sdk](https://github.com/modelcontextprotocol/python-sdk)
* Example MCP Servers: [https://github.com/modelcontextprotocol/python-sdk/tree/main/examples/servers](https://github.com/modelcontextprotocol/python-sdk/tree/main/examples/servers) (Specifically, `simple-tool` provides a relevant pattern).**4. Documentation:**
- generate a soluton diagram for tariff news MCP server
- generate documentation for source code
Based on recent news articles retrieved using the tariff-news-server, Mexico was spared from the latest round of US tariffs announced in April 2025.Here's a summary of the reaction:Relief and Celebration: Mexico celebrated dodging these new tariffs, which targeted many other US trading partners.
Continued Free Trade: This exclusion suggests that the existing free trade relationship between the US and Mexico might survive this round of tariff implementations.
Awareness of Broader Impact: Despite being spared directly, Mexico acknowledges that the global economic uncertainty created by these widespread tariffs could still negatively affect its economy.
In essence, Mexico is relieved to have avoided the direct impact of the tariffs but remains cautious about the indirect consequences stemming from global trade instability.
$ git clone https://github.com/minyang-chen/AI-powered-Development.git
$ cd tariff-news-server
$ pip install -e .
{"mcpServers": {"memory": {"command": "npx","args": ["-y", "@modelcontextprotocol/server-memory"]},"time": {"command": "uvx","args": ["mcp-server-time", "--local-timezone=America/New_York"]},"fetch": {"command": "uvx","args": ["mcp-server-fetch"]},"tariff": {"command": "python3","args": ["-m","tariff_news_server.server"]}}
}
$ uvx mcpo --config ./config.json --port 8001
## MCPO server LogINFO: 127.0.0.1:33694 - "OPTIONS /tariff/get_tariff_reaction_news HTTP/1.1" 200 OK
Calling get_tariff_reaction_news with arguments: {'country': 'Canada'}
2025-04-03 20:06:39,935 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
2025-04-03 20:06:39,935 - __main__ - INFO - Received call_tool request for tool: get_tariff_reaction_news
2025-04-03 20:06:39,935 - __main__ - INFO - Parsed tool input: country='Canada' additional_keywords=None
2025-04-03 20:06:39,935 - tariff_news_server.tool - INFO - Executing search with query: 'reactions from Canada to US tariffs'
2025-04-03 20:06:40,189 - primp - INFO - response: https://duckduckgo.com/?q=reactions+from+Canada+to+US+tariffs 200
2025-04-03 20:06:41,407 - primp - INFO - response: https://duckduckgo.com/news.js?l=wt-wt&o=json&noamp=1&q=reactions+from+Canada+to+US+tariffs&vqd=4-290943568794945560942460956578934889745&p=-2&df=w 200
2025-04-03 20:06:41,408 - tariff_news_server.tool - INFO - Found 10 results.
2025-04-03 20:06:41,408 - __main__ - INFO - Tool execution result type: <class 'tariff_news_server.schemas.SearchSuccessOutput'>
2025-04-03 20:06:41,408 - __main__ - INFO - Tool succeeded, returning 10 results.
INFO: 127.0.0.1:33694 - "POST /tariff/get_tariff_reaction_news HTTP/1.1" 200 OK