当前位置: 首页 > ops >正文

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架构

None

图。MCPO架构

根据解决方案架构,MCPO使用标准输入/输出(stdio)传输直接与MCP服务器进行交互。随后,在与Open-WebUI交互时,所有MCP通信都会转换为RESTful API。

准备好了,我们现在就试试吧!

目录

  • 前提条件
  • 配置MCPO服务器
  • 配置Open-WebUI工具
  • 测试工具调用
  • 创建全新的MCP服务器:资费新闻反应
  • 生成关税新闻反应MCP服务器代码
  • 在Roo代码中测试新的MCP服务器工具调用
  • 将新的MCP服务器添加到MCPO服务器配置中
  • 将新的MCP服务器添加到Open-WebUI
  • 测试工具调用
  • 最终想法

前提条件

  1. 如果本地没有Ollama,请下载并安装,详见:https://ollama.com/download
  2. 如果您本地没有open-webui,请下载并安装,详见:https://github.com/open-webui/open-webui
  3. NodeJS(可译为“节点.js”,是一个基于Chrome V8引擎的JavaScript运行时环境,常用于构建网络服务器和网络应用程序 ),此处保留英文表述在技术语境中也较为常见 。如果按要求翻译则为“节点.js” 。
  4. Python 3.11(需要open-webui)/pip或使用uv(curl -LsSf https://astral.sh/uv/install.sh | sh
  5. VS Code + Roo Code + Google Gemini 2.5 Pro(可选,用于新MCP服务器的代码生成)

配置MCPO服务器

  1. 创建一个新的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服务器。

None

图。MCPO为MCP工具生成的API文档

目前看起来不错。MCP服务器已启动并运行,其中一个工具名为fetch。

配置Open-WebUI工具

接下来,要在Open-WebUI中将MCPO端点添加为工具,请点击设置>工具>+。然后,输入MCPO工具的URL,再点击保存

请见以下屏幕截图:

None

None

None

None


**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

None

None

None

None

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

None

None

None

None

## 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
http://www.xdnf.cn/news/11950.html

相关文章:

  • 渗透测试服务如何全方位评估企业安全状况并揭示潜在缺陷?
  • SpringBoot(七) --- Redis基础
  • 在Windows11上安装 Ubuntu WSL
  • 大语言模型备案与深度合成算法备案的区别与联系
  • Rebel系列数据记录仪:智能车载数据采集专家
  • kafka命令
  • 【unity游戏开发入门到精通——通用篇】AssetBundle(AB包)和AssetBundleBrowser的使用介绍
  • 数据结构期末PTA选择汇总
  • L1-019 谁先倒 (15 分)
  • [安卓/ios辅助工具]给按键精灵脚本做一个日志悬浮窗
  • 制造业数字化转型解决方案及应用
  • rk3588开发板实现磁盘自动挂载vmware共享文件夹设置
  • C学习--内存管理
  • 2. 库的操作
  • 企业如何抵御复杂网络攻击?
  • Delphi用if else实现 select case、switch语句功能,实现case 以字符串为分类条件。
  • Java对象创建过程
  • 操作系统入门:核心概念与设计逻辑
  • 数字孪生在智能制造中的实践:某汽车总装车间的全流程仿真优化
  • https和http有什么区别-http各个版本有什么区别
  • DINO-R1
  • 商务合同范本智能审核系统 AI 大模型处理方案
  • 探索分布式存储与通信:去中心化共享及通訊(DSAC)
  • 区块链跨链通信:使用 Cosmos SDK 实现链间互操作
  • 手动清理C盘文件的一些方法
  • 共聚焦显微镜—赋能光学元件精密质控
  • C语言获取数组长度方法大全(附带实例)
  • gateway 网关 路由新增 (已亲测)
  • Python训练营打卡 Day44
  • linux shell脚本硬件定时检测通过邮箱警告管理人员