展示HN:PolyMCP – 轻松将Python/TS函数暴露为MCP工具
我构建了PolyMCP,以便轻松地将现有函数暴露为MCP工具,而无需重写逻辑或添加过多的胶水代码。
目标:将“普通”的Python或TypeScript函数立即使其可被MCP客户端(如Claude Desktop、代理、Ollama等)使用。
Python示例:
```python
from polymcp.polymcp_toolkit import expose_tools
def greet(name: str) -> str:
"""打招呼。"""
return f"你好,{name}!"
def add(a: int, b: int) -> int:
"""将两个数字相加。"""
return a + b
app = expose_tools([greet, add], title="我的MCP工具")
```
运行命令:
```
uvicorn server:app --reload
```
MCP端点出现于:
- /mcp/list_tools
- /mcp/invoke
TypeScript示例:
```typescript
import { z } from "zod";
import { tool, exposeTools } from "polymcp";
const uppercaseTool = tool({
name: "uppercase",
description: "将文本转换为大写",
inputSchema: z.object({ text: z.string() }),
function: async ({ text }) => text.toUpperCase(),
});
const app = exposeTools([uppercaseTool], { title: "文本工具" });
app.listen(3000);
```
更“真实”的示例(Python):
```python
import pandas as pd
from polymcp.polymcp_toolkit import expose_tools
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([calculate_commissions], title="商业工具")
```
你将获得:
- 最小修改即可重用现有代码
- 兼容MCP(Claude Desktop、代理、Ollama等)
- 支持HTTP、标准输入输出和WASM
- 自动输入验证
- 基本生产特性(预算、重试、数据脱敏、日志)
- 内置检查器用于测试和监控
安装:
- Python: `pip install polymcp`
- TypeScript: 克隆仓库 → `cd polymcp-ts` → `npm install` → `npm run build`
仓库地址:
[https://github.com/poly-mcp/Polymcp](https://github.com/poly-mcp/Polymcp)
我很好奇如果这么简单,人们会首先暴露什么样的函数。
欢迎反馈。
查看原文
I built PolyMCP to make it trivial to expose existing functions as MCP tools, without rewriting logic or adding much glue code.<p>The goal: take “normal” Python or TypeScript functions and instantly make them usable by MCP clients (Claude Desktop, agents, Ollama, etc.).<p>Python example<p>from polymcp.polymcp_toolkit import expose_tools<p>def greet(name: str) -> str:
"""Say hello."""
return f"Hello, {name}!"<p>def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b<p>app = expose_tools([greet, add], title="My MCP Tools")<p>Run with:<p>uvicorn server:app --reload<p>MCP endpoints appear at:
• /mcp/list_tools
• /mcp/invoke<p>TypeScript example<p>import { z } from "zod";
import { tool, exposeTools } from "polymcp";<p>const uppercaseTool = tool({
name: "uppercase",
description: "Convert text to uppercase",
inputSchema: z.object({ text: z.string() }),
function: async ({ text }) => text.toUpperCase(),
});<p>const app = exposeTools([uppercaseTool], { title: "Text Tools" });
app.listen(3000);<p>More “real” example (Python)<p>import pandas as pd
from polymcp.polymcp_toolkit import expose_tools<p>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")<p>app = expose_tools([calculate_commissions], title="Business Tools")<p>What you get
• Reuse existing code with minimal changes
• MCP-compatible (Claude Desktop, agents, Ollama, etc.)
• HTTP, stdio, and WASM support
• Automatic input validation
• Basic production features (budgets, retries, redaction, logs)
• Built-in inspector for testing and monitoring<p>Install
• Python: pip install polymcp
• TypeScript: clone repo → cd polymcp-ts → npm install → npm run build<p>Repo:
<a href="https://github.com/poly-mcp/Polymcp" rel="nofollow">https://github.com/poly-mcp/Polymcp</a><p>Curious what kind of function people would expose first if it was this easy.
Feedback very welcome.