Show HN: Polymcp – 将任意 Python 函数转化为 AI 代理的 MCP 工具
5 分•作者: justvugg•24 天前
我构建了 Polymcp,这是一个框架,可以将任何 Python 函数转换为 MCP(模型上下文协议)工具,供 AI 智能体使用。无需重写,无需复杂的集成。
示例
简单函数:
```python
from polymcp.polymcp_toolkit import expose_tools_http
def add(a: int, b: int) -> int:
"""将两个数字相加"""
return a + b
app = expose_tools_http([add], title="数学工具")
```
运行方式:
```bash
uvicorn server_mcp:app --reload
```
现在,add 函数通过 MCP 暴露出来,可以被 AI 智能体直接调用。
API 函数:
```python
import requests
from polymcp.polymcp_toolkit import expose_tools_http
def get_weather(city: str):
"""返回城市当前的的天气数据"""
response = requests.get(f"https://api.weatherapi.com/v1/current.json?q={city}")
return response.json()
app = expose_tools_http([get_weather], title="天气工具")
```
AI 智能体可以调用 `get_weather("London")` 立即获取实时天气数据。
业务工作流函数:
```python
import pandas as pd
from polymcp.polymcp_toolkit import expose_tools_http
def calculate_commissions(sales_data: list[dict]):
"""从销售数据中计算销售佣金"""
df = pd.DataFrame(sales_data)
df["commission"] = df["sales_amount"] * 0.05
return df.to_dict(orient="records")
app = expose_tools_http([calculate_commissions], title="业务工具")
```
AI 智能体现在可以自动生成佣金报告。
它对企业的重要性
* 立即重用现有代码:遗留脚本、内部库、API。
* 自动化复杂工作流程:AI 可以可靠地编排多个工具。
* 即插即用:在同一个 MCP 服务器上暴露多个 Python 函数。
* 减少开发时间:无需自定义包装器或中间件。
* 内置可靠性:包含输入/输出验证和错误处理。
Polymcp 使 Python 函数立即可供 AI 智能体使用,从而实现跨企业软件的标准化集成。
代码库:[https://github.com/poly-mcp/Polymcp](https://github.com/poly-mcp/Polymcp)
查看原文
I built Polymcp, a framework that allows you to transform any Python function into an MCP (Model Context Protocol) tool ready to be used by AI agents. No rewriting, no complex integrations.<p>Examples<p>Simple function:<p>from polymcp.polymcp_toolkit import expose_tools_http<p>def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b<p>app = expose_tools_http([add], title="Math Tools")<p>Run with:<p>uvicorn server_mcp:app --reload<p>Now add is exposed via MCP and can be called directly by AI agents.<p>API function:<p>import requests
from polymcp.polymcp_toolkit import expose_tools_http<p>def get_weather(city: str):
"""Return current weather data for a city"""
response = requests.get(f"<a href="https://api.weatherapi.com/v1/current.json?q={city}" rel="nofollow">https://api.weatherapi.com/v1/current.json?q={city}</a>")
return response.json()<p>app = expose_tools_http([get_weather], title="Weather Tools")<p>AI agents can call get_weather("London") to get real-time weather data instantly.<p>Business workflow function:<p>import pandas as pd
from polymcp.polymcp_toolkit import expose_tools_http<p>def calculate_commissions(sales_data: list[dict]):
"""Calculate sales commissions from sales data"""
df = pd.DataFrame(sales_data)
df["commission"] = df["sales_amount"] * 0.05
return df.to_dict(orient="records")<p>app = expose_tools_http([calculate_commissions], title="Business Tools")<p>AI agents can now generate commission reports automatically.<p>Why it matters for companies
• Reuse existing code immediately: legacy scripts, internal libraries, APIs.
• Automate complex workflows: AI can orchestrate multiple tools reliably.
• Plug-and-play: multiple Python functions exposed on the same MCP server.
• Reduce development time: no custom wrappers or middleware needed.
• Built-in reliability: input/output validation and error handling included.<p>Polymcp makes Python functions immediately usable by AI agents, standardizing integration across enterprise software.<p>Repo: <a href="https://github.com/poly-mcp/Polymcp" rel="nofollow">https://github.com/poly-mcp/Polymcp</a>