install-mcpcat-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseIntegrate MCPCat into a Python MCP Server
将MCPCat集成到Python MCP服务器
MCPCat is a one-line SDK integration. It automatically tracks all tool calls, sessions, and user intent. You do NOT add tracking code inside tool functions — MCPCat hooks into the server at the protocol level.
MCPCat是只需一行代码即可集成的SDK。它会自动追踪所有工具调用、会话和用户意图。无需在工具函数内部添加追踪代码——MCPCat会在协议层面与服务器挂钩。
Step 1: Find the MCP server
步骤1:找到MCP服务器
Search all files for these import patterns (check in order):
.py- or
from mcp.server.fastmcp import FastMCP— Official FastMCPfrom mcp.server import FastMCP - — Community FastMCP (different package, needs
from fastmcp import FastMCP)mcpcat[community] - or
from mcp.server.lowlevel import Server— Low-level Serverfrom mcp.server import Server
In the matched file, find the server variable assignment (e.g., or ). Store the variable name.
server = FastMCP(...)app = Server(...)If is already present in the file, tell the user MCPCat is already integrated and stop.
mcpcat.trackIf multiple server instantiations exist, ask the user which one to instrument.
在所有文件中搜索以下导入模式(按顺序检查):
.py- 或
from mcp.server.fastmcp import FastMCP——官方FastMCPfrom mcp.server import FastMCP - ——社区版FastMCP(不同的包,需要安装
from fastmcp import FastMCP)mcpcat[community] - 或
from mcp.server.lowlevel import Server——底层Serverfrom mcp.server import Server
在匹配的文件中,找到服务器变量的赋值语句(例如或),记录变量名称。
server = FastMCP(...)app = Server(...)如果文件中已存在,则告知用户MCPCat已完成集成并停止操作。
mcpcat.track如果存在多个服务器实例化语句,请询问用户需要为哪一个添加监控。
Step 2: Get the project ID
步骤2:获取项目ID
If the user didn't provide a project ID (format: followed by characters), ask them:
proj_What is your MCPCat project ID? (format: proj_xxxxx) Create a free account at https://mcpcat.io to get one.
如果用户未提供项目ID(格式为后跟字符),请询问他们:
proj_你的MCPCat项目ID是什么?(格式:proj_xxxxx) 可访问https://mcpcat.io创建免费账户获取。
Step 3: Check peer dependency
步骤3:检查依赖版本
Check the project's or for the MCP SDK version:
pyproject.tomlrequirements.txt- Official MCP (): Requires
from mcp.server.... If older, tell the user to upgrade.mcp >= 1.2.0 - Community FastMCP (): Requires
from fastmcp import FastMCPand NOTfastmcp >= 2.7.0. If older or on 2.9.x, tell the user to upgrade/change version.2.9.*
If the version can't be determined from project files, run or to check the installed version.
pip show mcppip show fastmcp检查项目的或文件中的MCP SDK版本:
pyproject.tomlrequirements.txt- 官方MCP(导入语句为):要求
from mcp.server...。如果版本较旧,请告知用户进行升级。mcp >= 1.2.0 - 社区版FastMCP(导入语句为):要求
from fastmcp import FastMCP且不能是fastmcp >= 2.7.0版本。如果版本较旧或为2.9.x版本,请告知用户升级或更换版本。2.9.*
如果无法从项目文件中确定版本,可运行或来检查已安装的版本。
pip show mcppip show fastmcpStep 4: Install the package
步骤4:安装包
First check if (or ) is already listed in the project's dependency files ( or ). If it is already listed, skip this step.
mcpcatmcpcat[community]pyproject.tomlrequirements.txtIf not listed, detect the package manager and install:
| Indicator | Command |
|---|---|
| |
| |
| |
| Add |
| Otherwise | Check the project's README and other |
For community FastMCP (import is ), use instead of in all install commands above.
from fastmcp import FastMCP"mcpcat[community]"mcpcatuv addpoetry addpipenv installpip-toolsrequirements.inmcpcat首先检查项目的依赖文件(或)中是否已列出(或)。如果已列出,则跳过此步骤。
pyproject.tomlrequirements.txtmcpcatmcpcat[community]如果未列出,检测包管理器并执行安装:
| 识别标识 | 命令 |
|---|---|
| |
| |
| |
| 将 |
| 其他情况 | 查看项目的README和其他 |
对于社区版FastMCP(导入语句为),请将上述所有安装命令中的替换为。
from fastmcp import FastMCPmcpcat"mcpcat[community]"uv addpoetry addpipenv installpip-toolsrequirements.inmcpcatStep 5: Add the integration code
步骤5:添加集成代码
Make two edits to the server file. Do NOT add tracking code inside tool functions — MCPCat does this automatically.
1. Imports — add at the top with other imports:
python
import os
import mcpcat
from mcpcat import MCPCatOptionsAdd only if not already present.
import os2. Track call — add AFTER the server variable is created, BEFORE or ASGI mounting:
server.run()python
mcpcat.track(SERVER_VAR, "PROJECT_ID", MCPCatOptions(
debug_mode=os.getenv("MCPCAT_DEBUG_MODE", "false").lower() in ("true", "1", "yes", "on")
))Replace with the actual variable name (e.g., , , ) and with the user's project ID.
SERVER_VARserverappmcpPROJECT_ID对服务器文件进行两处修改。无需在工具函数内部添加追踪代码——MCPCat会自动完成此操作。
1. 导入语句——在文件顶部与其他导入语句一起添加:
python
import os
import mcpcat
from mcpcat import MCPCatOptions仅当未在文件中存在时才添加它。
import os2. Track调用——在服务器变量创建之后、或ASGI挂载之前添加:
server.run()python
mcpcat.track(SERVER_VAR, "PROJECT_ID", MCPCatOptions(
debug_mode=os.getenv("MCPCAT_DEBUG_MODE", "false").lower() in ("true", "1", "yes", "on")
))将替换为实际的变量名称(例如、、),将替换为用户的项目ID。
SERVER_VARserverappmcpPROJECT_IDPlacement rules
放置规则
- MUST be after the line that creates the server (e.g., )
server = FastMCP(...) - MUST be before or any ASGI app mounting
server.run() - Can go before or after definitions — both work. Before is canonical.
@server.tool() - If server is created inline (e.g., ), refactor to assign to a variable first
export default FastMCP(...)
- 必须在创建服务器的语句之后(例如)
server = FastMCP(...) - 必须在或任何ASGI应用挂载之前
server.run() - 可以在定义之前或之后——两种方式都有效。通常放在定义之前。
@server.tool() - 如果服务器是内联创建的(例如),请先重构为赋值给一个变量
export default FastMCP(...)
Why this MCPCatOptions pattern is required
为何需要这种MCPCatOptions模式
The env var IS read at module load time, but calls which overrides it. Since defaults to , calling without explicit options silently disables env-var-based debug mode. The pattern above preserves env var control. Debug logs are written to .
MCPCAT_DEBUG_MODEmcpcat.track()set_debug_mode(options.debug_mode)MCPCatOptions.debug_modeFalsetrack()~/mcpcat.logMCPCAT_DEBUG_MODEmcpcat.track()set_debug_mode(options.debug_mode)MCPCatOptions.debug_modeFalsetrack()~/mcpcat.logComplete example
完整示例
A typical FastMCP server after integration:
python
import os
import mcpcat
from mcpcat import MCPCatOptions
from mcp.server.fastmcp import FastMCP
server = FastMCP("weather-server", version="2.1.0")
mcpcat.track(server, "proj_abc123", MCPCatOptions(
debug_mode=os.getenv("MCPCAT_DEBUG_MODE", "false").lower() in ("true", "1", "yes", "on")
))
@server.tool()
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"Weather in {city}: 72F, sunny"
if __name__ == "__main__":
server.run()Notice: zero tracking code inside . MCPCat automatically captures all tool calls at the protocol level.
get_weather集成后的典型FastMCP服务器:
python
import os
import mcpcat
from mcpcat import MCPCatOptions
from mcp.server.fastmcp import FastMCP
server = FastMCP("weather-server", version="2.1.0")
mcpcat.track(server, "proj_abc123", MCPCatOptions(
debug_mode=os.getenv("MCPCAT_DEBUG_MODE", "false").lower() in ("true", "1", "yes", "on")
))
@server.tool()
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"Weather in {city}: 72F, sunny"
if __name__ == "__main__":
server.run()注意:函数内部没有任何追踪代码。MCPCat会在协议层面自动捕获所有工具调用。
get_weatherStep 6: Verify
步骤6:验证
- Read the modified file and confirm the import and call are correctly placed.
track() - Run to verify the package is importable.
python -c "import mcpcat" - If the project uses a linter (ruff, mypy, pyright), run it if configured.
- Tell the user:
MCPCat is integrated. Your server will automatically report analytics when it runs. Enable debug logging:(logs to ~/mcpcat.log) View your dashboard at https://app.mcpcat.ioMCPCAT_DEBUG_MODE=true python your_server.py
- 查看修改后的文件,确认导入语句和调用的位置正确。
track() - 运行以验证包可被正常导入。
python -c "import mcpcat" - 如果项目使用了代码检查工具(ruff、mypy、pyright),若已配置则运行该工具。
- 告知用户:
MCPCat已集成完成。你的服务器运行时会自动上报分析数据。 启用调试日志:(日志会写入~/mcpcat.log) 访问https://app.mcpcat.io查看你的仪表盘MCPCAT_DEBUG_MODE=true python your_server.py
Common mistakes
常见错误
| Mistake | Fix |
|---|---|
| Adding tracking code inside each tool function | MCPCat is automatic. Remove manual tracking. Only |
| Initializing MCPCat before server creation | |
Using | Use |
Using | Defaults to |
| Using wrong env var name | The env var is |
| 错误 | 修复方法 |
|---|---|
| 在每个工具函数内部添加追踪代码 | MCPCat是自动追踪的。移除手动添加的追踪代码。只需保留 |
| 在服务器创建之前初始化MCPCat | |
为社区版FastMCP使用 | 当导入语句为 |
使用 | 默认 |
| 使用错误的环境变量名称 | 正确的环境变量名称是 |