FastMCP服务器工具

FastMCP是一个用于创建和运行远程计算服务器的Python工具,支持HTTP和STDIO传输,适用于开发数学工具和API服务。

  • by sshailesh49
  • Python
  • 混合部署
  • 更新于
在 GitHub 查看 sshailesh49/mcp-remote-server

使用说明

1.️⃣ 安装系统必备组件(Ubuntu)

 sudo apt update
 sudo apt install -y python3 python3-venv python3-pip curl

检查Python版本(必须≥3.10):

 python3 --version

2.️⃣ 安装uv(推荐)

注意:紫外线比pip+venv更快、更清洁。

  curl -LsSf https://astral.sh/uv/install.sh | sh

重新加载外壳:

 source ~/.bashrc

验证:

uv --version

3.️⃣ 创建项目目录

mkdir remote-server
cd remote-server

初始化项目(可选但干净)

uv init .    

这将创建:pyproject.toml.python版本(可选)

在.toml文件中添加depandancy

   ''' [project]
   name = "remote-server"
   version = "0.1.0"
   requires-python = ">=3.10"

  dependencies = [
       "fastmcp>=2.14.1",
   ]
...

安装依赖项(这是uv命令)

 uv sync 

这有什么作用

✔ Creates .venv/ automatically
✔ Installs all dependencies
✔ Generates uv.lock
✔ Uses correct Python version

,,,

或

###################################################################

可选使用uv创建虚拟环境

 uv venv

这将创建:

.venv/

激活它:

source .venv/bin/activate

在venv中添加包

  uv add fastmcp

从pyproject.toml安装依赖项

 uv pip install -r pyproject.toml

############################################################

确认:

 which python

/home/ram/remote server/.venv/bin/python#你应该看到这样的内容:

验证是否安装了FastMCP:

python -c "import fastmcp; print(fastmcp.__version__)"

7.️⃣ 创建main.py

 vim main.py

粘贴已更正且兼容的代码

        from fastmcp import FastMCP
        import random
        import json

        # Create the FastMCP server instance
        mcp = FastMCP("Simple Calculator Server")

        # Tool: Add three numbers
        @mcp.tool
        def add(a: int, b: int, c: int) -> int:
            """Add three numbers together."""
            return a + b + c

        # Tool: Generate a random number
        @mcp.tool
        def random_number(min_val: int = 1, max_val: int = 100) -> int:
            """Generate a random number within a range."""
            return random.randint(min_val, max_val)

        # Resource: Server information
        @mcp.resource("info://server")
        def server_info() -> str:
            info = {
                "name": "Simple Calculator Server",
                "version": "1.0.0",
                "description": "A basic MCP server with math tools",
                "tools": ["add", "random_number"],
                "author": "Ram"
            }
            return json.dumps(info, indent=2)

        if __name__ == "__main__":
           #  mcp.run()   #  for stdio
           mcp.run(transport="http", host="0.0.0.0", port=8000)    #   for stream-http


Save and exit.

运行服务器(仅限uv)

选项A——通过激活的venv

  uv run python main.py

选项B-不激活venv(推荐)

 uv run main.py

验证安装包(仅限紫外线)

 uv pip list 

####################################################################################################################################################################################################################################################################

逐步快速MCP设置(无紫外线)

1️ 安装系统必备组件

 sudo apt update
 sudo apt install -y python3 python3-venv python3-pip curl

检查Python版本(必须≥3.10):

 python3 --version

2️ 创建项目目录

  mkdir remote-server
  cd remote-server

3️ 创建虚拟环境

 python3 -m venv .venv

激活它:

source .venv/bin/activate

4验证:

which python

您应该看到类似以下内容:

/home/ram/远程服务器/.venv/bin/python

4️ 安装FastMCP

pip install --upgrade pip
pip install fastmcp>=2.14.1

验证安装:

python -c "import fastmcp; print(fastmcp.__version__)"

5️ 创建main.py

vim main.py

粘贴您的FastMCP代码:

,,,
    from fastmcp import FastMCP
    import random
    import json

    # Create the FastMCP server instance
    mcp = FastMCP("Simple Calculator Server")

    # Tool: Add three numbers
    @mcp.tool
    def add(a: int, b: int, c: int) -> int:
        return a + b + c

    # Tool: Generate a random number
    @mcp.tool
    def random_number(min_val: int = 1, max_val: int = 100) -> int:
        return random.randint(min_val, max_val)

    # Resource: Server information
    @mcp.resource("info://server")
    def server_info() -> str:
        return json.dumps({
            "name": "Simple Calculator Server",
            "version": "1.0.0",
            "tools": ["add", "random_number"],
            "author": "Ram"
        }, indent=2)

    if __name__ == "__main__":
        # For standard input/output STDIO 
        # mcp.run()
        # For HTTP streaming
        mcp.run(transport="http", host="0.0.0.0", port=8000)
,,,

保存并退出。

6️ 运行MCP服务器

python main.py

您应该看到:

信息FastMCP服务器正在运行 信息工具已注册:添加,随机编号 已注册的信息资源:info://server

7️ 验证依赖关系(可选)

 pip list

检查是否安装了fastmcp。

要停用venv,请执行以下操作:

 deactivate

####################################################################################################################################################################################################################################################################

使用CURSOR进行测试和部署https://fastmcp.cloud/

    ,,, {
    "mcpServers": {

        "remote-server": {
        "transport": "http",
        "url": "https://bluech-remote-mcp-server.fastmcp.app/mcp"
        }  
    }
    }


    {
    "mcpServers": {
        "local-mcp": {
        "transport": "stdio",
        "command": "/mnt/c/agya/mcp/remote-server/.venv/bin/python3",
        "args": [
            "/mnt/c/agya/mcp/remote-server/main.py"
        ],
        "cwd": "/mnt/c/agya/mcp/remote-server"
        }
    }
    }

,,,

1.️⃣ 安装系统必备组件(Ubuntu)

sudo apt更新 sudo apt安装-y python3 python3 venv python3 pip curl

检查Python版本(必须≥3.10):

python3--版本

2.️⃣ 安装uv(推荐)

注意:紫外线比pip+venv更快、更清洁。

curl-LsSfhttps://astral.sh/uv/install.sh|sh

重新加载外壳:

源码~/.bashrc

验证:

uv版本

3.️⃣ 创建项目目录

mkdir远程服务器 cd远程服务器

初始化项目(可选但干净)

紫外线初始化。

这将创建:pyproject.toml.python版本(可选)

在.toml文件中添加depandancy

   ''' [project]
   name = "remote-server"
   version = "0.1.0"
   requires-python = ">=3.10"

  dependencies = [
       "fastmcp>=2.14.1",
   ]
