316 查看 · 2026-07-07 更新
MCP Python Server — API Wrapper
本项目创建了一个MCP(Model Context Protocol)Python服务器,该服务器提供了一个工具来查询外部API。兼容支持MCP协议的Claude Desktop或ChatGPT Desktop。
✨ 特性
- 通过MCP暴露一个工具(tool)
- 对外部API进行HTTP查询
- 直接通过claude.json与Claude/Desktop集成
🚀 要求
- Python 3.9+
- mcp[cli] (可通过pip或uv安装)
- 支持MCP的Claude或ChatGPT Desktop
📁 项目结构
. ├── servidores/profile.py # 包含用于与我的简历后端交互的MCP服务器和工具 ├── server.py # 包含"consultar_api"工具的MCP服务器 ├── .env # 可选的认证/API变量 ├── claude.json # 直接集成所需的MCP配置 └── README.md # 此文档
⚙️ 安装
使用pip
bash pip install "mcp[cli]"
使用uv(推荐)
bash uv init mcp-api-server cd mcp-api-server uv add "mcp[cli]"
MCP安装
bash mcp install mi_script.py
带有.env文件的安装
bash mcp install mi_script.py -f .env
依赖项安装
bash pip install -r requirements.txt
环境变量
在项目根目录下创建一个.env文件以定义可选环境变量:
env
.env
API_KEY=mi_api_key API_URL=https://miapi.com/consulta
👷 快速开始 (Quickstart)
创建server.py服务器
python from mcp.server.fastmcp import FastMCP import httpx
mcp = FastMCP("API Wrapper")
@mcp.tool(description="查询外部API") async def consultar_api(param: str) -> str: """使用参数查询外部API并返回响应。""" async with httpx.AsyncClient() as client: r = await client.get(f"https://miapi.com/consulta?param={param}") return r.text
在本地开发模式下运行
bash mcp dev server.py
在生产模式下运行
bash mcp run server.py
或者使用uv: bash uv run --with mcp[cli] mcp run server.py
🚀 与Claude/Desktop集成
将claude.json放置于Claude/Desktop的配置文件夹中:
- 在Windows: %APPDATA%Claudeclaude.json
- 在Linux/macOS: ~/.claude/claude.json
示例: json { "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/codigo/backend-curso-inkor/proyectos_memes" ] }, "Demo": { "command": "uv", "args": [ "run", "--with", "mcp[cli]", "mcp", "run", "C:\codigo\backend-curso-inkor\MCP\server.py" ] } } }
---## 🤖 在 Claude/Desktop 中使用
你可以要求模型:
使用带有参数 "ping" 的 consultar_api 工具
然后模型将使用你的 MCP 服务器进行实时 HTTP 调用。
🎁 奖励:工具扩展
python @mcp.tool() async def traducir(texto: str, lang: str) -> str: return f"已翻译: {texto} → {lang}"
🔍 资源
- MCP 官方文档: https://docs.mcp.run/
- Python SDK 仓库: https://github.com/modelcontextprotocol/mcp
✅ 用心制作并使用 httpx 🚀
来源
- 来源:github
- 链接:https://github.com/cdryampi/MCP