近期在做mcp sse的项目,找了许多例子,大多介绍mcp ,但基本都是在介绍本地stdio 的模式,这对于使用的mcp的用户来讲还是不友好,当然也可以使用桥接的方式使用远程的服务,但还是需要客户本地跑一个stido的server,对于程序员以外更多的用户并不友好,因此在MCP 20241105 版本出现的时候只是关注到了它,在 20250326 版本出现后意味着mcp 准备面对更多的小白用户了,然后就有了本文

首先我用的是py , 这是一个例子, 文件名是 mymcp.py

from starlette.applications import Starlette
from starlette.routing import Mount
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-mcp-server")

# 官方演示方法1
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b
    
# 官方演示方法2
@mcp.tool()
def calculate_bmi(weight_kg: float, height_m: float) -> float:
    """Calculate BMI given weight in kg and height in meters"""
    return weight_kg / (height_m**2)

# 挂载SSE服务器到ASGI服务器上
app = Starlette(
    routes=[
        Mount('/', app=mcp.sse_app()),
    ]
)


if __name__ == "__main__":
    mcp.run("sse")

启动

python3 ./mymcp.py

此处以curror 为例, 配置mcp.json

{
  "mcpServers": {
    "myMcpServer": {
      "url": "http://127.0.0.1:8000/sse"
    }
  }
}

然后使用curror 的agent的模式即可


yangrd
1.3k 声望225 粉丝

代码接管世界,知行合一。