...

安装依赖项(这是uv命令)

紫外同步

这有什么作用

✔ Creates .venv/ automatically
✔ Installs all dependencies
✔ Generates uv.lock
✔ Uses correct Python version

,,,

或

###################################################################

可选使用uv创建虚拟环境

紫外线真空

这将创建:

.venv/

激活它:

source.ven/bin/activate

在venv中添加包

uv添加fastmcp

从pyproject.toml安装依赖项

uv pip安装-r pyproject.toml

############################################################

确认:

哪个python

/home/ram/remote server/.venv/bin/python#你应该看到这样的内容:

验证是否安装了FastMCP:

python-c“导入fastmcp;打印(fastmcp。版本)"

7.️⃣ 创建main.py

vim main.py

粘贴已更正且兼容的代码

        from fastmcp import FastMCP
        import random
        import json

        # Create the FastMCP server instance
        mcp = FastMCP("Simple Calculator Server")

        # Tool: Add three numbers
        @mcp.tool
        def add(a: int, b: int, c: int) -> int:
            """Add three numbers together."""
            return a + b + c

        # Tool: Generate a random number
        @mcp.tool
        def random_number(min_val: int = 1, max_val: int = 100) -> int:
            """Generate a random number within a range."""
            return random.randint(min_val, max_val)

        # Resource: Server information
        @mcp.resource("info://server")
        def server_info() -> str:
            info = {
                "name": "Simple Calculator Server",
                "version": "1.0.0",
                "description": "A basic MCP server with math tools",
                "tools": ["add", "random_number"],
                "author": "Ram"
            }
            return json.dumps(info, indent=2)

        if __name__ == "__main__":
           #  mcp.run()   #  for stdio
           mcp.run(transport="http", host="0.0.0.0", port=8000)    #   for stream-http


Save and exit.

