展示 HN:Polymcp – 将任何 Python 函数转换为 AI 代理的 MCP 工具

2作者: justvugg7 天前原帖
我构建了 Polymcp,这是一个框架,可以将任何 Python 函数转换为可供 AI 代理使用的 MCP(模型上下文协议)工具。无需重写,无需复杂的集成。 <p>示例</p> <p>简单函数:</p> ```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="数学工具") ``` <p>运行命令:</p> ``` uvicorn server_mcp:app --reload ``` <p>现在,add 函数通过 MCP 暴露,可以被 AI 代理直接调用。</p> <p>API 函数:</p> ```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="天气工具") ``` <p>AI 代理可以调用 get_weather("London") 来即时获取实时天气数据。</p> <p>业务工作流函数:</p> ```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="业务工具") ``` <p>AI 代理现在可以自动生成佣金报告。</p> <p>对企业的重要性:</p> - 立即重用现有代码:遗留脚本、内部库、API。 - 自动化复杂工作流:AI 可以可靠地协调多个工具。 - 即插即用:多个 Python 函数可以在同一 MCP 服务器上暴露。 - 减少开发时间:无需自定义包装器或中间件。 - 内置可靠性:包含输入/输出验证和错误处理。 <p>Polymcp 使 Python 函数能够被 AI 代理立即使用,标准化了企业软件的集成。</p> <p>代码库:<a href="https://github.com/poly-mcp/Polymcp" rel="nofollow">https://github.com/poly-mcp/Polymcp</a></p>
查看原文
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) -&gt; int: &quot;&quot;&quot;Add two numbers&quot;&quot;&quot; return a + b<p>app = expose_tools_http([add], title=&quot;Math Tools&quot;)<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): &quot;&quot;&quot;Return current weather data for a city&quot;&quot;&quot; response = requests.get(f&quot;<a href="https:&#x2F;&#x2F;api.weatherapi.com&#x2F;v1&#x2F;current.json?q={city}" rel="nofollow">https:&#x2F;&#x2F;api.weatherapi.com&#x2F;v1&#x2F;current.json?q={city}</a>&quot;) return response.json()<p>app = expose_tools_http([get_weather], title=&quot;Weather Tools&quot;)<p>AI agents can call get_weather(&quot;London&quot;) 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]): &quot;&quot;&quot;Calculate sales commissions from sales data&quot;&quot;&quot; df = pd.DataFrame(sales_data) df[&quot;commission&quot;] = df[&quot;sales_amount&quot;] * 0.05 return df.to_dict(orient=&quot;records&quot;)<p>app = expose_tools_http([calculate_commissions], title=&quot;Business Tools&quot;)<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&#x2F;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:&#x2F;&#x2F;github.com&#x2F;poly-mcp&#x2F;Polymcp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;poly-mcp&#x2F;Polymcp</a>