部署
FastMCP 命令行界面
学习如何使用FastMCP命令行界面
FastMCP提供了一个命令行界面(CLI),可以方便地运行、开发和安装您的MCP服务器。当您安装FastMCP时,CLI会自动安装。
命令概览
| 命令 | 用途 | 依赖管理 |
|---|---|---|
run | Run a FastMCP server directly | Uses your current environment; you are responsible for ensuring all dependencies are available |
dev | Run a server with the MCP Inspector for testing | Creates an isolated environment; dependencies must be explicitly specified with --with and/or --with-editable |
install | Install a server in the Claude desktop app | Creates an isolated environment; dependencies must be explicitly specified with --with and/or --with-editable |
version | Display version information | N/A |
命令详情
run
直接运行一个FastMCP服务器。
该命令直接在您当前的Python环境中运行服务器。您需要自行确保所有依赖项可用。
选项
| 选项 | 标志 | 描述 |
|---|---|---|
| Transport | --transport, -t | Transport protocol to use (stdio, streamable-http, or sse) |
| Host | --host | Host to bind to when using http transport (default: 127.0.0.1) |
| Port | --port, -p | Port to bind to when using http transport (default: 8000) |
| Log Level | --log-level, -l | Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
服务器规格
可以通过两种方式指定服务器:
server.py- 导入模块并查找名为mcp、server或app的FastMCP对象。如果找不到该对象则报错。server.py:custom_name- 导入并使用指定的服务器对象
使用fastmcp run时,它会完全忽略if __name__ == "__main__"代码块。相反,它会找到你的服务器对象,并直接使用你指定的传输选项调用其run()方法。这意味着你可以使用fastmcp run来覆盖代码中指定的传输方式。
例如,如果你的代码包含:
无论__main__块中包含什么内容,您都可以使用可流式HTTP传输来运行它:
示例
dev
使用MCP Inspector运行MCP服务器进行测试。
该命令在隔离环境中运行您的服务器。所有依赖项必须使用--with和/或--with-editable选项明确指定。
dev 命令是一个仅通过 STDIO 测试服务器的快捷方式。当检查器启动时,您可能需要:
- 从传输下拉菜单中选择“STDIO”
- 手动连接
此命令不支持HTTP测试。要通过HTTP测试服务器:
- 手动启动您的HTTP传输服务器,可选择以下方式:
或
- 单独打开MCP检查器并连接到您正在运行的服务器
选项
| 选项 | 标志 | 描述 |
|---|---|---|
| Editable Package | --with-editable, -e | Directory containing pyproject.toml to install in editable mode |
| Additional Packages | --with | Additional packages to install (can be used multiple times) |
| Inspector Version | --inspector-version | Version of the MCP Inspector to use |
| UI Port | --ui-port | Port for the MCP Inspector UI |
| Server Port | --server-port | Port for the MCP Inspector Proxy server |
示例
install
在Claude桌面应用中安装MCP服务器。
该命令将您的服务器安装在一个隔离环境中。所有依赖项必须明确使用--with和/或--with-editable选项指定。
install 命令目前仅设置用于 STDIO 传输的服务器。当安装在 Claude 桌面应用中时,无论代码中的传输配置如何,您的服务器都将通过 STDIO 运行。
选项
| 选项 | 标志 | 描述 |
|---|---|---|
| Server Name | --name, -n | Custom name for the server |
| Editable Package | --with-editable, -e | Directory containing pyproject.toml to install in editable mode |
| Additional Packages | --with | Additional packages to install (can be used multiple times) |
| Environment Variables | --env-var, -v | Environment variables in KEY=VALUE format (can be used multiple times) |
| Environment File | --env-file, -f | Load environment variables from a .env file |
示例
version
显示关于FastMCP及相关组件的版本信息。