FastMCP提供了一个命令行界面(CLI),可以方便地运行、开发和安装您的MCP服务器。当您安装FastMCP时,CLI会自动安装。

fastmcp --help

命令概览

命令用途依赖管理
runRun a FastMCP server directlyUses your current environment; you are responsible for ensuring all dependencies are available
devRun a server with the MCP Inspector for testingCreates an isolated environment; dependencies must be explicitly specified with --with and/or --with-editable
installInstall a server in the Claude desktop appCreates an isolated environment; dependencies must be explicitly specified with --with and/or --with-editable
versionDisplay version informationN/A

命令详情

run

直接运行一个FastMCP服务器。

fastmcp run server.py

该命令直接在您当前的Python环境中运行服务器。您需要自行确保所有依赖项可用。

选项

选项标志描述
Transport--transport, -tTransport protocol to use (stdio, streamable-http, or sse)
Host--hostHost to bind to when using http transport (default: 127.0.0.1)
Port--port, -pPort to bind to when using http transport (default: 8000)
Log Level--log-level, -lLog level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

服务器规格

可以通过两种方式指定服务器:

  1. server.py - 导入模块并查找名为mcpserverapp的FastMCP对象。如果找不到该对象则报错。
  2. server.py:custom_name - 导入并使用指定的服务器对象

使用fastmcp run时,它会完全忽略if __name__ == "__main__"代码块。相反,它会找到你的服务器对象,并直接使用你指定的传输选项调用其run()方法。这意味着你可以使用fastmcp run来覆盖代码中指定的传输方式。

例如,如果你的代码包含:

# server.py
from fastmcp import FastMCP

mcp = FastMCP("MyServer")

@mcp.tool()
def hello(name: str) -> str:
    return f"Hello, {name}!"

if __name__ == "__main__":
    # This is ignored when using `fastmcp run`!
    mcp.run(transport="stdio")

无论__main__块中包含什么内容,您都可以使用可流式HTTP传输来运行它:

fastmcp run server.py --transport streamable-http --port 8000

示例

# Run a server with Streamable HTTP transport on a custom port
fastmcp run server.py --transport streamable-http --port 8000

dev

使用MCP Inspector运行MCP服务器进行测试。

fastmcp dev server.py

该命令在隔离环境中运行您的服务器。所有依赖项必须使用--with和/或--with-editable选项明确指定。

dev 命令是一个仅通过 STDIO 测试服务器的快捷方式。当检查器启动时,您可能需要:

  1. 从传输下拉菜单中选择“STDIO”
  2. 手动连接

此命令不支持HTTP测试。要通过HTTP测试服务器:

  1. 手动启动您的HTTP传输服务器,可选择以下方式:
    fastmcp run server.py --transport streamable-http
    
    python server.py  # 假设您的__main__模块设置了HTTP传输
    
  2. 单独打开MCP检查器并连接到您正在运行的服务器

选项

选项标志描述
Editable Package--with-editable, -eDirectory containing pyproject.toml to install in editable mode
Additional Packages--withAdditional packages to install (can be used multiple times)
Inspector Version--inspector-versionVersion of the MCP Inspector to use
UI Port--ui-portPort for the MCP Inspector UI
Server Port--server-portPort for the MCP Inspector Proxy server

示例

# Run dev server with editable mode and additional packages
fastmcp dev server.py -e . --with pandas --with matplotlib

install

在Claude桌面应用中安装MCP服务器。

fastmcp install server.py

该命令将您的服务器安装在一个隔离环境中。所有依赖项必须明确使用--with和/或--with-editable选项指定。

install 命令目前仅设置用于 STDIO 传输的服务器。当安装在 Claude 桌面应用中时,无论代码中的传输配置如何,您的服务器都将通过 STDIO 运行。

选项

选项标志描述
Server Name--name, -nCustom name for the server
Editable Package--with-editable, -eDirectory containing pyproject.toml to install in editable mode
Additional Packages--withAdditional packages to install (can be used multiple times)
Environment Variables--env-var, -vEnvironment variables in KEY=VALUE format (can be used multiple times)
Environment File--env-file, -fLoad environment variables from a .env file

示例

# Install server with custom name, dependencies, and environment variables
fastmcp install server.py -n "My Analysis Server" -e . --with pandas --env-var API_KEY=12345

version

显示关于FastMCP及相关组件的版本信息。

fastmcp version