运行服务器(仅限uv)

选项A——通过激活的venv

uv运行python main.py

选项B-不激活venv(推荐)

uv运行main.py

验证安装包(仅限紫外线)

uv pip列表

####################################################################################################################################################################################################################################################################

逐步快速MCP设置(无紫外线)

1️ 安装系统必备组件

sudo apt更新 sudo apt安装-y python3 python3 venv python3 pip curl

检查Python版本(必须≥3.10):

python3--版本

2️ 创建项目目录

mkdir remote-server
cd remote-server

3️ 创建虚拟环境

python3-m视频

激活它:

source.ven/bin/activate

4验证:

哪个python

您应该看到类似以下内容:

/home/ram/远程服务器/.venv/bin/python

4️ 安装FastMCP

pip install --upgrade pip
pip install fastmcp>=2.14.1

验证安装:

python-c“导入fastmcp;打印(fastmcp。版本)"

5️ 创建main.py

vim main.py

粘贴您的FastMCP代码:

,,,
    from fastmcp import FastMCP
    import random
    import json

    # Create the FastMCP server instance
    mcp = FastMCP("Simple Calculator Server")

    # Tool: Add three numbers
    @mcp.tool
    def add(a: int, b: int, c: int) -> int:
        return a + b + c

    # Tool: Generate a random number
    @mcp.tool
    def random_number(min_val: int = 1, max_val: int = 100) -> int:
        return random.randint(min_val, max_val)

    # Resource: Server information
    @mcp.resource("info://server")
    def server_info() -> str:
        return json.dumps({
            "name": "Simple Calculator Server",
            "version": "1.0.0",
            "tools": ["add", "random_number"],
            "author": "Ram"
        }, indent=2)

    if __name__ == "__main__":
        # For standard input/output STDIO 
        # mcp.run()
        # For HTTP streaming
        mcp.run(transport="http", host="0.0.0.0", port=8000)
,,,

保存并退出。

6️ 运行MCP服务器

python main.py

您应该看到:

信息FastMCP服务器正在运行 信息工具已注册:添加,随机编号 已注册的信息资源:info://server

7️ 验证依赖关系(可选)

pip列表

检查是否安装了fastmcp。

8️ 项目结构

远程服务器/

要停用venv,请执行以下操作:

停用 ####################################################################################################################################################################################################################################################################

使用CURSOR进行测试和部署https://fastmcp.cloud/

    ,,, {
    "mcpServers": {

        "remote-server": {
        "transport": "http",
        "url": "https://bluech-remote-mcp-server.fastmcp.app/mcp"
        }  
    }
    }


    {
    "mcpServers": {
        "local-mcp": {
        "transport": "stdio",
        "command": "/mnt/c/agya/mcp/remote-server/.venv/bin/python3",
        "args": [
            "/mnt/c/agya/mcp/remote-server/main.py"
        ],
        "cwd": "/mnt/c/agya/mcp/remote-server"
        }
    }
    }

,,,

########################################################################################################################

本地测试

########################################################################################################################

创建Docker镜像:

 docker build --no-cache -t server-mcp-uv .

创建docker容器:

 docker run -d -p 8000:8000 --name fastmcp fastmcp-uv

你想揭露http://localhost:8000使用Ngrok(来自WSL/Ubuntu)。

在WSL(Ubuntu)中安装Ngrok

curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc \
       | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null
       

echo "deb https://ngrok-agent.s3.amazonaws.com buster main" \
    | sudo tee /etc/apt/sources.list.d/ngrok.list

sudo apt update
sudo apt install -y ngrok

验证:

ngrok version

添加您的Ngrok认证令牌(重要)

  Go to  https://dashboard.ngrok.com/get-started/your-authtoken

  Copy your token

运行:

ngrok config add-authtoken YOUR_TOKEN_HERE

启动MCP服务器(端口8000)

python main.py

确保你看到了以下内容:

Running on http://0.0.0.0:8000

本地检查:

     curl http://localhost:8000

使用Ngrok公开localhost:8000

ngrok http 8000

使用公共Ngrok URL

您将看到如下输出:

转发https://abcd-12-34-56.ngrok-free.app -> http://localhost:8000

您的MCP服务器现在位于:

https://abcd-12-34-56.ngrok-free.app/mcp